@ulm0/gonogo 0.0.0-stage → 0.1.2
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/LICENSE +21 -0
- package/README.md +24 -2
- package/bin/gonogo.js +18 -0
- package/dist/client/THIRD-PARTY-NOTICES.md +409 -0
- package/dist/client/apple-touch-icon.png +0 -0
- package/dist/client/assets/doto-latin-900-normal-D1bF_x6I.woff +0 -0
- package/dist/client/assets/geist-latin-400-normal-B40WzpMT.woff2 +0 -0
- package/dist/client/assets/geist-latin-400-normal-akEymXtG.woff +0 -0
- package/dist/client/assets/geist-latin-500-normal-BDXIbFrL.woff +0 -0
- package/dist/client/assets/geist-latin-500-normal-CTWBw9NS.woff2 +0 -0
- package/dist/client/assets/geist-latin-600-normal-CSETrqM2.woff2 +0 -0
- package/dist/client/assets/geist-latin-600-normal-DsPlZH-9.woff +0 -0
- package/dist/client/assets/geist-mono-latin-400-normal-B2yvC1Cq.woff +0 -0
- package/dist/client/assets/geist-mono-latin-400-normal-DTRLJnHl.woff2 +0 -0
- package/dist/client/assets/geist-mono-latin-500-normal-D-GG86Jb.woff +0 -0
- package/dist/client/assets/geist-mono-latin-500-normal-YINYabwD.woff2 +0 -0
- package/dist/client/assets/geist-mono-latin-600-normal-V-KvD_pi.woff2 +0 -0
- package/dist/client/assets/geist-mono-latin-600-normal-bZn07FKM.woff +0 -0
- package/dist/client/assets/index-BqCn1SM7.js +50 -0
- package/dist/client/assets/index-KZtFVMJG.css +1 -0
- package/dist/client/favicon.ico +0 -0
- package/dist/client/favicon.svg +1 -0
- package/dist/client/index.html +19 -0
- package/dist/server/audited.js +133 -0
- package/dist/server/blackbox.js +261 -0
- package/dist/server/board.js +123 -0
- package/dist/server/cleanroom.js +214 -0
- package/dist/server/clearance.js +158 -0
- package/dist/server/cli.js +763 -0
- package/dist/server/command.js +44 -0
- package/dist/server/constants.js +465 -0
- package/dist/server/docking.js +541 -0
- package/dist/server/downlink.js +191 -0
- package/dist/server/hangar.js +311 -0
- package/dist/server/home.js +520 -0
- package/dist/server/hooks.js +166 -0
- package/dist/server/hub.js +2642 -0
- package/dist/server/launcher.js +547 -0
- package/dist/server/livery.js +210 -0
- package/dist/server/logchart.js +58 -0
- package/dist/server/logsearch.js +112 -0
- package/dist/server/massbudget.js +169 -0
- package/dist/server/mcp.js +228 -0
- package/dist/server/migrate.js +122 -0
- package/dist/server/missionlog.js +211 -0
- package/dist/server/payload.js +168 -0
- package/dist/server/propellant.js +314 -0
- package/dist/server/providers.js +199 -0
- package/dist/server/recorder.js +176 -0
- package/dist/server/relay.js +300 -0
- package/dist/server/relaykinds.js +182 -0
- package/dist/server/relief.js +226 -0
- package/dist/server/rollout.js +172 -0
- package/dist/server/schedules.js +277 -0
- package/dist/server/tasks.js +272 -0
- package/dist/server/telemetry.js +110 -0
- package/dist/server/terminal.js +635 -0
- package/dist/server/uplinks.js +356 -0
- package/dist/server/web.js +1607 -0
- package/dist/shared/cron.js +149 -0
- package/dist/shared/fuel.js +18 -0
- package/dist/shared/livery.js +348 -0
- package/dist/shared/positions.js +132 -0
- package/dist/shared/protocol.js +245 -0
- package/package.json +84 -3
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
2
|
+
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { LIVERY_DESCRIPTION_MAX_CHARS, LIVERY_NAME_MAX_CHARS } from '../shared/livery.js';
|
|
5
|
+
import { AGENT_NAME_MAX, DIR_MAX_CHARS, BOARD_MAX_CHARS, LOG_BODY_MAX_CHARS, LOG_QUERY_MAX_CHARS, LOG_SEARCH_MAX, LOG_TAGS_MAX, LOG_TITLE_MAX_CHARS, MCP_SERVER_NAME, POLL_VOTES, ROLE_MAX_CHARS, TASK_DETAIL_MAX_CHARS, TASK_NOTE_MAX_CHARS, TASK_STATUSES, TASK_TITLE_MAX_CHARS, } from '../shared/protocol.js';
|
|
6
|
+
import { BODY_MAX_CHARS, MAX_MCP_BODY_BYTES, MCP_DESCRIPTIONS, POLL_REASON_MAX_CHARS, READ_MAX_CHARS, SUBJECT_MAX_CHARS } from './constants.js';
|
|
7
|
+
import { bearerToken, hubErrorInfo, readBody, sendJson, sendUnauthorized } from './hooks.js';
|
|
8
|
+
/** JSON-RPC error codes: parse error, invalid request, "server error" (the SDK's own value for a refused method), internal error. */
|
|
9
|
+
const RPC_PARSE_ERROR = -32700;
|
|
10
|
+
const RPC_INVALID_REQUEST = -32600;
|
|
11
|
+
const RPC_METHOD_NOT_ALLOWED = -32000;
|
|
12
|
+
const RPC_INTERNAL_ERROR = -32603;
|
|
13
|
+
function rpcError(code, message) {
|
|
14
|
+
return { jsonrpc: '2.0', error: { code, message }, id: null };
|
|
15
|
+
}
|
|
16
|
+
function toolResult(object, isError) {
|
|
17
|
+
const result = { content: [{ type: 'text', text: JSON.stringify(object) }] };
|
|
18
|
+
if (isError)
|
|
19
|
+
result.isError = true;
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
export function createMcpHandler(deps) {
|
|
23
|
+
const { hub, log } = deps;
|
|
24
|
+
const logClose = (e) => log.debug('mcp close failed', e);
|
|
25
|
+
/** A HubError tells the agent why; anything else is a bug, logged here and reported without its text. */
|
|
26
|
+
const guarded = async (what, run) => {
|
|
27
|
+
try {
|
|
28
|
+
return await run();
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
const info = hubErrorInfo(e);
|
|
32
|
+
if (info)
|
|
33
|
+
return toolResult({ ok: false, error: info.message }, true);
|
|
34
|
+
log.error(`mcp tool ${what} failed`, e);
|
|
35
|
+
return toolResult({ ok: false, error: 'internal error' }, true);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
// The identity is captured here, from the token: no tool argument can name a sender.
|
|
39
|
+
const buildServer = ({ agent, generation }) => {
|
|
40
|
+
const server = new McpServer({ name: MCP_SERVER_NAME, version: deps.version?.() ?? '0.0.0' });
|
|
41
|
+
server.registerTool('send_message', {
|
|
42
|
+
description: MCP_DESCRIPTIONS.send_message,
|
|
43
|
+
inputSchema: {
|
|
44
|
+
to: z.string().min(1).max(64),
|
|
45
|
+
subject: z.string().max(SUBJECT_MAX_CHARS).optional(),
|
|
46
|
+
body: z.string().min(1).max(BODY_MAX_CHARS),
|
|
47
|
+
in_reply_to: z.string().max(40).optional(),
|
|
48
|
+
},
|
|
49
|
+
}, (args) => guarded('send_message', () => {
|
|
50
|
+
const request = { to: args.to, subject: args.subject ?? '', body: args.body };
|
|
51
|
+
if (args.in_reply_to !== undefined)
|
|
52
|
+
request.inReplyTo = args.in_reply_to;
|
|
53
|
+
const r = hub.sendFromAgent(agent, generation, request);
|
|
54
|
+
return toolResult({ ok: r.ok, id: r.id, to: r.to, status: r.status, note: r.note }, !r.ok);
|
|
55
|
+
}));
|
|
56
|
+
server.registerTool('list_roster', { description: MCP_DESCRIPTIONS.list_roster }, () => guarded('list_roster', () => toolResult(hub.rosterFor(agent, generation), false)));
|
|
57
|
+
server.registerTool('read_messages', { description: MCP_DESCRIPTIONS.read_messages }, () => guarded('read_messages', () => toolResult(hub.readMessages(agent, generation), false)));
|
|
58
|
+
server.registerTool('go_nogo', {
|
|
59
|
+
description: MCP_DESCRIPTIONS.go_nogo,
|
|
60
|
+
inputSchema: { vote: z.enum(POLL_VOTES), reason: z.string().max(POLL_REASON_MAX_CHARS).optional() },
|
|
61
|
+
}, (args) => guarded('go_nogo', () => {
|
|
62
|
+
const r = hub.vote(agent, generation, args.vote, args.reason ?? '');
|
|
63
|
+
return toolResult({ ok: r.ok, note: r.note }, !r.ok);
|
|
64
|
+
}));
|
|
65
|
+
const { tasks } = deps;
|
|
66
|
+
const noBoard = () => toolResult({ ok: false, error: 'the Flight Plan is not available' }, true);
|
|
67
|
+
const actor = { kind: 'agent', name: agent, generation };
|
|
68
|
+
server.registerTool('task_list', {
|
|
69
|
+
description: MCP_DESCRIPTIONS.task_list,
|
|
70
|
+
inputSchema: { mine: z.boolean().optional(), status: z.enum(TASK_STATUSES).optional() },
|
|
71
|
+
}, (args) => guarded('task_list', () => {
|
|
72
|
+
if (!tasks)
|
|
73
|
+
return noBoard();
|
|
74
|
+
const found = tasks
|
|
75
|
+
.list()
|
|
76
|
+
.filter((t) => (args.mine !== true || t.assignee === agent) && (args.status === undefined || t.status === args.status))
|
|
77
|
+
.sort((a, b) => b.updatedAt - a.updatedAt);
|
|
78
|
+
// the same cap as read_messages: Claude Code refuses a tool result of more than 25,000 tokens; the first task always goes
|
|
79
|
+
const shown = [];
|
|
80
|
+
let chars = 0;
|
|
81
|
+
for (const t of found) {
|
|
82
|
+
chars += JSON.stringify(t).length;
|
|
83
|
+
if (shown.length > 0 && chars > READ_MAX_CHARS)
|
|
84
|
+
break;
|
|
85
|
+
shown.push(t);
|
|
86
|
+
}
|
|
87
|
+
return toolResult(found.length === shown.length ? { count: shown.length, tasks: shown } : { count: shown.length, tasks: shown, remaining: found.length - shown.length, note: 'filter by status or mine to see the rest' }, false);
|
|
88
|
+
}));
|
|
89
|
+
server.registerTool('task_create', {
|
|
90
|
+
description: MCP_DESCRIPTIONS.task_create,
|
|
91
|
+
inputSchema: { title: z.string().min(1).max(TASK_TITLE_MAX_CHARS), detail: z.string().max(TASK_DETAIL_MAX_CHARS).optional(), assignee: z.string().min(1).max(64).optional() },
|
|
92
|
+
}, (args) => guarded('task_create', async () => (tasks ? toolResult({ ok: true, task: await tasks.create(actor, args) }, false) : noBoard())));
|
|
93
|
+
server.registerTool('task_update', {
|
|
94
|
+
description: MCP_DESCRIPTIONS.task_update,
|
|
95
|
+
inputSchema: {
|
|
96
|
+
id: z.string().min(1).max(16),
|
|
97
|
+
status: z.enum(TASK_STATUSES).optional(),
|
|
98
|
+
assignee: z.string().min(1).max(64).nullable().optional(),
|
|
99
|
+
note: z.string().max(TASK_NOTE_MAX_CHARS).optional(),
|
|
100
|
+
},
|
|
101
|
+
}, ({ id, ...patch }) => guarded('task_update', async () => (tasks ? toolResult({ ok: true, task: await tasks.update(actor, id, patch) }, false) : noBoard())));
|
|
102
|
+
const { missionLog } = deps;
|
|
103
|
+
const noLog = () => toolResult({ ok: false, error: 'the Mission Log is not available' }, true);
|
|
104
|
+
server.registerTool('log_write', {
|
|
105
|
+
description: MCP_DESCRIPTIONS.log_write,
|
|
106
|
+
inputSchema: {
|
|
107
|
+
id: z.string().min(1).max(16).optional(),
|
|
108
|
+
title: z.string().min(1).max(LOG_TITLE_MAX_CHARS).optional(),
|
|
109
|
+
body: z.string().min(1).max(LOG_BODY_MAX_CHARS).optional(),
|
|
110
|
+
tags: z.array(z.string().min(1).max(64)).max(LOG_TAGS_MAX).optional(),
|
|
111
|
+
},
|
|
112
|
+
}, ({ id, ...fields }) => guarded('log_write', async () => {
|
|
113
|
+
if (!missionLog)
|
|
114
|
+
return noLog();
|
|
115
|
+
// the author is the caller's own name, never a field of the call
|
|
116
|
+
const entry = await (id === undefined ? missionLog.create(agent, fields) : missionLog.update(id, fields, agent));
|
|
117
|
+
return toolResult({ ok: true, id: entry.id, title: entry.title, tags: entry.tags }, false);
|
|
118
|
+
}));
|
|
119
|
+
server.registerTool('log_search', {
|
|
120
|
+
description: MCP_DESCRIPTIONS.log_search,
|
|
121
|
+
inputSchema: { query: z.string().min(1).max(LOG_QUERY_MAX_CHARS), limit: z.number().int().min(1).max(LOG_SEARCH_MAX).optional() },
|
|
122
|
+
}, ({ query, limit }) => guarded('log_search', () => {
|
|
123
|
+
if (!missionLog)
|
|
124
|
+
return noLog();
|
|
125
|
+
const { hits } = missionLog.search(query, limit ?? LOG_SEARCH_MAX);
|
|
126
|
+
return toolResult({ count: hits.length, results: hits }, false);
|
|
127
|
+
}));
|
|
128
|
+
server.registerTool('log_read', { description: MCP_DESCRIPTIONS.log_read, inputSchema: { id: z.string().min(1).max(16) } }, ({ id }) => guarded('log_read', () => (missionLog ? toolResult({ ok: true, entry: missionLog.get(id) }, false) : noLog())));
|
|
129
|
+
const { board } = deps;
|
|
130
|
+
const noPlot = () => toolResult({ ok: false, error: 'the Plot Board is not available' }, true);
|
|
131
|
+
// the boss is the one the hub says it is, asked at each call: a rename or a new boss takes effect at once
|
|
132
|
+
const bossName = () => hub.rosterFor(agent, generation).boss;
|
|
133
|
+
server.registerTool('board_read', { description: MCP_DESCRIPTIONS.board_read }, () => guarded('board_read', () => (board ? toolResult({ ok: true, ...board.get(), canWrite: bossName() === agent }, false) : noPlot())));
|
|
134
|
+
server.registerTool('board_write', { description: MCP_DESCRIPTIONS.board_write, inputSchema: { text: z.string().max(BOARD_MAX_CHARS), revision: z.number().int().min(0) } }, (args) => guarded('board_write', async () => {
|
|
135
|
+
if (!board)
|
|
136
|
+
return noPlot();
|
|
137
|
+
if (bossName() !== agent)
|
|
138
|
+
return toolResult({ ok: false, error: 'only the boss may write the Plot Board' }, true);
|
|
139
|
+
// the author is the caller's own name, never a field of the call
|
|
140
|
+
const view = await board.write(agent, args);
|
|
141
|
+
return toolResult({ ok: true, revision: view.revision, updatedAt: view.updatedAt }, false);
|
|
142
|
+
}));
|
|
143
|
+
const { liveries } = deps;
|
|
144
|
+
server.registerTool('livery_submit', {
|
|
145
|
+
description: MCP_DESCRIPTIONS.livery_submit,
|
|
146
|
+
inputSchema: { name: z.string().min(1).max(LIVERY_NAME_MAX_CHARS), description: z.string().max(LIVERY_DESCRIPTION_MAX_CHARS).optional(), tokens: z.record(z.string().max(64), z.string().max(64)) },
|
|
147
|
+
}, (args) => guarded('livery_submit', async () => {
|
|
148
|
+
if (!liveries)
|
|
149
|
+
return toolResult({ ok: false, error: 'Livery is not available' }, true);
|
|
150
|
+
// the author is the caller's own name, never a field of the call
|
|
151
|
+
const entry = await liveries.submit(agent, args);
|
|
152
|
+
return toolResult({ ok: true, slug: entry.slug, note: 'saved; the Director chooses whether to apply it' }, false);
|
|
153
|
+
}));
|
|
154
|
+
const { relief } = deps;
|
|
155
|
+
server.registerTool('hire_agent', {
|
|
156
|
+
description: MCP_DESCRIPTIONS.hire_agent,
|
|
157
|
+
// strict: a field the tool does not have (confirmBypass, command, payload) is refused, not dropped; the mode is checked by Relief, so that its answer says why
|
|
158
|
+
inputSchema: z.strictObject({
|
|
159
|
+
name: z.string().min(1).max(AGENT_NAME_MAX),
|
|
160
|
+
position: z.string().max(32).optional(),
|
|
161
|
+
role: z.string().max(ROLE_MAX_CHARS).optional(),
|
|
162
|
+
dir: z.string().max(DIR_MAX_CHARS).optional(),
|
|
163
|
+
engine: z.string().max(32).optional(),
|
|
164
|
+
mode: z.string().max(32).optional(),
|
|
165
|
+
}),
|
|
166
|
+
}, (args) => guarded('hire_agent', async () => {
|
|
167
|
+
if (!relief)
|
|
168
|
+
return toolResult({ ok: false, error: 'Relief is not available' }, true);
|
|
169
|
+
// the requester is the agent of the token, never a field of the call
|
|
170
|
+
return toolResult({ ok: true, ...(await relief.request({ agent, generation }, args)) }, false);
|
|
171
|
+
}));
|
|
172
|
+
return server;
|
|
173
|
+
};
|
|
174
|
+
return async (req, res) => {
|
|
175
|
+
const token = bearerToken(req.headers.authorization);
|
|
176
|
+
const identity = token === null ? null : deps.resolveToken(token);
|
|
177
|
+
if (!identity) {
|
|
178
|
+
sendUnauthorized(res);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
// stateless: there is no session to resume (GET) or end (DELETE); claude opens one GET, gets this, and carries on
|
|
182
|
+
if (req.method !== 'POST') {
|
|
183
|
+
sendJson(res, 405, rpcError(RPC_METHOD_NOT_ALLOWED, 'Method not allowed.'), { Allow: 'POST' });
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
try {
|
|
187
|
+
const body = await readBody(req, MAX_MCP_BODY_BYTES);
|
|
188
|
+
if (!body.ok) {
|
|
189
|
+
if (body.reason === 'too_large')
|
|
190
|
+
sendJson(res, 413, rpcError(RPC_INVALID_REQUEST, 'Request body too large.'));
|
|
191
|
+
else
|
|
192
|
+
res.destroy();
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
let parsed;
|
|
196
|
+
try {
|
|
197
|
+
parsed = JSON.parse(body.body.toString('utf8'));
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
sendJson(res, 400, rpcError(RPC_PARSE_ERROR, 'Parse error: the body is not valid JSON.'));
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
// The SDK writes its own Cache-Control for the event stream, over the one set here; contract 3.2 wants no-store on every MCP answer.
|
|
204
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
205
|
+
const setHeader = res.setHeader.bind(res);
|
|
206
|
+
res.setHeader = (name, value) => (name.toLowerCase() === 'cache-control' ? res : setHeader(name, value));
|
|
207
|
+
const server = buildServer(identity);
|
|
208
|
+
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined });
|
|
209
|
+
const closeBoth = () => {
|
|
210
|
+
void transport.close().catch(logClose);
|
|
211
|
+
void server.close().catch(logClose);
|
|
212
|
+
};
|
|
213
|
+
if (res.destroyed)
|
|
214
|
+
closeBoth();
|
|
215
|
+
else
|
|
216
|
+
res.on('close', closeBoth);
|
|
217
|
+
await server.connect(transport);
|
|
218
|
+
await transport.handleRequest(req, res, parsed);
|
|
219
|
+
}
|
|
220
|
+
catch (e) {
|
|
221
|
+
log.error('mcp request failed', e);
|
|
222
|
+
if (res.headersSent)
|
|
223
|
+
res.destroy();
|
|
224
|
+
else
|
|
225
|
+
sendJson(res, 500, rpcError(RPC_INTERNAL_ERROR, 'Internal error.'));
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Versions of the saved state (docs/release-readiness-spec.md, docs/reference/configuration.md "Versions of the saved state").
|
|
3
|
+
*
|
|
4
|
+
* Every JSON file under the home that has a format holds `"version": <n>`. `migrateHome` runs at start, under the home's lock and
|
|
5
|
+
* before any store reads its file: a file of an older version is upgraded one version at a time, after a copy of it was made as
|
|
6
|
+
* `<file>.bak-v<old>`; a file of a newer version stops the start with a message, because the older code would read it wrongly or
|
|
7
|
+
* overwrite it. A file that is missing, is not JSON or has no version is left to the store that owns it: it reports it its own way.
|
|
8
|
+
* The exception is a format marked `unversionedIs1` (propellant.json), which gonogo wrote without a version before: that is version 1.
|
|
9
|
+
*
|
|
10
|
+
* To change a format: add a step at the end of `steps` of its entry in FORMATS (version n to n + 1; it gets the parsed file and
|
|
11
|
+
* returns the new one; the runner sets `version`), and change the store to read and write the new shape.
|
|
12
|
+
*
|
|
13
|
+
* Not run through here: the logs messages.jsonl, fuel.jsonl and the day files of the Black Box (a line a reader cannot parse is
|
|
14
|
+
* skipped; a new line format needs a new file name), and the Mission Log entries and board.md (their front matter has `version`,
|
|
15
|
+
* checked in missionlog.ts and board.ts).
|
|
16
|
+
* shortcut: a home with one file too new and another to upgrade can upgrade the second before it refuses the first; the copy
|
|
17
|
+
* makes that harmless, so there is no check pass.
|
|
18
|
+
*/
|
|
19
|
+
import { randomBytes } from 'node:crypto';
|
|
20
|
+
import { chmodSync, constants, copyFileSync, readFileSync, readdirSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
21
|
+
import { basename, dirname, join } from 'node:path';
|
|
22
|
+
import { AGENTS_FILE, DOCKING_FILE, DOWNLINK_FILE, FILE_MODE, KEYS_FILE, LIVERY_DIR, LIVERY_FILE, PROPELLANT_FILE, RELAY_FILE, ROLLOUT_FILE, SCHEDULES_FILE, STATION_FILE, TASKS_FILE, UPLINKS_FILE, } from './constants.js';
|
|
23
|
+
const ALL_JSON = '/*.json';
|
|
24
|
+
/** Everything is still at version 1: no step yet. */
|
|
25
|
+
export const FORMATS = [
|
|
26
|
+
AGENTS_FILE,
|
|
27
|
+
SCHEDULES_FILE,
|
|
28
|
+
UPLINKS_FILE,
|
|
29
|
+
TASKS_FILE,
|
|
30
|
+
DOWNLINK_FILE,
|
|
31
|
+
RELAY_FILE,
|
|
32
|
+
DOCKING_FILE,
|
|
33
|
+
STATION_FILE,
|
|
34
|
+
LIVERY_FILE,
|
|
35
|
+
PROPELLANT_FILE,
|
|
36
|
+
KEYS_FILE,
|
|
37
|
+
ROLLOUT_FILE,
|
|
38
|
+
LIVERY_DIR + ALL_JSON,
|
|
39
|
+
].map((file) => (file === PROPELLANT_FILE ? { file, steps: [], unversionedIs1: true } : { file, steps: [] }));
|
|
40
|
+
function isRecord(x) {
|
|
41
|
+
return typeof x === 'object' && x !== null && !Array.isArray(x);
|
|
42
|
+
}
|
|
43
|
+
function errMessage(e) {
|
|
44
|
+
return e instanceof Error ? e.message : String(e);
|
|
45
|
+
}
|
|
46
|
+
function filesOf(home, file) {
|
|
47
|
+
if (!file.endsWith(ALL_JSON))
|
|
48
|
+
return [join(home, file)];
|
|
49
|
+
const folder = join(home, file.slice(0, -ALL_JSON.length));
|
|
50
|
+
try {
|
|
51
|
+
return readdirSync(folder)
|
|
52
|
+
.filter((name) => name.endsWith('.json'))
|
|
53
|
+
.sort()
|
|
54
|
+
.map((name) => join(folder, name));
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// no folder yet, or one the store will complain about
|
|
58
|
+
return [];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function migrateFile(path, { steps, unversionedIs1 }, log) {
|
|
62
|
+
let data;
|
|
63
|
+
try {
|
|
64
|
+
data = JSON.parse(readFileSync(path, 'utf8'));
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (!isRecord(data))
|
|
70
|
+
return;
|
|
71
|
+
const old = data.version === undefined && unversionedIs1 ? 1 : data.version;
|
|
72
|
+
if (typeof old !== 'number' || !Number.isInteger(old) || old < 1)
|
|
73
|
+
return;
|
|
74
|
+
const known = steps.length + 1;
|
|
75
|
+
if (old > known) {
|
|
76
|
+
throw new Error(`${path} was saved by a newer gonogo (format version ${old}; this one reads up to ${known}). Start the gonogo that saved it, or move the file away to start without it.`);
|
|
77
|
+
}
|
|
78
|
+
if (old === known)
|
|
79
|
+
return;
|
|
80
|
+
const backup = `${path}.bak-v${old}`;
|
|
81
|
+
try {
|
|
82
|
+
// an existing copy is the one made by an earlier try that failed after the copy: the file has not changed since
|
|
83
|
+
copyFileSync(path, backup, constants.COPYFILE_EXCL);
|
|
84
|
+
chmodSync(backup, FILE_MODE);
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
if (e.code !== 'EEXIST')
|
|
88
|
+
throw new Error(`cannot copy ${path} to ${backup}: ${errMessage(e)}; nothing was changed`);
|
|
89
|
+
}
|
|
90
|
+
let next = data;
|
|
91
|
+
let version = old;
|
|
92
|
+
for (const step of steps.slice(old - 1)) {
|
|
93
|
+
try {
|
|
94
|
+
next = { ...step(next), version: version + 1 };
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
throw new Error(`${path}: the step from version ${version} to ${version + 1} failed (${errMessage(e)}); the file was not changed, and a copy is ${backup}`);
|
|
98
|
+
}
|
|
99
|
+
version += 1;
|
|
100
|
+
}
|
|
101
|
+
const tmp = join(dirname(path), `.${basename(path)}.${process.pid}.${randomBytes(4).toString('hex')}.tmp`);
|
|
102
|
+
try {
|
|
103
|
+
writeFileSync(tmp, JSON.stringify(next, null, 2), { mode: FILE_MODE, flag: 'wx' });
|
|
104
|
+
renameSync(tmp, path);
|
|
105
|
+
}
|
|
106
|
+
catch (e) {
|
|
107
|
+
try {
|
|
108
|
+
unlinkSync(tmp);
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
// never written
|
|
112
|
+
}
|
|
113
|
+
throw new Error(`cannot write ${path}: ${errMessage(e)}; the file was not changed, and a copy is ${backup}`);
|
|
114
|
+
}
|
|
115
|
+
log.info(`${path} was upgraded from format version ${old} to ${known}; the old file is kept as ${backup}`);
|
|
116
|
+
}
|
|
117
|
+
/** Throws an Error whose message names the file; the caller lets the start fail with it. */
|
|
118
|
+
export function migrateHome(home, log, formats = FORMATS) {
|
|
119
|
+
for (const format of formats)
|
|
120
|
+
for (const path of filesOf(home, format.file))
|
|
121
|
+
migrateFile(path, format, log);
|
|
122
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mission Log: one memory shared by every agent, searchable (docs/mission-log-spec.md).
|
|
3
|
+
*
|
|
4
|
+
* One markdown file per entry, `<home>/mission-log/<id>.md`: a small front matter (id, title, tags, author, createdAt, updatedAt,
|
|
5
|
+
* each value written as JSON on one line) and then the body. The Director (the browser routes) and every agent (the MCP tools
|
|
6
|
+
* log_write, log_search, log_read) use the same store. The entries are held in memory and indexed for BM25 search (logsearch.ts) at
|
|
7
|
+
* start and at every write; a write goes to the file first, so a refusal of the disk reaches the caller and nothing changes in memory.
|
|
8
|
+
* The author is stamped by the server from the agent's token. An agent may change only what it wrote; the Director may change or
|
|
9
|
+
* remove any entry. The clock is injected for tests.
|
|
10
|
+
*
|
|
11
|
+
* shortcut: every entry is held in memory (at the limits, 2000 entries of 20,000 characters is about 40 MB) and the list of the page
|
|
12
|
+
* shows the newest LOG_SEARCH_MAX; page it or read the files on demand if a log ever gets that big.
|
|
13
|
+
* shortcut: `author` is an agent name, so a renamed agent can no longer change what it wrote (the Director still can), and a new
|
|
14
|
+
* agent hired under the name of a removed one may change what the old one wrote; stamp a per-hire id if that ever matters.
|
|
15
|
+
*/
|
|
16
|
+
import { randomBytes } from 'node:crypto';
|
|
17
|
+
import { mkdir, readdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
18
|
+
import { join } from 'node:path';
|
|
19
|
+
import { LOG_BODY_MAX_CHARS, LOG_ENTRIES_MAX, LOG_QUERY_MAX_CHARS, LOG_SEARCH_MAX, LOG_TAG_PATTERN, LOG_TAGS_MAX, LOG_TITLE_MAX_CHARS } from '../shared/protocol.js';
|
|
20
|
+
import { DIR_MODE, FILE_MODE, LOG_DIR } from './constants.js';
|
|
21
|
+
import { isRecord } from './hooks.js';
|
|
22
|
+
import { HubError } from './hub.js';
|
|
23
|
+
import { buildChart } from './logchart.js';
|
|
24
|
+
import { createIndex, snippet } from './logsearch.js';
|
|
25
|
+
import { UNSAFE_IN_NAME } from './schedules.js';
|
|
26
|
+
const ID_RE = /^l[1-9]\d{0,5}$/;
|
|
27
|
+
const FILE_RE = /^(l[1-9]\d{0,5})\.md$/;
|
|
28
|
+
const TAG_RE = new RegExp(LOG_TAG_PATTERN);
|
|
29
|
+
const FIELDS = ['title', 'body', 'tags'];
|
|
30
|
+
/** Holds the highest id ever given, so that the id of a removed entry is not given again after a restart. */
|
|
31
|
+
const SEQ_FILE = '.seq';
|
|
32
|
+
const bad = (message, field) => new HubError('bad_request', message, field);
|
|
33
|
+
const copy = (entry) => ({ ...entry, tags: [...entry.tags] });
|
|
34
|
+
function validate(input, whole) {
|
|
35
|
+
if (!isRecord(input))
|
|
36
|
+
throw bad('request body must be a JSON object');
|
|
37
|
+
for (const key of Object.keys(input))
|
|
38
|
+
if (!FIELDS.includes(key))
|
|
39
|
+
throw bad(`unknown field "${key}"`, key);
|
|
40
|
+
if (whole && (input.title === undefined || input.body === undefined))
|
|
41
|
+
throw bad('title and body are required', input.title === undefined ? 'title' : 'body');
|
|
42
|
+
if (!whole && Object.keys(input).length === 0)
|
|
43
|
+
throw bad('nothing to change: give a title, a body or tags');
|
|
44
|
+
const out = {};
|
|
45
|
+
if (input.title !== undefined) {
|
|
46
|
+
const title = typeof input.title === 'string' ? input.title.trim() : '';
|
|
47
|
+
if (title === '' || title.length > LOG_TITLE_MAX_CHARS || UNSAFE_IN_NAME.test(title))
|
|
48
|
+
throw bad(`title must be 1 to ${LOG_TITLE_MAX_CHARS} characters on one line`, 'title');
|
|
49
|
+
out.title = title;
|
|
50
|
+
}
|
|
51
|
+
if (input.body !== undefined) {
|
|
52
|
+
const body = typeof input.body === 'string' ? input.body.trim() : '';
|
|
53
|
+
if (body === '' || body.length > LOG_BODY_MAX_CHARS)
|
|
54
|
+
throw bad(`body must be 1 to ${LOG_BODY_MAX_CHARS} characters`, 'body');
|
|
55
|
+
out.body = body;
|
|
56
|
+
}
|
|
57
|
+
if (input.tags !== undefined) {
|
|
58
|
+
const tags = Array.isArray(input.tags) ? input.tags.map((t) => (typeof t === 'string' ? t.trim().toLowerCase() : '')) : [];
|
|
59
|
+
if (!Array.isArray(input.tags) || tags.length > LOG_TAGS_MAX || !tags.every((t) => TAG_RE.test(t))) {
|
|
60
|
+
throw bad(`tags must be a list of at most ${LOG_TAGS_MAX} words of 1 to 30 letters, digits, dots, dashes or underscores`, 'tags');
|
|
61
|
+
}
|
|
62
|
+
out.tags = [...new Set(tags)];
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
const render = (e) => [
|
|
67
|
+
'---',
|
|
68
|
+
'version: 1',
|
|
69
|
+
`id: ${JSON.stringify(e.id)}`,
|
|
70
|
+
`title: ${JSON.stringify(e.title)}`,
|
|
71
|
+
`tags: ${JSON.stringify(e.tags)}`,
|
|
72
|
+
`author: ${JSON.stringify(e.author)}`,
|
|
73
|
+
`createdAt: ${JSON.stringify(new Date(e.createdAt).toISOString())}`,
|
|
74
|
+
`updatedAt: ${JSON.stringify(new Date(e.updatedAt).toISOString())}`,
|
|
75
|
+
'---',
|
|
76
|
+
e.body,
|
|
77
|
+
'',
|
|
78
|
+
].join('\n');
|
|
79
|
+
/** The entry in a file; a file that is not one throws, and the caller skips it. */
|
|
80
|
+
function parse(text, id) {
|
|
81
|
+
const found = /^---\n([\s\S]*?)\n---\n([\s\S]*)$/.exec(text.replace(/\r\n/g, '\n'));
|
|
82
|
+
if (!found)
|
|
83
|
+
throw new Error('no front matter');
|
|
84
|
+
const values = new Map();
|
|
85
|
+
for (const line of (found[1] ?? '').split('\n')) {
|
|
86
|
+
const at = line.indexOf(': ');
|
|
87
|
+
if (at < 0)
|
|
88
|
+
throw new Error('a front matter line is not "key: value"');
|
|
89
|
+
values.set(line.slice(0, at), JSON.parse(line.slice(at + 2)));
|
|
90
|
+
}
|
|
91
|
+
const { version, title, tags, author, createdAt, updatedAt } = Object.fromEntries(values);
|
|
92
|
+
const at = (value) => (typeof value === 'string' ? Date.parse(value) : Number.NaN);
|
|
93
|
+
// an entry from before there was a version has none and reads as version 1
|
|
94
|
+
if (version !== undefined && version !== 1)
|
|
95
|
+
throw new Error(`format version ${String(version)} was saved by another gonogo; this one reads version 1`);
|
|
96
|
+
if (values.get('id') !== id)
|
|
97
|
+
throw new Error('the id does not match the name of the file');
|
|
98
|
+
if (typeof title !== 'string' || title === '' || title.length > LOG_TITLE_MAX_CHARS)
|
|
99
|
+
throw new Error('no valid title');
|
|
100
|
+
if (!Array.isArray(tags) || !tags.every((t) => typeof t === 'string'))
|
|
101
|
+
throw new Error('no valid tags');
|
|
102
|
+
if (typeof author !== 'string' || author === '')
|
|
103
|
+
throw new Error('no valid author');
|
|
104
|
+
if (Number.isNaN(at(createdAt)) || Number.isNaN(at(updatedAt)))
|
|
105
|
+
throw new Error('no valid times');
|
|
106
|
+
const body = (found[2] ?? '').replace(/\n$/, '');
|
|
107
|
+
return { id, title, body: body.slice(0, LOG_BODY_MAX_CHARS), tags: tags, author, createdAt: at(createdAt), updatedAt: at(updatedAt) };
|
|
108
|
+
}
|
|
109
|
+
export function createMissionLog(options) {
|
|
110
|
+
const { now, log } = options;
|
|
111
|
+
const dir = join(options.dir, LOG_DIR);
|
|
112
|
+
const entries = new Map();
|
|
113
|
+
const index = createIndex();
|
|
114
|
+
let seq = 0;
|
|
115
|
+
let chain = Promise.resolve();
|
|
116
|
+
/** One write at a time, so the limit and the ids hold; a failure goes to the caller and the next write goes on. */
|
|
117
|
+
const locked = (fn) => {
|
|
118
|
+
const run = chain.then(fn);
|
|
119
|
+
chain = run.catch(() => undefined);
|
|
120
|
+
return run;
|
|
121
|
+
};
|
|
122
|
+
/** Written whole to a temporary file and renamed, so a crash leaves the old text or the new, never half. */
|
|
123
|
+
const writeFileAtomic = async (name, text) => {
|
|
124
|
+
const tmp = join(dir, `.${name}.${process.pid}.${randomBytes(4).toString('hex')}.tmp`);
|
|
125
|
+
await writeFile(tmp, text, { mode: FILE_MODE, flag: 'wx' });
|
|
126
|
+
await rename(tmp, join(dir, name));
|
|
127
|
+
};
|
|
128
|
+
const keep = (entry) => {
|
|
129
|
+
entries.set(entry.id, entry);
|
|
130
|
+
index.add({ id: entry.id, title: entry.title, tags: entry.tags, body: entry.body, at: entry.updatedAt });
|
|
131
|
+
};
|
|
132
|
+
const find = (id) => {
|
|
133
|
+
const entry = ID_RE.test(id) ? entries.get(id) : undefined;
|
|
134
|
+
if (!entry)
|
|
135
|
+
throw new HubError('not_found', 'unknown entry');
|
|
136
|
+
return entry;
|
|
137
|
+
};
|
|
138
|
+
const hit = (e, query) => ({ id: e.id, title: e.title, author: e.author, tags: e.tags, updatedAt: e.updatedAt, snippet: snippet(e.body, query) });
|
|
139
|
+
return {
|
|
140
|
+
async load() {
|
|
141
|
+
await mkdir(dir, { recursive: true, mode: DIR_MODE });
|
|
142
|
+
for (const name of await readdir(dir)) {
|
|
143
|
+
const id = FILE_RE.exec(name)?.[1];
|
|
144
|
+
if (id === undefined)
|
|
145
|
+
continue;
|
|
146
|
+
seq = Math.max(seq, Number(id.slice(1)));
|
|
147
|
+
try {
|
|
148
|
+
keep(parse(await readFile(join(dir, name), 'utf8'), id));
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
// the file stays as it is: it may be a hand edit that the Director wants to mend
|
|
152
|
+
log.warn(`${LOG_DIR}/${name} was skipped: ${error instanceof Error ? error.message : String(error)}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const saved = Number(await readFile(join(dir, SEQ_FILE), 'utf8').catch(() => '0'));
|
|
156
|
+
if (Number.isSafeInteger(saved) && saved > seq)
|
|
157
|
+
seq = saved;
|
|
158
|
+
},
|
|
159
|
+
search(query, limit = LOG_SEARCH_MAX) {
|
|
160
|
+
const q = query.slice(0, LOG_QUERY_MAX_CHARS);
|
|
161
|
+
const cap = Math.min(Math.max(Math.trunc(limit), 1), LOG_SEARCH_MAX);
|
|
162
|
+
const found = q.trim() === '' ? [...entries.values()].sort((a, b) => b.updatedAt - a.updatedAt || b.createdAt - a.createdAt).slice(0, cap) : index.search(q, cap).map((r) => find(r.id));
|
|
163
|
+
return { total: entries.size, hits: found.map((e) => hit(e, q)) };
|
|
164
|
+
},
|
|
165
|
+
get: (id) => copy(find(id)),
|
|
166
|
+
chart: () => buildChart([...entries.values()]),
|
|
167
|
+
create(author, input) {
|
|
168
|
+
return locked(async () => {
|
|
169
|
+
const fields = validate(input, true);
|
|
170
|
+
if (entries.size >= LOG_ENTRIES_MAX)
|
|
171
|
+
throw new HubError('conflict', `the Mission Log holds at most ${LOG_ENTRIES_MAX} entries: remove one first`, 'log');
|
|
172
|
+
const at = now();
|
|
173
|
+
const entry = { id: `l${seq + 1}`, title: fields.title ?? '', body: fields.body ?? '', tags: fields.tags ?? [], author, createdAt: at, updatedAt: at };
|
|
174
|
+
await writeFileAtomic(`${entry.id}.md`, render(entry));
|
|
175
|
+
seq += 1;
|
|
176
|
+
keep(entry);
|
|
177
|
+
options.emit({ t: 'log', changed: [{ id: entry.id, title: entry.title }], removed: [] });
|
|
178
|
+
return copy(entry);
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
update(id, input, by) {
|
|
182
|
+
return locked(async () => {
|
|
183
|
+
const old = find(id);
|
|
184
|
+
if (by !== null && old.author !== by)
|
|
185
|
+
throw new HubError('forbidden', `${id} was written by ${old.author}: only its author or the Director may change it`);
|
|
186
|
+
const fields = validate(input, false);
|
|
187
|
+
const next = { ...old, ...fields, updatedAt: now() };
|
|
188
|
+
if (next.title === old.title && next.body === old.body && next.tags.join() === old.tags.join())
|
|
189
|
+
return copy(old);
|
|
190
|
+
await writeFileAtomic(`${id}.md`, render(next));
|
|
191
|
+
keep(next);
|
|
192
|
+
options.emit({ t: 'log', changed: [{ id, title: next.title }], removed: [] });
|
|
193
|
+
return copy(next);
|
|
194
|
+
});
|
|
195
|
+
},
|
|
196
|
+
remove(id) {
|
|
197
|
+
return locked(async () => {
|
|
198
|
+
find(id);
|
|
199
|
+
await writeFileAtomic(SEQ_FILE, String(seq));
|
|
200
|
+
// force: a file already deleted by hand must not leave an entry that can never be removed
|
|
201
|
+
await rm(join(dir, `${id}.md`), { force: true });
|
|
202
|
+
entries.delete(id);
|
|
203
|
+
index.remove(id);
|
|
204
|
+
options.emit({ t: 'log', changed: [], removed: [id] });
|
|
205
|
+
});
|
|
206
|
+
},
|
|
207
|
+
close() {
|
|
208
|
+
return chain.then(() => undefined);
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
}
|