@agentshouse/kit 0.2.1-cb6679d048e60 → 0.2.1-cbabe8ad337ff
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 +13 -13
- package/dist/acp.js +16 -93
- package/dist/agents.js +81 -39
- package/dist/api.js +32 -3
- package/dist/app-git.js +64 -0
- package/dist/bootstrap.json +4 -4
- package/dist/bridge.js +35 -40
- package/dist/clis.js +95 -65
- package/dist/codex-main.js +58 -0
- package/dist/conversations.js +284 -236
- package/dist/copies.js +29 -36
- package/dist/end.js +8 -0
- package/dist/events.js +521 -0
- package/dist/files.js +16 -1
- package/dist/house-main.js +28 -10
- package/dist/instructions.js +2 -2
- package/dist/kit-main.js +3 -0
- package/dist/login.js +23 -98
- package/dist/logout.js +16 -13
- package/dist/mcp.js +2 -1
- package/dist/parts.js +64 -0
- package/dist/refusals.js +12 -0
- package/dist/relay.js +10 -0
- package/dist/resident.js +12 -12
- package/dist/scope.js +203 -0
- package/dist/secret-input.js +4 -1
- package/dist/shell.js +3 -0
- package/dist/sign-in.js +34 -9
- package/dist/skills.js +1 -1
- package/dist/stream.js +31 -5
- package/dist/transcription.js +54 -6
- package/dist/update.js +2 -0
- package/dist/work.js +348 -0
- package/package.json +1 -1
- package/dist/blocks.js +0 -51
package/dist/login.js
CHANGED
|
@@ -1,53 +1,19 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
1
2
|
import { createHash, randomBytes } from 'node:crypto';
|
|
2
|
-
import { createServer } from 'node:http';
|
|
3
3
|
import { hostname } from 'node:os';
|
|
4
|
-
import { createInterface } from 'node:readline/promises';
|
|
5
4
|
import { parseArgs } from 'node:util';
|
|
6
|
-
import { post } from './api.js';
|
|
5
|
+
import { post, retrying } from './api.js';
|
|
6
|
+
import { placeAppGit } from './app-git.js';
|
|
7
7
|
import { forgetOwnAgent, readDisconnected, readEnrolment, readOwnAgent, writeEnrolment, writeOwnAgent } from './home.js';
|
|
8
8
|
import { callHouse, rpc } from './mcp.js';
|
|
9
9
|
const DEFAULT_HOUSE = 'https://agents.house';
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const server = createServer((request, response) => {
|
|
18
|
-
const received = new URL(request.url ?? '/', 'http://127.0.0.1').searchParams.get('code');
|
|
19
|
-
if (received === null || held !== null) {
|
|
20
|
-
response.writeHead(404).end();
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
held = response;
|
|
24
|
-
deliver(received);
|
|
25
|
-
});
|
|
26
|
-
const close = () => {
|
|
27
|
-
server.close();
|
|
28
|
-
server.closeAllConnections();
|
|
29
|
-
};
|
|
30
|
-
const port = Number(process.env.HOUSE_KIT_LOGIN_PORT ?? 0);
|
|
31
|
-
server.once('error', reject);
|
|
32
|
-
server.listen(port, port === 0 ? '127.0.0.1' : '0.0.0.0', () => {
|
|
33
|
-
const address = server.address();
|
|
34
|
-
if (address === null || typeof address === 'string') {
|
|
35
|
-
reject(new Error('the login callback has no port'));
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
resolve({
|
|
39
|
-
uri: `http://127.0.0.1:${address.port}/callback`,
|
|
40
|
-
code,
|
|
41
|
-
answer: (text) => new Promise((written) => {
|
|
42
|
-
if (held === null || held.destroyed || !held.socket || held.socket.destroyed)
|
|
43
|
-
return written();
|
|
44
|
-
held.once('close', () => written());
|
|
45
|
-
held.writeHead(200, { 'content-type': 'text/plain; charset=utf-8', connection: 'close' }).end(`${text}\n`);
|
|
46
|
-
}),
|
|
47
|
-
close,
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
});
|
|
10
|
+
const OPENER = process.platform === 'darwin' ? 'open' : 'xdg-open';
|
|
11
|
+
// Polling every two seconds brings the User's confirmation to the waiting Kit moments after they give it.
|
|
12
|
+
const POLL_INTERVAL_MS = 2000;
|
|
13
|
+
function open(link) {
|
|
14
|
+
spawn(OPENER, [link], { detached: true, stdio: 'ignore' })
|
|
15
|
+
.on('error', () => undefined)
|
|
16
|
+
.unref();
|
|
51
17
|
}
|
|
52
18
|
async function accepted(house, credential) {
|
|
53
19
|
const checked = await callHouse(house, credential, rpc('tools/list', {}));
|
|
@@ -57,25 +23,12 @@ async function accepted(house, credential) {
|
|
|
57
23
|
return false;
|
|
58
24
|
throw new Error(`House answered ${checked.status} when Kit checked this computer's own agent's connection: ${checked.text}`);
|
|
59
25
|
}
|
|
60
|
-
async function typed(question) {
|
|
61
|
-
const reader = createInterface({ input: process.stdin, output: process.stdout });
|
|
62
|
-
const closed = new Promise((_, reject) => {
|
|
63
|
-
reader.once('close', () => reject(new Error('no code was typed')));
|
|
64
|
-
});
|
|
65
|
-
try {
|
|
66
|
-
return (await Promise.race([reader.question(question), closed])).trim();
|
|
67
|
-
}
|
|
68
|
-
finally {
|
|
69
|
-
reader.close();
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
26
|
export async function login(argv) {
|
|
73
27
|
const { values } = parseArgs({
|
|
74
28
|
args: argv,
|
|
75
29
|
options: {
|
|
76
30
|
house: { type: 'string' },
|
|
77
31
|
environment: { type: 'string' },
|
|
78
|
-
manual: { type: 'boolean', default: false },
|
|
79
32
|
},
|
|
80
33
|
});
|
|
81
34
|
const enrolled = (await readEnrolment()) ?? (await readDisconnected());
|
|
@@ -85,52 +38,24 @@ export async function login(argv) {
|
|
|
85
38
|
const ownAgent = await readOwnAgent();
|
|
86
39
|
const held = ownAgent !== null && ownAgent.house === house && (await accepted(house, ownAgent.credential));
|
|
87
40
|
const verifier = randomBytes(32).toString('base64url');
|
|
88
|
-
const started = {
|
|
41
|
+
const started = await retrying(() => post(house, '/kit', {
|
|
89
42
|
...(environment === undefined
|
|
90
43
|
? { act: 'new_environment', label: hostname(), ...(replaces === undefined ? {} : { replaces }) }
|
|
91
44
|
: { act: 'existing_environment', environment_id: environment }),
|
|
92
45
|
code_challenge: createHash('sha256').update(verifier).digest('base64url'),
|
|
93
46
|
...(held ? {} : { headless: true }),
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
else if (held && ownAgent.user !== exchanged.user) {
|
|
103
|
-
await forgetOwnAgent();
|
|
104
|
-
process.stdout.write("This computer's own agent was another User's; run kit login again to connect it to you.\n");
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
if (values.manual) {
|
|
108
|
-
const { start_url } = await post(house, '/kit', {
|
|
109
|
-
...started,
|
|
110
|
-
presentation: 'manual',
|
|
111
|
-
});
|
|
112
|
-
process.stdout.write(`Open this link and confirm: ${start_url}\n`);
|
|
113
|
-
await exchange(await typed('Code: '));
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
const callback = await listen();
|
|
117
|
-
try {
|
|
118
|
-
const { start_url } = await post(house, '/kit', {
|
|
119
|
-
...started,
|
|
120
|
-
presentation: 'loopback',
|
|
121
|
-
loopback_uri: callback.uri,
|
|
122
|
-
});
|
|
123
|
-
process.stdout.write(`Open this link and confirm: ${start_url}\n`);
|
|
124
|
-
try {
|
|
125
|
-
await exchange(await callback.code);
|
|
126
|
-
}
|
|
127
|
-
catch (error) {
|
|
128
|
-
await callback.answer(`This Environment was not connected: ${error.message}`);
|
|
129
|
-
throw error;
|
|
130
|
-
}
|
|
131
|
-
await callback.answer('This Environment is connected. You can close this tab.');
|
|
47
|
+
}));
|
|
48
|
+
process.stdout.write(`Open this link and confirm: ${started.start_url}\n`);
|
|
49
|
+
open(started.start_url);
|
|
50
|
+
const exchanged = await retrying(() => post(house, '/kit/token', { login: started.login, code_verifier: verifier }), undefined, () => POLL_INTERVAL_MS);
|
|
51
|
+
await writeEnrolment({ house, environment: exchanged.environment, credential: exchanged.credential });
|
|
52
|
+
process.stdout.write(`Environment ${exchanged.environment} is connected to ${house}.\n`);
|
|
53
|
+
if (exchanged.headless_credential !== undefined) {
|
|
54
|
+
await writeOwnAgent({ house, user: exchanged.user, credential: exchanged.headless_credential });
|
|
132
55
|
}
|
|
133
|
-
|
|
134
|
-
|
|
56
|
+
else if (held && ownAgent.user !== exchanged.user) {
|
|
57
|
+
await forgetOwnAgent();
|
|
58
|
+
process.stdout.write("This computer's own agent was another User's; run kit login again to connect it to you.\n");
|
|
135
59
|
}
|
|
60
|
+
await placeAppGit(ownAgent, await readOwnAgent());
|
|
136
61
|
}
|
package/dist/logout.js
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
import { HouseRefusal, post } from './api.js';
|
|
2
|
-
import {
|
|
2
|
+
import { forgetAppGit } from './app-git.js';
|
|
3
|
+
import { forgetEnrolment, forgetOwnAgent, readEnrolment } from './home.js';
|
|
3
4
|
export async function logout() {
|
|
4
5
|
const enrolled = await readEnrolment();
|
|
5
|
-
if (enrolled
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
if (enrolled !== null) {
|
|
7
|
+
try {
|
|
8
|
+
await post(enrolled.house, '/kit/logout', {}, enrolled.credential);
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
if (!(error instanceof HouseRefusal && error.status === 401))
|
|
12
|
+
throw error;
|
|
13
|
+
}
|
|
14
|
+
await forgetEnrolment(enrolled);
|
|
8
15
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
throw error;
|
|
15
|
-
}
|
|
16
|
-
await forgetEnrolment(enrolled);
|
|
17
|
-
process.stdout.write(`Environment ${enrolled.environment} is disconnected; kit login reconnects it.\n`);
|
|
16
|
+
await forgetAppGit();
|
|
17
|
+
await forgetOwnAgent();
|
|
18
|
+
process.stdout.write(enrolled === null
|
|
19
|
+
? 'This computer is not connected to House.\n'
|
|
20
|
+
: `Environment ${enrolled.environment} is disconnected; kit login reconnects it.\n`);
|
|
18
21
|
}
|
package/dist/mcp.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
2
|
import { KIT_VERSION } from './clis.js';
|
|
3
|
+
import { relayed } from './relay.js';
|
|
3
4
|
const MCP_PROTOCOL_VERSION = '2026-07-28';
|
|
4
5
|
export const UNREACHABLE = 'House is unreachable; call again in a minute';
|
|
5
6
|
export function rpc(method, params) {
|
|
@@ -21,7 +22,7 @@ export function mcpBody(message, operation) {
|
|
|
21
22
|
}
|
|
22
23
|
export async function callHouse(origin, credential, message, operation = null, signal) {
|
|
23
24
|
const tool = message.method === 'tools/call' ? String(message.params.name) : null;
|
|
24
|
-
const answer = await fetch(new URL('/', origin), {
|
|
25
|
+
const answer = await fetch(relayed(new URL('/', origin)), {
|
|
25
26
|
method: 'POST',
|
|
26
27
|
headers: {
|
|
27
28
|
'content-type': 'application/json',
|
package/dist/parts.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export class Parts {
|
|
2
|
+
parts = [];
|
|
3
|
+
context = null;
|
|
4
|
+
emit;
|
|
5
|
+
writing = null;
|
|
6
|
+
planned = null;
|
|
7
|
+
constructor(emit) {
|
|
8
|
+
this.emit = emit;
|
|
9
|
+
}
|
|
10
|
+
write(type, id, text) {
|
|
11
|
+
if (text === '')
|
|
12
|
+
return;
|
|
13
|
+
const last = this.parts.length - 1;
|
|
14
|
+
const part = this.parts[last];
|
|
15
|
+
if (this.writing?.type === type && this.writing.id === id && part?.type === type) {
|
|
16
|
+
part.text += text;
|
|
17
|
+
this.emit({ op: 'append', index: last, text });
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
this.start({ type, text });
|
|
21
|
+
this.writing = { type, id };
|
|
22
|
+
}
|
|
23
|
+
command() {
|
|
24
|
+
const last = this.parts.length - 1;
|
|
25
|
+
const part = this.parts[last];
|
|
26
|
+
if (part?.type !== 'commands') {
|
|
27
|
+
this.start({ type: 'commands', count: 1 });
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
part.count++;
|
|
31
|
+
this.emit({ op: 'count', index: last, count: part.count });
|
|
32
|
+
}
|
|
33
|
+
start(part) {
|
|
34
|
+
this.writing = null;
|
|
35
|
+
this.parts.push(part);
|
|
36
|
+
const index = this.parts.length - 1;
|
|
37
|
+
this.emit({ op: 'start', index, part });
|
|
38
|
+
return index;
|
|
39
|
+
}
|
|
40
|
+
done(index, result) {
|
|
41
|
+
const part = this.parts[index];
|
|
42
|
+
part.status = 'done';
|
|
43
|
+
part.result = result;
|
|
44
|
+
this.emit({ op: 'status', index, status: 'done', result });
|
|
45
|
+
}
|
|
46
|
+
plan(entries) {
|
|
47
|
+
if (this.planned === null) {
|
|
48
|
+
this.planned = this.start({ type: 'plan', entries });
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
this.parts[this.planned].entries = entries;
|
|
52
|
+
this.emit({ op: 'plan', index: this.planned, entries });
|
|
53
|
+
}
|
|
54
|
+
measure(context) {
|
|
55
|
+
this.context = context;
|
|
56
|
+
this.emit({ op: 'context', ...context });
|
|
57
|
+
}
|
|
58
|
+
operations() {
|
|
59
|
+
return [
|
|
60
|
+
...this.parts.map((part, index) => ({ op: 'start', index, part })),
|
|
61
|
+
...(this.context === null ? [] : [{ op: 'context', ...this.context }]),
|
|
62
|
+
];
|
|
63
|
+
}
|
|
64
|
+
}
|
package/dist/refusals.js
CHANGED
|
@@ -21,6 +21,18 @@ function spoken(text) {
|
|
|
21
21
|
const detail = said.indexOf('\n');
|
|
22
22
|
return { code: head[1], text: said, conflicts: detail === -1 ? [] : conflictCodes(said.slice(detail + 1)) };
|
|
23
23
|
}
|
|
24
|
+
export function retryable(text) {
|
|
25
|
+
let body;
|
|
26
|
+
try {
|
|
27
|
+
body = JSON.parse(text);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
const error = body?.error;
|
|
33
|
+
const verdict = error?.retryable ?? error?.data?.retryable;
|
|
34
|
+
return typeof verdict === 'boolean' ? verdict : null;
|
|
35
|
+
}
|
|
24
36
|
export function refusalOf(text) {
|
|
25
37
|
let body;
|
|
26
38
|
try {
|
package/dist/relay.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function relayed(target) {
|
|
2
|
+
const url = new URL(target);
|
|
3
|
+
const relay = process.env.HOUSE_KIT_RELAY;
|
|
4
|
+
if (relay === undefined || relay === '')
|
|
5
|
+
return url;
|
|
6
|
+
const through = new URL(`${url.host}${url.pathname}${url.search}`, `${relay}/`);
|
|
7
|
+
if (url.protocol === 'wss:' || url.protocol === 'ws:')
|
|
8
|
+
through.protocol = 'wss:';
|
|
9
|
+
return through;
|
|
10
|
+
}
|
package/dist/resident.js
CHANGED
|
@@ -2,34 +2,31 @@ import { once } from 'node:events';
|
|
|
2
2
|
import { watch } from 'node:fs';
|
|
3
3
|
import { mkdir, rm, writeFile } from 'node:fs/promises';
|
|
4
4
|
import { createServer } from 'node:http';
|
|
5
|
-
import { extname, join } from 'node:path';
|
|
5
|
+
import { dirname, extname, join } from 'node:path';
|
|
6
6
|
import { fileURLToPath } from 'node:url';
|
|
7
|
-
import { killDescendants } from './acp.js';
|
|
8
7
|
import { Agents } from './agents.js';
|
|
9
8
|
import { house } from './api.js';
|
|
10
9
|
import { received } from './bridge.js';
|
|
11
10
|
import { Conversations } from './conversations.js';
|
|
12
11
|
import { LocalCopies } from './copies.js';
|
|
13
12
|
import { agentBase, enrolled, kitHome } from './home.js';
|
|
13
|
+
import { killDescendants } from './scope.js';
|
|
14
|
+
import { quoted } from './shell.js';
|
|
14
15
|
import { SignIns } from './sign-in.js';
|
|
15
16
|
import { placeSkillSet } from './skills.js';
|
|
16
17
|
import { holdStream } from './stream.js';
|
|
17
18
|
import { Updates } from './update.js';
|
|
19
|
+
import { codexLauncher } from './work.js';
|
|
18
20
|
function report(error) {
|
|
19
21
|
process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
20
22
|
}
|
|
21
23
|
function logged(work) {
|
|
22
24
|
work.catch(report);
|
|
23
25
|
}
|
|
24
|
-
function
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const main = fileURLToPath(new URL(`./git-main${extname(fileURLToPath(import.meta.url))}`, import.meta.url));
|
|
29
|
-
await mkdir(join(kitHome(), 'shim'), { recursive: true });
|
|
30
|
-
await writeFile(join(kitHome(), 'shim', 'git'), `#!/bin/sh\nexec ${quoted(process.execPath)} ${quoted(main)} "$@"\n`, {
|
|
31
|
-
mode: 0o755,
|
|
32
|
-
});
|
|
26
|
+
async function installShim(path, module) {
|
|
27
|
+
const main = fileURLToPath(new URL(`./${module}${extname(fileURLToPath(import.meta.url))}`, import.meta.url));
|
|
28
|
+
await mkdir(dirname(path), { recursive: true });
|
|
29
|
+
await writeFile(path, `#!/bin/sh\nexec ${quoted(process.execPath)} ${quoted(main)} "$@"\n`, { mode: 0o755 });
|
|
33
30
|
}
|
|
34
31
|
async function serveOwner(copies) {
|
|
35
32
|
const socketPath = join(kitHome(), 'owner.sock');
|
|
@@ -81,7 +78,8 @@ export async function resident() {
|
|
|
81
78
|
const agents = new Agents(house);
|
|
82
79
|
const copies = new LocalCopies(house);
|
|
83
80
|
await copies.load();
|
|
84
|
-
await installShim();
|
|
81
|
+
await installShim(join(kitHome(), 'shim', 'git'), 'git-main');
|
|
82
|
+
await installShim(codexLauncher(), 'codex-main');
|
|
85
83
|
await serveOwner(copies);
|
|
86
84
|
let stream = null;
|
|
87
85
|
let reported = false;
|
|
@@ -120,6 +118,8 @@ export async function resident() {
|
|
|
120
118
|
logged(copies.select());
|
|
121
119
|
},
|
|
122
120
|
frame: (arrived) => {
|
|
121
|
+
if (arrived.type === 'watching')
|
|
122
|
+
conversations.watching(arrived.watching === true);
|
|
123
123
|
if (arrived.type === 'work_available' && arrived.subject === 'agents')
|
|
124
124
|
logged(agents.refresh());
|
|
125
125
|
if (arrived.type === 'work_available' && arrived.subject === 'local_copy')
|
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 { retryDelay } from './api.js';
|
|
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,7 +10,10 @@ export async function holdSecretInput(house, subject, steps, waiting) {
|
|
|
10
10
|
failures = 0;
|
|
11
11
|
}
|
|
12
12
|
catch (error) {
|
|
13
|
+
if (finalRefusal(error))
|
|
14
|
+
return null;
|
|
13
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.
|
|
14
17
|
await new Promise((resolve) => setTimeout(resolve, retryDelay(failures++)));
|
|
15
18
|
}
|
|
16
19
|
}
|
package/dist/shell.js
ADDED