@ulm0/gonogo 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/assets/index-DxUnpGbS.js +52 -0
- package/dist/client/assets/index-tpcmltjw.css +1 -0
- package/dist/client/index.html +2 -2
- package/dist/server/audited.js +26 -3
- package/dist/server/cleanroom.js +7 -51
- package/dist/server/cli.js +87 -19
- package/dist/server/constants.js +15 -2
- package/dist/server/hangar.js +4 -4
- package/dist/server/home.js +17 -1
- package/dist/server/hub.js +102 -9
- package/dist/server/launcher.js +17 -10
- package/dist/server/mcp.js +41 -2
- package/dist/server/migrate.js +2 -1
- package/dist/server/missions.js +310 -0
- package/dist/server/models.js +73 -0
- package/dist/server/phone.js +122 -0
- package/dist/server/phonetag.js +47 -0
- package/dist/server/prices.js +112 -0
- package/dist/server/propellant.js +243 -16
- package/dist/server/providers.js +52 -0
- package/dist/server/relay.js +53 -7
- package/dist/server/relaykinds.js +11 -1
- package/dist/server/relief.js +2 -2
- package/dist/server/remotecontrol.js +15 -0
- package/dist/server/tasks.js +44 -8
- package/dist/server/uplinks.js +7 -0
- package/dist/server/web.js +165 -6
- package/dist/server/worktree.js +137 -0
- package/dist/shared/fuel.js +8 -0
- package/dist/shared/prices.js +75 -0
- package/dist/shared/protocol.js +16 -3
- package/package.json +1 -1
- package/dist/client/assets/index-BqCn1SM7.js +0 -50
- package/dist/client/assets/index-KZtFVMJG.css +0 -1
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phone approvals, Path A (docs/phone-approvals-spec.md): a pending permission or hire request goes to the Director's Telegram chat
|
|
3
|
+
* with buttons, and a press comes back through the Telegram uplink as a signed `callback_query`.
|
|
4
|
+
*
|
|
5
|
+
* Buttons carry `a:<kind>:<id>:<verdict>:<tag>` (phonetag.ts). For a permission the id is a short handle (`p7`) of this process's
|
|
6
|
+
* map to the agent and the dialog number (`needSeq`); the answer is `Hub.answer(name, { go, needSeq })`, so a press that comes
|
|
7
|
+
* late, for a dialog that was answered or replaced, does nothing. For a hire request the id is the Relief id, and only NO-GO and
|
|
8
|
+
* Open are offered: the role text is not shown on a phone, so GO is the page's (docs/relief-spec.md). A dialog or a request whose
|
|
9
|
+
* text was cut also gets NO-GO and Open only.
|
|
10
|
+
*
|
|
11
|
+
* Every id starts with a nonce made at start: the signing secret survives a restart but the handles do not, so without it an old
|
|
12
|
+
* "GO p1" would approve the new dialog p1. A button of before a restart answers "too late".
|
|
13
|
+
* shortcut: the handles are in memory and bounded.
|
|
14
|
+
* shortcut: Telegram does not say who pressed beyond the chat; the chat of the target is the Director's, as for messages.
|
|
15
|
+
*/
|
|
16
|
+
import { randomBytes } from 'node:crypto';
|
|
17
|
+
import { callbackData, verifyCallback } from './phonetag.js';
|
|
18
|
+
/** Presses taken in a minute: more are told "slow down". */
|
|
19
|
+
export const PHONE_PRESS_MAX = 10;
|
|
20
|
+
const PRESS_WINDOW_MS = 60_000;
|
|
21
|
+
const HANDLES_MAX = 200;
|
|
22
|
+
const OPEN_NOTE = 'Open gonogo to read it and answer';
|
|
23
|
+
const PERMISSION_CUT = '…';
|
|
24
|
+
export function createPhone(options) {
|
|
25
|
+
const { hub, relief, relay, log } = options;
|
|
26
|
+
/** 4 hex characters, new for each process: part of every id that is signed. */
|
|
27
|
+
const nonce = options.nonce?.() ?? randomBytes(2).toString('hex');
|
|
28
|
+
const handles = new Map();
|
|
29
|
+
let handleSeq = 0;
|
|
30
|
+
let taken = [];
|
|
31
|
+
const reply = (press, text) => relay.telegramCall('answerCallbackQuery', { callback_query_id: press.queryId, text });
|
|
32
|
+
const clear = (press) => press.messageId === '' ? Promise.resolve(false) : relay.telegramCall('editMessageReplyMarkup', { message_id: Number(press.messageId), reply_markup: { inline_keyboard: [] } });
|
|
33
|
+
return {
|
|
34
|
+
approval(ref, text) {
|
|
35
|
+
if (!relay.phoneTelegram())
|
|
36
|
+
return;
|
|
37
|
+
const secret = options.secret();
|
|
38
|
+
if (secret === null)
|
|
39
|
+
return;
|
|
40
|
+
const cut = text.endsWith(PERMISSION_CUT);
|
|
41
|
+
let id;
|
|
42
|
+
let verdicts;
|
|
43
|
+
let shown = text;
|
|
44
|
+
if (ref.kind === 'permission') {
|
|
45
|
+
handleSeq += 1;
|
|
46
|
+
id = `p${nonce}${handleSeq}`;
|
|
47
|
+
handles.set(id, { agent: ref.agent, seq: ref.seq });
|
|
48
|
+
if (handles.size > HANDLES_MAX)
|
|
49
|
+
handles.delete(handles.keys().next().value);
|
|
50
|
+
verdicts = cut ? ['nogo', 'open'] : ['go', 'nogo'];
|
|
51
|
+
if (cut)
|
|
52
|
+
shown = `${text}\n(truncated: open gonogo to see all of it)`;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
id = `r${nonce}${ref.id}`;
|
|
56
|
+
verdicts = ['nogo', 'open'];
|
|
57
|
+
shown = `${text}\n(role text: open gonogo to read it)`;
|
|
58
|
+
}
|
|
59
|
+
const label = { go: 'GO', nogo: 'NO-GO', open: 'Open' };
|
|
60
|
+
const buttons = verdicts.flatMap((verdict) => {
|
|
61
|
+
const data = callbackData(secret, { kind: ref.kind, id, verdict });
|
|
62
|
+
return data === null ? [] : [{ text: label[verdict], callback_data: data }];
|
|
63
|
+
});
|
|
64
|
+
if (buttons.length === 0)
|
|
65
|
+
return;
|
|
66
|
+
void relay.sendButtons(shown, { inline_keyboard: [buttons] }).catch(() => undefined);
|
|
67
|
+
},
|
|
68
|
+
async press(secret, press) {
|
|
69
|
+
const t = options.now();
|
|
70
|
+
// a bad tag counts too: somebody guessing tags must not have more than the limit of tries
|
|
71
|
+
taken = taken.filter((at) => t - at < PRESS_WINDOW_MS);
|
|
72
|
+
if (taken.length >= PHONE_PRESS_MAX) {
|
|
73
|
+
await reply(press, 'Too many presses: wait a minute');
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
taken.push(t);
|
|
77
|
+
// the Director may have turned the path off since the button was sent
|
|
78
|
+
if (!relay.phoneTelegram()) {
|
|
79
|
+
await reply(press, 'Phone approvals are off');
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const call = verifyCallback(secret, press.data);
|
|
83
|
+
if (call === null) {
|
|
84
|
+
log.debug('phone: a button with a wrong tag');
|
|
85
|
+
await reply(press, 'Invalid button');
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (call.verdict === 'open') {
|
|
89
|
+
await reply(press, OPEN_NOTE);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const go = call.verdict === 'go';
|
|
93
|
+
try {
|
|
94
|
+
if (call.kind === 'permission') {
|
|
95
|
+
const found = handles.get(call.id);
|
|
96
|
+
if (found === undefined)
|
|
97
|
+
throw new Error('too late: that dialog is gone');
|
|
98
|
+
hub.answer(found.agent, { go, needSeq: found.seq });
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
// GO is never offered for a hire request; a signed one that was made by hand is refused all the same
|
|
102
|
+
if (go)
|
|
103
|
+
throw new Error('hire requests are approved in gonogo');
|
|
104
|
+
// r<nonce><id>: a button of another process is a request that is gone
|
|
105
|
+
if (!call.id.startsWith(`r${nonce}`))
|
|
106
|
+
throw new Error('too late: that request is gone');
|
|
107
|
+
await relief.decide(call.id.slice(1 + nonce.length), 'no-go', { kind: 'user' });
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
const why = error instanceof Error ? error.message : String(error);
|
|
112
|
+
log.debug(`phone: ${call.kind} ${call.id}: ${why}`);
|
|
113
|
+
await reply(press, why.startsWith('too late') || /answered or replaced|not asking|no such hire/.test(why) ? 'Too late: already answered' : cutWhy(why));
|
|
114
|
+
await clear(press);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
await reply(press, go ? 'Sent GO' : 'Sent NO-GO');
|
|
118
|
+
await clear(press);
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
const cutWhy = (why) => (why.length > 150 ? `${why.slice(0, 149)}…` : why);
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phone approvals: the signed `callback_data` of a Telegram button (docs/phone-approvals-spec.md, Path A).
|
|
3
|
+
*
|
|
4
|
+
* `a:<kind>:<id>:<verdict>:<tag>` fits Telegram's 64 bytes. `<tag>` is the first 8 hex characters (32 bits) of
|
|
5
|
+
* HMAC-SHA256(secret, "kind:id:verdict"): it binds a button to one request and one verdict, so an edited or forwarded button
|
|
6
|
+
* cannot approve something else. Pure: no clock, no network, no files; the secret is the Telegram uplink's, handed in.
|
|
7
|
+
*
|
|
8
|
+
* shortcut: 32 bits are enough only because the uplink limits callbacks to 10 a minute and a request lives minutes;
|
|
9
|
+
* lengthen the tag (there is room up to 64 bytes) if that ever stops being true.
|
|
10
|
+
*/
|
|
11
|
+
import { createHash, createHmac, timingSafeEqual } from 'node:crypto';
|
|
12
|
+
export const PHONE_KINDS = ['permission', 'relief'];
|
|
13
|
+
export const PHONE_VERDICTS = ['go', 'nogo', 'open'];
|
|
14
|
+
/** An id as the hub makes them: the needs_you sequence number or a Relief id. */
|
|
15
|
+
const ID_RE = /^[A-Za-z0-9_-]{1,24}$/;
|
|
16
|
+
const TAG_RE = /^[0-9a-f]{8}$/;
|
|
17
|
+
const TELEGRAM_CALLBACK_MAX_BYTES = 64;
|
|
18
|
+
const digest = (text) => createHash('sha256').update(text, 'utf8').digest();
|
|
19
|
+
const same = (a, b) => timingSafeEqual(digest(a), digest(b));
|
|
20
|
+
/** The tag of one button; null when the parts are not valid (so nothing odd is ever signed). */
|
|
21
|
+
export function callbackTag(secret, call) {
|
|
22
|
+
if (secret === '' || !ID_RE.test(call.id) || !PHONE_KINDS.includes(call.kind) || !PHONE_VERDICTS.includes(call.verdict))
|
|
23
|
+
return null;
|
|
24
|
+
return createHmac('sha256', secret).update(`${call.kind}:${call.id}:${call.verdict}`).digest('hex').slice(0, 8);
|
|
25
|
+
}
|
|
26
|
+
/** The `callback_data` of a button, or null when it would not be valid or would not fit. */
|
|
27
|
+
export function callbackData(secret, call) {
|
|
28
|
+
const tag = callbackTag(secret, call);
|
|
29
|
+
if (tag === null)
|
|
30
|
+
return null;
|
|
31
|
+
const data = `a:${call.kind}:${call.id}:${call.verdict}:${tag}`;
|
|
32
|
+
return Buffer.byteLength(data, 'utf8') <= TELEGRAM_CALLBACK_MAX_BYTES ? data : null;
|
|
33
|
+
}
|
|
34
|
+
/** What a button says if, and only if, its tag is right for this secret; anything else is null. Constant-time compare. */
|
|
35
|
+
export function verifyCallback(secret, data) {
|
|
36
|
+
if (typeof data !== 'string' || Buffer.byteLength(data, 'utf8') > TELEGRAM_CALLBACK_MAX_BYTES)
|
|
37
|
+
return null;
|
|
38
|
+
const parts = data.split(':');
|
|
39
|
+
if (parts.length !== 5 || parts[0] !== 'a')
|
|
40
|
+
return null;
|
|
41
|
+
const [, kind, id, verdict, tag] = parts;
|
|
42
|
+
if (!PHONE_KINDS.includes(kind) || !PHONE_VERDICTS.includes(verdict) || !TAG_RE.test(tag))
|
|
43
|
+
return null;
|
|
44
|
+
const call = { kind, id, verdict };
|
|
45
|
+
const expected = callbackTag(secret, call);
|
|
46
|
+
return expected !== null && same(tag, expected) ? call : null;
|
|
47
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The owner's model prices: `prices.json` in gonogo's home, over the defaults of src/shared/prices.ts (docs/cost-spec.md).
|
|
3
|
+
* Local and editable; nothing is fetched. `currency` is a label only, there is no conversion.
|
|
4
|
+
*/
|
|
5
|
+
import { randomBytes } from 'node:crypto';
|
|
6
|
+
import { readFile, rename, writeFile } from 'node:fs/promises';
|
|
7
|
+
import { join } from 'node:path';
|
|
8
|
+
import { mergePrices, PRICE_MAX, PRICE_PREFIX_PATTERN } from '../shared/prices.js';
|
|
9
|
+
import { FILE_MODE, PRICES_FILE } from './constants.js';
|
|
10
|
+
const PREFIX_RE = new RegExp(PRICE_PREFIX_PATTERN);
|
|
11
|
+
const PRICE_KEYS = ['input', 'output', 'cacheWrite', 'cacheRead'];
|
|
12
|
+
function isRecord(value) {
|
|
13
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
14
|
+
}
|
|
15
|
+
/** One price from untrusted JSON: exactly the four numbers, each from 0 to PRICE_MAX; anything else is a message. */
|
|
16
|
+
export function parsePrice(value, what) {
|
|
17
|
+
if (!isRecord(value))
|
|
18
|
+
return `${what} must be an object with ${PRICE_KEYS.join(', ')}`;
|
|
19
|
+
for (const key of Object.keys(value))
|
|
20
|
+
if (!PRICE_KEYS.includes(key))
|
|
21
|
+
return `${what}: unknown field "${key}"`;
|
|
22
|
+
const price = {};
|
|
23
|
+
for (const key of PRICE_KEYS) {
|
|
24
|
+
const v = value[key];
|
|
25
|
+
if (typeof v !== 'number' || !Number.isFinite(v) || v < 0 || v > PRICE_MAX)
|
|
26
|
+
return `${what}.${key} must be a number of dollars per million tokens from 0 to ${PRICE_MAX}`;
|
|
27
|
+
price[key] = v;
|
|
28
|
+
}
|
|
29
|
+
return price;
|
|
30
|
+
}
|
|
31
|
+
/** The body of PUT /api/prices: `{ models: { <prefix>: price } }` (and `currency`, a label). Strict: unknown fields are refused. */
|
|
32
|
+
export function parsePricesRequest(body) {
|
|
33
|
+
for (const key of Object.keys(body)) {
|
|
34
|
+
if (key !== 'models' && key !== 'currency' && key !== 'version')
|
|
35
|
+
return { ok: false, error: `unknown field "${key}"`, field: key };
|
|
36
|
+
}
|
|
37
|
+
if (body.currency !== undefined && (typeof body.currency !== 'string' || !/^[A-Za-z]{3}$/.test(body.currency))) {
|
|
38
|
+
return { ok: false, error: 'currency must be a three-letter label such as USD', field: 'currency' };
|
|
39
|
+
}
|
|
40
|
+
if (!isRecord(body.models))
|
|
41
|
+
return { ok: false, error: 'models must be an object of model prefixes', field: 'models' };
|
|
42
|
+
const models = {};
|
|
43
|
+
for (const [prefix, raw] of Object.entries(body.models)) {
|
|
44
|
+
if (!PREFIX_RE.test(prefix))
|
|
45
|
+
return { ok: false, error: `models: invalid prefix "${prefix}"`, field: 'models' };
|
|
46
|
+
const price = parsePrice(raw, `models.${prefix}`);
|
|
47
|
+
if (typeof price === 'string')
|
|
48
|
+
return { ok: false, error: price, field: 'models' };
|
|
49
|
+
models[prefix] = price;
|
|
50
|
+
}
|
|
51
|
+
return { ok: true, models };
|
|
52
|
+
}
|
|
53
|
+
export function createPrices(options) {
|
|
54
|
+
const { dir, log } = options;
|
|
55
|
+
const path = join(dir, PRICES_FILE);
|
|
56
|
+
let file = {};
|
|
57
|
+
let currency = 'USD';
|
|
58
|
+
let merged = mergePrices(file);
|
|
59
|
+
let chain = Promise.resolve();
|
|
60
|
+
const save = () => {
|
|
61
|
+
const json = JSON.stringify({ version: 1, currency, models: file }, null, 2);
|
|
62
|
+
const run = chain.then(async () => {
|
|
63
|
+
const tmp = join(dir, `.${PRICES_FILE}.${process.pid}.${randomBytes(4).toString('hex')}.tmp`);
|
|
64
|
+
await writeFile(tmp, json, { mode: FILE_MODE, flag: 'wx' });
|
|
65
|
+
await rename(tmp, path);
|
|
66
|
+
});
|
|
67
|
+
chain = run.catch((error) => log.error('prices could not write', error));
|
|
68
|
+
return run;
|
|
69
|
+
};
|
|
70
|
+
const view = () => ({ currency, rows: merged });
|
|
71
|
+
return {
|
|
72
|
+
async load() {
|
|
73
|
+
try {
|
|
74
|
+
const data = JSON.parse(await readFile(path, 'utf8'));
|
|
75
|
+
if (isRecord(data)) {
|
|
76
|
+
const parsed = parsePricesRequest({ models: data.models ?? {}, ...(typeof data.currency === 'string' ? { currency: data.currency } : {}) });
|
|
77
|
+
if (parsed.ok) {
|
|
78
|
+
file = parsed.models;
|
|
79
|
+
if (typeof data.currency === 'string')
|
|
80
|
+
currency = data.currency;
|
|
81
|
+
}
|
|
82
|
+
else
|
|
83
|
+
log.warn(`${PRICES_FILE} is not valid (${parsed.error}); the default prices are used`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
if (error.code !== 'ENOENT')
|
|
88
|
+
log.warn(`${PRICES_FILE} could not be read; the default prices are used`, error);
|
|
89
|
+
}
|
|
90
|
+
merged = mergePrices(file);
|
|
91
|
+
},
|
|
92
|
+
table: () => merged,
|
|
93
|
+
view,
|
|
94
|
+
async replace(models, label) {
|
|
95
|
+
file = { ...models };
|
|
96
|
+
if (label !== undefined)
|
|
97
|
+
currency = label.toUpperCase();
|
|
98
|
+
merged = mergePrices(file);
|
|
99
|
+
await save();
|
|
100
|
+
return view();
|
|
101
|
+
},
|
|
102
|
+
async remove(prefix) {
|
|
103
|
+
if (!(prefix in file))
|
|
104
|
+
return false;
|
|
105
|
+
delete file[prefix];
|
|
106
|
+
merged = mergePrices(file);
|
|
107
|
+
await save();
|
|
108
|
+
return true;
|
|
109
|
+
},
|
|
110
|
+
close: () => chain.then(() => undefined),
|
|
111
|
+
};
|
|
112
|
+
}
|