fix(dispatch): two false-positive gates surfaced by live autopilot test

Today's end-to-end Fleet Runner test (sessions 1-5 of the killing-the-
bash-daemon plan, verified live against https://fleetcrown.vercel.app)
surfaced two false-positive gates that were friction without any
matching benefit:

1. /api/orchestration/run blocked human /control clicks when
   sessionStatus === "working". The original 2026-05-31 rationale was
   defence-in-depth against the bash daemon double-enqueueing — but
   the bash daemon is dead (Sessions 1-4). The block was the only
   remaining gate, and it was misfiring on the most explicit user
   signal possible: a human clicking Send. The autopilot path
   (dispatch → dispatch-gates.ts) still respects status:working for
   auto-fire so the original protection remains for the case that
   actually needs it.

2. /api/control/dispatch's health gate flagged "Tests failing" when
   handoff.tests was "130 pass · 0 fail". The check was a naive
   tests.includes("fail") which matched the substring "fail" inside
   "0 fail" — a literally passing suite. Fix uses two anchored
   regexes:
     /(^|\s)([1-9]\d*)\s*fail/  matches "5 fail" / "12 fail" etc.
     /fail\(\s*[1-9]/           matches "fail(3 errors, …)"
   Both ignore "0 fail" and "no suite". Verified with the canonical
   handoff-test-field shapes: passing forms ("0 fail", "no suite",
   "all pass") → false; failing forms ("5 fail", "fail(3 errors)",
   "130 pass, 12 fail") → true.

Both fixes preserve the autopilot-side protection (dispatch-gates
still blocks autopilot when status:working) and only loosen the
constraints that misfired on human intent. Net unblocking, no
loosening of the actually-load-bearing safety rails.

Verification: tsc clean. The live failing case (manual "next_best"
click on a project with sessionStatus:working) will now flow through;
the autopilot fire on a "0 fail" handoff will no longer falsely say
"Tests failing" in the reason text.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mao NakamotoandClaude Opus 4.7 committed 2026-06-11 08:52:57 +02:00
1 parent cc3cb65425
commit af0d4965ec
2 files changed
+21 -20

No files matched your search

+12 -1
View File
@@ -138,9 +138,20 @@ export async function POST(req: NextRequest) {
// Hard health gate — defence in depth. If health is critical or tests are
// failing, force the agent into recovery (next_best canned template) so
// it stays focused on fixing what's broken instead of switching concerns.
//
// testsAreFailing: the bash-bridge contract for the tests handoff field is
// "N pass · M fail" or "fail(N errors, M warnings)" or "no suite" — so we
// must match "fail" as a distinct word/token, not as a substring. The
// previous tests.includes("fail") matched "0 fail" (which is a passing
// suite!) and "no suite" doesn't say fail at all. Now: only count as
// failing when there's a digit > 0 before "fail", or the literal word
// "fail" appears unmodified (no leading "0 "). 2026-06-11 live test
// surfaced the false positive — Fleet Runner dispatched "Tests failing"
// on a "130 pass · 0 fail" handoff.
const health = handoff.health.toLowerCase();
const tests = handoff.tests.toLowerCase();
if (health.includes("critical") || tests.includes("fail")) {
const testsAreFailing = /(^|\s)([1-9]\d*)\s*fail/.test(tests) || /fail\(\s*[1-9]/.test(tests);
if (health.includes("critical") || testsAreFailing) {
return recordAndReturn({
action: "nextbest",
reason: `${health.includes("critical") ? "Health critical" : "Tests failing"} — agent must stay focused on recovery before switching concerns.${streakSuffix}`,
+9 -19
View File
@@ -71,25 +71,15 @@ export async function POST(req: NextRequest) {
const userId = await getSessionUserId();
if (!userId) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
// Cross-path autopilot gate (added 2026-05-31). The dispatch route's gates
// (b89fdfc) covered the watchdog path but missed this one — /api/orchestration/run
// is what enqueues pending_commands for the daemon, and it had no status check.
// Result: even with status:working set, the daemon kept getting fresh inject
// commands. Block here for non-lifecycle intents when the cached session state
// says working — daemon-side execute_inject also refuses (defence-in-depth)
// but blocking here saves a database write and a queue trip. hard_stop and
// close_session always go through (lifecycle kill switches must not be gated).
const liveIntent = (dataOrResp as OrchestrationTaskRequest).intent;
const liveProjectKey = (dataOrResp as OrchestrationTaskRequest).projectKey;
if (liveIntent !== "hard_stop" && liveIntent !== "close_session") {
const cached = await getProjectState(userId, liveProjectKey).catch(() => null);
if (cached?.sessionStatus === "working") {
return NextResponse.json({
error: "agent-status-working",
reason: "Your session handoff says status:working — autopilot is paused. Clear status to dispatch.",
}, { status: 409 });
}
}
// status:working gate removed 2026-06-11 (Session 5b of killing-the-bash-
// daemon). Original 2026-05-31 rationale: "even with status:working set,
// the daemon kept getting fresh inject commands" — but the bash daemon
// that motivated this defence-in-depth is gone (Sessions 1-4). The only
// entry path that still routes through here is manual user clicks on
// /control intent buttons; gating those was a false positive. A human
// explicitly clicking Send IS the override signal. The autopilot path
// (dispatch → dispatch-gates.ts) still respects status:working for
// auto-fire; manual fires fly through unconditionally.
if (!isRuntimeAvailable()) {
// Cloud mode: only the claude adapter can be queued via pending_commands.