@genesislcap/ai-assistant 15.95.2 → 15.96.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/ai-assistant.api.json +264 -0
  2. package/dist/ai-assistant.d.ts +124 -0
  3. package/dist/chat-driver.cjs +149 -14
  4. package/dist/chat-driver.cjs.map +3 -3
  5. package/dist/chat-driver.mjs +149 -14
  6. package/dist/chat-driver.mjs.map +3 -3
  7. package/dist/custom-elements.json +178 -99
  8. package/dist/dts/autopilot/decision-message.d.ts.map +1 -1
  9. package/dist/dts/components/chat-driver/chat-driver.constants.d.ts +14 -0
  10. package/dist/dts/components/chat-driver/chat-driver.constants.d.ts.map +1 -1
  11. package/dist/dts/components/chat-driver/chat-driver.d.ts +16 -0
  12. package/dist/dts/components/chat-driver/chat-driver.d.ts.map +1 -1
  13. package/dist/dts/components/orchestrating-driver/orchestrating-driver.d.ts.map +1 -1
  14. package/dist/dts/config/config.d.ts +76 -0
  15. package/dist/dts/config/config.d.ts.map +1 -1
  16. package/dist/dts/config/define-stateful-agent.d.ts +30 -1
  17. package/dist/dts/config/define-stateful-agent.d.ts.map +1 -1
  18. package/dist/dts/react.d.ts +8 -8
  19. package/dist/dts/state/debug-event-log.d.ts +1 -1
  20. package/dist/dts/state/debug-event-log.d.ts.map +1 -1
  21. package/dist/dts/state/persistence/session-snapshot.d.ts +2 -1
  22. package/dist/dts/state/persistence/session-snapshot.d.ts.map +1 -1
  23. package/dist/dts/suggestions/generate-suggestions.d.ts.map +1 -1
  24. package/dist/dts/utils/driver-note.d.ts +26 -0
  25. package/dist/dts/utils/driver-note.d.ts.map +1 -0
  26. package/dist/dts/utils/history-transform.d.ts.map +1 -1
  27. package/dist/dts/utils/message-partition.d.ts.map +1 -1
  28. package/dist/dts/utils/preserved-user-messages.d.ts.map +1 -1
  29. package/dist/esm/autopilot/decision-message.js +3 -0
  30. package/dist/esm/components/chat-driver/chat-driver.constants.js +17 -0
  31. package/dist/esm/components/chat-driver/chat-driver.js +123 -6
  32. package/dist/esm/components/orchestrating-driver/orchestrating-driver.js +4 -0
  33. package/dist/esm/config/define-stateful-agent.js +20 -13
  34. package/dist/esm/state/debug-event-log.js +4 -3
  35. package/dist/esm/state/persistence/session-snapshot.js +11 -3
  36. package/dist/esm/suggestions/generate-suggestions.js +2 -0
  37. package/dist/esm/utils/driver-note.js +30 -0
  38. package/dist/esm/utils/history-transform.js +4 -0
  39. package/dist/esm/utils/message-partition.js +5 -0
  40. package/dist/esm/utils/preserved-user-messages.js +2 -1
  41. package/dist/react.cjs +5 -5
  42. package/dist/react.mjs +4 -4
  43. package/docs/finish-guard.md +125 -0
  44. package/package.json +17 -17
@@ -0,0 +1,125 @@
1
+ # Finish guard: holding a turn open, and releasing on a prose answer
2
+
3
+ `AgentConfig.finishGuard` (and the same option on `defineStatefulAgent`) is an optional hook the
4
+ `ChatDriver` calls when a model reply in the tool loop has **no tool call**, so the turn would end.
5
+ Additive for top-level agents: one without it behaves exactly as before. Sub-agents are the
6
+ exception: one that ends in prose is now nudged to call its completion tool before it fails (see
7
+ [Sub-agents](#sub-agents)).
8
+
9
+ Why it exists: with `toolChoice: 'auto'` nothing stops a model from narrating "I'll now apply it"
10
+ and ending the turn with the work still open. Prompt instructions don't reliably prevent that. The
11
+ guard lets the agent decide in code, from its own state, whether the turn may end.
12
+
13
+ ## The hook
14
+
15
+ ```ts
16
+ finishGuard?: (ctx: FinishGuardContext) =>
17
+ FinishGuardVerdict | undefined | Promise<FinishGuardVerdict | undefined>;
18
+
19
+ interface FinishGuardContext extends SystemPromptContext {
20
+ reply: string; // the text of the reply that would end the turn (already in history)
21
+ continuations: number; // how many times this guard answered 'continue' this turn (0 first)
22
+ canContinue: boolean; // false on the last tool-loop iteration: a 'continue' would be refused
23
+ }
24
+
25
+ type FinishGuardVerdict =
26
+ | { action: 'end'; release?: boolean }
27
+ | { action: 'continue'; note: string };
28
+ ```
29
+
30
+ On `defineStatefulAgent` the context also carries the agent's live `state`, as `toolDefinitions`
31
+ and `toolHandlers` get it. Before `init`, the hook isn't called and the turn ends as usual.
32
+
33
+ | Verdict | What the driver does |
34
+ |---|---|
35
+ | `undefined`, `{ action: 'end' }`, a thrown error, or `'continue'` with a blank note | Ends the turn as if there were no hook. |
36
+ | `{ action: 'continue', note }` | Stores `note` in history and makes the next model call **in the same turn**. |
37
+ | `{ action: 'end', release: true }` | Ends the turn and releases the agent: the same release `releaseAgent()` from a tool handler asks for (`onDeactivate`, the flow lock dropped). |
38
+
39
+ ### The stored note
40
+
41
+ On `'continue'` the driver appends one message to stored history:
42
+
43
+ ```ts
44
+ { role: 'user', content: '<system-reminder>\n<note>\n</system-reminder>', driverNote: 'finish-guard' }
45
+ ```
46
+
47
+ - **Stored, not tail-only.** Later calls in the turn, and later turns, still see why the turn went
48
+ on.
49
+ - **Between the two assistant replies**, so a transport never merges them into one message.
50
+ - **Model-facing only.** `driverNote` hides it from the transcript (`filterVisibleMessages`), and it
51
+ is never read as the user's words: not in the composer pre-fill after an interrupted flow, the
52
+ words compaction keeps, the router's or suggestions' transcript, or the autopilot's transcript.
53
+ It does reach the provider, and the context estimate counts it.
54
+ - Recorded in the debug log as a `turn.finish-guard` event (`action: 'continue'`, `continuation`,
55
+ `note`). A release records `action: 'end', release: true`.
56
+
57
+ ### Capping holds
58
+
59
+ - **Your cap:** answer `{ action: 'end' }` once `ctx.continuations` reaches it.
60
+ - **The driver's bound:** each hold spends one tool-loop iteration, so `maxToolIterations` caps a
61
+ guard that never says `'end'`. On the loop's last iteration the guard is still consulted, with
62
+ `ctx.canContinue: false`: a `'continue'` then ends the turn (there is no call left to hold it
63
+ open for), but `{ action: 'end', release: true }` still releases the agent, and the turn ends
64
+ as a clean answer rather than as the iteration cap.
65
+ - **Stop wins.** If the user stops the turn while the guard is pending, the turn ends as stopped
66
+ (with the usual `Stopped.` marker), whatever the guard answers or throws.
67
+ - **Not consulted on a closing call** (the call after an accepted hand-off), which ends the turn
68
+ whatever the model says, **nor after a tool handler called `releaseAgent()`** earlier in the
69
+ turn: that flow has ended, so there is nothing to hold open.
70
+
71
+ ### Release on a prose answer
72
+
73
+ A turn that ends in prose has no tool handler running, so nothing can call `releaseAgent()`, and a
74
+ stateful agent that answered a question in prose kept its flow lock. Answering
75
+ `{ action: 'end', release: true }` releases it. The usual guards apply: it is ignored after a
76
+ hand-off or a release earlier in the turn, and on a sub-agent, which owns no lock (it ends with
77
+ `completeSubAgent`).
78
+
79
+ ## Sub-agents
80
+
81
+ A sub-agent succeeds only by calling `completeSubAgent(result)` from a tool handler, and it
82
+ defaults to `toolChoice: 'required'`, so a prose reply only happens when a provider ignores the
83
+ forced tool call or the agent sets `toolChoice: 'auto'`. Its prose never reaches the caller, so
84
+ the driver enforces the completion contract on that exit:
85
+
86
+ - The sub-agent's own `finishGuard` is consulted first. `{ action: 'continue', note }` holds the
87
+ turn with its note, as on a top-level agent. `{ action: 'end' }` gives up: the sub-agent fails
88
+ with the same reason as before. `release` is ignored (a sub-agent owns no flow lock).
89
+ - With no guard, or a guard that answers `undefined`, the **driver holds the turn itself** with a
90
+ completion nudge: a stored note asking the model to call the tool that completes its task
91
+ (`driverNote: 'completion-nudge'`), at most `MAX_SUB_AGENT_COMPLETION_NUDGES` (2) times per
92
+ turn, and never on the last iteration. A sub-agent that still answers in prose after that fails
93
+ as before.
94
+ - Each nudge is recorded as `turn.finish-guard` with `source: 'completion-nudge'` (and
95
+ `nudge` / `maxNudges`), and is
96
+ folded into the parent's debug log with the sub-agent's other per-attempt events.
97
+
98
+ `ctx.continuations` counts only the guard's own holds; the driver's nudges are counted apart, so
99
+ they never use up the guard's cap (the iteration cap still bounds both).
100
+
101
+ `continueFromHistory` runs the same loop, so a continued turn is guarded too. A hold is the next
102
+ iteration of the same loop, so `turnIndex` keeps counting up: turn-start work keyed on
103
+ `turnIndex === 0` doesn't run again.
104
+
105
+ ## Example: a stop guard on a machine agent
106
+
107
+ ```ts
108
+ const MAX_HOLDS = 2;
109
+
110
+ defineStatefulAgent<FlatState>({
111
+ name: 'UI Builder',
112
+ // …
113
+ finishGuard: ({ state, continuations }) => {
114
+ if (continuations >= MAX_HOLDS) return { action: 'end' };
115
+ const v = stopGuardVerdict(state.machine, state.buffer); // the agent's own pure decision
116
+ if (v.hold) return { action: 'continue', note: v.note };
117
+ // Nothing open: a question answered in prose ends the flow, so drop the lock.
118
+ return { action: 'end', release: isResting(state.machine) };
119
+ },
120
+ });
121
+ ```
122
+
123
+ Replacing a host-side fallback: a host that called `continueFromHistory(undefined, { tailNote })`
124
+ after a `done` turn to emulate this can delete that loop. Its note was sent once in the tail, never
125
+ stored, and the two assistant replies on either side of it were merged.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@genesislcap/ai-assistant",
3
3
  "description": "Genesis AI Assistant micro-frontend",
4
- "version": "15.95.2",
4
+ "version": "15.96.0",
5
5
  "license": "SEE LICENSE IN license.txt",
6
6
  "main": "dist/esm/index.js",
7
7
  "types": "dist/dts/index.d.ts",
@@ -102,31 +102,31 @@
102
102
  }
103
103
  },
104
104
  "devDependencies": {
105
- "@genesislcap/foundation-comms": "15.95.2",
106
- "@genesislcap/foundation-testing": "15.95.2",
107
- "@genesislcap/genx": "15.95.2",
108
- "@genesislcap/ts-builder": "15.95.2",
109
- "@genesislcap/uvu-playwright-builder": "15.95.2",
110
- "@genesislcap/vite-builder": "15.95.2",
111
- "@genesislcap/webpack-builder": "15.95.2",
105
+ "@genesislcap/foundation-comms": "15.96.0",
106
+ "@genesislcap/foundation-testing": "15.96.0",
107
+ "@genesislcap/genx": "15.96.0",
108
+ "@genesislcap/ts-builder": "15.96.0",
109
+ "@genesislcap/uvu-playwright-builder": "15.96.0",
110
+ "@genesislcap/vite-builder": "15.96.0",
111
+ "@genesislcap/webpack-builder": "15.96.0",
112
112
  "@types/dompurify": "^3.0.5",
113
113
  "@types/marked": "^5.0.2",
114
114
  "esbuild": "0.25.12"
115
115
  },
116
116
  "dependencies": {
117
- "@genesislcap/foundation-ai": "15.95.2",
118
- "@genesislcap/foundation-logger": "15.95.2",
119
- "@genesislcap/foundation-notifications": "15.95.2",
120
- "@genesislcap/foundation-redux": "15.95.2",
121
- "@genesislcap/foundation-ui": "15.95.2",
122
- "@genesislcap/foundation-utils": "15.95.2",
123
- "@genesislcap/rapid-design-system": "15.95.2",
124
- "@genesislcap/web-core": "15.95.2",
117
+ "@genesislcap/foundation-ai": "15.96.0",
118
+ "@genesislcap/foundation-logger": "15.96.0",
119
+ "@genesislcap/foundation-notifications": "15.96.0",
120
+ "@genesislcap/foundation-redux": "15.96.0",
121
+ "@genesislcap/foundation-ui": "15.96.0",
122
+ "@genesislcap/foundation-utils": "15.96.0",
123
+ "@genesislcap/rapid-design-system": "15.96.0",
124
+ "@genesislcap/web-core": "15.96.0",
125
125
  "dompurify": "^3.3.1",
126
126
  "marked": "^17.0.3"
127
127
  },
128
128
  "peerDependencies": {
129
- "@genesislcap/foundation-comms": "15.95.2"
129
+ "@genesislcap/foundation-comms": "15.96.0"
130
130
  },
131
131
  "peerDependenciesMeta": {
132
132
  "@genesislcap/foundation-comms": {