@fastagent-sh/fastagent 0.14.0 → 0.15.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 (209) hide show
  1. package/README.md +3 -3
  2. package/dist/agent.d.ts +20 -0
  3. package/dist/agent.js +9 -0
  4. package/dist/channels/context-buffer.d.ts +30 -0
  5. package/dist/channels/context-buffer.js +98 -0
  6. package/dist/channels/control.d.ts +28 -0
  7. package/dist/channels/control.js +214 -0
  8. package/dist/channels/feishu/card.js +1 -1
  9. package/dist/channels/feishu/context-buffer.d.ts +8 -11
  10. package/dist/channels/feishu/context-buffer.js +6 -67
  11. package/dist/channels/feishu/feishu-api.d.ts +11 -3
  12. package/dist/channels/feishu/feishu-api.js +20 -2
  13. package/dist/channels/feishu/feishu.d.ts +28 -16
  14. package/dist/channels/feishu/feishu.js +210 -188
  15. package/dist/channels/feishu/invoke-turn.d.ts +7 -20
  16. package/dist/channels/feishu/invoke-turn.js +10 -62
  17. package/dist/channels/feishu/model.d.ts +2 -1
  18. package/dist/channels/feishu/normalize.d.ts +2 -1
  19. package/dist/channels/feishu/owned-threads.d.ts +2 -6
  20. package/dist/channels/feishu/parse.d.ts +1 -1
  21. package/dist/channels/feishu/preview.d.ts +4 -7
  22. package/dist/channels/feishu/preview.js +24 -140
  23. package/dist/channels/feishu/register-app.d.ts +2 -1
  24. package/dist/channels/feishu/register-webhook.d.ts +2 -1
  25. package/dist/channels/feishu/setup-mode.d.ts +8 -0
  26. package/dist/channels/feishu/setup-mode.js +2 -0
  27. package/dist/channels/feishu/ws-ingress.d.ts +28 -0
  28. package/dist/channels/feishu/ws-ingress.js +136 -0
  29. package/dist/channels/github/github.js +8 -6
  30. package/dist/channels/http.d.ts +14 -0
  31. package/dist/channels/http.js +35 -2
  32. package/dist/channels/invoke-turn-kit.d.ts +65 -0
  33. package/dist/channels/invoke-turn-kit.js +87 -0
  34. package/dist/channels/lark/lark.d.ts +4 -2
  35. package/dist/channels/lark/lark.js +4 -1
  36. package/dist/channels/lark/onboard.d.ts +8 -4
  37. package/dist/channels/lark/onboard.js +8 -0
  38. package/dist/channels/preview-kit.d.ts +109 -0
  39. package/dist/channels/preview-kit.js +183 -0
  40. package/dist/channels/seen.d.ts +5 -0
  41. package/dist/channels/seen.js +35 -0
  42. package/dist/channels/slack/bot-auth.d.ts +15 -0
  43. package/dist/channels/slack/bot-auth.js +146 -0
  44. package/dist/channels/slack/config-api.d.ts +60 -0
  45. package/dist/channels/slack/config-api.js +149 -0
  46. package/dist/channels/slack/context-buffer.d.ts +24 -0
  47. package/dist/channels/slack/context-buffer.js +37 -0
  48. package/dist/channels/slack/invoke-turn.d.ts +19 -0
  49. package/dist/channels/slack/invoke-turn.js +63 -0
  50. package/dist/channels/slack/manifest.d.ts +49 -0
  51. package/dist/channels/slack/manifest.js +69 -0
  52. package/dist/channels/slack/model.d.ts +67 -0
  53. package/dist/channels/slack/model.js +2 -0
  54. package/dist/channels/slack/onboard.d.ts +41 -0
  55. package/dist/channels/slack/onboard.js +120 -0
  56. package/dist/channels/slack/onboarding-state.d.ts +31 -0
  57. package/dist/channels/slack/onboarding-state.js +69 -0
  58. package/dist/channels/slack/owned-threads.d.ts +6 -0
  59. package/dist/channels/slack/owned-threads.js +43 -0
  60. package/dist/channels/slack/parse.d.ts +23 -0
  61. package/dist/channels/slack/parse.js +81 -0
  62. package/dist/channels/slack/preview.d.ts +24 -0
  63. package/dist/channels/slack/preview.js +359 -0
  64. package/dist/channels/slack/reaction.d.ts +24 -0
  65. package/dist/channels/slack/reaction.js +62 -0
  66. package/dist/channels/slack/register-webhook.d.ts +10 -0
  67. package/dist/channels/slack/register-webhook.js +49 -0
  68. package/dist/channels/slack/scaffold/channel.ts +33 -0
  69. package/dist/channels/slack/scaffold/slack-send.ts +171 -0
  70. package/dist/channels/slack/setup-server.d.ts +17 -0
  71. package/dist/channels/slack/setup-server.js +103 -0
  72. package/dist/channels/slack/slack-api.d.ts +77 -0
  73. package/dist/channels/slack/slack-api.js +415 -0
  74. package/dist/channels/slack/slack.d.ts +58 -0
  75. package/dist/channels/slack/slack.js +451 -0
  76. package/dist/channels/slack/welcomed.d.ts +5 -0
  77. package/dist/channels/slack/welcomed.js +32 -0
  78. package/dist/channels/state.js +3 -3
  79. package/dist/channels/stop-command.d.ts +6 -0
  80. package/dist/channels/stop-command.js +36 -0
  81. package/dist/channels/tasks.d.ts +13 -0
  82. package/dist/channels/tasks.js +10 -0
  83. package/dist/channels/telegram/context-buffer.d.ts +8 -17
  84. package/dist/channels/telegram/context-buffer.js +6 -85
  85. package/dist/channels/telegram/invoke-turn.d.ts +5 -22
  86. package/dist/channels/telegram/invoke-turn.js +11 -58
  87. package/dist/channels/telegram/preview.d.ts +4 -7
  88. package/dist/channels/telegram/preview.js +24 -142
  89. package/dist/channels/telegram/telegram.js +23 -9
  90. package/dist/channels/{feishu/text.d.ts → text.d.ts} +1 -1
  91. package/dist/channels/{feishu/text.js → text.js} +1 -1
  92. package/dist/channels/turn-queue.js +1 -1
  93. package/dist/channels/turn-store.d.ts +1 -1
  94. package/dist/channels/turn-store.js +2 -3
  95. package/dist/cli/add-feishu.d.ts +27 -0
  96. package/dist/{cli-add-feishu.js → cli/add-feishu.js} +167 -44
  97. package/dist/cli/add-slack.d.ts +10 -0
  98. package/dist/cli/add-slack.js +204 -0
  99. package/dist/{cli-auth.d.ts → cli/auth-view.d.ts} +1 -1
  100. package/dist/{cli-auth.js → cli/auth-view.js} +1 -1
  101. package/dist/cli/commands/add.d.ts +4 -0
  102. package/dist/cli/commands/add.js +152 -20
  103. package/dist/cli/commands/attach.d.ts +82 -0
  104. package/dist/cli/commands/attach.js +559 -0
  105. package/dist/cli/commands/chat.d.ts +1 -0
  106. package/dist/cli/commands/chat.js +6 -1
  107. package/dist/cli/commands/deploy.d.ts +2 -0
  108. package/dist/cli/commands/deploy.js +78 -22
  109. package/dist/cli/commands/dev.js +11 -5
  110. package/dist/cli/commands/fire.js +3 -3
  111. package/dist/cli/commands/info.js +2 -2
  112. package/dist/cli/commands/invoke.js +2 -3
  113. package/dist/cli/commands/login.js +14 -25
  114. package/dist/cli/commands/models.js +1 -1
  115. package/dist/cli/commands/start.js +15 -8
  116. package/dist/cli/commands/tool.js +2 -1
  117. package/dist/cli/fail.d.ts +0 -2
  118. package/dist/cli/fail.js +1 -1
  119. package/dist/{invoke-stream.d.ts → cli/invoke-stream.d.ts} +1 -1
  120. package/dist/{invoke-stream.js → cli/invoke-stream.js} +4 -0
  121. package/dist/cli/kernel.d.ts +3 -2
  122. package/dist/cli/models-view.d.ts +21 -0
  123. package/dist/cli/models-view.js +66 -0
  124. package/dist/cli/program.js +79 -21
  125. package/dist/cli/serve.d.ts +38 -18
  126. package/dist/cli/serve.js +197 -39
  127. package/dist/cli/shared.d.ts +26 -6
  128. package/dist/cli/shared.js +163 -33
  129. package/dist/collect.d.ts +14 -3
  130. package/dist/collect.js +24 -0
  131. package/dist/core.d.ts +3 -1
  132. package/dist/core.js +2 -0
  133. package/dist/deploy/docker/plan.d.ts +2 -2
  134. package/dist/deploy/docker/plan.js +8 -6
  135. package/dist/deploy/fly/plan.d.ts +3 -1
  136. package/dist/deploy/fly/plan.js +12 -7
  137. package/dist/deploy/fly/run.d.ts +5 -4
  138. package/dist/deploy/fly/run.js +16 -5
  139. package/dist/deploy/preflight.d.ts +10 -4
  140. package/dist/deploy/preflight.js +53 -9
  141. package/dist/deploy/railway/plan.d.ts +3 -1
  142. package/dist/deploy/railway/plan.js +12 -4
  143. package/dist/deploy/railway/run.d.ts +5 -4
  144. package/dist/deploy/railway/run.js +15 -5
  145. package/dist/deploy/runner.d.ts +2 -1
  146. package/dist/deploy/secrets.d.ts +2 -1
  147. package/dist/deploy/secrets.js +23 -3
  148. package/dist/dev-supervisor.d.ts +0 -2
  149. package/dist/dev-supervisor.js +7 -3
  150. package/dist/engines/pi/channel.d.ts +22 -16
  151. package/dist/engines/pi/channel.js +90 -60
  152. package/dist/engines/pi/chat.d.ts +4 -16
  153. package/dist/engines/pi/chat.js +8 -261
  154. package/dist/engines/pi/config.d.ts +14 -9
  155. package/dist/engines/pi/config.js +33 -18
  156. package/dist/engines/pi/create.d.ts +22 -2
  157. package/dist/engines/pi/create.js +20 -14
  158. package/dist/engines/pi/harness.d.ts +49 -0
  159. package/dist/engines/pi/harness.js +90 -2
  160. package/dist/engines/pi/invoke.d.ts +55 -2
  161. package/dist/engines/pi/invoke.js +304 -19
  162. package/dist/engines/pi/login.d.ts +11 -0
  163. package/dist/engines/pi/login.js +16 -4
  164. package/dist/engines/pi/models.d.ts +56 -10
  165. package/dist/engines/pi/models.js +61 -23
  166. package/dist/engines/pi/session-builder.d.ts +16 -0
  167. package/dist/engines/pi/session-builder.js +308 -0
  168. package/dist/engines/pi/session-control.d.ts +50 -0
  169. package/dist/engines/pi/session-control.js +604 -0
  170. package/dist/engines/pi/sessions.d.ts +17 -2
  171. package/dist/engines/pi/sessions.js +9 -0
  172. package/dist/engines/pi/tool-context.d.ts +18 -11
  173. package/dist/engines/pi/tool-context.js +3 -9
  174. package/dist/engines/pi/tool.d.ts +6 -8
  175. package/dist/engines/pi/tool.js +6 -1
  176. package/dist/engines/pi/wake-tool.d.ts +0 -3
  177. package/dist/engines/pi/wake-tool.js +9 -7
  178. package/dist/engines/pi/workspace.d.ts +52 -0
  179. package/dist/engines/pi/workspace.js +72 -15
  180. package/dist/feishu.d.ts +1 -1
  181. package/dist/feishu.js +1 -1
  182. package/dist/host/node.d.ts +23 -6
  183. package/dist/host/node.js +5 -4
  184. package/dist/index.d.ts +1 -0
  185. package/dist/index.js +1 -0
  186. package/dist/lark.d.ts +1 -1
  187. package/dist/lark.js +1 -1
  188. package/dist/observe.js +3 -0
  189. package/dist/pi.d.ts +5 -4
  190. package/dist/pi.js +2 -1
  191. package/dist/scaffold/add-channel.d.ts +19 -6
  192. package/dist/scaffold/add-channel.js +126 -17
  193. package/dist/scaffold/templates/fastagent.config.mjs +4 -3
  194. package/dist/schedule/wakeups.d.ts +0 -3
  195. package/dist/schedule/wakeups.js +1 -1
  196. package/dist/session-remote.d.ts +53 -0
  197. package/dist/session-remote.js +336 -0
  198. package/dist/session.d.ts +265 -0
  199. package/dist/session.js +37 -0
  200. package/dist/slack.d.ts +2 -0
  201. package/dist/slack.js +2 -0
  202. package/dist/tunnel.d.ts +7 -4
  203. package/dist/tunnel.js +21 -10
  204. package/package.json +19 -7
  205. package/dist/channels/feishu/seen.d.ts +0 -5
  206. package/dist/channels/feishu/seen.js +0 -47
  207. package/dist/cli-add-feishu.d.ts +0 -8
  208. package/dist/cli-models.d.ts +0 -11
  209. package/dist/cli-models.js +0 -20
@@ -38,6 +38,13 @@ export function harnessSession(harness) {
38
38
  * so a mid-stream failure surfaces as a `failed` event.
39
39
  */
40
40
  const PROVIDER_MAX_RETRIES = 2;
41
+ /**
42
+ * Retry policy for generated compaction/branch-summary model calls (pi ≥0.81.1, #6901). OPT-IN
43
+ * upstream — an undefined policy means no retries — so both compaction paths pass it explicitly:
44
+ * the harness config (auto-compaction inside a run) and the manual `compact()` dispatch in
45
+ * session-control. Values mirror pi's own app defaults (maxRetries 3, base 2s exponential).
46
+ */
47
+ export const SUMMARIZATION_RETRY_POLICY = { enabled: true, maxRetries: 3, baseDelayMs: 2000 };
41
48
  /**
42
49
  * The serving default for reasoning effort, pinned to what pi's TUI defaults to (its
43
50
  * DEFAULT_THINKING_LEVEL) — NOT inherited from the bare harness, whose own fallback is "off": an
@@ -69,6 +76,84 @@ const DEFAULT_THINKING_LEVEL = "medium";
69
76
  * (like L2's findings memo), not session state — the resolve stays derived from the session.
70
77
  */
71
78
  const warnedRestores = new Set();
79
+ /** pi's ThinkingLevel scale as a checkable set — THE single source for fastagent (session entries
80
+ * store plain strings; session-control's dispatch validation and capabilities derive from this).
81
+ * The `satisfies Record<ThinkingLevel, …>` anchor makes it EXHAUSTIVE against pi's union: pi
82
+ * adding a level turns this into a type error instead of a silent drift where `set_thinking`
83
+ * rejects a value pi supports. */
84
+ const ALL_THINKING_LEVELS = {
85
+ off: true,
86
+ minimal: true,
87
+ low: true,
88
+ medium: true,
89
+ high: true,
90
+ xhigh: true,
91
+ max: true,
92
+ };
93
+ export const THINKING_LEVELS = new Set(Object.keys(ALL_THINKING_LEVELS));
94
+ /**
95
+ * The session's durable override FACTS — the ONE walk both surfaces consume (`state()` reports the
96
+ * recorded truth; `resolveHarnessOverrides` below applies registry/scale fallbacks on top). The
97
+ * LAST entry of each kind wins, and a malformed record reads as ABSENT for that kind — never
98
+ * skipped over to an earlier record: the reporting surface and the execution surface must agree on
99
+ * which record is "the" override.
100
+ */
101
+ export function lastOverrideEntries(entries) {
102
+ let model;
103
+ let modelSeen = false;
104
+ let thinkingLevel;
105
+ let thinkingSeen = false;
106
+ for (let i = entries.length - 1; i >= 0 && !(modelSeen && thinkingSeen); i--) {
107
+ const e = entries[i];
108
+ if (!modelSeen && e?.type === "model_change") {
109
+ modelSeen = true;
110
+ if (e.provider !== undefined && e.modelId !== undefined)
111
+ model = { provider: e.provider, modelId: e.modelId };
112
+ }
113
+ if (!thinkingSeen && e?.type === "thinking_level_change") {
114
+ thinkingSeen = true;
115
+ if (e.thinkingLevel !== undefined)
116
+ thinkingLevel = e.thinkingLevel;
117
+ }
118
+ }
119
+ return { model, thinkingLevel };
120
+ }
121
+ /**
122
+ * Resolve the session's model/thinking OVERRIDES for a fresh harness — same shape as the
123
+ * active-tools resolve above: pi writes `model_change`/`thinking_level_change` entries on explicit
124
+ * setModel/setThinkingLevel (the control plane's `set_model`/`set_thinking` append them directly)
125
+ * but a fresh harness never reads them back. Override facts come from {@link lastOverrideEntries};
126
+ * this adds the EXECUTION fallbacks: a recorded model no longer in this deployment's registry falls
127
+ * back to the default with a deduped warn (fail visibly without bricking the session — the
128
+ * conversation must survive a registry change across deploys); an unknown thinking level likewise.
129
+ */
130
+ export function resolveHarnessOverrides(entries, models, defaults, sessionId) {
131
+ let model = defaults.model;
132
+ let thinkingLevel = defaults.thinkingLevel;
133
+ const warnOnce = (key, message) => {
134
+ const emit = warnedRestores.has(key) ? log.debug : log.warn;
135
+ warnedRestores.add(key);
136
+ emit(message);
137
+ };
138
+ const recorded = lastOverrideEntries(entries);
139
+ if (recorded.model) {
140
+ const found = models.getModel(recorded.model.provider, recorded.model.modelId);
141
+ if (found)
142
+ model = found;
143
+ else {
144
+ warnOnce(`${sessionId}\u0000model\u0000${recorded.model.provider}/${recorded.model.modelId}`, `[fastagent] session ${sessionId}: recorded model override ${recorded.model.provider}/${recorded.model.modelId} is not in this deployment's registry — using the configured default`);
145
+ }
146
+ }
147
+ if (recorded.thinkingLevel !== undefined) {
148
+ if (THINKING_LEVELS.has(recorded.thinkingLevel)) {
149
+ thinkingLevel = recorded.thinkingLevel;
150
+ }
151
+ else {
152
+ warnOnce(`${sessionId}\u0000thinking\u0000${recorded.thinkingLevel}`, `[fastagent] session ${sessionId}: recorded thinking level "${recorded.thinkingLevel}" is unknown — using the configured default`);
153
+ }
154
+ }
155
+ return { model, thinkingLevel };
156
+ }
72
157
  export function resolveHarnessActiveToolNames(recorded, tools, sessionId) {
73
158
  const anyDeferred = tools.some(isDeferredTool);
74
159
  const initial = tools.filter((t) => !isDeferredTool(t)).map((t) => t.name);
@@ -99,17 +184,20 @@ export function piHarnessFactory(options) {
99
184
  const { systemPrompt } = options;
100
185
  const prompt = fresh ? fresh.systemPrompt : typeof systemPrompt === "function" ? systemPrompt() : systemPrompt;
101
186
  const skills = fresh ? fresh.skills : options.skills;
187
+ // Session overrides (set_model / set_thinking) win over the assembly defaults — same entry walk.
188
+ const overrides = resolveHarnessOverrides(entries, options.models, { model: options.model, thinkingLevel: options.thinkingLevel ?? DEFAULT_THINKING_LEVEL }, sessionId);
102
189
  const harness = new AgentHarness({
103
190
  env: options.env,
104
191
  session,
105
192
  models: options.models,
106
- model: options.model,
107
- thinkingLevel: options.thinkingLevel ?? DEFAULT_THINKING_LEVEL,
193
+ model: overrides.model,
194
+ thinkingLevel: overrides.thinkingLevel,
108
195
  tools: options.tools,
109
196
  activeToolNames: resolveHarnessActiveToolNames(activated.length > 0 ? activated : null, options.tools ?? [], sessionId),
110
197
  systemPrompt: prompt,
111
198
  resources: skills ? { skills } : undefined,
112
199
  streamOptions: { maxRetries: PROVIDER_MAX_RETRIES },
200
+ retry: SUMMARIZATION_RETRY_POLICY,
113
201
  });
114
202
  harnessSessions.set(harness, session);
115
203
  return harness;
@@ -1,5 +1,20 @@
1
+ /**
2
+ * The turn mechanism (request-time): fan pi AgentHarness's two ports (subscribe event side-channel
3
+ * + prompt final value) into SPEC's single event stream, under a single-writer-per-session lease.
4
+ *
5
+ * §1 Lease — single-writer concurrency floor (injectable port + in-process default)
6
+ * §2 translate — the single pi↔SPEC translation point (both directions)
7
+ * §3 EventQueue — push→pull plumbing for pi's two-port shape
8
+ * §4 createPiAgentFromHarness — composes §1–§3 into Agent.invoke
9
+ *
10
+ * Concurrency: at most one in-flight turn per session; a second invoke fails fast with
11
+ * `failed{retryable}` ("session busy"), leaving dedupe/queueing/steering to the channel. Each
12
+ * invoke builds a fresh harness bound to the session and discards it (stateless multi-session).
13
+ */
14
+ import type { AgentHarnessEvent } from "@earendil-works/pi-agent-core";
1
15
  import type { AssistantMessage } from "@earendil-works/pi-ai";
2
- import { type Agent, type AgentEvent } from "../../agent.ts";
16
+ import { type Agent, type AgentEvent, type Prompt } from "../../agent.ts";
17
+ import type { SessionEvent } from "../../session.ts";
3
18
  import { type PiHarnessFactory } from "./harness.ts";
4
19
  export type Release = () => void;
5
20
  export interface Lease {
@@ -12,17 +27,55 @@ export declare function classifyRetryable(details: string, signal: {
12
27
  status?: number;
13
28
  code?: unknown;
14
29
  }): boolean;
30
+ /**
31
+ * In-stream event mapping — pi events are translated ONCE into the rich `SessionEvent` vocabulary;
32
+ * the SPEC `AgentEvent` stream is a narrow {@link projectAgentEvent} of it (design §6: one
33
+ * translation plus one projection, never two parallel translations). pi events with no session
34
+ * vocabulary yet (turn_start, agent_start, …) are dropped.
35
+ */
36
+ export declare function toSessionEvent(pe: AgentHarnessEvent, runId: string): SessionEvent | null;
37
+ /** The SPEC projection of the rich stream. Events with no `AgentEvent` counterpart (progress,
38
+ * message boundaries, run boundaries) project to null — the invoke terminal is produced from the
39
+ * resolved message ({@link toTerminal}), not from `run_settled`. */
40
+ export declare function projectAgentEvent(se: SessionEvent): AgentEvent | null;
41
+ /** Live modulation handles for one active run — what the control plane's `dispatch` routes to.
42
+ * Built inside the invoke closure (it owns the harness); registered with the observer at
43
+ * run_started, gone after run_settled. RACE WINDOW (all three commands, symmetric): the run may
44
+ * resolve between the settled-check and the engine call landing — an accepted `abort` can still
45
+ * settle `completed`, and an accepted `steer`/`followUp` can settle without the prompt ever being
46
+ * consumed. Acceptance is not outcome; the settlement is the truth. */
47
+ export interface RunControls {
48
+ steer(prompt: Prompt): Promise<void>;
49
+ followUp(prompt: Prompt): Promise<void>;
50
+ abort(): Promise<void>;
51
+ }
52
+ /** The DATA-plane observation seam: every rich event of every run, pushed as it happens. `run`
53
+ * carries the live {@link RunControls}, attached to the `run_started` event only. A hub
54
+ * (session-control.ts) implements this to serve `events()`/`state()`/`dispatch`; absent = zero
55
+ * overhead. Scope: RUN events only — the hub's own boundary-mutation events (`state_changed`,
56
+ * `compaction_*`) originate in the hub and reach full-vocabulary taps via the hub's `tap` option,
57
+ * not this seam. TRUST BOUNDARY: since Phase 2a this seam hands every wired observer the run's
58
+ * modulation handles — it is the trusted hub seam, not a public fan-out point. Do not wire
59
+ * untrusted taps here; give third parties the read-only `events()` stream instead. */
60
+ export type SessionObserver = (session: string, event: SessionEvent, run?: RunControls) => void;
15
61
  /**
16
62
  * Terminal mapping, decided by the resolved message's stopReason: pi's prompt() resolves a message
17
63
  * with stopReason "error"/"aborted" rather than throwing, so relying on catch alone would miss this
18
64
  * entire failure class (violating SPEC MUST 1).
19
65
  */
20
66
  export declare function toTerminal(message: AssistantMessage): AgentEvent;
21
- export declare function errorToTerminal(error: unknown): AgentEvent;
67
+ export declare function errorToTerminal(error: unknown): Extract<AgentEvent, {
68
+ type: "failed";
69
+ }>;
22
70
  export interface CreatePiAgentFromHarnessOptions {
23
71
  harnessFactory: PiHarnessFactory;
24
72
  /** Single-writer lease. Defaults to the in-process per-session fail-fast lease. */
25
73
  lease?: Lease;
74
+ /** Observation-plane tap (see {@link SessionObserver}). Optional; invoke behavior is identical
75
+ * with or without it — the SPEC stream is a projection of what the observer sees. */
76
+ observer?: SessionObserver;
77
+ /** Working directory exposed to FastAgent-defined tools. Defaults to process.cwd(). */
78
+ cwd?: string;
26
79
  }
27
80
  /** "From a harness factory": engine wired by the caller; adds only the concurrency/stream shell. */
28
81
  export declare function createPiAgentFromHarness(options: CreatePiAgentFromHarnessOptions): Agent;
@@ -1,5 +1,6 @@
1
1
  import { DEFAULT_COMPACTION_SETTINGS, calculateContextTokens, shouldCompact } from "@earendil-works/pi-agent-core";
2
- import { SESSION_BUSY_CODE } from "../../agent.js";
2
+ import { ABORTED_CODE, SESSION_BUSY_CODE, } from "../../agent.js";
3
+ import { abortFirstIterator } from "../../collect.js";
3
4
  import { log } from "../../log.js";
4
5
  import { TOOL_ACTIVATION_ENTRY, harnessSession } from "./harness.js";
5
6
  import { additiveActivation, turnContext } from "./tool-context.js";
@@ -95,21 +96,109 @@ function messageSignal(message) {
95
96
  }
96
97
  return {};
97
98
  }
98
- /** In-stream event mapping. Non text/tool_* pi events (turn_start, message_start, …) are dropped. */
99
- function toAgentEvent(pe) {
99
+ /**
100
+ * In-stream event mapping — pi events are translated ONCE into the rich `SessionEvent` vocabulary;
101
+ * the SPEC `AgentEvent` stream is a narrow {@link projectAgentEvent} of it (design §6: one
102
+ * translation plus one projection, never two parallel translations). pi events with no session
103
+ * vocabulary yet (turn_start, agent_start, …) are dropped.
104
+ */
105
+ export function toSessionEvent(pe, runId) {
106
+ const at = Date.now();
100
107
  switch (pe.type) {
108
+ case "queue_update":
109
+ return {
110
+ type: "queue_changed",
111
+ timestamp: at,
112
+ runId,
113
+ data: { steering: pe.steer.length, followUp: pe.followUp.length },
114
+ };
115
+ case "message_start":
116
+ // Assistant streaming only — a user/toolResult message is not a live message boundary.
117
+ if (pe.message.role !== "assistant")
118
+ return null;
119
+ return { type: "message_started", timestamp: at, runId, data: {} };
101
120
  case "message_update": {
102
121
  const ev = pe.assistantMessageEvent;
103
- if (ev.type === "text_delta")
104
- return { type: "text", delta: ev.delta };
105
- if (ev.type === "thinking_delta")
106
- return { type: "thinking", delta: ev.delta };
122
+ if (ev.type === "text_delta") {
123
+ return { type: "message_delta", timestamp: at, runId, data: { channel: "text", delta: ev.delta } };
124
+ }
125
+ if (ev.type === "thinking_delta") {
126
+ return { type: "message_delta", timestamp: at, runId, data: { channel: "thinking", delta: ev.delta } };
127
+ }
107
128
  return null;
108
129
  }
130
+ case "message_end":
131
+ if (pe.message.role !== "assistant")
132
+ return null;
133
+ return { type: "message_finished", timestamp: at, runId, data: {} };
109
134
  case "tool_execution_start":
110
- return { type: "tool_started", id: pe.toolCallId, name: pe.toolName, args: pe.args };
135
+ return {
136
+ type: "tool_started",
137
+ timestamp: at,
138
+ runId,
139
+ data: { id: pe.toolCallId, name: pe.toolName, args: pe.args },
140
+ };
141
+ case "tool_execution_update":
142
+ return {
143
+ type: "tool_progress",
144
+ timestamp: at,
145
+ runId,
146
+ data: { id: pe.toolCallId, name: pe.toolName, partialResult: pe.partialResult },
147
+ };
111
148
  case "tool_execution_end":
112
- return { type: "tool_ended", id: pe.toolCallId, isError: pe.isError, content: pe.result };
149
+ return {
150
+ type: "tool_finished",
151
+ timestamp: at,
152
+ runId,
153
+ data: { id: pe.toolCallId, isError: pe.isError, content: pe.result },
154
+ };
155
+ case "retry_scheduled": {
156
+ // Summarization retry backoff (auto-compaction / branch summary, pi ≥0.81.1) — without it,
157
+ // up to ~14s of backoff at the turn's tail reads as a hang. `retry_attempt_start`/
158
+ // `retry_finished` stay dropped: they carry no outcome, and the next event is the closure.
159
+ // Typed against the vocabulary (as is the second construction site, session-control's
160
+ // manual-compact callback) so a payload change breaks both at compile time.
161
+ const event = {
162
+ type: "retry_scheduled",
163
+ timestamp: at,
164
+ runId,
165
+ data: {
166
+ operation: pe.operation,
167
+ attempt: pe.attempt,
168
+ maxAttempts: pe.maxAttempts,
169
+ delayMs: pe.delayMs,
170
+ error: pe.errorMessage,
171
+ },
172
+ };
173
+ return event;
174
+ }
175
+ default:
176
+ return null;
177
+ }
178
+ }
179
+ /** The SPEC projection of the rich stream. Events with no `AgentEvent` counterpart (progress,
180
+ * message boundaries, run boundaries) project to null — the invoke terminal is produced from the
181
+ * resolved message ({@link toTerminal}), not from `run_settled`. */
182
+ export function projectAgentEvent(se) {
183
+ switch (se.type) {
184
+ case "message_delta": {
185
+ const d = se.data;
186
+ return d.channel === "text" ? { type: "text", delta: d.delta } : { type: "thinking", delta: d.delta };
187
+ }
188
+ case "tool_started": {
189
+ const d = se.data;
190
+ return { type: "tool_started", id: d.id, name: d.name, args: d.args };
191
+ }
192
+ case "tool_finished": {
193
+ const d = se.data;
194
+ return { type: "tool_ended", id: d.id, isError: d.isError, content: d.content };
195
+ }
196
+ case "retry_scheduled": {
197
+ // `operation` (compaction | branch_summary) stays session-plane vocabulary — a turn renderer
198
+ // only needs "transient failure, retrying"; the engine detail lives in the control plane.
199
+ const d = se.data;
200
+ return { type: "retrying", attempt: d.attempt, maxAttempts: d.maxAttempts, delayMs: d.delayMs, reason: d.error };
201
+ }
113
202
  default:
114
203
  return null;
115
204
  }
@@ -120,7 +209,13 @@ function toAgentEvent(pe) {
120
209
  * entire failure class (violating SPEC MUST 1).
121
210
  */
122
211
  export function toTerminal(message) {
123
- if (message.stopReason === "error" || message.stopReason === "aborted") {
212
+ if (message.stopReason === "aborted") {
213
+ // A deliberate stop (control-plane abort / harness abort), not an error — see {@link ABORTED_CODE}
214
+ // for the consumer contract (design §6).
215
+ const details = message.errorMessage ?? "run aborted";
216
+ return { type: "failed", details, retryable: false, code: ABORTED_CODE };
217
+ }
218
+ if (message.stopReason === "error") {
124
219
  const details = message.errorMessage ?? `model stopped: ${message.stopReason}`;
125
220
  return { type: "failed", details, retryable: classifyRetryable(details, messageSignal(message)) };
126
221
  }
@@ -130,6 +225,20 @@ export function errorToTerminal(error) {
130
225
  const details = error instanceof Error ? error.message : String(error);
131
226
  return { type: "failed", details, retryable: classifyRetryable(details, errorSignal(error)) };
132
227
  }
228
+ /** Bind the concrete pi-agent-core Session behind FastAgent's tool-runtime manager port. */
229
+ function toolSessionManager(sessionId, harness) {
230
+ const session = harnessSession(harness);
231
+ if (!session)
232
+ return undefined;
233
+ return {
234
+ getSessionId: () => sessionId,
235
+ async getHeader() {
236
+ const metadata = await session.getMetadata();
237
+ return { id: sessionId, timestamp: metadata.createdAt };
238
+ },
239
+ getBranch: () => session.getBranch(),
240
+ };
241
+ }
133
242
  /**
134
243
  * The turn's {@link ToolActivation} over the live harness. `activate` is additive and filters to the
135
244
  * registered names first — pi's `setActiveTools` THROWS on unknown names, and a loader must get a
@@ -243,10 +352,39 @@ class EventQueue {
243
352
  }
244
353
  /** "From a harness factory": engine wired by the caller; adds only the concurrency/stream shell. */
245
354
  export function createPiAgentFromHarness(options) {
246
- const { harnessFactory, lease = inProcessLease() } = options;
247
- async function* invoke(scope, prompt) {
355
+ const { harnessFactory, lease = inProcessLease(), observer } = options;
356
+ function invoke(scope, prompt) {
357
+ // The cancellation DOOR (SPEC MUST 3), via the shared abort-first protocol (see
358
+ // abortFirstIterator): cancel aborts the engine work, which settles the run and releases a
359
+ // generator suspended on a quiet stream (a tool mid-execution). The local for-await pattern
360
+ // never hit the underlying deadlock (it breaks at a yield boundary); pull-driven consumers
361
+ // (the SSE handler's eager reads) do.
362
+ // The cancel intent is LATCHED: a consumer may cancel while the generator is still inside
363
+ // the harness build (the door not yet armed) — abortFirstIterator knocks exactly once, and a
364
+ // knock before prompt() starts would be a no-op on an idle harness (the LATER run would
365
+ // ignore it). So turn consults the latch right after arming and, when the consumer already
366
+ // walked away, never starts the model call at all.
367
+ let externalCancel;
368
+ let cancelled = false;
369
+ const gen = turn(scope, prompt, (cancel) => {
370
+ externalCancel = cancel;
371
+ }, () => cancelled);
372
+ const iterator = abortFirstIterator(gen, () => {
373
+ cancelled = true;
374
+ externalCancel?.();
375
+ });
376
+ return {
377
+ [Symbol.asyncIterator]() {
378
+ return iterator;
379
+ },
380
+ };
381
+ }
382
+ async function* turn(scope, prompt, onCancelReady,
383
+ /** The consumer's cancel latch (see invoke's wrapper) — checked once at arming. */
384
+ wasCancelled) {
248
385
  const release = lease.tryAcquire(scope.session);
249
386
  if (!release) {
387
+ // Rejected BEFORE acceptance: no run exists, so the observer sees nothing (replay-safe).
250
388
  yield {
251
389
  type: "failed",
252
390
  details: "session busy: a turn is already in flight for this session",
@@ -255,6 +393,90 @@ export function createPiAgentFromHarness(options) {
255
393
  };
256
394
  return;
257
395
  }
396
+ // The run exists from here: one run_started, exactly one run_settled. Terminal points only
397
+ // RECORD the outcome; the settlement event is emitted in the outer finally, right before
398
+ // release() — so the observation plane's "running" window equals the lease window (state()
399
+ // must never say idle while a new invoke would still be rejected session_busy), and the
400
+ // post-terminal auto-compaction is naturally inside the run. A run with no recorded outcome
401
+ // was cancelled by the caller (SPEC: cancellation has no terminal event) → aborted.
402
+ const runId = crypto.randomUUID();
403
+ let outcome;
404
+ const observe = (event, run) => {
405
+ if (!event || !observer)
406
+ return;
407
+ try {
408
+ observer(scope.session, event, run);
409
+ }
410
+ catch (error) {
411
+ // The observation plane must never break the data plane; a broken hub is its own problem.
412
+ log.warn(`[fastagent] session observer threw (event ${event.type}): ${String(error)}`);
413
+ }
414
+ };
415
+ // run_started must be observed before the (awaited) harness build so no early event outruns
416
+ // registration — so the controls AWAIT the build instead of erroring on the assembling window:
417
+ // a dispatch that races the build simply queues on the freshly built harness. A setup failure
418
+ // rejects the gate (and the run settles failed); the guard keeps an undispatched rejection from
419
+ // becoming an unhandled-rejection crash.
420
+ let harnessReady;
421
+ let harnessFailed;
422
+ const harnessGate = new Promise((resolve, reject) => {
423
+ harnessReady = resolve;
424
+ harnessFailed = reject;
425
+ });
426
+ harnessGate.catch(() => { }); // observed via controls only when a dispatch actually happens
427
+ // Aborted classification has two attribution sources, either suffices: pi's own
428
+ // stopReason:"aborted" (toTerminal), and control-plane INTENT — needed because providers do
429
+ // not uniformly attribute an aborted stream (verified empirically: the faux path surfaces a
430
+ // plain error). Intent = "an abort() succeeded, OR one was still in flight when the terminal
431
+ // arrived" (the harness error often lands before abort() resolves). A rejected abort that
432
+ // RETURNED before the terminal counts as nothing — no rollback dance, no interleaving hazard.
433
+ // GUARANTEE BOUNDARY: an abort still in flight that ultimately rejects can classify a
434
+ // concurrent real error as aborted — narrow, and non-lossy: the settlement carries
435
+ // `error.message` either way.
436
+ let abortsInFlight = 0;
437
+ let abortSucceeded = false;
438
+ // Stale-controls guard: after settlement pi's steer()/followUp()/abort() would still resolve
439
+ // (they queue / no-op on the to-be-discarded harness) — a silent acceptance of a command that
440
+ // can never take effect. The flag flips at THREE points, earliest wins: (1) the moment the
441
+ // run's terminal is determined (the main window — before the consumer-paced `yield terminal`
442
+ // and auto-compaction), (2) the setup-failure path, (3) the outer finally as the backstop for
443
+ // caller cancellation. A post-settle call throws and the dispatcher maps it to
444
+ // `run_command_failed`.
445
+ let runSettled = false;
446
+ const settledError = () => new Error("run already settled; the command cannot take effect");
447
+ // The settled check and the harness call MUST share one synchronous block (no await between):
448
+ // pi enqueues/aborts synchronously at method entry, so check-then-call in the same tick truly
449
+ // closes the race — a check behind its own await boundary would only shrink it.
450
+ const controls = {
451
+ async steer(p) {
452
+ const opts = await toPiPromptOptions(p);
453
+ const harness = await harnessGate;
454
+ if (runSettled)
455
+ throw settledError();
456
+ await harness.steer(p.text, opts);
457
+ },
458
+ async followUp(p) {
459
+ const opts = await toPiPromptOptions(p);
460
+ const harness = await harnessGate;
461
+ if (runSettled)
462
+ throw settledError();
463
+ await harness.followUp(p.text, opts);
464
+ },
465
+ async abort() {
466
+ const harness = await harnessGate;
467
+ if (runSettled)
468
+ throw settledError();
469
+ abortsInFlight++;
470
+ try {
471
+ await harness.abort();
472
+ abortSucceeded = true;
473
+ }
474
+ finally {
475
+ abortsInFlight--;
476
+ }
477
+ },
478
+ };
479
+ observe({ type: "run_started", timestamp: Date.now(), runId, data: {} }, controls);
258
480
  try {
259
481
  let harness;
260
482
  try {
@@ -262,22 +484,58 @@ export function createPiAgentFromHarness(options) {
262
484
  }
263
485
  catch (error) {
264
486
  // Setup failures (session open / auth / …) MUST surface as a failed event, never a throw.
265
- yield errorToTerminal(error);
266
- return;
487
+ harnessFailed(error); // a pending dispatch learns the run cannot take commands
488
+ const terminal = errorToTerminal(error);
489
+ outcome = { status: "failed", error: { message: terminal.details, retryable: terminal.retryable } };
490
+ runSettled = true; // commands can no longer take effect — reject stale controls from here on
491
+ yield terminal;
492
+ return; // → outer finally emits the settlement
493
+ }
494
+ harnessReady(harness);
495
+ // Arm the cancellation door (see invoke's wrapper): aborting the harness settles the run,
496
+ // releasing any await the generator is parked on so a queued return() can reach the finally.
497
+ onCancelReady(() => {
498
+ void harness.abort().catch(() => { });
499
+ });
500
+ // The consumer cancelled DURING the build (latched — the door above came too late to be
501
+ // knocked): never start the model call; settle as aborted and let the queued return()
502
+ // finish the generator. Same synchronous tick as the arming — a cancel from here on
503
+ // reaches the armed door instead.
504
+ if (wasCancelled()) {
505
+ outcome = { status: "aborted" };
506
+ runSettled = true;
507
+ try {
508
+ await harness.abort(); // teardown — fresh-harness discipline
509
+ }
510
+ catch (error) {
511
+ log.warn(`[fastagent] harness abort failed during cleanup: ${String(error)}`);
512
+ }
513
+ return; // → outer finally emits the settlement
267
514
  }
268
515
  const queue = new EventQueue();
269
516
  const unsub = harness.subscribe((pe) => {
270
- const event = toAgentEvent(pe);
517
+ // Summarization retries also warn to server logs: the session `retry_scheduled` event only
518
+ // reaches attached observers, and an operator tailing logs must see the backoff too.
519
+ if (pe.type === "retry_scheduled") {
520
+ log.warn(`[fastagent] ${pe.operation} retry ${pe.attempt}/${pe.maxAttempts} in ${pe.delayMs}ms (session ${scope.session}): ${pe.errorMessage}`);
521
+ }
522
+ const rich = toSessionEvent(pe, runId);
523
+ if (!rich)
524
+ return;
525
+ observe(rich);
526
+ const event = projectAgentEvent(rich);
271
527
  if (event)
272
528
  queue.push(event);
273
529
  });
274
530
  let completed; // the assistant message of a cleanly completed turn
275
531
  try {
276
- // Run the turn inside the session context so a tool's `execute` can read which session it is in
277
- // (turnContext / ToolContext.session). prompt() starts the async work synchronously here, so the
278
- // store propagates to the tool calls awaited within it.
532
+ // Bind current cwd/session/activation capabilities for every FastAgent-defined tool.
279
533
  const opts = await toPiPromptOptions(prompt);
280
- const run = turnContext.run({ session: scope.session, tools: toolActivation(harness) }, () => harness.prompt(prompt.text, opts));
534
+ const run = turnContext.run({
535
+ cwd: options.cwd ?? process.cwd(),
536
+ sessionManager: toolSessionManager(scope.session, harness),
537
+ tools: toolActivation(harness),
538
+ }, () => harness.prompt(prompt.text, opts));
281
539
  yield* queue.drainUntil(run);
282
540
  let terminal;
283
541
  try {
@@ -289,6 +547,28 @@ export function createPiAgentFromHarness(options) {
289
547
  catch (error) {
290
548
  terminal = errorToTerminal(error);
291
549
  }
550
+ if ((abortSucceeded || abortsInFlight > 0) && terminal.type === "failed") {
551
+ terminal = { type: "failed", details: terminal.details, retryable: false, code: ABORTED_CODE };
552
+ }
553
+ if (terminal.type === "completed")
554
+ outcome = { status: "completed" };
555
+ else if (terminal.type === "failed") {
556
+ outcome =
557
+ terminal.code === ABORTED_CODE
558
+ ? // Carry the detail: an independent real error that raced an accepted abort must stay
559
+ // diagnosable in the settlement (audit consumers read run_settled, not the invoke
560
+ // stream) — aborted classifies the run, the message preserves what actually stopped it.
561
+ { status: "aborted", error: { message: terminal.details, retryable: false } }
562
+ : {
563
+ status: "failed",
564
+ error: { code: terminal.code, message: terminal.details, retryable: terminal.retryable },
565
+ };
566
+ }
567
+ // Commands become ineffective the moment the run resolved — NOT at the outer finally, which
568
+ // sits behind `yield terminal` (a consumer-paced suspension) and auto-compaction. Flipping
569
+ // here closes the silent-drop window for steer/follow_up dispatched in that gap; the
570
+ // outer-finally flip remains as the backstop for caller cancellation.
571
+ runSettled = true;
292
572
  yield terminal;
293
573
  }
294
574
  finally {
@@ -326,6 +606,11 @@ export function createPiAgentFromHarness(options) {
326
606
  }
327
607
  }
328
608
  finally {
609
+ // Exactly-one settlement, after ALL run work (incl. auto-compaction) and immediately before
610
+ // the lease releases — see the outcome note above. The stale-controls flag flips FIRST so a
611
+ // dispatch racing this settlement is rejected instead of silently accepted.
612
+ runSettled = true;
613
+ observe({ type: "run_settled", timestamp: Date.now(), runId, data: outcome ?? { status: "aborted" } });
329
614
  release(); // after cleanup, so the next invoke for this session can enter
330
615
  }
331
616
  }
@@ -33,6 +33,17 @@ export interface LoginIO {
33
33
  openUrl(url: string): void;
34
34
  }
35
35
  export type LoginMethod = "oauth" | "api_key";
36
+ /** The user backed out of a prompt/menu — a decision, not a failure. Callers (the first-run picker,
37
+ * the login command) match on this to report neutrally instead of as a login "failure". */
38
+ export declare class LoginCancelled extends Error {
39
+ }
40
+ /** What `loginFlow` can offer a provider interactively: an OAuth flow, an API-key ENTRY prompt, or
41
+ * nothing ("none" — the key must come from the provider's env var). */
42
+ export type InteractiveLoginKind = LoginMethod | "none";
43
+ /** The provider's {@link InteractiveLoginKind}. OAuth wins when both exist (methodForProvider still
44
+ * asks at login time); the first-run picker annotates with this so the hint predicts what picking
45
+ * actually does — a browser login ("oauth"), a key prompt ("api_key"), or neither. */
46
+ export declare function interactiveLoginKind(p: Provider): InteractiveLoginKind;
36
47
  export interface LoginResult {
37
48
  provider: string;
38
49
  method: LoginMethod;