@agentshouse/kit 0.2.1-cd9cbd8b6428c → 0.2.1-cdc3937dea52e
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 +46 -49
- package/dist/acp.js +101 -108
- package/dist/afk.js +17 -0
- package/dist/agents.js +271 -73
- package/dist/api.js +43 -6
- package/dist/app-git.js +48 -0
- package/dist/bridge.js +42 -62
- package/dist/building.js +50 -0
- package/dist/calls.js +88 -0
- package/dist/chunks.js +49 -0
- package/dist/clis.js +179 -95
- package/dist/codex-main.js +58 -0
- package/dist/connect.js +108 -0
- package/dist/connection.js +120 -0
- package/dist/content.js +106 -0
- package/dist/conversations.js +817 -333
- package/dist/copies.js +451 -251
- package/dist/earlier.js +263 -0
- package/dist/end.js +26 -0
- package/dist/events.js +793 -0
- package/dist/files.js +139 -17
- package/dist/git-main.js +24 -29
- package/dist/git.js +57 -21
- package/dist/grok.js +39 -0
- package/dist/home.js +134 -9
- package/dist/house-cli.js +362 -0
- package/dist/house-main.js +7 -159
- package/dist/instructions.js +7 -11
- package/dist/keys.js +238 -0
- package/dist/kit-cli.js +78 -0
- package/dist/kit-main.js +8 -19
- package/dist/login.js +133 -104
- package/dist/logout.js +15 -14
- package/dist/mcp.js +6 -2
- package/dist/owner.js +46 -0
- package/dist/placement.js +25 -0
- package/dist/refusals.js +12 -0
- package/dist/relay.js +33 -2
- package/dist/resident.js +161 -61
- package/dist/scope.js +214 -0
- package/dist/scripts.js +303 -0
- package/dist/secret-input.js +5 -2
- package/dist/settings.js +188 -0
- package/dist/shell.js +3 -0
- package/dist/sign-in.js +85 -32
- package/dist/silence.js +28 -0
- package/dist/skills.js +49 -35
- package/dist/supervisor.js +163 -0
- package/dist/transcription.js +131 -40
- package/dist/translate.js +19 -16
- package/dist/update.js +119 -100
- package/dist/wire.js +32 -0
- package/dist/work.js +353 -0
- package/dist/workspace.js +103 -0
- package/package.json +26 -21
- package/dist/authority-main.js +0 -22
- package/dist/blocks.js +0 -51
- package/dist/bootstrap.json +0 -24
- package/dist/clis-main.js +0 -23
- package/dist/configure-main.js +0 -6
- package/dist/enrol-main.js +0 -4
- package/dist/stream.js +0 -47
- package/how-we-work.md +0 -23
- 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
CHANGED
|
@@ -1,117 +1,136 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
|
-
import {
|
|
3
|
-
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
|
|
4
|
-
import { homedir } from 'node:os';
|
|
2
|
+
import { mkdir } from 'node:fs/promises';
|
|
5
3
|
import { join } from 'node:path';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
import { downloadServed, servedRelease } from './building.js';
|
|
5
|
+
import { KIT_RELEASE } from './clis.js';
|
|
6
|
+
import { restart, updateLine } from './end.js';
|
|
7
|
+
import { kitHome, readEnrolment } from './home.js';
|
|
8
|
+
import { placedPrefix } from './placement.js';
|
|
9
|
+
import { childEnvironment, relayOrigin } from './relay.js';
|
|
10
|
+
const INSTALLER_BYTES = 1_000_000;
|
|
11
|
+
// A release's Node.js, Kit package and adapters are about a hundred megabytes, so an hour carries them at thirty kilobytes a second, and a placement still running then has hung.
|
|
12
|
+
const PLACEMENT_MS = 60 * 60_000;
|
|
13
|
+
// A refused Kit is offline until its placement succeeds, so it tries again within five minutes of its Building or this computer recovering, while a placement that fails only after its hundred-megabyte download runs at most twelve times an hour.
|
|
14
|
+
export const PLACEMENT_RETRY_MS = 5 * 60_000;
|
|
15
|
+
const REFUSED = 'kit_bootstrap_refused: ';
|
|
16
|
+
const OUTPUT_CHARACTERS = 4000;
|
|
17
|
+
function causeOf(error) {
|
|
18
|
+
if (error instanceof TypeError && error.cause instanceof Error)
|
|
19
|
+
return `the Building did not answer: ${error.cause.message}`;
|
|
20
|
+
return error instanceof Error ? error.message : String(error);
|
|
11
21
|
}
|
|
12
|
-
function
|
|
13
|
-
return
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
function placing(installer, house, prefix, signal) {
|
|
23
|
+
return new Promise((resolve) => {
|
|
24
|
+
const relay = relayOrigin();
|
|
25
|
+
const child = spawn('bash', [installer, '--house', house, '--place', prefix], {
|
|
26
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
27
|
+
env: { ...childEnvironment(), ...(relay === undefined ? {} : { HOUSE_KIT_RELAY: relay }) },
|
|
28
|
+
detached: true,
|
|
29
|
+
});
|
|
30
|
+
const stop = () => {
|
|
31
|
+
try {
|
|
32
|
+
process.kill(-child.pid, 'SIGKILL');
|
|
33
|
+
}
|
|
34
|
+
catch { }
|
|
35
|
+
};
|
|
36
|
+
signal.addEventListener('abort', stop, { once: true });
|
|
37
|
+
if (signal.aborted)
|
|
38
|
+
stop();
|
|
39
|
+
let output = '';
|
|
40
|
+
for (const stream of [child.stdout, child.stderr]) {
|
|
41
|
+
stream.setEncoding('utf8').on('data', (chunk) => {
|
|
42
|
+
output = (output + chunk).slice(-OUTPUT_CHARACTERS);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const ended = (failure) => {
|
|
46
|
+
signal.removeEventListener('abort', stop);
|
|
47
|
+
resolve(failure);
|
|
48
|
+
};
|
|
49
|
+
child.on('error', (error) => ended(error.message));
|
|
50
|
+
child.on('close', (status) => {
|
|
51
|
+
if (status === 0)
|
|
52
|
+
return ended(null);
|
|
53
|
+
if (signal.aborted)
|
|
54
|
+
return ended(`the placement did not end within ${PLACEMENT_MS / 1000} seconds`);
|
|
55
|
+
const lines = output.split('\n').filter((line) => line.trim() !== '');
|
|
56
|
+
const refused = lines.findLast((line) => line.startsWith(REFUSED));
|
|
57
|
+
ended(refused?.slice(REFUSED.length) ?? lines.at(-1) ?? `the placement ended with status ${status ?? 'a signal'}`);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
23
60
|
}
|
|
24
|
-
async function
|
|
61
|
+
async function placed(prefix, admit) {
|
|
62
|
+
const signal = AbortSignal.timeout(PLACEMENT_MS);
|
|
63
|
+
let version = '';
|
|
25
64
|
try {
|
|
26
|
-
|
|
65
|
+
const placedIn = prefix();
|
|
66
|
+
const served = await servedRelease(signal);
|
|
67
|
+
admit(served.version);
|
|
68
|
+
version = served.version;
|
|
69
|
+
await mkdir(join(kitHome(), 'update'), { recursive: true, mode: 0o700 });
|
|
70
|
+
const installer = join(kitHome(), 'update', served.installer);
|
|
71
|
+
await downloadServed(served.installer, served.sha256, installer, signal, INSTALLER_BYTES);
|
|
72
|
+
return { version, failure: await placing(installer, (await readEnrolment()).house, placedIn, signal) };
|
|
27
73
|
}
|
|
28
|
-
catch {
|
|
29
|
-
return
|
|
74
|
+
catch (error) {
|
|
75
|
+
return { version, failure: signal.aborted ? `the placement did not end within ${PLACEMENT_MS / 1000} seconds` : causeOf(error) };
|
|
30
76
|
}
|
|
31
77
|
}
|
|
32
|
-
async function
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
return null;
|
|
36
|
-
return status.trim() === '0' ? {} : { refused: `the Kit bootstrap exited with ${status.trim()}` };
|
|
78
|
+
export async function placeServedRelease(prefix) {
|
|
79
|
+
const { version, failure } = await placed(() => prefix, () => undefined);
|
|
80
|
+
return failure === null ? null : `${version === '' ? "The Building's Kit release" : `Kit ${version}`} could not be placed: ${failure}`;
|
|
37
81
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
82
|
+
export class KitUpdate {
|
|
83
|
+
calls;
|
|
84
|
+
queue = Promise.resolve();
|
|
85
|
+
placed = null;
|
|
86
|
+
constructor(calls) {
|
|
87
|
+
this.calls = calls;
|
|
88
|
+
}
|
|
89
|
+
take(call) {
|
|
90
|
+
void this.calls.carry(call.input_id, async () => ({ result: await this.queued(() => this.update(call.release)) }));
|
|
91
|
+
}
|
|
92
|
+
async takeRefused(release) {
|
|
93
|
+
for (;;) {
|
|
94
|
+
const failure = await this.queued(async () => (this.placed === null ? this.place(release, 'served') : null));
|
|
95
|
+
if (failure === null)
|
|
96
|
+
restart(`Kit ${this.placed} is placed; its supervisor starts it`);
|
|
97
|
+
process.stderr.write(`kit: ${updateLine(`Kit ${KIT_RELEASE} stays offline and tries again in ${PLACEMENT_RETRY_MS / 60_000} minutes`)}\n`);
|
|
98
|
+
await new Promise((resolve) => setTimeout(resolve, PLACEMENT_RETRY_MS));
|
|
45
99
|
}
|
|
46
|
-
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
47
100
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return new Promise((resolve) => {
|
|
51
|
-
const child = spawn('systemd-run', [
|
|
52
|
-
'--wait',
|
|
53
|
-
'--collect',
|
|
54
|
-
'--quiet',
|
|
55
|
-
`--unit=${unit(inputId)}`,
|
|
56
|
-
`--setenv=HOME=${homedir()}`,
|
|
57
|
-
'/bin/sh',
|
|
58
|
-
'-c',
|
|
59
|
-
RECORD,
|
|
60
|
-
script,
|
|
61
|
-
outcome(),
|
|
62
|
-
...flags,
|
|
63
|
-
], { stdio: ['ignore', 'ignore', 'inherit'] });
|
|
64
|
-
child.on('error', () => resolve({ refused: 'no service manager runs this Kit' }));
|
|
65
|
-
child.on('close', () => resolve(null));
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
export class Updates {
|
|
69
|
-
house;
|
|
70
|
-
held = new Set();
|
|
71
|
-
queue = Promise.resolve();
|
|
72
|
-
constructor(house) {
|
|
73
|
-
this.house = house;
|
|
101
|
+
placedRelease() {
|
|
102
|
+
return this.placed;
|
|
74
103
|
}
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
await this.acknowledge(inputId, await finished(inputId));
|
|
104
|
+
queued(work) {
|
|
105
|
+
const taken = this.queue.then(work);
|
|
106
|
+
this.queue = taken.catch(() => undefined);
|
|
107
|
+
return taken;
|
|
80
108
|
}
|
|
81
|
-
|
|
82
|
-
if (this.
|
|
83
|
-
return;
|
|
84
|
-
this.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
109
|
+
async update(release) {
|
|
110
|
+
if (release === (this.placed ?? KIT_RELEASE))
|
|
111
|
+
return {};
|
|
112
|
+
if (this.placed !== null)
|
|
113
|
+
return { refused: `Kit ${this.placed} is placed and starts first, once no turn is open` };
|
|
114
|
+
const failure = await this.place(release, 'named');
|
|
115
|
+
if (failure !== null)
|
|
116
|
+
return { refused: failure };
|
|
117
|
+
const starts = process.env.HOUSE_KIT_SANDBOX === '1' ? 'when the Sandbox starts again' : 'once no turn is open';
|
|
118
|
+
process.stderr.write(`kit: Kit ${release} is placed; it starts ${starts}\n`);
|
|
119
|
+
return {};
|
|
90
120
|
}
|
|
91
|
-
async
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (
|
|
99
|
-
|
|
121
|
+
async place(release, which) {
|
|
122
|
+
const { version, failure } = await placed(placedPrefix, (served) => {
|
|
123
|
+
if (which === 'named' && served !== release)
|
|
124
|
+
throw new Error(`the Building serves Kit ${served}, not Kit ${release}`);
|
|
125
|
+
if (served === KIT_RELEASE)
|
|
126
|
+
throw new Error(`the Building serves Kit ${KIT_RELEASE}, which this Kit runs`);
|
|
127
|
+
});
|
|
128
|
+
if (failure !== null) {
|
|
129
|
+
const refused = `Kit ${version || release} could not be placed: ${failure}`;
|
|
130
|
+
process.stderr.write(`kit: ${refused}\n`);
|
|
131
|
+
return refused;
|
|
100
132
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
await writeFile(path, script, { mode: 0o700 });
|
|
104
|
-
await rm(outcome(), { force: true });
|
|
105
|
-
await writeFile(pending(), `${JSON.stringify({ input_id: input.input_id })}\n`, { mode: 0o600 });
|
|
106
|
-
return (await rerun(path, input.input_id, input.update_clis ? ['--update-clis'] : [])) ?? (await recorded()) ?? {
|
|
107
|
-
refused: 'the Kit bootstrap did not finish',
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
async acknowledge(inputId, ack) {
|
|
111
|
-
await this.house.deliver(`/kit/inputs/${inputId}/ack`, ack);
|
|
112
|
-
if ((await pendingInput()) !== inputId)
|
|
113
|
-
return;
|
|
114
|
-
await rm(pending(), { force: true });
|
|
115
|
-
await rm(outcome(), { force: true });
|
|
133
|
+
this.placed = version;
|
|
134
|
+
return null;
|
|
116
135
|
}
|
|
117
136
|
}
|
package/dist/wire.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { updateRequired } from './end.js';
|
|
2
|
+
import { refusalOf } from './refusals.js';
|
|
3
|
+
let take = null;
|
|
4
|
+
let taken = false;
|
|
5
|
+
function unservedBy(text) {
|
|
6
|
+
let body;
|
|
7
|
+
try {
|
|
8
|
+
body = JSON.parse(text);
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
return body?.error?.code === 'kit_update_required' ? body.error : null;
|
|
14
|
+
}
|
|
15
|
+
export function takeUnservedRelease(taking) {
|
|
16
|
+
take = taking;
|
|
17
|
+
}
|
|
18
|
+
export function unserved() {
|
|
19
|
+
return taken;
|
|
20
|
+
}
|
|
21
|
+
export function endUnserved(text) {
|
|
22
|
+
const refused = unservedBy(text);
|
|
23
|
+
if (refused !== null && take !== null) {
|
|
24
|
+
if (!taken) {
|
|
25
|
+
taken = true;
|
|
26
|
+
take(refused.release);
|
|
27
|
+
}
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (refusalOf(text)?.code === 'kit_update_required')
|
|
31
|
+
updateRequired();
|
|
32
|
+
}
|
package/dist/work.js
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
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
|
+
tapped;
|
|
181
|
+
threads = new Map();
|
|
182
|
+
queues = new Map();
|
|
183
|
+
goals = new Set();
|
|
184
|
+
items = new Set();
|
|
185
|
+
hooks = new Set();
|
|
186
|
+
starting = new Set();
|
|
187
|
+
settling = [];
|
|
188
|
+
memories = null;
|
|
189
|
+
lost = false;
|
|
190
|
+
server = null;
|
|
191
|
+
path = null;
|
|
192
|
+
socket = null;
|
|
193
|
+
asked = 0;
|
|
194
|
+
heard = 0;
|
|
195
|
+
proving = -1;
|
|
196
|
+
proven = -1;
|
|
197
|
+
constructor(changed, tapped) {
|
|
198
|
+
super(changed);
|
|
199
|
+
this.tapped = tapped;
|
|
200
|
+
}
|
|
201
|
+
async open(cli) {
|
|
202
|
+
this.path = join(socketDirectory(), `${randomUUID()}.sock`);
|
|
203
|
+
this.server = createServer((socket) => this.connected(socket));
|
|
204
|
+
this.server.listen(this.path);
|
|
205
|
+
await once(this.server, 'listening');
|
|
206
|
+
return { CODEX_PATH: codexLauncher(), HOUSE_KIT_CODEX: cli, HOUSE_KIT_CODEX_STREAM: this.path };
|
|
207
|
+
}
|
|
208
|
+
settled() {
|
|
209
|
+
return new Promise((resolve) => {
|
|
210
|
+
this.settling.push(resolve);
|
|
211
|
+
this.settle();
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
take(message) {
|
|
215
|
+
super.take(message);
|
|
216
|
+
this.heard++;
|
|
217
|
+
}
|
|
218
|
+
busy() {
|
|
219
|
+
if (super.busy() ||
|
|
220
|
+
this.lost ||
|
|
221
|
+
this.memories !== false ||
|
|
222
|
+
[...this.threads.values()].some(Boolean) ||
|
|
223
|
+
this.queues.size > 0 ||
|
|
224
|
+
this.goals.size > 0 ||
|
|
225
|
+
this.items.size > 0 ||
|
|
226
|
+
this.hooks.size > 0) {
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
if (this.proven === this.heard)
|
|
230
|
+
return false;
|
|
231
|
+
this.prove();
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
close() {
|
|
235
|
+
this.server?.close();
|
|
236
|
+
if (this.path !== null)
|
|
237
|
+
void rm(this.path, { force: true });
|
|
238
|
+
}
|
|
239
|
+
connected(socket) {
|
|
240
|
+
this.socket = socket;
|
|
241
|
+
let buffer = '';
|
|
242
|
+
socket.on('error', () => this.lose());
|
|
243
|
+
socket.on('close', () => this.lose());
|
|
244
|
+
socket.setEncoding('utf8').on('data', (chunk) => {
|
|
245
|
+
buffer += chunk;
|
|
246
|
+
for (let index = buffer.indexOf('\n'); index >= 0; index = buffer.indexOf('\n')) {
|
|
247
|
+
const message = this.parsed(buffer.slice(0, index));
|
|
248
|
+
buffer = buffer.slice(index + 1);
|
|
249
|
+
if (message !== null)
|
|
250
|
+
this.stream(message, socket);
|
|
251
|
+
}
|
|
252
|
+
this.settle();
|
|
253
|
+
this.changed();
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
parsed(line) {
|
|
257
|
+
try {
|
|
258
|
+
return JSON.parse(line);
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
this.lost = true;
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
lose() {
|
|
266
|
+
this.lost = true;
|
|
267
|
+
this.changed();
|
|
268
|
+
}
|
|
269
|
+
prove() {
|
|
270
|
+
if (this.proving === this.heard)
|
|
271
|
+
return;
|
|
272
|
+
this.proving = this.heard;
|
|
273
|
+
this.socket.write(`${JSON.stringify({ jsonrpc: '2.0', id: `${SYNC}${this.heard}`, method: 'house-kit/sync' })}\n`);
|
|
274
|
+
}
|
|
275
|
+
settle() {
|
|
276
|
+
if (this.starting.size === 0)
|
|
277
|
+
for (const resolve of this.settling.splice(0))
|
|
278
|
+
resolve();
|
|
279
|
+
}
|
|
280
|
+
stream(message, socket) {
|
|
281
|
+
const params = message.params;
|
|
282
|
+
if (message.method !== undefined)
|
|
283
|
+
this.tapped(message);
|
|
284
|
+
switch (message.method) {
|
|
285
|
+
case 'thread/status/changed':
|
|
286
|
+
this.threads.set(params.threadId, params.status.type === 'active' && params.status.activeFlags.length === 0);
|
|
287
|
+
break;
|
|
288
|
+
case 'thread/closed':
|
|
289
|
+
this.threads.delete(params.threadId);
|
|
290
|
+
break;
|
|
291
|
+
case 'thread/queue/changed':
|
|
292
|
+
this.queues.set(params.threadId, ++this.asked);
|
|
293
|
+
socket.write(`${JSON.stringify({ jsonrpc: '2.0', id: `${QUEUE_LIST}${this.asked}`, method: 'thread/queue/list', params: { threadId: params.threadId } })}\n`);
|
|
294
|
+
break;
|
|
295
|
+
case 'thread/goal/updated':
|
|
296
|
+
if (params.goal.status === 'active')
|
|
297
|
+
this.goals.add(params.threadId);
|
|
298
|
+
else
|
|
299
|
+
this.goals.delete(params.threadId);
|
|
300
|
+
break;
|
|
301
|
+
case 'thread/goal/cleared':
|
|
302
|
+
this.goals.delete(params.threadId);
|
|
303
|
+
break;
|
|
304
|
+
case 'item/started':
|
|
305
|
+
this.items.add(`${params.threadId} ${params.item.id}`);
|
|
306
|
+
break;
|
|
307
|
+
case 'item/completed':
|
|
308
|
+
this.items.delete(`${params.threadId} ${params.item.id}`);
|
|
309
|
+
break;
|
|
310
|
+
case 'hook/started':
|
|
311
|
+
this.hooks.add(params.run.id);
|
|
312
|
+
break;
|
|
313
|
+
case 'hook/completed':
|
|
314
|
+
this.hooks.delete(params.run.id);
|
|
315
|
+
break;
|
|
316
|
+
case 'mcpServer/startupStatus/updated':
|
|
317
|
+
if (params.status === 'starting')
|
|
318
|
+
this.starting.add(`${params.threadId} ${params.name}`);
|
|
319
|
+
else
|
|
320
|
+
this.starting.delete(`${params.threadId} ${params.name}`);
|
|
321
|
+
break;
|
|
322
|
+
case undefined:
|
|
323
|
+
this.answered(message);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
answered(message) {
|
|
327
|
+
if (typeof message.id === 'string' && message.id.startsWith(SYNC)) {
|
|
328
|
+
this.proven = Number(message.id.slice(SYNC.length));
|
|
329
|
+
}
|
|
330
|
+
else if (typeof message.id === 'string' && message.id.startsWith(QUEUE_LIST)) {
|
|
331
|
+
const asked = Number(message.id.slice(QUEUE_LIST.length));
|
|
332
|
+
for (const [thread, latest] of this.queues) {
|
|
333
|
+
if (latest === asked && message.result?.data?.length === 0)
|
|
334
|
+
this.queues.delete(thread);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
else if (message.result?.config?.features !== undefined) {
|
|
338
|
+
this.memories = message.result.config.features.memories !== false;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
class GrokWork extends Work {
|
|
343
|
+
busy() {
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
export function workFor(kind, cwd, changed, tapped) {
|
|
348
|
+
if (kind === 'claude-agent-acp')
|
|
349
|
+
return new ClaudeWork(cwd, changed);
|
|
350
|
+
if (kind === 'codex-acp')
|
|
351
|
+
return new CodexWork(changed, tapped);
|
|
352
|
+
return new GrokWork(changed);
|
|
353
|
+
}
|