Repository navigation
Refuse with one exception, translated once at each boundary - #2328
Merged
Merged
Conversation
Domain code turned a request down four ways: `sys.exit(reason)`, ValueError and RuntimeError subclasses (EventRefused, FileBindingError, ReceiptRefused, StartRefused, ...), ClickException subclasses (DataError, RegistryError), and bare RuntimeErrors that the CLI's catch-alls printed as refusals. Each boundary re-derived which of these meant "refusal", and they disagreed. Three failures followed:
- A `sys.exit` from a helper the server shares with the CLI (`revision_names`, `revision_path`, `require_registry`) escaped `PageEndpoint._answer`, which catches `Exception`, so uvicorn answered a plain-text 500 with no fault record and no `{error, attempt}` body for the event outbox.
- On the website, a custody refusal from the failure receipt in `_run_follow_turn`'s hand-on ended its daemon thread silently and skipped `_continue_page`, so the page's remaining moves were never started or receipted.
- The CLI's per-command `except RuntimeError` / `except ValueError` printed any bug inside `wait`, `server start`, `codex start` or `delivery read` as a one-line "Error:" with no traceback.
Now `refusals.Refused(Exception)` is the one refusal type, carrying the reason and optional user wording (`Refusal`, moved beside it). The existing refusal types are its subclasses, so boundaries that dispatch on one (StaleFileError's 409, AttemptConflict's 409, EventRefused's `.user`) keep working: EventRefused, AttemptConflict, FileBindingError/StaleFileError, MediaUploadError, ArtifactError, ReceiptRefused, StartRefused, StartUnconfirmed, DataError and RegistryError. Every string `sys.exit` under `skills/leaf/scripts/leaf/` outside `cli.py` and `__main__.py` now raises `Refused`; numeric exits are unchanged.
Each boundary translates it once:
- `LeafGroup.invoke` turns `Refused` into a ClickException and nothing else; its SystemExit-string and ReceiptRefused special cases are gone. `cli.py`'s own refusals raise ClickException directly.
- `__main__` ends a refusal from the hook fast path by printing its reason and exiting 1, as `sys.exit` did.
- `PageEndpoint._answer` answers a refusal from corrupt state as a recorded 500 through its existing `except Exception`.
- `Handshake.__exit__` reports a refusal by its reason and anything else by its class, replacing "any RuntimeError is a refusal".
- The Codex reply commit catches `(OSError, Refused)` instead of `(OSError, RuntimeError, SystemExit, ValueError)`.
- The website's agent-failure route catches `Refused` instead of SystemExit; `_run_follow_turn` records a refused hand-on receipt as `turn_receipt_refused` and still hands the page on; the turn-start catch adds `Refused`, since ReceiptRefused is no longer a RuntimeError.
- `leaf-dev`'s root group and preview worker translate `Refused` like the CLI.
Deleted: the nine per-command try/excepts in `cli.py` and `contract_writer`'s EventRefused-to-`sys.exit` conversion. A RuntimeError that means a refusal became `Refused`: an invalid `--app-server`, a missing `codex` executable, an adapter already running, a Codex queue command or App Server that fails to start, and a prepared acquisition whose session ended. Invariant faults stay RuntimeError. OSError treated as a user error converts at its source: `bind_file` refuses an unreachable path, and `delivery ack` refuses a delivery whose page is gone.
`skills/leaf/scripts/AGENTS.md` states the rule: domain code refuses by raising `Refused` or a subclass, and only a process or transport boundary exits, prints, or answers HTTP.
Printed refusals are byte-identical: "Error: <reason>" through the CLI and the bare reason through the hook fast path.
Behavior that changed:
- A RuntimeError, ValueError or OSError that is not a refusal now surfaces with its traceback from the commands that used to swallow it (for example an invalid stored delivery envelope under `delivery ack`, or a storage failure while acknowledging).
- A detached child's fault (not a refusal) reaches its parent as "ExceptionClass: message".
- Duplicate revision markers end a serving process through the row publisher, whose header says a failed producer ends the process so its lease withdraws the row. Before, the `sys.exit` ended only the producer thread and left its row standing under a live lease.
Also, from the same fault path (survey F95): `PageEndpoint.__init__` now sets `parent`, `posted` and `posted_error`, and `SampleEndpoint` takes `parent`, `path`, `passive` and `asset_root` as keyword-only constructor arguments rather than having them assigned after construction. Before, a `_select_page` that raised on POST `/api/event` made `_fault` read an unset `self.posted`, so the AttributeError escaped and no response was sent.
Tests:
- `test_a_fault_before_the_event_body_is_read_answers_in_the_doors_shape` (fails without the `__init__` fix)
- `test_a_refusal_from_corrupt_page_state_is_an_answered_fault`
- `test_a_refused_correction_receipt_still_hands_the_page_on` (fails without the worker catch)
- Assertions on SystemExit, RuntimeError and ValueError for converted refusals now expect `Refused` or the specific subclass.
- The non-browser `test_interact_*`, website, concurrency, tasks, questions and file-binding suites pass.
> _This was written by Claude Code on behalf of max-sixty_
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
leaf-agent
reviewed
Oct 11, 2026
leaf-agent
left a comment
Collaborator
There was a problem hiding this comment.
bind_file still classifies a page-storage failure as a rejected file binding. An OSError from write_json becomes FileBindingError, so leaf file bind prints Error: and loses the traceback when its grants cannot be saved. I reproduced this by making write_json raise OSError("page storage unavailable"); the result was FileBindingError. Limit the translation to resolving and reading the requested file, and let failures of the page lock or grants write surface as faults.
… failing is a fault Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
leaf-agent
previously approved these changes
Oct 11, 2026
# Conflicts: # skills/leaf/scripts/leaf/tasks.py # skills/leaf/scripts/leaf/thread.py
leaf-agent
previously approved these changes
Oct 11, 2026
# Conflicts: # skills/leaf/scripts/leaf/__main__.py # skills/leaf/scripts/leaf/agent_state.py # skills/leaf/scripts/leaf/delivery.py # skills/leaf/scripts/leaf/session.py
leaf-agent
approved these changes
Oct 11, 2026
leaf-agent
pushed a commit
that referenced
this pull request
Oct 12, 2026
Code-quality survey item F17. The event endpoint read a browser retry's attempt in `_accepted_retry` before admission, merging the stored comment anchor into the retry first, and `append_admitted` read it again under the same lease, and the append a third time. The anchor merge answered an enrichment only the MCP Apps transport made, removed in #1465. `append_admitted` now returns `(record, appended)`, so the endpoint gates the nudge and the thread-title request on whether this request appended, and answers `AttemptConflict` with the same 409. The endpoint stamps the author once, before validation, and `browser_command_error` no longer restates the rule. Attempt identity is derived from the append envelope, `$events.ownership.derived`, author and publication; events.md and the registry description stop saying `browser_discard` shapes it, and both name `post_reply`'s looser CLI retry rule. Also: the stale `append_admitted` docstring about `contract_writer` exiting (gone since #2328), and presence reads the claim's id and turn fields directly, since `readable_claim` always supplies them. No user-visible behavior changes. > _This was written by Claude Code on behalf of max-sixty_ Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Domain code turned a request down four ways:
sys.exit(reason), ValueError and RuntimeError subclasses (EventRefused, FileBindingError, ReceiptRefused, StartRefused, ...), ClickException subclasses (DataError, RegistryError), and bare RuntimeErrors that the CLI's catch-alls printed as refusals. Each boundary re-derived which of these meant "refusal", and they disagreed. Three failures followed:sys.exitfrom a helper the server shares with the CLI (revision_names,revision_path,require_registry) escapedPageEndpoint._answer, which catchesException, so uvicorn answered a plain-text 500 with no fault record and no{error, attempt}body for the event outbox._run_follow_turn's hand-on ended its daemon thread silently and skipped_continue_page, so the page's remaining moves were never started or receipted.except RuntimeError/except ValueErrorprinted any bug insidewait,server start,codex startordelivery readas a one-line "Error:" with no traceback.Now
refusals.Refused(Exception)is the one refusal type, carrying the reason and optional user wording (Refusal, moved beside it). The existing refusal types are its subclasses, so boundaries that dispatch on one (StaleFileError's 409, AttemptConflict's 409, EventRefused's.user) keep working: EventRefused, AttemptConflict, FileBindingError/StaleFileError, MediaUploadError, ArtifactError, ReceiptRefused, StartRefused, StartUnconfirmed, DataError and RegistryError. Every stringsys.exitunderskills/leaf/scripts/leaf/outsidecli.pyand__main__.pynow raisesRefused; numeric exits are unchanged.Each boundary translates it once:
LeafGroup.invoketurnsRefusedinto a ClickException and nothing else; its SystemExit-string and ReceiptRefused special cases are gone.cli.py's own refusals raise ClickException directly.__main__ends a refusal from the hook entry by printing its reason and exiting 1, assys.exitdid.PageEndpoint._answeranswers a refusal from corrupt state as a recorded 500 through its existingexcept Exception.Handshake.__exit__reports a refusal by its reason and anything else by its class, replacing "any RuntimeError is a refusal".(OSError, Refused)instead of(OSError, RuntimeError, SystemExit, ValueError).Refusedinstead of SystemExit;_run_follow_turnrecords a refused hand-on receipt asturn_receipt_refusedand still hands the page on; the turn-start catch addsRefused, since ReceiptRefused is no longer a RuntimeError.leaf-dev's root group and preview worker translateRefusedlike the CLI.Deleted: the nine per-command try/excepts in
cli.pyandcontract_writer's EventRefused-to-sys.exitconversion. A RuntimeError that means a refusal becameRefused: an invalid--app-server, a missingcodexexecutable, an adapter already running, a Codex queue command or App Server that fails to start, and a prepared acquisition whose session ended. Invariant faults stay RuntimeError. OSError treated as a user error converts at its source:bind_filerefuses a path it cannot resolve or read, anddelivery ackrefuses a delivery whose page is gone.skills/leaf/scripts/AGENTS.mdstates the rule: domain code refuses by raisingRefusedor a subclass, and only a process or transport boundary exits, prints, or answers HTTP.Printed refusals are byte-identical: "Error: " through the CLI and the bare reason through the hook entry.
Behavior that changed:
delivery ack, a storage failure while acknowledging, orfile bindfailing to write the page's grants).sys.exitended only the producer thread and left its row standing under a live lease.Also, from the same fault path (survey F95):
PageEndpoint.__init__now setsparent,postedandposted_error, andSampleEndpointtakesparent,path,passiveandasset_rootas keyword-only constructor arguments rather than having them assigned after construction. Before, a_select_pagethat raised on POST/api/eventmade_faultread an unsetself.posted, so the AttributeError escaped and no response was sent.Tests:
test_a_fault_before_the_event_body_is_read_answers_in_the_doors_shape(fails without the__init__fix)test_a_refusal_from_corrupt_page_state_is_an_answered_faulttest_a_refused_correction_receipt_still_hands_the_page_on(fails without the worker catch)Refusedor the specific subclass.test_interact_*, website, concurrency, tasks, questions and file-binding suites pass.