@agentshouse/kit 0.2.1-c30728be20485 → 0.2.1-c35516351ef49
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 +27 -26
- package/dist/acp.js +16 -93
- package/dist/agents.js +119 -34
- package/dist/api.js +9 -5
- package/dist/app-git.js +64 -0
- package/dist/bootstrap.json +4 -4
- package/dist/bridge.js +11 -37
- package/dist/chatgpt.js +32 -0
- package/dist/clis.js +116 -70
- package/dist/codex-main.js +58 -0
- package/dist/conversations.js +301 -237
- package/dist/copies.js +145 -110
- package/dist/end.js +18 -4
- package/dist/enrol-main.js +7 -1
- package/dist/events.js +524 -0
- package/dist/files.js +21 -1
- package/dist/git.js +11 -8
- package/dist/home.js +81 -6
- package/dist/house-cli.js +291 -0
- package/dist/house-main.js +7 -159
- package/dist/instructions.js +9 -32
- package/dist/kit-cli.js +68 -0
- package/dist/kit-main.js +8 -19
- package/dist/login.js +118 -103
- package/dist/logout.js +16 -13
- package/dist/parts.js +82 -0
- package/dist/placement.js +48 -0
- package/dist/relay.js +12 -2
- package/dist/resident.js +38 -27
- package/dist/scope.js +203 -0
- package/dist/service-main.js +9 -0
- package/dist/service.js +33 -0
- package/dist/shell.js +3 -0
- package/dist/sign-in.js +34 -10
- package/dist/skills.js +81 -12
- package/dist/stream.js +66 -15
- package/dist/transcription.js +57 -11
- package/dist/update.js +28 -6
- package/dist/wire.js +6 -0
- package/dist/work.js +348 -0
- package/how-we-work.md +13 -3
- package/package.json +3 -2
- package/skills/ask-matt/SKILL.md +17 -11
- package/skills/ask-matt/agents/openai.yaml +1 -0
- package/skills/code-review/SKILL.md +5 -5
- package/skills/codebase-design/DESIGN-IT-TWICE.md +1 -1
- package/skills/codebase-design/SKILL.md +0 -2
- package/skills/diagnosing-bugs/SKILL.md +2 -4
- package/skills/domain-modeling/{CONTEXT-FORMAT.md → GLOSSARY-FORMAT.md} +10 -10
- package/skills/domain-modeling/SKILL.md +12 -14
- package/skills/grill-me/SKILL.md +1 -2
- package/skills/grill-me/agents/openai.yaml +1 -0
- package/skills/grill-with-docs/SKILL.md +1 -2
- package/skills/grill-with-docs/agents/openai.yaml +1 -0
- package/skills/grilling/SKILL.md +2 -2
- package/skills/handoff/SKILL.md +2 -3
- package/skills/handoff/agents/openai.yaml +1 -0
- package/skills/implement/SKILL.md +5 -4
- package/skills/implement/agents/openai.yaml +1 -0
- package/skills/implement-spec/SKILL.md +40 -0
- package/skills/implement-spec/agents/openai.yaml +5 -0
- package/skills/improve-codebase-architecture/SKILL.md +6 -7
- package/skills/improve-codebase-architecture/agents/openai.yaml +1 -0
- package/skills/pr/CREDITS.md +3 -0
- package/skills/pr/LICENSE +21 -0
- package/skills/pr/SKILL.md +170 -0
- package/skills/pr/agents/openai.yaml +3 -0
- package/skills/prototype/SKILL.md +0 -2
- package/skills/research/SKILL.md +0 -2
- package/skills/retro/LICENSE +21 -0
- package/skills/retro/SKILL.md +44 -0
- package/skills/retro/agents/openai.yaml +5 -0
- package/skills/tdd/SKILL.md +2 -4
- package/skills/teach/SKILL.md +3 -4
- package/skills/teach/agents/openai.yaml +1 -0
- package/skills/to-questionnaire/SKILL.md +1 -2
- package/skills/to-questionnaire/agents/openai.yaml +1 -0
- package/skills/to-spec/SKILL.md +1 -2
- package/skills/to-spec/agents/openai.yaml +1 -0
- package/skills/to-tickets/SKILL.md +3 -4
- package/skills/to-tickets/agents/openai.yaml +1 -0
- package/skills/triage/AGENT-BRIEF.md +207 -0
- package/skills/triage/LICENSE +21 -0
- package/skills/triage/OUT-OF-SCOPE.md +105 -0
- package/skills/triage/SKILL.md +112 -0
- package/skills/triage/agents/openai.yaml +5 -0
- package/skills/wait-what/SKILL.md +2 -3
- package/skills/wait-what/agents/openai.yaml +1 -0
- package/skills/wayfinder/SKILL.md +5 -6
- package/skills/wayfinder/agents/openai.yaml +1 -0
- package/skills/wizard/LICENSE +21 -0
- package/skills/wizard/SKILL.md +44 -0
- package/skills/wizard/agents/openai.yaml +3 -0
- package/skills/wizard/template.sh +223 -0
- package/skills/writing-for-agents/SKILL.md +0 -2
- package/dist/blocks.js +0 -51
- package/skills/resolving-merge-conflicts/SKILL.md +0 -16
- package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
- /package/skills/{resolving-merge-conflicts → implement-spec}/LICENSE +0 -0
package/dist/stream.js
CHANGED
|
@@ -1,11 +1,42 @@
|
|
|
1
1
|
import { subscribe } from 'node:diagnostics_channel';
|
|
2
2
|
import { finalRefusal, HouseRefusal, retryDelay } from './api.js';
|
|
3
|
-
import { end, REPLACED,
|
|
3
|
+
import { end, REPLACED, signInAgain, updateRequired } from './end.js';
|
|
4
4
|
import { readEnrolment } from './home.js';
|
|
5
5
|
import { relayed } from './relay.js';
|
|
6
|
+
import { KIT_WIRE, tooOld } from './wire.js';
|
|
6
7
|
const PATH = '/kit/stream';
|
|
7
|
-
const SUBPROTOCOL =
|
|
8
|
+
const SUBPROTOCOL = `house.kit.stream.${KIT_WIRE}`;
|
|
8
9
|
const REFUSAL_HEADER = 'x-house-refusal';
|
|
10
|
+
// House beats each stream every thirty seconds, so two silent beats mean this Kit cannot hear House.
|
|
11
|
+
export const STREAM_SILENCE_MS = 60_000;
|
|
12
|
+
const SYSTEM_STREAM_CLOCK = {
|
|
13
|
+
later: (delay, callback) => setTimeout(callback, delay),
|
|
14
|
+
clear: (timer) => clearTimeout(timer),
|
|
15
|
+
};
|
|
16
|
+
export class StreamSilence {
|
|
17
|
+
timer = null;
|
|
18
|
+
silent;
|
|
19
|
+
clock;
|
|
20
|
+
interval;
|
|
21
|
+
constructor(silent, clock = SYSTEM_STREAM_CLOCK, interval = STREAM_SILENCE_MS) {
|
|
22
|
+
this.silent = silent;
|
|
23
|
+
this.clock = clock;
|
|
24
|
+
this.interval = interval;
|
|
25
|
+
}
|
|
26
|
+
heard() {
|
|
27
|
+
this.stop();
|
|
28
|
+
this.timer = this.clock.later(this.interval, () => {
|
|
29
|
+
this.timer = null;
|
|
30
|
+
this.silent();
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
stop() {
|
|
34
|
+
if (this.timer === null)
|
|
35
|
+
return;
|
|
36
|
+
this.clock.clear(this.timer);
|
|
37
|
+
this.timer = null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
9
40
|
function streamUrl(house) {
|
|
10
41
|
const url = new URL(PATH, house);
|
|
11
42
|
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
@@ -36,29 +67,49 @@ export function holdStream(handlers) {
|
|
|
36
67
|
headers: { authorization: `Bearer ${credential}` },
|
|
37
68
|
});
|
|
38
69
|
let opened = false;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
handlers.opened();
|
|
44
|
-
};
|
|
45
|
-
opening.onmessage = (event) => {
|
|
46
|
-
handlers.frame(JSON.parse(String(event.data)));
|
|
47
|
-
};
|
|
48
|
-
opening.onerror = () => undefined;
|
|
49
|
-
opening.onclose = (event) => {
|
|
70
|
+
let silence = null;
|
|
71
|
+
const closed = (event) => {
|
|
72
|
+
silence?.stop();
|
|
73
|
+
silence = null;
|
|
50
74
|
if (socket === opening)
|
|
51
75
|
socket = null;
|
|
52
76
|
process.stderr.write(`kit: control stream closed ${event.code} ${event.reason}\n`);
|
|
53
77
|
if (event.reason === 'replaced')
|
|
54
78
|
end(REPLACED);
|
|
55
|
-
if (event.reason === 'credential_rejected' || refused?.status === 401)
|
|
56
|
-
|
|
79
|
+
if (event.reason === 'credential_rejected' || refused?.status === 401) {
|
|
80
|
+
void signInAgain(credential);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (refused !== null && tooOld(refused.text))
|
|
84
|
+
updateRequired(house);
|
|
57
85
|
if (finalRefusal(refused))
|
|
58
86
|
end(refused.message);
|
|
59
87
|
// A closed socket reopens in one to three seconds, spread so Kits one restart closed return apart; an opening House refused as retryable or left unanswered backs off on the jittered curve.
|
|
60
88
|
setTimeout(() => void connect(), opened ? 1000 + Math.random() * 2000 : retryDelay(failures++));
|
|
61
89
|
};
|
|
90
|
+
opening.onopen = () => {
|
|
91
|
+
opened = true;
|
|
92
|
+
failures = 0;
|
|
93
|
+
socket = opening;
|
|
94
|
+
silence = new StreamSilence(() => {
|
|
95
|
+
if (socket !== opening)
|
|
96
|
+
return;
|
|
97
|
+
socket = null;
|
|
98
|
+
opening.onclose = null;
|
|
99
|
+
opening.close();
|
|
100
|
+
closed(new CloseEvent('close', { code: 1006, reason: 'heartbeat_missed' }));
|
|
101
|
+
});
|
|
102
|
+
silence.heard();
|
|
103
|
+
handlers.opened();
|
|
104
|
+
};
|
|
105
|
+
opening.onmessage = (event) => {
|
|
106
|
+
silence?.heard();
|
|
107
|
+
const frame = JSON.parse(String(event.data));
|
|
108
|
+
if (frame.type !== 'beat')
|
|
109
|
+
handlers.frame(frame);
|
|
110
|
+
};
|
|
111
|
+
opening.onerror = () => undefined;
|
|
112
|
+
opening.onclose = closed;
|
|
62
113
|
};
|
|
63
114
|
void connect();
|
|
64
115
|
return {
|
package/dist/transcription.js
CHANGED
|
@@ -29,6 +29,10 @@ export const ENGINES = {
|
|
|
29
29
|
};
|
|
30
30
|
const CHUNK_SECONDS = 20;
|
|
31
31
|
let held = Promise.resolve();
|
|
32
|
+
let holding = Promise.resolve();
|
|
33
|
+
let setting = null;
|
|
34
|
+
let downloading = null;
|
|
35
|
+
let readiness = { installed: false, failure: null };
|
|
32
36
|
function directory() {
|
|
33
37
|
return join(kitHome(), 'transcription');
|
|
34
38
|
}
|
|
@@ -36,7 +40,7 @@ function installed(artifact) {
|
|
|
36
40
|
return join(directory(), artifact.sha256);
|
|
37
41
|
}
|
|
38
42
|
async function executed(command, args, signal) {
|
|
39
|
-
// A download or a transcription runs as long as its size needs; the
|
|
43
|
+
// A download or a transcription runs as long as its size needs; the setting turning off or the turn's interrupt or end aborts it instead.
|
|
40
44
|
const ran = await run(command, args, 0, false, signal);
|
|
41
45
|
if (ran.status !== 0)
|
|
42
46
|
throw new Error(failureOf(ran, basename(command)));
|
|
@@ -45,16 +49,22 @@ async function executed(command, args, signal) {
|
|
|
45
49
|
function present(path) {
|
|
46
50
|
return stat(path).then(() => true, () => false);
|
|
47
51
|
}
|
|
48
|
-
async function
|
|
52
|
+
async function threads() {
|
|
49
53
|
const [quota, period] = (await readFile('/sys/fs/cgroup/cpu.max', 'utf8').catch(() => 'max')).split(' ');
|
|
50
54
|
const allowed = quota === 'max' ? Infinity : Math.ceil(Number(quota) / Number(period));
|
|
51
|
-
return Math.min(availableParallelism(), allowed);
|
|
55
|
+
return Math.max(1, Math.floor(Math.min(availableParallelism(), allowed) / 2));
|
|
52
56
|
}
|
|
53
57
|
async function sha256(path) {
|
|
54
58
|
const hash = createHash('sha256');
|
|
55
59
|
await pipeline(createReadStream(path), hash);
|
|
56
60
|
return hash.digest('hex');
|
|
57
61
|
}
|
|
62
|
+
function causeOf(error) {
|
|
63
|
+
return error instanceof Error ? error.message : String(error);
|
|
64
|
+
}
|
|
65
|
+
function pinnedHere() {
|
|
66
|
+
return ENGINES[`${process.platform}-${process.arch}`];
|
|
67
|
+
}
|
|
58
68
|
async function install(artifacts, signal) {
|
|
59
69
|
await mkdir(directory(), { recursive: true });
|
|
60
70
|
const pinned = new Set(artifacts.map((artifact) => artifact.sha256));
|
|
@@ -69,7 +79,7 @@ async function install(artifacts, signal) {
|
|
|
69
79
|
try {
|
|
70
80
|
for (const artifact of missing) {
|
|
71
81
|
const part = `${installed(artifact)}.part`;
|
|
72
|
-
// Three retries carry a large download past a dropped connection before the
|
|
82
|
+
// Three retries carry a large download past a dropped connection before the download fails.
|
|
73
83
|
await executed('curl', ['-fsSL', '--retry', '3', '-o', part, artifact.url], signal);
|
|
74
84
|
if ((await sha256(part)) !== artifact.sha256)
|
|
75
85
|
throw new Error(`${artifact.url} does not match its sha256`);
|
|
@@ -85,18 +95,52 @@ async function install(artifacts, signal) {
|
|
|
85
95
|
await rm(`${installed(artifact)}.part`, { force: true });
|
|
86
96
|
}
|
|
87
97
|
}
|
|
98
|
+
function reported(next) {
|
|
99
|
+
const changed = next.installed !== readiness.installed || next.failure !== readiness.failure;
|
|
100
|
+
readiness = next;
|
|
101
|
+
return changed;
|
|
102
|
+
}
|
|
103
|
+
async function settle(transcribe) {
|
|
104
|
+
if (transcribe === setting)
|
|
105
|
+
return false;
|
|
106
|
+
setting = transcribe;
|
|
107
|
+
if (!transcribe) {
|
|
108
|
+
await rm(directory(), { recursive: true, force: true });
|
|
109
|
+
return reported({ installed: false, failure: null });
|
|
110
|
+
}
|
|
111
|
+
const download = new AbortController();
|
|
112
|
+
downloading = download;
|
|
113
|
+
try {
|
|
114
|
+
const pinned = pinnedHere();
|
|
115
|
+
await install([pinned.decoder, pinned.engine, MODEL], download.signal);
|
|
116
|
+
return reported({ installed: true, failure: null });
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
await rm(directory(), { recursive: true, force: true });
|
|
120
|
+
return reported({ installed: false, failure: download.signal.aborted ? null : causeOf(error) });
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
downloading = null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
export function holdTranscription(transcribe) {
|
|
127
|
+
if (!transcribe)
|
|
128
|
+
downloading?.abort();
|
|
129
|
+
const settled = holding.then(() => settle(transcribe));
|
|
130
|
+
holding = settled.catch(() => undefined);
|
|
131
|
+
return settled;
|
|
132
|
+
}
|
|
133
|
+
export function transcriptionReport() {
|
|
134
|
+
return readiness;
|
|
135
|
+
}
|
|
88
136
|
async function transcribed(path, signal) {
|
|
89
|
-
const
|
|
90
|
-
const pinned = ENGINES[platform];
|
|
91
|
-
if (pinned === undefined)
|
|
92
|
-
throw new Error(`no transcription engine is pinned for ${platform}`);
|
|
93
|
-
await install([pinned.decoder, pinned.engine, MODEL], signal);
|
|
137
|
+
const pinned = pinnedHere();
|
|
94
138
|
const work = await mkdtemp(join(tmpdir(), 'kit-transcription-'));
|
|
95
139
|
try {
|
|
96
140
|
await executed(installed(pinned.decoder), ['-nostdin', '-hide_banner', '-loglevel', 'error', '-i', path, '-ar', '16000', '-ac', '1', '-c:a', 'pcm_s16le',
|
|
97
141
|
'-f', 'segment', '-segment_time', String(CHUNK_SECONDS), join(work, '%05d.wav')], signal);
|
|
98
142
|
const parts = (await readdir(work)).sort().flatMap((part) => ['-f', join(work, part)]);
|
|
99
|
-
const text = await executed(installed(pinned.engine), ['-t', String(await
|
|
143
|
+
const text = await executed(installed(pinned.engine), ['-t', String(await threads()), '-np', '-m', installed(MODEL), ...parts], signal);
|
|
100
144
|
return text
|
|
101
145
|
.split('\n')
|
|
102
146
|
.map((line) => line.trim())
|
|
@@ -108,6 +152,8 @@ async function transcribed(path, signal) {
|
|
|
108
152
|
}
|
|
109
153
|
}
|
|
110
154
|
export async function transcribe(path, signal) {
|
|
155
|
+
if (!readiness.installed)
|
|
156
|
+
return null;
|
|
111
157
|
const before = held;
|
|
112
158
|
let release;
|
|
113
159
|
const mine = new Promise((resolve) => {
|
|
@@ -116,7 +162,7 @@ export async function transcribe(path, signal) {
|
|
|
116
162
|
held = Promise.all([before, mine]);
|
|
117
163
|
try {
|
|
118
164
|
await unlessAborted(before, signal);
|
|
119
|
-
return await transcribed(path, signal);
|
|
165
|
+
return readiness.installed ? await transcribed(path, signal) : null;
|
|
120
166
|
}
|
|
121
167
|
finally {
|
|
122
168
|
release();
|
package/dist/update.js
CHANGED
|
@@ -4,8 +4,8 @@ import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
|
|
|
4
4
|
import { homedir } from 'node:os';
|
|
5
5
|
import { join } from 'node:path';
|
|
6
6
|
import { run } from './clis.js';
|
|
7
|
-
import { kitHome } from './home.js';
|
|
8
|
-
|
|
7
|
+
import { kitHome, workspaceRoot } from './home.js';
|
|
8
|
+
import { service, throughSudo } from './placement.js';
|
|
9
9
|
function logged(error) {
|
|
10
10
|
process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
11
11
|
}
|
|
@@ -21,6 +21,21 @@ function outcome() {
|
|
|
21
21
|
function unit(inputId) {
|
|
22
22
|
return `house-kit-update-${inputId}`;
|
|
23
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
|
+
}
|
|
24
39
|
async function pendingInput() {
|
|
25
40
|
try {
|
|
26
41
|
return JSON.parse(await readFile(pending(), 'utf8')).input_id;
|
|
@@ -41,7 +56,7 @@ async function finished(inputId) {
|
|
|
41
56
|
if (ack !== null)
|
|
42
57
|
return ack;
|
|
43
58
|
// systemctl answers at once, so ten seconds only bounds a stuck service manager.
|
|
44
|
-
if ((await run('systemctl', ['is-active', '--quiet', unit(inputId)], 10_000)).status !== 0) {
|
|
59
|
+
if ((await run('systemctl', [...manager(), 'is-active', '--quiet', unit(inputId)], 10_000)).status !== 0) {
|
|
45
60
|
return (await recorded()) ?? { refused: 'the Kit bootstrap did not finish' };
|
|
46
61
|
}
|
|
47
62
|
// The bootstrap runs for minutes, so checking each second sees its outcome within a second at little cost.
|
|
@@ -50,19 +65,26 @@ async function finished(inputId) {
|
|
|
50
65
|
}
|
|
51
66
|
function rerun(script, inputId, flags) {
|
|
52
67
|
return new Promise((resolve) => {
|
|
53
|
-
const
|
|
68
|
+
const [command, ...args] = [
|
|
69
|
+
...elevated(),
|
|
70
|
+
'systemd-run',
|
|
71
|
+
...manager(),
|
|
72
|
+
...account(),
|
|
54
73
|
'--wait',
|
|
55
74
|
'--collect',
|
|
56
75
|
'--quiet',
|
|
57
76
|
`--unit=${unit(inputId)}`,
|
|
58
77
|
`--setenv=HOME=${homedir()}`,
|
|
78
|
+
`--setenv=HOUSE_KIT_HOME=${kitHome()}`,
|
|
59
79
|
'/bin/sh',
|
|
60
80
|
'-c',
|
|
61
|
-
|
|
81
|
+
record(),
|
|
62
82
|
script,
|
|
63
83
|
outcome(),
|
|
84
|
+
...placement(),
|
|
64
85
|
...flags,
|
|
65
|
-
]
|
|
86
|
+
];
|
|
87
|
+
const child = spawn(command, args, { stdio: ['ignore', 'ignore', 'inherit'] });
|
|
66
88
|
child.on('error', () => resolve({ refused: 'no service manager runs this Kit' }));
|
|
67
89
|
child.on('close', () => resolve(null));
|
|
68
90
|
});
|
package/dist/wire.js
ADDED
package/dist/work.js
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { once } from 'node:events';
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
import { mkdir, 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 } 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
|
+
const sockets = join(kitHome(), 'bridges');
|
|
198
|
+
await mkdir(sockets, { recursive: true, mode: 0o700 });
|
|
199
|
+
this.path = join(sockets, `${randomUUID()}.sock`);
|
|
200
|
+
this.server = createServer((socket) => this.connected(socket));
|
|
201
|
+
this.server.listen(this.path);
|
|
202
|
+
await once(this.server, 'listening');
|
|
203
|
+
return { CODEX_PATH: codexLauncher(), HOUSE_KIT_CODEX: cli, HOUSE_KIT_CODEX_STREAM: this.path };
|
|
204
|
+
}
|
|
205
|
+
settled() {
|
|
206
|
+
return new Promise((resolve) => {
|
|
207
|
+
this.settling.push(resolve);
|
|
208
|
+
this.settle();
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
take(message) {
|
|
212
|
+
super.take(message);
|
|
213
|
+
this.heard++;
|
|
214
|
+
}
|
|
215
|
+
busy() {
|
|
216
|
+
if (super.busy() ||
|
|
217
|
+
this.lost ||
|
|
218
|
+
this.memories !== false ||
|
|
219
|
+
[...this.threads.values()].some(Boolean) ||
|
|
220
|
+
this.queues.size > 0 ||
|
|
221
|
+
this.goals.size > 0 ||
|
|
222
|
+
this.items.size > 0 ||
|
|
223
|
+
this.hooks.size > 0) {
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
if (this.proven === this.heard)
|
|
227
|
+
return false;
|
|
228
|
+
this.prove();
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
close() {
|
|
232
|
+
this.server?.close();
|
|
233
|
+
if (this.path !== null)
|
|
234
|
+
void rm(this.path, { force: true });
|
|
235
|
+
}
|
|
236
|
+
connected(socket) {
|
|
237
|
+
this.socket = socket;
|
|
238
|
+
let buffer = '';
|
|
239
|
+
socket.on('error', () => this.lose());
|
|
240
|
+
socket.on('close', () => this.lose());
|
|
241
|
+
socket.setEncoding('utf8').on('data', (chunk) => {
|
|
242
|
+
buffer += chunk;
|
|
243
|
+
for (let index = buffer.indexOf('\n'); index >= 0; index = buffer.indexOf('\n')) {
|
|
244
|
+
const message = this.parsed(buffer.slice(0, index));
|
|
245
|
+
buffer = buffer.slice(index + 1);
|
|
246
|
+
if (message !== null)
|
|
247
|
+
this.stream(message, socket);
|
|
248
|
+
}
|
|
249
|
+
this.settle();
|
|
250
|
+
this.changed();
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
parsed(line) {
|
|
254
|
+
try {
|
|
255
|
+
return JSON.parse(line);
|
|
256
|
+
}
|
|
257
|
+
catch {
|
|
258
|
+
this.lost = true;
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
lose() {
|
|
263
|
+
this.lost = true;
|
|
264
|
+
this.changed();
|
|
265
|
+
}
|
|
266
|
+
prove() {
|
|
267
|
+
if (this.proving === this.heard)
|
|
268
|
+
return;
|
|
269
|
+
this.proving = this.heard;
|
|
270
|
+
this.socket.write(`${JSON.stringify({ jsonrpc: '2.0', id: `${SYNC}${this.heard}`, method: 'house-kit/sync' })}\n`);
|
|
271
|
+
}
|
|
272
|
+
settle() {
|
|
273
|
+
if (this.starting.size === 0)
|
|
274
|
+
for (const resolve of this.settling.splice(0))
|
|
275
|
+
resolve();
|
|
276
|
+
}
|
|
277
|
+
stream(message, socket) {
|
|
278
|
+
const params = message.params;
|
|
279
|
+
switch (message.method) {
|
|
280
|
+
case 'thread/status/changed':
|
|
281
|
+
this.threads.set(params.threadId, params.status.type === 'active' && params.status.activeFlags.length === 0);
|
|
282
|
+
break;
|
|
283
|
+
case 'thread/closed':
|
|
284
|
+
this.threads.delete(params.threadId);
|
|
285
|
+
break;
|
|
286
|
+
case 'thread/queue/changed':
|
|
287
|
+
this.queues.set(params.threadId, ++this.asked);
|
|
288
|
+
socket.write(`${JSON.stringify({ jsonrpc: '2.0', id: `${QUEUE_LIST}${this.asked}`, method: 'thread/queue/list', params: { threadId: params.threadId } })}\n`);
|
|
289
|
+
break;
|
|
290
|
+
case 'thread/goal/updated':
|
|
291
|
+
if (params.goal.status === 'active')
|
|
292
|
+
this.goals.add(params.threadId);
|
|
293
|
+
else
|
|
294
|
+
this.goals.delete(params.threadId);
|
|
295
|
+
break;
|
|
296
|
+
case 'thread/goal/cleared':
|
|
297
|
+
this.goals.delete(params.threadId);
|
|
298
|
+
break;
|
|
299
|
+
case 'item/started':
|
|
300
|
+
this.items.add(`${params.threadId} ${params.item.id}`);
|
|
301
|
+
break;
|
|
302
|
+
case 'item/completed':
|
|
303
|
+
this.items.delete(`${params.threadId} ${params.item.id}`);
|
|
304
|
+
break;
|
|
305
|
+
case 'hook/started':
|
|
306
|
+
this.hooks.add(params.run.id);
|
|
307
|
+
break;
|
|
308
|
+
case 'hook/completed':
|
|
309
|
+
this.hooks.delete(params.run.id);
|
|
310
|
+
break;
|
|
311
|
+
case 'mcpServer/startupStatus/updated':
|
|
312
|
+
if (params.status === 'starting')
|
|
313
|
+
this.starting.add(`${params.threadId} ${params.name}`);
|
|
314
|
+
else
|
|
315
|
+
this.starting.delete(`${params.threadId} ${params.name}`);
|
|
316
|
+
break;
|
|
317
|
+
case undefined:
|
|
318
|
+
this.answered(message);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
answered(message) {
|
|
322
|
+
if (typeof message.id === 'string' && message.id.startsWith(SYNC)) {
|
|
323
|
+
this.proven = Number(message.id.slice(SYNC.length));
|
|
324
|
+
}
|
|
325
|
+
else if (typeof message.id === 'string' && message.id.startsWith(QUEUE_LIST)) {
|
|
326
|
+
const asked = Number(message.id.slice(QUEUE_LIST.length));
|
|
327
|
+
for (const [thread, latest] of this.queues) {
|
|
328
|
+
if (latest === asked && message.result?.data?.length === 0)
|
|
329
|
+
this.queues.delete(thread);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
else if (message.result?.config?.features !== undefined) {
|
|
333
|
+
this.memories = message.result.config.features.memories !== false;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
class GrokWork extends Work {
|
|
338
|
+
busy() {
|
|
339
|
+
return true;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
export function workFor(kind, cwd, changed) {
|
|
343
|
+
if (kind === 'claude-agent-acp')
|
|
344
|
+
return new ClaudeWork(cwd, changed);
|
|
345
|
+
if (kind === 'codex-acp')
|
|
346
|
+
return new CodexWork(changed);
|
|
347
|
+
return new GrokWork(changed);
|
|
348
|
+
}
|