Skip to content

feat(hooks): user_prompt_submit hook with prompt origin - #2036

Open
fbdo wants to merge 46 commits into
1jehuang:masterfrom
fbdo:pr/hook-user-prompt-submit
Open

fbdo wants to merge 46 commits into
1jehuang:masterfrom
fbdo:pr/hook-user-prompt-submit

Conversation

@fbdo

@fbdo fbdo commented Nov 1, 2026

Copy link
Copy Markdown

Depends on #2035. Part of #2028. Refs #999.

New commits in this PR: feat(hooks): user_prompt_submit hook with prompt origin and docs(hooks): document user_prompt_submit in the default config. The earlier commits are the dependency.

Problem

A hook cannot see or gate a new user message (#999). Frameworks read the session file to recover the prompt.

Changes

A synchronous user_prompt_submit hook. The prompt arrives on stdin as Claude-shaped JSON (never in env) with an origin (person, or host-generated: auto-poke, reminder, swarm message, swarm spawn, background task, system, sdk). Stdout or additionalContext is appended as a hidden reminder after the user message. decision:"block" or exit 2 rejects the message and nothing is sent to the model. It fails open. user_prompt_submit_timeout_ms defaults to 5000 and is capped at 30000. Request::Message gains an optional origin.

Test

9 new tests: stdin JSON, added context and blocking; fail-open on timeout and odd exit; origin names round-trip and an unknown name is rejected; only a user message is a person; a pending origin is consumed once while the current persists; the payload carries the prompt, origin and jcode detail; several hooks combine context and the first block wins; and a protocol round trip with a legacy message. Run with cargo test -p jcode-app-core --lib prompt_hook and -p jcode-protocol.

Edge cases

Timeout and odd exit codes fail open, an unknown origin name is rejected, and the first block wins among several hooks (all tested).

Compatibility

New key only. The optional origin wire field is absent for old clients (tested).

Docs

docs/HOOKS.md and the default config.

Validation

cargo fmt --check, cargo check --all-targets --all-features, cargo clippy --all-targets --all-features -D warnings, the module-file check, warning budget, sdk parity, the size, test-size, panic, swallowed-error and wildcard-reexport ratchets, and the targeted tests above were run on a Linux arm64 box and compared against a baseline of unmodified master: no regressions. Runtime: stdin carries the UserPromptSubmit event with origin.kind=person, hook stdout reaches the model as hidden context, and a blocking hook produces zero model requests with its reason shown.

Pre-existing master CI failures

master at f74bb39 already fails CI independently of this change: cargo clippy --all-targets --all-features -- -D warnings (collapsible if in runtime_ssh_accounts.rs and tool/todo.rs, type_complexity in runtime_ssh_accounts.rs, an if with identical blocks in todos_view.rs, a needless borrow in ui_header.rs, an unused account_switch_notice), the warning budget, and the code-size, test-size, panic and swallowed-error ratchets (Guardrails and Build & Test are red on master, Format is green). I used "no file is worse than on master and no new lint or test failure" as the bar, and compared this branch against a baseline run of unmodified master: no regressions.

AI assistance

This change was developed with AI assistance (Claude) and reviewed and validated by me as described under Validation.

fbdo added 4 commits October 31, 2026 22:07
Move HookCommands/HooksConfig into jcode-config-types/src/hooks.rs and split
jcode-base hooks into mod/gate/feedback/sync_runner. Synchronous hooks now run
in their own process group with bounded readers, so a grandchild holding
stdout cannot defeat the timeout. Adds a regression test.
Empty until a feature adds its token; the field is omitted when empty.
…ynchronous hooks

Claude-shaped common payload fields with a jcode object, a verdict parser for
decision/continue/additionalContext/exit-2 with 10,000 char caps, and a runner
that feeds a JSON document on stdin. Used by the upcoming prompt and stop hooks.
…d observers

Workers register an identity at spawn; every hook event for that session carries
swarm id, coordinator, parent, label, agent id and agent type in env and payload.
New observers swarm_member_start and swarm_member_end fire in the coordinator
context with SubagentStart/SubagentStop JSON on stdin, once per running to end
transition. Observer hooks can now receive a JSON document on stdin.
Adds JCODE_SPAWN_SWARM_LABEL and capabilities swarm_identity, swarm_member_events.
@greptile-apps

greptile-apps Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The changes reach incoming message handling and swarm lifecycle reporting.

The PR still needs the coordinator origin fix before merging.

Findings

  1. P1 Coordinator replies count as people. ▶
Fix with agent prompt
### Issue 1
crates/jcode-app-core/src/agent/turn_execution.rs:102-108
`run_swarm_message` still calls `run_once_capture` for its generated `integration_prompt`, without declaring an origin. The new default here marks that prompt-including the workers’ replies-as `origin.kind="person"`. This affects both `swarm_message:` and `swarm_message_async:` requests. A hook that allows only human input therefore admits the unattended integration turn. Declare a host origin for the generated coordinator calls, as the worker call already does.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Adds user_prompt_submit hooks that receive the prompt and its origin on stdin before the message is saved.

  • Prompt hooks can add context or stop a message before it reaches the model.
  • Prompt origin labels distinguish person-typed messages from host-generated work.
  • Swarm hooks report when workers start and reach an end state.

No new findings were accepted.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[New message and origin] --> B[Run configured prompt hooks]
    B --> C{Hook blocks?}
    C -->|Yes| D[Return reason without saving the message]
    C -->|No| E[Save user message]
    E --> F[Add hidden context and show notices]
    F --> G{Context only?}
    G -->|Yes| H[Acknowledge message]
    G -->|No| I[Start model turn]
Loading

Reviews (13) · Last reviewed commit: "Merge hook swarm identity refresh into h..." · Reviewed by Greptile

@fbdo
fbdo force-pushed the pr/hook-user-prompt-submit branch from 03a9cb5 to bb2ac37 Compare November 1, 2026 11:21
fbdo added 6 commits November 1, 2026 12:37
Synchronous hook run before each new user message is appended. Claude-shaped
JSON on stdin with the prompt (never in env), a TurnOrigin-shaped origin and
jcode.origin_detail (user, auto, reminder, swarm_message, swarm_spawn,
background_task, system, sdk). Plain stdout or additionalContext becomes a hidden
reminder after the message, decision block/exit 2/continue false rejects it, and
failures fail open. Request::Message gains an optional origin, set to auto by the
TUI for client-generated sends and to sdk by the harness API bridge.
Adds capabilities user_prompt_submit and prompt_origin.
…sible worker startup as swarm_spawn, forget prompt origins
@fbdo
fbdo force-pushed the pr/hook-user-prompt-submit branch from bb2ac37 to 2de70d2 Compare November 1, 2026 13:09
…generated ambient and overnight prompts, and never fail over a hook-blocked prompt
@fbdo
fbdo force-pushed the pr/hook-user-prompt-submit branch from 2de70d2 to ebd1474 Compare November 1, 2026 14:16
Comment thread crates/jcode-base/src/hooks/gate.rs Resolved
fbdo added 3 commits November 8, 2026 19:31
…on cancel

Give the pre_tool gate the same 64 MiB capture limit as the transformer and
block (never silently allow) when stdout is still truncated. Add a drop guard
in the sync runner that kills the hook's process group if the run future is
dropped, so a cancelled turn no longer leaves hook subprocesses running.
Comment on lines +100 to +106
let default_origin = if display_role.is_some() {
PromptOrigin::System
} else {
PromptOrigin::User
};
let prompt_hook = self
.run_user_prompt_submit_hook(user_message, default_origin, "capture")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Coordinator replies count as people. run_swarm_message still calls run_once_capture for its generated integration_prompt, without declaring an origin. The new default here marks that prompt—including the workers’ replies—as origin.kind="person". This affects both swarm_message: and swarm_message_async: requests. A hook that allows only human input therefore admits the unattended integration turn. Declare a host origin for the generated coordinator calls, as the worker call already does.

Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/jcode-app-core/src/agent/turn_execution.rs
Line: 100-106

Comment:
**Coordinator replies count as people.** `run_swarm_message` still calls `run_once_capture` for its generated `integration_prompt`, without declaring an origin. The new default here marks that prompt—including the workers’ replies—as `origin.kind="person"`. This affects both `swarm_message:` and `swarm_message_async:` requests. A hook that allows only human input therefore admits the unattended integration turn. Declare a host origin for the generated coordinator calls, as the worker call already does.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

This branch has not been deployed

No deployments
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