@agentshouse/kit 0.2.1-ccb257fa8a318 → 0.2.1-ccffb3f637815
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -31
- package/dist/acp.js +17 -10
- package/dist/afk.js +17 -0
- package/dist/agents.js +121 -52
- package/dist/api.js +11 -6
- package/dist/binaries.js +89 -0
- package/dist/bridge.js +28 -27
- package/dist/building.js +7 -3
- package/dist/calls.js +18 -1
- package/dist/chunks.js +49 -0
- package/dist/clis.js +52 -63
- package/dist/connect.js +108 -0
- package/dist/connection.js +24 -20
- package/dist/content.js +106 -0
- package/dist/conversations.js +255 -134
- package/dist/copies.js +748 -248
- package/dist/earlier.js +263 -0
- package/dist/end.js +9 -6
- package/dist/events.js +549 -281
- package/dist/files.js +77 -11
- package/dist/git-main.js +24 -29
- package/dist/git.js +87 -12
- package/dist/grok.js +39 -0
- package/dist/home.js +80 -29
- package/dist/house-cli.js +106 -123
- package/dist/instructions.js +2 -4
- package/dist/keys.js +238 -0
- package/dist/kit-cli.js +14 -4
- package/dist/login.js +34 -20
- package/dist/logout.js +2 -2
- package/dist/mcp.js +38 -3
- package/dist/old-references.js +27 -0
- package/dist/owner.js +46 -0
- package/dist/placement.js +9 -23
- package/dist/refusals.js +37 -21
- package/dist/resident.js +107 -72
- package/dist/scope.js +12 -4
- package/dist/scripts.js +59 -11
- package/dist/secret-input.js +1 -1
- package/dist/settings.js +188 -0
- package/dist/sign-in.js +49 -4
- package/dist/supervisor.js +163 -0
- package/dist/transcription.js +5 -2
- package/dist/translate.js +18 -11
- package/dist/update.js +115 -160
- package/dist/wire.js +19 -9
- package/dist/work.js +27 -2
- package/dist/workspace.js +103 -0
- package/package.json +13 -8
- package/dist/authority-main.js +0 -22
- package/dist/chatgpt.js +0 -32
- package/dist/clis-main.js +0 -23
- package/dist/configure-main.js +0 -6
- package/dist/enrol-main.js +0 -10
- package/dist/parts.js +0 -82
- package/dist/service-main.js +0 -9
- package/dist/service.js +0 -55
package/dist/sign-in.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
import { createInterface } from 'node:readline';
|
|
3
|
-
import { CLIS } from './clis.js';
|
|
4
|
-
import { childEnvironment } from './relay.js';
|
|
3
|
+
import { CLIS, cliEnvironment } from './clis.js';
|
|
5
4
|
import { killTree } from './scope.js';
|
|
6
5
|
import { holdSecretInput } from './secret-input.js';
|
|
7
6
|
const ESCAPE = /\u001b\[[\d;]*m/g;
|
|
@@ -10,6 +9,33 @@ const CODE = /^[A-Z0-9]+-[A-Z0-9]+$/;
|
|
|
10
9
|
function causeOf(error) {
|
|
11
10
|
return error instanceof Error ? error.message : String(error);
|
|
12
11
|
}
|
|
12
|
+
function keySteps(name) {
|
|
13
|
+
return [
|
|
14
|
+
{ kind: 'collect', label: 'API key', name: 'key' },
|
|
15
|
+
{
|
|
16
|
+
kind: 'collect',
|
|
17
|
+
label: 'Endpoint, for another provider',
|
|
18
|
+
name: 'endpoint',
|
|
19
|
+
description: `Leave Endpoint empty for ${name}’s own. Both stay on this computer.`,
|
|
20
|
+
optional: true,
|
|
21
|
+
},
|
|
22
|
+
];
|
|
23
|
+
}
|
|
24
|
+
function endpointOf(given) {
|
|
25
|
+
const endpoint = given.trim();
|
|
26
|
+
if (endpoint === '')
|
|
27
|
+
return null;
|
|
28
|
+
let protocol;
|
|
29
|
+
try {
|
|
30
|
+
protocol = new URL(endpoint).protocol;
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
protocol = '';
|
|
34
|
+
}
|
|
35
|
+
if (protocol !== 'https:' && protocol !== 'http:')
|
|
36
|
+
throw new Error('the endpoint is no http or https address');
|
|
37
|
+
return endpoint;
|
|
38
|
+
}
|
|
13
39
|
function endOf(signal) {
|
|
14
40
|
return new Promise((resolve) => signal.addEventListener('abort', () => resolve(true), { once: true }));
|
|
15
41
|
}
|
|
@@ -36,15 +62,34 @@ export class SignIns {
|
|
|
36
62
|
return ended ? { result: {} } : { result: {}, returned: () => this.agents.reread(call.cli) };
|
|
37
63
|
}
|
|
38
64
|
async signedIn(call, signal) {
|
|
39
|
-
await this.login(call, signal);
|
|
65
|
+
await (call.api_key === true ? this.keyed(call, signal) : this.login(call, signal));
|
|
40
66
|
if (!signal.aborted)
|
|
41
67
|
await this.agents.recheck(call.cli, signal);
|
|
42
68
|
return false;
|
|
43
69
|
}
|
|
70
|
+
async keyed(call, signal) {
|
|
71
|
+
const { name, key } = CLIS[call.cli];
|
|
72
|
+
let secret = '';
|
|
73
|
+
try {
|
|
74
|
+
const cli = await this.agents.located(call.cli, signal);
|
|
75
|
+
const given = await holdSecretInput(this.house, call.input_id, keySteps(name), () => !signal.aborted);
|
|
76
|
+
if (given === null || signal.aborted)
|
|
77
|
+
return;
|
|
78
|
+
secret = given.key.trim();
|
|
79
|
+
await key.write(secret, endpointOf(given.endpoint), cli, signal);
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
const cause = secret === '' ? causeOf(error) : causeOf(error).replaceAll(secret, 'the key');
|
|
83
|
+
if (!signal.aborted)
|
|
84
|
+
process.stderr.write(`kit: the ${call.cli} sign-in failed: ${cause}\n`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
44
87
|
async login(call, signal) {
|
|
45
88
|
let cli;
|
|
89
|
+
let env;
|
|
46
90
|
try {
|
|
47
91
|
cli = await this.agents.located(call.cli, signal);
|
|
92
|
+
env = await cliEnvironment(call.cli);
|
|
48
93
|
}
|
|
49
94
|
catch (error) {
|
|
50
95
|
if (!signal.aborted)
|
|
@@ -57,7 +102,7 @@ export class SignIns {
|
|
|
57
102
|
const child = spawn(cli, login.args, {
|
|
58
103
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
59
104
|
detached: true,
|
|
60
|
-
env
|
|
105
|
+
env,
|
|
61
106
|
});
|
|
62
107
|
const running = () => child.exitCode === null && child.signalCode === null;
|
|
63
108
|
const stop = () => killTree(child);
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
import { dirname, join } from 'node:path';
|
|
6
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
7
|
+
import { oneFolder } from './home.js';
|
|
8
|
+
const SERVICE = 'house-kit';
|
|
9
|
+
const UNIT = `${SERVICE}.service`;
|
|
10
|
+
// launchctl, loginctl and systemctl answer at once and lingering starts the account's service manager within a second, so ten seconds only bound a stuck one.
|
|
11
|
+
const MANAGER_MS = 10_000;
|
|
12
|
+
// The service manager lingering starts answers within a second, so a check every tenth of a second starts Kit as soon as it does.
|
|
13
|
+
const MANAGER_CHECK_MS = 100;
|
|
14
|
+
// A Kit that failed restarts after five seconds, back soon without spinning on a failure that repeats; a Kit that ended itself stays stopped.
|
|
15
|
+
const RESTART_SECONDS = 5;
|
|
16
|
+
export function userUnit() {
|
|
17
|
+
return join(process.env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'systemd', 'user', UNIT);
|
|
18
|
+
}
|
|
19
|
+
export function launchAgent() {
|
|
20
|
+
return join(homedir(), 'Library', 'LaunchAgents', `${SERVICE}.plist`);
|
|
21
|
+
}
|
|
22
|
+
export function launcher() {
|
|
23
|
+
return join(oneFolder(), 'bin', 'kit');
|
|
24
|
+
}
|
|
25
|
+
export function systemd() {
|
|
26
|
+
return existsSync('/run/systemd/system');
|
|
27
|
+
}
|
|
28
|
+
function userBus() {
|
|
29
|
+
if (process.env.XDG_RUNTIME_DIR)
|
|
30
|
+
return process.env;
|
|
31
|
+
return { ...process.env, XDG_RUNTIME_DIR: `/run/user/${process.getuid()}` };
|
|
32
|
+
}
|
|
33
|
+
export function manage(command, args) {
|
|
34
|
+
return spawnSync(command, args, { encoding: 'utf8', timeout: MANAGER_MS, env: command === 'systemctl' ? userBus() : process.env });
|
|
35
|
+
}
|
|
36
|
+
export function required(command, args, failed) {
|
|
37
|
+
const ran = manage(command, args);
|
|
38
|
+
if (ran.status === 0)
|
|
39
|
+
return;
|
|
40
|
+
const cause = ran.error?.message ?? (ran.stderr.trim() || `${command} exited with ${ran.status ?? 'a signal'}`);
|
|
41
|
+
throw new Error(`${failed}: ${cause}`);
|
|
42
|
+
}
|
|
43
|
+
function unitWord(text) {
|
|
44
|
+
return `"${text.replaceAll('\\', '\\\\').replaceAll('"', '\\"').replaceAll('%', '%%')}"`;
|
|
45
|
+
}
|
|
46
|
+
function xmlText(text) {
|
|
47
|
+
return text.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>');
|
|
48
|
+
}
|
|
49
|
+
function unitText() {
|
|
50
|
+
return `[Unit]
|
|
51
|
+
Description=Kit
|
|
52
|
+
|
|
53
|
+
[Service]
|
|
54
|
+
ExecStart=${unitWord(launcher())} resident
|
|
55
|
+
Restart=on-failure
|
|
56
|
+
RestartSec=${RESTART_SECONDS}
|
|
57
|
+
Delegate=yes
|
|
58
|
+
OOMPolicy=continue
|
|
59
|
+
|
|
60
|
+
[Install]
|
|
61
|
+
WantedBy=default.target
|
|
62
|
+
`;
|
|
63
|
+
}
|
|
64
|
+
function plistText() {
|
|
65
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
66
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
67
|
+
<plist version="1.0">
|
|
68
|
+
<dict>
|
|
69
|
+
\t<key>Label</key>
|
|
70
|
+
\t<string>${SERVICE}</string>
|
|
71
|
+
\t<key>ProgramArguments</key>
|
|
72
|
+
\t<array>
|
|
73
|
+
\t\t<string>${xmlText(launcher())}</string>
|
|
74
|
+
\t\t<string>resident</string>
|
|
75
|
+
\t</array>
|
|
76
|
+
\t<key>RunAtLoad</key>
|
|
77
|
+
\t<true/>
|
|
78
|
+
\t<key>KeepAlive</key>
|
|
79
|
+
\t<dict>
|
|
80
|
+
\t\t<key>SuccessfulExit</key>
|
|
81
|
+
\t\t<false/>
|
|
82
|
+
\t</dict>
|
|
83
|
+
\t<key>ProcessType</key>
|
|
84
|
+
\t<string>Interactive</string>
|
|
85
|
+
\t<key>StandardErrorPath</key>
|
|
86
|
+
\t<string>${xmlText(join(oneFolder(), 'kit.log'))}</string>
|
|
87
|
+
</dict>
|
|
88
|
+
</plist>
|
|
89
|
+
`;
|
|
90
|
+
}
|
|
91
|
+
async function define(path, text) {
|
|
92
|
+
if ((await readFile(path, 'utf8').catch(() => null)) === text)
|
|
93
|
+
return false;
|
|
94
|
+
await mkdir(dirname(path), { recursive: true });
|
|
95
|
+
await writeFile(`${path}.new`, text);
|
|
96
|
+
await rename(`${path}.new`, path);
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
export function defineSupervisor() {
|
|
100
|
+
return process.platform === 'darwin' ? define(launchAgent(), plistText()) : define(userUnit(), unitText());
|
|
101
|
+
}
|
|
102
|
+
export async function linger() {
|
|
103
|
+
required('loginctl', ['enable-linger', String(process.getuid())], 'lingering could not be turned on for this account');
|
|
104
|
+
if (process.getuid() === 0)
|
|
105
|
+
required('systemctl', ['start', 'user@0.service'], "root's service manager could not be started");
|
|
106
|
+
const stuck = Date.now() + MANAGER_MS;
|
|
107
|
+
while (manage('systemctl', ['--user', 'show-environment']).status !== 0) {
|
|
108
|
+
if (Date.now() >= stuck) {
|
|
109
|
+
throw new Error(`this account's service manager did not start within ${MANAGER_MS / 1000} seconds; restart this computer, then run this command again`);
|
|
110
|
+
}
|
|
111
|
+
await delay(MANAGER_CHECK_MS);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function macos() {
|
|
115
|
+
const target = `gui/${process.getuid()}`;
|
|
116
|
+
return { target, service: `${target}/${SERVICE}` };
|
|
117
|
+
}
|
|
118
|
+
export async function supervise(changed, restart) {
|
|
119
|
+
if (process.platform === 'darwin') {
|
|
120
|
+
const { target, service } = macos();
|
|
121
|
+
const printed = manage('launchctl', ['print', service]);
|
|
122
|
+
const loaded = printed.status === 0;
|
|
123
|
+
if (loaded && changed)
|
|
124
|
+
required('launchctl', ['bootout', service], 'the Kit service could not be stopped');
|
|
125
|
+
if (!loaded || changed) {
|
|
126
|
+
manage('launchctl', ['enable', service]);
|
|
127
|
+
required('launchctl', ['bootstrap', target, launchAgent()], 'the Kit service could not be started');
|
|
128
|
+
}
|
|
129
|
+
else if (restart) {
|
|
130
|
+
required('launchctl', ['kickstart', '-k', service], 'the Kit service could not be restarted');
|
|
131
|
+
}
|
|
132
|
+
else if (!/^\tstate = running$/m.test(printed.stdout)) {
|
|
133
|
+
required('launchctl', ['kickstart', service], 'the Kit service could not be started');
|
|
134
|
+
}
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
await linger();
|
|
138
|
+
if (changed)
|
|
139
|
+
required('systemctl', ['--user', 'daemon-reload'], 'systemd could not read the Kit service');
|
|
140
|
+
required('systemctl', ['--user', '--quiet', 'enable', UNIT], 'the Kit service could not be enabled');
|
|
141
|
+
if (manage('systemctl', ['--user', '--quiet', 'is-active', UNIT]).status !== 0) {
|
|
142
|
+
required('systemctl', ['--user', 'start', UNIT], 'the Kit service could not be started');
|
|
143
|
+
}
|
|
144
|
+
else if (changed || restart) {
|
|
145
|
+
required('systemctl', ['--user', 'restart', UNIT], 'the Kit service could not be restarted');
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
export function startService() {
|
|
149
|
+
if (process.platform === 'darwin') {
|
|
150
|
+
const { service } = macos();
|
|
151
|
+
const printed = manage('launchctl', ['print', service]);
|
|
152
|
+
if (printed.status !== 0 || /^\tstate = running$/m.test(printed.stdout))
|
|
153
|
+
return false;
|
|
154
|
+
required('launchctl', ['kickstart', service], 'the Kit service could not be started');
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
if (manage('systemctl', ['--user', '--quiet', 'is-enabled', UNIT]).status !== 0)
|
|
158
|
+
return false;
|
|
159
|
+
if (manage('systemctl', ['--user', '--quiet', 'is-active', UNIT]).status === 0)
|
|
160
|
+
return false;
|
|
161
|
+
required('systemctl', ['--user', 'start', UNIT], 'the Kit service could not be started');
|
|
162
|
+
return true;
|
|
163
|
+
}
|
package/dist/transcription.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { chmod, mkdir, mkdtemp, readdir, readFile, rename, rm, stat } from 'node:fs/promises';
|
|
2
2
|
import { availableParallelism, tmpdir } from 'node:os';
|
|
3
3
|
import { basename, join } from 'node:path';
|
|
4
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
4
5
|
import { unlessAborted } from './abort.js';
|
|
5
6
|
import { downloadServed } from './building.js';
|
|
6
7
|
import { failureOf, run } from './clis.js';
|
|
@@ -125,7 +126,7 @@ async function install(artifacts, signal) {
|
|
|
125
126
|
}
|
|
126
127
|
}
|
|
127
128
|
function reported(next) {
|
|
128
|
-
const changed = next
|
|
129
|
+
const changed = !isDeepStrictEqual(next, readiness);
|
|
129
130
|
readiness = next;
|
|
130
131
|
return changed;
|
|
131
132
|
}
|
|
@@ -137,10 +138,12 @@ async function settle(transcribe) {
|
|
|
137
138
|
await rm(directory(), { recursive: true, force: true });
|
|
138
139
|
return reported({ installed: false, failure: null });
|
|
139
140
|
}
|
|
141
|
+
const pinned = pinnedHere();
|
|
142
|
+
if (pinned === undefined)
|
|
143
|
+
return reported({ installed: false, failure: null, no_engine: true });
|
|
140
144
|
const download = new AbortController();
|
|
141
145
|
downloading = download;
|
|
142
146
|
try {
|
|
143
|
-
const pinned = pinnedHere();
|
|
144
147
|
await install([pinned.decoder, pinned.engine, MODEL], download.signal);
|
|
145
148
|
return reported({ installed: true, failure: null });
|
|
146
149
|
}
|
package/dist/translate.js
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
|
-
import { apply, parse, serialize } from '@agentshouse/mdmodel';
|
|
1
|
+
import { apply, definitionAt, HOUSE_TYPED_PATHS, parse, serialize, } from '@agentshouse/mdmodel';
|
|
2
2
|
function same(left, right) {
|
|
3
3
|
return JSON.stringify(left) === JSON.stringify(right);
|
|
4
4
|
}
|
|
5
|
-
function applied(document, changes) {
|
|
6
|
-
const result = apply(document, changes);
|
|
5
|
+
function applied(document, changes, definition) {
|
|
6
|
+
const result = apply(document, changes, definition);
|
|
7
7
|
return result.outcome === 'applied' ? serialize(result.document) : null;
|
|
8
8
|
}
|
|
9
|
-
function
|
|
9
|
+
export function readDocument(content, file, model) {
|
|
10
|
+
const definitions = definitionAt(model, file, HOUSE_TYPED_PATHS);
|
|
11
|
+
const parsed = parse(content, file, definitions);
|
|
12
|
+
if (!parsed.ok)
|
|
13
|
+
return null;
|
|
14
|
+
const { document } = parsed;
|
|
15
|
+
const definition = definitions(document.type);
|
|
16
|
+
return definition !== undefined || document.type === undefined ? { document, definition } : null;
|
|
17
|
+
}
|
|
18
|
+
function components(path, before, after, text, definition) {
|
|
10
19
|
const edits = [];
|
|
11
20
|
const changes = [];
|
|
12
21
|
if (before.preamble !== after.preamble) {
|
|
@@ -33,13 +42,11 @@ function components(path, before, after, text) {
|
|
|
33
42
|
edits.push({ op: 'replace_section', path, section: name, base: prior, content: value });
|
|
34
43
|
changes.push({ kind: 'section', name, value, prior });
|
|
35
44
|
}
|
|
36
|
-
return edits.length > 0 && applied(before, changes) === text ? edits : null;
|
|
45
|
+
return edits.length > 0 && applied(before, changes, definition) === text ? edits : null;
|
|
37
46
|
}
|
|
38
|
-
export function changed(path, file, base, before, after) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
const parsedAfter = parse(after, file);
|
|
43
|
-
const structured = parsedBefore.ok && parsedAfter.ok ? components(path, parsedBefore.document, parsedAfter.document, after) : null;
|
|
47
|
+
export function changed(path, file, base, before, after, model) {
|
|
48
|
+
const prior = readDocument(before, file, model);
|
|
49
|
+
const next = readDocument(after, file, model);
|
|
50
|
+
const structured = prior !== null && next !== null ? components(path, prior.document, next.document, after, prior.definition) : null;
|
|
44
51
|
return structured ?? [{ op: 'replace', path, base, content: after }];
|
|
45
52
|
}
|
package/dist/update.js
CHANGED
|
@@ -1,181 +1,136 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
|
-
import {
|
|
3
|
-
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
|
|
4
|
-
import { homedir } from 'node:os';
|
|
2
|
+
import { mkdir } from 'node:fs/promises';
|
|
5
3
|
import { join } from 'node:path';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
// A
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
4
|
+
import { downloadServed, servedRelease } from './building.js';
|
|
5
|
+
import { KIT_RELEASE } from './clis.js';
|
|
6
|
+
import { restartPlaced, updateLine } from './end.js';
|
|
7
|
+
import { kitHome, readEnrolment } from './home.js';
|
|
8
|
+
import { placedPrefix } from './placement.js';
|
|
9
|
+
import { childEnvironment, relayOrigin } from './relay.js';
|
|
10
|
+
const INSTALLER_BYTES = 1_000_000;
|
|
11
|
+
// A release's Node.js, Kit package and adapters are about a hundred megabytes, so an hour carries them at thirty kilobytes a second, and a placement still running then has hung.
|
|
12
|
+
const PLACEMENT_MS = 60 * 60_000;
|
|
13
|
+
// A refused Kit is offline until its placement succeeds, so it tries again within five minutes of its Building or this computer recovering, while a placement that fails only after its hundred-megabyte download runs at most twelve times an hour.
|
|
14
|
+
export const PLACEMENT_RETRY_MS = 5 * 60_000;
|
|
15
|
+
const REFUSED = 'kit_bootstrap_refused: ';
|
|
16
|
+
const OUTPUT_CHARACTERS = 4000;
|
|
17
|
+
function causeOf(error) {
|
|
18
|
+
if (error instanceof TypeError && error.cause instanceof Error)
|
|
19
|
+
return `the Building did not answer: ${error.cause.message}`;
|
|
20
|
+
return error instanceof Error ? error.message : String(error);
|
|
21
|
+
}
|
|
22
|
+
function placing(installer, house, prefix, signal) {
|
|
23
|
+
return new Promise((resolve) => {
|
|
24
|
+
const relay = relayOrigin();
|
|
25
|
+
const child = spawn('bash', [installer, '--house', house, '--place', prefix], {
|
|
26
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
27
|
+
env: { ...childEnvironment(), ...(relay === undefined ? {} : { HOUSE_KIT_RELAY: relay }) },
|
|
28
|
+
detached: true,
|
|
29
|
+
});
|
|
30
|
+
const stop = () => {
|
|
31
|
+
try {
|
|
32
|
+
process.kill(-child.pid, 'SIGKILL');
|
|
33
|
+
}
|
|
34
|
+
catch { }
|
|
35
|
+
};
|
|
36
|
+
signal.addEventListener('abort', stop, { once: true });
|
|
37
|
+
if (signal.aborted)
|
|
38
|
+
stop();
|
|
39
|
+
let output = '';
|
|
40
|
+
for (const stream of [child.stdout, child.stderr]) {
|
|
41
|
+
stream.setEncoding('utf8').on('data', (chunk) => {
|
|
42
|
+
output = (output + chunk).slice(-OUTPUT_CHARACTERS);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const ended = (failure) => {
|
|
46
|
+
signal.removeEventListener('abort', stop);
|
|
47
|
+
resolve(failure);
|
|
48
|
+
};
|
|
49
|
+
child.on('error', (error) => ended(error.message));
|
|
50
|
+
child.on('close', (status) => {
|
|
51
|
+
if (status === 0)
|
|
52
|
+
return ended(null);
|
|
53
|
+
if (signal.aborted)
|
|
54
|
+
return ended(`the placement did not end within ${PLACEMENT_MS / 1000} seconds`);
|
|
55
|
+
const lines = output.split('\n').filter((line) => line.trim() !== '');
|
|
56
|
+
const refused = lines.findLast((line) => line.startsWith(REFUSED));
|
|
57
|
+
ended(refused?.slice(REFUSED.length) ?? lines.at(-1) ?? `the placement ended with status ${status ?? 'a signal'}`);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
42
60
|
}
|
|
43
|
-
async function
|
|
61
|
+
async function placed(prefix, admit) {
|
|
62
|
+
const signal = AbortSignal.timeout(PLACEMENT_MS);
|
|
63
|
+
let version = '';
|
|
44
64
|
try {
|
|
45
|
-
|
|
65
|
+
const placedIn = prefix();
|
|
66
|
+
const served = await servedRelease(signal);
|
|
67
|
+
admit(served.version);
|
|
68
|
+
version = served.version;
|
|
69
|
+
await mkdir(join(kitHome(), 'update'), { recursive: true, mode: 0o700 });
|
|
70
|
+
const installer = join(kitHome(), 'update', served.installer);
|
|
71
|
+
await downloadServed(served.installer, served.sha256, installer, signal, INSTALLER_BYTES);
|
|
72
|
+
return { version, failure: await placing(installer, (await readEnrolment()).house, placedIn, signal) };
|
|
46
73
|
}
|
|
47
|
-
catch {
|
|
48
|
-
return
|
|
74
|
+
catch (error) {
|
|
75
|
+
return { version, failure: signal.aborted ? `the placement did not end within ${PLACEMENT_MS / 1000} seconds` : causeOf(error) };
|
|
49
76
|
}
|
|
50
77
|
}
|
|
51
|
-
async function
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
return null;
|
|
55
|
-
if (status.trim() === TIMED_OUT)
|
|
56
|
-
return { refused: `the Kit bootstrap did not finish within ${RERUN_MS / 1000} seconds` };
|
|
57
|
-
return status.trim() === '0' ? {} : { refused: `the Kit bootstrap exited with ${status.trim()}` };
|
|
58
|
-
}
|
|
59
|
-
async function forget(callId) {
|
|
60
|
-
if ((await pendingCall()) !== callId)
|
|
61
|
-
return;
|
|
62
|
-
await rm(pending(), { force: true });
|
|
63
|
-
await rm(outcome(), { force: true });
|
|
64
|
-
}
|
|
65
|
-
async function finished(callId) {
|
|
66
|
-
for (;;) {
|
|
67
|
-
const result = await recorded();
|
|
68
|
-
if (result !== null)
|
|
69
|
-
return result;
|
|
70
|
-
// systemctl answers at once, so ten seconds only bounds a stuck service manager.
|
|
71
|
-
if ((await run('systemctl', [...manager(), 'is-active', '--quiet', unit(callId)], 10_000)).status !== 0) {
|
|
72
|
-
return (await recorded()) ?? { refused: 'the Kit bootstrap did not finish' };
|
|
73
|
-
}
|
|
74
|
-
// The bootstrap runs for minutes, so checking each second sees its outcome within a second at little cost.
|
|
75
|
-
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
async function bounded(body, most) {
|
|
79
|
-
const chunks = [];
|
|
80
|
-
let bytes = 0;
|
|
81
|
-
for await (const chunk of body ?? []) {
|
|
82
|
-
bytes += chunk.length;
|
|
83
|
-
if (bytes > most)
|
|
84
|
-
return null;
|
|
85
|
-
chunks.push(chunk);
|
|
86
|
-
}
|
|
87
|
-
return Buffer.concat(chunks, bytes);
|
|
88
|
-
}
|
|
89
|
-
function rerun(script, callId, flags) {
|
|
90
|
-
return new Promise((resolve) => {
|
|
91
|
-
const [command, ...args] = [
|
|
92
|
-
...elevated(),
|
|
93
|
-
'systemd-run',
|
|
94
|
-
...manager(),
|
|
95
|
-
...account(),
|
|
96
|
-
'--wait',
|
|
97
|
-
'--collect',
|
|
98
|
-
'--quiet',
|
|
99
|
-
`--unit=${unit(callId)}`,
|
|
100
|
-
`--setenv=HOME=${homedir()}`,
|
|
101
|
-
`--setenv=HOUSE_KIT_HOME=${kitHome()}`,
|
|
102
|
-
'/bin/sh',
|
|
103
|
-
'-c',
|
|
104
|
-
record(),
|
|
105
|
-
script,
|
|
106
|
-
outcome(),
|
|
107
|
-
...placement(),
|
|
108
|
-
...flags,
|
|
109
|
-
];
|
|
110
|
-
const child = spawn(command, args, { stdio: ['ignore', 'ignore', 'inherit'] });
|
|
111
|
-
child.on('error', () => resolve({ refused: 'no service manager runs this Kit process' }));
|
|
112
|
-
child.on('close', () => resolve(null));
|
|
113
|
-
});
|
|
78
|
+
export async function placeServedRelease(prefix) {
|
|
79
|
+
const { version, failure } = await placed(() => prefix, () => undefined);
|
|
80
|
+
return failure === null ? null : `${version === '' ? "The Building's Kit release" : `Kit ${version}`} could not be placed: ${failure}`;
|
|
114
81
|
}
|
|
115
82
|
export class KitUpdate {
|
|
116
83
|
calls;
|
|
117
|
-
changed;
|
|
118
|
-
waiting = new Map();
|
|
119
84
|
queue = Promise.resolve();
|
|
120
|
-
|
|
85
|
+
placed = null;
|
|
86
|
+
constructor(calls) {
|
|
121
87
|
this.calls = calls;
|
|
122
|
-
this.changed = changed;
|
|
123
88
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
if (callId === null)
|
|
127
|
-
return;
|
|
128
|
-
await this.enqueue(callId, () => finished(callId));
|
|
89
|
+
take(call) {
|
|
90
|
+
void this.calls.carry(call.input_id, async () => ({ result: await this.queued(() => this.update(call.release)) }));
|
|
129
91
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
92
|
+
async takeRefused(release) {
|
|
93
|
+
for (;;) {
|
|
94
|
+
const failure = await this.queued(async () => (this.placed === null ? this.place(release, 'served') : null));
|
|
95
|
+
if (failure === null)
|
|
96
|
+
restartPlaced(this.placed);
|
|
97
|
+
process.stderr.write(`kit: ${updateLine(`Kit ${KIT_RELEASE} stays offline and tries again in ${PLACEMENT_RETRY_MS / 60_000} minutes`)}\n`);
|
|
98
|
+
await new Promise((resolve) => setTimeout(resolve, PLACEMENT_RETRY_MS));
|
|
99
|
+
}
|
|
133
100
|
}
|
|
134
|
-
|
|
135
|
-
return this.
|
|
101
|
+
placedRelease() {
|
|
102
|
+
return this.placed;
|
|
136
103
|
}
|
|
137
|
-
|
|
138
|
-
const
|
|
139
|
-
this.
|
|
140
|
-
|
|
104
|
+
queued(work) {
|
|
105
|
+
const taken = this.queue.then(work);
|
|
106
|
+
this.queue = taken.catch(() => undefined);
|
|
107
|
+
return taken;
|
|
141
108
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
return {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
109
|
+
async update(release) {
|
|
110
|
+
if (release === (this.placed ?? KIT_RELEASE))
|
|
111
|
+
return {};
|
|
112
|
+
if (this.placed !== null)
|
|
113
|
+
return { refused: `Kit ${this.placed} is placed and starts first, once no turn is open` };
|
|
114
|
+
const failure = await this.place(release, 'named');
|
|
115
|
+
if (failure !== null)
|
|
116
|
+
return { refused: failure };
|
|
117
|
+
const starts = process.env.HOUSE_KIT_SANDBOX === '1' ? 'when the Sandbox starts again' : 'once no turn is open';
|
|
118
|
+
process.stderr.write(`kit: Kit ${release} is placed; it starts ${starts}\n`);
|
|
119
|
+
return {};
|
|
150
120
|
}
|
|
151
|
-
async
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
catch (error) {
|
|
163
|
-
if (!deadline.aborted)
|
|
164
|
-
throw error;
|
|
165
|
-
return { refused: `the Kit bootstrap did not arrive within ${BOOTSTRAP_MS / 1000} seconds` };
|
|
166
|
-
}
|
|
167
|
-
if (script === null)
|
|
168
|
-
return { refused: `the Kit bootstrap is larger than ${BOOTSTRAP_BYTES} bytes` };
|
|
169
|
-
if (createHash('sha256').update(script).digest('hex') !== call.sha256) {
|
|
170
|
-
return { refused: 'the Kit bootstrap does not match its sha256' };
|
|
121
|
+
async place(release, which) {
|
|
122
|
+
const { version, failure } = await placed(placedPrefix, (served) => {
|
|
123
|
+
if (which === 'named' && served !== release)
|
|
124
|
+
throw new Error(`the Building serves Kit ${served}, not Kit ${release}`);
|
|
125
|
+
if (served === KIT_RELEASE)
|
|
126
|
+
throw new Error(`the Building serves Kit ${KIT_RELEASE}, which this Kit runs`);
|
|
127
|
+
});
|
|
128
|
+
if (failure !== null) {
|
|
129
|
+
const refused = `Kit ${version || release} could not be placed: ${failure}`;
|
|
130
|
+
process.stderr.write(`kit: ${refused}\n`);
|
|
131
|
+
return refused;
|
|
171
132
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
await writeFile(path, script, { mode: 0o700 });
|
|
175
|
-
await rm(outcome(), { force: true });
|
|
176
|
-
await writeFile(pending(), `${JSON.stringify({ input_id: call.input_id })}\n`, { mode: 0o600 });
|
|
177
|
-
return (await rerun(path, call.input_id, call.update_clis ? ['--update-clis'] : [])) ?? (await recorded()) ?? {
|
|
178
|
-
refused: 'the Kit bootstrap did not finish',
|
|
179
|
-
};
|
|
133
|
+
this.placed = version;
|
|
134
|
+
return null;
|
|
180
135
|
}
|
|
181
136
|
}
|