@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,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Livery: the look of the page, and an agent that designs one (docs/livery-spec.md).
|
|
3
|
+
*
|
|
4
|
+
* Four liveries ship (src/shared/livery.ts). Custom ones are `<home>/liveries/<slug>.json` (mode 0600, in a 0700 folder), each
|
|
5
|
+
* `{ version, name, description, author, tokens }`; the chosen slug is `<home>/livery.json`. Both are written whole through a
|
|
6
|
+
* temporary file and a rename, before the memory changes. A livery is checked on the way in (every token present, each a plain
|
|
7
|
+
* colour, every text pair readable: checkLivery) and again when a file is read, so a hand-made file cannot put a value that is not a
|
|
8
|
+
* colour into the page. An agent submits one with the tool livery_submit; the Director chooses it. Submitting never applies a
|
|
9
|
+
* livery. The send and the clock are injected for tests.
|
|
10
|
+
*
|
|
11
|
+
* shortcut: the custom liveries are read once, at start, so a file put in the folder by hand shows after a restart; watch the
|
|
12
|
+
* folder if that gets in the way.
|
|
13
|
+
* shortcut: a submit under the name of a custom livery replaces it, whoever made it (never the one in use); keep a version per
|
|
14
|
+
* author 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 { AGENT_NAME_PATTERN } from '../shared/protocol.js';
|
|
20
|
+
import { builtinEntries, checkLivery, contrastRules, DEFAULT_LIVERY, LIVERIES_MAX, LIVERY_BRIEF_MAX_CHARS, LIVERY_DESCRIPTION_MAX_CHARS, LIVERY_NAME_MAX_CHARS, LIVERY_SLUG_PATTERN, LIVERY_TOKENS, MIN_CONTRAST, slugOf, } from '../shared/livery.js';
|
|
21
|
+
import { DIR_MODE, FILE_MODE, LIVERY_DIR, LIVERY_FILE } from './constants.js';
|
|
22
|
+
import { isRecord } from './hooks.js';
|
|
23
|
+
import { HubError } from './hub.js';
|
|
24
|
+
import { UNSAFE_IN_NAME } from './schedules.js';
|
|
25
|
+
const NAME_RE = new RegExp(AGENT_NAME_PATTERN);
|
|
26
|
+
const SLUG_RE = new RegExp(LIVERY_SLUG_PATTERN);
|
|
27
|
+
const FILE_RE = /^([a-z0-9][a-z0-9-]{0,39})\.json$/;
|
|
28
|
+
const SUBMIT_FIELDS = ['name', 'description', 'tokens'];
|
|
29
|
+
/** The answer to a refused submit names this many faults, then says how many more there are. */
|
|
30
|
+
const PROBLEMS_SHOWN = 12;
|
|
31
|
+
const bad = (message, field) => new HubError('bad_request', message, field);
|
|
32
|
+
/** The message that asks an agent for a livery: the brief, the tokens with their roles, the contrast rule and what to call. */
|
|
33
|
+
export function designBody(brief) {
|
|
34
|
+
return [
|
|
35
|
+
'The Director asks you to design a livery, a colour theme for gonogo.',
|
|
36
|
+
'',
|
|
37
|
+
`Brief: ${brief}`,
|
|
38
|
+
'',
|
|
39
|
+
`Call mcp__gonogo__livery_submit with a name (1 to ${LIVERY_NAME_MAX_CHARS} characters), a one-line description and tokens: an object that holds every token below and no other. Each value is a CSS colour written as #rgb, #rrggbb, rgb(), hsl() or oklch(), with no alpha, no colour names and no var().`,
|
|
40
|
+
'',
|
|
41
|
+
'Tokens:',
|
|
42
|
+
...LIVERY_TOKENS.map((t) => `${t.name}: ${t.role}`),
|
|
43
|
+
'',
|
|
44
|
+
`Contrast (WCAG 2): a text must reach ${MIN_CONTRAST}:1 on the surfaces it sits on.`,
|
|
45
|
+
...contrastRules().map((line) => `- ${line}`),
|
|
46
|
+
'',
|
|
47
|
+
'If the livery is refused, the answer lists what fails: fix it and submit again. Submitting does not apply it; the Director picks it in the Livery view.',
|
|
48
|
+
].join('\n');
|
|
49
|
+
}
|
|
50
|
+
export function createLiveries(options) {
|
|
51
|
+
const { log } = options;
|
|
52
|
+
const dir = join(options.dir, LIVERY_DIR);
|
|
53
|
+
const choice = join(options.dir, LIVERY_FILE);
|
|
54
|
+
const builtin = builtinEntries();
|
|
55
|
+
const custom = new Map();
|
|
56
|
+
let current = DEFAULT_LIVERY;
|
|
57
|
+
let chain = Promise.resolve();
|
|
58
|
+
/** One write at a time, so the limit holds; a failure goes to the caller and the next write goes on. */
|
|
59
|
+
const locked = (fn) => {
|
|
60
|
+
const run = chain.then(fn);
|
|
61
|
+
chain = run.catch(() => undefined);
|
|
62
|
+
return run;
|
|
63
|
+
};
|
|
64
|
+
const writeAtomic = async (path, value) => {
|
|
65
|
+
const tmp = `${path}.${process.pid}.${randomBytes(4).toString('hex')}.tmp`;
|
|
66
|
+
await writeFile(tmp, JSON.stringify(value, null, 2), { mode: FILE_MODE, flag: 'wx' });
|
|
67
|
+
await rename(tmp, path);
|
|
68
|
+
};
|
|
69
|
+
const view = () => ({
|
|
70
|
+
current,
|
|
71
|
+
liveries: [...builtin, ...[...custom.values()].sort((a, b) => (a.slug < b.slug ? -1 : 1))].map((e) => ({ ...e, tokens: { ...e.tokens } })),
|
|
72
|
+
});
|
|
73
|
+
const changed = () => {
|
|
74
|
+
const now = view();
|
|
75
|
+
options.emit({ t: 'livery', livery: now });
|
|
76
|
+
return now;
|
|
77
|
+
};
|
|
78
|
+
const exists = (slug) => builtin.some((b) => b.slug === slug) || custom.has(slug);
|
|
79
|
+
/** A livery from the wire or a file: the name, the description and the tokens, checked. Throws a HubError that says what to fix. */
|
|
80
|
+
function read(input) {
|
|
81
|
+
const name = typeof input.name === 'string' ? input.name.trim() : '';
|
|
82
|
+
if (name === '' || name.length > LIVERY_NAME_MAX_CHARS || UNSAFE_IN_NAME.test(name) || slugOf(name) === '') {
|
|
83
|
+
throw bad(`name must be 1 to ${LIVERY_NAME_MAX_CHARS} characters on one line, with a letter or a digit`, 'name');
|
|
84
|
+
}
|
|
85
|
+
const description = input.description === undefined ? '' : typeof input.description === 'string' ? input.description.trim() : null;
|
|
86
|
+
if (description === null || description.length > LIVERY_DESCRIPTION_MAX_CHARS || UNSAFE_IN_NAME.test(description)) {
|
|
87
|
+
throw bad(`description must be at most ${LIVERY_DESCRIPTION_MAX_CHARS} characters on one line`, 'description');
|
|
88
|
+
}
|
|
89
|
+
const problems = checkLivery(input.tokens);
|
|
90
|
+
if (problems.length > 0) {
|
|
91
|
+
const shown = problems.slice(0, PROBLEMS_SHOWN).join('; ');
|
|
92
|
+
throw bad(problems.length > PROBLEMS_SHOWN ? `${shown}; and ${problems.length - PROBLEMS_SHOWN} more` : shown, 'tokens');
|
|
93
|
+
}
|
|
94
|
+
const tokens = Object.fromEntries(Object.entries(input.tokens).map(([k, v]) => [k, v.trim()]));
|
|
95
|
+
return { name, description, tokens };
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
async load() {
|
|
99
|
+
await mkdir(dir, { recursive: true, mode: DIR_MODE });
|
|
100
|
+
for (const file of await readdir(dir)) {
|
|
101
|
+
const slug = FILE_RE.exec(file)?.[1];
|
|
102
|
+
if (slug === undefined)
|
|
103
|
+
continue;
|
|
104
|
+
try {
|
|
105
|
+
const saved = JSON.parse(await readFile(join(dir, file), 'utf8'));
|
|
106
|
+
if (!isRecord(saved) || saved.version !== 1)
|
|
107
|
+
throw new Error('not a version 1 livery file');
|
|
108
|
+
if (builtin.some((b) => b.slug === slug))
|
|
109
|
+
throw new Error('the name is taken by a livery that ships');
|
|
110
|
+
const { name, description, tokens } = read(saved);
|
|
111
|
+
if (slugOf(name) !== slug)
|
|
112
|
+
throw new Error('the name does not match the name of the file');
|
|
113
|
+
custom.set(slug, { slug, name, description, builtin: false, author: typeof saved.author === 'string' ? saved.author : null, tokens });
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
// the file stays as it is: it may be a hand edit that the Director wants to mend
|
|
117
|
+
log.warn(`${LIVERY_DIR}/${file} was skipped: ${error instanceof Error ? error.message : String(error)}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
const saved = JSON.parse(await readFile(choice, 'utf8'));
|
|
122
|
+
if (isRecord(saved) && saved.version === 1 && typeof saved.current === 'string' && exists(saved.current))
|
|
123
|
+
current = saved.current;
|
|
124
|
+
else
|
|
125
|
+
log.warn(`${LIVERY_FILE} does not name a livery that exists; the default is used`);
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
if (error.code !== 'ENOENT')
|
|
129
|
+
log.warn(`${LIVERY_FILE} could not be read; the default is used`);
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
view,
|
|
133
|
+
select(input) {
|
|
134
|
+
return locked(async () => {
|
|
135
|
+
if (!isRecord(input))
|
|
136
|
+
throw bad('request body must be a JSON object');
|
|
137
|
+
for (const key of Object.keys(input))
|
|
138
|
+
if (key !== 'slug')
|
|
139
|
+
throw bad(`unknown field "${key}"`, key);
|
|
140
|
+
if (typeof input.slug !== 'string' || !SLUG_RE.test(input.slug))
|
|
141
|
+
throw bad('slug must be the slug of a livery', 'slug');
|
|
142
|
+
if (!exists(input.slug))
|
|
143
|
+
throw new HubError('not_found', 'unknown livery', 'slug');
|
|
144
|
+
if (input.slug === current)
|
|
145
|
+
return view();
|
|
146
|
+
await writeAtomic(choice, { version: 1, current: input.slug });
|
|
147
|
+
current = input.slug;
|
|
148
|
+
return changed();
|
|
149
|
+
});
|
|
150
|
+
},
|
|
151
|
+
remove(slug) {
|
|
152
|
+
return locked(async () => {
|
|
153
|
+
if (builtin.some((b) => b.slug === slug))
|
|
154
|
+
throw new HubError('forbidden', 'a livery that ships cannot be deleted');
|
|
155
|
+
if (!SLUG_RE.test(slug) || !custom.has(slug))
|
|
156
|
+
throw new HubError('not_found', 'unknown livery');
|
|
157
|
+
if (current === slug) {
|
|
158
|
+
await writeAtomic(choice, { version: 1, current: DEFAULT_LIVERY });
|
|
159
|
+
current = DEFAULT_LIVERY;
|
|
160
|
+
}
|
|
161
|
+
// force: a file already deleted by hand must not leave a livery that can never be removed
|
|
162
|
+
await rm(join(dir, `${slug}.json`), { force: true });
|
|
163
|
+
custom.delete(slug);
|
|
164
|
+
return changed();
|
|
165
|
+
});
|
|
166
|
+
},
|
|
167
|
+
submit(author, input) {
|
|
168
|
+
return locked(async () => {
|
|
169
|
+
if (!isRecord(input))
|
|
170
|
+
throw bad('request body must be a JSON object');
|
|
171
|
+
for (const key of Object.keys(input))
|
|
172
|
+
if (!SUBMIT_FIELDS.includes(key))
|
|
173
|
+
throw bad(`unknown field "${key}"`, key);
|
|
174
|
+
const { name, description, tokens } = read(input);
|
|
175
|
+
const slug = slugOf(name);
|
|
176
|
+
if (builtin.some((b) => b.slug === slug))
|
|
177
|
+
throw bad(`"${name}" is the name of a livery that ships: pick another name`, 'name');
|
|
178
|
+
// a replace of the one in use would change the look on every device: the Director chooses, an agent only submits
|
|
179
|
+
if (slug === current)
|
|
180
|
+
throw new HubError('conflict', `"${name}" is the livery in use: pick another name`, 'name');
|
|
181
|
+
if (!custom.has(slug) && custom.size >= LIVERIES_MAX) {
|
|
182
|
+
throw new HubError('conflict', `there are ${LIVERIES_MAX} custom liveries already: the Director has to delete one first`, 'livery');
|
|
183
|
+
}
|
|
184
|
+
const entry = { slug, name, description, builtin: false, author, tokens };
|
|
185
|
+
await writeAtomic(join(dir, `${slug}.json`), { version: 1, name, description, author, tokens });
|
|
186
|
+
custom.set(slug, entry);
|
|
187
|
+
changed();
|
|
188
|
+
options.notify?.('livery', `${author} submitted the livery "${name}". Pick it in the Livery view to try it.`);
|
|
189
|
+
return { ...entry, tokens: { ...tokens } };
|
|
190
|
+
});
|
|
191
|
+
},
|
|
192
|
+
design(input) {
|
|
193
|
+
if (!isRecord(input))
|
|
194
|
+
throw bad('request body must be a JSON object');
|
|
195
|
+
for (const key of Object.keys(input))
|
|
196
|
+
if (key !== 'to' && key !== 'brief')
|
|
197
|
+
throw bad(`unknown field "${key}"`, key);
|
|
198
|
+
const to = input.to === undefined ? 'boss' : input.to;
|
|
199
|
+
if (typeof to !== 'string' || (to !== 'boss' && (to === 'user' || !NAME_RE.test(to))))
|
|
200
|
+
throw bad('to must be an agent name or "boss"', 'to');
|
|
201
|
+
const brief = typeof input.brief === 'string' ? input.brief.trim() : '';
|
|
202
|
+
if (brief === '' || brief.length > LIVERY_BRIEF_MAX_CHARS)
|
|
203
|
+
throw bad(`brief must be 1 to ${LIVERY_BRIEF_MAX_CHARS} characters`, 'brief');
|
|
204
|
+
return options.send({ to, subject: 'Livery: design request', body: designBody(brief) });
|
|
205
|
+
},
|
|
206
|
+
close() {
|
|
207
|
+
return chain.then(() => undefined);
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export const CHART_ENTRIES_MAX = 150;
|
|
2
|
+
export const CHART_TAGS_MAX = 40;
|
|
3
|
+
/** Two tags are joined by a co-occurrence edge when this many entries hold both. */
|
|
4
|
+
export const COOCCUR_MIN = 2;
|
|
5
|
+
/** An entry id as a word of its own: `l12`, `(l12)`, `l12.`; not `cl12`, `l12x` or `l12-b`. */
|
|
6
|
+
const MENTION = /(?<![\p{L}\p{N}_-])l([1-9]\d{0,5})(?![\p{L}\p{N}_-])/gu;
|
|
7
|
+
/** The ids of other entries named in a text, each once, in order of appearance. */
|
|
8
|
+
export function mentions(text, self) {
|
|
9
|
+
const found = new Set();
|
|
10
|
+
for (const match of text.matchAll(MENTION))
|
|
11
|
+
if (`l${match[1]}` !== self)
|
|
12
|
+
found.add(`l${match[1]}`);
|
|
13
|
+
return [...found];
|
|
14
|
+
}
|
|
15
|
+
export function buildChart(all) {
|
|
16
|
+
const shown = [...all].sort((a, b) => b.updatedAt - a.updatedAt || b.createdAt - a.createdAt || a.id.localeCompare(b.id)).slice(0, CHART_ENTRIES_MAX);
|
|
17
|
+
const ids = new Set(shown.map((e) => e.id));
|
|
18
|
+
const uses = new Map();
|
|
19
|
+
for (const entry of shown)
|
|
20
|
+
for (const tag of entry.tags)
|
|
21
|
+
uses.set(tag, (uses.get(tag) ?? 0) + 1);
|
|
22
|
+
const tags = [...uses].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])).slice(0, CHART_TAGS_MAX);
|
|
23
|
+
const tagged = new Set(tags.map(([tag]) => tag));
|
|
24
|
+
const authors = new Map();
|
|
25
|
+
for (const entry of shown)
|
|
26
|
+
authors.set(entry.author, (authors.get(entry.author) ?? 0) + 1);
|
|
27
|
+
const edges = [];
|
|
28
|
+
const named = new Map();
|
|
29
|
+
const together = new Map();
|
|
30
|
+
for (const entry of shown) {
|
|
31
|
+
const from = `entry:${entry.id}`;
|
|
32
|
+
for (const tag of entry.tags)
|
|
33
|
+
if (tagged.has(tag))
|
|
34
|
+
edges.push({ from, to: `tag:${tag}`, kind: 'tag', weight: 1 });
|
|
35
|
+
edges.push({ from, to: `author:${entry.author}`, kind: 'author', weight: 1 });
|
|
36
|
+
for (const id of mentions(`${entry.title}\n${entry.body}`, entry.id)) {
|
|
37
|
+
if (!ids.has(id))
|
|
38
|
+
continue;
|
|
39
|
+
edges.push({ from, to: `entry:${id}`, kind: 'mention', weight: 1 });
|
|
40
|
+
named.set(id, (named.get(id) ?? 0) + 1);
|
|
41
|
+
}
|
|
42
|
+
const own = entry.tags.filter((t) => tagged.has(t)).sort();
|
|
43
|
+
for (let i = 0; i < own.length; i += 1)
|
|
44
|
+
for (let j = i + 1; j < own.length; j += 1)
|
|
45
|
+
together.set(`${own[i]}\n${own[j]}`, (together.get(`${own[i]}\n${own[j]}`) ?? 0) + 1);
|
|
46
|
+
}
|
|
47
|
+
for (const [pair, weight] of [...together].sort((a, b) => a[0].localeCompare(b[0]))) {
|
|
48
|
+
const [a = '', b = ''] = pair.split('\n');
|
|
49
|
+
if (weight >= COOCCUR_MIN)
|
|
50
|
+
edges.push({ from: `tag:${a}`, to: `tag:${b}`, kind: 'cooccur', weight });
|
|
51
|
+
}
|
|
52
|
+
const nodes = [
|
|
53
|
+
...shown.map((e) => ({ id: `entry:${e.id}`, kind: 'entry', ref: e.id, label: e.title, weight: 1 + (named.get(e.id) ?? 0) })),
|
|
54
|
+
...tags.map(([tag, weight]) => ({ id: `tag:${tag}`, kind: 'tag', ref: tag, label: tag, weight })),
|
|
55
|
+
...[...authors].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])).map(([name, weight]) => ({ id: `author:${name}`, kind: 'author', ref: name, label: name, weight })),
|
|
56
|
+
];
|
|
57
|
+
return { total: all.length, shown: shown.length, nodes, edges };
|
|
58
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mission Log search (docs/mission-log-spec.md): BM25 over the title and the body, in memory.
|
|
3
|
+
*
|
|
4
|
+
* The title and the tags count as TITLE_WEIGHT copies of themselves, so a word in a title weighs more than the same word in a body (the
|
|
5
|
+
* usual way to weight a field in BM25). A word is a run of letters or digits, lowercased, two characters or more; there is no stemming,
|
|
6
|
+
* so "builds" does not find "build". The index holds only counts: the text stays with the entry, and `snippet` reads it.
|
|
7
|
+
*
|
|
8
|
+
* shortcut: no stemming, no phrase or prefix search, and the words are Unicode letters and digits, so Chinese or Japanese text is one
|
|
9
|
+
* long word; upgrade if the log is not mostly in a language that a space separates.
|
|
10
|
+
*/
|
|
11
|
+
import { LOG_SNIPPET_CHARS, LOG_SNIPPET_LEAD_CHARS } from './constants.js';
|
|
12
|
+
const K1 = 1.2;
|
|
13
|
+
const B = 0.75;
|
|
14
|
+
const TITLE_WEIGHT = 3;
|
|
15
|
+
const WORD = /[\p{L}\p{N}]{2,}/gu;
|
|
16
|
+
/** The words of a text, lowercased. */
|
|
17
|
+
export const tokenize = (text) => text.toLowerCase().match(WORD) ?? [];
|
|
18
|
+
export function createIndex() {
|
|
19
|
+
/** word -> id -> weighted count */
|
|
20
|
+
const postings = new Map();
|
|
21
|
+
const docs = new Map();
|
|
22
|
+
let total = 0;
|
|
23
|
+
const remove = (id) => {
|
|
24
|
+
const doc = docs.get(id);
|
|
25
|
+
if (doc === undefined)
|
|
26
|
+
return;
|
|
27
|
+
for (const term of doc.terms) {
|
|
28
|
+
const posting = postings.get(term);
|
|
29
|
+
posting?.delete(id);
|
|
30
|
+
if (posting?.size === 0)
|
|
31
|
+
postings.delete(term);
|
|
32
|
+
}
|
|
33
|
+
total -= doc.length;
|
|
34
|
+
docs.delete(id);
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
add(doc) {
|
|
38
|
+
remove(doc.id);
|
|
39
|
+
const counts = new Map();
|
|
40
|
+
let length = 0;
|
|
41
|
+
const count = (words, weight) => {
|
|
42
|
+
for (const word of words)
|
|
43
|
+
counts.set(word, (counts.get(word) ?? 0) + weight);
|
|
44
|
+
length += words.length * weight;
|
|
45
|
+
};
|
|
46
|
+
count(tokenize(`${doc.title} ${doc.tags.join(' ')}`), TITLE_WEIGHT);
|
|
47
|
+
count(tokenize(doc.body), 1);
|
|
48
|
+
for (const [term, n] of counts) {
|
|
49
|
+
const posting = postings.get(term) ?? new Map();
|
|
50
|
+
posting.set(doc.id, n);
|
|
51
|
+
postings.set(term, posting);
|
|
52
|
+
}
|
|
53
|
+
docs.set(doc.id, { length, terms: [...counts.keys()], at: doc.at });
|
|
54
|
+
total += length;
|
|
55
|
+
},
|
|
56
|
+
remove,
|
|
57
|
+
search(query, limit) {
|
|
58
|
+
const average = total / Math.max(docs.size, 1);
|
|
59
|
+
const scores = new Map();
|
|
60
|
+
for (const term of new Set(tokenize(query))) {
|
|
61
|
+
const posting = postings.get(term);
|
|
62
|
+
if (posting === undefined)
|
|
63
|
+
continue;
|
|
64
|
+
const idf = Math.log(1 + (docs.size - posting.size + 0.5) / (posting.size + 0.5));
|
|
65
|
+
for (const [id, tf] of posting) {
|
|
66
|
+
const length = docs.get(id)?.length ?? 0;
|
|
67
|
+
scores.set(id, (scores.get(id) ?? 0) + (idf * tf * (K1 + 1)) / (tf + K1 * (1 - B + (B * length) / average)));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return [...scores]
|
|
71
|
+
.map(([id, score]) => ({ id, score }))
|
|
72
|
+
.sort((a, b) => b.score - a.score || (docs.get(b.id)?.at ?? 0) - (docs.get(a.id)?.at ?? 0))
|
|
73
|
+
.slice(0, limit);
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* A piece of `text` around the place where the most different words of the query come close together (the first such place on a
|
|
79
|
+
* tie), on one line, with "…" where it was cut. A text with no match gives its start.
|
|
80
|
+
*/
|
|
81
|
+
export function snippet(text, query) {
|
|
82
|
+
const wanted = new Set(tokenize(query));
|
|
83
|
+
const hits = [...text.matchAll(WORD)].filter((m) => wanted.has(m[0].toLowerCase()));
|
|
84
|
+
let at = 0;
|
|
85
|
+
let best = -1;
|
|
86
|
+
for (let i = 0; i < hits.length; i += 1) {
|
|
87
|
+
const start = hits[i]?.index ?? 0;
|
|
88
|
+
const seen = new Set();
|
|
89
|
+
for (let j = i; j < hits.length && (hits[j]?.index ?? 0) < start + LOG_SNIPPET_CHARS - LOG_SNIPPET_LEAD_CHARS; j += 1)
|
|
90
|
+
seen.add(hits[j]?.[0].toLowerCase() ?? '');
|
|
91
|
+
if (seen.size > best) {
|
|
92
|
+
best = seen.size;
|
|
93
|
+
at = start;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// a match near the end gets the window back, so that the piece is as long as it can be
|
|
97
|
+
let from = Math.max(0, Math.min(at - LOG_SNIPPET_LEAD_CHARS, text.length - LOG_SNIPPET_CHARS));
|
|
98
|
+
// start at a word, unless that would cut off the match itself
|
|
99
|
+
if (from > 0 && /\S/.test(text[from - 1] ?? '')) {
|
|
100
|
+
const space = text.slice(from, at).search(/\s/);
|
|
101
|
+
if (space >= 0)
|
|
102
|
+
from += space + 1;
|
|
103
|
+
}
|
|
104
|
+
let to = Math.min(text.length, from + LOG_SNIPPET_CHARS);
|
|
105
|
+
if (to < text.length && /\S/.test(text[to] ?? '')) {
|
|
106
|
+
const space = text.slice(at, to).search(/\s\S*$/);
|
|
107
|
+
if (space > 0)
|
|
108
|
+
to = at + space;
|
|
109
|
+
}
|
|
110
|
+
const piece = text.slice(from, to).replace(/\s+/g, ' ').trim();
|
|
111
|
+
return `${from > 0 ? '…' : ''}${piece}${to < text.length ? '…' : ''}`;
|
|
112
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mass Budget: how big each agent's memory.md is, and a request to the agent to condense it (docs/mass-budget-spec.md).
|
|
3
|
+
*
|
|
4
|
+
* gonogo never writes memory.md: the agent may be writing it. A request is a message from the Director through the hub, so Max Q,
|
|
5
|
+
* Redline and held mail apply, and the agent rewrites its own file. What gonogo does write is one copy, memory.md.bak, taken from
|
|
6
|
+
* the bytes it read when the request is sent and put in place only after the hub took the message (so a refused request keeps
|
|
7
|
+
* the older copy). The file is opened without following a symbolic link and is never read past MEMORY_READ_MAX bytes.
|
|
8
|
+
*
|
|
9
|
+
* shortcut: the lines and the bytes of the soft limit are constants (the role text tells an agent to keep its file under 200
|
|
10
|
+
* lines); make them options if anyone runs agents that need more.
|
|
11
|
+
*/
|
|
12
|
+
import { randomBytes } from 'node:crypto';
|
|
13
|
+
import { constants } from 'node:fs';
|
|
14
|
+
import { lstat, open, rename, rm, writeFile } from 'node:fs/promises';
|
|
15
|
+
import { basename, dirname, join } from 'node:path';
|
|
16
|
+
import { FILE_MODE, MEMORY_FILE } from './constants.js';
|
|
17
|
+
import { HubError } from './hub.js';
|
|
18
|
+
export const MEMORY_BACKUP_FILE = `${MEMORY_FILE}.bak`;
|
|
19
|
+
/** The role text asks for under 200 lines; the size limit is what 200 lines of notes come to. */
|
|
20
|
+
export const MEMORY_SOFT_LINES = 200;
|
|
21
|
+
export const MEMORY_SOFT_BYTES = 16 * 1024;
|
|
22
|
+
/** What the request asks the agent to bring the file down to. */
|
|
23
|
+
export const MEMORY_TARGET_LINES = 100;
|
|
24
|
+
/** A larger file is not copied or counted: trim it by hand. */
|
|
25
|
+
export const MEMORY_READ_MAX = 1024 * 1024;
|
|
26
|
+
/** The message to the agent. Everything it needs is in it: the file, the size, what to keep, what to drop, how to write it and how to answer. */
|
|
27
|
+
export function reflectBody(input) {
|
|
28
|
+
return [
|
|
29
|
+
'The Director asks you to condense your memory file.',
|
|
30
|
+
`File: ${input.path} (${input.bytes} bytes, ${input.lines} lines). gonogo copied it to ${input.path}.bak when it sent this message: do not use that copy as notes and do not edit it.`,
|
|
31
|
+
'When your current step is done:',
|
|
32
|
+
'1. Read the whole file.',
|
|
33
|
+
`2. Write a shorter version of at most ${MEMORY_TARGET_LINES} lines. Keep what is still true and still useful: decisions and their reasons, rules and conventions, paths, commands, names, ids and open problems. Drop what is done, repeated or replaced, and any step-by-step log of what you did. Merge notes on one subject. Add nothing that is not in the file.`,
|
|
34
|
+
'3. A finding that every agent should know goes to the Mission Log with log_write, and leaves your file.',
|
|
35
|
+
`4. Replace the file with the new text in one write. Keep the first line "# Memory of ${input.name}" and plain markdown. Do not change any other file.`,
|
|
36
|
+
'5. Then send_message to "user" with the subject "Memory condensed" and one sentence: the lines before and after. If the file is already short and has nothing to drop, change nothing and say so in one sentence.',
|
|
37
|
+
].join('\n');
|
|
38
|
+
}
|
|
39
|
+
const conflict = (message) => new HubError('conflict', message);
|
|
40
|
+
const lineCount = (text) => {
|
|
41
|
+
if (text.length === 0)
|
|
42
|
+
return 0;
|
|
43
|
+
let lines = 0;
|
|
44
|
+
for (const byte of text)
|
|
45
|
+
if (byte === 0x0a)
|
|
46
|
+
lines += 1;
|
|
47
|
+
return text[text.length - 1] === 0x0a ? lines : lines + 1;
|
|
48
|
+
};
|
|
49
|
+
/** The file as it is, read without following a link. null when it is not there. A link, a folder or a device is refused. */
|
|
50
|
+
async function readMemory(path) {
|
|
51
|
+
let handle;
|
|
52
|
+
try {
|
|
53
|
+
// O_NONBLOCK: opening a named pipe in that place must not wait for a writer
|
|
54
|
+
handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
const code = error.code;
|
|
58
|
+
if (code === 'ENOENT')
|
|
59
|
+
return null;
|
|
60
|
+
if (code === 'ELOOP')
|
|
61
|
+
throw conflict(`${basename(path)} is a symbolic link: gonogo does not follow it`);
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
const info = await handle.stat();
|
|
66
|
+
if (!info.isFile())
|
|
67
|
+
throw conflict(`${basename(path)} is not a regular file`);
|
|
68
|
+
if (info.size > MEMORY_READ_MAX)
|
|
69
|
+
return { bytes: info.size, data: null, changedAt: info.mtimeMs };
|
|
70
|
+
const data = Buffer.alloc(info.size);
|
|
71
|
+
let got = 0;
|
|
72
|
+
while (got < data.length) {
|
|
73
|
+
const { bytesRead } = await handle.read(data, got, data.length - got, got);
|
|
74
|
+
if (bytesRead === 0)
|
|
75
|
+
break;
|
|
76
|
+
got += bytesRead;
|
|
77
|
+
}
|
|
78
|
+
return { bytes: got, data: data.subarray(0, got), changedAt: info.mtimeMs };
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
await handle.close();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export function createMassBudget(options) {
|
|
85
|
+
const { log } = options;
|
|
86
|
+
/** One request per agent at a time: a double click must not send the message twice. */
|
|
87
|
+
const asking = new Set();
|
|
88
|
+
const known = (name) => {
|
|
89
|
+
const agent = options.agent(name);
|
|
90
|
+
if (!agent)
|
|
91
|
+
throw new HubError('not_found', 'unknown agent');
|
|
92
|
+
if (agent.provider === 'custom')
|
|
93
|
+
throw new HubError('not_found', `${name} runs a custom command: it has no memory file`);
|
|
94
|
+
return agent;
|
|
95
|
+
};
|
|
96
|
+
const backupOf = async (path) => {
|
|
97
|
+
try {
|
|
98
|
+
const info = await lstat(`${path}.bak`);
|
|
99
|
+
return info.isFile() ? { bytes: info.size, at: info.mtimeMs } : null;
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
const describe = async (path, read) => {
|
|
106
|
+
const bytes = read?.bytes ?? 0;
|
|
107
|
+
const lines = read === null ? 0 : read.data === null ? null : lineCount(read.data);
|
|
108
|
+
return {
|
|
109
|
+
exists: read !== null,
|
|
110
|
+
bytes,
|
|
111
|
+
lines,
|
|
112
|
+
softLines: MEMORY_SOFT_LINES,
|
|
113
|
+
softBytes: MEMORY_SOFT_BYTES,
|
|
114
|
+
over: bytes > MEMORY_SOFT_BYTES || (lines ?? Infinity) > MEMORY_SOFT_LINES,
|
|
115
|
+
changedAt: read?.changedAt ?? null,
|
|
116
|
+
backup: await backupOf(path),
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
return {
|
|
120
|
+
async view(name) {
|
|
121
|
+
known(name);
|
|
122
|
+
const path = options.memoryPath(name);
|
|
123
|
+
return describe(path, await readMemory(path));
|
|
124
|
+
},
|
|
125
|
+
async reflect(name) {
|
|
126
|
+
known(name);
|
|
127
|
+
if (asking.has(name))
|
|
128
|
+
throw conflict(`a request to ${name} is being sent`);
|
|
129
|
+
asking.add(name);
|
|
130
|
+
try {
|
|
131
|
+
const path = options.memoryPath(name);
|
|
132
|
+
const read = await readMemory(path);
|
|
133
|
+
if (read === null)
|
|
134
|
+
throw conflict(`${name} has no memory file yet: start it first`);
|
|
135
|
+
if (read.data === null)
|
|
136
|
+
throw conflict(`${MEMORY_FILE} of ${name} is ${Math.round(read.bytes / 1024)} KiB, more than the ${MEMORY_READ_MAX / 1024} KiB gonogo copies: trim it by hand`);
|
|
137
|
+
// written first, put in place after the hub took the message
|
|
138
|
+
const tmp = join(dirname(path), `.${MEMORY_BACKUP_FILE}.${process.pid}.${randomBytes(4).toString('hex')}.tmp`);
|
|
139
|
+
await writeFile(tmp, read.data, { mode: FILE_MODE, flag: 'wx' }).catch(async (error) => {
|
|
140
|
+
// a full disk leaves half a file behind
|
|
141
|
+
await rm(tmp, { force: true });
|
|
142
|
+
throw error;
|
|
143
|
+
});
|
|
144
|
+
let send;
|
|
145
|
+
try {
|
|
146
|
+
send = options.send({ to: name, subject: 'Mass Budget: condense your memory file', body: reflectBody({ name, path, bytes: read.bytes, lines: lineCount(read.data) }) });
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
await rm(tmp, { force: true });
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
if (send.ok) {
|
|
153
|
+
// rename puts the copy in place of a link too, never through it
|
|
154
|
+
await rename(tmp, `${path}.bak`).catch(async (error) => {
|
|
155
|
+
log.warn(`could not write ${MEMORY_BACKUP_FILE} of ${name}`, error);
|
|
156
|
+
await rm(tmp, { force: true });
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
await rm(tmp, { force: true });
|
|
161
|
+
}
|
|
162
|
+
return { send, memory: await describe(path, await readMemory(path)) };
|
|
163
|
+
}
|
|
164
|
+
finally {
|
|
165
|
+
asking.delete(name);
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|