Repository navigation
fix(guardrails): stop yolo sessions waiting on the guardrail checker #2519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rdimitrov
merged 4 commits into
main
from
claude/mecatl-performance-investigation-7372a3
Oct 26, 2026
+836
−42
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9f0adfd
fix(guardrails): stop yolo sessions waiting on the guardrail checker
rdimitrov c260ac9
refactor(guardrails): simplify yolo background reviews and fix run-en…
rdimitrov 7acd9a2
fix(guardrails): keep yolo review set allocation-free and de-flake ca…
rdimitrov d1f3b21
fix(guardrails): snapshot evidence, cap and order yolo background rev…
rdimitrov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| package agent | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/stacklok/mecatl/engine/governance" | ||
| "github.com/stacklok/mecatl/engine/port" | ||
| "github.com/stacklok/mecatl/engine/session" | ||
| "github.com/stacklok/mecatl/engine/tool" | ||
| ) | ||
|
|
||
| // backgroundReviewDrainGrace bounds how long run end waits for cancelled yolo | ||
| // advisory reviews to unwind. A var only as a TEST seam, like childDrainCap. | ||
| var backgroundReviewDrainGrace = 1 * time.Second | ||
|
|
||
| // maxBackgroundReviews caps concurrent advisory checker runs per run, bounding | ||
| // provider load, cost, and held evidence on read-heavy runs. | ||
| const maxBackgroundReviews = 4 | ||
|
|
||
| // backgroundReviews is a run's set of opted-in yolo advisory reviews. Each | ||
| // review snapshots its request and evidence on the dispatching goroutine, then | ||
| // runs the checker off the dispatch path on a context detached from the run's | ||
| // own cancellation. At most maxBackgroundReviews run at once; a review over the | ||
| // cap is skipped. Advisory verdicts never feed the shared trajectory, so | ||
| // enforcing reviews of delegated children see the same facts in dispatch order. Run end cancels them; their advisory events go out through the | ||
| // child registry's sealed emit, so none follows the run's terminal event. The | ||
| // context is created on first use, so a run without background reviews pays | ||
| // no allocations for the set. | ||
| type backgroundReviews struct { | ||
| mu sync.Mutex | ||
| wg sync.WaitGroup | ||
| ctx context.Context //nolint:containedctx // run-scoped review lifetime, cancelled at run end | ||
| cancel context.CancelFunc | ||
| closed bool | ||
| active int | ||
| } | ||
|
|
||
| // reserve claims a review slot and returns the review context, creating it | ||
| // from parent on first use. It reports false once run end has begun draining | ||
| // or when maxBackgroundReviews are already running; a true return must be | ||
| // followed by start, which releases the slot. | ||
| func (b *backgroundReviews) reserve(parent context.Context) (context.Context, bool) { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| if b.closed || b.active >= maxBackgroundReviews { | ||
| return nil, false | ||
| } | ||
| b.active++ | ||
| if b.ctx == nil { | ||
| if parent == nil { | ||
| parent = context.Background() | ||
| } | ||
| b.ctx, b.cancel = context.WithCancel(context.WithoutCancel(parent)) | ||
| } | ||
| return b.ctx, true | ||
| } | ||
|
|
||
| // start runs a reserved review and releases its slot when it ends. It reports | ||
| // false, releasing the slot at once, when run end began after the reservation. | ||
| func (b *backgroundReviews) start(review func()) bool { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| if b.closed { | ||
| b.active-- | ||
| return false | ||
| } | ||
| b.wg.Add(1) | ||
| go func() { | ||
| defer b.wg.Done() | ||
| defer b.releaseSlot() | ||
| review() | ||
| }() | ||
| return true | ||
| } | ||
|
|
||
| func (b *backgroundReviews) releaseSlot() { | ||
| b.mu.Lock() | ||
| b.active-- | ||
| b.mu.Unlock() | ||
| } | ||
|
|
||
| // drain stops new reviews, cancels in-flight ones, and waits up to the grace for | ||
| // them to unwind. It is idempotent and reports whether every review unwound. | ||
| func (b *backgroundReviews) drain() bool { | ||
| b.mu.Lock() | ||
| b.closed = true | ||
| cancel := b.cancel | ||
| b.mu.Unlock() | ||
| if cancel == nil { | ||
| return true | ||
| } | ||
| cancel() | ||
| done := make(chan struct{}) | ||
| go func() { | ||
| b.wg.Wait() | ||
| close(done) | ||
| }() | ||
| timer := time.NewTimer(backgroundReviewDrainGrace) | ||
| defer timer.Stop() | ||
| select { | ||
| case <-done: | ||
| return true | ||
| case <-timer.C: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| func (r *Run) drainBackgroundReviews(ctx context.Context) { | ||
| if !r.backgroundReviews.drain() { | ||
| r.diag.Log(ctx, port.LevelWarn, "abandoning yolo advisory guardrail reviews that did not stop within the run-end grace", | ||
| "grace", backgroundReviewDrainGrace.String()) | ||
| } | ||
| } | ||
|
|
||
| func advisoryDisposition(result ToolReviewResult, err error, clean string) string { | ||
| if err != nil || result.Assessment != ReviewAcceptable { | ||
| return "pass_advisory" | ||
| } | ||
| return clean | ||
| } | ||
|
|
||
| // launchBackgroundReview runs assess off the dispatch path, records its checker | ||
| // usage, and publishes the advisory event built by publish unless run end | ||
| // cancelled the review first. release always runs once. | ||
| func (r *Run) launchBackgroundReview(ctx context.Context, assess func() session.AuxiliaryUsage, publish func() session.Event, release func()) { | ||
| if !r.backgroundReviews.start(func() { | ||
| defer release() | ||
| usage := assess() | ||
| r.enqueueAuxiliaryUsageWhileActive(ctx, RemapAuxiliaryUsage(ctx, r.diag, session.UsageKindGuardrail, usage)) | ||
| if ctx.Err() == nil { | ||
| r.children.safeEmit(publish()) | ||
| } | ||
| }) { | ||
| release() | ||
| } | ||
| } | ||
|
|
||
| // startBackgroundActionReview reviews an opted-in yolo action without delaying | ||
| // it. The call has already been authorized and proceeds regardless of verdict. | ||
| func (e *Engine) startBackgroundActionReview(r *Run, sess *session.Session, env tool.Environment, turnIdx int, call session.ToolCall) { | ||
| if r.reviewRoot == nil || !yoloBackgroundReview(r.reviewRoot.reviewer, sess.Mode, call.Name, ReviewJobAction) { | ||
| return | ||
|
rdimitrov marked this conversation as resolved.
|
||
| } | ||
| bg, ok := r.backgroundReviews.reserve(r.ctx) | ||
| if !ok { | ||
| return | ||
| } | ||
| sessID := sess.ID | ||
|
rdimitrov marked this conversation as resolved.
|
||
| a := e.prepareActionAssessment(bg, r, sess, env, call, true) | ||
| r.launchBackgroundReview(bg, func() session.AuxiliaryUsage { | ||
| assessAction(bg, r, &a) | ||
| return a.usage | ||
| }, func() session.Event { | ||
| result := a.result | ||
| if a.err != nil { | ||
| result.Assessment = ReviewUnresolved | ||
| } | ||
| r.publishActionDetail(bg, sessID, a.action.request.ReviewID, a.action.request, result, a.err) | ||
| guardrail := reviewMachinePayload(r.reviewRoot.reviewer, a.action.request, result, a.err, advisoryDisposition(result, a.err, "execute")) | ||
| return session.Event{Type: session.EvHook, Turn: turnIdx, Hook: &session.HookPayload{Phase: string(governance.PhasePreToolUse), Tool: call.Name, Decision: session.HookInfo, CallID: call.ID, Guardrail: guardrail}} | ||
| }, func() { | ||
| a.cancel() | ||
| if a.action.close != nil { | ||
| a.action.close() | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // startBackgroundInboundReview inspects an opted-in yolo result that has | ||
| // already been released to the model; the verdict is advisory only. | ||
| func (e *Engine) startBackgroundInboundReview(r *Run, sess *session.Session, env tool.Environment, turnIdx int, call session.ToolCall, result session.ToolResult) { | ||
| if r.reviewRoot == nil || !yoloBackgroundReview(r.reviewRoot.reviewer, sess.Mode, call.Name, ReviewJobInbound) { | ||
| return | ||
| } | ||
| bg, ok := r.backgroundReviews.reserve(r.ctx) | ||
| if !ok { | ||
| return | ||
| } | ||
| sessID := sess.ID | ||
| a := inboundAssessment{applies: true, mode: sess.Mode, snapshot: true} | ||
| e.prepareInboundRequest(bg, r, sess, env, call, result, &a) | ||
| r.launchBackgroundReview(bg, func() session.AuxiliaryUsage { | ||
| assessInbound(bg, r, &a) | ||
| return a.usage | ||
| }, func() session.Event { | ||
| r.publishInboundDetail(bg, sessID, a) | ||
| return inboundReviewEvent(r, turnIdx, call, a, advisoryDisposition(a.result, a.err, "release_result")) | ||
| }, func() { | ||
| a.cancel() | ||
| if a.close != nil { | ||
| a.close() | ||
| } | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.