Skip to content

beforeReturn: the approval's modifications.output is returned without the post-approval check or the output guardrails #366

Closed

Description

@jddunn

maybeApproveFinalResult() runs the post-approval check over the strategy's final text (src/api/agency.ts L1512-1531 at 7cc36c9) and then returns the approval's modifications.output in its place (L1533-1535). finalizeExecutionResult() runs the agency's guardrails.output and parses opts.output before it asks for the approval (L273-279, L292). So the text a beforeReturn approval writes reaches the caller without code-safety, pii-redaction or the agency's output guardrails, and parsed holds the strategy's text rather than the replacement. docs/safety/HUMAN_IN_THE_LOOP.md and the HitlConfig.guardrailOverride JSDoc state that the replacement is not checked. #321 (fixed in #340) closed the same gap for a beforeAgent approval's modifications.instructions.

Failing sequence: hitl.approvals.beforeReturn: true with a custom handler or hitl.webhook() that answers { approved: true, modifications: { output: 'Run rm -rf / to finish.' } }; generate() returns that text and guardrailHitlOverride never fires. (hitl.llmJudge() returns no modifications, so it does not reach this path.)

Fix direction: check the replacement before returning it: run the post-approval check over the text the caller receives (the replacement when the approval carries one, as #340 does for instructions), and run the output guardrails and the structured parse over that text too. A test through agency() with a stub model: an approval whose replacement the check blocks rejects generate() with the override, and one whose replacement passes is returned with parsed read from it.

Activity

  1. jddunn commented on Oct 13, 2026

    @jddunn
    MemberAuthor

    #393 (squash c1e716a2) landed the check half of this issue. maybeApproveFinalResult() now sends a beforeReturn approval's modifications.output through the agency's guardrails.output, and one post-approval check then reads { output, returned } (agency.ts L1514-1551). A replacement the check blocks fires guardrailHitlOverride (return) and rejects as a blocked final text does. Tests are in src/api/__tests__/agency-return-approval.e2e.test.ts.

    The parsed half stays open. finalizeExecutionResult() parses opts.output from the strategy's text before it asks for the approval (L277-279, approval at L292), so after a replacement parsed still describes the text that was replaced.

    Recommended design: parse after the approval, from the text the caller receives. When an approval replaced the text and the replacement does not parse, return it with parsed undefined and do not retry. Otherwise the generate() validation retry (L392-405) would run the strategy again, throw away the approver's text and ask for the approval a second time. Test through agency() with opts.output and a stub model: a replacement that matches the schema comes back with parsed read from it, and one that does not comes back once, with no second strategy run.

  2. jddunn commented on Oct 28, 2026

    @jddunn
    MemberAuthor

    The parsed half landed in #624 (squash 8f8ae61) and is released in @framers/agentos 0.15.31.

    parsed is now read from the text the caller receives, after the beforeReturn approval and, in an agency with a pool, after the pool's mask. A replacement that matches the output schema is parsed, so the result of generate(), the final-output stream part and stream().parsed carry its value. A replacement that does not match is returned once, with parsed undefined and no retry: a retry would throw the approver's text away and ask for the approval again. The agency writes an agency_replacement_unparsed line through the host's logger when that happens. A replacement equal to the strategy's text counts as an approval without modifications, so generate() retries a text that fails the schema as before; stream() runs no validation retry in either case. In an agency without an output schema, a parsed value the strategy's result carried is dropped once a replacement changes the text.

    With the check half from #393 (c1e716a), both parts of this issue are on master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions