Skip to content

feat: run out-of-scope guardrail concurrently with the supreme agent #497 - #508

Open
ypldan wants to merge 9 commits into
developmentfrom
perf/497-optimistic-guardrails
Open

ypldan wants to merge 9 commits into
developmentfrom
perf/497-optimistic-guardrails

Conversation

@ypldan

@ypldan ypldan commented Jul 2, 2026

Copy link
Copy Markdown

Applicable issues

Description of changes

The out-of-scope checker ran before the supreme agent, adding an LLM round-trip to time-to-first-token even though verdicts are almost always "in scope". When a channel opts in, the checker and agent now run concurrently: the agent runs speculatively against a recording choice, a copied history, and a deepcopied state, and only commits once the verdict confirms "in scope". Out of scope, the speculative run is cancelled and its output discarded, so the user sees exactly the sequential flow's output — minus one round-trip of latency.

  • SpeculationRecordingChoice/RecordingStage record every choice write; on an in-scope verdict the buffer is flushed to the real choice and history/state committed, otherwise it is discarded. Irreversible side effects (real tool dispatch) wait on a permission gate.
  • Isolation by substitution — the agent only ever holds speculative surfaces (recording choice, copied history/state, adopt_stage-wrapped performance stage), so nothing can leak to the real choice before commit. The substitution is built in one place (_build_speculative_inputs) and a unit test guards the invariant by rejecting any un-substituted choice/stage surface on the speculative inputs.
  • Cleanup guarantee — an async with context manager reaps both tasks and drops any un-flushed output on every exit (commit, out-of-scope, checker failure, cancellation, or a mid-replay flush failure).
  • Observability — committed vs discarded speculation is logged; a speculative agent failure on the out-of-scope path is surfaced rather than swallowed.
  • Per-channel toggleout_of_scope.optimistic (default off); off keeps the exact sequential check-then-agent chain.

Checklist

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

@ypldan ypldan changed the title perf: run out-of-scope guardrail concurrently with the supreme agent feat: run out-of-scope guardrail concurrently with the supreme agent Jul 2, 2026
@ypldan ypldan changed the title feat: run out-of-scope guardrail concurrently with the supreme agent perf: run out-of-scope guardrail concurrently with the supreme agent Jul 2, 2026
@ypldan ypldan changed the title perf: run out-of-scope guardrail concurrently with the supreme agent feat: run out-of-scope guardrail concurrently with the supreme agent Jul 2, 2026
Daniil Yarmalkevich added 6 commits July 9, 2026 11:56
…istic-guardrails

# Conflicts:
#	statgpt/app/chains/main.py
Address review findings on the concurrent out-of-scope guardrail:

- Move the toggle from DIAL_APP_OPTIMISTIC_GUARDRAILS to a per-channel
  out_of_scope.optimistic flag (default off)
- Route the shared performance stage through the recording via
  adopt_stage, so speculative debug rows are held back until commit;
  drop the second event gate in the agent
- Reap tasks and drop un-flushed output on every exit via an async
  context manager (fixes a live, ungated agent task after a mid-replay
  flush failure)
- Log committed vs discarded speculation; surface a speculative agent
  failure that was silently swallowed on the out-of-scope path
- Extract a shared check_with_stage; rename BufferedChoice/BufferedStage
  -> RecordingChoice/RecordingStage, OOS_VERDICT_EVENT -> SIDE_EFFECT_GATE,
  resolve_skip -> try_short_circuit
- Note at spec_inputs that any real-choice object must be substituted or
  routed through the recording, or speculative writes leak before commit
- Narrow _await_side_effect_gate docstring: it gates the main-loop tool
  dispatch; fake tool calls run ungated
Post-review hardening and cleanup of the optimistic out-of-scope guardrails,
no user-facing behavior change.

- Extract MainChainFactory._build_speculative_inputs() and split the
  orchestrator into _commit_speculation/_abort_speculation; add a tripwire test
  that rejects any un-substituted choice/stage write surface on spec_inputs.
- Collapse the double try_short_circuit on the checker-only path; rename
  try_short_circuit -> resolve_short_circuit (returns bool).
- Add ChainParameters.get_side_effect_gate accessor; type recording_choice
  proxies with StageI.
- Deduplicate the start-new-conversation threshold check into
  _maybe_start_new_conversation.
- Document the side-effect-free constraint on FakeCall/fake tool calls, the
  state deepcopy-ability contract, and the speculative-flow divergences; trim
  verbose comments to one-line invariant notes.
@ypldan
ypldan marked this pull request as ready for review July 9, 2026 12:51
@ypldan

ypldan commented Jul 9, 2026

Copy link
Copy Markdown
Author

/deploy-review

GitHub actions run: 29020787991

Stage Status
deploy-review Success ✅
matrix.application Skipped ➖

@ypldan ypldan changed the title feat: run out-of-scope guardrail concurrently with the supreme agent feat: run out-of-scope guardrail concurrently with the supreme agent #497 Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Run the out-of-scope guardrail concurrently with the supreme agent

1 participant