@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,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Payload (docs/payload-spec.md): the one reader of an agent's MCP servers and skill folders. The hub uses it for hire
|
|
3
|
+
* and edit, home.ts for the roster file. It looks at the shape only; whether a skill folder exists is the hub's and the
|
|
4
|
+
* launcher's to check.
|
|
5
|
+
*/
|
|
6
|
+
import { isAbsolute, basename, resolve } from 'node:path';
|
|
7
|
+
import { MCP_SERVER_NAME, providerInfo } from '../shared/protocol.js';
|
|
8
|
+
import { DIR_MAX_CHARS, PAYLOAD_MAX_CHARS, PAYLOAD_MAX_ITEMS, PAYLOAD_MAX_SERVERS, PAYLOAD_MAX_SKILLS, PAYLOAD_SERVER_NAME_PATTERN, PAYLOAD_STRING_MAX_CHARS, } from './constants.js';
|
|
9
|
+
const NAME_RE = new RegExp(PAYLOAD_SERVER_NAME_PATTERN);
|
|
10
|
+
const ENV_NAME_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
11
|
+
const HEADER_NAME_RE = /^[A-Za-z0-9-]+$/;
|
|
12
|
+
const SERVER_TYPES = ['stdio', 'http', 'sse'];
|
|
13
|
+
const FIELDS_OF = { stdio: ['type', 'command', 'args', 'env'], http: ['type', 'url', 'headers'], sse: ['type', 'url', 'headers'] };
|
|
14
|
+
class Fault extends Error {
|
|
15
|
+
field;
|
|
16
|
+
constructor(field, message) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.field = field;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
22
|
+
/** A string within the limits; a NUL would end a C string, so it never passes. */
|
|
23
|
+
function text(value, what, min, max = PAYLOAD_STRING_MAX_CHARS) {
|
|
24
|
+
if (typeof value !== 'string' || value.length < min || value.length > max) {
|
|
25
|
+
throw new Fault('payload.mcpServers', `${what} must be text of ${min === 0 ? 'at most' : `1 to`} ${max} characters`);
|
|
26
|
+
}
|
|
27
|
+
if (value.includes('\u0000'))
|
|
28
|
+
throw new Fault('payload.mcpServers', `${what} cannot contain a NUL character`);
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
/** env or headers: names by `nameRe`, values text. Built with fromEntries so that a name like __proto__ stays a plain key. */
|
|
32
|
+
function table(value, what, nameRe, valueRe) {
|
|
33
|
+
if (!isRecord(value))
|
|
34
|
+
throw new Fault('payload.mcpServers', `${what} must be an object of text values`);
|
|
35
|
+
const entries = Object.entries(value);
|
|
36
|
+
if (entries.length > PAYLOAD_MAX_ITEMS)
|
|
37
|
+
throw new Fault('payload.mcpServers', `${what} can hold at most ${PAYLOAD_MAX_ITEMS} entries`);
|
|
38
|
+
return Object.fromEntries(entries.map(([key, item]) => {
|
|
39
|
+
if (!nameRe.test(key))
|
|
40
|
+
throw new Fault('payload.mcpServers', `${what}: "${key}" is not a usable name`);
|
|
41
|
+
const checked = text(item, `${what}.${key}`, 0);
|
|
42
|
+
if (valueRe?.test(checked))
|
|
43
|
+
throw new Fault('payload.mcpServers', `${what}.${key} cannot contain a line break`);
|
|
44
|
+
return [key, checked];
|
|
45
|
+
}));
|
|
46
|
+
}
|
|
47
|
+
function server(name, value) {
|
|
48
|
+
const where = `server "${name}"`;
|
|
49
|
+
if (!isRecord(value))
|
|
50
|
+
throw new Fault('payload.mcpServers', `${where} must be an object`);
|
|
51
|
+
const type = value.type ?? 'stdio';
|
|
52
|
+
if (!SERVER_TYPES.includes(type)) {
|
|
53
|
+
throw new Fault('payload.mcpServers', `${where}: type must be one of ${SERVER_TYPES.join(', ')}`);
|
|
54
|
+
}
|
|
55
|
+
const kind = type;
|
|
56
|
+
for (const key of Object.keys(value)) {
|
|
57
|
+
if (!FIELDS_OF[kind].includes(key)) {
|
|
58
|
+
throw new Fault('payload.mcpServers', `${where}: "${key}" is not a field of a ${kind} server (a server with a url needs "type": "http" or "sse")`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const out = {};
|
|
62
|
+
if (value.type !== undefined)
|
|
63
|
+
out.type = kind;
|
|
64
|
+
if (kind === 'stdio') {
|
|
65
|
+
out.command = text(value.command, `${where}: command`, 1);
|
|
66
|
+
if (value.args !== undefined) {
|
|
67
|
+
if (!Array.isArray(value.args) || value.args.length > PAYLOAD_MAX_ITEMS) {
|
|
68
|
+
throw new Fault('payload.mcpServers', `${where}: args must be a list of at most ${PAYLOAD_MAX_ITEMS} texts`);
|
|
69
|
+
}
|
|
70
|
+
out.args = value.args.map((arg, i) => text(arg, `${where}: args[${i}]`, 0));
|
|
71
|
+
}
|
|
72
|
+
if (value.env !== undefined)
|
|
73
|
+
out.env = table(value.env, `${where}: env`, ENV_NAME_RE, null);
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
out.url = text(value.url, `${where}: url`, 1);
|
|
77
|
+
let parsed = null;
|
|
78
|
+
try {
|
|
79
|
+
parsed = new URL(out.url);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// reported below
|
|
83
|
+
}
|
|
84
|
+
if (parsed === null || (parsed.protocol !== 'http:' && parsed.protocol !== 'https:')) {
|
|
85
|
+
throw new Fault('payload.mcpServers', `${where}: url must be an http or https address`);
|
|
86
|
+
}
|
|
87
|
+
if (value.headers !== undefined)
|
|
88
|
+
out.headers = table(value.headers, `${where}: headers`, HEADER_NAME_RE, /[\r\n]/);
|
|
89
|
+
return out;
|
|
90
|
+
}
|
|
91
|
+
function servers(value) {
|
|
92
|
+
if (!isRecord(value))
|
|
93
|
+
throw new Fault('payload.mcpServers', 'mcpServers must be an object of servers');
|
|
94
|
+
const entries = Object.entries(value);
|
|
95
|
+
if (entries.length > PAYLOAD_MAX_SERVERS)
|
|
96
|
+
throw new Fault('payload.mcpServers', `at most ${PAYLOAD_MAX_SERVERS} servers`);
|
|
97
|
+
return Object.fromEntries(entries.map(([name, config]) => {
|
|
98
|
+
if (name === MCP_SERVER_NAME)
|
|
99
|
+
throw new Fault('payload.mcpServers', `"${MCP_SERVER_NAME}" is reserved for the gonogo tools`);
|
|
100
|
+
if (!NAME_RE.test(name)) {
|
|
101
|
+
throw new Fault('payload.mcpServers', `server name "${name}": use lowercase letters, digits, - and _, starting with a letter or digit (at most 32 characters)`);
|
|
102
|
+
}
|
|
103
|
+
return [name, server(name, config)];
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
function skills(value) {
|
|
107
|
+
if (!Array.isArray(value) || value.length > PAYLOAD_MAX_SKILLS) {
|
|
108
|
+
throw new Fault('payload.skills', `skills must be a list of at most ${PAYLOAD_MAX_SKILLS} folders`);
|
|
109
|
+
}
|
|
110
|
+
const paths = new Set();
|
|
111
|
+
const names = new Set();
|
|
112
|
+
for (const item of value) {
|
|
113
|
+
if (typeof item !== 'string' || item.length === 0 || item.length > DIR_MAX_CHARS || item.includes('\u0000')) {
|
|
114
|
+
throw new Fault('payload.skills', `a skill folder must be a path of 1 to ${DIR_MAX_CHARS} characters`);
|
|
115
|
+
}
|
|
116
|
+
if (!isAbsolute(item))
|
|
117
|
+
throw new Fault('payload.skills', `a skill folder must be an absolute path: ${item}`);
|
|
118
|
+
const path = resolve(item);
|
|
119
|
+
// The folder's own name is the skill's name in the plugin, so two folders with one name would collide.
|
|
120
|
+
if (paths.has(path) || names.has(basename(path)))
|
|
121
|
+
throw new Fault('payload.skills', `skill folders must differ in name: ${path}`);
|
|
122
|
+
paths.add(path);
|
|
123
|
+
names.add(basename(path));
|
|
124
|
+
}
|
|
125
|
+
return [...paths];
|
|
126
|
+
}
|
|
127
|
+
/** `undefined` and `null` are no payload; an empty `mcpServers` or `skills` is dropped, and a payload left empty is none. */
|
|
128
|
+
export function parsePayload(value) {
|
|
129
|
+
try {
|
|
130
|
+
if (value === undefined || value === null)
|
|
131
|
+
return { ok: true, payload: undefined };
|
|
132
|
+
if (!isRecord(value))
|
|
133
|
+
throw new Fault('payload', 'payload must be an object');
|
|
134
|
+
for (const key of Object.keys(value)) {
|
|
135
|
+
if (key !== 'mcpServers' && key !== 'skills')
|
|
136
|
+
throw new Fault('payload', `payload has no field "${key}"`);
|
|
137
|
+
}
|
|
138
|
+
const payload = {};
|
|
139
|
+
if (value.mcpServers !== undefined) {
|
|
140
|
+
const found = servers(value.mcpServers);
|
|
141
|
+
if (Object.keys(found).length > 0)
|
|
142
|
+
payload.mcpServers = found;
|
|
143
|
+
}
|
|
144
|
+
if (value.skills !== undefined) {
|
|
145
|
+
const found = skills(value.skills);
|
|
146
|
+
if (found.length > 0)
|
|
147
|
+
payload.skills = found;
|
|
148
|
+
}
|
|
149
|
+
if (JSON.stringify(payload).length > PAYLOAD_MAX_CHARS)
|
|
150
|
+
throw new Fault('payload', `payload must be at most ${PAYLOAD_MAX_CHARS} characters of JSON`);
|
|
151
|
+
return { ok: true, payload: Object.keys(payload).length === 0 ? undefined : payload };
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
if (error instanceof Fault)
|
|
155
|
+
return { ok: false, field: error.field, error: error.message };
|
|
156
|
+
throw error;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/** Why `provider` cannot take `payload` (PROVIDER_INFO.payload), or null. */
|
|
160
|
+
export function payloadRefusal(provider, payload) {
|
|
161
|
+
const info = providerInfo(provider);
|
|
162
|
+
if (info.payload === null)
|
|
163
|
+
return { field: 'payload', error: `${info.label} takes no payload` };
|
|
164
|
+
if (payload.skills !== undefined && info.payload !== 'all') {
|
|
165
|
+
return { field: 'payload.skills', error: `skills need Claude Code; ${info.label} takes MCP servers only` };
|
|
166
|
+
}
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Propellant: token use per agent and for the room, read from Claude Code's own transcripts, kept in a ledger, checked
|
|
3
|
+
* against the Director's limits over rolling windows (docs/redline-propellant-spec.md).
|
|
4
|
+
*
|
|
5
|
+
* A transcript is JSON lines; an assistant line carries `message.usage`, and one reply is written as several lines (one
|
|
6
|
+
* per content block) that repeat the same `message.id` and the same usage, so only the first line of an id counts.
|
|
7
|
+
* Tokens = input + cache creation + output. Cache reads are left out: in a long session they dwarf the rest and would
|
|
8
|
+
* make every limit meaningless.
|
|
9
|
+
*
|
|
10
|
+
* shortcut: subagent (Task tool) transcripts are other files and are not counted; read them too if that spend matters.
|
|
11
|
+
* shortcut: the ledger and the limits are keyed by agent name, so a rename starts from zero; key by record id if renames get common.
|
|
12
|
+
*/
|
|
13
|
+
import { appendFile, open, readFile, realpath, rename, writeFile } from 'node:fs/promises';
|
|
14
|
+
import { join, sep } from 'node:path';
|
|
15
|
+
import { randomBytes } from 'node:crypto';
|
|
16
|
+
import { AGENT_NAME_PATTERN, FUEL_WINDOWS } from '../shared/protocol.js';
|
|
17
|
+
import { tokenText } from '../shared/fuel.js';
|
|
18
|
+
import { FILE_MODE, FUEL_KEEP_MS, FUEL_LIMIT_MAX, METER_READ_MAX_BYTES, PROPELLANT_FILE as LIMITS_FILE } from './constants.js';
|
|
19
|
+
const HOUR = 3_600_000;
|
|
20
|
+
export const WINDOW_MS = { '5h': 5 * HOUR, day: 24 * HOUR, week: 7 * 24 * HOUR, month: 30 * 24 * HOUR };
|
|
21
|
+
const LEDGER_FILE = 'fuel.jsonl';
|
|
22
|
+
const NAME_RE = new RegExp(AGENT_NAME_PATTERN);
|
|
23
|
+
function isRecord(value) {
|
|
24
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
25
|
+
}
|
|
26
|
+
/** The tokens of one transcript line and its message id, or null for a line without usage. */
|
|
27
|
+
export function lineUsage(line) {
|
|
28
|
+
let value;
|
|
29
|
+
try {
|
|
30
|
+
value = JSON.parse(line);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
if (!isRecord(value) || !isRecord(value.message) || !isRecord(value.message.usage))
|
|
36
|
+
return null;
|
|
37
|
+
const usage = value.message.usage;
|
|
38
|
+
const id = typeof value.message.id === 'string' ? value.message.id : typeof value.requestId === 'string' ? value.requestId : '';
|
|
39
|
+
if (id === '')
|
|
40
|
+
return null;
|
|
41
|
+
const n = (key) => {
|
|
42
|
+
const v = usage[key];
|
|
43
|
+
return typeof v === 'number' && Number.isFinite(v) && v > 0 ? v : 0;
|
|
44
|
+
};
|
|
45
|
+
const at = typeof value.timestamp === 'string' ? Date.parse(value.timestamp) : NaN;
|
|
46
|
+
return { id, at: Number.isFinite(at) ? at : null, tokens: n('input_tokens') + n('cache_creation_input_tokens') + n('output_tokens') };
|
|
47
|
+
}
|
|
48
|
+
/** Limits from untrusted JSON: whole numbers from 1 to FUEL_LIMIT_MAX per known window; anything else is refused. */
|
|
49
|
+
export function parseLimits(value, what) {
|
|
50
|
+
if (!isRecord(value))
|
|
51
|
+
return `${what} must be an object of windows (${FUEL_WINDOWS.join(', ')})`;
|
|
52
|
+
const limits = {};
|
|
53
|
+
for (const [key, v] of Object.entries(value)) {
|
|
54
|
+
if (!FUEL_WINDOWS.includes(key))
|
|
55
|
+
return `${what}: unknown window "${key}"`;
|
|
56
|
+
if (v === null)
|
|
57
|
+
continue;
|
|
58
|
+
if (typeof v !== 'number' || !Number.isInteger(v) || v < 1 || v > FUEL_LIMIT_MAX) {
|
|
59
|
+
return `${what}.${key} must be a whole number of tokens from 1 to ${FUEL_LIMIT_MAX}, or null`;
|
|
60
|
+
}
|
|
61
|
+
limits[key] = v;
|
|
62
|
+
}
|
|
63
|
+
return limits;
|
|
64
|
+
}
|
|
65
|
+
export function parsePropellantRequest(body) {
|
|
66
|
+
const value = {};
|
|
67
|
+
for (const key of Object.keys(body)) {
|
|
68
|
+
if (key !== 'room' && key !== 'agents')
|
|
69
|
+
return { ok: false, error: `unknown field "${key}"`, field: key };
|
|
70
|
+
}
|
|
71
|
+
if (body.room !== undefined) {
|
|
72
|
+
const room = parseLimits(body.room, 'room');
|
|
73
|
+
if (typeof room === 'string')
|
|
74
|
+
return { ok: false, error: room, field: 'room' };
|
|
75
|
+
value.room = room;
|
|
76
|
+
}
|
|
77
|
+
if (body.agents !== undefined) {
|
|
78
|
+
if (!isRecord(body.agents))
|
|
79
|
+
return { ok: false, error: 'agents must be an object of agent names', field: 'agents' };
|
|
80
|
+
value.agents = {};
|
|
81
|
+
for (const [name, raw] of Object.entries(body.agents)) {
|
|
82
|
+
if (!NAME_RE.test(name))
|
|
83
|
+
return { ok: false, error: `agents: invalid name "${name}"`, field: 'agents' };
|
|
84
|
+
const limits = parseLimits(raw, `agents.${name}`);
|
|
85
|
+
if (typeof limits === 'string')
|
|
86
|
+
return { ok: false, error: limits, field: 'agents' };
|
|
87
|
+
value.agents[name] = limits;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return { ok: true, value };
|
|
91
|
+
}
|
|
92
|
+
export { tokenText };
|
|
93
|
+
export function createPropellant(options) {
|
|
94
|
+
const { dir, now, log } = options;
|
|
95
|
+
const ledgerPath = join(dir, LEDGER_FILE);
|
|
96
|
+
const limitsPath = join(dir, LIMITS_FILE);
|
|
97
|
+
let entries = [];
|
|
98
|
+
let saved = { room: {}, agents: {}, cursors: {} };
|
|
99
|
+
/** The last message id counted per transcript: the repeated lines of one reply follow each other. */
|
|
100
|
+
const lastId = new Map();
|
|
101
|
+
/** One read per transcript at a time, and the ledger and limits file written in order. */
|
|
102
|
+
const reading = new Map();
|
|
103
|
+
let chain = Promise.resolve();
|
|
104
|
+
let projectsReal = null;
|
|
105
|
+
const later = (fn) => {
|
|
106
|
+
const run = chain.then(fn);
|
|
107
|
+
chain = run.catch((error) => log.error('propellant could not write', error));
|
|
108
|
+
return run.then(() => undefined);
|
|
109
|
+
};
|
|
110
|
+
const saveLimits = () => {
|
|
111
|
+
// a file from before it had a version has none and reads as version 1 (src/server/migrate.ts)
|
|
112
|
+
const json = JSON.stringify({ version: 1, ...saved }, null, 2);
|
|
113
|
+
return later(async () => {
|
|
114
|
+
const tmp = join(dir, `.${LIMITS_FILE}.${process.pid}.${randomBytes(4).toString('hex')}.tmp`);
|
|
115
|
+
await writeFile(tmp, json, { mode: FILE_MODE, flag: 'wx' });
|
|
116
|
+
await rename(tmp, limitsPath);
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
const prune = () => {
|
|
120
|
+
const cut = now() - FUEL_KEEP_MS;
|
|
121
|
+
const before = entries.length;
|
|
122
|
+
entries = entries.filter((e) => e.at >= cut);
|
|
123
|
+
return entries.length !== before;
|
|
124
|
+
};
|
|
125
|
+
const gauge = (agent, limits) => {
|
|
126
|
+
const t = now();
|
|
127
|
+
const used = { '5h': 0, day: 0, week: 0, month: 0 };
|
|
128
|
+
for (const e of entries) {
|
|
129
|
+
if (agent !== null && e.agent !== agent)
|
|
130
|
+
continue;
|
|
131
|
+
for (const w of FUEL_WINDOWS)
|
|
132
|
+
if (t - e.at < WINDOW_MS[w])
|
|
133
|
+
used[w] += e.tokens;
|
|
134
|
+
}
|
|
135
|
+
return { used, limits: { ...limits } };
|
|
136
|
+
};
|
|
137
|
+
const firstOver = (g) => {
|
|
138
|
+
for (const w of FUEL_WINDOWS) {
|
|
139
|
+
const limit = g.limits[w];
|
|
140
|
+
if (limit !== undefined && g.used[w] >= limit)
|
|
141
|
+
return w;
|
|
142
|
+
}
|
|
143
|
+
return null;
|
|
144
|
+
};
|
|
145
|
+
const inside = async (path) => {
|
|
146
|
+
projectsReal ??= await realpath(options.projectsRoot).catch(() => null);
|
|
147
|
+
if (projectsReal === null || !path.endsWith('.jsonl'))
|
|
148
|
+
return null;
|
|
149
|
+
const real = await realpath(path).catch(() => null);
|
|
150
|
+
return real !== null && real.startsWith(projectsReal + sep) ? real : null;
|
|
151
|
+
};
|
|
152
|
+
const readNew = async (agent, transcriptPath) => {
|
|
153
|
+
const path = await inside(transcriptPath);
|
|
154
|
+
if (path === null) {
|
|
155
|
+
log.debug(`propellant ignored a transcript outside ${options.projectsRoot}`);
|
|
156
|
+
return 0;
|
|
157
|
+
}
|
|
158
|
+
const handle = await open(path, 'r');
|
|
159
|
+
let tokens = 0;
|
|
160
|
+
try {
|
|
161
|
+
const size = (await handle.stat()).size;
|
|
162
|
+
let offset = saved.cursors[path] ?? 0;
|
|
163
|
+
if (offset > size)
|
|
164
|
+
offset = 0; // the file was replaced: count it again from the start
|
|
165
|
+
const length = Math.min(size - offset, METER_READ_MAX_BYTES);
|
|
166
|
+
if (length <= 0)
|
|
167
|
+
return 0;
|
|
168
|
+
const buffer = Buffer.alloc(length);
|
|
169
|
+
const { bytesRead } = await handle.read(buffer, 0, length, offset);
|
|
170
|
+
const end = buffer.subarray(0, bytesRead).lastIndexOf(0x0a);
|
|
171
|
+
if (end < 0)
|
|
172
|
+
return 0; // no complete line yet
|
|
173
|
+
const fresh = [];
|
|
174
|
+
for (const line of buffer.subarray(0, end).toString('utf8').split('\n')) {
|
|
175
|
+
const usage = lineUsage(line);
|
|
176
|
+
if (usage === null || usage.id === lastId.get(path))
|
|
177
|
+
continue;
|
|
178
|
+
lastId.set(path, usage.id);
|
|
179
|
+
if (usage.tokens === 0)
|
|
180
|
+
continue;
|
|
181
|
+
const at = usage.at ?? now();
|
|
182
|
+
// one ledger line per minute and agent keeps the file small
|
|
183
|
+
const last = fresh[fresh.length - 1];
|
|
184
|
+
if (last && Math.floor(last.at / 60_000) === Math.floor(at / 60_000))
|
|
185
|
+
last.tokens += usage.tokens;
|
|
186
|
+
else
|
|
187
|
+
fresh.push({ at, agent, tokens: usage.tokens });
|
|
188
|
+
tokens += usage.tokens;
|
|
189
|
+
}
|
|
190
|
+
saved.cursors[path] = offset + end + 1;
|
|
191
|
+
const cut = now() - FUEL_KEEP_MS;
|
|
192
|
+
const kept = fresh.filter((e) => e.at >= cut);
|
|
193
|
+
entries.push(...kept);
|
|
194
|
+
if (kept.length > 0) {
|
|
195
|
+
const text = kept.map((e) => JSON.stringify(e)).join('\n') + '\n';
|
|
196
|
+
await later(() => appendFile(ledgerPath, text, { mode: FILE_MODE }));
|
|
197
|
+
}
|
|
198
|
+
await saveLimits();
|
|
199
|
+
}
|
|
200
|
+
finally {
|
|
201
|
+
await handle.close();
|
|
202
|
+
}
|
|
203
|
+
return tokens;
|
|
204
|
+
};
|
|
205
|
+
const view = () => {
|
|
206
|
+
prune();
|
|
207
|
+
const agents = {};
|
|
208
|
+
const names = new Set([...entries.map((e) => e.agent), ...Object.keys(saved.agents)]);
|
|
209
|
+
for (const name of [...names].sort())
|
|
210
|
+
agents[name] = gauge(name, saved.agents[name] ?? {});
|
|
211
|
+
return { room: gauge(null, saved.room), agents };
|
|
212
|
+
};
|
|
213
|
+
return {
|
|
214
|
+
async load() {
|
|
215
|
+
try {
|
|
216
|
+
const data = JSON.parse(await readFile(limitsPath, 'utf8'));
|
|
217
|
+
if (isRecord(data)) {
|
|
218
|
+
const room = parseLimits(data.room ?? {}, 'room');
|
|
219
|
+
if (typeof room !== 'string')
|
|
220
|
+
saved.room = room;
|
|
221
|
+
if (isRecord(data.agents)) {
|
|
222
|
+
for (const [name, raw] of Object.entries(data.agents)) {
|
|
223
|
+
const limits = parseLimits(raw, name);
|
|
224
|
+
if (NAME_RE.test(name) && typeof limits !== 'string')
|
|
225
|
+
saved.agents[name] = limits;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (isRecord(data.cursors)) {
|
|
229
|
+
for (const [path, n] of Object.entries(data.cursors)) {
|
|
230
|
+
if (typeof n === 'number' && Number.isSafeInteger(n) && n >= 0)
|
|
231
|
+
saved.cursors[path] = n;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
catch (error) {
|
|
237
|
+
if (error.code !== 'ENOENT')
|
|
238
|
+
log.warn(`${LIMITS_FILE} could not be read; limits start empty`, error);
|
|
239
|
+
}
|
|
240
|
+
let text = '';
|
|
241
|
+
try {
|
|
242
|
+
text = await readFile(ledgerPath, 'utf8');
|
|
243
|
+
}
|
|
244
|
+
catch (error) {
|
|
245
|
+
if (error.code !== 'ENOENT')
|
|
246
|
+
log.warn(`${LEDGER_FILE} could not be read; token use starts at 0`, error);
|
|
247
|
+
}
|
|
248
|
+
for (const line of text.split('\n')) {
|
|
249
|
+
if (line === '')
|
|
250
|
+
continue;
|
|
251
|
+
try {
|
|
252
|
+
const e = JSON.parse(line);
|
|
253
|
+
if (isRecord(e) && typeof e.at === 'number' && typeof e.agent === 'string' && typeof e.tokens === 'number' && e.tokens > 0) {
|
|
254
|
+
entries.push({ at: e.at, agent: e.agent, tokens: e.tokens });
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
catch {
|
|
258
|
+
// a torn last line after a crash: skipped
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (prune()) {
|
|
262
|
+
const kept = entries.map((e) => JSON.stringify(e)).join('\n');
|
|
263
|
+
await later(() => writeFile(ledgerPath, kept === '' ? '' : kept + '\n', { mode: FILE_MODE }));
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
record(agent, transcriptPath) {
|
|
267
|
+
const previous = reading.get(transcriptPath) ?? Promise.resolve(0);
|
|
268
|
+
const run = previous
|
|
269
|
+
.catch(() => 0)
|
|
270
|
+
.then(() => readNew(agent, transcriptPath))
|
|
271
|
+
.catch((error) => {
|
|
272
|
+
log.warn(`propellant could not read the transcript of ${agent}`, error);
|
|
273
|
+
return 0;
|
|
274
|
+
});
|
|
275
|
+
reading.set(transcriptPath, run);
|
|
276
|
+
void run.then(() => {
|
|
277
|
+
if (reading.get(transcriptPath) === run)
|
|
278
|
+
reading.delete(transcriptPath);
|
|
279
|
+
});
|
|
280
|
+
return run;
|
|
281
|
+
},
|
|
282
|
+
view,
|
|
283
|
+
async setLimits(request) {
|
|
284
|
+
if (request.room !== undefined)
|
|
285
|
+
saved.room = { ...request.room };
|
|
286
|
+
for (const [name, limits] of Object.entries(request.agents ?? {})) {
|
|
287
|
+
if (Object.keys(limits).length === 0)
|
|
288
|
+
delete saved.agents[name];
|
|
289
|
+
else
|
|
290
|
+
saved.agents[name] = { ...limits };
|
|
291
|
+
}
|
|
292
|
+
await saveLimits();
|
|
293
|
+
return view();
|
|
294
|
+
},
|
|
295
|
+
over(agent) {
|
|
296
|
+
const own = gauge(agent, saved.agents[agent] ?? {});
|
|
297
|
+
const ownWindow = firstOver(own);
|
|
298
|
+
if (ownWindow !== null) {
|
|
299
|
+
const limit = own.limits[ownWindow] ?? 0;
|
|
300
|
+
return { scope: 'agent', reason: `out of propellant: ${tokenText(own.used[ownWindow])} of ${tokenText(limit)} tokens in ${ownWindow}` };
|
|
301
|
+
}
|
|
302
|
+
const room = gauge(null, saved.room);
|
|
303
|
+
const roomWindow = firstOver(room);
|
|
304
|
+
if (roomWindow !== null) {
|
|
305
|
+
const limit = room.limits[roomWindow] ?? 0;
|
|
306
|
+
return { scope: 'room', reason: `room out of propellant: ${tokenText(room.used[roomWindow])} of ${tokenText(limit)} tokens in ${roomWindow}` };
|
|
307
|
+
}
|
|
308
|
+
return null;
|
|
309
|
+
},
|
|
310
|
+
close() {
|
|
311
|
+
return chain.then(() => undefined);
|
|
312
|
+
},
|
|
313
|
+
};
|
|
314
|
+
}
|