@forwardimpact/libeval 0.1.44 → 0.1.46

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.
@@ -1,7 +1,7 @@
1
1
  /**
2
- * AgentRunner — runs a single Claude Agent SDK session and emits raw NDJSON
3
- * events to an output stream. Building block for both `fit-eval run` and
4
- * `fit-eval supervise`.
2
+ * AgentRunner — runs a single Claude Agent SDK session and emits raw
3
+ * NDJSON events to an output stream. Building block for `fit-eval run`,
4
+ * `fit-eval supervise`, `fit-eval facilitate`, and `fit-eval discuss`.
5
5
  *
6
6
  * Follows OO+DI: constructor injection, factory function, tests bypass factory.
7
7
  */
@@ -13,25 +13,6 @@ const DEFAULT_ALLOWED_TOOLS = ["Bash", "Read", "Glob", "Grep", "Write", "Edit"];
13
13
  // overridable — so a future caller can't accidentally reduce permissions.
14
14
  const PERMISSION_MODE = "bypassPermissions";
15
15
 
16
- function applyDefaults(deps) {
17
- return {
18
- cwd: deps.cwd,
19
- query: deps.query,
20
- output: deps.output,
21
- model: deps.model ?? "claude-opus-4-7[1m]",
22
- maxTurns: deps.maxTurns ?? 50,
23
- allowedTools: deps.allowedTools ?? DEFAULT_ALLOWED_TOOLS,
24
- onLine: deps.onLine ?? null,
25
- onBatch: deps.onBatch ?? null,
26
- batchSize: deps.batchSize ?? 3,
27
- settingSources: deps.settingSources ?? [],
28
- systemPrompt: deps.systemPrompt ?? null,
29
- disallowedTools: deps.disallowedTools ?? [],
30
- mcpServers: deps.mcpServers ?? null,
31
- taskAmend: deps.taskAmend ?? null,
32
- };
33
- }
34
-
35
16
  /** Run a single Claude Agent SDK session and emit raw NDJSON events to an output stream. */
36
17
  export class AgentRunner {
37
18
  /**
@@ -43,29 +24,38 @@ export class AgentRunner {
43
24
  * @param {number} [deps.maxTurns] - Maximum agentic turns; 0 means unlimited
44
25
  * @param {string[]} [deps.allowedTools] - Tools the agent may use
45
26
  * @param {function} [deps.onLine] - Callback invoked with each NDJSON line as it's produced
46
- * @param {function} [deps.onBatch] - Async callback invoked with a batch of NDJSON lines at flush boundaries: every `batchSize` assistant text blocks, the terminal `result` message, and — on iterator crash/abort — once more in a final flush carrying any lines that never reached a boundary. Receives `(lines, { abort })` where calling `abort()` stops the in-flight SDK session via the AbortController. Optional; assignable at runtime so the Supervisor can swap it per turn.
47
- * @param {number} [deps.batchSize] - Assistant text-block messages to accumulate before firing onBatch. Tool-only assistant messages ride along without counting. Default 3: the supervisor reviews the agent every three text turns instead of every turn. The terminal `result` always flushes regardless of count.
48
27
  * @param {string[]} [deps.settingSources] - SDK setting sources (e.g. ['project'] to load CLAUDE.md)
49
28
  * @param {string|object} [deps.systemPrompt] - SDK system prompt (string replaces default; {type:'preset', preset:'claude_code', append} appends)
50
29
  * @param {string[]} [deps.disallowedTools] - Tools to explicitly remove from the model's context
51
30
  * @param {Record<string, object>} [deps.mcpServers] - MCP server configs to pass to the SDK query
31
+ * @param {object} deps.redactor
52
32
  */
53
33
  constructor(deps) {
54
34
  if (!deps.cwd) throw new Error("cwd is required");
55
35
  if (!deps.query) throw new Error("query is required");
56
36
  if (!deps.output) throw new Error("output is required");
57
37
  if (!deps.redactor) throw new Error("redactor is required");
58
- Object.assign(this, applyDefaults(deps));
38
+ this.cwd = deps.cwd;
39
+ this.query = deps.query;
40
+ this.output = deps.output;
59
41
  this.redactor = deps.redactor;
42
+ this.model = deps.model ?? "claude-opus-4-7[1m]";
43
+ this.maxTurns = deps.maxTurns ?? 50;
44
+ this.allowedTools = deps.allowedTools ?? DEFAULT_ALLOWED_TOOLS;
45
+ this.onLine = deps.onLine ?? null;
46
+ this.settingSources = deps.settingSources ?? [];
47
+ this.systemPrompt = deps.systemPrompt ?? null;
48
+ this.disallowedTools = deps.disallowedTools ?? [];
49
+ this.mcpServers = deps.mcpServers ?? null;
50
+ this.taskAmend = deps.taskAmend ?? null;
60
51
  this.sessionId = null;
61
- this.buffer = [];
62
52
  /** @type {AbortController|null} */
63
53
  this.currentAbortController = null;
64
54
  }
65
55
 
66
56
  /**
67
57
  * Run a new agent session with the given task.
68
- * @param {string} task - The task prompt
58
+ * @param {string} task
69
59
  * @returns {Promise<{success: boolean, text: string, sessionId: string|null, error: Error|null, aborted: boolean}>}
70
60
  */
71
61
  async run(task) {
@@ -87,7 +77,7 @@ export class AgentRunner {
87
77
 
88
78
  /**
89
79
  * Resume an existing session with a follow-up prompt.
90
- * @param {string} prompt - The follow-up prompt
80
+ * @param {string} prompt
91
81
  * @returns {Promise<{success: boolean, text: string, sessionId: string|null, error: Error|null, aborted: boolean}>}
92
82
  */
93
83
  async resume(prompt) {
@@ -108,17 +98,16 @@ export class AgentRunner {
108
98
  }
109
99
 
110
100
  /**
111
- * Build the options passed to every SDK query() call. Shared by run() and
112
- * resume() so the agent's configuration — cwd, tools, prompt, setting
113
- * sources, turn budget — is identical across the session's lifetime. Only
114
- * resume() layers `resume: this.sessionId` on top.
101
+ * Build the options passed to every SDK query() call. Shared by run()
102
+ * and resume() so the agent's configuration — cwd, tools, prompt,
103
+ * setting sources, turn budget — is identical across the session's
104
+ * lifetime. Only resume() layers `resume: this.sessionId` on top.
115
105
  *
116
- * SDK options are call-attached, not session-attached: the resumed call
117
- * loads the prior conversation but otherwise uses whatever options this
118
- * call passes. Omitting tool/prompt/setting options on resume causes the
119
- * agent to silently lose its restrictions and persona between turns.
120
- * @param {AbortController} abortController
121
- * @returns {object}
106
+ * SDK options are call-attached, not session-attached: the resumed
107
+ * call loads the prior conversation but otherwise uses whatever
108
+ * options this call passes. Omitting tool/prompt/setting options on
109
+ * resume causes the agent to silently lose its restrictions and
110
+ * persona between turns.
122
111
  */
123
112
  #callOptions(abortController) {
124
113
  return {
@@ -139,59 +128,28 @@ export class AgentRunner {
139
128
  }
140
129
 
141
130
  /**
142
- * Shared consumer for both `run()` and `resume()`. Iterates the SDK query
143
- * iterator, mirroring every line to the output stream / buffer / onLine
144
- * callback, and — when `onBatch` is set flushes accumulated lines to it
145
- * at coarse boundaries: every `batchSize` assistant text-block messages,
146
- * and the terminal `result` message. Tool-only assistant messages still
147
- * accumulate in the pending batch and ride along in the next flush, so
148
- * the supervisor always sees the tool calls that led up to each text
149
- * block. Raising `batchSize` above 1 is the knob that makes the mid-turn
150
- * supervisor review less chatty — with the default of 3, the supervisor
151
- * sees the agent in chunks of three text turns instead of every turn.
152
- *
153
- * Corollary: a turn that is *entirely* tool_use with no text blocks and
154
- * then hits `result` produces exactly one flush at `result` regardless
155
- * of how many tools ran. That is deliberate — the supervisor only needs
156
- * to weigh in when the agent surfaces something text-like to react to.
157
- *
158
- * INVARIANT: the `await this.onBatch(...)` call below is the ONLY
159
- * suspension point in this loop. While it is pending, no further lines
160
- * are pulled from the SDK generator. The Supervisor relies on this — its
161
- * onBatch callback flips `currentSource` to "supervisor" for the duration
162
- * of its mid-turn LLM call, and the invariant guarantees no agent line
163
- * can arrive concurrently and be mis-tagged.
164
- *
165
- * If the supervisor calls `abort()` from inside the callback, the next
166
- * iteration of the for-await loop will throw. We catch the throw, check
167
- * `currentAbortController.signal.aborted` (avoiding fragility around
168
- * AbortError vs DOMException shapes), and report `aborted: true` so the
169
- * caller can distinguish "supervisor asked us to stop" from a real error.
131
+ * Iterate the SDK query iterator, mirroring every message to the
132
+ * output stream and the `onLine` callback. Captures `sessionId` from
133
+ * the SDK's `system/init` message and tracks Skill invocations into
134
+ * `LIBEVAL_SKILL` for downstream metrics.
170
135
  *
171
- * If the iterator throws before a flush boundary, any lines still in the
172
- * pending batch would otherwise vanish without the supervisor seeing
173
- * them. The `finally` block emits a terminal batch so the supervisor can
174
- * observe the partial state (e.g. note a crash or react to an external
175
- * abort). A throw from that final flush becomes the returned `error`
176
- * only if no earlier error was captured — the original failure wins.
177
- * @param {AsyncIterable<object>} iterator
178
- * @returns {Promise<{success: boolean, text: string, sessionId: string|null, error: Error|null, aborted: boolean}>}
136
+ * If the iterator throws and we triggered the abort ourselves
137
+ * (`currentAbortController.signal.aborted`), we report `aborted:
138
+ * true`; otherwise the error propagates as `error`.
179
139
  */
180
140
  async #consumeQuery(iterator) {
181
141
  let text = "";
182
142
  let stopReason = null;
183
143
  let error = null;
184
144
  let aborted = false;
185
- const state = { pendingBatch: [], assistantTextCount: 0 };
186
145
 
187
146
  try {
188
147
  for await (const message of iterator) {
189
- this.#recordLine(message, state);
148
+ this.#recordLine(message);
190
149
  if (message.type === "result") {
191
150
  text = message.result ?? "";
192
151
  stopReason = message.subtype;
193
152
  }
194
- await this.#maybeFlushBatch(message, state);
195
153
  }
196
154
  } catch (err) {
197
155
  if (this.currentAbortController?.signal.aborted) {
@@ -201,118 +159,28 @@ export class AgentRunner {
201
159
  }
202
160
  }
203
161
 
204
- const flushErr = await this.#terminalFlush(state, { error, aborted });
205
- if (flushErr && !error) error = flushErr;
206
-
207
- const success = stopReason === "success";
208
- return { success, text, sessionId: this.sessionId, error, aborted };
162
+ return {
163
+ success: stopReason === "success",
164
+ text,
165
+ sessionId: this.sessionId,
166
+ error,
167
+ aborted,
168
+ };
209
169
  }
210
170
 
211
- /**
212
- * Mirror a single SDK message to the output stream, buffer, onLine
213
- * callback, and (when set) the pending-batch state. Also handles
214
- * session id capture and text-block counting so `#consumeQuery` can
215
- * stay within the complexity budget.
216
- * @param {object} message
217
- * @param {{pendingBatch: string[], assistantTextCount: number}} state
218
- */
219
- #recordLine(message, state) {
171
+ #recordLine(message) {
220
172
  const redacted = this.redactor.redactValue(message);
221
173
  const line = JSON.stringify(redacted);
222
174
  this.output.write(line + "\n");
223
- this.buffer.push(line);
224
175
  if (this.onLine) this.onLine(line);
225
- if (this.onBatch) state.pendingBatch.push(line);
226
176
 
227
- // Session-id / text-block tracking reads the ORIGINAL message —
228
- // these fields are not secret carriers, and the trackers rely on
229
- // shape, not string contents.
230
177
  if (message.type === "system" && message.subtype === "init") {
231
178
  this.sessionId = message.session_id;
232
179
  }
233
- if (message.type === "assistant") {
234
- if (hasTextBlock(message)) state.assistantTextCount++;
235
- trackSkillInvocation(message);
236
- }
237
- }
238
-
239
- /**
240
- * Terminal flush — only fires on the abnormal-end paths (iterator
241
- * threw or was aborted mid-stream). Delivers any pending lines so the
242
- * supervisor sees the partial state instead of losing the tail of
243
- * the run. A natural-end iterator that simply ran out of messages
244
- * without a `result` marker is treated as an incomplete stub (the
245
- * real SDK always terminates with `result`) and its pending batch is
246
- * not re-flushed. Returns an error thrown by the flush callback, or
247
- * `null` if the flush succeeded or did not fire.
248
- * @param {{pendingBatch: string[], assistantTextCount: number}} state
249
- * @param {{error: Error|null, aborted: boolean}} outcome
250
- * @returns {Promise<Error|null>}
251
- */
252
- async #terminalFlush(state, { error, aborted }) {
253
- const loopEndedAbnormally = Boolean(error || aborted);
254
- if (!loopEndedAbnormally) return null;
255
- if (!this.onBatch || state.pendingBatch.length === 0) return null;
256
- try {
257
- const batchLines = state.pendingBatch.splice(0);
258
- await this.onBatch(batchLines, {
259
- abort: () => this.currentAbortController?.abort(),
260
- });
261
- return null;
262
- } catch (flushErr) {
263
- return flushErr;
264
- }
265
- }
266
-
267
- /**
268
- * Flush the pending batch to `onBatch` if either the batchSize threshold
269
- * has been reached or the current message is the terminal `result`.
270
- * Extracted so that `#consumeQuery` stays within the project's complexity
271
- * budget — the flush is one cohesive unit of logic in its own right.
272
- * @param {object} message
273
- * @param {{pendingBatch: string[], assistantTextCount: number}} state
274
- */
275
- async #maybeFlushBatch(message, state) {
276
- if (!this.onBatch) return;
277
- const shouldFlush =
278
- message.type === "result" || state.assistantTextCount >= this.batchSize;
279
- if (!shouldFlush) return;
280
- state.assistantTextCount = 0;
281
- const batchLines = state.pendingBatch.splice(0);
282
- await this.onBatch(batchLines, {
283
- abort: () => this.currentAbortController?.abort(),
284
- });
285
- }
286
-
287
- /**
288
- * Drain buffered output lines. Used by Supervisor to tag and re-emit lines.
289
- * @returns {string[]}
290
- */
291
- drainOutput() {
292
- const lines = [...this.buffer];
293
- this.buffer = [];
294
- return lines;
180
+ if (message.type === "assistant") trackSkillInvocation(message);
295
181
  }
296
182
  }
297
183
 
298
- /**
299
- * Whether an SDK assistant message contains at least one text block.
300
- * Only text-block messages count toward the `batchSize` threshold — tool-only
301
- * assistant messages accumulate silently into the pending batch and ride along
302
- * in the next flush, keeping supervisor LLM cost bounded. Exported so the mock
303
- * runner can mirror the real flush predicate without duplicating the logic.
304
- * @param {object} message
305
- * @returns {boolean}
306
- */
307
- export function hasTextBlock(message) {
308
- const content = message.message?.content ?? message.content;
309
- if (!Array.isArray(content)) return false;
310
- for (const block of content) {
311
- if (block.type === "text" && block.text) return true;
312
- }
313
- return false;
314
- }
315
-
316
184
  function trackSkillInvocation(message) {
317
185
  const content = message.message?.content ?? message.content;
318
186
  if (!Array.isArray(content)) return;
@@ -327,11 +195,7 @@ function trackSkillInvocation(message) {
327
195
  }
328
196
  }
329
197
 
330
- /**
331
- * Factory function — wires real dependencies.
332
- * @param {object} deps - Same as AgentRunner constructor
333
- * @returns {AgentRunner}
334
- */
198
+ /** Factory function — wires real dependencies. */
335
199
  export function createAgentRunner(deps) {
336
200
  return new AgentRunner(deps);
337
201
  }
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Phases per (task, runIndex):
5
5
  * 1. WorkdirManager.start → seed CWD + run pre-flight probe
6
- * 2. Supervisor relay (agent + supervisor) → produce traces + submission
6
+ * 2. Supervisor session (agent + supervisor) → produce traces + submission
7
7
  * 3. Scorer.runScoring → exit-code-driven verdict via fd-3 NDJSON
8
8
  * 4. Judge.runJudge → Conclude-driven verdict mapped to pass/fail
9
9
  * 5. WorkdirManager.teardown → process-group cleanup
@@ -272,7 +272,7 @@ export class BenchmarkRunner {
272
272
  }
273
273
 
274
274
  /**
275
- * Run the agent-under-test via a Supervisor relay. The supervisor writes
275
+ * Run the agent-under-test under a Supervisor. The supervisor writes
276
276
  * a combined tagged NDJSON trace; after the session we split it into
277
277
  * agent.ndjson and supervisor.ndjson and extract cost/turns/submission.
278
278
  */
@@ -53,7 +53,9 @@ export function parseSuperviseOptions(values) {
53
53
  }
54
54
 
55
55
  /**
56
- * Supervise command — run two agents in a relay loop via the Claude Agent SDK.
56
+ * Supervise command — run one agent under a supervisor via the
57
+ * orchestration loop. The supervisor delegates work through Ask, sees
58
+ * each reply on its next turn, and ends with Conclude.
57
59
  *
58
60
  * Usage: fit-eval supervise [options]
59
61
  *
@@ -1,32 +1,33 @@
1
1
  /**
2
- * DiscussTools — tool servers and prompts for the `discuss` orchestration
3
- * mode. The lead's set is sibling to (not derived from) the facilitator's:
4
- * `Conclude` is absent; instead `Adjourn` (terminal verdict) and `Recess`
5
- * (suspend with a ResumeTrigger) end a run, and `RequestForComment` queues
6
- * structured replies onto the trace for the bridge to deliver after the
7
- * workflow run completes.
2
+ * DiscussTools — discuss-mode tool servers. The lead's surface extends the
3
+ * base set with three discuss-only terminal tools:
8
4
  *
9
- * Discuss-mode prompts and tool wiring stay in this module; nothing here
10
- * imports from `facilitator.js`.
5
+ * - `RequestForComment` posts a fire-and-forget message to a human channel
6
+ * via the bridge; the reply arrives on a later workflow run.
7
+ * - `Recess` suspends the session with a resumption trigger.
8
+ * - `Adjourn` ends the discussion with a verdict.
9
+ *
10
+ * `Conclude` is absent — discuss mode ends via Adjourn or Recess. The
11
+ * agent surface is identical to the facilitated agent's: Ask / Answer /
12
+ * Announce / RollCall, with Ask defaulting to the lead.
11
13
  */
12
14
 
13
- import { createSdkMcpServer, tool } from "@anthropic-ai/claude-agent-sdk";
15
+ import { tool } from "@anthropic-ai/claude-agent-sdk";
14
16
  import { z } from "zod";
15
17
 
16
18
  import {
17
- createAskHandler,
18
- createAnswerHandler,
19
- createAnnounceHandler,
20
- createRollCallHandler,
21
- createRedirectHandler,
19
+ baseTools,
20
+ concludeSession,
21
+ orchestrationServer,
22
22
  } from "./orchestration-toolkit.js";
23
23
 
24
24
  /** System prompt appended for discuss-mode agent runners. */
25
25
  export const DISCUSS_AGENT_SYSTEM_PROMPT =
26
26
  "You participate in an asynchronous discussion. " +
27
- "Answer replies to an ask addressed to you. " +
28
- "Ask sends a question to the lead or another participant. " +
29
- "Announce broadcasts a message. " +
27
+ "Each question you receive carries an [ask#N] header quote that N back as the askId field on Answer so the reply pairs with the right question. " +
28
+ "Answer replies to an ask addressed to you. askId is optional: omit it and the handler auto-picks if exactly one ask is owed to you, otherwise it routes your message as an Announce. " +
29
+ "Ask sends a question to the lead or another participant and returns immediately with {askIds:[N]}; the reply arrives on a later turn as `[answer#N] <participant>: <text>` in your inbox. " +
30
+ "Announce broadcasts a message to every other participant — use this for unsolicited remarks or to reply to an Announce. " +
30
31
  "RollCall lists participants.";
31
32
 
32
33
  const RESUME_TRIGGER_SCHEMA = z
@@ -37,128 +38,51 @@ const RESUME_TRIGGER_SCHEMA = z
37
38
  })
38
39
  .strict();
39
40
 
40
- /**
41
- * Lead tools for the discusser. The discuss-mode surface is Ask / Answer /
42
- * Announce / Redirect / RollCall plus the discuss-only RequestForComment,
43
- * Recess, and Adjourn. `Conclude` is intentionally absent — discuss mode
44
- * ends via Adjourn or Recess, never Conclude. `RequestForComment` writes
45
- * a structured reply onto `ctx.replies[]`; the discusser flushes those
46
- * into the terminal summary event at end-of-run.
47
- *
48
- * @param {object} ctx - Orchestration context (must carry `replies` array)
49
- * @returns {object} MCP server config (type: "sdk")
50
- */
41
+ /** Discuss-mode lead tool server. */
51
42
  export function createDiscussLeadToolServer(ctx) {
52
- return createSdkMcpServer({
53
- name: "orchestration",
54
- tools: [
55
- tool(
56
- "RollCall",
57
- "List all participants in the session.",
58
- {},
59
- createRollCallHandler(ctx),
60
- ),
61
- tool(
62
- "Ask",
63
- "Send a question to a participant. Omit 'to' to broadcast. The reply arrives via Answer.",
64
- { question: z.string(), to: z.string().optional() },
65
- createAskHandler(ctx, { from: "lead", defaultTo: undefined }),
66
- ),
67
- tool(
68
- "Answer",
69
- "Reply to an ask addressed to you.",
70
- { message: z.string() },
71
- createAnswerHandler(ctx, { from: "lead" }),
72
- ),
73
- tool(
74
- "Announce",
75
- "Broadcast a message with no reply expected.",
76
- { message: z.string() },
77
- createAnnounceHandler(ctx, { from: "lead" }),
78
- ),
79
- tool(
80
- "Redirect",
81
- "Interrupt a participant with replacement instructions.",
82
- { message: z.string(), to: z.string().optional() },
83
- createRedirectHandler(ctx),
84
- ),
85
- tool(
86
- "RequestForComment",
87
- "Post a fire-and-forget message to a channel via the bridge. Returns a correlation id; the reply arrives on a later workflow run.",
88
- {
89
- channel: z.string(),
90
- body: z.string(),
91
- addressees: z.array(z.string()).optional(),
92
- },
93
- createRequestForCommentHandler(ctx),
94
- ),
95
- tool(
96
- "Recess",
97
- "Suspend the run. The bridge re-dispatches the workflow when the trigger fires.",
98
- { reason: z.string(), trigger: RESUME_TRIGGER_SCHEMA },
99
- createRecessHandler(ctx),
100
- ),
101
- tool(
102
- "Adjourn",
103
- "End the discussion with a verdict and a summary.",
104
- {
105
- verdict: z.enum(["adjourned", "failed"]),
106
- summary: z.string(),
107
- outcome: z.string().optional(),
108
- },
109
- createAdjournHandler(ctx),
110
- ),
111
- ],
112
- });
43
+ return orchestrationServer([
44
+ ...baseTools(ctx, { from: "lead", defaultTo: undefined, broadcast: true }),
45
+ tool(
46
+ "RequestForComment",
47
+ "Post a fire-and-forget message to a channel via the bridge. Returns a correlation id; the reply arrives on a later workflow run.",
48
+ {
49
+ channel: z.string(),
50
+ body: z.string(),
51
+ addressees: z.array(z.string()).optional(),
52
+ },
53
+ createRequestForCommentHandler(ctx),
54
+ ),
55
+ tool(
56
+ "Recess",
57
+ "Suspend the run. The bridge re-dispatches the workflow when the trigger fires.",
58
+ { reason: z.string(), trigger: RESUME_TRIGGER_SCHEMA },
59
+ createRecessHandler(ctx),
60
+ ),
61
+ tool(
62
+ "Adjourn",
63
+ "End the discussion with a verdict ('adjourned' / 'failed') and a summary.",
64
+ {
65
+ verdict: z.enum(["adjourned", "failed"]),
66
+ summary: z.string(),
67
+ outcome: z.string().optional(),
68
+ },
69
+ createAdjournHandler(ctx),
70
+ ),
71
+ ]);
113
72
  }
114
73
 
115
- /**
116
- * Discuss-mode agent tools: Ask / Answer / Announce / RollCall. Surface is
117
- * defined here (not borrowed from facilitate mode) so the two modes stay
118
- * structurally independent.
119
- *
120
- * @param {object} ctx - Orchestration context
121
- * @param {{from: string}} opts - Agent name (canonical)
122
- * @returns {object} MCP server config (type: "sdk")
123
- */
74
+ /** Discuss-mode agent tool server. */
124
75
  export function createDiscussAgentToolServer(ctx, { from }) {
125
- return createSdkMcpServer({
126
- name: "orchestration",
127
- tools: [
128
- tool(
129
- "Ask",
130
- "Send a question to another participant. Omit 'to' to ask the lead.",
131
- { question: z.string(), to: z.string().optional() },
132
- createAskHandler(ctx, { from, defaultTo: "lead" }),
133
- ),
134
- tool(
135
- "Answer",
136
- "Reply to an ask addressed to you.",
137
- { message: z.string() },
138
- createAnswerHandler(ctx, { from }),
139
- ),
140
- tool(
141
- "Announce",
142
- "Broadcast a message with no reply expected.",
143
- { message: z.string() },
144
- createAnnounceHandler(ctx, { from }),
145
- ),
146
- tool(
147
- "RollCall",
148
- "List all participants in the session.",
149
- {},
150
- createRollCallHandler(ctx),
151
- ),
152
- ],
153
- });
76
+ return orchestrationServer(
77
+ baseTools(ctx, { from, defaultTo: "lead", broadcast: true }),
78
+ );
154
79
  }
155
80
 
156
- /** Create a RequestForComment handler. Queues a reply into ctx.replies[]. */
81
+ /** RequestForComment handler queues structured replies on `ctx.replies[]`. */
157
82
  export function createRequestForCommentHandler(ctx) {
158
83
  return async ({ channel, body, addressees }) => {
159
84
  const correlationId = `rfc_${++ctx.rfcCounter}`;
160
- const addresseeList =
161
- Array.isArray(addressees) && addressees.length > 0 ? addressees : [null];
85
+ const addresseeList = addressees?.length ? addressees : [null];
162
86
  for (const addressee of addresseeList) {
163
87
  ctx.replies.push({
164
88
  ...(addressee && { addressee }),
@@ -178,26 +102,34 @@ export function createRequestForCommentHandler(ctx) {
178
102
  };
179
103
  }
180
104
 
181
- /** Create a Recess handler. Marks the session as recessed with a trigger. */
105
+ /**
106
+ * Recess handler — ends the run with a structured pause + resumption
107
+ * trigger; cancels any open Asks so askers see a synthetic null answer.
108
+ * `concluded` flips true (same as Adjourn); the `recessed` verdict
109
+ * distinguishes them, and `recessTrigger` carries the resume shape for
110
+ * the bridge.
111
+ */
182
112
  export function createRecessHandler(ctx) {
183
113
  return async ({ reason, trigger }) => {
184
- ctx.recessed = true;
185
114
  ctx.recessTrigger = trigger;
186
- ctx.recessReason = reason;
187
- ctx.concluded = true;
188
- ctx.verdict = "recessed";
189
- ctx.summary = reason;
115
+ concludeSession(ctx, {
116
+ verdict: "recessed",
117
+ summary: reason,
118
+ reason: "session recessed",
119
+ });
190
120
  return { content: [{ type: "text", text: "Recess queued." }] };
191
121
  };
192
122
  }
193
123
 
194
- /** Create an Adjourn handler. Marks the session as concluded with a verdict. */
124
+ /** Adjourn handler ends the discussion with a verdict. */
195
125
  export function createAdjournHandler(ctx) {
196
126
  return async ({ verdict, summary, outcome }) => {
197
- ctx.concluded = true;
198
- ctx.verdict = verdict;
199
- ctx.summary = summary;
200
127
  if (outcome !== undefined) ctx.outcome = outcome;
128
+ concludeSession(ctx, {
129
+ verdict,
130
+ summary,
131
+ reason: "session adjourned",
132
+ });
201
133
  return { content: [{ type: "text", text: "Session adjourned." }] };
202
134
  };
203
135
  }