Skip to content

feat(cli): deterministic wrapper enforcement — claim, stand-down, cascade cap, length gate (ADR-018 D3) - #894

Merged
lilyshen0722 merged 1 commit into
mainfrom
feat/wrapper-enforcement
Aug 12, 2026
Merged

lilyshen0722 merged 1 commit into
mainfrom
feat/wrapper-enforcement

Conversation

@lilyshen0722

@lilyshen0722 lilyshen0722 commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

What

Deterministic wrapper enforcement in the commonly agent run path (ADR-018 D3, the "our drivers" row). The 2026-08-11 pilot showed advisory guidance does not bind: the tone contract sat in every seat's tool descriptions and the fleet still posted a 3,614-char median (baseline 2,698), took zero claims, and ran a mention cascade until the operator killed a wrapper by hand. This PR makes the wrapper enforce what the contract could only ask.

New module cli/src/lib/enforcement.js, wired into performRun:

  • Claim-before-act. Events carrying payload.messageId (chat.mention, message.posted, dm.message) are claimed via POST /api/agents/runtime/messages/:id/claim (feat(claims): kernel CAS, lease, and runtime routes (ADR-018 slices 1–2) #892) before the CLI spawns. CAS loser stands down — acked no_action/claim-held, no spawn, no model turn burned. Holder death is covered by the holder's event staying unacked (kernel redelivery), not by losers retrying.
  • Stand down on lost claim. The lease renews at half-life while the CLI turn runs. If it lapses (laptop lid, long turn) and a peer re-wins, the wrapper suppresses its own post at post time. Lost claims are not released (nothing held anymore); held claims release in a finally.
  • Per-seat cascade cap. Consecutive agent-triggered turns per pod capped at 3. Trigger authorship comes from the DM dmKind stamp or the trigger message's isBot flag in the pre-spawn snapshot the loop already fetches (no extra HTTP). Reset by a completed human-triggered turn or 10 quiet minutes. admit/record are split so a redelivered failed spawn never double-counts toward the cap. Human turns are never capped; unclassifiable triggers are neutral.
  • Post-time length gate. Wrapper-posted replies obey the tone contract mechanically: ≤400 chars as-is; up to 3 boundary-aligned messages (fences atomic, sentence/word fallback); anything longer uploads the FULL text via /pods/:podId/uploads and posts one lead message with the [[upload:]] file card. Content is never truncated. Upload failure degrades to posting all chunks — flood beats truncation or silence.

Fail-open everywhere (#887 rule): a kernel without the claim route, a 403 from a stale install, or an unreachable upload endpoint logs and proceeds unguarded. Enforcement must never convert an infrastructure failure into a silent agent. Only an explicit "someone else holds it" or "cascade cap reached" stands a turn down, and both are logged with the reason.

Also: client.upload() (multipart via native FormData/Blob) added to the CLI API client; docs/agents/LOCAL_CLI_WRAPPER.md gains the enforcement section.

Why now

Handoff priority 1 — "this is what the data demands." Kernel half (#892) is deployed and 401-gated; mcp 0.3.0 claim tools exist for agents that want to claim mid-turn, but the wrapper cannot depend on the model choosing to call them. Deterministic beats advisory for our own drivers (ADR-018 D3 table).

Contracts verified (not assumed)

  • payload.messageId set on all four mention enqueue paths (agentMentionService pod mention / auto-join / implicit reply / DM).
  • getRecentMessages returns isBot on both the PG and Mongo paths (the projection regression is called out at agentMessageService.ts:1714).
  • Uploads response carries fileName/originalName/size/kind (routes/uploads.ts handleUpload), matching the directive fields; every field also has a fallback.
  • Claim route resolves identity server-side; body only needs { podId, leaseSeconds }.

Tests

  • cli/__tests__/enforcement.test.mjs — 27 pure-unit tests: classifier, governor (cap/reset/decay/no-double-count), claim keeper (win/lose/fail-open/mid-turn loss/release semantics), splitter (never cuts content, fences atomic, URL whole), delivery modes (single/split/attach/attach-fallback).
  • cli/__tests__/run-loop.test.mjs — 9 new integration tests through performRun: claim before spawn + release after post, lost-claim stand-down, fail-open on route error, mid-turn loss suppression, no claim without messageId, cascade cap declines without spawn or claim, human turns never capped, stand-downs don't reset the spawn-failure streak (Wrapper retry on spawn failure amplifies a model-quota outage into a retry storm #782 invariant), split posting at the wire.
  • Full CLI suite green; mutation-checked at the diff's glue (details in review notes).

Out of scope (deliberate)

  • Kernel-side cascade dampening and the boot-backlog ack bug — being filed as separate issues (handoff priority 3).
  • MCP-path length gating — replies the agent posts itself via commonly_post_message are governed by the MCP contract, not re-gated here.
  • Publishing mcp 0.3.0 — gated on pilot 2.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XeUH4HVDsDHYPsHJthXjB8

…cade cap, length gate (ADR-018 D3)

The 2026-08-11 pilot showed advisory guidance does not bind: the tone
contract sat in every seat's tool descriptions and the fleet still posted
a 3,614-char median, took zero claims, and ran a mention cascade until a
wrapper was killed by hand. The run loop now enforces what the contract
could only ask:

- claim-before-act on message-bearing events (kernel CAS from #892);
  losers stand down acked, holder death is covered by the holder's own
  unacked event
- lease renewal at half-life; a claim lost mid-turn suppresses the
  wrapper post instead of recreating the two-agents-one-message crossing
- per-pod cap (3) on consecutive agent-triggered turns, reset by a human
  turn or 10 quiet minutes; admit/record split so redelivered failures
  never double-count
- post-time length gate: split at fences/paragraphs/sentences up to 3
  messages, attach the full text as a file beyond that — never truncate

Every enforcement failure fails open (#887 rule): a kernel without the
claim route or an unreachable upload endpoint logs and proceeds unguarded.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XeUH4HVDsDHYPsHJthXjB8
@lilyshen0722
lilyshen0722 merged commit 8f4450d into main Aug 12, 2026
10 checks passed
@lilyshen0722
lilyshen0722 deleted the feat/wrapper-enforcement branch August 12, 2026 07:30
lilyshen0722 added a commit that referenced this pull request Aug 12, 2026
Per-install opt-in (config.wakeOnMessage.enabled === true, default off,
revertible). On every message post, opted-in installs in the pod are woken
with a message.posted event; the claim layer (#892 kernel, #894 wrapper)
arbitrates who acts — all wake, one acts, and with claim-before-act the CAS
losers never even spawn.

The fan-out is deliberately dumb and cheap: zero opt-ins costs nothing
beyond the installations array the mention path already loads. Guards:
sender's own identity never wakes on its own post; mention-enqueued targets
are excluded (the mention is the stronger cue — double delivery would burn
a second model turn on one trigger); DM-shaped pods are excluded
(enqueueDmEvent already routes every message there); and bot-authored wake
storms are dampened on the message.posted count with #508's window and
threshold. Human-authored messages are never dampened.

The inline frame tells the woken agent it was NOT named, that silence is
the default, and that acting starts with a claim — payload.content, not
metadata, per the inline-cue rule. message.posted is already in the CLI
wrapper's prompt and claimable sets, so deployed wrappers hear this with no
driver change (D5).


Claude-Session: https://claude.ai/code/session_01XeUH4HVDsDHYPsHJthXjB8

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
lilyshen0722 added a commit that referenced this pull request Aug 12, 2026
…ound (#904)

ADR-018 D3 requires deterministic claim-before-act for every driver we
ship. The CLI wrapper got it in #894; the native runtime never did — the
event queue pre-claims DELIVERY, but nothing claimed the MESSAGE, so two
concurrent native agents could still both act on one trigger. Found by
Sprint Impl's implementation audit (Sharpen msg 53016) running through the
very machinery it was auditing.

runAgent now claims payload.messageId for message-shaped triggers before
creating the run row or touching the LLM. A lost CAS is a complete, silent
stand-down: no AgentRun row (a stand-down is not an execution — the acked
event carries the trace), no typing, no tokens. The claim releases in the
same cleanup that stops the typing indicator — one moment, per D7. Claim
infrastructure failures fail OPEN (#887).


Claude-Session: https://claude.ai/code/session_01XeUH4HVDsDHYPsHJthXjB8

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
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.

1 participant