Skip to content

Every hook returns a string - #21

Closed
nigelgilbert wants to merge 8 commits into
chore/pythonic-meditations-1from
feature/hook-returns
Closed

nigelgilbert wants to merge 8 commits into
chore/pythonic-meditations-1from
feature/hook-returns

Conversation

@nigelgilbert

Copy link
Copy Markdown
Owner

A hook is now one stage of a pipe: it takes the text and returns the text. Returning None, or nothing, used to mean "leave it as it was". That shorthand is gone, and a hook that does not return a string fails.

This is a breaking change to the hook API.

The contract

Hook Its return becomes
on_transcribed(t) the text the cleanup model sees
on_cleaned(t) the text that is pasted
on_sent(t) the log entry
on_done(t) the text the next on_done hook gets
on_error(e, message) the message shown and kept
  • A string replaces the value. To change nothing, return what you were given.
  • "" stops the chain for that point, and nothing downstream runs.
  • Anything else, None included, raises TypeError: on_cleaned returned NoneType, not str.

What changed

  • lifecycle.py: call rejects any return that is not a str. fire no longer checks for None. fire_done and hook_message are deleted, and fire_error replaces the latter. report no longer needs its ordering comment or its None check.
  • on_done: it runs through fire like the other text hooks, so each on_done hook gets what the one before it returned. The return of the last one goes nowhere.
  • on_error: it takes the message as a second argument. The message is piped through the on_error hooks in filename order, starting from the default. A hook that fails is recorded in errors.txt and skipped.
  • Docs: docs/hooks.md and sidecar/README.md state the new contract, and the six recipes that relied on returning nothing now return explicitly.
  • Examples: five files in examples/ returned None on some paths. Each now returns the text it was given on those paths, so behaviour is the same.
  • examples/terminal.py: the docstrings said to start with "shell" where the code matches "terminal". Fixed in passing.

What breaks for existing hook files

  • A text hook that returns nothing fails the dictation. The message names the file: on_cleaned in voice.py: TypeError: on_cleaned returned NoneType, not str. The fix is return t.
  • An on_error(e) hook is called with two arguments and raises TypeError. It is skipped, its failure goes in errors.txt, and the default message is reported. The fix is on_error(e, message) and return message.

Both need to go in the release notes of the next release.

Left as it was

  • An on_error hook that does raise e adds an on_error in <file> note to the original exception, so a later on_error hook reads that as the last note. The base branch behaves the same way.
  • Whitespace-only text stops after_reply but not fire or process. Also unchanged.

Testing

In sidecar/: uv run python -m pytest -q gives 107 passed (99 on the base branch). uv run python -m pyright and uv run ruff check are clean.

The review record is in docs/factory-report-feature-hook-returns-1.md.

@nigelgilbert

Copy link
Copy Markdown
Owner Author

Sloppy drift. I'm re-writing the plan and running a new factory run.

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