@agentshouse/kit 0.2.1-c4ea88ba9424e → 0.2.1-c5d36e97b0fea
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 -12
- package/dist/acp.js +13 -93
- package/dist/agents.js +2 -1
- package/dist/api.js +3 -3
- package/dist/bootstrap.json +4 -4
- package/dist/bridge.js +11 -37
- package/dist/clis.js +56 -57
- package/dist/codex-main.js +58 -0
- package/dist/conversations.js +279 -235
- package/dist/copies.js +121 -96
- package/dist/end.js +1 -1
- package/dist/events.js +521 -0
- package/dist/files.js +14 -0
- package/dist/house-main.js +41 -12
- package/dist/instructions.js +3 -2
- package/dist/login.js +22 -101
- package/dist/logout.js +14 -14
- package/dist/parts.js +64 -0
- package/dist/resident.js +22 -18
- package/dist/scope.js +203 -0
- package/dist/sign-in.js +1 -1
- package/dist/work.js +348 -0
- package/package.json +1 -1
- package/dist/blocks.js +0 -51
package/dist/login.js
CHANGED
|
@@ -1,54 +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';
|
|
7
6
|
import { placeAppGit } from './app-git.js';
|
|
8
7
|
import { forgetOwnAgent, readDisconnected, readEnrolment, readOwnAgent, writeEnrolment, writeOwnAgent } from './home.js';
|
|
9
8
|
import { callHouse, rpc } from './mcp.js';
|
|
10
9
|
const DEFAULT_HOUSE = 'https://agents.house';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const server = createServer((request, response) => {
|
|
19
|
-
const received = new URL(request.url ?? '/', 'http://127.0.0.1').searchParams.get('code');
|
|
20
|
-
if (received === null || held !== null) {
|
|
21
|
-
response.writeHead(404).end();
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
held = response;
|
|
25
|
-
deliver(received);
|
|
26
|
-
});
|
|
27
|
-
const close = () => {
|
|
28
|
-
server.close();
|
|
29
|
-
server.closeAllConnections();
|
|
30
|
-
};
|
|
31
|
-
const port = Number(process.env.HOUSE_KIT_LOGIN_PORT ?? 0);
|
|
32
|
-
server.once('error', reject);
|
|
33
|
-
server.listen(port, port === 0 ? '127.0.0.1' : '0.0.0.0', () => {
|
|
34
|
-
const address = server.address();
|
|
35
|
-
if (address === null || typeof address === 'string') {
|
|
36
|
-
reject(new Error('the login callback has no port'));
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
resolve({
|
|
40
|
-
uri: `http://127.0.0.1:${address.port}/callback`,
|
|
41
|
-
code,
|
|
42
|
-
answer: (text) => new Promise((written) => {
|
|
43
|
-
if (held === null || held.destroyed || !held.socket || held.socket.destroyed)
|
|
44
|
-
return written();
|
|
45
|
-
held.once('close', () => written());
|
|
46
|
-
held.writeHead(200, { 'content-type': 'text/plain; charset=utf-8', connection: 'close' }).end(`${text}\n`);
|
|
47
|
-
}),
|
|
48
|
-
close,
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
});
|
|
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();
|
|
52
17
|
}
|
|
53
18
|
async function accepted(house, credential) {
|
|
54
19
|
const checked = await callHouse(house, credential, rpc('tools/list', {}));
|
|
@@ -58,25 +23,12 @@ async function accepted(house, credential) {
|
|
|
58
23
|
return false;
|
|
59
24
|
throw new Error(`House answered ${checked.status} when Kit checked this computer's own agent's connection: ${checked.text}`);
|
|
60
25
|
}
|
|
61
|
-
async function typed(question) {
|
|
62
|
-
const reader = createInterface({ input: process.stdin, output: process.stdout });
|
|
63
|
-
const closed = new Promise((_, reject) => {
|
|
64
|
-
reader.once('close', () => reject(new Error('no code was typed')));
|
|
65
|
-
});
|
|
66
|
-
try {
|
|
67
|
-
return (await Promise.race([reader.question(question), closed])).trim();
|
|
68
|
-
}
|
|
69
|
-
finally {
|
|
70
|
-
reader.close();
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
26
|
export async function login(argv) {
|
|
74
27
|
const { values } = parseArgs({
|
|
75
28
|
args: argv,
|
|
76
29
|
options: {
|
|
77
30
|
house: { type: 'string' },
|
|
78
31
|
environment: { type: 'string' },
|
|
79
|
-
manual: { type: 'boolean', default: false },
|
|
80
32
|
},
|
|
81
33
|
});
|
|
82
34
|
const enrolled = (await readEnrolment()) ?? (await readDisconnected());
|
|
@@ -86,55 +38,24 @@ export async function login(argv) {
|
|
|
86
38
|
const ownAgent = await readOwnAgent();
|
|
87
39
|
const held = ownAgent !== null && ownAgent.house === house && (await accepted(house, ownAgent.credential));
|
|
88
40
|
const verifier = randomBytes(32).toString('base64url');
|
|
89
|
-
const started = {
|
|
41
|
+
const started = await retrying(() => post(house, '/kit', {
|
|
90
42
|
...(environment === undefined
|
|
91
43
|
? { act: 'new_environment', label: hostname(), ...(replaces === undefined ? {} : { replaces }) }
|
|
92
44
|
: { act: 'existing_environment', environment_id: environment }),
|
|
93
45
|
code_challenge: createHash('sha256').update(verifier).digest('base64url'),
|
|
94
46
|
...(held ? {} : { headless: true }),
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
else if (held && ownAgent.user !== exchanged.user) {
|
|
104
|
-
await forgetOwnAgent();
|
|
105
|
-
process.stdout.write("This computer's own agent was another User's; run kit login again to connect it to you.\n");
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
const placeGit = async () => placeAppGit(ownAgent, await readOwnAgent());
|
|
109
|
-
if (values.manual) {
|
|
110
|
-
const { start_url } = await post(house, '/kit', {
|
|
111
|
-
...started,
|
|
112
|
-
presentation: 'manual',
|
|
113
|
-
});
|
|
114
|
-
process.stdout.write(`Open this link and confirm: ${start_url}\n`);
|
|
115
|
-
await exchange(await typed('Code: '));
|
|
116
|
-
await placeGit();
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
const callback = await listen();
|
|
120
|
-
try {
|
|
121
|
-
const { start_url } = await post(house, '/kit', {
|
|
122
|
-
...started,
|
|
123
|
-
presentation: 'loopback',
|
|
124
|
-
loopback_uri: callback.uri,
|
|
125
|
-
});
|
|
126
|
-
process.stdout.write(`Open this link and confirm: ${start_url}\n`);
|
|
127
|
-
try {
|
|
128
|
-
await exchange(await callback.code);
|
|
129
|
-
}
|
|
130
|
-
catch (error) {
|
|
131
|
-
await callback.answer(`This Environment was not connected: ${error.message}`);
|
|
132
|
-
throw error;
|
|
133
|
-
}
|
|
134
|
-
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 });
|
|
135
55
|
}
|
|
136
|
-
|
|
137
|
-
|
|
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");
|
|
138
59
|
}
|
|
139
|
-
await
|
|
60
|
+
await placeAppGit(ownAgent, await readOwnAgent());
|
|
140
61
|
}
|
package/dist/logout.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import { HouseRefusal, post } from './api.js';
|
|
2
2
|
import { forgetAppGit } from './app-git.js';
|
|
3
|
-
import { forgetEnrolment, readEnrolment } from './home.js';
|
|
3
|
+
import { forgetEnrolment, forgetOwnAgent, readEnrolment } from './home.js';
|
|
4
4
|
export async function logout() {
|
|
5
5
|
const enrolled = await readEnrolment();
|
|
6
|
-
if (enrolled
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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);
|
|
10
15
|
}
|
|
11
|
-
try {
|
|
12
|
-
await post(enrolled.house, '/kit/logout', {}, enrolled.credential);
|
|
13
|
-
}
|
|
14
|
-
catch (error) {
|
|
15
|
-
if (!(error instanceof HouseRefusal && error.status === 401))
|
|
16
|
-
throw error;
|
|
17
|
-
}
|
|
18
|
-
await forgetEnrolment(enrolled);
|
|
19
16
|
await forgetAppGit();
|
|
20
|
-
|
|
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`);
|
|
21
21
|
}
|
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/resident.js
CHANGED
|
@@ -2,45 +2,51 @@ 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
14
|
import { quoted } from './shell.js';
|
|
15
15
|
import { SignIns } from './sign-in.js';
|
|
16
16
|
import { placeSkillSet } from './skills.js';
|
|
17
17
|
import { holdStream } from './stream.js';
|
|
18
18
|
import { Updates } from './update.js';
|
|
19
|
+
import { codexLauncher } from './work.js';
|
|
19
20
|
function report(error) {
|
|
20
21
|
process.stderr.write(`kit: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
21
22
|
}
|
|
22
23
|
function logged(work) {
|
|
23
24
|
work.catch(report);
|
|
24
25
|
}
|
|
25
|
-
async function installShim() {
|
|
26
|
-
const main = fileURLToPath(new URL(
|
|
27
|
-
await mkdir(
|
|
28
|
-
await writeFile(
|
|
29
|
-
mode: 0o755,
|
|
30
|
-
});
|
|
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 });
|
|
31
30
|
}
|
|
32
31
|
async function serveOwner(copies) {
|
|
33
32
|
const socketPath = join(kitHome(), 'owner.sock');
|
|
34
33
|
await rm(socketPath, { force: true });
|
|
35
34
|
const server = createServer(async (request, response) => {
|
|
36
35
|
try {
|
|
37
|
-
|
|
36
|
+
const text = await received(request);
|
|
37
|
+
let answered;
|
|
38
|
+
if (request.url === '/git/fetch') {
|
|
39
|
+
answered = await copies.fetch();
|
|
40
|
+
}
|
|
41
|
+
else if (request.url === '/git/push') {
|
|
42
|
+
const { cwd, commit } = JSON.parse(text);
|
|
43
|
+
answered = await copies.push(cwd, commit, copies.owner);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
38
46
|
response.writeHead(404).end();
|
|
39
47
|
return;
|
|
40
48
|
}
|
|
41
|
-
|
|
42
|
-
const pushed = await copies.push(cwd, commit, copies.owner);
|
|
43
|
-
response.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify(pushed));
|
|
49
|
+
response.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify(answered));
|
|
44
50
|
}
|
|
45
51
|
catch (error) {
|
|
46
52
|
response.writeHead(502).end(error instanceof Error ? error.message : String(error));
|
|
@@ -79,7 +85,8 @@ export async function resident() {
|
|
|
79
85
|
const agents = new Agents(house);
|
|
80
86
|
const copies = new LocalCopies(house);
|
|
81
87
|
await copies.load();
|
|
82
|
-
await installShim();
|
|
88
|
+
await installShim(join(kitHome(), 'shim', 'git'), 'git-main');
|
|
89
|
+
await installShim(codexLauncher(), 'codex-main');
|
|
83
90
|
await serveOwner(copies);
|
|
84
91
|
let stream = null;
|
|
85
92
|
let reported = false;
|
|
@@ -118,13 +125,10 @@ export async function resident() {
|
|
|
118
125
|
logged(copies.select());
|
|
119
126
|
},
|
|
120
127
|
frame: (arrived) => {
|
|
128
|
+
if (arrived.type === 'watching')
|
|
129
|
+
conversations.watching(arrived.watching === true);
|
|
121
130
|
if (arrived.type === 'work_available' && arrived.subject === 'agents')
|
|
122
131
|
logged(agents.refresh());
|
|
123
|
-
if (arrived.type === 'work_available' && arrived.subject === 'local_copy')
|
|
124
|
-
logged(copies.select());
|
|
125
|
-
if (arrived.type === 'entries') {
|
|
126
|
-
copies.entries(arrived);
|
|
127
|
-
}
|
|
128
132
|
if (arrived.type === 'input') {
|
|
129
133
|
reported = false;
|
|
130
134
|
if (arrived.kind === 'sign_in')
|
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/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+/;
|