Commit Graph
5 Commits
Author SHA1 Message Date
g-but b51fa18cbf fix(home): decide() respects the status field — symmetry with cloud auto-inject gate
27f31cb introduced the agent-driven status field as the gate for the
cloud /control auto-inject. The home/ Brain's decide() function read
lastHandoff.health but not lastHandoff.status — meaning the home/
stack's autonomous dispatches (autonomy=auto, autonomy=sleep) would
fire on a project whose agent said status=working, contradicting the
cloud's behavior.

shouldAutoExecute now takes a status parameter (defaults to '' for
back-compat with any external callers). When autonomy is non-manual
AND status is anything other than 'ready' (including empty, missing,
or 'working'), it returns false — same strict semantics as the cloud
gate in src/hooks/use-project-card-actions.ts and BeaconClient.tsx.
Manual mode bypasses (user click is the gate).

decide() pulls status from project.lastHandoff?.status and threads it
to all four shouldAutoExecute call sites (the recovery branch, the
queue-head branch, and the next_best branch — caught one I missed on
the first pass by re-running tests).

home/decide tests: 14/14 (was 11) — three new cases:
  • sleep mode does NOT fire when status='working'
  • auto mode does NOT fire when status is missing
  • manual mode ignores status (user click IS the gate)
Plus an update to the existing 'sleep auto-fires on high confidence'
fixture to include status: 'ready' (otherwise it'd fail the new
gate). All eight home/ suites: 85/85 total (was 82).

tsc clean, eslint clean.
2026-05-20 10:17:43 +02:00
g-but 27f31cb664 feat(handoff): agent-driven auto-inject gate via status field — model-agnostic
User asked: stop firing auto-inject prompts while the agent is mid-task;
inject should only fire when the agent has done everything and now
needs a nudge. They also called out the broader goal that this should
work for any model (claude, codex, gemini, openclaw, or future
adapters) — not just claude — since today's behavior is implicitly
claude-coupled through ~/.claude/sessions/*.md and the stop hook.

Added a new \`status\` field to the canonical handoff format. The
agent self-reports lifecycle state per turn:

  status: ready    — task fully done, auto-inject may proceed
  status: working  — agent is mid-task, suppress auto-inject

Missing / undefined / any-other-value is treated as NOT ready. This
inverts today's implicit default (auto-fire fires on every turn end)
so the agent must explicitly signal it's done with everything before
the beacon/ReadyBanner countdown fires the next prompt. Matches the
user's stated intent literally.

Why this is model-agnostic: the field lives in the canonical
ORCHESTRATION_TASK_SUMMARY_FIELDS constant that ALL adapters' handoff
templates read from (claude via buildPromptWithSession, openclaw via
renderSharedHandoffBlock, codex/gemini will inherit when their
handoff plumbing is wired). The parsers (parseSessionFile,
parseSessionText, home/watcher.ts:parseHandoff) all pick it up
automatically. Any new adapter that writes the standard handoff
format gets the same gating.

Files touched:

  • src/lib/orchestration/contract.ts — added 'status' to
    ORCHESTRATION_TASK_SUMMARY_FIELDS; OrchestrationTaskSummary makes
    status optional for back-compat.
  • src/lib/events.ts — Handoff zod schema gains status (defaults to
    "") so home/ events with status round-trip cleanly.
  • src/lib/control-types.ts — SessionState.status is optional;
    legacy construction sites that don't yet plumb session_status can
    omit it and naturally suppress auto-fire.
  • src/lib/agent-config.ts:buildPromptWithSession — new status line
    in the handoff template prose (the one claude reads each turn).
  • src/lib/orchestration/renderers.ts:renderSharedHandoffBlock —
    same line for openclaw.
  • src/lib/session-content.ts:parseSessionText — beacon DB parser
    extracts status.
  • home/watcher.ts:FIELDS — home/ watcher's handoff parser includes
    status (cascades to worker.idle events).
  • src/hooks/use-project-card-actions.ts:handleAutoInject —
    explicit gate: only fires when status === 'ready' (or
    health-critical for recovery dispatches).
  • src/app/beacon/[id]/BeaconClient.tsx:handleAutoInject — same
    gate, parsing status out of session.sessionContent via
    parseSessionText (BeaconSession carries raw md text).
  • src/app/api/orchestration/runs/[id]/finish/route.ts — finish
    body summary preserves status.
  • home/{decide,emit,state,worker}.ts test fixtures — added
    status: "" to all Handoff literals so the new required-after-
    default field type-checks.

tsc clean, eslint clean, npm run test:home 82/82.

Transition note: until adapters start emitting status in their
handoffs, auto-inject will suppress for everyone. This is the user's
explicit choice ("only fire when the agent has done everything").
The handoff template prose now tells the agent to write the field,
so going forward agents will include it.

NOT shipped in this commit (deliberate scope cut, queued for next):
  • DB column session_status on project_states (mirror of the new
    field) — without it, control/stream/route.ts can't surface the
    field server-side. Today the BeaconClient parses status directly
    from raw session.md content; the broader cloud /control flow
    will get accurate status once the column lands.
  • Bash side (agent-hook-bridge.sh:emit_worker_finished) reading
    status from session.md and including in worker.finished events.
    The user has denied prior edits to that file; happy to ship if
    they want or leave it for them to land.
2026-05-20 09:23:41 +02:00
g-but 4237260e1c fix(home): manual autonomy fires on receipt — Dispatch button wasn't bootstrappable
Bug: a fresh project (zero outcome history) has confidence 0.5, but the
Dispatch button posted autonomy="auto" which gates on confidence >= 0.55.
Result: clicking Dispatch on a fresh project returned a proposal, not a
fire. With no fires, no outcomes accrued, confidence never moved, and
the brain stayed stuck at 0.5 forever. The system could not bootstrap.

Root cause was a wrong mental model in AUTONOMY_THRESHOLD: 'manual' was
set to Infinity (never auto-execute) with a comment saying "UI shows
the proposal, user clicks" — but the user clicking IS what makes it
manual. There's no second click step. Manual conflated with confirm.

Sharpened the meanings:

  manual  → 0       human clicked Dispatch — fire on receipt, the
                    human IS the gate. Confidence still flows for
                    display but never blocks.
  confirm → Infinity   show the proposal so the human can review or
                       click Dispatch (manual) to fire.
  auto    → 0.55    autonomous scheduler (cron, queue drain) — moderate
                    confidence gate.
  sleep   → 0.75    autonomous while-away — high confidence gate.

Health-critical still blocks manual via shouldAutoExecute's hard gate,
so an unhealthy project can't be force-dispatched past recovery.

Wired the UI Dispatch button to send autonomy="manual" instead of
"auto". Propose stays on "confirm".

home/decide.ts self-tests: 11/11 (was 10/10) — flipped the now-wrong
"manual never auto-executes" case to assert it ALWAYS fires, and added
the health-critical-still-blocks-manual regression. state 13/13 and
worker 12/12 still green. tsc clean.
2026-05-20 05:10:54 +02:00
g-butandClaude Opus 4.7 b8fecb9a79 fix(home): autonomy=confirm was silently auto-firing every dispatch
Real bug in home/decide.ts. The autonomy threshold map had:

  confirm: 0.0

…which combined with shouldAutoExecute's `confidence >= AUTONOMY_THRESHOLD[autonomy]`
check meant confirm-mode dispatches ALWAYS returned autoExecute=true (any
non-negative confidence cleared the bar). Defeated the whole point of
"confirm" as a UI-mediated gate — every POST /api/dispatch with
autonomy=confirm would fire the bridge.dispatch event immediately,
exactly like autonomy=auto.

Per the original design comment ("UI countdown gates the actual fire"),
confirm should never auto-execute from the API. The UI runs a countdown
then re-POSTs with autonomy=auto to fire, or the user clicks NOW. Same
shape as manual from the API's perspective: both return the proposal
with dispatched=false.

Fix: confirm threshold → Infinity. Now:

  manual  : Infinity  – never auto-execute
  confirm : Infinity  – never auto-execute (UI countdown takes over)
  auto    : 0.55      – moderate-confidence fire
  sleep   : 0.75      – high-confidence fire while user away

Regression test added — confirm with full-success outcome history +
healthy state must return autoExecute=false. decide() suite grows to
10/10. All other suites still green: state 8/8, worker 6/6,
render 12/12, projects 7/7.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 03:49:45 +02:00
g-butandClaude Opus 4.7 0b29338982 feat(home): M5 — decide() pure function with confidence + autonomy gating
Fifth milestone of the greenfield rewrite. home/decide.ts is now the only
decision function in the new system: given a ProjectState (from M2's
projection) + queueHead + autonomy preference + clock, returns the
proposed action, a confidence score, and whether to auto-execute.

Pure. No I/O. Deterministic given inputs. Unit-testable by feeding canned
states and asserting on the output — proven by an inline self-test that
covers the 9 representative cases:

  ✓ wait when a run is in flight
  ✓ recovery when health is critical
  ✓ recovery when tests fail (test_and_fix)
  ✓ queue head dispatched when present and health OK
  ✓ empty queue + healthy → next_best
  ✓ manual autonomy never auto-executes
  ✓ sleep mode auto-fires on high confidence + healthy
  ✓ sleep mode holds when recent errors drop confidence
  ✓ empty outcome history gives neutral 0.5 confidence

Run:  npx tsx home/decide.ts

Confidence v1 is a piecewise mapping of weighted outcome history:
  per-outcome weight {success:+1, partial:0, error:-2, hang:-2, timeout:-1.5, user_abort:0}
  asymmetric on purpose — errors scream louder than successes earn
  empty history → neutral 0.5

Autonomy gating layers on top:
  manual  → never auto-execute, regardless of confidence
  confirm → always proposes (UI countdown still gates the actual fire)
  auto    → auto-executes at confidence ≥ 0.55
  sleep   → auto-executes at confidence ≥ 0.75 AND health is not 'critical'

What this commit deliberately does NOT do:
  - call decide() from anywhere yet (M6 wires it into dispatch)
  - replace /api/control/dispatch (still load-bearing for cockpitapp.vercel.app)
  - persist confidence history (it's derived from projection every call)

decide() is the single function that any future tweaking of autonomy
behavior touches. Sleep mode is no longer a binary localStorage toggle —
it's a real strategy with a confidence floor.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 01:50:24 +02:00