@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,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Downlink: the Director's inbox (docs/downlink-spec.md).
|
|
3
|
+
*
|
|
4
|
+
* Two kinds of item: every message to "user" (the server keeps a copy of the newest DOWNLINK_MAIL_MAX, found again in
|
|
5
|
+
* messages.jsonl at start) and the events other parts of the server post (a Redline trip, a T-minus result, a failed Launch
|
|
6
|
+
* Window, an Uplink that was refused too often, a task an agent moved to blocked or review). downlink.json keeps the events
|
|
7
|
+
* with their text and one flag per item id, `read` or `archived` (archived is read too); no flag is unread. So phone and
|
|
8
|
+
* desktop agree: the browsers only show what the server says. The clock is injected for tests.
|
|
9
|
+
*
|
|
10
|
+
* shortcut: a message leaves the inbox when DOWNLINK_MAIL_MAX newer ones came (it stays in messages.jsonl), and the whole
|
|
11
|
+
* inbox is in every frame and every snapshot (at the limits that is about 700 KB); send only the changes if that ever hurts.
|
|
12
|
+
*/
|
|
13
|
+
import { randomBytes } from 'node:crypto';
|
|
14
|
+
import { readFile, rename, writeFile } from 'node:fs/promises';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
import { ACTIVITY_WIRE_BODY_CHARS, DOWNLINK_EVENT_KINDS } from '../shared/protocol.js';
|
|
17
|
+
import { DOWNLINK_EVENTS_MAX, DOWNLINK_FILE, DOWNLINK_IDS_MAX, DOWNLINK_MAIL_MAX, DOWNLINK_TEXT_MAX_CHARS, FILE_MODE } from './constants.js';
|
|
18
|
+
import { isRecord } from './hooks.js';
|
|
19
|
+
import { cutText, HubError } from './hub.js';
|
|
20
|
+
const ID_RE = /^[me][1-9]\d{0,9}$/;
|
|
21
|
+
const EVENT_ID_RE = /^e[1-9]\d{0,9}$/;
|
|
22
|
+
// C0, DEL, C1 and bidi controls: an event text comes from agents and must stay one clean line.
|
|
23
|
+
const UNSAFE_IN_TEXT = /[\u0000-\u001f\u007f-\u009f--]/g;
|
|
24
|
+
const bad = (message, field) => new HubError('bad_request', message, field);
|
|
25
|
+
function idsOf(input) {
|
|
26
|
+
if (!isRecord(input))
|
|
27
|
+
throw bad('request body must be a JSON object');
|
|
28
|
+
for (const key of Object.keys(input))
|
|
29
|
+
if (key !== 'ids')
|
|
30
|
+
throw bad(`unknown field "${key}"`, key);
|
|
31
|
+
const ids = input.ids;
|
|
32
|
+
if (!Array.isArray(ids) || ids.length === 0 || ids.length > DOWNLINK_IDS_MAX || !ids.every((id) => typeof id === 'string' && ID_RE.test(id))) {
|
|
33
|
+
throw bad(`ids must be a list of 1 to ${DOWNLINK_IDS_MAX} item ids`, 'ids');
|
|
34
|
+
}
|
|
35
|
+
return ids;
|
|
36
|
+
}
|
|
37
|
+
export function createDownlink(options) {
|
|
38
|
+
const { dir, now, log } = options;
|
|
39
|
+
const path = join(dir, DOWNLINK_FILE);
|
|
40
|
+
/** Both lists are oldest first. */
|
|
41
|
+
let mail = [];
|
|
42
|
+
let events = [];
|
|
43
|
+
/** No entry is unread. */
|
|
44
|
+
const flags = new Map();
|
|
45
|
+
let seq = 0;
|
|
46
|
+
let chain = Promise.resolve();
|
|
47
|
+
const later = (fn) => {
|
|
48
|
+
const run = chain.then(fn);
|
|
49
|
+
chain = run.catch((error) => log.error('downlink could not write', error));
|
|
50
|
+
return run.then(() => undefined);
|
|
51
|
+
};
|
|
52
|
+
const save = () => {
|
|
53
|
+
// the mail is not stored: messages.jsonl has it, the file keeps what the Director did with it
|
|
54
|
+
const json = JSON.stringify({ version: 1, seq, events, flags: Object.fromEntries(flags) }, null, 2);
|
|
55
|
+
return later(async () => {
|
|
56
|
+
const tmp = join(dir, `.${DOWNLINK_FILE}.${process.pid}.${randomBytes(4).toString('hex')}.tmp`);
|
|
57
|
+
await writeFile(tmp, json, { mode: FILE_MODE, flag: 'wx' });
|
|
58
|
+
await rename(tmp, path);
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
const view = () => {
|
|
62
|
+
const marks = (id) => ({ read: flags.has(id), archived: flags.get(id) === 'archived' });
|
|
63
|
+
// newest first: the lists are read from their ends, so that the sort (it is stable) leaves the later of two items of one millisecond first
|
|
64
|
+
const items = [
|
|
65
|
+
...mail.map((m) => ({ ...m, type: 'message', ...marks(m.id) })).reverse(),
|
|
66
|
+
...events.map((e) => ({ ...e, type: 'event', ...marks(e.id) })).reverse(),
|
|
67
|
+
];
|
|
68
|
+
items.sort((a, b) => b.at - a.at);
|
|
69
|
+
return { unread: items.filter((item) => !item.read).length, items };
|
|
70
|
+
};
|
|
71
|
+
/** Tells the browsers and saves; a failed save is logged by `later`, so a caller that does not wait may ignore it. */
|
|
72
|
+
const changed = () => {
|
|
73
|
+
options.emit(view());
|
|
74
|
+
return save();
|
|
75
|
+
};
|
|
76
|
+
const addMail = (record) => {
|
|
77
|
+
if (mail.some((m) => m.id === record.id))
|
|
78
|
+
return false;
|
|
79
|
+
mail.push(record);
|
|
80
|
+
for (const old of mail.splice(0, Math.max(0, mail.length - DOWNLINK_MAIL_MAX)))
|
|
81
|
+
flags.delete(old.id);
|
|
82
|
+
return true;
|
|
83
|
+
};
|
|
84
|
+
/** A record of the log, or an activity item whose body the hub cut already (`truncated`). */
|
|
85
|
+
const toMail = (r) => {
|
|
86
|
+
const body = cutText(r.body, ACTIVITY_WIRE_BODY_CHARS);
|
|
87
|
+
return { id: r.id, at: r.at, from: r.from, subject: r.subject, body, truncated: r.truncated === true || body.length < r.body.length, inReplyTo: r.inReplyTo };
|
|
88
|
+
};
|
|
89
|
+
const setFlag = async (flag, input) => {
|
|
90
|
+
const ids = idsOf(input);
|
|
91
|
+
const held = new Set([...mail, ...events].map((item) => item.id));
|
|
92
|
+
let any = false;
|
|
93
|
+
for (const id of ids) {
|
|
94
|
+
if (!held.has(id) || flags.get(id) === 'archived' || flags.get(id) === flag)
|
|
95
|
+
continue;
|
|
96
|
+
flags.set(id, flag);
|
|
97
|
+
any = true;
|
|
98
|
+
}
|
|
99
|
+
if (any)
|
|
100
|
+
await changed();
|
|
101
|
+
return view();
|
|
102
|
+
};
|
|
103
|
+
return {
|
|
104
|
+
async load() {
|
|
105
|
+
let saved = null;
|
|
106
|
+
let fresh = false;
|
|
107
|
+
try {
|
|
108
|
+
saved = JSON.parse(await readFile(path, 'utf8'));
|
|
109
|
+
if (!isRecord(saved) || saved.version !== 1 || !Array.isArray(saved.events) || !isRecord(saved.flags))
|
|
110
|
+
throw new Error('not a version 1 downlink file');
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
saved = null;
|
|
114
|
+
fresh = true;
|
|
115
|
+
if (error.code !== 'ENOENT') {
|
|
116
|
+
// kept aside, so that the next save cannot overwrite what the Director marked
|
|
117
|
+
log.error(`${DOWNLINK_FILE} could not be read; the inbox starts empty and the file is kept as ${DOWNLINK_FILE}.bad`, error instanceof SyntaxError ? 'not valid JSON' : error);
|
|
118
|
+
await rename(path, `${path}.bad`).catch(() => undefined);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (isRecord(saved)) {
|
|
122
|
+
if (typeof saved.seq === 'number' && Number.isSafeInteger(saved.seq) && saved.seq > 0)
|
|
123
|
+
seq = saved.seq;
|
|
124
|
+
for (const raw of Array.isArray(saved.events) ? saved.events : []) {
|
|
125
|
+
const { id, at, kind, text } = isRecord(raw) ? raw : {};
|
|
126
|
+
if (typeof id !== 'string' ||
|
|
127
|
+
!EVENT_ID_RE.test(id) ||
|
|
128
|
+
typeof at !== 'number' ||
|
|
129
|
+
typeof text !== 'string' ||
|
|
130
|
+
!DOWNLINK_EVENT_KINDS.includes(kind) ||
|
|
131
|
+
events.some((e) => e.id === id)) {
|
|
132
|
+
log.warn(`${DOWNLINK_FILE}: an event was skipped`);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
events.push({ id, at, kind: kind, text: cutText(text, DOWNLINK_TEXT_MAX_CHARS) });
|
|
136
|
+
seq = Math.max(seq, Number(id.slice(1)));
|
|
137
|
+
}
|
|
138
|
+
events = events.slice(-DOWNLINK_EVENTS_MAX);
|
|
139
|
+
}
|
|
140
|
+
for (const record of options.history())
|
|
141
|
+
if (record.to === 'user')
|
|
142
|
+
addMail(toMail(record));
|
|
143
|
+
if (isRecord(saved) && isRecord(saved.flags)) {
|
|
144
|
+
const held = new Set([...mail, ...events].map((item) => item.id));
|
|
145
|
+
for (const [id, flag] of Object.entries(saved.flags))
|
|
146
|
+
if (held.has(id) && (flag === 'read' || flag === 'archived'))
|
|
147
|
+
flags.set(id, flag);
|
|
148
|
+
}
|
|
149
|
+
if (fresh && mail.length > 0) {
|
|
150
|
+
// the first run: the mail that is already there was seen in the comm loop, so the badge does not start at 200
|
|
151
|
+
for (const m of mail)
|
|
152
|
+
flags.set(m.id, 'read');
|
|
153
|
+
await save();
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
view,
|
|
157
|
+
post(kind, text) {
|
|
158
|
+
const line = cutText(text.replace(UNSAFE_IN_TEXT, ' ').replace(/\s+/g, ' ').trim(), DOWNLINK_TEXT_MAX_CHARS);
|
|
159
|
+
if (line === '')
|
|
160
|
+
return;
|
|
161
|
+
seq += 1;
|
|
162
|
+
events.push({ id: `e${seq}`, at: now(), kind, text: line });
|
|
163
|
+
for (const old of events.splice(0, Math.max(0, events.length - DOWNLINK_EVENTS_MAX)))
|
|
164
|
+
flags.delete(old.id);
|
|
165
|
+
void changed().catch(() => undefined);
|
|
166
|
+
},
|
|
167
|
+
observe(event) {
|
|
168
|
+
if (event.t !== 'activity' || event.item.to !== 'user')
|
|
169
|
+
return;
|
|
170
|
+
if (addMail(toMail(event.item)))
|
|
171
|
+
options.emit(view());
|
|
172
|
+
},
|
|
173
|
+
read: (input) => setFlag('read', input),
|
|
174
|
+
archive: (input) => setFlag('archived', input),
|
|
175
|
+
async readAll() {
|
|
176
|
+
let any = false;
|
|
177
|
+
for (const item of [...mail, ...events]) {
|
|
178
|
+
if (flags.has(item.id))
|
|
179
|
+
continue;
|
|
180
|
+
flags.set(item.id, 'read');
|
|
181
|
+
any = true;
|
|
182
|
+
}
|
|
183
|
+
if (any)
|
|
184
|
+
await changed();
|
|
185
|
+
return view();
|
|
186
|
+
},
|
|
187
|
+
close() {
|
|
188
|
+
return chain.then(() => undefined);
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
}
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hangar (docs/hangar-spec.md): the files of one agent's folder, or of its Clean Room worktree, for the Director's browser.
|
|
3
|
+
* List a folder, read a text file, save one the Director opened (refused when it changed on disk since), make a new one. No
|
|
4
|
+
* delete, no rename. Every path is a relative one that is resolved with realpath and kept inside the root; `.git` and gonogo's
|
|
5
|
+
* own folder are never reachable. The agent runs as the same user, so this guards the browser's requests, not the agent.
|
|
6
|
+
* shortcut: a folder swapped for a link between the realpath and the open escapes the check (only the last name is opened with
|
|
7
|
+
* O_NOFOLLOW); whoever can swap it can write the file itself. Close it with openat if the agent is ever not the owner's user.
|
|
8
|
+
*/
|
|
9
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
10
|
+
import { constants as fs } from 'node:fs';
|
|
11
|
+
import { access, lstat, open, opendir, realpath, rename, unlink } from 'node:fs/promises';
|
|
12
|
+
import { isAbsolute, join, relative, sep } from 'node:path';
|
|
13
|
+
import { CLEAN_ROOM_DIR, HANGAR_FILE_MAX_BYTES } from '../shared/protocol.js';
|
|
14
|
+
import { HubError } from './hub.js';
|
|
15
|
+
/** A save body: the text, in the worst case a `\u0000` escape for each byte, plus the path and the etag. */
|
|
16
|
+
export const HANGAR_BODY_MAX_BYTES = 6 * HANGAR_FILE_MAX_BYTES + 8192;
|
|
17
|
+
/** A folder shows this many entries and says there are more. */
|
|
18
|
+
export const HANGAR_LIST_MAX = 2000;
|
|
19
|
+
const PATH_MAX_CHARS = 1024;
|
|
20
|
+
const ETAG_MAX_CHARS = 64;
|
|
21
|
+
/** git's own test for a binary file: a NUL in the first 8000 bytes. */
|
|
22
|
+
const SNIFF_BYTES = 8000;
|
|
23
|
+
const bad = (message, field) => new HubError('bad_request', message, field);
|
|
24
|
+
const forbidden = (message) => new HubError('forbidden', message, 'path');
|
|
25
|
+
/** `child` is `parent` or below it. */
|
|
26
|
+
export function inside(parent, child) {
|
|
27
|
+
const r = relative(parent, child);
|
|
28
|
+
return r === '' || (r !== '..' && !r.startsWith(`..${sep}`) && !isAbsolute(r));
|
|
29
|
+
}
|
|
30
|
+
/** A name that a file system may take for `.git`: other case, a zero-width or ignorable character in it (HFS+ ignores those). */
|
|
31
|
+
export function isGitName(segment) {
|
|
32
|
+
return segment.normalize('NFKC').replace(/[---]/g, '').toLowerCase() === '.git';
|
|
33
|
+
}
|
|
34
|
+
/** The segments of a relative path from the browser. Anything that could leave the root, or reach `.git`, is refused here. */
|
|
35
|
+
export function pathParts(raw, field = 'path') {
|
|
36
|
+
if (typeof raw !== 'string')
|
|
37
|
+
throw bad(`${field} must be a string`, field);
|
|
38
|
+
if (raw.length > PATH_MAX_CHARS)
|
|
39
|
+
throw bad(`${field} is longer than ${PATH_MAX_CHARS} characters`, field);
|
|
40
|
+
if (raw.includes('\0'))
|
|
41
|
+
throw bad(`${field} has a NUL character`, field);
|
|
42
|
+
if (raw.startsWith('/'))
|
|
43
|
+
throw forbidden('the path must be relative to the agent\'s folder');
|
|
44
|
+
const parts = raw.split('/').filter((p) => p !== '' && p !== '.');
|
|
45
|
+
if (parts.includes('..'))
|
|
46
|
+
throw forbidden('a path cannot go up with ..');
|
|
47
|
+
if (parts.some(isGitName))
|
|
48
|
+
throw forbidden('.git is not reachable');
|
|
49
|
+
return parts;
|
|
50
|
+
}
|
|
51
|
+
/** Turns what a file system call said into an answer; anything else is the server's own fault and goes up as it is. */
|
|
52
|
+
function fsError(e) {
|
|
53
|
+
const code = e?.code;
|
|
54
|
+
if (code === 'ENOENT')
|
|
55
|
+
return new HubError('not_found', 'no such file or folder', 'path');
|
|
56
|
+
if (code === 'ENOTDIR')
|
|
57
|
+
return bad('a part of the path is not a folder', 'path');
|
|
58
|
+
if (code === 'EISDIR')
|
|
59
|
+
return bad('that is a folder', 'path');
|
|
60
|
+
if (code === 'EEXIST')
|
|
61
|
+
return new HubError('conflict', 'a file with that name exists', 'path');
|
|
62
|
+
if (code === 'EACCES' || code === 'EPERM')
|
|
63
|
+
return forbidden('permission denied');
|
|
64
|
+
if (code === 'ELOOP')
|
|
65
|
+
return forbidden('a symbolic link is not followed there');
|
|
66
|
+
if (code === 'ENAMETOOLONG')
|
|
67
|
+
return bad('a name in the path is too long', 'path');
|
|
68
|
+
return e;
|
|
69
|
+
}
|
|
70
|
+
const etagOf = (bytes) => createHash('sha256').update(bytes).digest('hex').slice(0, 32);
|
|
71
|
+
/** At most `max` bytes from the start; a file that grows while it is read cannot make this large. */
|
|
72
|
+
async function readUpTo(handle, max) {
|
|
73
|
+
const buffer = Buffer.alloc(max);
|
|
74
|
+
let size = 0;
|
|
75
|
+
while (size < max) {
|
|
76
|
+
const { bytesRead } = await handle.read(buffer, size, max - size, size);
|
|
77
|
+
if (bytesRead === 0)
|
|
78
|
+
break;
|
|
79
|
+
size += bytesRead;
|
|
80
|
+
}
|
|
81
|
+
return buffer.subarray(0, size);
|
|
82
|
+
}
|
|
83
|
+
async function writeAll(handle, bytes) {
|
|
84
|
+
let at = 0;
|
|
85
|
+
while (at < bytes.length)
|
|
86
|
+
at += (await handle.write(bytes, at, bytes.length - at, at)).bytesWritten;
|
|
87
|
+
}
|
|
88
|
+
/** Text the editor may hold: valid UTF-8 without a NUL. A byte order mark stays in the text, so a save writes it back. */
|
|
89
|
+
function decode(bytes) {
|
|
90
|
+
if (bytes.subarray(0, SNIFF_BYTES).includes(0))
|
|
91
|
+
throw new HubError('unsupported', 'a binary file: the editor opens text only', 'path');
|
|
92
|
+
try {
|
|
93
|
+
return new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(bytes);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
throw new HubError('unsupported', 'not valid UTF-8: the editor opens text only', 'path');
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function textOf(input, required) {
|
|
100
|
+
const { text } = input;
|
|
101
|
+
if (text === undefined && !required)
|
|
102
|
+
return Buffer.alloc(0);
|
|
103
|
+
if (typeof text !== 'string')
|
|
104
|
+
throw bad('text must be a string', 'text');
|
|
105
|
+
if (text.includes('\0'))
|
|
106
|
+
throw bad('text has a NUL character: the editor holds text only', 'text');
|
|
107
|
+
const bytes = Buffer.from(text, 'utf8');
|
|
108
|
+
if (bytes.length > HANGAR_FILE_MAX_BYTES)
|
|
109
|
+
throw new HubError('too_large', `the file would be ${bytes.length} bytes; the limit is ${HANGAR_FILE_MAX_BYTES}`, 'text');
|
|
110
|
+
return bytes;
|
|
111
|
+
}
|
|
112
|
+
const isInput = (x) => typeof x === 'object' && x !== null && !Array.isArray(x);
|
|
113
|
+
/** Where an agent's files are: its Clean Room's worktree, else its folder. The home folder is resolved too, to keep gonogo's own state out of reach. */
|
|
114
|
+
export function createRoots(deps) {
|
|
115
|
+
return async (name) => {
|
|
116
|
+
const place = deps.agent(name);
|
|
117
|
+
if (place === null)
|
|
118
|
+
throw new HubError('not_found', 'unknown agent');
|
|
119
|
+
const wanted = place.cleanRoom ? join(deps.home, CLEAN_ROOM_DIR, name) : place.dir;
|
|
120
|
+
try {
|
|
121
|
+
return { root: await realpath(wanted), home: await realpath(deps.home) };
|
|
122
|
+
}
|
|
123
|
+
catch (e) {
|
|
124
|
+
if (e.code === 'ENOENT') {
|
|
125
|
+
throw new HubError('not_found', place.cleanRoom ? 'the Clean Room has no worktree yet: start the agent once' : 'the agent\'s folder is gone');
|
|
126
|
+
}
|
|
127
|
+
throw e;
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
export function createHangar(roots) {
|
|
132
|
+
/** Saves of one file run one after the other: two browsers saving with the same etag at once would both pass the check, and the first text would be lost. */
|
|
133
|
+
const saving = new Map();
|
|
134
|
+
async function serial(key, work) {
|
|
135
|
+
const run = (saving.get(key) ?? Promise.resolve()).then(work);
|
|
136
|
+
const tail = run.then(() => undefined, () => undefined);
|
|
137
|
+
saving.set(key, tail);
|
|
138
|
+
try {
|
|
139
|
+
return await run;
|
|
140
|
+
}
|
|
141
|
+
finally {
|
|
142
|
+
if (saving.get(key) === tail)
|
|
143
|
+
saving.delete(key);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/** gonogo's home is out of reach, except the Clean Room worktrees below it (an agent's own files). */
|
|
147
|
+
const ownState = (at, real) => inside(at.home, real) && !inside(join(at.home, CLEAN_ROOM_DIR), real);
|
|
148
|
+
const guard = (at, real) => {
|
|
149
|
+
if (!inside(at.root, real))
|
|
150
|
+
throw forbidden('that path leads outside the agent\'s folder');
|
|
151
|
+
if (relative(at.root, real).split(sep).some(isGitName))
|
|
152
|
+
throw forbidden('.git is not reachable');
|
|
153
|
+
if (ownState(at, real))
|
|
154
|
+
throw forbidden('that is gonogo\'s own folder');
|
|
155
|
+
};
|
|
156
|
+
async function resolveIn(at, parts) {
|
|
157
|
+
let real;
|
|
158
|
+
try {
|
|
159
|
+
real = await realpath(join(at.root, ...parts));
|
|
160
|
+
}
|
|
161
|
+
catch (e) {
|
|
162
|
+
throw fsError(e);
|
|
163
|
+
}
|
|
164
|
+
guard(at, real);
|
|
165
|
+
return real;
|
|
166
|
+
}
|
|
167
|
+
/** Opens without following a link in the last name and without waiting on a pipe; what is opened must be a regular file. */
|
|
168
|
+
async function openFile(real, flags) {
|
|
169
|
+
let handle;
|
|
170
|
+
try {
|
|
171
|
+
handle = await open(real, flags | fs.O_NOFOLLOW | fs.O_NONBLOCK);
|
|
172
|
+
}
|
|
173
|
+
catch (e) {
|
|
174
|
+
throw fsError(e);
|
|
175
|
+
}
|
|
176
|
+
try {
|
|
177
|
+
const info = await handle.stat();
|
|
178
|
+
if (!info.isFile())
|
|
179
|
+
throw new HubError('unsupported', 'not a regular file', 'path');
|
|
180
|
+
return { handle, info };
|
|
181
|
+
}
|
|
182
|
+
catch (e) {
|
|
183
|
+
await handle.close();
|
|
184
|
+
throw e;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/** The file `real` takes `bytes` when it still has the etag `etag`: written whole beside it, then renamed over it. */
|
|
188
|
+
async function replace(real, leaf, bytes, etag) {
|
|
189
|
+
const { handle, info } = await openFile(real, fs.O_RDONLY);
|
|
190
|
+
let temp = null;
|
|
191
|
+
try {
|
|
192
|
+
// the file is replaced by a new one, which a read-only file must not allow
|
|
193
|
+
await access(real, fs.W_OK).catch((e) => {
|
|
194
|
+
throw fsError(e);
|
|
195
|
+
});
|
|
196
|
+
const now = info.size > HANGAR_FILE_MAX_BYTES ? null : await readUpTo(handle, HANGAR_FILE_MAX_BYTES + 1);
|
|
197
|
+
if (now === null || etagOf(now) !== etag)
|
|
198
|
+
throw new HubError('conflict', 'the file changed on disk since you opened it: nothing was saved', 'etag');
|
|
199
|
+
// Written whole beside the file and renamed over it, so that a full disk or a crash leaves the old file.
|
|
200
|
+
temp = join(real, '..', `.${leaf}.gonogo-${randomBytes(4).toString('hex')}.tmp`);
|
|
201
|
+
const out = await open(temp, 'wx', 0o600).catch((e) => {
|
|
202
|
+
temp = null;
|
|
203
|
+
throw fsError(e);
|
|
204
|
+
});
|
|
205
|
+
try {
|
|
206
|
+
await writeAll(out, bytes);
|
|
207
|
+
await out.chmod(info.mode & 0o7777);
|
|
208
|
+
}
|
|
209
|
+
finally {
|
|
210
|
+
await out.close();
|
|
211
|
+
}
|
|
212
|
+
// shortcut: a write by another process between this check and the rename is lost; take a lock file when two writers matter
|
|
213
|
+
const after = await lstat(real);
|
|
214
|
+
if (after.mtimeMs !== info.mtimeMs || after.size !== info.size)
|
|
215
|
+
throw new HubError('conflict', 'the file changed on disk while it was saved: nothing was saved', 'etag');
|
|
216
|
+
await rename(temp, real);
|
|
217
|
+
temp = null;
|
|
218
|
+
}
|
|
219
|
+
finally {
|
|
220
|
+
await handle.close();
|
|
221
|
+
if (temp !== null)
|
|
222
|
+
await unlink(temp).catch(() => undefined);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const filePath = (raw) => {
|
|
226
|
+
const parts = pathParts(raw);
|
|
227
|
+
if (parts.length === 0)
|
|
228
|
+
throw bad('path must name a file', 'path');
|
|
229
|
+
return parts;
|
|
230
|
+
};
|
|
231
|
+
return {
|
|
232
|
+
async list(name, raw) {
|
|
233
|
+
const parts = pathParts(raw ?? '');
|
|
234
|
+
const at = await roots(name);
|
|
235
|
+
const dir = await resolveIn(at, parts);
|
|
236
|
+
const entries = [];
|
|
237
|
+
let truncated = false;
|
|
238
|
+
try {
|
|
239
|
+
// shortcut: past the limit the entries are the first the disk gave, not the first by name; sort a whole folder when one needs it
|
|
240
|
+
for await (const item of await opendir(dir)) {
|
|
241
|
+
if (isGitName(item.name) || ownState(at, join(dir, item.name)))
|
|
242
|
+
continue;
|
|
243
|
+
if (entries.length >= HANGAR_LIST_MAX) {
|
|
244
|
+
truncated = true;
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
const kind = item.isDirectory() ? 'dir' : item.isSymbolicLink() ? 'link' : item.isFile() ? 'file' : null;
|
|
248
|
+
if (kind !== null)
|
|
249
|
+
entries.push({ name: item.name, kind, size: kind === 'file' ? (await lstat(join(dir, item.name)).catch(() => null))?.size ?? 0 : 0 });
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
catch (e) {
|
|
253
|
+
throw fsError(e);
|
|
254
|
+
}
|
|
255
|
+
entries.sort((a, b) => (a.kind === 'dir' ? 0 : 1) - (b.kind === 'dir' ? 0 : 1) || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
256
|
+
return { path: parts.join('/'), entries, truncated };
|
|
257
|
+
},
|
|
258
|
+
async read(name, raw) {
|
|
259
|
+
const parts = filePath(raw);
|
|
260
|
+
const real = await resolveIn(await roots(name), parts);
|
|
261
|
+
const { handle, info } = await openFile(real, fs.O_RDONLY);
|
|
262
|
+
try {
|
|
263
|
+
if (info.size > HANGAR_FILE_MAX_BYTES)
|
|
264
|
+
throw new HubError('too_large', `the file has ${info.size} bytes; the editor opens at most ${HANGAR_FILE_MAX_BYTES}`, 'path');
|
|
265
|
+
const bytes = await readUpTo(handle, HANGAR_FILE_MAX_BYTES + 1);
|
|
266
|
+
if (bytes.length > HANGAR_FILE_MAX_BYTES)
|
|
267
|
+
throw new HubError('too_large', `the file is larger than ${HANGAR_FILE_MAX_BYTES} bytes`, 'path');
|
|
268
|
+
return { path: parts.join('/'), text: decode(bytes), size: bytes.length, etag: etagOf(bytes) };
|
|
269
|
+
}
|
|
270
|
+
finally {
|
|
271
|
+
await handle.close();
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
async save(name, input) {
|
|
275
|
+
if (!isInput(input))
|
|
276
|
+
throw bad('the body must be an object');
|
|
277
|
+
const parts = filePath(input.path);
|
|
278
|
+
const { etag } = input;
|
|
279
|
+
if (typeof etag !== 'string' || etag === '' || etag.length > ETAG_MAX_CHARS)
|
|
280
|
+
throw bad('etag must be the one the file was read with', 'etag');
|
|
281
|
+
const bytes = textOf(input, true);
|
|
282
|
+
const real = await resolveIn(await roots(name), parts);
|
|
283
|
+
await serial(real, () => replace(real, parts[parts.length - 1] ?? 'file', bytes, etag));
|
|
284
|
+
return { path: parts.join('/'), size: bytes.length, etag: etagOf(bytes) };
|
|
285
|
+
},
|
|
286
|
+
async create(name, input) {
|
|
287
|
+
if (!isInput(input))
|
|
288
|
+
throw bad('the body must be an object');
|
|
289
|
+
const parts = filePath(input.path);
|
|
290
|
+
const bytes = textOf(input, false);
|
|
291
|
+
const at = await roots(name);
|
|
292
|
+
const folder = await resolveIn(at, parts.slice(0, -1));
|
|
293
|
+
const leaf = parts[parts.length - 1] ?? '';
|
|
294
|
+
let handle;
|
|
295
|
+
try {
|
|
296
|
+
// O_EXCL: the name must be new, and a link by that name is never followed.
|
|
297
|
+
handle = await open(join(folder, leaf), 'wx', 0o644);
|
|
298
|
+
}
|
|
299
|
+
catch (e) {
|
|
300
|
+
throw fsError(e);
|
|
301
|
+
}
|
|
302
|
+
try {
|
|
303
|
+
await writeAll(handle, bytes);
|
|
304
|
+
}
|
|
305
|
+
finally {
|
|
306
|
+
await handle.close();
|
|
307
|
+
}
|
|
308
|
+
return { path: parts.join('/'), size: bytes.length, etag: etagOf(bytes) };
|
|
309
|
+
},
|
|
310
|
+
};
|
|
311
|
+
}
|