Skip to content

Refuse with one exception, translated once at each boundary - #2328

Merged
max-sixty merged 6 commits into
mainfrom
refused-exception
Oct 11, 2026
Merged

max-sixty merged 6 commits into
mainfrom
refused-exception

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 11, 2026 •

Copy link
Copy Markdown
Owner

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 entry 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 a path it cannot resolve or read, 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: " through the CLI and the bare reason through the hook entry.

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, a storage failure while acknowledging, or file bind failing to write the page's grants).
  • 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

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 leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
leaf-agent previously approved these changes Oct 11, 2026
# Conflicts:
#	skills/leaf/scripts/leaf/tasks.py
#	skills/leaf/scripts/leaf/thread.py
leaf-agent
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
@max-sixty
max-sixty merged commit a844b9e into main Oct 11, 2026
6 checks passed
@max-sixty
max-sixty deleted the refused-exception branch October 11, 2026 03:52
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]>
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.

2 participants