-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodeAgentRuntime.ts
More file actions
186 lines (175 loc) · 6.1 KB
/
Copy pathnodeAgentRuntime.ts
File metadata and controls
186 lines (175 loc) · 6.1 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* NodeAgent runtime — the loop that makes four surfaces one agent.
*
* Orchestrator-workers in miniature: gather context from the room, search &
* synthesize a grounded answer, apply a versioned model delta, write the memo.
* Each step is bounded, each failure is surfaced (no step silently "succeeds"),
* and the overall status is honest: `ok` only when every step completed.
*
* This is where eval/reliability discipline shows up as code — the same loop
* runs live (streaming, real keys) or deterministically (demo, no keys).
*
* Prior art: Anthropic "Building Effective Agents" (orchestrator-workers);
* NodeBench AI scratchpad-first pipeline. See .claude/rules/orchestrator_workers.md.
*/
import { collectContext } from "../../chat/contextCollector";
import { searchAndSynthesize, type SynthesizeOptions } from "../../search/searchAndSynthesize";
import { VersionedSpreadsheetSync } from "../../spreadsheet/versionedSpreadsheetSync";
import {
appendParagraph,
createNotebook,
insertCitation,
insertClaim,
} from "../../notebook/notebookEditor";
import type {
AgentRunResult,
AgentStep,
AppliedDelta,
RoomContext,
SearchSource,
SpreadsheetDelta,
SpreadsheetModel,
StepName,
} from "../types/nodeAgentTypes";
export interface RunInput {
question: string;
room: RoomContext;
sources: SearchSource[];
/** Optional model + delta to apply during the loop. */
model?: SpreadsheetModel;
modelDelta?: SpreadsheetDelta;
memoTitle?: string;
synthesize?: SynthesizeOptions;
/** Injected clock for deterministic runs. */
now?: number;
}
/**
* Run the full agent loop. Always returns a result — never throws — so an agent
* orchestrator calling this in a swarm gets structured partial output on failure
* rather than a crash that takes down concurrent lanes (ERROR_BOUNDARY).
*/
export function runNodeAgent(input: RunInput): AgentRunResult {
const now = input.now ?? Date.now();
const steps: AgentStep[] = [];
const mkStep = (name: StepName): AgentStep => {
const s: AgentStep = { name, status: "active", detail: "", durationMs: 0 };
steps.push(s);
return s;
};
/* 1 — gather context from the room */
const gather = mkStep("gather");
const context = safe(
gather,
() => collectContext(input.room, input.question, { now }),
{
focus: input.question,
items: [],
activeParticipants: 0,
truncated: false,
},
);
gather.detail = `${context.items.length} context items · ${context.activeParticipants} active${context.truncated ? " · truncated" : ""}`;
/* 2 — search & synthesize */
const search = mkStep("search");
const synthesis = safe(
search,
() => searchAndSynthesize(input.question, input.sources, input.synthesize),
{
query: input.question,
confidence: "low" as const,
answer: "",
sources: [],
citations: [],
groundedCount: 0,
note: "search step failed",
},
);
if (synthesis.confidence === "low") {
search.status = "error";
search.detail = synthesis.note ?? "insufficient grounding";
} else {
search.detail = `${synthesis.groundedCount} grounded · confidence ${synthesis.confidence}`;
}
/* 3 — apply the versioned model delta (optional) */
const modelStep = mkStep("model");
let modelDelta: AppliedDelta | null = null;
if (input.model && input.modelDelta) {
const sync = new VersionedSpreadsheetSync(input.model);
const outcome = sync.commit(input.modelDelta, now);
if (outcome.ok) {
modelDelta = outcome.applied;
modelStep.detail = `v${outcome.applied.fromVersion} → v${outcome.applied.toVersion} · ${outcome.applied.changes.length} cells${outcome.rebased ? " · rebased" : ""}`;
modelStep.status = "done";
} else if (outcome.conflict) {
modelStep.status = "error";
modelStep.detail = `version conflict on ${outcome.cells.join(", ")}`;
} else {
modelStep.status = "error";
modelStep.detail = outcome.error;
}
} else {
modelStep.detail = "no model change requested";
modelStep.status = "done";
}
/* 4 — write the memo */
const memoStep = mkStep("memo");
let memo = createNotebook(input.memoTitle ?? "Agent memo", now);
memo = appendParagraph(memo, `The room asked: "${input.question}"`, now);
if (synthesis.answer) {
memo = insertClaim(
memo,
{
text: stripMarkers(synthesis.answer),
evidence: synthesis.citations,
groundedRatio: `${synthesis.groundedCount}/${synthesis.sources.length}`,
},
now,
);
const winner = synthesis.sources.find((s) => s.winner) ?? synthesis.sources[0];
if (winner) {
memo = insertCitation(
memo,
{ index: winner.citation, sourceId: winner.id, title: winner.title, url: winner.url },
now,
);
}
} else {
memo = appendParagraph(memo, "No grounded answer was produced; manual review required.", now);
}
if (modelDelta) {
const runway = modelDelta.changes.find((c) => c.address.toLowerCase().includes("runway"));
memo = appendParagraph(
memo,
runway
? `Model updated to v${modelDelta.toVersion}: runway now ${runway.to}.`
: `Model updated to v${modelDelta.toVersion} (${modelDelta.changes.length} cells).`,
now,
);
}
memoStep.detail = `${memo.blocks.length} blocks`;
memoStep.status = "done";
// Honest overall status.
const hadError = steps.some((s) => s.status === "error");
const status: AgentRunResult["status"] = hadError
? synthesis.answer
? "partial"
: "error"
: "ok";
return { question: input.question, steps, context, synthesis, modelDelta, memo, status };
}
/** Run a step body; on throw, mark the step errored and fall back. */
function safe<T>(step: AgentStep, fn: () => T, fallback: T): T {
try {
const out = fn();
if (step.status === "active") step.status = "done";
return out;
} catch (e) {
step.status = "error";
step.detail = e instanceof Error ? e.message : "step failed";
return fallback;
}
}
function stripMarkers(answer: string): string {
// Keep the prose, drop nothing — the [n] markers ARE the citation chain.
return answer.trim();
}