@agentshouse/kit 0.2.1-ce145e9aadbf6 → 0.2.1-ce46b2b55463f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -49
- package/dist/acp.js +101 -108
- package/dist/afk.js +17 -0
- package/dist/agents.js +252 -73
- package/dist/api.js +43 -6
- package/dist/app-git.js +48 -0
- package/dist/binaries.js +79 -0
- package/dist/bridge.js +62 -79
- package/dist/building.js +50 -0
- package/dist/calls.js +88 -0
- package/dist/chunks.js +49 -0
- package/dist/clis.js +171 -102
- package/dist/codex-main.js +58 -0
- package/dist/connect.js +108 -0
- package/dist/connection.js +120 -0
- package/dist/content.js +106 -0
- package/dist/conversations.js +816 -335
- package/dist/copies.js +842 -309
- package/dist/earlier.js +263 -0
- package/dist/end.js +26 -0
- package/dist/events.js +793 -0
- package/dist/files.js +206 -18
- package/dist/git-main.js +24 -29
- package/dist/git.js +95 -18
- package/dist/grok.js +39 -0
- package/dist/home.js +134 -9
- package/dist/house-cli.js +282 -0
- package/dist/house-main.js +7 -159
- package/dist/instructions.js +7 -11
- package/dist/keys.js +238 -0
- package/dist/kit-cli.js +78 -0
- package/dist/kit-main.js +8 -19
- package/dist/login.js +133 -104
- package/dist/logout.js +15 -14
- package/dist/mcp.js +42 -3
- package/dist/owner.js +46 -0
- package/dist/placement.js +25 -0
- package/dist/refusals.js +43 -15
- package/dist/relay.js +33 -2
- package/dist/resident.js +161 -61
- package/dist/scope.js +214 -0
- package/dist/scripts.js +303 -0
- package/dist/secret-input.js +4 -3
- package/dist/settings.js +188 -0
- package/dist/shell.js +3 -0
- package/dist/sign-in.js +85 -33
- package/dist/silence.js +28 -0
- package/dist/skills.js +49 -35
- package/dist/supervisor.js +163 -0
- package/dist/transcription.js +131 -40
- package/dist/translate.js +19 -16
- package/dist/update.js +119 -100
- package/dist/wire.js +32 -0
- package/dist/work.js +353 -0
- package/dist/workspace.js +103 -0
- package/package.json +26 -21
- package/dist/authority-main.js +0 -22
- package/dist/blocks.js +0 -51
- package/dist/bootstrap.json +0 -24
- package/dist/clis-main.js +0 -23
- package/dist/configure-main.js +0 -6
- package/dist/enrol-main.js +0 -4
- package/dist/stream.js +0 -47
- package/how-we-work.md +0 -23
- package/skills/ask-matt/LICENSE +0 -21
- package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
- package/skills/ask-matt/SKILL.md +0 -91
- package/skills/ask-matt/agents/openai.yaml +0 -4
- package/skills/code-review/LICENSE +0 -21
- package/skills/code-review/SKILL.md +0 -89
- package/skills/code-review/agents/openai.yaml +0 -3
- package/skills/codebase-design/DEEPENING.md +0 -37
- package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
- package/skills/codebase-design/LICENSE +0 -21
- package/skills/codebase-design/SKILL.md +0 -116
- package/skills/codebase-design/agents/openai.yaml +0 -3
- package/skills/diagnosing-bugs/LICENSE +0 -21
- package/skills/diagnosing-bugs/SKILL.md +0 -140
- package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
- package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
- package/skills/domain-modeling/ADR-FORMAT.md +0 -47
- package/skills/domain-modeling/CONTEXT-FORMAT.md +0 -60
- package/skills/domain-modeling/LICENSE +0 -21
- package/skills/domain-modeling/SKILL.md +0 -76
- package/skills/domain-modeling/agents/openai.yaml +0 -3
- package/skills/grill-me/LICENSE +0 -21
- package/skills/grill-me/SKILL.md +0 -8
- package/skills/grill-me/agents/openai.yaml +0 -4
- package/skills/grill-with-docs/LICENSE +0 -21
- package/skills/grill-with-docs/SKILL.md +0 -8
- package/skills/grill-with-docs/agents/openai.yaml +0 -4
- package/skills/grilling/LICENSE +0 -21
- package/skills/grilling/SKILL.md +0 -30
- package/skills/grilling/agents/openai.yaml +0 -3
- package/skills/handoff/LICENSE +0 -21
- package/skills/handoff/SKILL.md +0 -17
- package/skills/handoff/agents/openai.yaml +0 -4
- package/skills/implement/LICENSE +0 -21
- package/skills/implement/SKILL.md +0 -16
- package/skills/implement/agents/openai.yaml +0 -4
- package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
- package/skills/improve-codebase-architecture/LICENSE +0 -21
- package/skills/improve-codebase-architecture/SKILL.md +0 -72
- package/skills/improve-codebase-architecture/agents/openai.yaml +0 -4
- package/skills/prototype/LICENSE +0 -21
- package/skills/prototype/LOGIC.md +0 -67
- package/skills/prototype/SKILL.md +0 -28
- package/skills/prototype/UI.md +0 -112
- package/skills/prototype/agents/openai.yaml +0 -3
- package/skills/research/LICENSE +0 -21
- package/skills/research/SKILL.md +0 -14
- package/skills/research/agents/openai.yaml +0 -3
- package/skills/resolving-merge-conflicts/LICENSE +0 -21
- package/skills/resolving-merge-conflicts/SKILL.md +0 -16
- package/skills/resolving-merge-conflicts/agents/openai.yaml +0 -3
- package/skills/tdd/LICENSE +0 -21
- package/skills/tdd/SKILL.md +0 -40
- package/skills/tdd/agents/openai.yaml +0 -3
- package/skills/tdd/mocking.md +0 -59
- package/skills/tdd/tests.md +0 -77
- package/skills/teach/GLOSSARY-FORMAT.md +0 -35
- package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
- package/skills/teach/LICENSE +0 -21
- package/skills/teach/MISSION-FORMAT.md +0 -31
- package/skills/teach/RESOURCES-FORMAT.md +0 -32
- package/skills/teach/SKILL.md +0 -141
- package/skills/teach/agents/openai.yaml +0 -4
- package/skills/to-questionnaire/LICENSE +0 -21
- package/skills/to-questionnaire/SKILL.md +0 -55
- package/skills/to-questionnaire/agents/openai.yaml +0 -4
- package/skills/to-spec/LICENSE +0 -21
- package/skills/to-spec/SKILL.md +0 -76
- package/skills/to-spec/agents/openai.yaml +0 -4
- package/skills/to-tickets/LICENSE +0 -21
- package/skills/to-tickets/SKILL.md +0 -106
- package/skills/to-tickets/agents/openai.yaml +0 -4
- package/skills/wait-what/LICENSE +0 -21
- package/skills/wait-what/SKILL.md +0 -8
- package/skills/wait-what/agents/openai.yaml +0 -4
- package/skills/wayfinder/LICENSE +0 -21
- package/skills/wayfinder/SKILL.md +0 -129
- package/skills/wayfinder/agents/openai.yaml +0 -4
- package/skills/writing-for-agents/LICENSE +0 -21
- package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
- package/skills/writing-for-agents/SKILL.md +0 -83
- package/skills/writing-for-agents/agents/openai.yaml +0 -3
package/dist/scope.js
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { mkdirSync, readdirSync, readFileSync, realpathSync, 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
|
+
const EXITED = new Set(['Z', 'X']);
|
|
8
|
+
function failure(error) {
|
|
9
|
+
return error.code ?? '';
|
|
10
|
+
}
|
|
11
|
+
function listed(environment) {
|
|
12
|
+
const found = new Map();
|
|
13
|
+
const output = execFileSync('ps', [environment ? '-AEww' : '-Aww', '-o', 'pid=,ppid=,sess=,command='], {
|
|
14
|
+
encoding: 'utf8',
|
|
15
|
+
maxBuffer: LISTING_BYTES,
|
|
16
|
+
});
|
|
17
|
+
for (const line of output.split('\n')) {
|
|
18
|
+
const fields = /^\s*(\d+)\s+(\d+)\s+(\S+)\s?(.*)$/.exec(line);
|
|
19
|
+
if (fields !== null)
|
|
20
|
+
found.set(Number(fields[1]), { parent: Number(fields[2]), session: fields[3], command: fields[4] });
|
|
21
|
+
}
|
|
22
|
+
return found;
|
|
23
|
+
}
|
|
24
|
+
function read(pid) {
|
|
25
|
+
try {
|
|
26
|
+
const stat = readFileSync(`/proc/${pid}/stat`, 'utf8');
|
|
27
|
+
const fields = stat.slice(stat.lastIndexOf(')') + 2).split(' ');
|
|
28
|
+
if (EXITED.has(fields[0]))
|
|
29
|
+
return null;
|
|
30
|
+
const command = readFileSync(`/proc/${pid}/cmdline`, 'utf8').replaceAll('\0', ' ');
|
|
31
|
+
return { parent: Number(fields[1]), session: fields[3], command };
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
if (GONE.has(failure(error)))
|
|
35
|
+
return null;
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function processes() {
|
|
40
|
+
if (process.platform === 'darwin')
|
|
41
|
+
return listed(false);
|
|
42
|
+
const found = new Map();
|
|
43
|
+
for (const entry of readdirSync('/proc')) {
|
|
44
|
+
const running = /^\d+$/.test(entry) ? read(entry) : null;
|
|
45
|
+
if (running !== null)
|
|
46
|
+
found.set(Number(entry), running);
|
|
47
|
+
}
|
|
48
|
+
return found;
|
|
49
|
+
}
|
|
50
|
+
function descendants(roots, table = processes()) {
|
|
51
|
+
const children = new Map();
|
|
52
|
+
for (const [pid, { parent }] of table)
|
|
53
|
+
children.set(parent, [...(children.get(parent) ?? []), pid]);
|
|
54
|
+
const tree = [];
|
|
55
|
+
const pending = roots.flatMap((root) => children.get(root) ?? []);
|
|
56
|
+
while (pending.length > 0) {
|
|
57
|
+
const pid = pending.pop();
|
|
58
|
+
tree.push(pid);
|
|
59
|
+
pending.push(...(children.get(pid) ?? []));
|
|
60
|
+
}
|
|
61
|
+
return tree;
|
|
62
|
+
}
|
|
63
|
+
function marked(marker, doubted) {
|
|
64
|
+
if (process.platform === 'darwin') {
|
|
65
|
+
return [...listed(true)].filter(([, { command }]) => ` ${command} `.includes(` ${marker} `)).map(([pid]) => pid);
|
|
66
|
+
}
|
|
67
|
+
return readdirSync('/proc')
|
|
68
|
+
.filter((entry) => /^\d+$/.test(entry) && carries(entry, marker, doubted))
|
|
69
|
+
.map(Number);
|
|
70
|
+
}
|
|
71
|
+
function carries(pid, marker, doubted) {
|
|
72
|
+
try {
|
|
73
|
+
return readFileSync(`/proc/${pid}/environ`, 'utf8').split('\0').includes(marker);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
if (GONE.has(failure(error)))
|
|
77
|
+
return false;
|
|
78
|
+
if (failure(error) !== 'EACCES')
|
|
79
|
+
throw error;
|
|
80
|
+
return doubted && statSync(`/proc/${pid}`, { throwIfNoEntry: false })?.uid === process.getuid();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function killAll(pids) {
|
|
84
|
+
for (const pid of pids) {
|
|
85
|
+
for (const target of [-pid, pid]) {
|
|
86
|
+
try {
|
|
87
|
+
process.kill(target, 'SIGKILL');
|
|
88
|
+
}
|
|
89
|
+
catch { }
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
export function killTree(child) {
|
|
94
|
+
killAll([child.pid, ...descendants([child.pid])]);
|
|
95
|
+
}
|
|
96
|
+
export function killMarked(marker) {
|
|
97
|
+
const pids = marked(marker, false);
|
|
98
|
+
killAll([...pids, ...descendants(pids)]);
|
|
99
|
+
}
|
|
100
|
+
export function killDescendants() {
|
|
101
|
+
const pids = marked(`HOUSE_KIT_RESIDENT=${process.pid}`, false);
|
|
102
|
+
killAll([...pids, ...descendants([process.pid, ...pids])]);
|
|
103
|
+
}
|
|
104
|
+
function ownGroup() {
|
|
105
|
+
if (process.platform !== 'linux')
|
|
106
|
+
return null;
|
|
107
|
+
try {
|
|
108
|
+
const line = readFileSync('/proc/self/cgroup', 'utf8')
|
|
109
|
+
.split('\n')
|
|
110
|
+
.find((entry) => entry.startsWith('0::'));
|
|
111
|
+
return line === undefined ? null : join(CGROUPS, line.slice(3));
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function members(group) {
|
|
118
|
+
const pids = readFileSync(join(group, 'cgroup.procs'), 'utf8').split('\n').filter(Boolean).map(Number);
|
|
119
|
+
for (const entry of readdirSync(group, { withFileTypes: true })) {
|
|
120
|
+
if (entry.isDirectory())
|
|
121
|
+
pids.push(...members(join(group, entry.name)));
|
|
122
|
+
}
|
|
123
|
+
return pids;
|
|
124
|
+
}
|
|
125
|
+
function besideCli(cli) {
|
|
126
|
+
try {
|
|
127
|
+
return `${realpathSync(cli)}-`;
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
export class Scope {
|
|
134
|
+
marker;
|
|
135
|
+
helpers;
|
|
136
|
+
group;
|
|
137
|
+
session = null;
|
|
138
|
+
recorded = null;
|
|
139
|
+
constructor(marker, name, cli) {
|
|
140
|
+
this.marker = marker;
|
|
141
|
+
this.helpers = besideCli(cli);
|
|
142
|
+
const own = ownGroup();
|
|
143
|
+
this.group = own === null ? null : join(own, name);
|
|
144
|
+
try {
|
|
145
|
+
if (this.group !== null)
|
|
146
|
+
mkdirSync(this.group);
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
this.group = null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
command(command, args) {
|
|
153
|
+
if (this.group === null)
|
|
154
|
+
return [command, args];
|
|
155
|
+
return ['/bin/sh', ['-c', 'echo $$ > "$0/cgroup.procs" 2>/dev/null; exec "$@"', this.group, command, ...args]];
|
|
156
|
+
}
|
|
157
|
+
entered(pid) {
|
|
158
|
+
this.session = process.platform === 'darwin' ? (listed(false).get(pid)?.session ?? null) : String(pid);
|
|
159
|
+
if (this.group === null)
|
|
160
|
+
return;
|
|
161
|
+
const joined = readFileSync(`/proc/${pid}/cgroup`, 'utf8').split('\n').find((entry) => entry.startsWith('0::'));
|
|
162
|
+
if (joined !== undefined && join(CGROUPS, joined.slice(3)) === this.group)
|
|
163
|
+
return;
|
|
164
|
+
this.close();
|
|
165
|
+
this.group = null;
|
|
166
|
+
}
|
|
167
|
+
record() {
|
|
168
|
+
const found = this.found();
|
|
169
|
+
if (found === null)
|
|
170
|
+
return;
|
|
171
|
+
this.recorded = new Set([...found].filter(([, { session }]) => session === this.session).map(([pid]) => pid));
|
|
172
|
+
}
|
|
173
|
+
working() {
|
|
174
|
+
const found = this.found();
|
|
175
|
+
if (found === null)
|
|
176
|
+
return true;
|
|
177
|
+
if (this.recorded === null)
|
|
178
|
+
return found.size > 0;
|
|
179
|
+
return [...found].some(([pid, { session, command }]) => session !== this.session || !(this.recorded.has(pid) || (this.helpers !== null && command.startsWith(this.helpers))));
|
|
180
|
+
}
|
|
181
|
+
empty() {
|
|
182
|
+
return this.found()?.size === 0;
|
|
183
|
+
}
|
|
184
|
+
kill() {
|
|
185
|
+
killMarked(this.marker);
|
|
186
|
+
if (this.group === null)
|
|
187
|
+
return;
|
|
188
|
+
try {
|
|
189
|
+
writeFileSync(join(this.group, 'cgroup.kill'), '1');
|
|
190
|
+
}
|
|
191
|
+
catch {
|
|
192
|
+
killAll([...(this.found()?.keys() ?? [])]);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
close() {
|
|
196
|
+
if (this.group === null)
|
|
197
|
+
return;
|
|
198
|
+
try {
|
|
199
|
+
rmdirSync(this.group);
|
|
200
|
+
}
|
|
201
|
+
catch { }
|
|
202
|
+
}
|
|
203
|
+
found() {
|
|
204
|
+
try {
|
|
205
|
+
const table = processes();
|
|
206
|
+
const roots = this.group === null ? marked(this.marker, true) : members(this.group);
|
|
207
|
+
const pids = this.group === null ? [...roots, ...descendants(roots, table)] : roots;
|
|
208
|
+
return new Map(pids.flatMap((pid) => (table.has(pid) ? [[pid, table.get(pid)]] : [])));
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
package/dist/scripts.js
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { spawn as spawnPiped } from 'node:child_process';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { once } from 'node:events';
|
|
4
|
+
import { rmSync } from 'node:fs';
|
|
5
|
+
import { mkdir, rm, writeFile } from 'node:fs/promises';
|
|
6
|
+
import { constants } from 'node:os';
|
|
7
|
+
import { join } from 'node:path';
|
|
8
|
+
import { loginPath, onLoginPath } from './clis.js';
|
|
9
|
+
import { kitHome } from './home.js';
|
|
10
|
+
import { childEnvironment } from './relay.js';
|
|
11
|
+
import { killTree } from './scope.js';
|
|
12
|
+
const TERMINAL = 'xterm-256color';
|
|
13
|
+
const FIRST_SIZE = { cols: 80, rows: 24 };
|
|
14
|
+
const SIGNALLED = 128;
|
|
15
|
+
const NEWLINE = 0x0a;
|
|
16
|
+
const NO_BASH = "bash is not on the login shell's PATH";
|
|
17
|
+
const LOST = { refused: 'the Kit lost its connection to House' };
|
|
18
|
+
const UNSENT_CHARS = 64_000;
|
|
19
|
+
// House holds a Web Wizard's run two minutes for its Kit to connect again, so the script waits as long for a dropped connection to come back.
|
|
20
|
+
const RETURN_MS = 120_000;
|
|
21
|
+
// House takes a run on again with its page's terminal size in the frames right after the Kit connects, so a script it sent nothing for in ten seconds is one House no longer carries.
|
|
22
|
+
const TAKEN_ON_MS = 10_000;
|
|
23
|
+
function causeOf(error) {
|
|
24
|
+
return error instanceof Error ? error.message : String(error);
|
|
25
|
+
}
|
|
26
|
+
function failed(plan, cause) {
|
|
27
|
+
process.stderr.write(`kit: ${plan.named} could not start: ${cause}\n`);
|
|
28
|
+
return { refused: `the script could not start: ${cause}` };
|
|
29
|
+
}
|
|
30
|
+
function planned(call) {
|
|
31
|
+
if (call.kind === 'run_wizard')
|
|
32
|
+
return { named: 'a Web Wizard', script: call.script, terminal: 'page', function: null };
|
|
33
|
+
return {
|
|
34
|
+
named: 'an Environment function',
|
|
35
|
+
script: call.script,
|
|
36
|
+
terminal: call.terminal ? 'at once' : null,
|
|
37
|
+
function: { input: call.input, timeCap: call.time_cap, outputCap: call.output_cap },
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function inputs() {
|
|
41
|
+
return join(kitHome(), 'inputs');
|
|
42
|
+
}
|
|
43
|
+
async function inputFile(value) {
|
|
44
|
+
await mkdir(inputs(), { recursive: true, mode: 0o700 });
|
|
45
|
+
const path = join(inputs(), `${randomUUID()}.json`);
|
|
46
|
+
await writeFile(path, JSON.stringify(value), { mode: 0o600, flag: 'wx' });
|
|
47
|
+
return path;
|
|
48
|
+
}
|
|
49
|
+
export function removeInputs() {
|
|
50
|
+
rmSync(inputs(), { recursive: true, force: true });
|
|
51
|
+
}
|
|
52
|
+
function latest(screen) {
|
|
53
|
+
if (screen.length <= UNSENT_CHARS)
|
|
54
|
+
return screen;
|
|
55
|
+
const line = screen.indexOf('\n', screen.length - UNSENT_CHARS);
|
|
56
|
+
return screen.slice(line < 0 ? screen.length - UNSENT_CHARS : line + 1);
|
|
57
|
+
}
|
|
58
|
+
function struck(line, typed) {
|
|
59
|
+
return typed.some((typedLine) => line.includes(typedLine));
|
|
60
|
+
}
|
|
61
|
+
class KeptOutput {
|
|
62
|
+
cap;
|
|
63
|
+
typedText = '';
|
|
64
|
+
text = '';
|
|
65
|
+
bytes = 0;
|
|
66
|
+
line = '';
|
|
67
|
+
lineBytes = 0;
|
|
68
|
+
dropping = false;
|
|
69
|
+
cut = false;
|
|
70
|
+
constructor(cap) {
|
|
71
|
+
this.cap = cap;
|
|
72
|
+
}
|
|
73
|
+
typed(data) {
|
|
74
|
+
this.typedText += data;
|
|
75
|
+
}
|
|
76
|
+
add(data) {
|
|
77
|
+
const pieces = data.split('\n');
|
|
78
|
+
const rest = pieces.pop();
|
|
79
|
+
for (const piece of pieces)
|
|
80
|
+
this.ended(piece);
|
|
81
|
+
this.extend(rest);
|
|
82
|
+
}
|
|
83
|
+
kept() {
|
|
84
|
+
const typed = this.typedLines(true);
|
|
85
|
+
this.text = [...this.text.split(/(?<=\n)/), this.line].filter((line) => line !== '' && !struck(line, typed)).join('');
|
|
86
|
+
this.bytes = Buffer.byteLength(this.text);
|
|
87
|
+
this.trim();
|
|
88
|
+
return { output: this.text, cut: this.cut };
|
|
89
|
+
}
|
|
90
|
+
typedLines(all) {
|
|
91
|
+
const lines = this.typedText.split(/\r\n|\r|\n/);
|
|
92
|
+
if (!all)
|
|
93
|
+
lines.pop();
|
|
94
|
+
return lines.filter((line) => line !== '');
|
|
95
|
+
}
|
|
96
|
+
ended(piece) {
|
|
97
|
+
const line = `${this.line}${piece}\n`;
|
|
98
|
+
const dropped = this.dropping;
|
|
99
|
+
this.line = '';
|
|
100
|
+
this.lineBytes = 0;
|
|
101
|
+
this.dropping = false;
|
|
102
|
+
if (dropped || struck(line, this.typedLines(false)))
|
|
103
|
+
return;
|
|
104
|
+
this.text += line;
|
|
105
|
+
this.bytes += Buffer.byteLength(line);
|
|
106
|
+
if (this.bytes > 2 * this.cap)
|
|
107
|
+
this.trim();
|
|
108
|
+
}
|
|
109
|
+
extend(piece) {
|
|
110
|
+
if (this.dropping)
|
|
111
|
+
return;
|
|
112
|
+
this.line += piece;
|
|
113
|
+
this.lineBytes += Buffer.byteLength(piece);
|
|
114
|
+
if (this.lineBytes <= this.cap)
|
|
115
|
+
return;
|
|
116
|
+
this.text = '';
|
|
117
|
+
this.bytes = 0;
|
|
118
|
+
this.line = '';
|
|
119
|
+
this.lineBytes = 0;
|
|
120
|
+
this.dropping = true;
|
|
121
|
+
this.cut = true;
|
|
122
|
+
}
|
|
123
|
+
trim() {
|
|
124
|
+
if (this.bytes <= this.cap)
|
|
125
|
+
return;
|
|
126
|
+
const bytes = Buffer.from(this.text);
|
|
127
|
+
const start = bytes.indexOf(NEWLINE, bytes.length - this.cap - 1) + 1;
|
|
128
|
+
this.text = bytes.subarray(start).toString('utf8');
|
|
129
|
+
this.bytes = bytes.length - start;
|
|
130
|
+
this.cut = true;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
export class Scripts {
|
|
134
|
+
calls;
|
|
135
|
+
send;
|
|
136
|
+
runs = new Map();
|
|
137
|
+
constructor(calls, send) {
|
|
138
|
+
this.calls = calls;
|
|
139
|
+
this.send = send;
|
|
140
|
+
}
|
|
141
|
+
start(call) {
|
|
142
|
+
void this.calls.carry(call.input_id, () => this.run(call.input_id, planned(call)));
|
|
143
|
+
}
|
|
144
|
+
notified(oneWayCall) {
|
|
145
|
+
if (!('wizard' in oneWayCall))
|
|
146
|
+
return;
|
|
147
|
+
const run = this.runs.get(oneWayCall.wizard);
|
|
148
|
+
if (run === undefined)
|
|
149
|
+
return;
|
|
150
|
+
this.waited(run, null);
|
|
151
|
+
if (oneWayCall.type === 'wizard_input')
|
|
152
|
+
this.typed(run, oneWayCall.data);
|
|
153
|
+
if (oneWayCall.type === 'wizard_size')
|
|
154
|
+
this.resized(run, { cols: oneWayCall.cols, rows: oneWayCall.rows });
|
|
155
|
+
if (oneWayCall.type === 'wizard_end' && run.plan.terminal === 'page')
|
|
156
|
+
this.end(run, {});
|
|
157
|
+
}
|
|
158
|
+
disconnected() {
|
|
159
|
+
for (const run of this.runs.values()) {
|
|
160
|
+
if (run.plan.terminal === 'page')
|
|
161
|
+
this.waited(run, RETURN_MS);
|
|
162
|
+
else
|
|
163
|
+
this.end(run, LOST);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
connected() {
|
|
167
|
+
for (const [id, run] of this.runs) {
|
|
168
|
+
if (run.waiting === null)
|
|
169
|
+
continue;
|
|
170
|
+
this.waited(run, TAKEN_ON_MS);
|
|
171
|
+
if (run.unsent !== '' && this.send({ type: 'wizard_output', wizard: id, data: run.unsent }))
|
|
172
|
+
run.unsent = '';
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
async run(id, plan) {
|
|
176
|
+
const size = plan.terminal === 'page' ? null : FIRST_SIZE;
|
|
177
|
+
const output = plan.function === null ? null : new KeptOutput(plan.function.outputCap);
|
|
178
|
+
const run = {
|
|
179
|
+
plan,
|
|
180
|
+
input: null,
|
|
181
|
+
output,
|
|
182
|
+
process: null,
|
|
183
|
+
pipes: [],
|
|
184
|
+
terminal: null,
|
|
185
|
+
size,
|
|
186
|
+
typed: [],
|
|
187
|
+
unsent: '',
|
|
188
|
+
waiting: null,
|
|
189
|
+
ended: null,
|
|
190
|
+
sized: Promise.withResolvers(),
|
|
191
|
+
};
|
|
192
|
+
if (size !== null)
|
|
193
|
+
run.sized.resolve();
|
|
194
|
+
this.runs.set(id, run);
|
|
195
|
+
try {
|
|
196
|
+
return { result: await this.ran(id, run), release: plan.function !== null };
|
|
197
|
+
}
|
|
198
|
+
finally {
|
|
199
|
+
this.runs.delete(id);
|
|
200
|
+
if (run.input !== null)
|
|
201
|
+
await rm(run.input, { force: true });
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
async ran(id, run) {
|
|
205
|
+
const { plan, output } = run;
|
|
206
|
+
let exited;
|
|
207
|
+
try {
|
|
208
|
+
const { spawn } = await import('@lydell/node-pty');
|
|
209
|
+
const shell = await onLoginPath('bash');
|
|
210
|
+
if (shell === null)
|
|
211
|
+
return failed(plan, NO_BASH);
|
|
212
|
+
const path = await loginPath();
|
|
213
|
+
await run.sized.promise;
|
|
214
|
+
if (plan.function !== null)
|
|
215
|
+
run.input = await inputFile(plan.function.input);
|
|
216
|
+
if (run.ended !== null)
|
|
217
|
+
return run.ended;
|
|
218
|
+
const environment = { ...childEnvironment(), PATH: path, ...(run.input === null ? {} : { HOUSE_INPUT: run.input }) };
|
|
219
|
+
if (plan.terminal === null) {
|
|
220
|
+
({ exited } = await this.piped(plan.script, shell, environment, run));
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
const terminal = spawn(shell, ['-c', plan.script], { name: TERMINAL, ...run.size, cwd: process.cwd(), env: environment });
|
|
224
|
+
run.process = terminal;
|
|
225
|
+
run.terminal = terminal;
|
|
226
|
+
terminal.onData((data) => {
|
|
227
|
+
if (!this.send({ type: 'wizard_output', wizard: id, data }))
|
|
228
|
+
run.unsent = latest(`${run.unsent}${data}`);
|
|
229
|
+
output?.add(data);
|
|
230
|
+
});
|
|
231
|
+
for (const data of run.typed.splice(0))
|
|
232
|
+
terminal.write(data);
|
|
233
|
+
exited = new Promise((resolve) => terminal.onExit(({ exitCode, signal }) => resolve(signal ? SIGNALLED + signal : exitCode)));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
catch (error) {
|
|
237
|
+
return failed(plan, causeOf(error));
|
|
238
|
+
}
|
|
239
|
+
let timedOut = false;
|
|
240
|
+
// House sends a function run's time cap in seconds, and a run still going at its cap ends with its process group and the output pipes it holds.
|
|
241
|
+
const capped = plan.function === null ? undefined : setTimeout(() => {
|
|
242
|
+
timedOut = true;
|
|
243
|
+
this.stop(run);
|
|
244
|
+
}, plan.function.timeCap * 1000);
|
|
245
|
+
const code = await exited;
|
|
246
|
+
clearTimeout(capped);
|
|
247
|
+
if (run.ended !== null)
|
|
248
|
+
return run.ended;
|
|
249
|
+
if (output === null)
|
|
250
|
+
return { exit_code: code };
|
|
251
|
+
const kept = output.kept();
|
|
252
|
+
return timedOut ? { timed_out: true, ...kept } : { exit_code: code, ...kept };
|
|
253
|
+
}
|
|
254
|
+
async piped(script, shell, environment, run) {
|
|
255
|
+
const child = spawnPiped(shell, ['-c', script], { cwd: process.cwd(), env: environment, detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
256
|
+
const exited = new Promise((resolve) => child.once('close', (code, signal) => resolve(signal === null ? code : SIGNALLED + constants.signals[signal])));
|
|
257
|
+
await once(child, 'spawn');
|
|
258
|
+
run.process = { pid: child.pid };
|
|
259
|
+
run.pipes = [child.stdout, child.stderr];
|
|
260
|
+
for (const pipe of run.pipes) {
|
|
261
|
+
pipe.setEncoding('utf8');
|
|
262
|
+
pipe.on('data', (data) => run.output.add(data));
|
|
263
|
+
}
|
|
264
|
+
if (run.ended !== null)
|
|
265
|
+
this.stop(run);
|
|
266
|
+
return { exited };
|
|
267
|
+
}
|
|
268
|
+
typed(run, data) {
|
|
269
|
+
run.output?.typed(data);
|
|
270
|
+
if (run.terminal === null)
|
|
271
|
+
run.typed.push(data);
|
|
272
|
+
else
|
|
273
|
+
run.terminal.write(data);
|
|
274
|
+
}
|
|
275
|
+
resized(run, size) {
|
|
276
|
+
run.size = size;
|
|
277
|
+
run.sized.resolve();
|
|
278
|
+
try {
|
|
279
|
+
run.terminal?.resize(size.cols, size.rows);
|
|
280
|
+
}
|
|
281
|
+
catch { }
|
|
282
|
+
}
|
|
283
|
+
end(run, result) {
|
|
284
|
+
if (run.ended !== null)
|
|
285
|
+
return;
|
|
286
|
+
this.waited(run, null);
|
|
287
|
+
run.ended = result;
|
|
288
|
+
run.sized.resolve();
|
|
289
|
+
this.stop(run);
|
|
290
|
+
}
|
|
291
|
+
waited(run, ms) {
|
|
292
|
+
if (run.waiting !== null)
|
|
293
|
+
clearTimeout(run.waiting);
|
|
294
|
+
run.waiting = ms === null ? null : setTimeout(() => this.end(run, LOST), ms);
|
|
295
|
+
run.waiting?.unref();
|
|
296
|
+
}
|
|
297
|
+
stop(run) {
|
|
298
|
+
if (run.process !== null)
|
|
299
|
+
killTree(run.process);
|
|
300
|
+
for (const pipe of run.pipes)
|
|
301
|
+
pipe.destroy();
|
|
302
|
+
}
|
|
303
|
+
}
|
package/dist/secret-input.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
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 {
|
|
5
|
-
const held = await house.
|
|
5
|
+
const held = await house.act('secret-inputs/await', { subject, steps });
|
|
6
6
|
if (held.outcome === 'collected')
|
|
7
7
|
return held.content;
|
|
8
8
|
if (held.release !== 'held')
|
|
@@ -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
|
}
|