mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 10:07:19 +01:00
fix(autopilot): close the pending_commands gate bypass
Live regression caught while sessioning with the user. Afterb89fdfcshipped the status:working gate on /api/control/dispatch, the daemon still fired autopilot injects into the user's terminal — 5 injections in 90 seconds, despite status:working AND a pending blocker file. Root cause: the daemon has TWO injection paths, andb89fdfconly covered one. Path A (gated byb89fdfc): daemon watchdog → POST /api/control/dispatch → returns prompt → daemon writes to terminal via autopilot_dispatch_and_inject() Path B (the bypass — fired all 5 times): user click OR ReadyBanner auto-fire OR stop-hook re-fire → POST /api/orchestration/run → enqueueInjectCommand writes a pending_commands row → daemon polls pending_commands, consumes row → daemon writes to terminal via execute_inject() Gates added at TWO chokepoints (defence-in-depth, since either path can be reached without the other): 1. scripts/cockpit-daemon.sh:execute_inject() — reads local FS for ~/.claude/sessions/<tab>.md status field and the .blockers/pending/ directory count. Refuses the inject (returns 1, logs "REFUSED") when either signal says the agent is mid-task. hard_stop bypasses the gate since it's the literal kill switch. 2. src/app/api/orchestration/run/route.ts — reads cached session state from project_states.sessionStatus (already pushed by the daemon to the DB). Returns HTTP 409 with reason="agent-status- working" when working. Saves a database write + pending_commands queue entry vs letting the request through to be rejected by the daemon-side gate. Both gates exempt hard_stop and close_session (lifecycle intents that end sessions, not start work). The dispatch-gate test suite (11 tests, scripts/test/dispatch-gates.ts) still covers the original /api/control/dispatch gate. New tests for the orchestration/run + execute_inject paths are TODO — gating them without a live integration test scaffold means I'd be unit-testing mock interfaces. For now, the live witness is the daemon log line "inject REFUSED — <tab> session status=working" when a refused inject lands. Daemon stopped manually during this session; will be restarted by the user after they pick where to take the UI work next (we had a 7-item plan queued before this bypass was found). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2 files changed
+51
No files matched your search
@@ -202,6 +202,37 @@ next: <state to resume from; empty when nothing is mid-flight>"
|
||||
execute_inject() {
|
||||
local id="$1" tab="$2" prompt="$3" prompt_key="${4:-}" prompt_label="${5:-}" payload_adapter="${6:-}" payload_model="${7:-}"
|
||||
|
||||
# Last-line autopilot gate (added 2026-05-31 after Commit 1's first gate was
|
||||
# bypassed by the pending_commands path). Any inject — whether from cloud
|
||||
# /api/orchestration/run, ReadyBanner auto-fire, or watchdog dispatch —
|
||||
# passes through here. Refuse when the session handoff says status:working
|
||||
# OR a blocker file is pending. The user's session-state is sovereign;
|
||||
# everything that wants to type into their terminal must respect it.
|
||||
#
|
||||
# hard_stop is the ONE exception — it's a kill switch and must always go
|
||||
# through, otherwise stuck loops can't be stopped.
|
||||
if [ "$prompt_key" != "hard_stop" ]; then
|
||||
local _session_file _session_status _blocker_dir _blocker_count
|
||||
_session_file="${HOME}/.claude/sessions/${tab}.md"
|
||||
_session_status=""
|
||||
if [ -f "$_session_file" ]; then
|
||||
_session_status=$(grep -m1 '^status:' "$_session_file" 2>/dev/null | sed 's/^status:[[:space:]]*//' || true)
|
||||
fi
|
||||
if [ "$_session_status" = "working" ]; then
|
||||
log "inject REFUSED — ${tab} session status=working (prompt_key=${prompt_key:-?}, id=${id})"
|
||||
return 1
|
||||
fi
|
||||
_blocker_dir="${HOME}/.claude/sessions/${tab}.blockers/pending"
|
||||
_blocker_count=0
|
||||
if [ -d "$_blocker_dir" ]; then
|
||||
_blocker_count=$(find "$_blocker_dir" -maxdepth 1 -type f 2>/dev/null | wc -l | tr -d ' ')
|
||||
fi
|
||||
if [ "$_blocker_count" -gt 0 ]; then
|
||||
log "inject REFUSED — ${tab} has ${_blocker_count} pending blocker(s) (prompt_key=${prompt_key:-?}, id=${id})"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Look up the project's directory and live adapter (process scan beats conf default).
|
||||
local adapter="claude" project_dir=""
|
||||
while IFS='|' read -r t d _a || [ -n "$t" ]; do
|
||||
|
||||
@@ -70,6 +70,26 @@ 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 });
|
||||
}
|
||||
}
|
||||
|
||||
if (!isRuntimeAvailable()) {
|
||||
// Cloud mode: only the claude adapter can be queued via pending_commands.
|
||||
// Other adapters (openclaw, codex, gemini) require local workers/tools.
|
||||
|
||||
Reference in new issue
Block a user