@agentshouse/kit 0.2.1-ce145e9aadbf6 → 0.2.1-cf6326dfa5c3e
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 +32 -31
- package/dist/acp.js +46 -103
- package/dist/agents.js +151 -41
- package/dist/api.js +34 -2
- package/dist/app-git.js +48 -0
- package/dist/bootstrap.json +4 -4
- package/dist/bridge.js +36 -57
- package/dist/chatgpt.js +32 -0
- package/dist/clis.js +126 -69
- package/dist/codex-main.js +58 -0
- package/dist/conversations.js +457 -277
- package/dist/copies.js +198 -124
- package/dist/end.js +23 -0
- package/dist/enrol-main.js +7 -1
- package/dist/events.js +525 -0
- package/dist/files.js +22 -2
- package/dist/git.js +10 -8
- package/dist/home.js +96 -7
- package/dist/house-cli.js +291 -0
- package/dist/house-main.js +7 -159
- package/dist/inputs.js +63 -0
- package/dist/instructions.js +10 -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 +14 -13
- package/dist/parts.js +82 -0
- package/dist/placement.js +30 -0
- package/dist/refusals.js +12 -0
- package/dist/relay.js +12 -2
- package/dist/resident.js +91 -35
- package/dist/scope.js +203 -0
- package/dist/secret-input.js +3 -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 +34 -28
- package/dist/skills.js +81 -12
- package/dist/stream.js +87 -11
- package/dist/transcription.js +57 -9
- package/dist/translate.js +2 -8
- package/dist/update.js +50 -29
- package/dist/wire.js +6 -0
- package/dist/work.js +346 -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/resident.js
CHANGED
|
@@ -1,56 +1,96 @@
|
|
|
1
1
|
import { once } from 'node:events';
|
|
2
2
|
import { watch } from 'node:fs';
|
|
3
3
|
import { mkdir, rm, writeFile } from 'node:fs/promises';
|
|
4
|
-
import { createServer } from 'node:http';
|
|
5
|
-
import { extname, join } from 'node:path';
|
|
4
|
+
import { createServer, get } from 'node:http';
|
|
5
|
+
import { dirname, extname, join } from 'node:path';
|
|
6
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
6
7
|
import { fileURLToPath } from 'node:url';
|
|
7
|
-
import { killDescendants } from './acp.js';
|
|
8
8
|
import { Agents } from './agents.js';
|
|
9
9
|
import { house } from './api.js';
|
|
10
10
|
import { received } from './bridge.js';
|
|
11
|
-
import { Conversations } from './conversations.js';
|
|
11
|
+
import { CONVERSATION_INPUTS, Conversations } from './conversations.js';
|
|
12
12
|
import { LocalCopies } from './copies.js';
|
|
13
|
-
import {
|
|
13
|
+
import { restart } from './end.js';
|
|
14
|
+
import { agentBase, enrolled, forgetRestart, kitHome, ownerLock, ownerSocket, restartAsked } from './home.js';
|
|
15
|
+
import { Inputs } from './inputs.js';
|
|
16
|
+
import { killDescendants } from './scope.js';
|
|
17
|
+
import { quoted } from './shell.js';
|
|
14
18
|
import { SignIns } from './sign-in.js';
|
|
15
19
|
import { placeSkillSet } from './skills.js';
|
|
16
20
|
import { holdStream } from './stream.js';
|
|
17
21
|
import { Updates } from './update.js';
|
|
22
|
+
import { codexLauncher } from './work.js';
|
|
18
23
|
function report(error) {
|
|
19
24
|
process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
20
25
|
}
|
|
21
26
|
function logged(work) {
|
|
22
27
|
work.catch(report);
|
|
23
28
|
}
|
|
24
|
-
function
|
|
25
|
-
|
|
29
|
+
async function installShim(path, module) {
|
|
30
|
+
const main = fileURLToPath(new URL(`./${module}${extname(fileURLToPath(import.meta.url))}`, import.meta.url));
|
|
31
|
+
await mkdir(dirname(path), { recursive: true });
|
|
32
|
+
await writeFile(path, `#!/bin/sh\nexec ${quoted(process.execPath)} ${quoted(main)} "$@"\n`, { mode: 0o755 });
|
|
26
33
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
function claimed(lock) {
|
|
35
|
+
try {
|
|
36
|
+
lock.exec('BEGIN EXCLUSIVE');
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (error.errcode === 5)
|
|
41
|
+
return false;
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
33
44
|
}
|
|
34
45
|
async function serveOwner(copies) {
|
|
35
|
-
const
|
|
36
|
-
|
|
46
|
+
const lock = new DatabaseSync(ownerLock());
|
|
47
|
+
if (!claimed(lock)) {
|
|
48
|
+
lock.close();
|
|
49
|
+
const running = await runningResident();
|
|
50
|
+
throw new Error(`${running === null ? 'another Kit' : `Kit ${running}`} already runs this Environment from Kit home ${kitHome()}; stop it first to run another here`);
|
|
51
|
+
}
|
|
37
52
|
const server = createServer(async (request, response) => {
|
|
38
53
|
try {
|
|
39
|
-
|
|
54
|
+
const text = await received(request);
|
|
55
|
+
let answered;
|
|
56
|
+
if (request.url === '/resident') {
|
|
57
|
+
response.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify({ pid: process.pid }));
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (request.url === '/git/fetch') {
|
|
61
|
+
answered = await copies.fetch();
|
|
62
|
+
}
|
|
63
|
+
else if (request.url === '/git/push') {
|
|
64
|
+
const { cwd, commit } = JSON.parse(text);
|
|
65
|
+
answered = await copies.push(cwd, commit, copies.owner);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
40
68
|
response.writeHead(404).end();
|
|
41
69
|
return;
|
|
42
70
|
}
|
|
43
|
-
|
|
44
|
-
const pushed = await copies.push(cwd, commit, copies.owner);
|
|
45
|
-
response.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify(pushed));
|
|
71
|
+
response.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify(answered));
|
|
46
72
|
}
|
|
47
73
|
catch (error) {
|
|
48
74
|
response.writeHead(502).end(error instanceof Error ? error.message : String(error));
|
|
49
75
|
}
|
|
50
76
|
});
|
|
51
|
-
server.
|
|
77
|
+
server.on('close', () => lock.close());
|
|
78
|
+
await rm(ownerSocket(), { force: true });
|
|
79
|
+
server.listen(ownerSocket());
|
|
52
80
|
await once(server, 'listening');
|
|
53
81
|
}
|
|
82
|
+
function runningResident() {
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
get({ socketPath: ownerSocket(), path: '/resident' }, (answer) => {
|
|
85
|
+
received(answer).then((text) => resolve(JSON.parse(text).pid), reject);
|
|
86
|
+
}).on('error', (error) => {
|
|
87
|
+
if (error.code === 'ENOENT' || error.code === 'ECONNREFUSED')
|
|
88
|
+
resolve(null);
|
|
89
|
+
else
|
|
90
|
+
reject(error);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
}
|
|
54
94
|
function connected() {
|
|
55
95
|
return new Promise((resolve) => {
|
|
56
96
|
const watcher = watch(kitHome(), () => {
|
|
@@ -67,8 +107,9 @@ function connected() {
|
|
|
67
107
|
resolve();
|
|
68
108
|
});
|
|
69
109
|
}
|
|
70
|
-
|
|
110
|
+
async function started() {
|
|
71
111
|
await connected();
|
|
112
|
+
await forgetRestart();
|
|
72
113
|
watch(kitHome(), () => {
|
|
73
114
|
if (enrolled())
|
|
74
115
|
return;
|
|
@@ -77,33 +118,46 @@ export async function resident() {
|
|
|
77
118
|
});
|
|
78
119
|
await placeSkillSet();
|
|
79
120
|
await mkdir(agentBase(), { recursive: true });
|
|
80
|
-
|
|
81
|
-
|
|
121
|
+
await installShim(join(kitHome(), 'shim', 'git'), 'git-main');
|
|
122
|
+
await installShim(codexLauncher(), 'codex-main');
|
|
123
|
+
}
|
|
124
|
+
export async function resident() {
|
|
125
|
+
await mkdir(kitHome(), { recursive: true, mode: 0o700 });
|
|
82
126
|
const copies = new LocalCopies(house);
|
|
83
127
|
await copies.load();
|
|
84
|
-
await installShim();
|
|
85
128
|
await serveOwner(copies);
|
|
129
|
+
await started().catch((error) => {
|
|
130
|
+
report(error);
|
|
131
|
+
process.exit(1);
|
|
132
|
+
});
|
|
133
|
+
process.env.HOUSE_KIT_RESIDENT = String(process.pid);
|
|
134
|
+
const agents = new Agents(house, () => changed());
|
|
86
135
|
let stream = null;
|
|
87
136
|
let reported = false;
|
|
88
|
-
const idle = () => conversations.idle() &&
|
|
137
|
+
const idle = () => inputs.idle() && conversations.idle() && agents.idle();
|
|
89
138
|
const changed = () => {
|
|
90
139
|
if (!idle()) {
|
|
91
140
|
reported = false;
|
|
92
141
|
}
|
|
142
|
+
else if (restartAsked() && conversations.quiet()) {
|
|
143
|
+
restart('restarting for an updated CLI');
|
|
144
|
+
}
|
|
93
145
|
else if (!reported) {
|
|
94
146
|
reported = true;
|
|
95
147
|
logged(house.deliver('/kit/idle', {}, idle));
|
|
96
148
|
}
|
|
97
149
|
};
|
|
150
|
+
const inputs = new Inputs(house, changed);
|
|
98
151
|
const conversations = new Conversations({
|
|
99
152
|
house,
|
|
100
153
|
agents,
|
|
101
154
|
copies,
|
|
155
|
+
inputs,
|
|
102
156
|
send: (frame) => stream?.send(frame) ?? false,
|
|
103
157
|
changed,
|
|
104
158
|
});
|
|
105
|
-
const signIns = new SignIns(house, agents,
|
|
106
|
-
const updates = new Updates(
|
|
159
|
+
const signIns = new SignIns(house, agents, inputs);
|
|
160
|
+
const updates = new Updates(inputs);
|
|
107
161
|
for (const signal of ['SIGTERM', 'SIGINT']) {
|
|
108
162
|
process.once(signal, () => {
|
|
109
163
|
killDescendants();
|
|
@@ -117,24 +171,26 @@ export async function resident() {
|
|
|
117
171
|
opened: () => {
|
|
118
172
|
conversations.opened();
|
|
119
173
|
changed();
|
|
120
|
-
logged(copies.
|
|
174
|
+
logged(copies.connected());
|
|
121
175
|
},
|
|
122
176
|
frame: (arrived) => {
|
|
177
|
+
if (arrived.type === 'watching')
|
|
178
|
+
conversations.watching(arrived.watching === true);
|
|
179
|
+
if (arrived.type === 'authority_changed')
|
|
180
|
+
logged(copies.changed(arrived.rooms));
|
|
123
181
|
if (arrived.type === 'work_available' && arrived.subject === 'agents')
|
|
124
182
|
logged(agents.refresh());
|
|
125
|
-
if (arrived.type === 'work_available' && arrived.subject === 'local_copy')
|
|
126
|
-
logged(copies.select());
|
|
127
|
-
if (arrived.type === 'entries') {
|
|
128
|
-
copies.entries(arrived);
|
|
129
|
-
}
|
|
130
183
|
if (arrived.type === 'input') {
|
|
131
|
-
reported = false;
|
|
132
184
|
if (arrived.kind === 'sign_in')
|
|
133
185
|
signIns.start(arrived);
|
|
134
186
|
else if (arrived.kind === 'update')
|
|
135
187
|
updates.start(arrived);
|
|
136
|
-
else
|
|
188
|
+
else if (CONVERSATION_INPUTS.has(arrived.kind))
|
|
137
189
|
conversations.input(arrived);
|
|
190
|
+
else {
|
|
191
|
+
const refused = { refused: `This Kit does not know ${arrived.kind}; update the Kit` };
|
|
192
|
+
void inputs.carry(String(arrived.input_id), async () => ({ ack: refused }));
|
|
193
|
+
}
|
|
138
194
|
}
|
|
139
195
|
},
|
|
140
196
|
});
|
package/dist/scope.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { mkdirSync, readdirSync, readFileSync, rmdirSync, statSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
const LISTING_BYTES = 1 << 28;
|
|
5
|
+
const CGROUPS = '/sys/fs/cgroup';
|
|
6
|
+
const GONE = new Set(['ENOENT', 'ESRCH']);
|
|
7
|
+
function failure(error) {
|
|
8
|
+
return error.code ?? '';
|
|
9
|
+
}
|
|
10
|
+
function listed(environment) {
|
|
11
|
+
const found = new Map();
|
|
12
|
+
const output = execFileSync('ps', [environment ? '-AEww' : '-Aww', '-o', 'pid=,ppid=,sess=,command='], {
|
|
13
|
+
encoding: 'utf8',
|
|
14
|
+
maxBuffer: LISTING_BYTES,
|
|
15
|
+
});
|
|
16
|
+
for (const line of output.split('\n')) {
|
|
17
|
+
const fields = /^\s*(\d+)\s+(\d+)\s+(\S+)\s?(.*)$/.exec(line);
|
|
18
|
+
if (fields !== null)
|
|
19
|
+
found.set(Number(fields[1]), { parent: Number(fields[2]), session: fields[3], command: fields[4] });
|
|
20
|
+
}
|
|
21
|
+
return found;
|
|
22
|
+
}
|
|
23
|
+
function read(pid) {
|
|
24
|
+
try {
|
|
25
|
+
const stat = readFileSync(`/proc/${pid}/stat`, 'utf8');
|
|
26
|
+
const fields = stat.slice(stat.lastIndexOf(')') + 2).split(' ');
|
|
27
|
+
const command = readFileSync(`/proc/${pid}/cmdline`, 'utf8').replaceAll('\0', ' ');
|
|
28
|
+
return { parent: Number(fields[1]), session: fields[3], command };
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
if (GONE.has(failure(error)))
|
|
32
|
+
return null;
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function processes() {
|
|
37
|
+
if (process.platform === 'darwin')
|
|
38
|
+
return listed(false);
|
|
39
|
+
const found = new Map();
|
|
40
|
+
for (const entry of readdirSync('/proc')) {
|
|
41
|
+
const running = /^\d+$/.test(entry) ? read(entry) : null;
|
|
42
|
+
if (running !== null)
|
|
43
|
+
found.set(Number(entry), running);
|
|
44
|
+
}
|
|
45
|
+
return found;
|
|
46
|
+
}
|
|
47
|
+
function descendants(roots, table = processes()) {
|
|
48
|
+
const children = new Map();
|
|
49
|
+
for (const [pid, { parent }] of table)
|
|
50
|
+
children.set(parent, [...(children.get(parent) ?? []), pid]);
|
|
51
|
+
const tree = [];
|
|
52
|
+
const pending = roots.flatMap((root) => children.get(root) ?? []);
|
|
53
|
+
while (pending.length > 0) {
|
|
54
|
+
const pid = pending.pop();
|
|
55
|
+
tree.push(pid);
|
|
56
|
+
pending.push(...(children.get(pid) ?? []));
|
|
57
|
+
}
|
|
58
|
+
return tree;
|
|
59
|
+
}
|
|
60
|
+
function marked(marker, doubted) {
|
|
61
|
+
if (process.platform === 'darwin') {
|
|
62
|
+
return [...listed(true)].filter(([, { command }]) => ` ${command} `.includes(` ${marker} `)).map(([pid]) => pid);
|
|
63
|
+
}
|
|
64
|
+
return readdirSync('/proc')
|
|
65
|
+
.filter((entry) => /^\d+$/.test(entry) && carries(entry, marker, doubted))
|
|
66
|
+
.map(Number);
|
|
67
|
+
}
|
|
68
|
+
function carries(pid, marker, doubted) {
|
|
69
|
+
try {
|
|
70
|
+
return readFileSync(`/proc/${pid}/environ`, 'utf8').split('\0').includes(marker);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (GONE.has(failure(error)))
|
|
74
|
+
return false;
|
|
75
|
+
if (failure(error) !== 'EACCES')
|
|
76
|
+
throw error;
|
|
77
|
+
return doubted && statSync(`/proc/${pid}`, { throwIfNoEntry: false })?.uid === process.getuid();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function killAll(pids) {
|
|
81
|
+
for (const pid of pids) {
|
|
82
|
+
for (const target of [-pid, pid]) {
|
|
83
|
+
try {
|
|
84
|
+
process.kill(target, 'SIGKILL');
|
|
85
|
+
}
|
|
86
|
+
catch { }
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
export function killTree(child) {
|
|
91
|
+
killAll([child.pid, ...descendants([child.pid])]);
|
|
92
|
+
}
|
|
93
|
+
export function killMarked(marker) {
|
|
94
|
+
const pids = marked(marker, false);
|
|
95
|
+
killAll([...pids, ...descendants(pids)]);
|
|
96
|
+
}
|
|
97
|
+
export function killDescendants() {
|
|
98
|
+
const pids = marked(`HOUSE_KIT_RESIDENT=${process.pid}`, false);
|
|
99
|
+
killAll([...pids, ...descendants([process.pid, ...pids])]);
|
|
100
|
+
}
|
|
101
|
+
function ownGroup() {
|
|
102
|
+
if (process.platform !== 'linux')
|
|
103
|
+
return null;
|
|
104
|
+
try {
|
|
105
|
+
const line = readFileSync('/proc/self/cgroup', 'utf8')
|
|
106
|
+
.split('\n')
|
|
107
|
+
.find((entry) => entry.startsWith('0::'));
|
|
108
|
+
return line === undefined ? null : join(CGROUPS, line.slice(3));
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function members(group) {
|
|
115
|
+
const pids = readFileSync(join(group, 'cgroup.procs'), 'utf8').split('\n').filter(Boolean).map(Number);
|
|
116
|
+
for (const entry of readdirSync(group, { withFileTypes: true })) {
|
|
117
|
+
if (entry.isDirectory())
|
|
118
|
+
pids.push(...members(join(group, entry.name)));
|
|
119
|
+
}
|
|
120
|
+
return pids;
|
|
121
|
+
}
|
|
122
|
+
export class Scope {
|
|
123
|
+
marker;
|
|
124
|
+
cli;
|
|
125
|
+
group;
|
|
126
|
+
session = null;
|
|
127
|
+
recorded = null;
|
|
128
|
+
constructor(marker, name, cli) {
|
|
129
|
+
this.marker = marker;
|
|
130
|
+
this.cli = cli;
|
|
131
|
+
const own = ownGroup();
|
|
132
|
+
this.group = own === null ? null : join(own, name);
|
|
133
|
+
try {
|
|
134
|
+
if (this.group !== null)
|
|
135
|
+
mkdirSync(this.group);
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
this.group = null;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
command(command, args) {
|
|
142
|
+
if (this.group === null)
|
|
143
|
+
return [command, args];
|
|
144
|
+
return ['/bin/sh', ['-c', 'echo $$ > "$0/cgroup.procs" 2>/dev/null; exec "$@"', this.group, command, ...args]];
|
|
145
|
+
}
|
|
146
|
+
entered(pid) {
|
|
147
|
+
this.session = process.platform === 'darwin' ? (listed(false).get(pid)?.session ?? null) : String(pid);
|
|
148
|
+
if (this.group === null)
|
|
149
|
+
return;
|
|
150
|
+
const joined = readFileSync(`/proc/${pid}/cgroup`, 'utf8').split('\n').find((entry) => entry.startsWith('0::'));
|
|
151
|
+
if (joined !== undefined && join(CGROUPS, joined.slice(3)) === this.group)
|
|
152
|
+
return;
|
|
153
|
+
this.close();
|
|
154
|
+
this.group = null;
|
|
155
|
+
}
|
|
156
|
+
record() {
|
|
157
|
+
const found = this.found();
|
|
158
|
+
if (found === null)
|
|
159
|
+
return;
|
|
160
|
+
this.recorded = new Set([...found].filter(([, { session }]) => session === this.session).map(([pid]) => pid));
|
|
161
|
+
}
|
|
162
|
+
working() {
|
|
163
|
+
const found = this.found();
|
|
164
|
+
if (found === null)
|
|
165
|
+
return true;
|
|
166
|
+
if (this.recorded === null)
|
|
167
|
+
return found.size > 0;
|
|
168
|
+
return [...found].some(([pid, { session, command }]) => session !== this.session || !(this.recorded.has(pid) || command.startsWith(`${this.cli}-`)));
|
|
169
|
+
}
|
|
170
|
+
empty() {
|
|
171
|
+
return this.found()?.size === 0;
|
|
172
|
+
}
|
|
173
|
+
kill() {
|
|
174
|
+
killMarked(this.marker);
|
|
175
|
+
if (this.group === null)
|
|
176
|
+
return;
|
|
177
|
+
try {
|
|
178
|
+
writeFileSync(join(this.group, 'cgroup.kill'), '1');
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
killAll([...(this.found()?.keys() ?? [])]);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
close() {
|
|
185
|
+
if (this.group === null)
|
|
186
|
+
return;
|
|
187
|
+
try {
|
|
188
|
+
rmdirSync(this.group);
|
|
189
|
+
}
|
|
190
|
+
catch { }
|
|
191
|
+
}
|
|
192
|
+
found() {
|
|
193
|
+
try {
|
|
194
|
+
const table = processes();
|
|
195
|
+
const roots = this.group === null ? marked(this.marker, true) : members(this.group);
|
|
196
|
+
const pids = this.group === null ? [...roots, ...descendants(roots, table)] : roots;
|
|
197
|
+
return new Map(pids.flatMap((pid) => (table.has(pid) ? [[pid, table.get(pid)]] : [])));
|
|
198
|
+
}
|
|
199
|
+
catch {
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
package/dist/secret-input.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { finalRefusal, retryDelay } from './api.js';
|
|
2
2
|
export async function holdSecretInput(house, subject, steps, waiting) {
|
|
3
3
|
for (let failures = 0; waiting();) {
|
|
4
4
|
try {
|
|
@@ -10,9 +10,10 @@ export async function holdSecretInput(house, subject, steps, waiting) {
|
|
|
10
10
|
failures = 0;
|
|
11
11
|
}
|
|
12
12
|
catch (error) {
|
|
13
|
-
if (error
|
|
13
|
+
if (finalRefusal(error))
|
|
14
14
|
return null;
|
|
15
15
|
process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
16
|
+
// A wait that did not reach House is held again on the jittered curve for as long as its asker still waits.
|
|
16
17
|
await new Promise((resolve) => setTimeout(resolve, retryDelay(failures++)));
|
|
17
18
|
}
|
|
18
19
|
}
|
package/dist/service.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, realpathSync } from 'node:fs';
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
|
+
import { kitHome } from './home.js';
|
|
5
|
+
const UNIT = 'house-kit.service';
|
|
6
|
+
// launchctl and systemctl answer at once, so ten seconds only bounds a stuck service manager.
|
|
7
|
+
const MANAGER_MS = 10_000;
|
|
8
|
+
function manage(command, args) {
|
|
9
|
+
return spawnSync(command, args, { encoding: 'utf8', timeout: MANAGER_MS });
|
|
10
|
+
}
|
|
11
|
+
function start(command, args) {
|
|
12
|
+
const started = manage(command, args);
|
|
13
|
+
if (started.status === 0)
|
|
14
|
+
return;
|
|
15
|
+
const cause = started.error?.message ?? (started.stderr.trim() || `${command} exited with ${started.status ?? 'a signal'}`);
|
|
16
|
+
throw new Error(`the House Kit service could not be started: ${cause}`);
|
|
17
|
+
}
|
|
18
|
+
function physical(path) {
|
|
19
|
+
return existsSync(path) ? realpathSync(path) : resolve(path);
|
|
20
|
+
}
|
|
21
|
+
function systemdHome(environment) {
|
|
22
|
+
for (const [, quoted, bare] of environment.matchAll(/"((?:[^"\\]|\\.)*)"|(\S+)/g)) {
|
|
23
|
+
const assignment = bare ?? quoted.replace(/\\(.)/g, '$1');
|
|
24
|
+
if (assignment.startsWith('HOUSE_KIT_HOME='))
|
|
25
|
+
return assignment.slice('HOUSE_KIT_HOME='.length);
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
function runsThisHome(served) {
|
|
30
|
+
return served !== undefined && physical(served) === physical(kitHome());
|
|
31
|
+
}
|
|
32
|
+
export function startService() {
|
|
33
|
+
if (process.platform === 'darwin') {
|
|
34
|
+
const service = `gui/${process.getuid()}/house-kit`;
|
|
35
|
+
const printed = manage('launchctl', ['print', service]);
|
|
36
|
+
if (printed.status !== 0 || /^\tstate = running$/m.test(printed.stdout))
|
|
37
|
+
return false;
|
|
38
|
+
if (!runsThisHome(/^\t+HOUSE_KIT_HOME => (.+)$/m.exec(printed.stdout)?.[1]))
|
|
39
|
+
return false;
|
|
40
|
+
start('launchctl', ['kickstart', service]);
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
for (const scope of [['--user'], []]) {
|
|
44
|
+
if (manage('systemctl', [...scope, 'is-enabled', '--quiet', UNIT]).status !== 0)
|
|
45
|
+
continue;
|
|
46
|
+
const environment = manage('systemctl', [...scope, 'show', '--property=Environment', '--value', UNIT]).stdout;
|
|
47
|
+
if (!runsThisHome(systemdHome(environment)))
|
|
48
|
+
continue;
|
|
49
|
+
if (manage('systemctl', [...scope, 'is-active', '--quiet', UNIT]).status === 0)
|
|
50
|
+
return false;
|
|
51
|
+
start('systemctl', [...scope, 'start', UNIT]);
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
package/dist/shell.js
ADDED
package/dist/sign-in.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
import { createInterface } from 'node:readline';
|
|
3
|
-
import { killTree } from './acp.js';
|
|
4
3
|
import { CLIS, withoutProxy } from './clis.js';
|
|
4
|
+
import { killTree } from './scope.js';
|
|
5
5
|
import { holdSecretInput } from './secret-input.js';
|
|
6
6
|
const ESCAPE = /\u001b\[[\d;]*m/g;
|
|
7
7
|
const LINK = /https:\/\/\S+/;
|
|
@@ -9,46 +9,49 @@ const CODE = /^[A-Z0-9]+-[A-Z0-9]+$/;
|
|
|
9
9
|
function causeOf(error) {
|
|
10
10
|
return error instanceof Error ? error.message : String(error);
|
|
11
11
|
}
|
|
12
|
-
function
|
|
13
|
-
|
|
12
|
+
function endOf(signal) {
|
|
13
|
+
return new Promise((resolve) => signal.addEventListener('abort', () => resolve(true), { once: true }));
|
|
14
14
|
}
|
|
15
15
|
export class SignIns {
|
|
16
16
|
house;
|
|
17
17
|
agents;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
constructor(house, agents, changed) {
|
|
18
|
+
inputs;
|
|
19
|
+
constructor(house, agents, inputs) {
|
|
21
20
|
this.house = house;
|
|
22
21
|
this.agents = agents;
|
|
23
|
-
this.
|
|
24
|
-
}
|
|
25
|
-
idle() {
|
|
26
|
-
return this.running.size === 0;
|
|
22
|
+
this.inputs = inputs;
|
|
27
23
|
}
|
|
28
24
|
start(input) {
|
|
29
|
-
|
|
30
|
-
return;
|
|
31
|
-
this.running.add(input.input_id);
|
|
32
|
-
this.changed();
|
|
33
|
-
this.login(input)
|
|
34
|
-
.then(() => this.agents.recheck(input.cli))
|
|
35
|
-
.then(() => this.house.deliver(`/kit/inputs/${input.input_id}/ack`, {}))
|
|
36
|
-
.then(() => this.agents.reread(input.cli))
|
|
37
|
-
.catch(logged)
|
|
38
|
-
.finally(() => {
|
|
39
|
-
this.running.delete(input.input_id);
|
|
40
|
-
this.changed();
|
|
41
|
-
});
|
|
25
|
+
void this.inputs.carry(input.input_id, () => this.signIn(input));
|
|
42
26
|
}
|
|
43
|
-
async
|
|
27
|
+
async signIn(input) {
|
|
28
|
+
const ending = new AbortController();
|
|
29
|
+
// House stops waiting for a sign-in ten minutes after it wrote it and sends the seconds left, so its install and login end with the page.
|
|
30
|
+
const timer = setTimeout(() => ending.abort(), input.ends_in * 1000);
|
|
31
|
+
const ended = await Promise.race([this.signedIn(input, ending.signal), endOf(ending.signal)]);
|
|
32
|
+
clearTimeout(timer);
|
|
33
|
+
if (ended)
|
|
34
|
+
process.stderr.write(`kit: the ${input.cli} sign-in reached the end House set for it\n`);
|
|
35
|
+
return ended ? { ack: {} } : { ack: {}, acknowledged: () => this.agents.reread(input.cli) };
|
|
36
|
+
}
|
|
37
|
+
async signedIn(input, signal) {
|
|
38
|
+
await this.login(input, signal);
|
|
39
|
+
if (!signal.aborted)
|
|
40
|
+
await this.agents.recheck(input.cli, signal);
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
async login(input, signal) {
|
|
44
44
|
let cli;
|
|
45
45
|
try {
|
|
46
|
-
cli = await this.agents.located(input.cli);
|
|
46
|
+
cli = await this.agents.located(input.cli, signal);
|
|
47
47
|
}
|
|
48
48
|
catch (error) {
|
|
49
|
-
|
|
49
|
+
if (!signal.aborted)
|
|
50
|
+
process.stderr.write(`kit: the ${input.cli} sign-in failed: ${causeOf(error)}\n`);
|
|
50
51
|
return;
|
|
51
52
|
}
|
|
53
|
+
if (signal.aborted)
|
|
54
|
+
return;
|
|
52
55
|
const login = CLIS[input.cli].login;
|
|
53
56
|
const child = spawn(cli, login.args, {
|
|
54
57
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
@@ -56,6 +59,8 @@ export class SignIns {
|
|
|
56
59
|
env: withoutProxy(),
|
|
57
60
|
});
|
|
58
61
|
const running = () => child.exitCode === null && child.signalCode === null;
|
|
62
|
+
const stop = () => killTree(child);
|
|
63
|
+
signal.addEventListener('abort', stop, { once: true });
|
|
59
64
|
let link;
|
|
60
65
|
let code;
|
|
61
66
|
let last = '';
|
|
@@ -92,9 +97,10 @@ export class SignIns {
|
|
|
92
97
|
createInterface({ input: output }).on('line', read);
|
|
93
98
|
const failed = await new Promise((resolve) => {
|
|
94
99
|
child.on('error', (error) => resolve(error.message));
|
|
95
|
-
child.on('close', (status,
|
|
100
|
+
child.on('close', (status, killed) => resolve(status === 0 ? null : `exited with ${status ?? killed}${last ? `: ${last}` : ''}`));
|
|
96
101
|
});
|
|
97
|
-
|
|
102
|
+
signal.removeEventListener('abort', stop);
|
|
103
|
+
if (failed !== null && !signal.aborted)
|
|
98
104
|
process.stderr.write(`kit: the ${input.cli} sign-in failed: ${failed}\n`);
|
|
99
105
|
}
|
|
100
106
|
}
|