Commit 708c946
feat(agents): add /goal slash command with token-budget enforcement (#4552)
## Summary
Adds a `/goal` slash command to Horton sessions. The user sets an
objective with an optional token cap; the agent works autonomously
toward it and stops when `mark_goal_complete` is called or when the run
exceeds the budget. Budget enforcement is **mid-run** (via an
`onStepEnd` hook on the outbound bridge), not at run boundaries — so a
goal with a small budget actually halts the agent within a step, rather
than letting it finish whatever giant tool-loop is in flight.
```text
/goal set "ship feature X" --tokens 50k # default 50k tokens
/goal set "explore" --unlimited # opt out of the cap
/goal show # current state
/goal complete # mark done manually
/goal clear # remove the goal
```
## Behaviour
- **One goal per session**, persisted as a `kind: 'goal'` entry on the
`manifests` collection — resumes across desktop restarts via the
existing Electric sync, no schema migration.
- **Mid-run token enforcement**: an `onStepEnd` hook on the outbound
bridge surfaces per-step input/output tokens; Horton accumulates them
and aborts `ctx.agent.run()` via an `AbortController` once `tokensUsed
>= tokenBudget`. The cap covers the sum of input + output across all
steps since the goal was set.
- **Live progress**: the goal banner ticks up after each step. The
manifest update is written via `writeEvent` directly (not the
wake-session's staged manifest transaction, which only commits at
end-of-wake — too late for a long-running run).
- **`mark_goal_complete` tool**: registered on Horton's tool list; flips
status to `complete`. The chat reply renders via the new `ctx.replyText`
helper, which synthesizes a complete `runs + texts + textDeltas`
sequence.
- **State-changing `/goal` commands interrupt the active run** — `/goal
complete`, `/goal clear`, and `/goal set` typed while a run is in flight
signal SIGINT alongside sending the message, so the prior run aborts
instead of finishing its old work first. `/goal show` is read-only and
never interrupts.
- **Budget-limited stop message**: when the cap is hit mid-run, the
agent posts a synthetic reply explaining what happened (with a suggested
larger budget) instead of just falling silent.
## Plumbing
- `entity-schema.ts` — new `ManifestGoalEntryValue` (objective, status,
tokenBudget, tokensUsed, tokensAtCreation, createdAt, updatedAt) added
to the manifest discriminated union.
- `goal-api.ts` (new) — `setGoal` / `clearGoal` / `getGoal` /
`markGoalComplete` / `markGoalBudgetLimited` / `updateGoalUsage` /
`refreshGoalUsage`. `updateGoalUsage` writes the manifest update
directly through `writeEvent` for live UI; `refreshGoalUsage` never
decreases `tokensUsed` so a stale collection sum can't clobber an
authoritative in-memory value.
- `goal-command.ts` (new) — `/goal` parser (`--tokens
N|50k|1.2m|unlimited`, `--unlimited` flag, subcommand aliases
`done`/`status`) and dispatcher.
- `tools/goal-tools.ts` (new) — `createMarkGoalCompleteTool` exposes the
completion signal to the LLM.
- `outbound-bridge.ts` — new optional `OutboundBridgeHooks.onStepEnd`
callback, threaded through `pi-adapter` and the `AgentConfig` passed to
`useAgent`.
- `context-factory.ts` — `AgentHandle.run` now accepts an optional
`abortSignal` and combines it with the runtime's `runSignal`. New
`ctx.replyText(text)` writes a complete runs+texts+textDeltas sequence
so synthetic replies render in the chat. New goal-related methods
exposed on `HandlerContext`.
- `horton.ts` — `tryHandleSlashCommand` intercepts `/goal *` before the
LLM; `/goal set` enqueues a one-shot kickoff so the agent starts
immediately; `assistantHandler` wires the budget-enforcing `onStepEnd`,
aborts on overflow, and posts the explanation reply.
- `agents-server-ui` — new `GoalBanner` component above the timeline
(objective + budget bar + status badge). `MessageInput` aborts the
active run when a state-changing `/goal` command is submitted.
`EntityTimeline` / `EntityContextDrawer` handle the new `goal` manifest
kind.
## Stacking
Branched off `kevin/agent-token-usage` (#4502) since this depends on the
persisted per-step `input_tokens` / `output_tokens` columns from that
PR. Base will retarget to `main` when #4502 merges.
## Test plan
- [x] `npx tsc --noEmit` clean in `agents-runtime`, `agents`, and
`agents-server-ui`
- [x] 21/21 unit tests pass in
`packages/agents-runtime/test/goal-command.test.ts` (parser +
dispatcher, including `--tokens` formats, `unlimited`, error paths, and
all subcommands)
- [x] Manual: `/goal set "..." --tokens 100k` — banner appears, agent
kicks off, tokens tick up live during the run, goal completes when model
calls `mark_goal_complete`
- [x] Manual: `/goal set "..." --tokens 5k` on a large task — agent
halts mid-run with `budget_limited` status and the explanation reply
- [x] Manual: `/goal complete` typed while a run is active — prior run
aborts via SIGINT, goal flips to complete
- [x] Manual: `/goal set "B"` typed while goal A is running — prior run
aborts, goal A replaced, agent kicks off on B
- [x] Manual: `/goal clear` removes the banner
- [x] Manual: `/goal show` reports current state (no abort)
- [x] Manual: persistence across desktop restart — goal entry resumes
correctly
- [ ] Manual: a no-goal regression check (normal chat behaves unchanged)
## Followups (deferred)
- The `/goal set` kickoff message (`Start working toward the active goal
now...`) is currently visible in the chat as a self-sent inbox message.
Could be filtered from the LLM's view or styled differently.
- Dollar-cost budgets (instead of raw tokens) would be more intuitive
but require provider-specific pricing tables.
- Goose-style "verify the goal is met before stopping" nudge is not
implemented; the model just calls `mark_goal_complete` when it decides
it's done.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent c48c1a8 commit 708c946
28 files changed
Lines changed: 2070 additions & 50 deletions
File tree
- .changeset
- packages
- agents-runtime
- src
- tools
- test
- agents-server-ui/src/components
- views
- agents
- src/agents
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
43 | 49 | | |
44 | 50 | | |
45 | 51 | | |
| |||
58 | 64 | | |
59 | 65 | | |
60 | 66 | | |
| 67 | + | |
61 | 68 | | |
62 | 69 | | |
| 70 | + | |
63 | 71 | | |
64 | 72 | | |
65 | 73 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
7 | 12 | | |
8 | 13 | | |
9 | 14 | | |
| |||
249 | 254 | | |
250 | 255 | | |
251 | 256 | | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
252 | 280 | | |
253 | 281 | | |
254 | 282 | | |
| |||
450 | 478 | | |
451 | 479 | | |
452 | 480 | | |
453 | | - | |
454 | | - | |
455 | | - | |
456 | | - | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
457 | 488 | | |
458 | | - | |
459 | | - | |
460 | | - | |
461 | | - | |
462 | | - | |
463 | | - | |
464 | | - | |
465 | | - | |
466 | | - | |
467 | | - | |
468 | | - | |
469 | | - | |
| 489 | + | |
| 490 | + | |
470 | 491 | | |
471 | 492 | | |
472 | 493 | | |
| |||
476 | 497 | | |
477 | 498 | | |
478 | 499 | | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
479 | 506 | | |
480 | 507 | | |
481 | 508 | | |
| |||
713 | 740 | | |
714 | 741 | | |
715 | 742 | | |
716 | | - | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
717 | 747 | | |
718 | 748 | | |
719 | 749 | | |
| |||
756 | 786 | | |
757 | 787 | | |
758 | 788 | | |
| 789 | + | |
759 | 790 | | |
760 | 791 | | |
761 | 792 | | |
| |||
805 | 836 | | |
806 | 837 | | |
807 | 838 | | |
808 | | - | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
809 | 844 | | |
810 | 845 | | |
811 | 846 | | |
| |||
950 | 985 | | |
951 | 986 | | |
952 | 987 | | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
953 | 993 | | |
954 | 994 | | |
955 | 995 | | |
| |||
1043 | 1083 | | |
1044 | 1084 | | |
1045 | 1085 | | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
1046 | 1133 | | |
1047 | 1134 | | |
1048 | 1135 | | |
| |||
1054 | 1141 | | |
1055 | 1142 | | |
1056 | 1143 | | |
1057 | | - | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
1058 | 1148 | | |
0 commit comments