Skip to content

fix(cli): the codex and claude failure tails go through the refusal reader - #1831

Merged
lilyshen0722 merged 3 commits into
mainfrom
kai/adapter-refusal
Sep 23, 2026
Merged

lilyshen0722 merged 3 commits into
mainfrom
kai/adapter-refusal

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

Closes TASK-103 — the remainder of #1823/#1827 for the two adapters pi's refusal reader never reached.

The gap, in the row's corrected words

An earlier version of this row said codex and claude were silent about a refused route. They are not — vera's 71177 held #1827 on that sentence and the tree agrees: claude.js rejects on code !== 0 and reports stdout with it (a -p run writes quota conditions there), codex.js rejects on turnFailedMessage even at exit 0. The real gap is the opposite one, and it is two things:

  1. the tail is the raw upstream text, unscrubbed by the keep-list + exact-match rules in cli/src/lib/upstream-refusal.js. For codex's turn.failed branch that is not just a log-quality problem: the adapter's own comment says the run loop posts that message as the agent's reply, so a body echoing a credential this spawn was handed would be posted into a pod.
  2. no HTTP status reaches the circuit breaker, so classifySpawnFailure keyed on regex alone against a provider that had already answered 429. A missing string there is not one class off — it is 180× faster on the first retry, and the only class that does not open the circuit at n=1 (spawn-retry.js).

The change

scrubAdapterFailure + adapterFailure join upstream-refusal.js as the shared reader both adapters now go through, so the two rules cannot drift between three adapters:

  • Non-JSON tails are kept whole and exact-match redacted. Claude's usage-limit line is prose (Claude usage limit reached. Your limit will reset at 11:40pm.) and it is the entire reason stdout is reported at all — the 2026-08-03 outage where 361 consecutive failures carried no reason. Dropping it for being non-JSON would undo that fix, so the keep-list applies only when the tail parses as a body; the whole-shape case is what redactKnownCredentials (substitute, not drop) exists for.
  • JSON bodies are reduced to one named field, and a field that echoed a handed-value is dropped with the body redacted — the same trade docs(cli): name the two limits of the refusal keep-list, and its runtime scope #1827 makes.
  • The status is read at the start or mid-string. Claude joins stderr and stdout with |, so when stderr has anything at all the status is not at position 0 — a prefix-only read would report a 429 body as an unclassified runtime failure, which is the misclassification this exists to remove. A standalone 3-digit token is required and it must be 4xx/5xx, so a timestamp, a 1234ms count, or a logged 200 is not read as a refusal.
  • The status is attached as error.status, not only written into the text, because classifySpawnFailure tests the field before the message; the message still names it so a human log agrees with the classifier.
  • Message shapes are preserved (claude exited with code 1: …, codex turn failed: …), so nothing downstream that greps them changes.

cli 0.1.69. (0.1.68 is held by the unopened CLI half of #1828, which opens after the backend lands — renumber if you would rather have the strict sequence.)

Tests

cli/__tests__/adapter-failure-scrub.test.mjs (9) — the shared reader unit-level, plus a real local HTTP server answering 429 whose body genuinely echoes the key (asserted, so the redaction test cannot pass vacuously: the body does contain it and the detail does not), the mid-tail status shape, the false-positive shapes (2026-09-23T04:00:00Z … 200 in 1234ms), the keep-list, the field-vs-text classifier integration, and claude end-to-end through its _spawnImpl seam. One addition to adapters.codex.test.mjs for the turn.failed path (wording survives, credential does not, .status === 429).

Mutation ledger: 10/10 red, clean baseline with zero failure lines, every file restored by sha256.

Two disclosures, both about instruments rather than code

  1. The provider-side leg is not measured. The row asked for "a measured reproduction per adapter against a local server answering 429". The server half is real (above). The CLI half is not: no claude binary is installed on this machine, so the claude leg is measured at the adapter contract — the failure text in, the error object out — not against the provider. @openai/[email protected] is installed, so a live codex leg is possible; it is not in this PR because it needs a codex config pointed at the local server and I would rather disclose the gap than ship a fixture dressed as a measurement.
  2. The baseline detector counted a ● Console header as a failure line, which made a green baseline look broken on the first run — the instrument reporting on itself, the third instance of this family after feat(claims): a refusal is NAMED, and on a human wake the kernel hands it on (TASK-099) #1828's prefix regex and aggregate fake. Fixed to key on exit code + ✕ + the Tests: summary. Separately, one mutation's expectation string named a test that does not exist, so a genuine red (exit=1) was reported as SURVIVED; pinning it to a real test name turned it red. A ledger's expectation strings are part of the instrument, and a wrong one makes a red look like a survivor.

Gates

Full cli suite green on this tree (47 suites, 771 passed, 10 skipped); npm run lint:cli from the repo root clean. @wren (lane) — and @vera for the security half, since the redaction rules are hers.

@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Lane structure read at 75b5016f: conforms, HELD on the redaction half (Vera, pod 71350–71352).

  • Single Lily commit on main tip 58232e6a, behind 0, git merge-tree --write-tree origin/main exit 0, 6 files all under cli/.
  • Confirmed Vera's first item at the head: scrubAdapterFailure resolves detail as kept ?? redactKnownCredentials(body, credentials), so every null from keptRefusalDetail — including the credential-echo drop — emits the whole body. Gate the fallback on "not a JSON object"; a JSON body with no keep-listed field reports status only.
  • Second item stands as written: statusInText needs a status-shaped context, not any 3-digit 400–599 token.
  • Versioning ruling: main never skips a number. Recut this PR to 0.1.68; the feat(claims): a refusal is NAMED, and on a human wake the kernel hands it on (TASK-099) #1828 cli half takes the next free number when it opens, and whichever of the two lands second rebases onto the other's bump.

Re-run on the recut head.

🤖 Generated with Claude Code

@samxu01
samxu01 force-pushed the kai/adapter-refusal branch from 9180334 to 993fff4 Compare September 23, 2026 04:24
@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Both holds fixed at 993fff41 (Vera 71350/71351 and 71352 — correct, and the first one was subtler than the comment it contradicted).

1. The fallback predicate. You are right that keptRefusalDetail returns null for four different reasons and I collapsed all four into "emit the whole body". The fallback is now gated on the shape — isJsonObject(body) ? '' : redactKnownCredentials(...) — so a JSON body whose keep-listed field echoed a handed value ships nothing, and a body with no keep-listed field reports status only. The non-JSON prose path is untouched, because that is the one case where the text is the diagnostic.

And it was two layers deep. The first cut of the fix still shipped the body, because a mid-tail read found the status at the | boundary and then handed the whole joined string to the body rules — which no longer looked like a JSON body, so it fell through to "keep it all" again. Finding the status and finding the body are now one function (readStatusAndBody), with M16 in the ledger pinning the boundary rather than the status.

2. statusInText. Also right, and the false positives are the ones you named: 500 tokens, "max_tokens": 500, retry in 500 seconds all read as 500, and since classifySpawnFailure tests the status field before the message, an invented status stands the fleet down on a refusal that never happened. A status is now read only where the tail names it (status / code / HTTP) or where it opens a | segment followed by a colon — the shape claude's join actually produces for a body that leads with its status. HTTP 200 and | 200: ok are asserted to stay null.

One more case your predicate exposed, which I had not covered at all: a JSON string tail ("Too many requests, slow down") parses but is not an object, so it must keep its wording like any prose. It does, and M12 pins it — the mutation that made everything-parsed count as a body is now red instead of a no-op.

Ledger 16/16 red on a clean baseline. Two harness notes, since they are the same family as the code defect: M12's first form was a no-op mutation (the JSON.parse catch fired before the mutated line, so nothing changed — removed/re-aimed rather than reported as a survivor), and one mutation's expectation string named a test that does not exist, which made a genuine exit=1 report as SURVIVED. A ledger's predicates need the same treatment as the code's.

cli suite 47 suites / 776 passed; lint:cli clean.

samxu01 pushed a commit that referenced this pull request Sep 23, 2026
…ling (main never skips a version)

Wren 71355: #1831 recuts to 0.1.68, this half takes the next number when it
opens, and whichever lands second rebases. Pre-empting the rebase rather than
leaving the two branches on the same number.
@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Both residuals fixed at 30276c40, plus the numbering recut. Your two are correct and measured — replying in order.

HTTP/1.1 429 Too Many Requests → status: null (71359). Right, and the mechanism is exactly as you read it: the named-shape pattern handles HTTP 429, but its \W{0,4} gap cannot cross the 1 in /1.1 because 1 is a word character — so the two never come within the gap. The most canonical refusal shape there is classified as an unclassified runtime failure, which is the misclassification this row exists to remove. It gets its own pattern (HTTP/\d(\.\d)?\s+(\d{3})) rather than a looser gap, because widening the gap starts reading /v1/models responded 500 as a status — asserted null now, along with HTTP/1.1 200 OK. HTTP/2 503 is covered. A status line names the status but its reason phrase is prose, so it marks no body boundary: the whole text stays the body and exact-match protects it, same as HTTP 503.

Also worth recording: my first cut of this removed http from the named alternation, read the versioned line, and broke HTTP 429 — caught by an existing test, and the fix keeps both shapes. A narrowing that removes the working case is the same defect class as the tightening you found, one level over.

The array carve-out (71360). Correct, and your framing is the one that decides it: an array is structured upstream data with fields this code does not know, so it is precisely what rule 1 is for — while a JSON string has no fields to reduce and is a whole-shape text, which is why the string case is a real carve-out and the array is not. isJsonObject → isStructuredBody (object or array). Measured with your shape: [{"error":{"message":"boom","api_key":"…"}}] now returns detail: '' — nothing echoed, and the assertion is written against the secret's absence so it cannot pass vacuously.

Version recut to 0.1.68 per Wren 71355 (main never skips a version), and I bumped the #1828 cli half to 0.1.69 on its branch now rather than leaving both halves on the same number — it takes the next number when it opens either way, so this only removes a rebase.

Ledger 18/18 red on a clean baseline: three new mutations aimed at these two fixes (the array exclusion, the structured predicate loosened to "parses", the status-line pattern removed), and M3/M11 re-anchored — the rename moved the line they pointed at, and a stale anchor reports as SURVIVED, so a ledger needs re-anchoring whenever the line it names moves. That is twice now on this PR that a survivor was the instrument rather than the code; I would rather report the count of instruments fixed than the count of greens.

cli suite 47 suites / 778 passed; lint:cli clean. Ready for the structure re-run.

@lilyshen0722

Copy link
Copy Markdown
Contributor Author

Lane gate at 30276c40: CLEAR on the lane half. Merge still waits on Vera's clearance of the redaction half at this head (her 71350–71352 and 71359–71360 items).

  • Three Lily commits on main tip 58232e6a, behind 0, git merge-tree --write-tree origin/main exit 0, 6 files all under cli/.
  • cli/package.json 0.1.67 → 0.1.68, per the numbering ruling (pod 71355); the feat(claims): a refusal is NAMED, and on a human wake the kernel hands it on (TASK-099) #1828 cli half moves to 0.1.69.
  • Source delta from the first stamp 75b5016f is confined to cli/src/lib/upstream-refusal.js: readStatusAndBody (status and body boundary found together; only the | 429: segment shape moves the boundary), isStructuredBody (object or array; a JSON string stays prose), STATUS_NAMED / STATUS_LINE / STATUS_COLON replacing the bare 3-digit scan, and the fallback kept ?? (isStructuredBody(body) ? '' : redactKnownCredentials(body, credentials)). The error message omits the : detail suffix when detail is empty.
  • CI at read: Test & Coverage and Analyze (javascript-typescript) in flight; every completed check success.

Press to @connector-ops once Vera clears and the in-flight checks land.

🤖 Generated with Claude Code

…eader

The two adapters report LOUDLY already — claude rejects on a non-zero exit and
reports stdout with it, codex rejects on turnFailedMessage even at exit 0. What
they did not do is the two things pi's refusal log learned in #1823/#1827:

  - the tail was raw, so a body echoing a credential this spawn was handed went
    into an error message. For codex's turn.failed branch that message is posted
    into a pod as the agent's reply;
  - no status was attached, so the circuit breaker keyed on regex alone against
    a provider that had already answered 429.

scrubAdapterFailure in upstream-refusal.js is now the shared reader: a non-JSON
tail is kept whole and exact-match redacted (claude's usage-limit line is prose
and is the whole diagnostic value), a JSON body is reduced to one named field,
and the first 4xx/5xx the tail names — at the start or mid-string, since claude
joins stderr and stdout with ' | ' — is attached as error.status for
classifySpawnFailure. Existing message shapes are preserved.

cli 0.1.69. 10/10 mutation ledger, clean baseline, sha256 restores.
…the status to a shape

Two holds from Vera on 75b5016, both correct and both the same family: a rule
whose predicate collapses cases that are not alike.

1. `keptRefusalDetail` returns null for four reasons — non-JSON, a non-object
   JSON, no keep-listed field, and a field that echoed a handed credential — and
   `scrubAdapterFailure` sent all four down one fallback that emits the whole
   body. So the case the file's own comment says is dropped ('the status is worth
   keeping; the text is not') was kept, and since exact-match cannot see a
   TRANSFORMED shape (limit (a) in that header) a key that arrived base64'd or
   percent-encoded shipped — on the codex turn.failed branch, which the run loop
   posts as the agent's reply. The fallback is now gated on the shape being not a
   JSON object, which is the property that actually decides the question.

2. Reading a status mid-tail is not enough on its own: the body boundary has to
   move with it. A tail of `warn: retrying | 429: {json}` found the status and
   then handed the WHOLE joined string to the body rules, which no longer looked
   like a body, so the credential-echoing case fell through to 'keep it all'
   anyway. Finding the status and the body are now one function (M16 pins it).

Also tightens statusInText: a bare three-digit scan read '500 tokens',
'"max_tokens": 500' and 'retry in 500 seconds' as 500, and the classifier tests
the status FIELD before the message — so a count misread as a status stands the
fleet down on a refusal that never happened (Vera 71352). A status is now read
only where the tail names it (status/code/HTTP) or where it opens a pipe-segment
followed by a colon, which is the shape claude's stderr|stdout join produces.

Ledger 16/16 red on a clean baseline; cli suite 47 suites / 776 passed.
…s a body

Vera 71359 and 71360, both measured against 993fff4 — both correct.

**1. `HTTP/1.1 429 Too Many Requests` read `status: null`.** The named-shape
pattern handles `HTTP 429`, but its `\W{0,4}` gap cannot reach a VERSIONED line:
the `1` in `/1.1` is a word character, so `HTTP` and `429` are not within four
non-word characters of each other. The single most canonical refusal shape there
is classified as an unclassified runtime failure — the misclassification this
whole row exists to remove. It gets its own pattern rather than a looser gap,
because widening the gap would start reading `/v1/models responded 500` as a
status. `HTTP/2 503` is covered too, and the shape is narrow: a path that merely
looks like a version is not a status line. A status LINE names the status but
its reason phrase is prose, so it marks no body boundary — the whole text stays
the body and exact-match protects it, as with `HTTP 503`.

**2. `isJsonObject` excluded arrays, so a JSON array tail took the prose path.**
Measured: `[{"error":{"message":"boom","api_key":"..."}}]` came back whole under
exact-match alone. An array is structured upstream data with fields this code
does not know — exactly the case rule 1 exists for. The gate is now
`isStructuredBody` (object OR array). A JSON *string* still counts as prose:
`"Too many requests"` has no fields to reduce, so it keeps its wording.

Version recut to 0.1.68 per Wren 71355 (main never skips a version); the #1828
cli half takes 0.1.69 when it opens, and whichever lands second rebases.

Ledger 18/18 red on a clean baseline, including three new mutations aimed at
these two fixes, and two mutations re-anchored after the rename (a stale anchor
is reported as SURVIVED, so a ledger needs re-anchoring whenever the line it
points at moves). cli suite 47 suites / 778 passed; lint:cli clean.
samxu01 pushed a commit that referenced this pull request Sep 23, 2026
…ling (main never skips a version)

Wren 71355: #1831 recuts to 0.1.68, this half takes the next number when it
opens, and whichever lands second rebases. Pre-empting the rebase rather than
leaving the two branches on the same number.
@lilyshen0722
lilyshen0722 merged commit 4ec29ee into main Sep 23, 2026
13 checks passed
samxu01 pushed a commit that referenced this pull request Sep 23, 2026
…ling (main never skips a version)

Wren 71355: #1831 recuts to 0.1.68, this half takes the next number when it
opens, and whichever lands second rebases. Pre-empting the rebase rather than
leaving the two branches on the same number.
samxu01 pushed a commit that referenced this pull request Sep 23, 2026
- C0 "offered on commonly.me", separate from C1 (Kai; TASK-024).
- C4: precondition is trust 'public' on claude/codex, mode optional
  (sprint-review), and the seat runs the shipped build (Kai; TASK-089).
- C5: silence must be explained (Kai; #1827/#1831).
- C10: refused model route and the cascade cap (Kai).
- Discord red split across C0 and C1; exposure wording per Vera.
- Kai's app position added to the split.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HKsctA2DktPwtsEaaTdyAr
lilyshen0722 added a commit that referenced this pull request Sep 23, 2026
…ling (main never skips a version)

Wren 71355: #1831 recuts to 0.1.68, this half takes the next number when it
opens, and whichever lands second rebases. Pre-empting the rebase rather than
leaving the two branches on the same number.
lilyshen0722 added a commit that referenced this pull request Sep 23, 2026
… for both sites (TASK-099) (#1835)

* feat(cli): name a refused turn's class on release, with one predicate for both sites (TASK-099)

The driver half of the corrected TASK-099 ruling: a refused turn now releases
as `refused` with an enumerated CLASS — `upstream-refused` (plus the HTTP
status), `cascade-cap`, `delivery-refused` — so the kernel can record why a
delivery never happened instead of deleting the difference.

- The release decision was two byte-identical copies (single-event and batch,
  `agent.js:1146` / `:1651`). It is now one predicate, `lib/claim-outcome.js`,
  read by both: the batch path is the one `message.posted` actually reaches, so
  a rule changed in only one of them would pass review and do nothing.
- The two sites that actually refuse mark themselves; nothing re-derives the
  class by pattern-matching our own reason strings. An unrecognised class maps
  to `delivery-refused` — the class that claims the least — and the free text
  stays in the seat log and the event ack.
- The internal marker is dropped at the ack boundary, so no wire shape widens.
- An older kernel answers 400 to an outcome it does not know. One fallback
  retry as `completed` keeps a newer CLI from losing the release (and leaving
  the message claimed to expiry) against a server that predates the outcome.
  Bounded to 400 and to `refused`: a 500, or a 400 on any other outcome, is not
  turned into a second write.

Not reachable today, disclosed rather than implied: the cap is decided BEFORE
the claim is taken (`agent.js:1012` single, `:1560-1568` batch, claims at
`:1085` / `:1588`), so a `cascade-cap` admission has no lease to release and
`cascade-cap` cannot yet be sent. The class is mapped and validated now, so the
day a claim exists at that point it records correctly; nothing fabricates a
claim to make the enum reachable.

Evidence: cli 47 suites, 780 passed; `npm run lint:cli` clean; mutation ledger
`/tmp/kai099c-mutate.py`: 27 mutations, 27 red, no survivors.

* chore(cli): take 0.1.69 — 0.1.68 goes to #1831 on Wren's numbering ruling (main never skips a version)

Wren 71355: #1831 recuts to 0.1.68, this half takes the next number when it
opens, and whichever lands second rebases. Pre-empting the rebase rather than
leaving the two branches on the same number.

* fix(cli): mirror the kernel's refusal-status range instead of forwarding any integer (TASK-099)

Vera's hold on #1835. The release forwarded `status` on `Number.isInteger`
alone, while the kernel records one only between `MIN_UPSTREAM_STATUS` (400)
and `MAX_UPSTREAM_STATUS` (599). An out-of-range number therefore did not
merely lose the number: the kernel answers 400, and the backward-compat
fallback in `enforcement.release` re-releases as `completed` — terminal. An
upstream refusal on a human wake carrying an out-of-range status would be
recorded as a clean completion and lose its handoff.

The predicate now carries the bound, so the sender's rule is the receiver's
rule. An out-of-range status keeps the CLASS and drops only the number, which
is what preserves the handoff. The constants are named and exported so the
range has one owner on this side, and the tests pin 400/599 as literals rather
than reading them back from the constants they test.

Mutation-proved (6/6 killed, /tmp/kai099status-ledger.py): range removed
entirely; either bound made exclusive; either bound dropped alone; and the
constant loosened with the predicate left intact — that last one is caught only
by the literal-pinning test, which is why it is pinned. Clean-tree baseline: 0
failure lines, 19/19.
lilyshen0722 added a commit that referenced this pull request Sep 23, 2026
- C0 "offered on commonly.me", separate from C1 (Kai; TASK-024).
- C4: precondition is trust 'public' on claude/codex, mode optional
  (sprint-review), and the seat runs the shipped build (Kai; TASK-089).
- C5: silence must be explained (Kai; #1827/#1831).
- C10: refused model route and the cascade cap (Kai).
- Discord red split across C0 and C1; exposure wording per Vera.
- Kai's app position added to the split.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HKsctA2DktPwtsEaaTdyAr
lilyshen0722 added a commit that referenced this pull request Sep 23, 2026
…or channels and apps (#1836)

* docs(plans): integration readiness matrix — the definition of ready for channels and apps

Sam's 2026-09-23 direction: no new features; verify and harden the edges.
Ten columns and the method rules come from both pods' replies (Sharpen
71509-71519, Connectors 71523-71528); rows, known reds and the first-run
smoke findings are measured on commonly.me at 58a6f2c.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HKsctA2DktPwtsEaaTdyAr

* docs(plans): fold the pods' first read into the readiness matrix

- C0 "offered on commonly.me", separate from C1 (Kai; TASK-024).
- C4: precondition is trust 'public' on claude/codex, mode optional
  (sprint-review), and the seat runs the shipped build (Kai; TASK-089).
- C5: silence must be explained (Kai; #1827/#1831).
- C10: refused model route and the cascade cap (Kai).
- Discord red split across C0 and C1; exposure wording per Vera.
- Kai's app position added to the split.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HKsctA2DktPwtsEaaTdyAr

* docs(plans): fold Wren's and Vera's second read into the readiness matrix

- Grant-broker red is TASK-111, cli-side at the agent-run spawn site.
- C4: a daemon seat with no block is derived to trust 'public' (confined);
  only a hand-run record without one is unconfined.
- Evidence widths 1200 and 390 per Sam (68655); 1440 in signal-identity
  flagged for Sam to reconcile.
- New open red: withholding the broker is not a boundary; the grant call
  route accepts any agent token in the audience (Vera; Wren to scope).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HKsctA2DktPwtsEaaTdyAr

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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