@agentpro/scroll 0.0.0-stage → 2.5.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.
- package/AGENTS.md +55 -0
- package/LICENSE +21 -0
- package/README.md +293 -2
- package/SPEC.md +1079 -0
- package/bin/scroll.js +8 -0
- package/docs/prompt-caching.md +128 -0
- package/index.js +33 -0
- package/lib/audit.js +358 -0
- package/lib/auth.js +849 -0
- package/lib/chat-sdk/admin-controller.js +289 -0
- package/lib/chat-sdk/debouncer.js +94 -0
- package/lib/chat-sdk/group-grants.js +150 -0
- package/lib/chat-sdk/index.js +222 -0
- package/lib/chat-sdk/long-task-runner.js +413 -0
- package/lib/chat-session.js +1788 -0
- package/lib/chat-telemetry.js +557 -0
- package/lib/codex-app-server.js +851 -0
- package/lib/digest.js +112 -0
- package/lib/effects.js +157 -0
- package/lib/eval.js +408 -0
- package/lib/grounding.js +356 -0
- package/lib/installers.js +176 -0
- package/lib/knowledge.js +335 -0
- package/lib/ledger.js +38 -0
- package/lib/loop.js +197 -0
- package/lib/mcp-client.js +926 -0
- package/lib/memory-cli.js +66 -0
- package/lib/memory-tracker.js +1472 -0
- package/lib/memory.js +1279 -0
- package/lib/people-cli.js +81 -0
- package/lib/people.js +534 -0
- package/lib/permissions.js +272 -0
- package/lib/providers.js +1572 -0
- package/lib/runtime.js +928 -0
- package/lib/sandbox.js +744 -0
- package/lib/scheduler.js +402 -0
- package/lib/scroll.js +1225 -0
- package/lib/skill-cli.js +120 -0
- package/lib/skill-eval.js +156 -0
- package/lib/skills.js +860 -0
- package/lib/style.js +126 -0
- package/lib/tool-retriever.js +489 -0
- package/lib/tools.js +1833 -0
- package/lib/user.js +711 -0
- package/lib/vault.js +151 -0
- package/package.json +88 -4
- package/templates/agent/IDENTITY.md +56 -0
- package/templates/agent/SOUL.md +23 -0
- package/templates/agent/TOOLS.md +25 -0
- package/templates/agent/evals/case-02-citation-discipline.md +29 -0
- package/templates/agent/evals/case-03-boundary.md +27 -0
- package/templates/agent/evals/case-example.md +43 -0
- package/templates/agent/hard-rules.md +11 -0
- package/templates/agent/memory/lessons.md +5 -0
- package/templates/agent/memory/log/.gitkeep +0 -0
- package/templates/external-safety-contract.md +5 -0
- package/templates/long-task-planner.md +17 -0
- package/templates/work/LOOP.md +37 -0
- package/templates/work/WORK.md +50 -0
- package/types/chat.d.ts +61 -0
- package/types/index.d.ts +112 -0
package/lib/runtime.js
ADDED
|
@@ -0,0 +1,928 @@
|
|
|
1
|
+
// SCROLL — the runtime. `scroll run` drives a thin, file-based multi-agent loop.
|
|
2
|
+
//
|
|
3
|
+
// Design (stays true to the thesis — no engine/DB/bus):
|
|
4
|
+
// • One controller owns WORK.md (a declarative DAG of tasks).
|
|
5
|
+
// • The runner advances the DAG DETERMINISTICALLY (ready tasks = deps done),
|
|
6
|
+
// the LLM only acts INSIDE a step.
|
|
7
|
+
// • Agents coordinate through the filesystem: each task appends to blackboard/.
|
|
8
|
+
// • A cost gate decides single vs multi BEFORE spawning.
|
|
9
|
+
// • Hard caps + a circuit breaker bound spend/time/iterations.
|
|
10
|
+
// • Context is ordered stable-prefix-first so providers can cache it.
|
|
11
|
+
// • Every step is verified and checkpointed; everything is an append-only event.
|
|
12
|
+
import fs from 'node:fs';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import { spawnSync } from 'node:child_process';
|
|
15
|
+
import { parseYaml, parseFrontmatter, loadAgent, estimateCost } from './scroll.js';
|
|
16
|
+
import { pickProvider, approxTokens } from './providers.js';
|
|
17
|
+
import { resolveTier, policyForTier, needsGrounding, evaluateAction, loadMcpRisk, loadAgentSecurity, normalizeTier } from './permissions.js';
|
|
18
|
+
import { collectSources, checkGrounding, collectPrincipalSources, groundReply, verifyClaimsInvariant } from './grounding.js';
|
|
19
|
+
import { enforceStyle, hasBareNumericSlider } from './style.js';
|
|
20
|
+
import { writeDigest } from './digest.js';
|
|
21
|
+
import { openStore } from './memory.js';
|
|
22
|
+
import { openSkills, resolveSources, bindingsToolFor } from './skills.js';
|
|
23
|
+
import { openTeam } from './people.js';
|
|
24
|
+
import { openUser, classifyTurn, isIdentityQuestion, checkAiDisclosure, applyAiLabel } from './user.js';
|
|
25
|
+
import { openKnowledge, formatKnowledgeBlock } from './knowledge.js';
|
|
26
|
+
import crypto from 'node:crypto';
|
|
27
|
+
|
|
28
|
+
const PRICING = { input: 3, cachedInput: 0.3, output: 15 }; // $/million tokens
|
|
29
|
+
const DEFAULT_CAPS = { maxIterations: 20, maxInputTokens: 120000, maxWallClockMs: 900000, maxUsd: 5, recursionDepth: 2 };
|
|
30
|
+
|
|
31
|
+
// ── WORK.md parsing ───────────────────────────────────────────────────────────
|
|
32
|
+
// Tasks are fenced ```task blocks of YAML (same shape `scroll check` validates).
|
|
33
|
+
export function parseWork(raw) {
|
|
34
|
+
const { frontmatter } = parseFrontmatter(raw.startsWith('---') ? raw : `---\n---\n${raw}`);
|
|
35
|
+
const controller = (frontmatter && frontmatter.controller) || null;
|
|
36
|
+
const blocks = [...raw.matchAll(/```task\n([\s\S]*?)```/g)].map((m) => m[1]);
|
|
37
|
+
const tasks = [];
|
|
38
|
+
for (const b of blocks) {
|
|
39
|
+
let t; try { t = parseYaml(b); } catch { continue; }
|
|
40
|
+
if (!t || !t.id) continue;
|
|
41
|
+
// v1.9 §40 — owner: agent:<name> | person:<slug> | <name> (an agent, as before)
|
|
42
|
+
const rawOwner = String(t.owner || controller || 'controller');
|
|
43
|
+
const person = rawOwner.startsWith('person:') ? rawOwner.slice(7) : null;
|
|
44
|
+
const owner = rawOwner.replace(/^agent:/, '');
|
|
45
|
+
tasks.push({
|
|
46
|
+
id: t.id, title: t.title || t.id, status: t.status || 'todo',
|
|
47
|
+
owner, person, agent: person ? null : (t.agent || owner || controller),
|
|
48
|
+
objective: t.objective || t.title || '', output_format: t.output_format || 'decision-ready markdown',
|
|
49
|
+
source_guidance: arr(t.source_guidance), boundaries: arr(t.boundaries),
|
|
50
|
+
blockedBy: arr(t.blockedBy), parallel: t.parallel === true,
|
|
51
|
+
risk: t.risk || null, tool: t.tool || null, ground: t.ground || null, final: t.final === true,
|
|
52
|
+
deterministic: t.deterministic === true, op: t.op || 'merge',
|
|
53
|
+
engine: t.engine || null, effort: t.effort || null, sandbox: t.sandbox || null,
|
|
54
|
+
workdir: t.workdir || null, timeout_ms: t.timeout_ms || null,
|
|
55
|
+
subject: t.subject || null, memory_topics: arr(t.memory_topics), // v1.7 §31.5: whose memory this step may read
|
|
56
|
+
skills: arr(t.skills), // v1.8 §34.7: skills this step loads in full
|
|
57
|
+
for: t.for ? String(t.for).replace(/^person:/, '') : null, people: arr(t.people).map((x) => String(x)), // v1.9 §39
|
|
58
|
+
action: t.action || null, amount: t.amount ?? null, // v1.9 §40: what the gate checks the approver against
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return { controller: String(controller || (tasks.find((x) => !x.person) || tasks[0] || {}).owner || 'controller').replace(/^agent:/, ''), tasks };
|
|
62
|
+
}
|
|
63
|
+
const arr = (x) => (Array.isArray(x) ? x : x == null || x === '' ? [] : [x]);
|
|
64
|
+
|
|
65
|
+
// ── the run ─────────────────────────────────────────────────────────────────
|
|
66
|
+
export async function runWork(opts = {}) {
|
|
67
|
+
const cwd = opts.cwd || process.cwd();
|
|
68
|
+
const providerName = opts.providerName || 'auto';
|
|
69
|
+
const provider = opts.provider || pickProvider(providerName, { model: opts.model }); // an injected provider object (apps, tests)
|
|
70
|
+
|
|
71
|
+
// run dir + isolated scratch (provider calls run here so a repo CLAUDE.md can't leak in)
|
|
72
|
+
const runId = opts.runId || new Date().toISOString().replace(/[:.]/g, '-');
|
|
73
|
+
const runDir = opts.runDir ? path.resolve(opts.runDir) : path.join(cwd, 'runs', runId);
|
|
74
|
+
const blackboardDir = path.join(runDir, 'blackboard');
|
|
75
|
+
const scratchDir = path.join(runDir, 'scratch');
|
|
76
|
+
const controlDir = opts.controlDir ? path.resolve(opts.controlDir) : path.join(runDir, 'control');
|
|
77
|
+
const approvalsDir = path.join(controlDir, 'approvals'); // v1.4: must-approve gates land here
|
|
78
|
+
fs.mkdirSync(blackboardDir, { recursive: true });
|
|
79
|
+
fs.mkdirSync(scratchDir, { recursive: true });
|
|
80
|
+
const eventsPath = opts.eventsPath ? path.resolve(opts.eventsPath) : path.join(runDir, 'events.jsonl');
|
|
81
|
+
const outPath = opts.outPath ? path.resolve(opts.outPath) : path.join(runDir, 'output.md');
|
|
82
|
+
fs.mkdirSync(path.dirname(eventsPath), { recursive: true });
|
|
83
|
+
if (fs.existsSync(eventsPath) && !opts.resume) fs.rmSync(eventsPath); // resume APPENDS to the stream
|
|
84
|
+
|
|
85
|
+
const emit = (type, data = {}) => {
|
|
86
|
+
const line = JSON.stringify({ ts: new Date().toISOString(), runId, type, data });
|
|
87
|
+
fs.appendFileSync(eventsPath, line + '\n');
|
|
88
|
+
if (opts.json) console.log(line);
|
|
89
|
+
else if (opts.verbose) console.log(` ${type}${data.id ? ' ' + data.id : ''}`);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
if (!provider.ready) { emit('error', { reason: provider.missing }); throw new Error(`provider not ready: ${provider.missing}`); }
|
|
93
|
+
|
|
94
|
+
// v1.6: per-task engine routing — a task may name its own engine (codex/grok/claude/...).
|
|
95
|
+
// Providers are memoized per engine name; the run-level provider stays the default.
|
|
96
|
+
const providerCache = new Map();
|
|
97
|
+
const providerFor = (engineName) => {
|
|
98
|
+
if (!engineName) return provider;
|
|
99
|
+
if (!providerCache.has(engineName)) providerCache.set(engineName, pickProvider(engineName, {}));
|
|
100
|
+
const p = providerCache.get(engineName);
|
|
101
|
+
if (!p.ready) { emit('error', { reason: `engine ${engineName}: ${p.missing}` }); throw new Error(`provider not ready: ${p.missing}`); }
|
|
102
|
+
return p;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// build the work model (multi-agent file, or a synthesized single task)
|
|
106
|
+
let work;
|
|
107
|
+
if (opts.workFile) {
|
|
108
|
+
work = parseWork(fs.readFileSync(path.resolve(cwd, opts.workFile), 'utf8'));
|
|
109
|
+
} else if (opts.task) {
|
|
110
|
+
work = { controller: opts.agent || 'controller', tasks: [{
|
|
111
|
+
id: 'task', title: 'single task', status: 'todo', owner: opts.agent || 'controller', agent: opts.agent || null,
|
|
112
|
+
objective: opts.task, output_format: 'decision-ready markdown', source_guidance: [], boundaries: [],
|
|
113
|
+
blockedBy: [], parallel: false, risk: opts.risk || null, tool: opts.tool || null, ground: opts.ground || null, final: true,
|
|
114
|
+
skills: arr(opts.skills), person: null, for: opts.for || null, people: arr(opts.people), action: opts.action || null, amount: opts.amount ?? null,
|
|
115
|
+
}] };
|
|
116
|
+
} else {
|
|
117
|
+
throw new Error('runWork needs --work <WORK.md> or --task "<text>"');
|
|
118
|
+
}
|
|
119
|
+
if (!work.tasks.length) throw new Error('no tasks found');
|
|
120
|
+
if (opts.subject) for (const t of work.tasks) if (!t.subject) t.subject = opts.subject;
|
|
121
|
+
if (opts.for) for (const t of work.tasks) if (!t.for && !t.person) t.for = String(opts.for).replace(/^person:/, '');
|
|
122
|
+
|
|
123
|
+
// v1.9 §38–§40 — deployment USER.md (single file outside agents/, preferred) or legacy people/
|
|
124
|
+
// dir. Read once per run so prompt prefixes stay byte-stable; frontmatter stays machine-only.
|
|
125
|
+
const userDoc = openUser({ cwd, file: opts.userFile || null, config: opts.config || null });
|
|
126
|
+
const legacyTeam = openTeam({ cwd, dir: opts.peopleDir || null, config: opts.config || null });
|
|
127
|
+
const team = userDoc.active ? userDoc : legacyTeam;
|
|
128
|
+
const peopleOn = team.active || work.tasks.some((t) => t.person);
|
|
129
|
+
// v1.10 §44 — deployment knowledge/ indexer (outside agents/, per §41). Loaded once per run.
|
|
130
|
+
const knowledgeStore = opts.useKnowledge === false
|
|
131
|
+
? null
|
|
132
|
+
: openKnowledge({ cwd, dir: opts.knowledgeDir || null, config: opts.config || null });
|
|
133
|
+
let hadConversationalTurn = false;
|
|
134
|
+
// §20.6 — gate decisions about approvals go to an append-only, hash-chained audit trail
|
|
135
|
+
const auditPath = path.join(runDir, 'audit.jsonl');
|
|
136
|
+
const audit = (rec) => {
|
|
137
|
+
let prev = '';
|
|
138
|
+
if (fs.existsSync(auditPath)) { const ls = fs.readFileSync(auditPath, 'utf8').trim().split('\n').filter(Boolean); prev = ls.length ? crypto.createHash('sha256').update(ls[ls.length - 1]).digest('hex').slice(0, 32) : ''; }
|
|
139
|
+
const line = JSON.stringify({ ts: new Date().toISOString(), runId, ...rec, prev });
|
|
140
|
+
fs.appendFileSync(auditPath, line + '\n');
|
|
141
|
+
};
|
|
142
|
+
if (userDoc.exists) {
|
|
143
|
+
const hc = userDoc.checkHashChange(opts.stateDir || path.join(cwd, '.scroll'));
|
|
144
|
+
if (hc.changed) {
|
|
145
|
+
emit('user_md_changed', { file: path.relative(cwd, userDoc.file) || userDoc.file, previousHash: hc.previousHash, hash: hc.hash });
|
|
146
|
+
audit({ event: 'user_md_changed', file: path.relative(cwd, userDoc.file) || userDoc.file, previousHash: hc.previousHash, hash: hc.hash });
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const userBlocks = new Map();
|
|
150
|
+
const userFor = (audience = 'internal') => {
|
|
151
|
+
if (!userDoc.active) return '';
|
|
152
|
+
if (!userBlocks.has(audience)) {
|
|
153
|
+
const r = userDoc.render({ audience });
|
|
154
|
+
userBlocks.set(audience, r);
|
|
155
|
+
if (r.text) emit('user_loaded', { audience, snapshot: r.hash, fileHash: r.fileHash, chars: r.chars });
|
|
156
|
+
}
|
|
157
|
+
return userBlocks.get(audience).text;
|
|
158
|
+
};
|
|
159
|
+
const notify = (reason, to, extra = {}) => {
|
|
160
|
+
if (!peopleOn || !to) return;
|
|
161
|
+
const people = team.resolve(to).map((slug) => { const c = team.get(slug); return { slug, channels: (c && c.frontmatter && Array.isArray(c.frontmatter.channels)) ? c.frontmatter.channels : [] }; });
|
|
162
|
+
emit('notify', { reason, to, people, ...extra });
|
|
163
|
+
};
|
|
164
|
+
const peopleInWork = [...new Set(work.tasks.filter((t) => t.person).map((t) => t.person))];
|
|
165
|
+
const peopleBlocks = new Map();
|
|
166
|
+
const peopleFor = (task, agent) => {
|
|
167
|
+
if (userDoc.active || !legacyTeam.active) return '';
|
|
168
|
+
const fm = (agent && agent.frontmatter) || {};
|
|
169
|
+
const sv = legacyTeam.servedBy(fm, { forSlug: task.for });
|
|
170
|
+
const mentioned = [...new Set([...task.people.flatMap((r) => (legacyTeam.get(r) ? [r] : legacyTeam.resolve(r))), ...peopleInWork])].sort();
|
|
171
|
+
const key = `${agent ? agent.name : '_'}|${sv.slug || ''}|${mentioned.join(',')}`;
|
|
172
|
+
if (!peopleBlocks.has(key)) {
|
|
173
|
+
const r = legacyTeam.render({ agent: agent ? agent.name : null, identity: fm, served: sv.slug, mentioned });
|
|
174
|
+
peopleBlocks.set(key, r);
|
|
175
|
+
if (!sv.slug && sv.reason !== 'none') emit('people_denied', { taskId: task.id, reason: sv.reason, candidates: sv.candidates });
|
|
176
|
+
if (r.text) emit('people_loaded', { taskId: task.id, agent: agent ? agent.name : null, served: r.served, mentioned: r.mentioned, snapshot: r.hash, chars: r.chars });
|
|
177
|
+
}
|
|
178
|
+
return peopleBlocks.get(key).text;
|
|
179
|
+
};
|
|
180
|
+
const waiting = [];
|
|
181
|
+
const outbox = [];
|
|
182
|
+
const writeOutbox = (ob) => {
|
|
183
|
+
const f = path.join(runDir, 'outbox', `${ob.taskId}.json`);
|
|
184
|
+
fs.mkdirSync(path.dirname(f), { recursive: true });
|
|
185
|
+
fs.writeFileSync(f, JSON.stringify(ob, null, 2) + '\n');
|
|
186
|
+
outbox.push(ob);
|
|
187
|
+
};
|
|
188
|
+
const turnInput = opts.turn || (opts.sender || opts.participants || opts.isGroup ? {
|
|
189
|
+
sender: opts.sender || null,
|
|
190
|
+
participants: opts.participants || null,
|
|
191
|
+
isGroup: !!opts.isGroup,
|
|
192
|
+
channelMap: opts.channelMap || null,
|
|
193
|
+
chatSubject: opts.chatSubject || null,
|
|
194
|
+
approvedByHuman: !!opts.approvedByHuman,
|
|
195
|
+
autoSendExternal: !!opts.autoSendExternal,
|
|
196
|
+
aiLabel: opts.aiLabel || undefined,
|
|
197
|
+
} : null);
|
|
198
|
+
|
|
199
|
+
// v1.7 §31.5: a step that names a subject gets that subject's frozen load contract — nothing else.
|
|
200
|
+
// The store is a data plane outside the repo; a store inside the workdir is refused (I-01).
|
|
201
|
+
let memStore = null;
|
|
202
|
+
const memRoot = opts.memoryStore || process.env.SCROLL_MEMORY_STORE;
|
|
203
|
+
if (work.tasks.some((t) => t.subject)) {
|
|
204
|
+
if (!memRoot) throw new Error('a task names a subject but no memory store is configured (--memory-store or SCROLL_MEMORY_STORE)');
|
|
205
|
+
memStore = openStore({ root: memRoot, region: opts.memoryRegion || process.env.SCROLL_MEMORY_REGION || 'local', policy: opts.memoryPolicy || process.env.SCROLL_MEMORY_POLICY || undefined, workdir: cwd });
|
|
206
|
+
}
|
|
207
|
+
const memoryFor = (task, caps, turnPolicy = null) => {
|
|
208
|
+
if (!memStore || !task.subject) return '';
|
|
209
|
+
// v1.9 §42: external content audience may ONLY load the current chat's own subject memory
|
|
210
|
+
if (turnPolicy && turnPolicy.memoryScope === 'chat_only') {
|
|
211
|
+
if (!turnPolicy.chatSubject || task.subject !== turnPolicy.chatSubject) {
|
|
212
|
+
emit('memory_denied', { taskId: task.id, subject: task.subject, reason: 'external_memory_scope' });
|
|
213
|
+
return '';
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const sid = /^s_[a-z0-9]+_[0-9a-f]{16}$/.test(task.subject) ? task.subject : memStore.resolve(task.subject);
|
|
217
|
+
if (!sid || !memStore.exists(sid)) { emit('memory_denied', { taskId: task.id, reason: 'unknown_subject' }); return ''; }
|
|
218
|
+
const snap = memStore.snapshot(sid, { capabilities: caps, topics: (task.memory_topics || []).length ? task.memory_topics : null, verified: opts.verified !== false });
|
|
219
|
+
if (snap.denied) { emit('memory_denied', { taskId: task.id, reason: snap.denied }); return ''; }
|
|
220
|
+
emit('memory_loaded', { taskId: task.id, subject: memStore.hmac(sid), snapshot: snap.hash, memoryChars: snap.chars, truncated: snap.truncated });
|
|
221
|
+
return snap.text;
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
// v1.8 §34 — one skill registry per agent for the whole run: its index is frozen at open, so the
|
|
225
|
+
// stable prefix stays byte-identical across ticks. Opened only when a skill source exists or a
|
|
226
|
+
// task names a skill (a repo without skills runs exactly as before).
|
|
227
|
+
const skillRegs = new Map();
|
|
228
|
+
const skillsFor = (agent, wanted) => {
|
|
229
|
+
const key = agent ? agent.name : '_';
|
|
230
|
+
if (skillRegs.has(key)) return skillRegs.get(key);
|
|
231
|
+
let reg = null;
|
|
232
|
+
try {
|
|
233
|
+
const fm = (agent && agent.frontmatter) || {};
|
|
234
|
+
const sources = resolveSources({ cwd, agent: agent ? agent.name : null, identity: fm });
|
|
235
|
+
if (wanted || sources.some((s) => fs.existsSync(s.path))) {
|
|
236
|
+
reg = openSkills({ cwd, agent: agent ? agent.name : null, identity: fm, sources, pinned: fm.skills,
|
|
237
|
+
available: { capabilities: Array.isArray(fm.capabilities) ? fm.capabilities : [] },
|
|
238
|
+
toolFor: bindingsToolFor(agent && agent.tools, opts.skillRuntime || null),
|
|
239
|
+
emit: (type, data) => emit(type, { agent: key, ...data }) });
|
|
240
|
+
}
|
|
241
|
+
} catch (e) { emit('error', { reason: `skills: ${e.message}` }); }
|
|
242
|
+
skillRegs.set(key, reg);
|
|
243
|
+
return reg;
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const startedAt = Date.now();
|
|
247
|
+
emit('run_started', { mode: opts.workFile ? 'multi-agent' : 'single', provider: provider.name, model: provider.model });
|
|
248
|
+
emit('work_loaded', { tasks: work.tasks.length, controller: work.controller });
|
|
249
|
+
|
|
250
|
+
// single controller (failure-mode #1 guard): exactly one owner of WORK.md, ≤1 owner per task
|
|
251
|
+
emit('work_contract', { controller: work.controller, controllerCount: 1, ownerCountPerTaskMax: 1 });
|
|
252
|
+
|
|
253
|
+
// per-language token budgeting: a controller agent's language scales the estimate
|
|
254
|
+
// (Vietnamese ≈ 1.8× English) so the cost gate is honest for non-English markets.
|
|
255
|
+
let runLanguage = opts.language || null;
|
|
256
|
+
if (!runLanguage) { try { runLanguage = loadAgent(work.controller, cwd)?.frontmatter?.language || null; } catch { /* no controller folder */ } }
|
|
257
|
+
const mcpTools = loadMcpRisk(cwd); // per-tool risk from a repo-level .mcp.json
|
|
258
|
+
|
|
259
|
+
// cost gate BEFORE any spawn
|
|
260
|
+
const distinctAgents = new Set(work.tasks.filter((t) => !t.person).map((t) => t.agent || t.owner)).size; // a person's step spawns no agent
|
|
261
|
+
const objectivesText = work.tasks.map((t) => t.objective).join(' ');
|
|
262
|
+
const est = estimateCost(objectivesText, Math.max(2, distinctAgents), { language: runLanguage });
|
|
263
|
+
const parallelReady = work.tasks.filter((t) => t.parallel).length;
|
|
264
|
+
let decision = (parallelReady >= 2 && distinctAgents > 1) ? 'multi' : 'single';
|
|
265
|
+
const spawnedAgents = decision === 'multi' ? distinctAgents : 1;
|
|
266
|
+
emit('cost_gate', { singleTokens: est.single, multiTokens: est.multi, decision, spawnedAgents, language: runLanguage || 'en', reason: decision === 'multi' ? 'parallel read-heavy work' : 'single is enough' });
|
|
267
|
+
if (decision === 'single') for (const t of work.tasks) t.parallel = false; // enforce it
|
|
268
|
+
|
|
269
|
+
// lean path: a single small task gets a slim prefix — a 1-call task can't amortize a full
|
|
270
|
+
// agent-definition dump, so sending it uncached is pure overhead (kept the contract, which drives quality).
|
|
271
|
+
const leanSingle = decision === 'single' && work.tasks.length === 1 && (work.tasks[0].blockedBy || []).length === 0 && !opts.noLean;
|
|
272
|
+
// nudges (on-thesis output behavior): reject-multi when a fan-out was genuinely on the table; checkpoint-frame for long work.
|
|
273
|
+
const rejectMultiNudge = decision === 'single' && (distinctAgents > 1 || /multi-?agent|spawn|fan-?out|nhiều agent|nhieu agent/i.test(objectivesText));
|
|
274
|
+
const longRun = work.tasks.length > 1 || /(\d+\s*(steps|bước|buoc|ticks?))|resume|long[- ]?run|nhiều bước|nhieu buoc/i.test(objectivesText);
|
|
275
|
+
|
|
276
|
+
// hard caps
|
|
277
|
+
const caps = { ...DEFAULT_CAPS, ...(opts.caps || {}) };
|
|
278
|
+
emit('cap_set', { maxIterations: caps.maxIterations, maxInputTokens: caps.maxInputTokens, maxWallClockMs: caps.maxWallClockMs, maxUsd: caps.maxUsd });
|
|
279
|
+
|
|
280
|
+
// efficiency levers: trim intermediate output · cap blackboard injected downstream · route sub-tasks to a cheaper model.
|
|
281
|
+
const finalMaxTokens = opts.maxTokens || 800;
|
|
282
|
+
const intermediateMaxTokens = opts.intermediateMaxTokens || Math.min(400, finalMaxTokens);
|
|
283
|
+
const blackboardCap = opts.blackboardCap || 900; // chars of a dependency's blackboard injected downstream
|
|
284
|
+
const route = opts.route !== false;
|
|
285
|
+
const dependents = new Set(work.tasks.flatMap((t) => t.blockedBy || []));
|
|
286
|
+
const isSynthesis = (task) => task.final || !dependents.has(task.id); // a leaf / final task gets full budget + primary model
|
|
287
|
+
|
|
288
|
+
const ledger = { uncached: 0, cached: 0, output: 0, usd: 0, calls: 0, cachedCalls: 0 };
|
|
289
|
+
const done = new Set();
|
|
290
|
+
const outputs = {};
|
|
291
|
+
|
|
292
|
+
// per-owner security (risk_defaults + approval overrides from IDENTITY.security), memoized
|
|
293
|
+
const securityCache = {};
|
|
294
|
+
const agentSecurity = (task) => {
|
|
295
|
+
const key = task.agent || task.owner || '_';
|
|
296
|
+
if (securityCache[key]) return securityCache[key];
|
|
297
|
+
let sec = { riskDefaults: {}, approvalOverrides: {} };
|
|
298
|
+
try { sec = loadAgentSecurity(loadAgent(task.agent || task.owner, cwd).dir); } catch { /* no agent folder */ }
|
|
299
|
+
securityCache[key] = sec; return sec;
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const billable = () => Math.round(ledger.uncached + ledger.cached * (PRICING.cachedInput / PRICING.input) + ledger.output * (PRICING.output / PRICING.input));
|
|
303
|
+
const usd = () => (ledger.uncached / 1e6) * PRICING.input + (ledger.cached / 1e6) * PRICING.cachedInput + (ledger.output / 1e6) * PRICING.output;
|
|
304
|
+
const capBreach = (iter) => {
|
|
305
|
+
if (iter > caps.maxIterations) return `maxIterations ${caps.maxIterations}`;
|
|
306
|
+
if (ledger.uncached + ledger.cached > caps.maxInputTokens) return `maxInputTokens ${caps.maxInputTokens}`;
|
|
307
|
+
if (Date.now() - startedAt > caps.maxWallClockMs) return `maxWallClockMs ${caps.maxWallClockMs}`;
|
|
308
|
+
if (usd() > caps.maxUsd) return `maxUsd ${caps.maxUsd}`;
|
|
309
|
+
return null;
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
async function runTask(task) {
|
|
313
|
+
task.status = 'doing';
|
|
314
|
+
// v1.9 §40 — a person's step: no model call. The runner asks (a request file + a notify event to
|
|
315
|
+
// the channels on the card) and stops there; it continues when that person marks it done.
|
|
316
|
+
if (task.person) return personStep(task);
|
|
317
|
+
const depSummaries = task.blockedBy.map((id) => {
|
|
318
|
+
const p = path.join(blackboardDir, `${id}.md`);
|
|
319
|
+
if (!fs.existsSync(p)) return '';
|
|
320
|
+
let body = fs.readFileSync(p, 'utf8');
|
|
321
|
+
if (body.length > blackboardCap) body = body.slice(0, blackboardCap) + '\n…[trimmed for downstream]';
|
|
322
|
+
return `### from ${id}\n${body}`;
|
|
323
|
+
}).filter(Boolean);
|
|
324
|
+
emit('task_contract', {
|
|
325
|
+
taskId: task.id, objective: task.objective, outputFormat: task.output_format,
|
|
326
|
+
sourceGuidance: task.source_guidance, boundaries: task.boundaries,
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
// v1.9 §42 — per-turn internal vs external audience classifier (verified channelId, group lowest-trust wall)
|
|
330
|
+
const turnPolicy = turnInput ? classifyTurn(userDoc, turnInput) : null;
|
|
331
|
+
if (turnPolicy) {
|
|
332
|
+
emit('turn_classified', { taskId: task.id, ...turnPolicy });
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// ── permission gate (v1.4): risk-tiered, DETERMINISTIC, enforced HERE before the action ──
|
|
336
|
+
// A tier comes from the task's declared tool (resolved via .mcp.json / IDENTITY.security) or a
|
|
337
|
+
// direct task.risk tier. Legacy `irreversible` maps to `destructive`. The gate is in code, not
|
|
338
|
+
// the prompt — a model cannot be talked out of it.
|
|
339
|
+
const sec = agentSecurity(task);
|
|
340
|
+
const tier = task.tool
|
|
341
|
+
? resolveTier(task.tool, { mcpTools, riskDefaults: sec.riskDefaults }).tier
|
|
342
|
+
: normalizeTier(task.risk);
|
|
343
|
+
if (tier && turnPolicy && !turnPolicy.writeToolsAllowed && ['reversible_write', 'financial', 'destructive'].includes(tier)) {
|
|
344
|
+
const gateId = `${task.id}-gate`;
|
|
345
|
+
emit('permission_decision', { taskId: task.id, tool: task.tool || null, tier, decision: 'deny', reason: 'external_write_forbidden' });
|
|
346
|
+
task.status = 'blocked';
|
|
347
|
+
emit('gate_blocked', { gateId, reason: 'external_write_forbidden' });
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
if (tier) {
|
|
351
|
+
const policy = policyForTier(tier, sec.approvalOverrides);
|
|
352
|
+
const gateId = `${task.id}-gate`;
|
|
353
|
+
let approved = !!opts.autoApprove;
|
|
354
|
+
const approvalFile = [path.join(approvalsDir, `${gateId}.json`), path.join(controlDir, `${gateId}.approved`)].find((f) => fs.existsSync(f)); // second: legacy path
|
|
355
|
+
const gate = { tier, action: task.action || null, amount: task.amount ?? (task.ground && task.ground.amount) ?? null };
|
|
356
|
+
if (opts.autoApprove && policy === 'must-approve') audit({ gateId, taskId: task.id, agent: task.agent || task.owner, tier, action: gate.action, decision: 'auto-approved', approver: 'operator:--auto-approve' });
|
|
357
|
+
else if (approvalFile && policy === 'must-approve') {
|
|
358
|
+
const raw = fs.readFileSync(approvalFile, 'utf8');
|
|
359
|
+
let by = null; try { by = JSON.parse(raw || '{}').by || null; } catch { /* legacy: an empty marker */ }
|
|
360
|
+
if (!peopleOn) {
|
|
361
|
+
approved = true; // no team configured: the file's presence approves, as before v1.9
|
|
362
|
+
audit({ gateId, taskId: task.id, agent: task.agent || task.owner, tier, action: gate.action, decision: 'accepted', approver: by || 'unattributed', file: crypto.createHash('sha256').update(raw).digest('hex').slice(0, 16) });
|
|
363
|
+
} else {
|
|
364
|
+
// §40 — the approval names its person, and that person's card must cover this action and amount
|
|
365
|
+
const v = team.canApprove(by, gate);
|
|
366
|
+
audit({ gateId, taskId: task.id, agent: task.agent || task.owner, tier, action: gate.action, amount: gate.amount, decision: v.ok ? 'accepted' : 'rejected', approver: by || 'unattributed', reason: v.ok ? undefined : v.reason, file: crypto.createHash('sha256').update(raw).digest('hex').slice(0, 16) });
|
|
367
|
+
if (v.ok) approved = true;
|
|
368
|
+
else emit('approval_rejected', { gateId, taskId: task.id, by: by || null, tier, action: gate.action, reason: v.reason });
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// grounding pre-check: for financial|destructive, every declared param must trace to a real
|
|
373
|
+
// prior source (blackboard / prior output / steer / objective) — a fabricated id never executes.
|
|
374
|
+
let grounded = null;
|
|
375
|
+
const groundingRequired = needsGrounding(tier) && task.ground != null;
|
|
376
|
+
if (groundingRequired) {
|
|
377
|
+
const steerP = path.join(controlDir, 'steer.md');
|
|
378
|
+
const sources = collectSources({
|
|
379
|
+
depSummaries, objective: task.objective,
|
|
380
|
+
steer: fs.existsSync(steerP) ? fs.readFileSync(steerP, 'utf8') : '',
|
|
381
|
+
priorOutputs: Object.values(outputs),
|
|
382
|
+
});
|
|
383
|
+
const g = checkGrounding(task.ground, sources);
|
|
384
|
+
grounded = g.ok;
|
|
385
|
+
emit('grounding_checked', { taskId: task.id, tier, traced: g.traced, missing: g.missing, passed: g.ok });
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const verdict = evaluateAction({ tier, policy, approved, groundingRequired, grounded });
|
|
389
|
+
emit('gate_requested', { gateId, risk: tier, tier, policy, approvedBeforeAction: verdict.decision === 'allow' });
|
|
390
|
+
emit('permission_decision', { taskId: task.id, tool: task.tool || null, tier, policy, decision: verdict.decision, reason: verdict.reason });
|
|
391
|
+
|
|
392
|
+
if (verdict.decision === 'deny') {
|
|
393
|
+
emit('grounding_failed', { taskId: task.id, tier });
|
|
394
|
+
task.status = 'blocked'; emit('gate_blocked', { gateId, reason: 'grounding' }); return;
|
|
395
|
+
}
|
|
396
|
+
if (verdict.decision === 'await') {
|
|
397
|
+
fs.mkdirSync(approvalsDir, { recursive: true });
|
|
398
|
+
const approvers = peopleOn ? team.approversFor(gate) : undefined;
|
|
399
|
+
fs.writeFileSync(path.join(approvalsDir, `${gateId}.requested.json`),
|
|
400
|
+
JSON.stringify({ gateId, tool: task.tool || null, tier, policy, action: gate.action, amount: gate.amount, approvers, ts: new Date().toISOString() }, null, 2));
|
|
401
|
+
task.status = 'blocked'; emit('gate_blocked', { gateId });
|
|
402
|
+
if (peopleOn) { waiting.push({ taskId: task.id, kind: 'approval', gateId, approvers }); for (const s of approvers) notify('gate', `person:${s}`, { gateId, taskId: task.id, tier }); }
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
emit('gate_approved', { gateId, approvedBeforeAction: true });
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// deterministic step: a mechanical task (merge/passthrough) runs in code — 0 provider call, 0 tokens.
|
|
409
|
+
if (task.deterministic) {
|
|
410
|
+
emit('context_loaded', {
|
|
411
|
+
taskId: task.id,
|
|
412
|
+
sourcesRead: task.blockedBy.length + task.source_guidance.length,
|
|
413
|
+
sources: [...task.blockedBy, ...task.source_guidance],
|
|
414
|
+
lean: leanSingle,
|
|
415
|
+
compact: false,
|
|
416
|
+
files: [],
|
|
417
|
+
chars: 0,
|
|
418
|
+
fileChars: {},
|
|
419
|
+
});
|
|
420
|
+
const mergedBody = task.op === 'passthrough'
|
|
421
|
+
? (depSummaries[0] || task.objective)
|
|
422
|
+
: [`## ${task.title}`, ...depSummaries].filter(Boolean).join('\n\n');
|
|
423
|
+
const merged = task.final
|
|
424
|
+
? [
|
|
425
|
+
'## Verdict',
|
|
426
|
+
'Use the checkpointed, resumable plan below; this deterministic merge combines the verified step outputs without another model call.',
|
|
427
|
+
'',
|
|
428
|
+
'## Evidence',
|
|
429
|
+
mergedBody,
|
|
430
|
+
'',
|
|
431
|
+
'## Assumptions',
|
|
432
|
+
'- The merged step outputs are the evidence source for this final report.',
|
|
433
|
+
'- Source ids are preserved from the upstream task outputs where available.',
|
|
434
|
+
'',
|
|
435
|
+
'## Next actions',
|
|
436
|
+
'- Continue from the latest checkpoint if interrupted.',
|
|
437
|
+
'- Verify each completed step before proceeding to the next one.',
|
|
438
|
+
].join('\n')
|
|
439
|
+
: mergedBody;
|
|
440
|
+
const bbPathD = path.join(blackboardDir, `${task.id}.md`);
|
|
441
|
+
fs.appendFileSync(bbPathD, `${merged}\n`);
|
|
442
|
+
emit('blackboard_write', { taskId: task.id, path: path.relative(runDir, bbPathD), appendOnly: true, bytes: Buffer.byteLength(merged) });
|
|
443
|
+
emit('deterministic_step', { taskId: task.id, op: task.op, providerCalls: 0, tokens: 0 });
|
|
444
|
+
outputs[task.id] = merged; task.status = 'done'; done.add(task.id);
|
|
445
|
+
emit('task_completed', { id: task.id, deterministic: true });
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// Stable-prefix-first context (cacheable) → volatile last.
|
|
450
|
+
let agent = null;
|
|
451
|
+
try { agent = loadAgent(task.agent || task.owner, cwd); } catch { /* agent folder optional */ }
|
|
452
|
+
// v1.8 §34.7 — a step that names skills gets their full instructions; one it cannot have does not run
|
|
453
|
+
const skillReg = skillsFor(agent, task.skills.length > 0);
|
|
454
|
+
const skillBodies = [];
|
|
455
|
+
for (const sname of task.skills) {
|
|
456
|
+
const v = skillReg ? skillReg.view(sname) : { ok: false, error: 'no skill sources' };
|
|
457
|
+
if (!v.ok) { emit('gate_blocked', { gateId: `${task.id}-skill`, reason: 'skill', skill: sname, detail: v.error }); task.status = 'blocked'; return; }
|
|
458
|
+
emit('skill_loaded', { taskId: task.id, name: sname, hash: v.hash, source: v.source });
|
|
459
|
+
skillBodies.push(`<skill name="${sname}">\n${v.text}\n</skill>`);
|
|
460
|
+
}
|
|
461
|
+
// the index is for an agent that can read skills on demand (§34.4: capability skills.read);
|
|
462
|
+
// without it, a step still gets the skills it names, in full
|
|
463
|
+
const canRead = Array.isArray(agent?.frontmatter?.capabilities) && agent.frontmatter.capabilities.includes('skills.read');
|
|
464
|
+
const skillIndex = skillReg && canRead ? skillReg.index().text : '';
|
|
465
|
+
const synthesis = isSynthesis(task);
|
|
466
|
+
const agentCaps = (agent && agent.frontmatter && Array.isArray(agent.frontmatter.capabilities)) ? agent.frontmatter.capabilities : [];
|
|
467
|
+
const baseToolPolicy = agentCaps.some((c) => /^(shell|fs\.write|mcp|web)/.test(String(c))) ? 'full' : 'none'; // reasoning-only agent → let the provider strip tool schemas
|
|
468
|
+
const toolPolicy = (turnPolicy && turnPolicy.toolPolicyOverride) ? turnPolicy.toolPolicyOverride : baseToolPolicy;
|
|
469
|
+
const compact = opts.compactPrefix !== undefined ? !!opts.compactPrefix : leanSingle; // compact prefix trims identity/tools but NEVER drops hard-rules.md (§6)
|
|
470
|
+
const userBlock = userFor(turnPolicy ? turnPolicy.contentAudience : 'internal');
|
|
471
|
+
const peopleBlock = peopleFor(task, agent); // v1.9 §39: last in the stable prefix, frozen per agent+person
|
|
472
|
+
const loadedFiles = [];
|
|
473
|
+
const fileChars = {};
|
|
474
|
+
const addCtx = (label, text) => {
|
|
475
|
+
if (!text) return '';
|
|
476
|
+
loadedFiles.push(label);
|
|
477
|
+
fileChars[label] = text.length;
|
|
478
|
+
return text;
|
|
479
|
+
};
|
|
480
|
+
if (peopleBlock) { loadedFiles.push('people'); fileChars.people = peopleBlock.length; }
|
|
481
|
+
const stablePrefix = compact
|
|
482
|
+
? [
|
|
483
|
+
leanSingle ? 'SCROLL agent — single small task; answer the objective directly.' : 'SCROLL agent — reason directly from the contract below.',
|
|
484
|
+
addCtx('SOUL.md', agent && agent.soul ? agent.soul : ''),
|
|
485
|
+
addCtx('hard-rules.md', agent && agent.hardRules ? agent.hardRules : ''),
|
|
486
|
+
addCtx('USER.md', userBlock),
|
|
487
|
+
peopleBlock, // compact-people
|
|
488
|
+
].filter(Boolean).join('\n\n')
|
|
489
|
+
: [
|
|
490
|
+
'SCROLL agent. Coordinate through files; one controller owns WORK.md.',
|
|
491
|
+
addCtx('IDENTITY.md', agent && agent.identityBody ? agent.identityBody : ''),
|
|
492
|
+
addCtx('SOUL.md', agent && agent.soul ? agent.soul : ''),
|
|
493
|
+
addCtx('TOOLS.md', agent && agent.tools ? agent.tools : ''),
|
|
494
|
+
addCtx('hard-rules.md', agent && agent.hardRules ? agent.hardRules : ''), // full-hr
|
|
495
|
+
addCtx('skills:index', skillIndex), // v1.8 §34.3: pinned skill index, frozen for the run
|
|
496
|
+
addCtx('USER.md', userBlock),
|
|
497
|
+
peopleBlock,
|
|
498
|
+
].filter(Boolean).join('\n\n');
|
|
499
|
+
emit('context_loaded', {
|
|
500
|
+
taskId: task.id,
|
|
501
|
+
sourcesRead: task.blockedBy.length + task.source_guidance.length,
|
|
502
|
+
sources: [...task.blockedBy, ...task.source_guidance],
|
|
503
|
+
lean: leanSingle,
|
|
504
|
+
compact,
|
|
505
|
+
files: loadedFiles,
|
|
506
|
+
chars: stablePrefix.length,
|
|
507
|
+
fileChars,
|
|
508
|
+
});
|
|
509
|
+
// v1.10 §44 — auto-retrieve relevant chunks from deployment `knowledge/` (or use caller-supplied chunks)
|
|
510
|
+
let turnKnowledgeChunks = [];
|
|
511
|
+
if (Array.isArray(opts.knowledgeChunks)) {
|
|
512
|
+
turnKnowledgeChunks = opts.knowledgeChunks.map(String).filter(Boolean);
|
|
513
|
+
} else if (knowledgeStore && knowledgeStore.active) {
|
|
514
|
+
const kRes = knowledgeStore.retrieve(task.objective, {
|
|
515
|
+
topK: opts.knowledgeTopK,
|
|
516
|
+
maxChars: opts.knowledgeMaxChars,
|
|
517
|
+
prevQuery: opts.prevQuery || '',
|
|
518
|
+
synonyms: opts.knowledgeSynonyms || null,
|
|
519
|
+
});
|
|
520
|
+
turnKnowledgeChunks = kRes.chunks;
|
|
521
|
+
emit('knowledge_retrieved', {
|
|
522
|
+
taskId: task.id,
|
|
523
|
+
filesCount: knowledgeStore.filesCount,
|
|
524
|
+
chunksTotal: knowledgeStore.chunksCount,
|
|
525
|
+
selectedCount: kRes.hits.length,
|
|
526
|
+
selectedChars: kRes.selectedChars,
|
|
527
|
+
totalCorpusChars: kRes.totalCorpusChars,
|
|
528
|
+
durationMs: kRes.durationMs,
|
|
529
|
+
topFiles: kRes.hits.map((h) => h.file),
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
const knowledgeBlock = formatKnowledgeBlock(turnKnowledgeChunks, { hash: knowledgeStore?.hash || null });
|
|
533
|
+
|
|
534
|
+
const styleCfgForPrompt = (agent && agent.frontmatter && agent.frontmatter.style && typeof agent.frontmatter.style === 'object') ? agent.frontmatter.style : null;
|
|
535
|
+
const speaksAsForPrompt = (agent && agent.frontmatter && agent.frontmatter.speaks_as) ? String(agent.frontmatter.speaks_as) : 'self';
|
|
536
|
+
const isConversationalTurn = Boolean(
|
|
537
|
+
opts.conversational === true
|
|
538
|
+
|| (turnPolicy && turnPolicy.contentAudience === 'external')
|
|
539
|
+
|| styleCfgForPrompt
|
|
540
|
+
|| speaksAsForPrompt !== 'self'
|
|
541
|
+
|| (opts.voiceHooks && typeof opts.voiceHooks.beforeTurn === 'function'),
|
|
542
|
+
);
|
|
543
|
+
if (isConversationalTurn) hadConversationalTurn = true;
|
|
544
|
+
|
|
545
|
+
// sub-tasks return terse findings (they only feed the blackboard); conversational turns reply naturally
|
|
546
|
+
// per SOUL.md & style: without forcing research-report Verdict/Evidence/Assumptions scaffolding (§44).
|
|
547
|
+
const deliverable = synthesis
|
|
548
|
+
? (isConversationalTurn
|
|
549
|
+
? 'Reply naturally and concisely directly to the message according to SOUL.md and retrieved knowledge. Do NOT output report headings like Verdict, Evidence, Assumptions, Next actions, or [S1] tags.'
|
|
550
|
+
: `Return ${task.output_format} with sections: Verdict, Evidence, Assumptions, Next actions. Cite source ids like [S1]. Do not say you are blocked unless a source is truly missing.`)
|
|
551
|
+
: 'Return ONLY the key findings as terse bullet points — no preamble, no headings, ≤120 words. Cite source ids like [S1].';
|
|
552
|
+
// v1.9 §43 — Official Voice Hook #1 (beforeTurn): volatile guidance appended AFTER history/deliverable
|
|
553
|
+
// so stablePrefix stays cache-identical. Rejects bare numeric sliders and caps at 650 chars.
|
|
554
|
+
let voiceGuidance = '';
|
|
555
|
+
const voiceHooks = opts.voiceHooks || null;
|
|
556
|
+
if (voiceHooks && typeof voiceHooks.beforeTurn === 'function') {
|
|
557
|
+
const hRes = await voiceHooks.beforeTurn({
|
|
558
|
+
agent: agent ? { name: agent.name, frontmatter: agent.frontmatter } : null,
|
|
559
|
+
task, turnPolicy,
|
|
560
|
+
incoming: task.objective,
|
|
561
|
+
history: Array.isArray(opts.history) ? opts.history : [],
|
|
562
|
+
});
|
|
563
|
+
const rawGuidance = typeof hRes === 'string' ? hRes : (hRes && typeof hRes.guidance === 'string' ? hRes.guidance : '');
|
|
564
|
+
if (rawGuidance.trim()) {
|
|
565
|
+
if (hasBareNumericSlider(rawGuidance)) {
|
|
566
|
+
emit('voice_hook_rejected', { taskId: task.id, hook: 'beforeTurn', reason: 'bare_number_slider_forbidden' });
|
|
567
|
+
} else {
|
|
568
|
+
voiceGuidance = rawGuidance.trim().slice(0, 650);
|
|
569
|
+
emit('voice_hook_before_turn', { taskId: task.id, chars: voiceGuidance.length });
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
// Machine-verified internal caller tag so the model can reliably honor internal owner requests (§44)
|
|
575
|
+
const verifiedCallerBlock = (turnPolicy && turnPolicy.contentAudience === 'internal' && turnPolicy.speakerId)
|
|
576
|
+
? `<verified-caller audience="internal" id="${turnPolicy.speakerId}" role="${(turnPolicy.speakerRoles || []).join(',') || 'internal'}">Machine-verified internal principal member; internal USER.md details may be shared with this caller.</verified-caller>`
|
|
577
|
+
: '';
|
|
578
|
+
|
|
579
|
+
const volatile = [
|
|
580
|
+
verifiedCallerBlock,
|
|
581
|
+
`Task: ${task.title}`,
|
|
582
|
+
`Objective: ${task.objective}`,
|
|
583
|
+
task.source_guidance.length ? `Sources:\n${task.source_guidance.join('\n')}` : '',
|
|
584
|
+
knowledgeBlock,
|
|
585
|
+
memoryFor(task, agentCaps, turnPolicy),
|
|
586
|
+
skillBodies.length ? `Skills for this step (follow them):\n\n${skillBodies.join('\n\n')}` : '',
|
|
587
|
+
depSummaries.length ? `Blackboard (prior findings):\n${depSummaries.join('\n\n')}` : '',
|
|
588
|
+
task.boundaries.length ? `Boundaries: ${task.boundaries.join('; ')}` : '',
|
|
589
|
+
deliverable,
|
|
590
|
+
rejectMultiNudge ? 'The cost gate selected SINGLE-agent: explicitly recommend single-agent and state you reject spawning unnecessary multi-agent for a task this small.' : '',
|
|
591
|
+
longRun ? 'Frame the work as short, checkpointed, resumable steps (verify/revise/report) — never one uninterrupted run.' : '',
|
|
592
|
+
voiceGuidance ? `<turn-guidance>\n${voiceGuidance}\n</turn-guidance>` : '',
|
|
593
|
+
].filter(Boolean).join('\n\n');
|
|
594
|
+
const prompt = `${stablePrefix}\n\n---\n\n${volatile}`;
|
|
595
|
+
|
|
596
|
+
const agentSub = agent && agent.frontmatter && agent.frontmatter.model && agent.frontmatter.model.fallback;
|
|
597
|
+
const callModel = task.engine ? null : ((!synthesis && route && (opts.subModel || agentSub)) ? (opts.subModel || agentSub) : opts.model); // engine task → that provider's own default model
|
|
598
|
+
const callMaxTokens = synthesis ? finalMaxTokens : intermediateMaxTokens;
|
|
599
|
+
// isolated working dir per task → two parallel agents never write the same scratch (worktree-lite).
|
|
600
|
+
// With --worktree on a git repo, a real `git worktree` is used so parallel writes can't corrupt state.
|
|
601
|
+
const taskScratch = path.join(scratchDir, task.id);
|
|
602
|
+
fs.mkdirSync(taskScratch, { recursive: true });
|
|
603
|
+
let workdir = taskScratch;
|
|
604
|
+
if (task.workdir) workdir = path.resolve(cwd, task.workdir); // v1.6: repo-anchored task (code work needs the repo, not scratch)
|
|
605
|
+
if (opts.worktree && task.parallel) { const wt = makeWorktree(cwd, runDir, task.id, emit); if (wt) workdir = wt; }
|
|
606
|
+
if (task.parallel) emit('isolated_scratch', { taskId: task.id, dir: path.relative(runDir, workdir) });
|
|
607
|
+
|
|
608
|
+
const prov = providerFor(task.engine);
|
|
609
|
+
emit('provider_call_started', { taskId: task.id, provider: prov.name, label: task.owner, role: synthesis ? 'synthesis' : 'sub', model: callModel || prov.model, maxTokens: callMaxTokens, toolPolicy, engine: task.engine || null, effort: task.effort || null, sandbox: task.sandbox || null });
|
|
610
|
+
let r = await prov.generate(prompt, { cwd: workdir, model: callModel, cachePrefix: stablePrefix, system: stablePrefix, maxTokens: callMaxTokens, toolPolicy, effort: task.effort, sandbox: task.sandbox, timeoutMs: task.timeout_ms || opts.stepTimeoutMs });
|
|
611
|
+
ledger.uncached += r.usage.uncachedInputTokens; ledger.cached += r.usage.cachedInputTokens; ledger.output += r.usage.outputTokens; ledger.calls += 1;
|
|
612
|
+
if (r.usage.cachedInputTokens > 0) ledger.cachedCalls += 1;
|
|
613
|
+
emit('provider_call_completed', { taskId: task.id, durationMs: r.durationMs, usage: r.usage, responseId: r.responseId });
|
|
614
|
+
if (r.usage.cachedInputTokens > 0 && ledger.calls >= 2) {
|
|
615
|
+
emit('cache_path_checked', { cachedInputTokens: r.usage.cachedInputTokens, uncachedInputTokens: r.usage.uncachedInputTokens, providerCalls: ledger.calls, passed: true });
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// v1.9 §42–§43 — reply grounding (on RAW text), machine style enforcement (1 retry on max_chars),
|
|
619
|
+
// Official Voice Hook #2 (shapeOutgoing), claim-preservation invariant, and approval-bound outbox.
|
|
620
|
+
const isExternalReply = !!(turnPolicy && turnPolicy.contentAudience === 'external');
|
|
621
|
+
const styleCfg = (agent && agent.frontmatter && agent.frontmatter.style && typeof agent.frontmatter.style === 'object') ? agent.frontmatter.style : null;
|
|
622
|
+
const runReplyPipeline = isExternalReply || opts.replyCheck === true || (voiceHooks && typeof voiceHooks.shapeOutgoing === 'function') || !!styleCfg;
|
|
623
|
+
let finalText = r.text;
|
|
624
|
+
if (runReplyPipeline) {
|
|
625
|
+
const speaksAs = (agent && agent.frontmatter && agent.frontmatter.speaks_as) ? String(agent.frontmatter.speaks_as) : 'self';
|
|
626
|
+
const approvedByHuman = !!(turnInput && turnInput.approvedByHuman);
|
|
627
|
+
const reasons = [];
|
|
628
|
+
let handoffTo = null;
|
|
629
|
+
let rawText = r.text;
|
|
630
|
+
|
|
631
|
+
// 1) Style enforcement with at most 1 regenerate when max_chars is exceeded
|
|
632
|
+
let styled = styleCfg ? enforceStyle(rawText, styleCfg) : { text: rawText, bubbles: [rawText], chars: rawText.length, overMaxChars: false, needsRegenerate: false, invariantOk: true };
|
|
633
|
+
if (styleCfg && styled.needsRegenerate) {
|
|
634
|
+
const retryPrompt = `${prompt}\n\nLength ceiling: rewrite the reply in <= ${styleCfg.max_chars} characters while keeping every exact number, code, date, and link unchanged.`;
|
|
635
|
+
const r2 = await prov.generate(retryPrompt, { cwd: workdir, model: callModel, cachePrefix: stablePrefix, system: stablePrefix, maxTokens: callMaxTokens, toolPolicy, effort: task.effort, sandbox: task.sandbox, timeoutMs: task.timeout_ms || opts.stepTimeoutMs });
|
|
636
|
+
ledger.uncached += r2.usage.uncachedInputTokens; ledger.cached += r2.usage.cachedInputTokens; ledger.output += r2.usage.outputTokens; ledger.calls += 1;
|
|
637
|
+
rawText = r2.text;
|
|
638
|
+
styled = enforceStyle(rawText, styleCfg);
|
|
639
|
+
if (styled.overMaxChars) reasons.push('style_max_chars_exceeded');
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// 2) Grounding check on ORIGINAL rawText BEFORE voice shaping (principal sources ONLY)
|
|
643
|
+
let groundingRes = null;
|
|
644
|
+
if (isExternalReply || opts.replyCheck === true) {
|
|
645
|
+
const principalSources = collectPrincipalSources({
|
|
646
|
+
knowledgeChunks: turnKnowledgeChunks,
|
|
647
|
+
maySay: userDoc.active ? userDoc.maySayText() : '',
|
|
648
|
+
toolOutputs: opts.toolOutputs || [],
|
|
649
|
+
});
|
|
650
|
+
const checkG = isExternalReply ? (opts.groundExternal !== false) : true;
|
|
651
|
+
if (checkG && (turnKnowledgeChunks.length > 0 || opts.knowledgeChunks !== undefined || opts.toolOutputs !== undefined || opts.replyCheck === true)) {
|
|
652
|
+
groundingRes = groundReply(rawText, principalSources);
|
|
653
|
+
if (!groundingRes.ok) {
|
|
654
|
+
if (groundingRes.missing.length) reasons.push('ungrounded_claim');
|
|
655
|
+
if (groundingRes.pairMismatches.length) reasons.push('code_price_mismatch');
|
|
656
|
+
if (groundingRes.held.length) reasons.push(...groundingRes.held.map((h) => `hold_${h.kind}`));
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// 3) Official Voice Hook #2 (shapeOutgoing): post-grounding formatting & delivery shaping
|
|
662
|
+
let shapedText = styled.text;
|
|
663
|
+
let bubbles = styled.bubbles;
|
|
664
|
+
let shapeMeta = {};
|
|
665
|
+
if (voiceHooks && typeof voiceHooks.shapeOutgoing === 'function') {
|
|
666
|
+
const sOut = await voiceHooks.shapeOutgoing({
|
|
667
|
+
text: styled.text,
|
|
668
|
+
rawText,
|
|
669
|
+
bubbles: styled.bubbles,
|
|
670
|
+
agent: agent ? { name: agent.name, frontmatter: agent.frontmatter } : null,
|
|
671
|
+
task, turnPolicy, style: styleCfg,
|
|
672
|
+
});
|
|
673
|
+
if (sOut && typeof sOut === 'object') {
|
|
674
|
+
if (Array.isArray(sOut.bubbles) && sOut.bubbles.length) {
|
|
675
|
+
bubbles = sOut.bubbles.map(String);
|
|
676
|
+
shapedText = bubbles.join('\n');
|
|
677
|
+
} else if (typeof sOut.text === 'string') {
|
|
678
|
+
shapedText = sOut.text;
|
|
679
|
+
bubbles = [shapedText];
|
|
680
|
+
}
|
|
681
|
+
if (sOut.reaction !== undefined) shapeMeta.reaction = sOut.reaction;
|
|
682
|
+
if (sOut.edits !== undefined) shapeMeta.edits = sOut.edits;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
// 4) Claim-preservation invariant: rawText vs shapedText MUST share identical numbers/codes/dates/links
|
|
687
|
+
const inv = verifyClaimsInvariant(rawText, shapedText);
|
|
688
|
+
if (!inv.ok) {
|
|
689
|
+
reasons.push('invariant_violation');
|
|
690
|
+
shapedText = rawText;
|
|
691
|
+
bubbles = [rawText];
|
|
692
|
+
}
|
|
693
|
+
if (voiceHooks && typeof voiceHooks.shapeOutgoing === 'function') {
|
|
694
|
+
emit('voice_hook_shape_outgoing', { taskId: task.id, bubblesCount: bubbles.length, invariantOk: inv.ok });
|
|
695
|
+
}
|
|
696
|
+
finalText = shapedText;
|
|
697
|
+
|
|
698
|
+
// 5) External audience identity disclosure & approval-bound outbox
|
|
699
|
+
if (isExternalReply || opts.replyCheck === true || reasons.length > 0) {
|
|
700
|
+
if (isExternalReply && isIdentityQuestion(task.objective)) {
|
|
701
|
+
if (speaksAs === 'owner') {
|
|
702
|
+
reasons.push('identity_question_handoff');
|
|
703
|
+
handoffTo = userDoc.active ? userDoc.handoffRole() : 'owner';
|
|
704
|
+
} else {
|
|
705
|
+
const disc = checkAiDisclosure(rawText);
|
|
706
|
+
if (!disc.ok) reasons.push(disc.reason);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
if (isExternalReply && !approvedByHuman && !(turnInput && turnInput.autoSendExternal === true && reasons.length === 0)) {
|
|
710
|
+
reasons.push('external_requires_approval');
|
|
711
|
+
}
|
|
712
|
+
const decision = reasons.length > 0 ? 'hold' : 'send';
|
|
713
|
+
const labeledText = isExternalReply
|
|
714
|
+
? applyAiLabel(shapedText, {
|
|
715
|
+
label: (turnInput && turnInput.aiLabel) || '[AI Assistant]',
|
|
716
|
+
speaksAs,
|
|
717
|
+
approvedByHuman,
|
|
718
|
+
})
|
|
719
|
+
: shapedText;
|
|
720
|
+
const shapedHash = crypto.createHash('sha256').update(JSON.stringify(bubbles)).digest('hex').slice(0, 16);
|
|
721
|
+
const approvers = peopleOn ? team.approversFor({ tier: 'external_comm', action: 'send' }) : [];
|
|
722
|
+
const ob = {
|
|
723
|
+
taskId: task.id, decision, reasons, speaksAs, handoffTo,
|
|
724
|
+
text: shapedText, rawText, bubbles, shapedHash, labeledText,
|
|
725
|
+
grounding: groundingRes, invariantOk: inv.ok,
|
|
726
|
+
...shapeMeta, approvers, ts: new Date().toISOString(),
|
|
727
|
+
};
|
|
728
|
+
writeOutbox(ob);
|
|
729
|
+
emit('reply_checked', { taskId: task.id, decision, reasons, speaksAs, handoffTo, shapedHash, invariantOk: inv.ok });
|
|
730
|
+
if (decision === 'hold') {
|
|
731
|
+
emit('reply_held', { taskId: task.id, reasons, approvers, handoffTo, shapedHash });
|
|
732
|
+
waiting.push({ taskId: task.id, kind: 'reply', reasons, approvers, handoffTo, shapedHash });
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
const bbPath = path.join(blackboardDir, `${task.id}.md`);
|
|
738
|
+
fs.appendFileSync(bbPath, `${finalText}\n`);
|
|
739
|
+
emit('blackboard_write', { taskId: task.id, path: path.relative(runDir, bbPath), appendOnly: true, bytes: Buffer.byteLength(finalText) });
|
|
740
|
+
|
|
741
|
+
outputs[task.id] = finalText;
|
|
742
|
+
task.status = 'done'; done.add(task.id);
|
|
743
|
+
emit('task_completed', { id: task.id });
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
function personStep(task) {
|
|
747
|
+
const slug = task.person;
|
|
748
|
+
const card = team.get(slug);
|
|
749
|
+
if (!card || !card.valid) {
|
|
750
|
+
emit('error', { reason: `task ${task.id}: person "${slug}" has no valid card in ${path.relative(cwd, team.dir) || team.dir}`, taskId: task.id });
|
|
751
|
+
task.status = 'blocked'; waiting.push({ taskId: task.id, kind: 'person', person: slug, problem: card ? 'invalid_card' : 'unknown_person' }); return;
|
|
752
|
+
}
|
|
753
|
+
const doneP = path.join(controlDir, 'done', `${task.id}.json`);
|
|
754
|
+
if (fs.existsSync(doneP)) {
|
|
755
|
+
let d = {}; try { d = JSON.parse(fs.readFileSync(doneP, 'utf8')); } catch { /* unreadable */ }
|
|
756
|
+
if (d.by === slug) {
|
|
757
|
+
const text = `${String(d.note || d.output || `${slug} marked this step done.`)}\n`;
|
|
758
|
+
const bbP = path.join(blackboardDir, `${task.id}.md`);
|
|
759
|
+
fs.appendFileSync(bbP, text);
|
|
760
|
+
emit('blackboard_write', { taskId: task.id, path: path.relative(runDir, bbP), appendOnly: true, bytes: Buffer.byteLength(text) });
|
|
761
|
+
emit('human_task_done', { taskId: task.id, person: slug });
|
|
762
|
+
outputs[task.id] = text; task.status = 'done'; done.add(task.id);
|
|
763
|
+
emit('task_completed', { id: task.id, person: slug });
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
// a done mark from anyone but the assignee does not count (machine-checked, like an approval)
|
|
767
|
+
emit('human_task_rejected', { taskId: task.id, person: slug, by: d.by || null, reason: 'not_assignee' });
|
|
768
|
+
audit({ taskId: task.id, kind: 'person_step', person: slug, decision: 'rejected', by: d.by || 'unattributed', reason: 'not_assignee' });
|
|
769
|
+
}
|
|
770
|
+
const reqP = path.join(controlDir, 'requests', `${task.id}.json`);
|
|
771
|
+
const channels = Array.isArray(card.frontmatter.channels) ? card.frontmatter.channels : [];
|
|
772
|
+
const first = !fs.existsSync(reqP);
|
|
773
|
+
if (first) {
|
|
774
|
+
fs.mkdirSync(path.dirname(reqP), { recursive: true });
|
|
775
|
+
fs.writeFileSync(reqP, JSON.stringify({ taskId: task.id, person: slug, title: task.title, objective: task.objective, output_format: task.output_format, channels, ts: new Date().toISOString() }, null, 2) + '\n');
|
|
776
|
+
}
|
|
777
|
+
emit('human_task_requested', { taskId: task.id, person: slug, channels, first });
|
|
778
|
+
if (first) notify('person_step', `person:${slug}`, { taskId: task.id }); // asked once; a resume does not ask again
|
|
779
|
+
task.status = 'blocked'; waiting.push({ taskId: task.id, kind: 'person', person: slug });
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
// crash-resume (real, cross-process): on --resume, restore completed tasks from the persisted
|
|
783
|
+
// checkpoint, reload their blackboard outputs into memory, and SKIP re-running them (no re-billing).
|
|
784
|
+
let startIteration = 0;
|
|
785
|
+
if (opts.resume) {
|
|
786
|
+
const cpPath = path.join(runDir, 'checkpoint.json');
|
|
787
|
+
if (fs.existsSync(cpPath)) {
|
|
788
|
+
try {
|
|
789
|
+
const cp = JSON.parse(fs.readFileSync(cpPath, 'utf8'));
|
|
790
|
+
for (const [id, st] of Object.entries(cp.statuses || {})) {
|
|
791
|
+
const tk = work.tasks.find((t) => t.id === id);
|
|
792
|
+
if (tk && st === 'done') {
|
|
793
|
+
tk.status = 'done'; done.add(id);
|
|
794
|
+
const bb = path.join(blackboardDir, `${id}.md`);
|
|
795
|
+
if (fs.existsSync(bb)) outputs[id] = fs.readFileSync(bb, 'utf8'); // restore output for synthesis
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
startIteration = cp.iteration || 0;
|
|
799
|
+
emit('resumed', { from: 'checkpoint.json', restoredDone: [...done], iteration: startIteration });
|
|
800
|
+
} catch (e) { emit('error', { reason: 'resume failed: ' + e.message }); }
|
|
801
|
+
} else {
|
|
802
|
+
emit('resume_noop', { reason: 'no checkpoint.json — running from the start' });
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
// deterministic advance loop
|
|
807
|
+
let iteration = startIteration;
|
|
808
|
+
let stopped = null;
|
|
809
|
+
let resumeReloaded = false;
|
|
810
|
+
while (work.tasks.some((t) => t.status !== 'done' && t.status !== 'blocked')) {
|
|
811
|
+
if (fs.existsSync(path.join(controlDir, 'stop'))) { stopped = 'control/stop'; emit('circuit_breaker', { reason: stopped }); break; }
|
|
812
|
+
const breach = capBreach(iteration + 1);
|
|
813
|
+
if (breach) { stopped = breach; emit('circuit_breaker', { reason: breach, action: 'pause' }); fs.writeFileSync(path.join(runDir, 'PAUSED'), breach); notify('on_exceed', team.cfg.notify.on_exceed || (team.membersOf('owner').length ? 'role:owner' : null), { breach }); break; }
|
|
814
|
+
|
|
815
|
+
const ready = work.tasks.filter((t) => t.status === 'todo' && t.blockedBy.every((id) => done.has(id)));
|
|
816
|
+
if (!ready.length) {
|
|
817
|
+
if (work.tasks.every((t) => t.status === 'done' || t.status === 'blocked')) break;
|
|
818
|
+
// what is left waits on a blocked step (a gate, a person): stop here and resume later — not a deadlock
|
|
819
|
+
if (work.tasks.some((t) => t.status === 'blocked')) { emit('waiting', { blocked: work.tasks.filter((t) => t.status === 'blocked').map((t) => t.id), pending: work.tasks.filter((t) => t.status === 'todo').map((t) => t.id) }); break; }
|
|
820
|
+
emit('error', { reason: 'deadlock: no ready tasks but work remains', pending: work.tasks.filter((t) => t.status === 'todo').map((t) => t.id) });
|
|
821
|
+
throw new Error('WORK.md deadlock (check blockedBy cycles)');
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
const parallelBatch = ready.filter((t) => t.parallel);
|
|
825
|
+
const batch = (decision === 'multi' && parallelBatch.length >= 2) ? parallelBatch : [ready[0]];
|
|
826
|
+
emit('dispatch', { iteration: iteration + 1, tasks: batch.map((t) => t.id), parallel: batch.length > 1 });
|
|
827
|
+
await Promise.all(batch.map((t) => runTask(t)));
|
|
828
|
+
|
|
829
|
+
iteration += 1;
|
|
830
|
+
// checkpoint: filesystem is the state; try git, fall back to a checkpoint file.
|
|
831
|
+
const statuses = Object.fromEntries(work.tasks.map((t) => [t.id, t.status]));
|
|
832
|
+
fs.writeFileSync(path.join(runDir, 'checkpoint.json'), JSON.stringify({ iteration, statuses }, null, 2));
|
|
833
|
+
let backend = 'file';
|
|
834
|
+
const git = spawnSync('git', ['-C', cwd, 'add', '-A'], { encoding: 'utf8' });
|
|
835
|
+
if (git.status === 0) { const c = spawnSync('git', ['-C', cwd, 'commit', '-m', `scroll run ${runId} iter ${iteration}`], { encoding: 'utf8' }); if (c.status === 0) backend = 'git'; }
|
|
836
|
+
emit('checkpoint_written', { checkpointId: `${runId}-iter${iteration}`, backend });
|
|
837
|
+
|
|
838
|
+
// measured resume: simulate a crash after the first checkpoint, then RESUME from the persisted file
|
|
839
|
+
// (re-read checkpoint.json → restore task statuses → continue). This exercises resume logic, not just file existence.
|
|
840
|
+
if (opts.resumeSelftest && !resumeReloaded && iteration === 1) {
|
|
841
|
+
const cp = JSON.parse(fs.readFileSync(path.join(runDir, 'checkpoint.json'), 'utf8'));
|
|
842
|
+
done.clear();
|
|
843
|
+
for (const [id, st] of Object.entries(cp.statuses || {})) {
|
|
844
|
+
const tk = work.tasks.find((t) => t.id === id);
|
|
845
|
+
if (tk) { tk.status = st; if (st === 'done') done.add(id); }
|
|
846
|
+
}
|
|
847
|
+
resumeReloaded = true;
|
|
848
|
+
emit('interrupt_simulated', { afterIteration: 1, restoredFrom: `${runId}-iter1`, restoredDone: [...done] });
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
// pick final output (dependents computed above)
|
|
853
|
+
const finalTask = work.tasks.find((t) => t.final && t.status === 'done')
|
|
854
|
+
|| [...work.tasks].reverse().find((t) => t.status === 'done' && !dependents.has(t.id))
|
|
855
|
+
|| [...work.tasks].reverse().find((t) => t.status === 'done');
|
|
856
|
+
const finalText = finalTask ? outputs[finalTask.id] : '';
|
|
857
|
+
fs.writeFileSync(outPath, finalText || '');
|
|
858
|
+
emit('output_saved', { path: path.relative(runDir, outPath) || outPath, bytes: Buffer.byteLength(finalText || ''), finalTask: finalTask ? finalTask.id : null });
|
|
859
|
+
|
|
860
|
+
// verify BEFORE completed
|
|
861
|
+
const checks = verify(finalText, { conversational: hadConversationalTurn });
|
|
862
|
+
emit('verification', { passed: checks.passed, checks: checks.detail });
|
|
863
|
+
|
|
864
|
+
// measured checkpoint-resume evidence (only when a resume self-test was requested)
|
|
865
|
+
if (opts.resumeSelftest) {
|
|
866
|
+
emit('checkpoint_resume_checked', {
|
|
867
|
+
passed: resumeReloaded && done.size === work.tasks.length,
|
|
868
|
+
checkpointId: `${runId}-iter1`,
|
|
869
|
+
resumedFrom: 'checkpoint.json',
|
|
870
|
+
completedAfterResume: done.size === work.tasks.length,
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
const inputTotal = ledger.uncached + ledger.cached;
|
|
875
|
+
const metrics = {
|
|
876
|
+
uncachedInputTokens: ledger.uncached, cachedInputTokens: ledger.cached, outputTokens: ledger.output,
|
|
877
|
+
billableTokenEquivalent: billable(), costUsd: round(usd(), 5), durationMs: Date.now() - startedAt,
|
|
878
|
+
cacheHitPct: inputTotal ? round((ledger.cached / inputTotal) * 100, 1) : 0,
|
|
879
|
+
providerCalls: ledger.calls, tasksCompleted: done.size, tasksTotal: work.tasks.length,
|
|
880
|
+
decision, spawnedAgents,
|
|
881
|
+
};
|
|
882
|
+
const status = stopped ? 'paused' : (done.size === work.tasks.length ? 'completed' : 'incomplete');
|
|
883
|
+
emit(stopped ? 'paused' : 'completed', { status, verificationPassed: checks.passed, metrics });
|
|
884
|
+
|
|
885
|
+
// comprehension digest — a human-readable summary derived from the event stream (no model call),
|
|
886
|
+
// so an owner can READ what the run did rather than just trust it (anti passive-delegation).
|
|
887
|
+
let digestPath = null;
|
|
888
|
+
try {
|
|
889
|
+
digestPath = writeDigest({ runDir, eventsPath, language: runLanguage || 'en', metrics });
|
|
890
|
+
emit('digest_written', { path: path.relative(runDir, digestPath) });
|
|
891
|
+
} catch (e) { emit('error', { reason: 'digest failed: ' + e.message }); }
|
|
892
|
+
|
|
893
|
+
return { runId, runDir, eventsPath, outPath, digestPath, output: finalText, status, verification: checks, metrics, decision, spawnedAgents, waiting, outbox };
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
// Create a git worktree for a parallel task (real isolation for parallel writes). Returns the path,
|
|
897
|
+
// or null if cwd is not a git repo / worktree creation fails (caller falls back to a scratch dir).
|
|
898
|
+
function makeWorktree(cwd, runDir, taskId, emit) {
|
|
899
|
+
const isRepo = spawnSync('git', ['-C', cwd, 'rev-parse', '--is-inside-work-tree'], { encoding: 'utf8' }).status === 0;
|
|
900
|
+
if (!isRepo) return null;
|
|
901
|
+
const wt = path.join(runDir, 'worktrees', taskId);
|
|
902
|
+
const r = spawnSync('git', ['-C', cwd, 'worktree', 'add', '--detach', '--force', wt], { encoding: 'utf8' });
|
|
903
|
+
if (r.status === 0) { emit('worktree_created', { taskId, path: path.relative(cwd, wt) }); return wt; }
|
|
904
|
+
return null;
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
function verify(text, { conversational = false } = {}) {
|
|
908
|
+
const t = (text || '').toLowerCase();
|
|
909
|
+
const notBlocked = !/\b(i am|i'm)\s+blocked\b|cannot read|can't read|không đọc được|khong doc duoc|bị kẹt|bi ket|missing sources|source is missing|sources are missing|permission denied/.test(t);
|
|
910
|
+
if (conversational) {
|
|
911
|
+
const nonEmpty = (text || '').trim().length > 0;
|
|
912
|
+
const detail = { conversational: true, nonEmpty, notBlocked };
|
|
913
|
+
return { passed: nonEmpty && notBlocked, detail };
|
|
914
|
+
}
|
|
915
|
+
const detail = {
|
|
916
|
+
hasVerdict: /verdict|kết luận|ket luan/.test(t),
|
|
917
|
+
hasEvidence: /evidence|bằng chứng|bang chung|##\s*evidence/.test(t),
|
|
918
|
+
hasAssumptions: /assumption|giả định|gia dinh/.test(t),
|
|
919
|
+
hasNextActions: /next action|next step|hành động|hanh dong|##\s*next/.test(t),
|
|
920
|
+
citesSources: /\[s\d\]/i.test(text || ''),
|
|
921
|
+
notBlocked,
|
|
922
|
+
};
|
|
923
|
+
// pass = solved the task in the required shape and did not stall on a blocker
|
|
924
|
+
const passed = detail.hasVerdict && (detail.hasNextActions || detail.hasEvidence) && detail.notBlocked;
|
|
925
|
+
return { passed, detail };
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
const round = (n, d = 2) => { const p = 10 ** d; return Math.round(n * p) / p; };
|