gcf-board 0.5.1 → 0.7.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/README.md +14 -12
- package/dist/server/app.js +166 -20
- package/dist/server/chat.js +42 -1
- package/dist/server/files.js +5 -2
- package/dist/server/mcp.js +28 -0
- package/dist/server/moves.js +11 -0
- package/dist/server/propose.js +96 -0
- package/dist/server/runs.js +28 -120
- package/dist/server/talks.js +553 -0
- package/dist/web/assets/golos-text-cyrillic-ext-wght-normal-hICkpzq7.woff2 +0 -0
- package/dist/web/assets/golos-text-cyrillic-wght-normal-mKek_fSS.woff2 +0 -0
- package/dist/web/assets/golos-text-latin-ext-wght-normal-B-I5lbAQ.woff2 +0 -0
- package/dist/web/assets/golos-text-latin-wght-normal-BS5oQT7X.woff2 +0 -0
- package/dist/web/assets/index-B3u-VrJK.css +1 -0
- package/dist/web/assets/index-BB3i2FaE.js +39 -0
- package/dist/web/assets/jetbrains-mono-cyrillic-wght-normal-D73BlboJ.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-greek-wght-normal-Bw9x6K1M.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
- package/dist/web/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
- package/dist/web/index.html +2 -2
- package/node_modules/gcf-session/lib/activity.js +371 -0
- package/node_modules/gcf-session/lib/adapter/claude.js +226 -0
- package/node_modules/gcf-session/lib/adapter/index.js +33 -0
- package/node_modules/gcf-session/lib/chat-input.js +260 -0
- package/node_modules/gcf-session/lib/chat.js +834 -0
- package/node_modules/gcf-session/lib/cli.js +52 -0
- package/node_modules/gcf-session/lib/cost.js +29 -0
- package/node_modules/gcf-session/lib/index.js +58 -0
- package/node_modules/gcf-session/lib/job.js +437 -0
- package/node_modules/gcf-session/lib/limit.js +132 -0
- package/node_modules/gcf-session/lib/lock.js +81 -0
- package/node_modules/gcf-session/lib/proc.js +93 -0
- package/node_modules/gcf-session/lib/session.js +320 -0
- package/node_modules/gcf-session/lib/store.js +28 -0
- package/node_modules/gcf-session/lib/thread.js +423 -0
- package/node_modules/gcf-session/lib/windows.js +46 -0
- package/node_modules/gcf-session/package.json +23 -0
- package/node_modules/gcf-session/types/index.d.ts +152 -0
- package/package.json +12 -3
- package/dist/web/assets/index-BYAEKw51.css +0 -1
- package/dist/web/assets/index-DpzFCrLV.js +0 -36
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// The usage-limit wait (docs/Session-Engine.md AD-19): when the limit resets, whether a run waits
|
|
2
|
+
// for it, and the wait itself, which a stop ends and a sleeping PC does not fool.
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
|
|
5
|
+
/** The wait ends this long after the reset: a resume right at it may meet the limit still. */
|
|
6
|
+
export const MARGIN_MS = 60_000;
|
|
7
|
+
|
|
8
|
+
/** Past this the run does not wait: it ends paused, as before. */
|
|
9
|
+
export const MAX_WAIT_MS = 6 * 3_600_000;
|
|
10
|
+
|
|
11
|
+
/** How often the wait reads the wall clock and the stop mark. */
|
|
12
|
+
export const POLL_MS = 30_000;
|
|
13
|
+
|
|
14
|
+
/** The waits one item gets in a run: its next limit ends it paused. */
|
|
15
|
+
export const MAX_WAITS = 2;
|
|
16
|
+
|
|
17
|
+
/** The real clock; a test gives its own. */
|
|
18
|
+
export const realClock = {
|
|
19
|
+
now: () => Date.now(),
|
|
20
|
+
sleep: (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The time a usage limit resets, as Claude names it (`3am`, `3:30pm`, `15:00`, with or without
|
|
25
|
+
* `(Europe/Berlin)`): its next occurrence after `now`; null when the text is no such time.
|
|
26
|
+
* @param {string} text
|
|
27
|
+
* @param {Date} [now]
|
|
28
|
+
*/
|
|
29
|
+
export function resetAt(text, now = new Date()) {
|
|
30
|
+
// shortcut: the time is read on this machine's clock and the zone it names is ignored: the
|
|
31
|
+
// run goes where the operator's claude does; convert through Intl if a zone ever differs.
|
|
32
|
+
const m = /^\s*(\d{1,2})(?::(\d{2}))?\s*([ap]m)?\s*(\(.*\))?\s*$/i.exec(text);
|
|
33
|
+
if (!m || (!m[2] && !m[3])) return null;
|
|
34
|
+
const hour = hourOf(Number(m[1]), m[3]);
|
|
35
|
+
const minute = Number(m[2] ?? 0);
|
|
36
|
+
if (hour === null || hour > 23 || minute > 59) return null;
|
|
37
|
+
const at = new Date(now);
|
|
38
|
+
at.setHours(hour, minute, 0, 0);
|
|
39
|
+
if (at <= now) at.setDate(at.getDate() + 1);
|
|
40
|
+
|
|
41
|
+
return at;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The 24-hour hour of `hour` with its `am`/`pm`, if any; null for a 12-hour one out of 1–12. */
|
|
45
|
+
function hourOf(hour, ampm) {
|
|
46
|
+
if (!ampm) return hour;
|
|
47
|
+
if (hour < 1 || hour > 12) return null;
|
|
48
|
+
|
|
49
|
+
return (hour % 12) + (ampm.toLowerCase() === 'pm' ? 12 : 0);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const weeklyType = (type) => /seven_day/.test(String(type ?? ''));
|
|
53
|
+
|
|
54
|
+
/** A reset in epoch seconds, while it is ahead of `now`: one behind it is a stale state. */
|
|
55
|
+
const ahead = (s, now) => (Number(s) * 1000 > now.getTime() ? new Date(Number(s) * 1000) : null);
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* When the limit that stopped a session resets, by the first source that says (AD-19): the
|
|
59
|
+
* latest reset of the account's windows that are used up, the reset of a rejected state, the
|
|
60
|
+
* time in the limit's text. `weekly` says the week's window is the one used up: no run waits
|
|
61
|
+
* for that. `quota` is the stream's `rate_limit_info`, `text` the limit message's time.
|
|
62
|
+
* @param {any} quota
|
|
63
|
+
* @param {string | null | undefined} text
|
|
64
|
+
* @param {Date} [now]
|
|
65
|
+
* @returns {{at: Date | null, weekly: boolean}}
|
|
66
|
+
*/
|
|
67
|
+
export function resetOf(quota, text, now = new Date()) {
|
|
68
|
+
const windows = Object.entries(quota?.unifiedWindows ?? {}).filter(
|
|
69
|
+
([, w]) => Number(w?.utilization) >= 1 && ahead(w.resetsAt, now),
|
|
70
|
+
);
|
|
71
|
+
if (windows.length) {
|
|
72
|
+
const at = new Date(Math.max(...windows.map(([, w]) => Number(w.resetsAt) * 1000)));
|
|
73
|
+
|
|
74
|
+
return { at, weekly: windows.some(([type]) => weeklyType(type)) };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (quota?.status === 'rejected' && ahead(quota.resetsAt, now))
|
|
78
|
+
return { at: ahead(quota.resetsAt, now), weekly: weeklyType(quota.rateLimitType) };
|
|
79
|
+
|
|
80
|
+
// shortcut: a weekly limit known by its text alone is not told apart; its reset is mostly a
|
|
81
|
+
// date, not a time, so it ends paused anyway. Read the message's wording once it is known.
|
|
82
|
+
return { at: text ? resetAt(text, now) : null, weekly: false };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The reset the account's last usage-limit state still waits for (`runs/quota.json`), when it
|
|
87
|
+
* is ahead: a run started now would meet the limit. Null when the state allows, or is unknown.
|
|
88
|
+
* @param {string | null | undefined} file
|
|
89
|
+
* @param {number} now
|
|
90
|
+
*/
|
|
91
|
+
export function resetAhead(file, now) {
|
|
92
|
+
if (!file) return null;
|
|
93
|
+
try {
|
|
94
|
+
const { info } = JSON.parse(readFileSync(file, 'utf8'));
|
|
95
|
+
const used = Object.values(info?.unifiedWindows ?? {}).some((w) => Number(w?.utilization) >= 1);
|
|
96
|
+
if (!used && info?.status !== 'rejected') return null;
|
|
97
|
+
const { at, weekly } = resetOf(info, null, new Date(now));
|
|
98
|
+
|
|
99
|
+
return at && !weekly ? at : null;
|
|
100
|
+
} catch {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Whether a run waits for a reset at `at`, its `waits` so far counted: not for the week's
|
|
107
|
+
* window, an unknown reset, one past `MAX_WAIT_MS`, or a third limit of the item.
|
|
108
|
+
* @param {{at: Date | null, weekly: boolean}} reset
|
|
109
|
+
* @param {number} waits
|
|
110
|
+
* @param {number} now
|
|
111
|
+
*/
|
|
112
|
+
export const waitsFor = (reset, waits, now) =>
|
|
113
|
+
Boolean(reset.at) &&
|
|
114
|
+
!reset.weekly &&
|
|
115
|
+
waits < MAX_WAITS &&
|
|
116
|
+
/** @type {Date} */ (reset.at).getTime() + MARGIN_MS - now <= MAX_WAIT_MS;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Wait until `until` (ms), reading the wall clock and `stopped()` each `POLL_MS`: a PC that
|
|
120
|
+
* slept wakes past it. Resolves true at the time, false at a stop.
|
|
121
|
+
* @param {number} until
|
|
122
|
+
* @param {() => boolean} stopped
|
|
123
|
+
* @param {{now: () => number, sleep: (ms: number) => Promise<unknown>}} [clock]
|
|
124
|
+
*/
|
|
125
|
+
export async function waitUntil(until, stopped, clock = realClock) {
|
|
126
|
+
while (clock.now() < until) {
|
|
127
|
+
if (stopped()) return false;
|
|
128
|
+
await clock.sleep(Math.min(POLL_MS, until - clock.now()));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return !stopped();
|
|
132
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Exclusive lock files: a file created with `wx` holds this process's pid; a lock whose process
|
|
2
|
+
// is gone is taken over under a breaker, so two takers never delete each other's fresh lock.
|
|
3
|
+
import { readFile, rm, stat, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { alive } from './proc.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The lock `file` for this process: created exclusively, or taken over when its process is
|
|
8
|
+
* gone. Returns the release, or null while another live process holds it.
|
|
9
|
+
*/
|
|
10
|
+
export async function claim(file) {
|
|
11
|
+
// Three tries: a breaker a dead run left, then the dead lock itself, is cleared on the way. A
|
|
12
|
+
// lock released between the create and the read spends no try, up to six rounds in all.
|
|
13
|
+
for (let attempt = 0, round = 0; attempt < 3 && round < 6; round++) {
|
|
14
|
+
if (await create(file)) return () => rm(file, { force: true });
|
|
15
|
+
let text;
|
|
16
|
+
try {
|
|
17
|
+
text = await readFile(file, 'utf8');
|
|
18
|
+
} catch (e) {
|
|
19
|
+
// Released meanwhile: the next create may take it. A lock that cannot be read is busy.
|
|
20
|
+
if (e.code === 'ENOENT') continue;
|
|
21
|
+
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
attempt++;
|
|
26
|
+
|
|
27
|
+
// The run's pid is the first line; a started session adds the item and its own pid.
|
|
28
|
+
const pid = Number(text.split('\n')[0]);
|
|
29
|
+
if (pid === process.pid) return () => rm(file, { force: true });
|
|
30
|
+
if (!(await dead(file, pid))) return null;
|
|
31
|
+
await clearDead(file, text);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Create the lock with this pid, or false when it exists. */
|
|
38
|
+
export async function create(file) {
|
|
39
|
+
try {
|
|
40
|
+
await writeFile(file, `${process.pid}\n`, { flag: 'wx' });
|
|
41
|
+
|
|
42
|
+
return true;
|
|
43
|
+
} catch (e) {
|
|
44
|
+
if (e.code !== 'EEXIST') throw e;
|
|
45
|
+
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** A lock whose pid is gone, or that never got one and is old: its creator died writing it. */
|
|
51
|
+
export async function dead(file, pid) {
|
|
52
|
+
if (Number.isInteger(pid) && pid > 0) return !alive(pid);
|
|
53
|
+
const age = await stat(file).then(
|
|
54
|
+
(s) => Date.now() - s.mtimeMs,
|
|
55
|
+
() => 0,
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
return age > 10000;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Remove a dead lock under `<lock>.break`, as the core's main lock does: two runs that judged
|
|
63
|
+
* the same lock dead cannot both remove it, so neither deletes the other's fresh lock.
|
|
64
|
+
*/
|
|
65
|
+
export async function clearDead(file, text) {
|
|
66
|
+
const breaker = `${file}.break`;
|
|
67
|
+
if (!(await create(breaker))) {
|
|
68
|
+
// A breaker is held for a read and a remove: one whose run is gone was left by a crash there.
|
|
69
|
+
const held = await readFile(breaker, 'utf8').catch(() => null);
|
|
70
|
+
if (held !== null && (await dead(breaker, Number(held.split('\n')[0]))))
|
|
71
|
+
await rm(breaker, { force: true });
|
|
72
|
+
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
if ((await readFile(file, 'utf8').catch(() => null)) === text) await rm(file, { force: true });
|
|
78
|
+
} finally {
|
|
79
|
+
await rm(breaker, { force: true });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// The processes of agent sessions: their start, a stop that takes all they started, the run's
|
|
2
|
+
// own SIGTERM, and their output read line by line.
|
|
3
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
4
|
+
|
|
5
|
+
export const WIN = process.platform === 'win32';
|
|
6
|
+
|
|
7
|
+
/** Whether a pid is a live process; EPERM is alive under another user. */
|
|
8
|
+
export function alive(pid) {
|
|
9
|
+
try {
|
|
10
|
+
process.kill(pid, 0);
|
|
11
|
+
|
|
12
|
+
return true;
|
|
13
|
+
} catch (e) {
|
|
14
|
+
return e.code === 'EPERM';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Stop a session and everything it started: the shell, the agent, its tools. Windows has no
|
|
20
|
+
* process groups to signal, `taskkill /T` walks the tree; elsewhere the session leads its own
|
|
21
|
+
* group (`detached`), and the group gets the signal.
|
|
22
|
+
*/
|
|
23
|
+
export function killTree(pid) {
|
|
24
|
+
if (WIN) spawnSync('taskkill', ['/pid', String(pid), '/T', '/F'], { stdio: 'ignore' });
|
|
25
|
+
else {
|
|
26
|
+
try {
|
|
27
|
+
process.kill(-pid, 'SIGKILL');
|
|
28
|
+
} catch {
|
|
29
|
+
// Gone already.
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// The sessions this process has going: a run that is killed (SIGTERM) takes them down with it.
|
|
35
|
+
export const sessions = new Set();
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A run that gets SIGTERM (`gcf-harness stop <id>` stops one session, never the run): its
|
|
39
|
+
* sessions lead their own process groups, so the run takes each down before it exits.
|
|
40
|
+
* Returns the unhook.
|
|
41
|
+
*/
|
|
42
|
+
export function stopWithSessions() {
|
|
43
|
+
const stop = () => {
|
|
44
|
+
for (const c of sessions) killTree(c.pid);
|
|
45
|
+
process.exit(143);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
process.once('SIGTERM', stop);
|
|
49
|
+
|
|
50
|
+
return () => process.removeListener('SIGTERM', stop);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Hand a stream to `take` line by line. Returns the flush of a last line with no newline: a
|
|
55
|
+
* stream destroyed under a tool that holds it open never ends.
|
|
56
|
+
*/
|
|
57
|
+
export function byLine(stream, take) {
|
|
58
|
+
let rest = '';
|
|
59
|
+
stream.setEncoding('utf8');
|
|
60
|
+
stream.on('data', (chunk) => {
|
|
61
|
+
const lines = (rest + chunk).split('\n');
|
|
62
|
+
rest = lines.pop() ?? '';
|
|
63
|
+
for (const l of lines) take(l);
|
|
64
|
+
});
|
|
65
|
+
// Cleared before the take: a take that ends the session flushes again, and must find nothing.
|
|
66
|
+
const flush = () => {
|
|
67
|
+
const last = rest;
|
|
68
|
+
rest = '';
|
|
69
|
+
if (last) take(last);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
stream.on('end', flush);
|
|
73
|
+
|
|
74
|
+
return flush;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* An agent session's shell: its own process group off Windows, so a stop takes all it started.
|
|
79
|
+
* @returns {import('node:child_process').ChildProcessByStdio<import('node:stream').Writable |
|
|
80
|
+
* null, import('node:stream').Readable, import('node:stream').Readable>}
|
|
81
|
+
*/
|
|
82
|
+
export function spawnAgent(command, dir, env, chat = false) {
|
|
83
|
+
// The stdio of a chat or not: the types cannot follow the choice, the streams are there.
|
|
84
|
+
return /** @type {any} */ (spawn)(command, {
|
|
85
|
+
cwd: dir,
|
|
86
|
+
shell: true,
|
|
87
|
+
env: { ...process.env, ...env },
|
|
88
|
+
detached: !WIN,
|
|
89
|
+
windowsHide: true,
|
|
90
|
+
// A chat session reads the operator's messages on stdin.
|
|
91
|
+
stdio: [chat ? 'pipe' : 'ignore', 'pipe', 'pipe'],
|
|
92
|
+
});
|
|
93
|
+
}
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
// One agent session (`runSession`): its process, its log, its timeout, the handoff watch of its
|
|
2
|
+
// context, its chat; it resolves with what the session told and how it ended. The adapter makes
|
|
3
|
+
// its command line and reads its stream into neutral events.
|
|
4
|
+
import { writeSync } from 'node:fs';
|
|
5
|
+
import { clock, count } from './activity.js';
|
|
6
|
+
import { openChat } from './chat.js';
|
|
7
|
+
import { byLine, killTree, sessions, spawnAgent } from './proc.js';
|
|
8
|
+
import { writeAtomic } from './store.js';
|
|
9
|
+
|
|
10
|
+
// A session that keeps on working past its flag: main-agent turns, or points over the share.
|
|
11
|
+
const HARD_TURNS = 8;
|
|
12
|
+
const HARD_OVER = 15;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Keep what a session's activity told: the last of each, and the model's window from the result.
|
|
16
|
+
*/
|
|
17
|
+
function absorb(seen, a) {
|
|
18
|
+
seen.cost = a.cost ?? seen.cost;
|
|
19
|
+
seen.result = a.result ?? seen.result;
|
|
20
|
+
seen.session_id = a.session_id ?? seen.session_id;
|
|
21
|
+
seen.limit = a.limit ?? seen.limit;
|
|
22
|
+
// The account's last usage-limit state: when a limit that cut the session resets.
|
|
23
|
+
seen.quota = a.quota ?? seen.quota;
|
|
24
|
+
seen.model = a.model ?? seen.model;
|
|
25
|
+
// The init model's window, as the result reports it; another model's says nothing of it.
|
|
26
|
+
if (a.windows) seen.window = a.windows[seen.model] ?? seen.window;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The handoff watch of one session (see `runSession`): `see(a, model)` takes each activity,
|
|
31
|
+
* flags the session past its share or at a compaction (`handoff.flag`), and calls `stop` once it
|
|
32
|
+
* works on past more. `windowOf(model)` is the window the share is taken of.
|
|
33
|
+
*/
|
|
34
|
+
function handoffWatch(handoff, windowOf, write, { stop, onFlag }) {
|
|
35
|
+
/** @type {{flagged: boolean, after: number, context: number, turn?: string, unknown: boolean,
|
|
36
|
+
* info?: object}} */
|
|
37
|
+
const w = { flagged: false, after: 0, context: 0, turn: undefined, unknown: false };
|
|
38
|
+
const unknownOnce = (model) => {
|
|
39
|
+
if (w.unknown) return;
|
|
40
|
+
w.unknown = true;
|
|
41
|
+
write([`[${clock()}] handoff: off, the window of ${model ?? 'the model'} is unknown`]);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const flag = (win, pct, compacted) => {
|
|
45
|
+
w.flagged = true;
|
|
46
|
+
const info = { pct, tokens: w.context, window: win, compacted: Boolean(compacted) };
|
|
47
|
+
w.info = info;
|
|
48
|
+
handoff.flag?.(info);
|
|
49
|
+
const why = compacted
|
|
50
|
+
? 'a compaction'
|
|
51
|
+
: `context ${pct}% (${count(w.context)} of ${count(win)})`;
|
|
52
|
+
write([`[${clock()}] handoff: ${why}, the session hands over`]);
|
|
53
|
+
onFlag(info);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const check = (model, compacted) => {
|
|
57
|
+
const win = windowOf(model);
|
|
58
|
+
const pct = win ? Math.round((100 * w.context) / win) : null;
|
|
59
|
+
// No share is under every threshold: a session of an unknown window hands over at a compaction.
|
|
60
|
+
const share = pct ?? 0;
|
|
61
|
+
if (pct === null && !compacted) unknownOnce(model);
|
|
62
|
+
if (!w.flagged && (compacted || share >= handoff.pct)) flag(win, pct, compacted);
|
|
63
|
+
// A project chat hands over by its summary turn, which goes on past the flag (AD-42).
|
|
64
|
+
if (handoff.soft) return;
|
|
65
|
+
if (w.flagged && (w.after > HARD_TURNS || share >= handoff.pct + HARD_OVER)) {
|
|
66
|
+
write([`[${clock()}] handoff: the session went on past its flag, stopped`]);
|
|
67
|
+
stop();
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
state: w,
|
|
73
|
+
see(a, model) {
|
|
74
|
+
if (a.context !== undefined) {
|
|
75
|
+
w.context = a.context;
|
|
76
|
+
if (w.flagged && a.turn !== w.turn) w.after++;
|
|
77
|
+
w.turn = a.turn;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (handoff?.pct && (a.context !== undefined || a.compacted))
|
|
81
|
+
check(model, Boolean(a.compacted));
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The last usage-limit state a session's stream gave, with its time; a failed write loses it.
|
|
88
|
+
* The last writer wins on purpose: it is the latest state (AD-34).
|
|
89
|
+
*/
|
|
90
|
+
function keepQuota(file, info) {
|
|
91
|
+
try {
|
|
92
|
+
writeAtomic(file, `${JSON.stringify({ at: new Date().toISOString(), info })}\n`);
|
|
93
|
+
} catch {
|
|
94
|
+
// A probe, not a record: the run goes on without it.
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The share of the window a session's chat shows mid-turn (AD-51): a `context` event at the
|
|
100
|
+
* first main-agent message, then each time the share moves by a point or more. While the window
|
|
101
|
+
* is unknown the share is null, and only the first message says so.
|
|
102
|
+
*/
|
|
103
|
+
function contextWatch(post, windowOf) {
|
|
104
|
+
/** @type {number | null | undefined} */
|
|
105
|
+
let last;
|
|
106
|
+
|
|
107
|
+
return (tokens, model) => {
|
|
108
|
+
const win = windowOf(model);
|
|
109
|
+
const pct = win ? Math.round((100 * tokens) / win) : null;
|
|
110
|
+
const moved =
|
|
111
|
+
last === undefined || (pct !== null && (last === null || Math.abs(pct - last) >= 1));
|
|
112
|
+
if (!moved) return;
|
|
113
|
+
last = pct;
|
|
114
|
+
post({ type: 'context', context: tokens, context_pct: pct, window: win });
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* What a chat needs of its session: stdin, the handoff watch (a flagged one closes), the pause,
|
|
120
|
+
* the context it holds and its share of the model's window (null when the window is unknown).
|
|
121
|
+
*/
|
|
122
|
+
const sessionOf = (child, watch, limit, windowOf) => ({
|
|
123
|
+
stdin: child.stdin,
|
|
124
|
+
handoff: () => watch.state,
|
|
125
|
+
pause: limit.pause,
|
|
126
|
+
context: () => {
|
|
127
|
+
const win = windowOf();
|
|
128
|
+
const tokens = watch.state.context;
|
|
129
|
+
|
|
130
|
+
return { context: tokens, context_pct: win ? Math.round((100 * tokens) / win) : null };
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
/** Write lines to a session's log while `live()`. */
|
|
135
|
+
const logWriter = (fd, live) => (lines) => {
|
|
136
|
+
try {
|
|
137
|
+
if (live() && lines.length) writeSync(fd, lines.map((l) => `${l}\n`).join(''));
|
|
138
|
+
} catch {
|
|
139
|
+
// A log that cannot be written loses its lines, not the run.
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Start the agent on `prompt`, a `start:` line in its log first: the log's time says the session
|
|
145
|
+
* is alive from its start. A chat session gets its prompt on stdin instead (chat.js). The
|
|
146
|
+
* adapter makes the command line and adds its env.
|
|
147
|
+
*/
|
|
148
|
+
function launchSession(s) {
|
|
149
|
+
const { adapter, agent, prompt, dir, resume, flags, fd, env = {}, chat } = s;
|
|
150
|
+
const command = adapter.command({ agent, resume, flags, chat: Boolean(chat), prompt });
|
|
151
|
+
writeSync(fd, `[${clock()}] start: ${command}${chat && prompt ? ` <<< "${prompt}"` : ''}\n`);
|
|
152
|
+
|
|
153
|
+
return spawnAgent(command, dir, { ...env, ...adapter.env(dir) }, Boolean(chat));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* A session's timeout that stands still while the session waits for the operator (`pause`);
|
|
158
|
+
* `waited()` is how long it stood. After `stop()` nothing starts it again.
|
|
159
|
+
*/
|
|
160
|
+
function sessionTimeout(ms, onTimeout) {
|
|
161
|
+
/** @type {{left: number, since: number, timer?: NodeJS.Timeout, waited: number,
|
|
162
|
+
* pausedAt: number | null, stopped: boolean}} */
|
|
163
|
+
const t = { left: ms, since: Date.now(), waited: 0, pausedAt: null, stopped: false };
|
|
164
|
+
const arm = () => {
|
|
165
|
+
t.since = Date.now();
|
|
166
|
+
t.timer = setTimeout(onTimeout, t.left);
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const resume = () => {
|
|
170
|
+
if (t.pausedAt === null) return;
|
|
171
|
+
t.waited += Date.now() - t.pausedAt;
|
|
172
|
+
t.pausedAt = null;
|
|
173
|
+
if (!t.stopped) arm();
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
arm();
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
pause: (on) => {
|
|
180
|
+
if (!on) return resume();
|
|
181
|
+
if (t.pausedAt !== null || t.stopped) return;
|
|
182
|
+
clearTimeout(t.timer);
|
|
183
|
+
t.left -= Date.now() - t.since;
|
|
184
|
+
t.pausedAt = Date.now();
|
|
185
|
+
},
|
|
186
|
+
stop: () => {
|
|
187
|
+
t.stopped = true;
|
|
188
|
+
clearTimeout(t.timer);
|
|
189
|
+
resume();
|
|
190
|
+
},
|
|
191
|
+
waited: () => t.waited,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @typedef {object} SessionSpec
|
|
197
|
+
* @property {any} adapter the provider adapter (adapter/index.js)
|
|
198
|
+
* @property {string} agent the agent command, its model and effort in it
|
|
199
|
+
* @property {string} prompt the first message
|
|
200
|
+
* @property {string} dir where the session works
|
|
201
|
+
* @property {string | null} [resume] the session id it goes on with
|
|
202
|
+
* @property {string} [flags] the flags of the session's kind (trusted, …)
|
|
203
|
+
* @property {number} fd the session log
|
|
204
|
+
* @property {number} timeoutMs
|
|
205
|
+
* @property {string} id the work's id: the name of its chat files
|
|
206
|
+
* @property {(pid: number) => void} [onSpawn]
|
|
207
|
+
* @property {Record<string, string>} [env]
|
|
208
|
+
* @property {any} [handoff] `{pct, windowOf(model, live), flag(info)}`
|
|
209
|
+
* @property {string | null} [quota] the file the last usage-limit state goes to
|
|
210
|
+
* @property {any} [chat] the chat's options (chat.js `openChat`)
|
|
211
|
+
* @property {((events: import('./activity.js').Neutral[]) => void) | null} [observe]
|
|
212
|
+
*/
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* One agent session (`SessionSpec`); resolves with its exit code, or `timeout` when it ran past
|
|
216
|
+
* `timeoutMs`: a hung session would hold the work's lock for good. Its stdout reaches the log
|
|
217
|
+
* through the adapter, its stderr as it is; `cost` is what the session reported at its end,
|
|
218
|
+
* `session_id` the id a rerun resumes it by, `limit` the usage limit that ended it, `gone` that
|
|
219
|
+
* the session it was to resume does not exist. `observe(events)` sees each line's neutral
|
|
220
|
+
* events; `onSpawn(pid)` the session's pid.
|
|
221
|
+
*
|
|
222
|
+
* `handoff` ({pct, windowOf(model, live), flag(info)}) watches the main agent's context: past
|
|
223
|
+
* `pct` of the window, or at a compaction, `flag` tells the session to hand over (the run's
|
|
224
|
+
* guard turns its tool calls into that instruction); one that keeps on working is stopped.
|
|
225
|
+
* `handoff: true` says the session was told to hand over, `handedOver` that it did.
|
|
226
|
+
*
|
|
227
|
+
* `chat` ({logs, input, answerMs, done, …}) opens the session to the operator (chat.js): its
|
|
228
|
+
* prompt goes in as the first message, stdin stays open until the work made its move, and the
|
|
229
|
+
* time it waits for the operator does not count against `timeoutMs`; `waited` is that time.
|
|
230
|
+
* @param {SessionSpec} s
|
|
231
|
+
* @returns {Promise<{code: number | string, cost?: import('./activity.js').Cost,
|
|
232
|
+
* session_id?: string, limit?: {resets: string | null}, gone: boolean, handoff: boolean,
|
|
233
|
+
* handedOver: boolean, model?: string, window?: number, result?: string, waited: number}>}
|
|
234
|
+
*/
|
|
235
|
+
export function runSession(s) {
|
|
236
|
+
const { adapter, fd, timeoutMs, id, onSpawn, handoff, quota, chat, observe } = s;
|
|
237
|
+
|
|
238
|
+
return new Promise((resolve) => {
|
|
239
|
+
const child = launchSession(s);
|
|
240
|
+
sessions.add(child);
|
|
241
|
+
if (child.pid) onSpawn?.(child.pid);
|
|
242
|
+
let open = true;
|
|
243
|
+
/** @type {Record<string, any>} */
|
|
244
|
+
const seen = {};
|
|
245
|
+
let gone = false;
|
|
246
|
+
const write = logWriter(fd, () => open);
|
|
247
|
+
const stop = (why) => () => {
|
|
248
|
+
killTree(child.pid);
|
|
249
|
+
end(why);
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
/** @type {ReturnType<typeof openChat> | null} */
|
|
253
|
+
let talk = null;
|
|
254
|
+
// A run's chat says the flag at once; a project chat at its summary turn (chat.js).
|
|
255
|
+
const flagged = (f) => (chat?.handover ? null : talk?.event({ type: 'handoff', ...f }));
|
|
256
|
+
// The window of a model: what the run knows of it, this session's own result as well.
|
|
257
|
+
const windowOf = (model = seen.model) => handoff?.windowOf?.(model, seen.window) ?? null;
|
|
258
|
+
const watch = handoffWatch(handoff, windowOf, write, {
|
|
259
|
+
stop: stop('handoff'),
|
|
260
|
+
onFlag: flagged,
|
|
261
|
+
});
|
|
262
|
+
const limit = sessionTimeout(timeoutMs, stop('timeout'));
|
|
263
|
+
/** @type {((tokens: number, model?: string) => void) | null} */
|
|
264
|
+
let shown = null;
|
|
265
|
+
if (chat) {
|
|
266
|
+
const parts = { ...sessionOf(child, watch, limit, windowOf), limited: () => !!seen.limit };
|
|
267
|
+
talk = openChat({ ...chat, ...parts, id, prompt: s.prompt, encode: adapter.encode });
|
|
268
|
+
shown = contextWatch((e) => talk?.event(e), windowOf);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const flushOut = byLine(child.stdout, (line) => {
|
|
272
|
+
const a = adapter.parse(line);
|
|
273
|
+
// The result's window first: the chat's context share of this line is taken of it.
|
|
274
|
+
absorb(seen, a);
|
|
275
|
+
if (talk) talk.see(a.events);
|
|
276
|
+
if (a.quota && quota) keepQuota(quota, a.quota);
|
|
277
|
+
observe?.(a.events);
|
|
278
|
+
write(a.lines);
|
|
279
|
+
if (open && a.context !== undefined) shown?.(a.context, seen.model);
|
|
280
|
+
if (open) watch.see(a, seen.model);
|
|
281
|
+
});
|
|
282
|
+
const flushErr = byLine(child.stderr, (line) => {
|
|
283
|
+
gone ||= adapter.gone(line);
|
|
284
|
+
write([line]);
|
|
285
|
+
});
|
|
286
|
+
function end(exit) {
|
|
287
|
+
if (!open) return;
|
|
288
|
+
flushOut();
|
|
289
|
+
flushErr();
|
|
290
|
+
open = false;
|
|
291
|
+
limit.stop();
|
|
292
|
+
// A project chat that closed its session says why: `idle`, `handoff`.
|
|
293
|
+
const code = talk?.why() ?? exit;
|
|
294
|
+
if (talk) talk.close(code, seen);
|
|
295
|
+
sessions.delete(child);
|
|
296
|
+
const { flagged, after } = watch.state;
|
|
297
|
+
// Told to hand over, and it went on past the flag: it read the guard's instruction, or the
|
|
298
|
+
// run stopped it. A flag on its last turn found a session already done.
|
|
299
|
+
// A project chat hands over by its summary turn, the operator's button too: unflagged.
|
|
300
|
+
const handedOver = code === 'handoff' || (flagged && after > 0);
|
|
301
|
+
resolve({ ...seen, code, gone, handoff: flagged, handedOver, waited: limit.waited() });
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
child.on('error', () => end(-1));
|
|
305
|
+
// `close` waits for the agent's output as well: its last lines, the result, may come after
|
|
306
|
+
// its exit.
|
|
307
|
+
child.on('close', (code) => end(code ?? -1));
|
|
308
|
+
letGo(child);
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** A tool the agent left running may hold its output open: the pipes close soon after it exits. */
|
|
313
|
+
function letGo(child) {
|
|
314
|
+
child.on('exit', () =>
|
|
315
|
+
setTimeout(() => {
|
|
316
|
+
child.stdout.destroy();
|
|
317
|
+
child.stderr.destroy();
|
|
318
|
+
}, 2000).unref(),
|
|
319
|
+
);
|
|
320
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// The engine's storage (docs/Session-Engine.md AD-23): one root, `.gcf-session/`, in the project.
|
|
2
|
+
// The paths are frozen: the board reads `runs/quota.json` and `runs/<id>.log`, the core's deny
|
|
3
|
+
// groups name `runs/**`. Writes to shared files are atomic (AD-34).
|
|
4
|
+
import { renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
|
|
7
|
+
export const ROOT = '.gcf-session';
|
|
8
|
+
|
|
9
|
+
/** `.gcf-session/runs`: item runs, their chats, logs, records, windows and quota. */
|
|
10
|
+
export const runsDir = (dir) => path.join(dir, ROOT, 'runs');
|
|
11
|
+
|
|
12
|
+
/** `.gcf-session/chats`: the project chat's threads. */
|
|
13
|
+
export const chatsDir = (dir) => path.join(dir, ROOT, 'chats');
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Write `text` to `file` whole or not at all: a temporary file beside it, then a rename. A
|
|
17
|
+
* reader never sees half of it, and two writers leave one whole file.
|
|
18
|
+
*/
|
|
19
|
+
export function writeAtomic(file, text) {
|
|
20
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
21
|
+
try {
|
|
22
|
+
writeFileSync(tmp, text);
|
|
23
|
+
renameSync(tmp, file);
|
|
24
|
+
} catch (e) {
|
|
25
|
+
rmSync(tmp, { force: true });
|
|
26
|
+
throw e;
|
|
27
|
+
}
|
|
28
|
+
}
|