@agentshouse/kit 0.2.1-alpha.9 → 0.2.1-c03721a31074a
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 +48 -29
- package/dist/abort.js +9 -0
- package/dist/acp.js +49 -86
- package/dist/agents.js +333 -52
- package/dist/api.js +40 -4
- package/dist/app-git.js +48 -0
- package/dist/bootstrap.json +10 -14
- package/dist/bridge.js +93 -78
- package/dist/calls.js +63 -0
- package/dist/chatgpt.js +32 -0
- package/dist/clis-main.js +23 -0
- package/dist/clis.js +217 -72
- package/dist/codex-main.js +58 -0
- package/dist/connection.js +126 -0
- package/dist/conversations.js +642 -263
- package/dist/copies.js +237 -173
- package/dist/end.js +27 -0
- package/dist/enrol-main.js +7 -1
- package/dist/events.js +525 -0
- package/dist/files.js +140 -17
- package/dist/git-main.js +2 -2
- package/dist/git.js +16 -10
- package/dist/home.js +127 -9
- package/dist/house-cli.js +300 -0
- package/dist/house-main.js +5 -102
- package/dist/instructions.js +16 -17
- package/dist/kit-cli.js +68 -0
- package/dist/kit-main.js +8 -16
- package/dist/login.js +128 -95
- package/dist/logout.js +19 -0
- package/dist/mcp.js +41 -0
- package/dist/operation.js +7 -4
- package/dist/parts.js +82 -0
- package/dist/placement.js +39 -0
- package/dist/refusals.js +53 -0
- package/dist/relay.js +20 -0
- package/dist/resident.js +139 -52
- package/dist/scope.js +206 -0
- package/dist/secret-input.js +7 -2
- package/dist/service-main.js +9 -0
- package/dist/service.js +55 -0
- package/dist/shell.js +3 -0
- package/dist/sign-in.js +50 -31
- package/dist/skills.js +20 -37
- package/dist/transcription.js +214 -0
- package/dist/translate.js +2 -8
- package/dist/update.js +163 -0
- package/dist/web-wizard.js +100 -0
- package/dist/wire.js +22 -0
- package/dist/work.js +346 -0
- package/package.json +14 -16
- package/dist/blocks.js +0 -51
- package/dist/stream.js +0 -47
- package/how-we-work.md +0 -25
- package/skills/ask-matt/LICENSE +0 -21
- package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
- package/skills/ask-matt/SKILL.md +0 -91
- package/skills/ask-matt/agents/openai.yaml +0 -4
- package/skills/code-review/LICENSE +0 -21
- package/skills/code-review/SKILL.md +0 -89
- package/skills/code-review/agents/openai.yaml +0 -3
- package/skills/codebase-design/DEEPENING.md +0 -37
- package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
- package/skills/codebase-design/LICENSE +0 -21
- package/skills/codebase-design/SKILL.md +0 -116
- package/skills/codebase-design/agents/openai.yaml +0 -3
- package/skills/diagnosing-bugs/LICENSE +0 -21
- package/skills/diagnosing-bugs/SKILL.md +0 -140
- package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
- package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
- package/skills/domain-modeling/ADR-FORMAT.md +0 -47
- package/skills/domain-modeling/CONTEXT-FORMAT.md +0 -60
- package/skills/domain-modeling/LICENSE +0 -21
- package/skills/domain-modeling/SKILL.md +0 -76
- package/skills/domain-modeling/agents/openai.yaml +0 -3
- package/skills/grill-me/LICENSE +0 -21
- package/skills/grill-me/SKILL.md +0 -8
- package/skills/grill-me/agents/openai.yaml +0 -4
- package/skills/grill-with-docs/LICENSE +0 -21
- package/skills/grill-with-docs/SKILL.md +0 -8
- package/skills/grill-with-docs/agents/openai.yaml +0 -4
- package/skills/grilling/LICENSE +0 -21
- package/skills/grilling/SKILL.md +0 -30
- package/skills/grilling/agents/openai.yaml +0 -3
- package/skills/handoff/LICENSE +0 -21
- package/skills/handoff/SKILL.md +0 -17
- package/skills/handoff/agents/openai.yaml +0 -4
- package/skills/implement/LICENSE +0 -21
- package/skills/implement/SKILL.md +0 -16
- package/skills/implement/agents/openai.yaml +0 -4
- package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
- package/skills/improve-codebase-architecture/LICENSE +0 -21
- package/skills/improve-codebase-architecture/SKILL.md +0 -72
- package/skills/improve-codebase-architecture/agents/openai.yaml +0 -4
- package/skills/prototype/LICENSE +0 -21
- package/skills/prototype/LOGIC.md +0 -67
- package/skills/prototype/SKILL.md +0 -28
- package/skills/prototype/UI.md +0 -112
- package/skills/prototype/agents/openai.yaml +0 -3
- package/skills/research/LICENSE +0 -21
- package/skills/research/SKILL.md +0 -14
- package/skills/research/agents/openai.yaml +0 -3
- package/skills/resolving-merge-conflicts/LICENSE +0 -21
- package/skills/resolving-merge-conflicts/SKILL.md +0 -16
- package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
- package/skills/tdd/LICENSE +0 -21
- package/skills/tdd/SKILL.md +0 -40
- package/skills/tdd/agents/openai.yaml +0 -3
- package/skills/tdd/mocking.md +0 -59
- package/skills/tdd/tests.md +0 -77
- package/skills/teach/GLOSSARY-FORMAT.md +0 -35
- package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
- package/skills/teach/LICENSE +0 -21
- package/skills/teach/MISSION-FORMAT.md +0 -31
- package/skills/teach/RESOURCES-FORMAT.md +0 -32
- package/skills/teach/SKILL.md +0 -141
- package/skills/teach/agents/openai.yaml +0 -4
- package/skills/to-questionnaire/LICENSE +0 -21
- package/skills/to-questionnaire/SKILL.md +0 -55
- package/skills/to-questionnaire/agents/openai.yaml +0 -4
- package/skills/to-spec/LICENSE +0 -21
- package/skills/to-spec/SKILL.md +0 -76
- package/skills/to-spec/agents/openai.yaml +0 -4
- package/skills/to-tickets/LICENSE +0 -21
- package/skills/to-tickets/SKILL.md +0 -106
- package/skills/to-tickets/agents/openai.yaml +0 -4
- package/skills/wait-what/LICENSE +0 -21
- package/skills/wait-what/SKILL.md +0 -8
- package/skills/wait-what/agents/openai.yaml +0 -4
- package/skills/wayfinder/LICENSE +0 -21
- package/skills/wayfinder/SKILL.md +0 -129
- package/skills/wayfinder/agents/openai.yaml +0 -4
- package/skills/writing-for-agents/LICENSE +0 -21
- package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
- package/skills/writing-for-agents/SKILL.md +0 -83
- package/skills/writing-for-agents/agents/openai.yaml +0 -3
package/dist/update.js
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { run } from './clis.js';
|
|
7
|
+
import { kitHome, workspaceRoot } from './home.js';
|
|
8
|
+
import { service, throughSudo } from './placement.js';
|
|
9
|
+
const BOOTSTRAP_BYTES = 1_000_000;
|
|
10
|
+
// A bootstrap is at most a million bytes, so two minutes carry it over ten kilobytes a second and no stalled host holds the update queue.
|
|
11
|
+
const BOOTSTRAP_MS = 120_000;
|
|
12
|
+
function directory() {
|
|
13
|
+
return join(kitHome(), 'update');
|
|
14
|
+
}
|
|
15
|
+
function pending() {
|
|
16
|
+
return join(directory(), 'pending.json');
|
|
17
|
+
}
|
|
18
|
+
function outcome() {
|
|
19
|
+
return join(directory(), 'outcome');
|
|
20
|
+
}
|
|
21
|
+
function unit(callId) {
|
|
22
|
+
return `house-kit-update-${callId}`;
|
|
23
|
+
}
|
|
24
|
+
function manager() {
|
|
25
|
+
return service() === 'user' ? ['--user'] : [];
|
|
26
|
+
}
|
|
27
|
+
function elevated() {
|
|
28
|
+
return throughSudo() ? ['sudo', '-n'] : [];
|
|
29
|
+
}
|
|
30
|
+
function account() {
|
|
31
|
+
return throughSudo() ? [`--uid=${process.getuid()}`, `--gid=${process.getgid()}`] : [];
|
|
32
|
+
}
|
|
33
|
+
function placement() {
|
|
34
|
+
return service() === 'user' ? ['--workspace', workspaceRoot()] : ['--linux'];
|
|
35
|
+
}
|
|
36
|
+
function record() {
|
|
37
|
+
return `outcome=$1; shift; bash "$0" "$@"; printf %s "$?" > "$outcome"; ${[...elevated(), 'systemctl', ...manager(), 'start', 'house-kit.service'].join(' ')}`;
|
|
38
|
+
}
|
|
39
|
+
async function pendingCall() {
|
|
40
|
+
try {
|
|
41
|
+
return JSON.parse(await readFile(pending(), 'utf8')).input_id;
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async function recorded() {
|
|
48
|
+
const status = await readFile(outcome(), 'utf8').catch(() => null);
|
|
49
|
+
if (status === null)
|
|
50
|
+
return null;
|
|
51
|
+
return status.trim() === '0' ? {} : { refused: `the Kit bootstrap exited with ${status.trim()}` };
|
|
52
|
+
}
|
|
53
|
+
async function forget(callId) {
|
|
54
|
+
if ((await pendingCall()) !== callId)
|
|
55
|
+
return;
|
|
56
|
+
await rm(pending(), { force: true });
|
|
57
|
+
await rm(outcome(), { force: true });
|
|
58
|
+
}
|
|
59
|
+
async function finished(callId) {
|
|
60
|
+
for (;;) {
|
|
61
|
+
const result = await recorded();
|
|
62
|
+
if (result !== null)
|
|
63
|
+
return result;
|
|
64
|
+
// systemctl answers at once, so ten seconds only bounds a stuck service manager.
|
|
65
|
+
if ((await run('systemctl', [...manager(), 'is-active', '--quiet', unit(callId)], 10_000)).status !== 0) {
|
|
66
|
+
return (await recorded()) ?? { refused: 'the Kit bootstrap did not finish' };
|
|
67
|
+
}
|
|
68
|
+
// The bootstrap runs for minutes, so checking each second sees its outcome within a second at little cost.
|
|
69
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async function bounded(body, most) {
|
|
73
|
+
const chunks = [];
|
|
74
|
+
let bytes = 0;
|
|
75
|
+
for await (const chunk of body ?? []) {
|
|
76
|
+
bytes += chunk.length;
|
|
77
|
+
if (bytes > most)
|
|
78
|
+
return null;
|
|
79
|
+
chunks.push(chunk);
|
|
80
|
+
}
|
|
81
|
+
return Buffer.concat(chunks, bytes);
|
|
82
|
+
}
|
|
83
|
+
function rerun(script, callId, flags) {
|
|
84
|
+
return new Promise((resolve) => {
|
|
85
|
+
const [command, ...args] = [
|
|
86
|
+
...elevated(),
|
|
87
|
+
'systemd-run',
|
|
88
|
+
...manager(),
|
|
89
|
+
...account(),
|
|
90
|
+
'--wait',
|
|
91
|
+
'--collect',
|
|
92
|
+
'--quiet',
|
|
93
|
+
`--unit=${unit(callId)}`,
|
|
94
|
+
`--setenv=HOME=${homedir()}`,
|
|
95
|
+
`--setenv=HOUSE_KIT_HOME=${kitHome()}`,
|
|
96
|
+
'/bin/sh',
|
|
97
|
+
'-c',
|
|
98
|
+
record(),
|
|
99
|
+
script,
|
|
100
|
+
outcome(),
|
|
101
|
+
...placement(),
|
|
102
|
+
...flags,
|
|
103
|
+
];
|
|
104
|
+
const child = spawn(command, args, { stdio: ['ignore', 'ignore', 'inherit'] });
|
|
105
|
+
child.on('error', () => resolve({ refused: 'no service manager runs this Kit process' }));
|
|
106
|
+
child.on('close', () => resolve(null));
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
export class KitUpdate {
|
|
110
|
+
calls;
|
|
111
|
+
queue = Promise.resolve();
|
|
112
|
+
constructor(calls) {
|
|
113
|
+
this.calls = calls;
|
|
114
|
+
}
|
|
115
|
+
async resumed() {
|
|
116
|
+
const callId = await pendingCall();
|
|
117
|
+
if (callId === null)
|
|
118
|
+
return;
|
|
119
|
+
await this.enqueue(callId, () => finished(callId));
|
|
120
|
+
}
|
|
121
|
+
start(call) {
|
|
122
|
+
void this.enqueue(call.input_id, () => this.carry(call));
|
|
123
|
+
}
|
|
124
|
+
enqueue(callId, work) {
|
|
125
|
+
const before = this.queue;
|
|
126
|
+
const carried = this.calls.carry(callId, async () => {
|
|
127
|
+
await before;
|
|
128
|
+
return { result: await work(), returned: () => forget(callId) };
|
|
129
|
+
});
|
|
130
|
+
this.queue = before.then(() => carried);
|
|
131
|
+
return carried;
|
|
132
|
+
}
|
|
133
|
+
async carry(call) {
|
|
134
|
+
if ((await pendingCall()) === call.input_id)
|
|
135
|
+
return finished(call.input_id);
|
|
136
|
+
const deadline = AbortSignal.timeout(BOOTSTRAP_MS);
|
|
137
|
+
let script;
|
|
138
|
+
try {
|
|
139
|
+
const answer = await fetch(call.bootstrap, { signal: deadline });
|
|
140
|
+
if (!answer.ok)
|
|
141
|
+
return { refused: `the Kit bootstrap could not be downloaded: ${answer.status}` };
|
|
142
|
+
script = await bounded(answer.body, BOOTSTRAP_BYTES);
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
if (!deadline.aborted)
|
|
146
|
+
throw error;
|
|
147
|
+
return { refused: `the Kit bootstrap did not arrive within ${BOOTSTRAP_MS / 1000} seconds` };
|
|
148
|
+
}
|
|
149
|
+
if (script === null)
|
|
150
|
+
return { refused: `the Kit bootstrap is larger than ${BOOTSTRAP_BYTES} bytes` };
|
|
151
|
+
if (createHash('sha256').update(script).digest('hex') !== call.sha256) {
|
|
152
|
+
return { refused: 'the Kit bootstrap does not match its sha256' };
|
|
153
|
+
}
|
|
154
|
+
await mkdir(directory(), { recursive: true, mode: 0o700 });
|
|
155
|
+
const path = join(directory(), 'connect-linux.sh');
|
|
156
|
+
await writeFile(path, script, { mode: 0o700 });
|
|
157
|
+
await rm(outcome(), { force: true });
|
|
158
|
+
await writeFile(pending(), `${JSON.stringify({ input_id: call.input_id })}\n`, { mode: 0o600 });
|
|
159
|
+
return (await rerun(path, call.input_id, call.update_clis ? ['--update-clis'] : [])) ?? (await recorded()) ?? {
|
|
160
|
+
refused: 'the Kit bootstrap did not finish',
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { loginPath, onLoginPath, withoutProxy } from './clis.js';
|
|
2
|
+
import { killTree } from './scope.js';
|
|
3
|
+
const TERMINAL = 'xterm-256color';
|
|
4
|
+
const SIGNALLED = 128;
|
|
5
|
+
const NO_BASH = "bash is not on the login shell's PATH";
|
|
6
|
+
const LOST = { refused: 'the Kit lost its connection to House' };
|
|
7
|
+
function causeOf(error) {
|
|
8
|
+
return error instanceof Error ? error.message : String(error);
|
|
9
|
+
}
|
|
10
|
+
function failed(cause) {
|
|
11
|
+
process.stderr.write(`kit: a Web Wizard could not start: ${cause}\n`);
|
|
12
|
+
return { refused: `the script could not start: ${cause}` };
|
|
13
|
+
}
|
|
14
|
+
export class WebWizards {
|
|
15
|
+
calls;
|
|
16
|
+
send;
|
|
17
|
+
runs = new Map();
|
|
18
|
+
constructor(calls, send) {
|
|
19
|
+
this.calls = calls;
|
|
20
|
+
this.send = send;
|
|
21
|
+
}
|
|
22
|
+
start(call) {
|
|
23
|
+
void this.calls.carry(call.input_id, () => this.run(call));
|
|
24
|
+
}
|
|
25
|
+
notified(notification) {
|
|
26
|
+
const run = this.runs.get(String(notification.wizard));
|
|
27
|
+
if (run === undefined)
|
|
28
|
+
return;
|
|
29
|
+
if (notification.type === 'wizard_input')
|
|
30
|
+
this.typed(run, String(notification.data));
|
|
31
|
+
if (notification.type === 'wizard_size')
|
|
32
|
+
this.resized(run, { cols: Number(notification.cols), rows: Number(notification.rows) });
|
|
33
|
+
if (notification.type === 'wizard_end')
|
|
34
|
+
this.end(run, {});
|
|
35
|
+
}
|
|
36
|
+
disconnected() {
|
|
37
|
+
for (const run of this.runs.values())
|
|
38
|
+
this.end(run, LOST);
|
|
39
|
+
}
|
|
40
|
+
async run(call) {
|
|
41
|
+
const run = { terminal: null, size: null, typed: [], ended: null, sized: Promise.withResolvers() };
|
|
42
|
+
this.runs.set(call.input_id, run);
|
|
43
|
+
try {
|
|
44
|
+
return { result: await this.ran(call.input_id, call.script, run) };
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
this.runs.delete(call.input_id);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async ran(wizard, script, run) {
|
|
51
|
+
let terminal;
|
|
52
|
+
try {
|
|
53
|
+
const { spawn } = await import('@lydell/node-pty');
|
|
54
|
+
const shell = await onLoginPath('bash');
|
|
55
|
+
if (shell === null)
|
|
56
|
+
return failed(NO_BASH);
|
|
57
|
+
const path = await loginPath();
|
|
58
|
+
await run.sized.promise;
|
|
59
|
+
if (run.ended !== null)
|
|
60
|
+
return run.ended;
|
|
61
|
+
terminal = spawn(shell, ['-c', script], {
|
|
62
|
+
name: TERMINAL,
|
|
63
|
+
...run.size,
|
|
64
|
+
cwd: process.cwd(),
|
|
65
|
+
env: { ...withoutProxy(), PATH: path },
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
return failed(causeOf(error));
|
|
70
|
+
}
|
|
71
|
+
run.terminal = terminal;
|
|
72
|
+
terminal.onData((data) => this.send({ type: 'wizard_output', wizard, data }));
|
|
73
|
+
for (const data of run.typed.splice(0))
|
|
74
|
+
terminal.write(data);
|
|
75
|
+
const exited = await new Promise((resolve) => terminal.onExit(resolve));
|
|
76
|
+
return run.ended ?? { exit_code: exited.signal ? SIGNALLED + exited.signal : exited.exitCode };
|
|
77
|
+
}
|
|
78
|
+
typed(run, data) {
|
|
79
|
+
if (run.terminal === null)
|
|
80
|
+
run.typed.push(data);
|
|
81
|
+
else
|
|
82
|
+
run.terminal.write(data);
|
|
83
|
+
}
|
|
84
|
+
resized(run, size) {
|
|
85
|
+
run.size = size;
|
|
86
|
+
run.sized.resolve();
|
|
87
|
+
try {
|
|
88
|
+
run.terminal?.resize(size.cols, size.rows);
|
|
89
|
+
}
|
|
90
|
+
catch { }
|
|
91
|
+
}
|
|
92
|
+
end(run, result) {
|
|
93
|
+
if (run.ended !== null)
|
|
94
|
+
return;
|
|
95
|
+
run.ended = result;
|
|
96
|
+
run.sized.resolve();
|
|
97
|
+
if (run.terminal !== null)
|
|
98
|
+
killTree(run.terminal);
|
|
99
|
+
}
|
|
100
|
+
}
|
package/dist/wire.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { newerThanHouse, updateRequired } from './end.js';
|
|
2
|
+
import { machine } from './placement.js';
|
|
3
|
+
import { refusalOf } from './refusals.js';
|
|
4
|
+
export const KIT_WIRE = 3;
|
|
5
|
+
export const KIT_WIRE_HEADER = 'x-house-kit-wire';
|
|
6
|
+
function newer(text) {
|
|
7
|
+
let body;
|
|
8
|
+
try {
|
|
9
|
+
body = JSON.parse(text);
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
return body?.error?.code === 'kit_newer_than_house' ? body.error : null;
|
|
15
|
+
}
|
|
16
|
+
export function endUnserved(text) {
|
|
17
|
+
if (refusalOf(text)?.code === 'kit_update_required')
|
|
18
|
+
updateRequired();
|
|
19
|
+
const refused = newer(text);
|
|
20
|
+
if (refused !== null)
|
|
21
|
+
newerThanHouse(refused.release, refused.commands[machine()]);
|
|
22
|
+
}
|
package/dist/work.js
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { once } from 'node:events';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
import { rm } from 'node:fs/promises';
|
|
5
|
+
import { createServer } from 'node:net';
|
|
6
|
+
import { join } from 'node:path';
|
|
7
|
+
import { RUNNING_JOB } from './events.js';
|
|
8
|
+
import { kitHome, socketDirectory } from './home.js';
|
|
9
|
+
const ENDED_TASK = new Set(['completed', 'failed', 'killed']);
|
|
10
|
+
const SCHEDULING = new Set(['CronCreate', 'CronDelete', 'ScheduleWakeup']);
|
|
11
|
+
const QUEUE_LIST = 'house-kit-queue-';
|
|
12
|
+
const SYNC = 'house-kit-sync-';
|
|
13
|
+
const WAKEUP = /Next wakeup scheduled for (\d\d):(\d\d):(\d\d) \(in (\d+)s\)/;
|
|
14
|
+
const HALF_DAY = 12 * 60 * 60 * 1000;
|
|
15
|
+
export function codexLauncher() {
|
|
16
|
+
return join(kitHome(), 'launchers', 'codex');
|
|
17
|
+
}
|
|
18
|
+
export class Work {
|
|
19
|
+
changed;
|
|
20
|
+
jobs = new Set();
|
|
21
|
+
constructor(changed) {
|
|
22
|
+
this.changed = changed;
|
|
23
|
+
}
|
|
24
|
+
async open(_cli) {
|
|
25
|
+
return {};
|
|
26
|
+
}
|
|
27
|
+
async settled() { }
|
|
28
|
+
read(message) {
|
|
29
|
+
this.take(message);
|
|
30
|
+
this.changed();
|
|
31
|
+
}
|
|
32
|
+
take(message) {
|
|
33
|
+
const update = message.params?.update;
|
|
34
|
+
if (message.method !== 'session/update' || update?.asyncTaskId === undefined)
|
|
35
|
+
return;
|
|
36
|
+
if (update.sessionUpdate === 'async_task_spawned')
|
|
37
|
+
this.jobs.add(update.asyncTaskId);
|
|
38
|
+
if (update.sessionUpdate === 'async_task_state_update' && !RUNNING_JOB.has(update.state))
|
|
39
|
+
this.jobs.delete(update.asyncTaskId);
|
|
40
|
+
}
|
|
41
|
+
busy() {
|
|
42
|
+
return this.jobs.size > 0;
|
|
43
|
+
}
|
|
44
|
+
close() { }
|
|
45
|
+
}
|
|
46
|
+
class ClaudeWork extends Work {
|
|
47
|
+
cwd;
|
|
48
|
+
level = new Set();
|
|
49
|
+
listed = new Set();
|
|
50
|
+
started = new Set();
|
|
51
|
+
running = false;
|
|
52
|
+
hooks = new Set();
|
|
53
|
+
calls = new Map();
|
|
54
|
+
crons = new Map();
|
|
55
|
+
wakeups = [];
|
|
56
|
+
goal = false;
|
|
57
|
+
constructor(cwd, changed) {
|
|
58
|
+
super(changed);
|
|
59
|
+
this.cwd = cwd;
|
|
60
|
+
}
|
|
61
|
+
take(message) {
|
|
62
|
+
super.take(message);
|
|
63
|
+
if (message.method === '_claude/sdkMessage')
|
|
64
|
+
this.sdk(message.params.message);
|
|
65
|
+
if (message.method === 'session/update')
|
|
66
|
+
this.update(message.params.update);
|
|
67
|
+
}
|
|
68
|
+
busy() {
|
|
69
|
+
return (super.busy() ||
|
|
70
|
+
this.level.size > 0 ||
|
|
71
|
+
this.started.size > 0 ||
|
|
72
|
+
this.running ||
|
|
73
|
+
this.hooks.size > 0 ||
|
|
74
|
+
this.crons.size > 0 ||
|
|
75
|
+
this.wakeups.length > 0 ||
|
|
76
|
+
this.goal ||
|
|
77
|
+
this.durable());
|
|
78
|
+
}
|
|
79
|
+
sdk(message) {
|
|
80
|
+
switch (message.subtype) {
|
|
81
|
+
case 'background_tasks_changed':
|
|
82
|
+
this.level = new Set(message.tasks.map((task) => task.task_id));
|
|
83
|
+
for (const id of this.level) {
|
|
84
|
+
this.listed.add(id);
|
|
85
|
+
this.started.delete(id);
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
case 'task_started':
|
|
89
|
+
if (!this.listed.has(message.task_id))
|
|
90
|
+
this.started.add(message.task_id);
|
|
91
|
+
break;
|
|
92
|
+
case 'task_updated':
|
|
93
|
+
if (ENDED_TASK.has(message.patch.status))
|
|
94
|
+
this.started.delete(message.task_id);
|
|
95
|
+
break;
|
|
96
|
+
case 'task_notification':
|
|
97
|
+
this.started.delete(message.task_id);
|
|
98
|
+
break;
|
|
99
|
+
case 'session_state_changed':
|
|
100
|
+
if (message.state === 'running' && !this.running)
|
|
101
|
+
this.wakeups = this.wakeups.filter((due) => due > Date.now());
|
|
102
|
+
this.running = message.state === 'running';
|
|
103
|
+
break;
|
|
104
|
+
case 'hook_started':
|
|
105
|
+
this.hooks.add(message.hook_id);
|
|
106
|
+
break;
|
|
107
|
+
case 'hook_response':
|
|
108
|
+
this.hooks.delete(message.hook_id);
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
update(update) {
|
|
113
|
+
const air = update._meta?.jetbrains?.air;
|
|
114
|
+
if (update.sessionUpdate === 'session_info_update' && air !== undefined && 'goal' in air) {
|
|
115
|
+
this.goal = air.goal?.status === 'active';
|
|
116
|
+
}
|
|
117
|
+
if (update.sessionUpdate !== 'tool_call' && update.sessionUpdate !== 'tool_call_update')
|
|
118
|
+
return;
|
|
119
|
+
const tool = update._meta?.claudeCode?.toolName;
|
|
120
|
+
if (update.sessionUpdate === 'tool_call' && SCHEDULING.has(tool ?? ''))
|
|
121
|
+
this.calls.set(update.toolCallId, { tool });
|
|
122
|
+
const call = this.calls.get(update.toolCallId);
|
|
123
|
+
if (call === undefined)
|
|
124
|
+
return;
|
|
125
|
+
if (update.rawInput !== undefined)
|
|
126
|
+
call.input = update.rawInput;
|
|
127
|
+
if (update.content !== undefined)
|
|
128
|
+
call.content = update.content;
|
|
129
|
+
if (update.status === 'failed')
|
|
130
|
+
this.calls.delete(update.toolCallId);
|
|
131
|
+
if (update.status !== 'completed')
|
|
132
|
+
return;
|
|
133
|
+
this.calls.delete(update.toolCallId);
|
|
134
|
+
if (call.tool === 'CronCreate')
|
|
135
|
+
this.crons.set(update.toolCallId, JSON.stringify(call.content ?? []));
|
|
136
|
+
if (call.tool === 'ScheduleWakeup')
|
|
137
|
+
this.wakeup(call);
|
|
138
|
+
if (call.tool !== 'CronDelete')
|
|
139
|
+
return;
|
|
140
|
+
for (const [id, text] of this.crons)
|
|
141
|
+
if (text.includes(String(call.input?.id)))
|
|
142
|
+
this.crons.delete(id);
|
|
143
|
+
}
|
|
144
|
+
wakeup(call) {
|
|
145
|
+
if (call.input?.stop === true) {
|
|
146
|
+
this.wakeups = [];
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const time = WAKEUP.exec(JSON.stringify(call.content ?? []));
|
|
150
|
+
if (time === null) {
|
|
151
|
+
this.wakeups.push(Infinity);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const fires = Date.now() + Number(time[4]) * 1000;
|
|
155
|
+
const due = new Date(fires);
|
|
156
|
+
due.setHours(Number(time[1]), Number(time[2]), Number(time[3]), 0);
|
|
157
|
+
if (due.getTime() - fires > HALF_DAY)
|
|
158
|
+
due.setDate(due.getDate() - 1);
|
|
159
|
+
if (fires - due.getTime() > HALF_DAY)
|
|
160
|
+
due.setDate(due.getDate() + 1);
|
|
161
|
+
this.wakeups.push(due.getTime());
|
|
162
|
+
}
|
|
163
|
+
durable() {
|
|
164
|
+
let text;
|
|
165
|
+
try {
|
|
166
|
+
text = readFileSync(join(this.cwd, '.claude', 'scheduled_tasks.json'), 'utf8');
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
return error.code !== 'ENOENT';
|
|
170
|
+
}
|
|
171
|
+
try {
|
|
172
|
+
return JSON.parse(text).tasks?.length !== 0;
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
class CodexWork extends Work {
|
|
180
|
+
threads = new Map();
|
|
181
|
+
queues = new Map();
|
|
182
|
+
goals = new Set();
|
|
183
|
+
items = new Set();
|
|
184
|
+
hooks = new Set();
|
|
185
|
+
starting = new Set();
|
|
186
|
+
settling = [];
|
|
187
|
+
memories = null;
|
|
188
|
+
lost = false;
|
|
189
|
+
server = null;
|
|
190
|
+
path = null;
|
|
191
|
+
socket = null;
|
|
192
|
+
asked = 0;
|
|
193
|
+
heard = 0;
|
|
194
|
+
proving = -1;
|
|
195
|
+
proven = -1;
|
|
196
|
+
async open(cli) {
|
|
197
|
+
this.path = join(socketDirectory(), `${randomUUID()}.sock`);
|
|
198
|
+
this.server = createServer((socket) => this.connected(socket));
|
|
199
|
+
this.server.listen(this.path);
|
|
200
|
+
await once(this.server, 'listening');
|
|
201
|
+
return { CODEX_PATH: codexLauncher(), HOUSE_KIT_CODEX: cli, HOUSE_KIT_CODEX_STREAM: this.path };
|
|
202
|
+
}
|
|
203
|
+
settled() {
|
|
204
|
+
return new Promise((resolve) => {
|
|
205
|
+
this.settling.push(resolve);
|
|
206
|
+
this.settle();
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
take(message) {
|
|
210
|
+
super.take(message);
|
|
211
|
+
this.heard++;
|
|
212
|
+
}
|
|
213
|
+
busy() {
|
|
214
|
+
if (super.busy() ||
|
|
215
|
+
this.lost ||
|
|
216
|
+
this.memories !== false ||
|
|
217
|
+
[...this.threads.values()].some(Boolean) ||
|
|
218
|
+
this.queues.size > 0 ||
|
|
219
|
+
this.goals.size > 0 ||
|
|
220
|
+
this.items.size > 0 ||
|
|
221
|
+
this.hooks.size > 0) {
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
if (this.proven === this.heard)
|
|
225
|
+
return false;
|
|
226
|
+
this.prove();
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
close() {
|
|
230
|
+
this.server?.close();
|
|
231
|
+
if (this.path !== null)
|
|
232
|
+
void rm(this.path, { force: true });
|
|
233
|
+
}
|
|
234
|
+
connected(socket) {
|
|
235
|
+
this.socket = socket;
|
|
236
|
+
let buffer = '';
|
|
237
|
+
socket.on('error', () => this.lose());
|
|
238
|
+
socket.on('close', () => this.lose());
|
|
239
|
+
socket.setEncoding('utf8').on('data', (chunk) => {
|
|
240
|
+
buffer += chunk;
|
|
241
|
+
for (let index = buffer.indexOf('\n'); index >= 0; index = buffer.indexOf('\n')) {
|
|
242
|
+
const message = this.parsed(buffer.slice(0, index));
|
|
243
|
+
buffer = buffer.slice(index + 1);
|
|
244
|
+
if (message !== null)
|
|
245
|
+
this.stream(message, socket);
|
|
246
|
+
}
|
|
247
|
+
this.settle();
|
|
248
|
+
this.changed();
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
parsed(line) {
|
|
252
|
+
try {
|
|
253
|
+
return JSON.parse(line);
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
this.lost = true;
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
lose() {
|
|
261
|
+
this.lost = true;
|
|
262
|
+
this.changed();
|
|
263
|
+
}
|
|
264
|
+
prove() {
|
|
265
|
+
if (this.proving === this.heard)
|
|
266
|
+
return;
|
|
267
|
+
this.proving = this.heard;
|
|
268
|
+
this.socket.write(`${JSON.stringify({ jsonrpc: '2.0', id: `${SYNC}${this.heard}`, method: 'house-kit/sync' })}\n`);
|
|
269
|
+
}
|
|
270
|
+
settle() {
|
|
271
|
+
if (this.starting.size === 0)
|
|
272
|
+
for (const resolve of this.settling.splice(0))
|
|
273
|
+
resolve();
|
|
274
|
+
}
|
|
275
|
+
stream(message, socket) {
|
|
276
|
+
const params = message.params;
|
|
277
|
+
switch (message.method) {
|
|
278
|
+
case 'thread/status/changed':
|
|
279
|
+
this.threads.set(params.threadId, params.status.type === 'active' && params.status.activeFlags.length === 0);
|
|
280
|
+
break;
|
|
281
|
+
case 'thread/closed':
|
|
282
|
+
this.threads.delete(params.threadId);
|
|
283
|
+
break;
|
|
284
|
+
case 'thread/queue/changed':
|
|
285
|
+
this.queues.set(params.threadId, ++this.asked);
|
|
286
|
+
socket.write(`${JSON.stringify({ jsonrpc: '2.0', id: `${QUEUE_LIST}${this.asked}`, method: 'thread/queue/list', params: { threadId: params.threadId } })}\n`);
|
|
287
|
+
break;
|
|
288
|
+
case 'thread/goal/updated':
|
|
289
|
+
if (params.goal.status === 'active')
|
|
290
|
+
this.goals.add(params.threadId);
|
|
291
|
+
else
|
|
292
|
+
this.goals.delete(params.threadId);
|
|
293
|
+
break;
|
|
294
|
+
case 'thread/goal/cleared':
|
|
295
|
+
this.goals.delete(params.threadId);
|
|
296
|
+
break;
|
|
297
|
+
case 'item/started':
|
|
298
|
+
this.items.add(`${params.threadId} ${params.item.id}`);
|
|
299
|
+
break;
|
|
300
|
+
case 'item/completed':
|
|
301
|
+
this.items.delete(`${params.threadId} ${params.item.id}`);
|
|
302
|
+
break;
|
|
303
|
+
case 'hook/started':
|
|
304
|
+
this.hooks.add(params.run.id);
|
|
305
|
+
break;
|
|
306
|
+
case 'hook/completed':
|
|
307
|
+
this.hooks.delete(params.run.id);
|
|
308
|
+
break;
|
|
309
|
+
case 'mcpServer/startupStatus/updated':
|
|
310
|
+
if (params.status === 'starting')
|
|
311
|
+
this.starting.add(`${params.threadId} ${params.name}`);
|
|
312
|
+
else
|
|
313
|
+
this.starting.delete(`${params.threadId} ${params.name}`);
|
|
314
|
+
break;
|
|
315
|
+
case undefined:
|
|
316
|
+
this.answered(message);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
answered(message) {
|
|
320
|
+
if (typeof message.id === 'string' && message.id.startsWith(SYNC)) {
|
|
321
|
+
this.proven = Number(message.id.slice(SYNC.length));
|
|
322
|
+
}
|
|
323
|
+
else if (typeof message.id === 'string' && message.id.startsWith(QUEUE_LIST)) {
|
|
324
|
+
const asked = Number(message.id.slice(QUEUE_LIST.length));
|
|
325
|
+
for (const [thread, latest] of this.queues) {
|
|
326
|
+
if (latest === asked && message.result?.data?.length === 0)
|
|
327
|
+
this.queues.delete(thread);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
else if (message.result?.config?.features !== undefined) {
|
|
331
|
+
this.memories = message.result.config.features.memories !== false;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
class GrokWork extends Work {
|
|
336
|
+
busy() {
|
|
337
|
+
return true;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
export function workFor(kind, cwd, changed) {
|
|
341
|
+
if (kind === 'claude-agent-acp')
|
|
342
|
+
return new ClaudeWork(cwd, changed);
|
|
343
|
+
if (kind === 'codex-acp')
|
|
344
|
+
return new CodexWork(changed);
|
|
345
|
+
return new GrokWork(changed);
|
|
346
|
+
}
|