code60-transmission-server 0.1.0-staging.2 → 0.1.0-staging.20
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/INTENT.md +47 -0
- package/README.md +58 -253
- package/dist/src/acp.js +115 -0
- package/dist/src/contract.js +108 -0
- package/dist/src/engine.js +596 -0
- package/dist/src/main.js +72 -22
- package/dist/src/repository.js +94 -0
- package/dist/src/service.js +188 -0
- package/package.json +16 -32
- package/reference/openapi.json +834 -0
- package/README.engine.md +0 -401
- package/dist/src/broker/filesystem.js +0 -385
- package/dist/src/broker/index.js +0 -734
- package/dist/src/broker/native/README.md +0 -192
- package/dist/src/broker/native/build.js +0 -43
- package/dist/src/broker/native/fs-helper.c +0 -529
- package/dist/src/broker/native/git-helper.c +0 -205
- package/dist/src/broker/policy.js +0 -149
- package/dist/src/broker/startup-lock.js +0 -14
- package/dist/src/broker/types.js +0 -11
- package/dist/src/cli/daemon.js +0 -365
- package/dist/src/cli/helpers.js +0 -104
- package/dist/src/cli/main.js +0 -180
- package/dist/src/config/deployment.js +0 -163
- package/dist/src/config/documents.js +0 -29
- package/dist/src/config/profile-validation.js +0 -123
- package/dist/src/config/schema-graph.js +0 -237
- package/dist/src/config/schema-loader.js +0 -185
- package/dist/src/config/validation-pool.js +0 -105
- package/dist/src/config/validation-protocol.js +0 -1
- package/dist/src/config/validation-worker.js +0 -106
- package/dist/src/config/workflow-language.js +0 -9
- package/dist/src/config/workflow-schema.json +0 -534
- package/dist/src/config/workflow-types.js +0 -1
- package/dist/src/config/workflow-validation.js +0 -95
- package/dist/src/engine/budgets.js +0 -50
- package/dist/src/engine/clocks.js +0 -27
- package/dist/src/engine/commands.js +0 -441
- package/dist/src/engine/contracts.js +0 -11
- package/dist/src/engine/drain-state.js +0 -31
- package/dist/src/engine/effective-permissions.js +0 -120
- package/dist/src/engine/instruction-pins.js +0 -205
- package/dist/src/engine/retries.js +0 -50
- package/dist/src/engine/state.js +0 -1
- package/dist/src/engine/timing.js +0 -23
- package/dist/src/engine/tool-observations.js +0 -87
- package/dist/src/engine/workflow-engine.js +0 -1807
- package/dist/src/mcp/catalog.js +0 -71
- package/dist/src/mcp/main.js +0 -24
- package/dist/src/mcp/server.js +0 -104
- package/dist/src/runtime/codex-app-server.js +0 -91
- package/dist/src/runtime/containment.js +0 -82
- package/dist/src/runtime/engine-driver.js +0 -212
- package/dist/src/runtime/json-line-writer.js +0 -192
- package/dist/src/runtime/pinned-instruction-sources.js +0 -24
- package/dist/src/runtime/protocol-client.js +0 -264
- package/dist/src/runtime/protocol-pool.js +0 -183
- package/dist/src/runtime/protocol-projection.js +0 -393
- package/dist/src/runtime/protocol-types.js +0 -1
- package/dist/src/runtime/protocol-worker.js +0 -18
- package/dist/src/runtime/provider-bridge.js +0 -288
- package/dist/src/runtime/provider-credentials.js +0 -41
- package/dist/src/runtime/provider-transport.js +0 -404
- package/dist/src/runtime/runtime-broker.js +0 -213
- package/dist/src/runtime/workspace-instructions.js +0 -120
- package/dist/src/runtime/workspace-view.js +0 -74
- package/dist/src/server/api-contract.json +0 -11078
- package/dist/src/server/api-types.js +0 -1
- package/dist/src/server/application.js +0 -38
- package/dist/src/server/auth.js +0 -67
- package/dist/src/server/broker-handlers.js +0 -242
- package/dist/src/server/control-plane.js +0 -50
- package/dist/src/server/deployment.js +0 -237
- package/dist/src/server/errors.js +0 -22
- package/dist/src/server/history-handlers.js +0 -113
- package/dist/src/server/http.js +0 -124
- package/dist/src/server/paging.js +0 -56
- package/dist/src/server/publication.js +0 -119
- package/dist/src/server/tool-event-handler.js +0 -77
- package/dist/src/server/workspace-handlers.js +0 -34
- package/dist/src/storage/coordinator.js +0 -224
- package/dist/src/storage/startup-lock.js +0 -71
- package/dist/src/storage/store.js +0 -167
- package/dist/src/tools/check-worker.js +0 -81
- package/dist/src/tools/checks.js +0 -106
- package/dist/src/tools/dns.js +0 -248
- package/dist/src/tools/http-contract.js +0 -38
- package/dist/src/tools/http.js +0 -358
- package/dist/src/tools/index.js +0 -177
- package/dist/src/tools/worker-check.js +0 -73
- package/dist/src/workspaces/git.js +0 -227
- package/dist/src/workspaces/index.js +0 -715
- package/examples/deployment.local.json +0 -22
- package/scripts/postinstall.mjs +0 -25
|
@@ -0,0 +1,596 @@
|
|
|
1
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { closeSync, mkdirSync, openSync, readSync, realpathSync, renameSync, rmSync, statSync, } from 'node:fs';
|
|
4
|
+
import { join, sep } from 'node:path';
|
|
5
|
+
import { AcpHarness } from './acp.js';
|
|
6
|
+
import { terminal, verifierReply, workerReply, } from './contract.js';
|
|
7
|
+
import { repository, snapshot, writePatch } from './repository.js';
|
|
8
|
+
const diagnostic = (error) => error instanceof Error ? error.message : String(error);
|
|
9
|
+
function taskState(run, index) {
|
|
10
|
+
if (index < run.index)
|
|
11
|
+
return 'accepted';
|
|
12
|
+
if (index > run.index)
|
|
13
|
+
return 'pending';
|
|
14
|
+
switch (run.status) {
|
|
15
|
+
case 'running':
|
|
16
|
+
if (run.phase === 'verifier')
|
|
17
|
+
return 'verifying';
|
|
18
|
+
return run.repairsUsed > 0 ? 'repairing' : 'implementing';
|
|
19
|
+
case 'completed':
|
|
20
|
+
return 'accepted';
|
|
21
|
+
default:
|
|
22
|
+
return run.status;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export class Engine {
|
|
26
|
+
state;
|
|
27
|
+
factory;
|
|
28
|
+
db;
|
|
29
|
+
runs = new Map();
|
|
30
|
+
pumps = new Map();
|
|
31
|
+
agents = new Map();
|
|
32
|
+
pending = new Map();
|
|
33
|
+
disposals = new Map();
|
|
34
|
+
pauses = new Map();
|
|
35
|
+
closing = false;
|
|
36
|
+
settling = new Set();
|
|
37
|
+
constructor(state, factory = (hooks, cwd) => new AcpHarness(hooks, cwd)) {
|
|
38
|
+
this.state = state;
|
|
39
|
+
this.factory = factory;
|
|
40
|
+
mkdirSync(state, { recursive: true, mode: 0o700 });
|
|
41
|
+
this.db = new DatabaseSync(join(state, 'workflows.sqlite'));
|
|
42
|
+
this.db.exec(`PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL;
|
|
43
|
+
CREATE TABLE IF NOT EXISTS runs(id TEXT PRIMARY KEY, caller_key TEXT UNIQUE NOT NULL, body TEXT NOT NULL);
|
|
44
|
+
CREATE TABLE IF NOT EXISTS events(sequence INTEGER PRIMARY KEY, run TEXT NOT NULL, time TEXT NOT NULL, kind TEXT NOT NULL, detail TEXT NOT NULL);
|
|
45
|
+
CREATE INDEX IF NOT EXISTS by_run ON events(run,sequence);`);
|
|
46
|
+
for (const row of this.db.prepare('SELECT body FROM runs').all()) {
|
|
47
|
+
const r = JSON.parse(String(row.body));
|
|
48
|
+
this.runs.set(r.id, r);
|
|
49
|
+
if (terminal(r.status))
|
|
50
|
+
continue;
|
|
51
|
+
if (r.status === 'pausing' || r.status === 'paused') {
|
|
52
|
+
this.withdrawPermission(r);
|
|
53
|
+
this.retireSessions(r);
|
|
54
|
+
const interrupted = r.status === 'pausing';
|
|
55
|
+
r.status = 'paused';
|
|
56
|
+
if (interrupted)
|
|
57
|
+
this.save(r, 'paused');
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (r.worker || r.verifier) {
|
|
61
|
+
this.retireSessions(r);
|
|
62
|
+
r.feedback +=
|
|
63
|
+
'\nTransmission restarted. Inspect existing work and the outcome of consequential actions before repeating them. Ask if their outcome cannot be determined.';
|
|
64
|
+
this.save(r, 'task_resumed', { task: r.plan.tasks[r.index].name });
|
|
65
|
+
}
|
|
66
|
+
this.wake(r);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
dir(r) {
|
|
70
|
+
return join(this.state, r.id);
|
|
71
|
+
}
|
|
72
|
+
save(r, kind, detail = {}) {
|
|
73
|
+
this.db.exec('BEGIN IMMEDIATE');
|
|
74
|
+
try {
|
|
75
|
+
this.db
|
|
76
|
+
.prepare('INSERT INTO runs VALUES(?,?,?) ON CONFLICT(id) DO UPDATE SET body=excluded.body')
|
|
77
|
+
.run(r.id, r.plan.idempotencyKey, JSON.stringify(r));
|
|
78
|
+
this.event(r, kind, detail);
|
|
79
|
+
this.db.exec('COMMIT');
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
this.db.exec('ROLLBACK');
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
event(r, kind, detail) {
|
|
87
|
+
this.db
|
|
88
|
+
.prepare('INSERT INTO events(run,time,kind,detail) VALUES(?,?,?,?)')
|
|
89
|
+
.run(r.id, new Date().toISOString(), kind, JSON.stringify(detail));
|
|
90
|
+
}
|
|
91
|
+
get(id) {
|
|
92
|
+
const r = this.runs.get(id);
|
|
93
|
+
if (!r)
|
|
94
|
+
throw new Error('Unknown workflow.');
|
|
95
|
+
return r;
|
|
96
|
+
}
|
|
97
|
+
start(plan) {
|
|
98
|
+
const prior = this.db
|
|
99
|
+
.prepare('SELECT id FROM runs WHERE caller_key=?')
|
|
100
|
+
.get(plan.idempotencyKey);
|
|
101
|
+
if (prior) {
|
|
102
|
+
const r = this.get(String(prior.id));
|
|
103
|
+
if (JSON.stringify(r.plan) !==
|
|
104
|
+
JSON.stringify({ ...plan, repository: repository(plan.repository) }))
|
|
105
|
+
throw new Error('This key belongs to a different workflow.');
|
|
106
|
+
return this.status(r.id);
|
|
107
|
+
}
|
|
108
|
+
const root = repository(plan.repository);
|
|
109
|
+
const state = realpathSync(this.state);
|
|
110
|
+
if (state === root || state.startsWith(root + sep))
|
|
111
|
+
throw new Error('Keep Transmission state outside the target repository.');
|
|
112
|
+
if ([...this.runs.values()].some((r) => (!terminal(r.status) || this.settling.has(r.id)) &&
|
|
113
|
+
r.plan.repository === root))
|
|
114
|
+
throw new Error('A workflow is already active in this project.');
|
|
115
|
+
const id = randomUUID();
|
|
116
|
+
const r = {
|
|
117
|
+
id,
|
|
118
|
+
plan: { ...plan, repository: root },
|
|
119
|
+
baseline: snapshot(root, join(this.state, id)),
|
|
120
|
+
index: 0,
|
|
121
|
+
status: 'running',
|
|
122
|
+
phase: 'worker',
|
|
123
|
+
generation: 0,
|
|
124
|
+
repairsUsed: 0,
|
|
125
|
+
feedback: '',
|
|
126
|
+
messages: [],
|
|
127
|
+
guidance: [],
|
|
128
|
+
formatErrors: 0,
|
|
129
|
+
accepted: [],
|
|
130
|
+
attempts: [],
|
|
131
|
+
};
|
|
132
|
+
this.save(r, 'started');
|
|
133
|
+
this.runs.set(id, r);
|
|
134
|
+
this.wake(r);
|
|
135
|
+
return this.status(id);
|
|
136
|
+
}
|
|
137
|
+
status(id) {
|
|
138
|
+
const row = this.db.prepare(`
|
|
139
|
+
SELECT body, (SELECT MAX(sequence) FROM events WHERE run = runs.id) AS cursor
|
|
140
|
+
FROM runs WHERE id = ?
|
|
141
|
+
`).get(id);
|
|
142
|
+
if (!row)
|
|
143
|
+
throw new Error('Unknown workflow.');
|
|
144
|
+
const r = JSON.parse(String(row.body));
|
|
145
|
+
return {
|
|
146
|
+
runId: id,
|
|
147
|
+
status: r.status,
|
|
148
|
+
tasks: r.plan.tasks.map(({ name }, index) => ({ index, name, state: taskState(r, index) })),
|
|
149
|
+
activeTaskIndex: terminal(r.status) ? null : r.index,
|
|
150
|
+
eventCursor: Number(row.cursor),
|
|
151
|
+
task: r.plan.tasks[r.index]?.name,
|
|
152
|
+
phase: terminal(r.status) ? undefined : r.phase,
|
|
153
|
+
accepted: r.accepted,
|
|
154
|
+
decision: r.question,
|
|
155
|
+
reason: r.failure,
|
|
156
|
+
attempts: r.attempts,
|
|
157
|
+
...(r.status === 'completed'
|
|
158
|
+
? {
|
|
159
|
+
result: r.accepted.at(-1).result,
|
|
160
|
+
verdict: r.accepted.at(-1).verdict,
|
|
161
|
+
}
|
|
162
|
+
: {}),
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
events(id, after) {
|
|
166
|
+
this.get(id);
|
|
167
|
+
return this.db
|
|
168
|
+
.prepare('SELECT sequence,time,kind,detail FROM events WHERE run=? AND sequence>? ORDER BY sequence LIMIT 100')
|
|
169
|
+
.all(id, after)
|
|
170
|
+
.map((e) => ({
|
|
171
|
+
sequence: Number(e.sequence),
|
|
172
|
+
time: String(e.time),
|
|
173
|
+
kind: String(e.kind),
|
|
174
|
+
detail: JSON.parse(String(e.detail)),
|
|
175
|
+
}));
|
|
176
|
+
}
|
|
177
|
+
diff(id, offset) {
|
|
178
|
+
const r = this.get(id), rev = r.accepted.at(-1)?.revision;
|
|
179
|
+
if (!rev)
|
|
180
|
+
return { content: '', nextOffset: null };
|
|
181
|
+
const path = join(this.dir(r), rev + '.patch'), size = statSync(path).size, fd = openSync(path, 'r');
|
|
182
|
+
try {
|
|
183
|
+
const b = Buffer.alloc(Math.max(0, Math.min(size - offset, 65536)));
|
|
184
|
+
const n = readSync(fd, b, 0, b.length, offset);
|
|
185
|
+
return {
|
|
186
|
+
content: b.subarray(0, n).toString('base64'),
|
|
187
|
+
nextOffset: offset + n < size ? offset + n : null,
|
|
188
|
+
revision: rev,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
finally {
|
|
192
|
+
closeSync(fd);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
answer(id, questionId, answer) {
|
|
196
|
+
const r = this.get(id);
|
|
197
|
+
if (r.lastAnswer?.id === questionId && r.lastAnswer.answer === answer)
|
|
198
|
+
return this.status(id);
|
|
199
|
+
if (!['needs_input', 'pausing', 'paused'].includes(r.status) || r.question?.id !== questionId)
|
|
200
|
+
throw new Error('This decision is no longer pending.');
|
|
201
|
+
if (r.question.options &&
|
|
202
|
+
!r.question.options.some((opt) => opt.id === answer))
|
|
203
|
+
throw new Error('Select one of the offered option IDs.');
|
|
204
|
+
const q = r.question, p = this.pending.get(id);
|
|
205
|
+
delete r.question;
|
|
206
|
+
delete r.questionKind;
|
|
207
|
+
r.lastAnswer = { id: questionId, answer };
|
|
208
|
+
if (r.status === 'needs_input')
|
|
209
|
+
r.status = 'running';
|
|
210
|
+
if (!p || p.id !== q.id)
|
|
211
|
+
r.guidance.push('Decision: ' + q.text + '\nUser answer: ' + answer);
|
|
212
|
+
this.save(r, 'answered', { question: q, answer });
|
|
213
|
+
this.pending.delete(id);
|
|
214
|
+
p?.resolve(answer);
|
|
215
|
+
this.wake(r);
|
|
216
|
+
return this.status(id);
|
|
217
|
+
}
|
|
218
|
+
message(id, message) {
|
|
219
|
+
const r = this.get(id);
|
|
220
|
+
if (terminal(r.status))
|
|
221
|
+
throw new Error('Workflow has ended.');
|
|
222
|
+
r.messages.push(message);
|
|
223
|
+
r.guidance.push(message);
|
|
224
|
+
this.save(r, 'message', { message });
|
|
225
|
+
return this.status(id);
|
|
226
|
+
}
|
|
227
|
+
pause(id) {
|
|
228
|
+
const r = this.get(id);
|
|
229
|
+
if (this.closing)
|
|
230
|
+
throw new Error('Engine is stopping.');
|
|
231
|
+
if (terminal(r.status))
|
|
232
|
+
throw new Error('Workflow has ended.');
|
|
233
|
+
const prior = this.pauses.get(id);
|
|
234
|
+
if (prior)
|
|
235
|
+
return prior;
|
|
236
|
+
if (r.status === 'paused')
|
|
237
|
+
return Promise.resolve(this.status(id));
|
|
238
|
+
if (r.status !== 'pausing') {
|
|
239
|
+
r.status = 'pausing';
|
|
240
|
+
this.withdrawPermission(r);
|
|
241
|
+
this.save(r, 'pause_requested');
|
|
242
|
+
}
|
|
243
|
+
// Install the shared operation before shutdown can settle a prompt or a
|
|
244
|
+
// concurrent pause/cancel can join the same physical-exit barrier.
|
|
245
|
+
const work = Promise.resolve().then(async () => {
|
|
246
|
+
await this.dispose(r);
|
|
247
|
+
await this.pumps.get(id);
|
|
248
|
+
if (r.status === 'pausing') {
|
|
249
|
+
this.retireSessions(r);
|
|
250
|
+
r.status = 'paused';
|
|
251
|
+
this.save(r, 'paused');
|
|
252
|
+
}
|
|
253
|
+
return this.status(id);
|
|
254
|
+
}).finally(() => this.pauses.delete(id));
|
|
255
|
+
this.pauses.set(id, work);
|
|
256
|
+
return work;
|
|
257
|
+
}
|
|
258
|
+
resume(id) {
|
|
259
|
+
const r = this.get(id);
|
|
260
|
+
if (this.closing)
|
|
261
|
+
throw new Error('Engine is stopping.');
|
|
262
|
+
if (terminal(r.status))
|
|
263
|
+
throw new Error('Workflow has ended.');
|
|
264
|
+
if (r.status === 'pausing')
|
|
265
|
+
throw new Error('Workflow is still pausing.');
|
|
266
|
+
if (r.status !== 'paused')
|
|
267
|
+
return this.status(id);
|
|
268
|
+
this.retireSessions(r);
|
|
269
|
+
r.feedback += '\nWorkflow resumed after an operator pause. Inspect existing work and the outcome of consequential actions before repeating them. Ask if their outcome cannot be determined.';
|
|
270
|
+
r.status = r.question ? 'needs_input' : 'running';
|
|
271
|
+
this.save(r, 'resumed');
|
|
272
|
+
this.wake(r);
|
|
273
|
+
return this.status(id);
|
|
274
|
+
}
|
|
275
|
+
withdrawPermission(r) {
|
|
276
|
+
// Older records did not store question origin. Only permissions have
|
|
277
|
+
// offered options; scope questions must survive the loss of a session.
|
|
278
|
+
if (r.questionKind === 'permission' || r.question?.options) {
|
|
279
|
+
delete r.question;
|
|
280
|
+
delete r.questionKind;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
async cancel(id) {
|
|
284
|
+
const r = this.get(id);
|
|
285
|
+
if (!terminal(r.status)) {
|
|
286
|
+
r.status = 'cancelled';
|
|
287
|
+
delete r.question;
|
|
288
|
+
delete r.questionKind;
|
|
289
|
+
this.save(r, 'cancelled');
|
|
290
|
+
}
|
|
291
|
+
if (r.status === 'cancelled') {
|
|
292
|
+
this.settling.add(id);
|
|
293
|
+
try {
|
|
294
|
+
await this.dispose(r);
|
|
295
|
+
await this.pumps.get(id);
|
|
296
|
+
}
|
|
297
|
+
finally {
|
|
298
|
+
this.settling.delete(id);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return this.status(id);
|
|
302
|
+
}
|
|
303
|
+
dispose(r) {
|
|
304
|
+
const prior = this.disposals.get(r.id);
|
|
305
|
+
if (prior)
|
|
306
|
+
return prior;
|
|
307
|
+
this.pending.get(r.id)?.resolve(undefined);
|
|
308
|
+
this.pending.delete(r.id);
|
|
309
|
+
const agent = this.agents.get(r.id);
|
|
310
|
+
this.agents.delete(r.id);
|
|
311
|
+
const work = Promise.resolve().then(() => agent?.close())
|
|
312
|
+
.catch(error => {
|
|
313
|
+
// A rejected shutdown is not proof of exit. Keep the same agent for
|
|
314
|
+
// another shutdown attempt instead of later declaring it paused.
|
|
315
|
+
if (agent)
|
|
316
|
+
this.agents.set(r.id, agent);
|
|
317
|
+
throw error;
|
|
318
|
+
})
|
|
319
|
+
.finally(() => this.disposals.delete(r.id));
|
|
320
|
+
this.disposals.set(r.id, work);
|
|
321
|
+
return work;
|
|
322
|
+
}
|
|
323
|
+
async close() {
|
|
324
|
+
this.closing = true;
|
|
325
|
+
await Promise.all([...this.runs.values()].map((r) => this.dispose(r)));
|
|
326
|
+
await Promise.all(this.pumps.values());
|
|
327
|
+
await Promise.all(this.pauses.values());
|
|
328
|
+
this.db.close();
|
|
329
|
+
}
|
|
330
|
+
wake(r) {
|
|
331
|
+
if (this.closing || r.status !== 'running' || this.pumps.has(r.id))
|
|
332
|
+
return;
|
|
333
|
+
const work = Promise.resolve()
|
|
334
|
+
.then(() => this.drive(r))
|
|
335
|
+
.finally(() => {
|
|
336
|
+
this.pumps.delete(r.id);
|
|
337
|
+
this.wake(r);
|
|
338
|
+
});
|
|
339
|
+
this.pumps.set(r.id, work);
|
|
340
|
+
}
|
|
341
|
+
retireSessions(r) {
|
|
342
|
+
delete r.worker;
|
|
343
|
+
delete r.verifier;
|
|
344
|
+
delete r.candidate;
|
|
345
|
+
r.phase = 'worker';
|
|
346
|
+
r.formatErrors = 0;
|
|
347
|
+
}
|
|
348
|
+
restart(r, reason) {
|
|
349
|
+
r.attempts.push(reason);
|
|
350
|
+
this.retireSessions(r);
|
|
351
|
+
r.repairsUsed = 0;
|
|
352
|
+
delete r.question;
|
|
353
|
+
delete r.questionKind;
|
|
354
|
+
if (r.generation >= r.plan.retries) {
|
|
355
|
+
r.failure = reason + ' Fresh-session retry limit reached.';
|
|
356
|
+
r.status = 'failed';
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
r.generation++;
|
|
360
|
+
r.feedback = reason + ' Inspect existing work before continuing.';
|
|
361
|
+
r.status = 'running';
|
|
362
|
+
}
|
|
363
|
+
this.save(r, 'attempt_ended', {
|
|
364
|
+
reason,
|
|
365
|
+
next: r.status,
|
|
366
|
+
retriesUsed: r.generation,
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
ask(r, question, kind = 'scope') {
|
|
370
|
+
r.question = question;
|
|
371
|
+
r.questionKind = kind;
|
|
372
|
+
r.status = 'needs_input';
|
|
373
|
+
this.save(r, 'needs_input', question);
|
|
374
|
+
}
|
|
375
|
+
client(r) {
|
|
376
|
+
let agent = this.agents.get(r.id);
|
|
377
|
+
if (agent)
|
|
378
|
+
return agent;
|
|
379
|
+
agent = this.factory({
|
|
380
|
+
tool: (session, detail) => {
|
|
381
|
+
if (this.agents.get(r.id) === agent)
|
|
382
|
+
this.event(r, 'tool', { session, detail });
|
|
383
|
+
},
|
|
384
|
+
permission: async (request) => {
|
|
385
|
+
if (r.status !== 'running' || this.closing || this.agents.get(r.id) !== agent)
|
|
386
|
+
return undefined;
|
|
387
|
+
const once = request.options.find((o) => o.kind === 'allow_once');
|
|
388
|
+
if (r.plan.autoApprove && once) {
|
|
389
|
+
this.event(r, 'permission', {
|
|
390
|
+
session: request.sessionId,
|
|
391
|
+
selected: once.optionId,
|
|
392
|
+
});
|
|
393
|
+
return once.optionId;
|
|
394
|
+
}
|
|
395
|
+
const question = {
|
|
396
|
+
id: randomUUID(),
|
|
397
|
+
text: request.toolCall.title ?? 'Choose an agent permission.',
|
|
398
|
+
options: request.options.map((o) => ({
|
|
399
|
+
id: o.optionId,
|
|
400
|
+
name: o.name,
|
|
401
|
+
})),
|
|
402
|
+
};
|
|
403
|
+
this.ask(r, question, 'permission');
|
|
404
|
+
return new Promise((resolve) => this.pending.set(r.id, { id: question.id, resolve }));
|
|
405
|
+
},
|
|
406
|
+
}, r.plan.repository);
|
|
407
|
+
this.agents.set(r.id, agent);
|
|
408
|
+
return agent;
|
|
409
|
+
}
|
|
410
|
+
async drive(r) {
|
|
411
|
+
try {
|
|
412
|
+
while (!this.closing && r.status === 'running') {
|
|
413
|
+
const harness = this.client(r), role = r.phase, task = r.plan.tasks[r.index];
|
|
414
|
+
if (!r[role]) {
|
|
415
|
+
const session = await harness.session(r.plan.repository);
|
|
416
|
+
if (this.closing || r.status !== 'running')
|
|
417
|
+
return;
|
|
418
|
+
r[role] = session;
|
|
419
|
+
this.save(r, 'session_started', {
|
|
420
|
+
role,
|
|
421
|
+
session,
|
|
422
|
+
task: task.name,
|
|
423
|
+
generation: r.generation,
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
const session = r[role], messages = [...r.messages];
|
|
427
|
+
const data = {
|
|
428
|
+
task: role === 'verifier'
|
|
429
|
+
? task
|
|
430
|
+
: {
|
|
431
|
+
name: task.name,
|
|
432
|
+
instructions: task.instructions,
|
|
433
|
+
checks: task.checks,
|
|
434
|
+
},
|
|
435
|
+
incoming: messages,
|
|
436
|
+
priorTasks: r.accepted.slice(-8).map((a) => ({
|
|
437
|
+
name: a.name,
|
|
438
|
+
result: a.result.slice(0, 4096),
|
|
439
|
+
})),
|
|
440
|
+
feedback: r.feedback,
|
|
441
|
+
operatorGuidance: r.guidance.join('\n').slice(-65536),
|
|
442
|
+
...(role === 'verifier' ? { candidate: r.candidate } : {}),
|
|
443
|
+
};
|
|
444
|
+
const instructions = role === 'worker'
|
|
445
|
+
? 'Implement the assigned task using your normal tools in this project. Preserve prior edits, HEAD and staging. Install dependencies and run the relevant checks. Finish commands and stop temporary background services before submitting. Act on feedback from independent review. End with one line of JSON: {"kind":"result","summary":"result and executed checks"}.'
|
|
446
|
+
: 'Independently verify the candidate in the current repository against the verification prompt. Inspect and test it yourself. Leave the source unchanged. If task.checks are specified, run each command with your own harness and include a checks array in the final JSON with command (exact), passed (boolean), and evidence (observed output) for each. End with one line of JSON: {"kind":"verdict","decision":"accept" or "reject","reason":"specific evidence or actionable repair instructions"}.';
|
|
447
|
+
this.save(r, 'turn_started', { role, session, task: task.name });
|
|
448
|
+
const raw = await harness.prompt(session, instructions +
|
|
449
|
+
' Only for a genuine decision about intent or scope, end with {"kind":"question","question":"decision needed"}.\n' +
|
|
450
|
+
JSON.stringify(data));
|
|
451
|
+
if (this.closing || r.status !== 'running')
|
|
452
|
+
return;
|
|
453
|
+
// Retain messages until a turn consumes them successfully.
|
|
454
|
+
r.messages.splice(0, messages.length);
|
|
455
|
+
const line = raw
|
|
456
|
+
.trim()
|
|
457
|
+
.replace(/^```(?:json)?\s*|\s*```$/g, '')
|
|
458
|
+
.trim();
|
|
459
|
+
let value;
|
|
460
|
+
try {
|
|
461
|
+
value = JSON.parse(line);
|
|
462
|
+
}
|
|
463
|
+
catch {
|
|
464
|
+
try {
|
|
465
|
+
value = JSON.parse(line.split('\n').at(-1));
|
|
466
|
+
}
|
|
467
|
+
catch { }
|
|
468
|
+
}
|
|
469
|
+
const reply = role === 'worker'
|
|
470
|
+
? workerReply.safeParse(value)
|
|
471
|
+
: verifierReply.safeParse(value);
|
|
472
|
+
if (!reply.success &&
|
|
473
|
+
role === 'verifier' &&
|
|
474
|
+
r.formatErrors < r.plan.repairs) {
|
|
475
|
+
r.formatErrors++;
|
|
476
|
+
r.feedback =
|
|
477
|
+
'Your verdict could not be parsed. Return the specified JSON verdict and evidence.';
|
|
478
|
+
this.save(r, 'invalid_verdict', { session });
|
|
479
|
+
continue;
|
|
480
|
+
}
|
|
481
|
+
if (!reply.success && role === 'verifier') {
|
|
482
|
+
this.restart(r, 'The verifier did not return a valid verdict.');
|
|
483
|
+
await this.dispose(r);
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
if (!reply.success) {
|
|
487
|
+
await this.repair(r, 'Invalid ' +
|
|
488
|
+
role +
|
|
489
|
+
' response. Return the requested final JSON object.');
|
|
490
|
+
continue;
|
|
491
|
+
}
|
|
492
|
+
r.formatErrors = 0;
|
|
493
|
+
const v = reply.data;
|
|
494
|
+
if (v.kind === 'question') {
|
|
495
|
+
this.ask(r, { id: randomUUID(), text: v.question });
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
const revision = snapshot(r.plan.repository, this.dir(r));
|
|
499
|
+
if (v.kind === 'result') {
|
|
500
|
+
r.candidate = { summary: v.summary, revision };
|
|
501
|
+
delete r.verifier;
|
|
502
|
+
r.phase = 'verifier';
|
|
503
|
+
r.feedback = '';
|
|
504
|
+
this.save(r, 'submitted', { session, revision, summary: v.summary });
|
|
505
|
+
continue;
|
|
506
|
+
}
|
|
507
|
+
const candidate = r.candidate;
|
|
508
|
+
const missing = task.checks?.filter((command) => !v.checks?.some((c) => c.command === command && c.passed));
|
|
509
|
+
if (v.decision === 'accept' && missing?.length) {
|
|
510
|
+
v.decision = 'reject';
|
|
511
|
+
v.reason =
|
|
512
|
+
'Missing passing verifier evidence for ' +
|
|
513
|
+
missing.join(', ') +
|
|
514
|
+
'. ' +
|
|
515
|
+
v.reason;
|
|
516
|
+
}
|
|
517
|
+
this.save(r, 'verdict', {
|
|
518
|
+
worker: r.worker,
|
|
519
|
+
verifier: session,
|
|
520
|
+
revision: candidate.revision,
|
|
521
|
+
...v,
|
|
522
|
+
});
|
|
523
|
+
if (v.decision === 'reject' ||
|
|
524
|
+
candidate.revision !== revision ||
|
|
525
|
+
r.messages.length ||
|
|
526
|
+
messages.length) {
|
|
527
|
+
await this.repair(r, v.decision === 'reject'
|
|
528
|
+
? v.reason
|
|
529
|
+
: candidate.revision !== revision
|
|
530
|
+
? 'Source changed during verification. Submit the current candidate.'
|
|
531
|
+
: 'Address user guidance before resubmitting: ' +
|
|
532
|
+
[...messages, ...r.messages].join('\n'));
|
|
533
|
+
continue;
|
|
534
|
+
}
|
|
535
|
+
const accepted = {
|
|
536
|
+
name: task.name,
|
|
537
|
+
result: candidate.summary,
|
|
538
|
+
revision,
|
|
539
|
+
worker: r.worker,
|
|
540
|
+
verifier: session,
|
|
541
|
+
verdict: {
|
|
542
|
+
decision: v.decision,
|
|
543
|
+
reason: v.reason,
|
|
544
|
+
...(v.checks ? { checks: v.checks } : {}),
|
|
545
|
+
},
|
|
546
|
+
};
|
|
547
|
+
const patch = join(this.dir(r), revision + '.patch'), tmp = patch + '.tmp', fd = openSync(tmp, 'w', 0o600);
|
|
548
|
+
try {
|
|
549
|
+
try {
|
|
550
|
+
writePatch(r.plan.repository, this.dir(r), r.baseline, revision, fd);
|
|
551
|
+
}
|
|
552
|
+
finally {
|
|
553
|
+
closeSync(fd);
|
|
554
|
+
}
|
|
555
|
+
renameSync(tmp, patch);
|
|
556
|
+
}
|
|
557
|
+
finally {
|
|
558
|
+
rmSync(tmp, { force: true });
|
|
559
|
+
}
|
|
560
|
+
r.accepted.push(accepted);
|
|
561
|
+
r.guidance = [];
|
|
562
|
+
r.index++;
|
|
563
|
+
r.phase = 'worker';
|
|
564
|
+
r.feedback = '';
|
|
565
|
+
r.repairsUsed = 0;
|
|
566
|
+
r.generation = 0;
|
|
567
|
+
delete r.worker;
|
|
568
|
+
delete r.verifier;
|
|
569
|
+
delete r.candidate;
|
|
570
|
+
if (r.index === r.plan.tasks.length)
|
|
571
|
+
r.status = 'completed';
|
|
572
|
+
this.save(r, 'task_accepted', accepted);
|
|
573
|
+
await this.dispose(r);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
catch (error) {
|
|
577
|
+
if (this.closing || terminal(r.status) || r.status === 'pausing' || r.status === 'paused')
|
|
578
|
+
return;
|
|
579
|
+
this.restart(r, diagnostic(error));
|
|
580
|
+
await this.dispose(r);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
async repair(r, reason) {
|
|
584
|
+
if (r.repairsUsed >= r.plan.repairs) {
|
|
585
|
+
this.restart(r, reason);
|
|
586
|
+
await this.dispose(r);
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
r.repairsUsed++;
|
|
590
|
+
r.attempts.push(reason);
|
|
591
|
+
r.feedback = reason;
|
|
592
|
+
r.phase = 'worker';
|
|
593
|
+
delete r.verifier;
|
|
594
|
+
this.save(r, 'repair_requested', { worker: r.worker, reason });
|
|
595
|
+
}
|
|
596
|
+
}
|
package/dist/src/main.js
CHANGED
|
@@ -1,25 +1,75 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
try {
|
|
14
|
-
await deployment.listen();
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
3
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
4
|
+
import { CallToolRequestSchema, ListToolsRequestSchema, } from '@modelcontextprotocol/sdk/types.js';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import { operations, descriptions, snapshotOperations, workflowSnapshot } from './contract.js';
|
|
7
|
+
import { call, ensure, serve } from './service.js';
|
|
8
|
+
const cmd = process.argv[2] ?? 'mcp';
|
|
9
|
+
async function main() {
|
|
10
|
+
if (cmd === 'engine') {
|
|
11
|
+
await serve();
|
|
12
|
+
return;
|
|
15
13
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
await deployment.close();
|
|
20
|
-
throw error;
|
|
14
|
+
if (cmd === 'status' || cmd === 'stop') {
|
|
15
|
+
console.log(await call(cmd === 'status' ? 'health' : 'stop'));
|
|
16
|
+
return;
|
|
21
17
|
}
|
|
18
|
+
if (cmd === 'mcp-config') {
|
|
19
|
+
console.log(JSON.stringify({
|
|
20
|
+
mcpServers: {
|
|
21
|
+
transmission: {
|
|
22
|
+
command: process.execPath,
|
|
23
|
+
args: [process.argv[1], 'mcp'],
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
}, null, 2));
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (cmd !== 'mcp') {
|
|
30
|
+
throw new Error('Usage: code60 [mcp | mcp-config | status | stop]');
|
|
31
|
+
}
|
|
32
|
+
await ensure();
|
|
33
|
+
const server = new Server({ name: 'transmission', version: '1' }, {
|
|
34
|
+
capabilities: { tools: {} },
|
|
35
|
+
instructions: 'Design ordered tasks with independent verification criteria from the user’s goal. Present the workflow, obtain confirmation, then submit it once with start_workflow. Follow the same run. Ordinary repairs and retries run autonomously.',
|
|
36
|
+
});
|
|
37
|
+
const outputSchema = z.toJSONSchema(workflowSnapshot);
|
|
38
|
+
server.setRequestHandler(ListToolsRequestSchema, () => ({
|
|
39
|
+
tools: Object.entries(operations).map(([name, schema]) => ({
|
|
40
|
+
name,
|
|
41
|
+
...(snapshotOperations.has(name) ? { outputSchema } : {}),
|
|
42
|
+
description: descriptions[name],
|
|
43
|
+
inputSchema: z.toJSONSchema(schema, { io: 'input' }),
|
|
44
|
+
})),
|
|
45
|
+
}));
|
|
46
|
+
server.setRequestHandler(CallToolRequestSchema, async ({ params }) => {
|
|
47
|
+
try {
|
|
48
|
+
if (!Object.hasOwn(operations, params.name))
|
|
49
|
+
throw new Error('Unknown workflow tool.');
|
|
50
|
+
const data = await call(params.name, params.arguments ?? {});
|
|
51
|
+
return {
|
|
52
|
+
structuredContent: data,
|
|
53
|
+
content: [{ type: 'text', text: JSON.stringify(data) }],
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
return {
|
|
58
|
+
isError: true,
|
|
59
|
+
content: [
|
|
60
|
+
{
|
|
61
|
+
type: 'text',
|
|
62
|
+
text: error instanceof Error
|
|
63
|
+
? error.message
|
|
64
|
+
: 'Workflow request failed.',
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
await server.connect(new StdioServerTransport());
|
|
22
71
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
72
|
+
main().catch((error) => {
|
|
73
|
+
console.error(error.message);
|
|
74
|
+
process.exitCode = 1;
|
|
75
|
+
});
|