@agentshouse/kit 0.2.1-cf110d15f69c8 → 0.2.1-cf2c570b41f24
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 +38 -44
- package/dist/acp.js +96 -22
- package/dist/afk.js +17 -0
- package/dist/agents.js +221 -65
- package/dist/api.js +21 -3
- package/dist/app-git.js +10 -26
- package/dist/binaries.js +60 -0
- package/dist/bridge.js +101 -49
- package/dist/building.js +50 -0
- package/dist/calls.js +88 -0
- package/dist/chunks.js +54 -0
- package/dist/clis.js +80 -52
- package/dist/connect.js +121 -0
- package/dist/connection.js +120 -0
- package/dist/content.js +106 -0
- package/dist/conversations.js +711 -267
- package/dist/copies.js +1293 -595
- package/dist/earlier.js +273 -0
- package/dist/end.js +15 -3
- package/dist/events.js +553 -281
- package/dist/files.js +206 -32
- package/dist/git.js +2 -70
- package/dist/grok.js +39 -0
- package/dist/home.js +123 -15
- package/dist/house-cli.js +282 -0
- package/dist/house-main.js +7 -206
- package/dist/instructions.js +7 -13
- package/dist/keys.js +238 -0
- package/dist/kit-cli.js +78 -0
- package/dist/kit-main.js +8 -22
- package/dist/login.js +71 -39
- package/dist/logout.js +2 -4
- package/dist/manifest.js +75 -0
- package/dist/mcp.js +47 -7
- package/dist/owner.js +46 -0
- package/dist/placement.js +25 -0
- package/dist/refusals.js +50 -23
- package/dist/relay.js +22 -1
- package/dist/resident.js +153 -53
- package/dist/scope.js +15 -4
- package/dist/scripts.js +319 -0
- package/dist/secret-input.js +1 -1
- package/dist/settings.js +247 -0
- package/dist/sign-in.js +66 -38
- package/dist/silence.js +28 -0
- package/dist/skills.js +49 -35
- package/dist/supervisor.js +163 -0
- package/dist/transcription.js +90 -47
- package/dist/update.js +119 -102
- package/dist/wire.js +32 -0
- package/dist/work.js +30 -7
- package/dist/workspace.js +113 -0
- package/package.json +25 -21
- package/dist/authority-main.js +0 -22
- 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 -10
- package/dist/git-main.js +0 -153
- package/dist/parts.js +0 -82
- package/dist/service-main.js +0 -9
- package/dist/service.js +0 -33
- package/dist/stream.js +0 -74
- package/dist/translate.js +0 -51
- 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/sign-in.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
import { createInterface } from 'node:readline';
|
|
3
|
-
import { CLIS,
|
|
3
|
+
import { CLIS, cliEnvironment } from './clis.js';
|
|
4
4
|
import { killTree } from './scope.js';
|
|
5
5
|
import { holdSecretInput } from './secret-input.js';
|
|
6
6
|
const ESCAPE = /\u001b\[[\d;]*m/g;
|
|
@@ -9,8 +9,32 @@ const CODE = /^[A-Z0-9]+-[A-Z0-9]+$/;
|
|
|
9
9
|
function causeOf(error) {
|
|
10
10
|
return error instanceof Error ? error.message : String(error);
|
|
11
11
|
}
|
|
12
|
-
function
|
|
13
|
-
|
|
12
|
+
function 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;
|
|
14
38
|
}
|
|
15
39
|
function endOf(signal) {
|
|
16
40
|
return new Promise((resolve) => signal.addEventListener('abort', () => resolve(true), { once: true }));
|
|
@@ -18,63 +42,67 @@ function endOf(signal) {
|
|
|
18
42
|
export class SignIns {
|
|
19
43
|
house;
|
|
20
44
|
agents;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
constructor(house, agents, changed) {
|
|
45
|
+
calls;
|
|
46
|
+
constructor(house, agents, calls) {
|
|
24
47
|
this.house = house;
|
|
25
48
|
this.agents = agents;
|
|
26
|
-
this.
|
|
49
|
+
this.calls = calls;
|
|
27
50
|
}
|
|
28
|
-
|
|
29
|
-
|
|
51
|
+
start(call) {
|
|
52
|
+
void this.calls.carry(call.input_id, () => this.signIn(call));
|
|
30
53
|
}
|
|
31
|
-
|
|
32
|
-
if (this.running.has(input.input_id))
|
|
33
|
-
return;
|
|
34
|
-
this.running.add(input.input_id);
|
|
35
|
-
this.changed();
|
|
36
|
-
this.signIn(input)
|
|
37
|
-
.catch(logged)
|
|
38
|
-
.finally(() => {
|
|
39
|
-
this.running.delete(input.input_id);
|
|
40
|
-
this.changed();
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
async signIn(input) {
|
|
54
|
+
async signIn(call) {
|
|
44
55
|
const ending = new AbortController();
|
|
45
56
|
// House stops waiting for a sign-in ten minutes after it wrote it and sends the seconds left, so its install and login end with the page.
|
|
46
|
-
const timer = setTimeout(() => ending.abort(),
|
|
47
|
-
const ended = await Promise.race([this.signedIn(
|
|
57
|
+
const timer = setTimeout(() => ending.abort(), call.ends_in * 1000);
|
|
58
|
+
const ended = await Promise.race([this.signedIn(call, ending.signal), endOf(ending.signal)]);
|
|
48
59
|
clearTimeout(timer);
|
|
49
60
|
if (ended)
|
|
50
|
-
process.stderr.write(`kit: the ${
|
|
51
|
-
|
|
52
|
-
if (!ended)
|
|
53
|
-
await this.agents.reread(input.cli);
|
|
61
|
+
process.stderr.write(`kit: the ${call.cli} sign-in reached the end House set for it\n`);
|
|
62
|
+
return ended ? { result: {} } : { result: {}, returned: () => this.agents.reread(call.cli) };
|
|
54
63
|
}
|
|
55
|
-
async signedIn(
|
|
56
|
-
await this.login(
|
|
64
|
+
async signedIn(call, signal) {
|
|
65
|
+
await (call.api_key === true ? this.keyed(call, signal) : this.login(call, signal));
|
|
57
66
|
if (!signal.aborted)
|
|
58
|
-
await this.agents.recheck(
|
|
67
|
+
await this.agents.recheck(call.cli, signal);
|
|
59
68
|
return false;
|
|
60
69
|
}
|
|
61
|
-
async
|
|
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
|
+
}
|
|
87
|
+
async login(call, signal) {
|
|
62
88
|
let cli;
|
|
89
|
+
let env;
|
|
63
90
|
try {
|
|
64
|
-
cli = await this.agents.located(
|
|
91
|
+
cli = await this.agents.located(call.cli, signal);
|
|
92
|
+
env = await cliEnvironment(call.cli);
|
|
65
93
|
}
|
|
66
94
|
catch (error) {
|
|
67
95
|
if (!signal.aborted)
|
|
68
|
-
process.stderr.write(`kit: the ${
|
|
96
|
+
process.stderr.write(`kit: the ${call.cli} sign-in failed: ${causeOf(error)}\n`);
|
|
69
97
|
return;
|
|
70
98
|
}
|
|
71
99
|
if (signal.aborted)
|
|
72
100
|
return;
|
|
73
|
-
const login = CLIS[
|
|
101
|
+
const login = CLIS[call.cli].login;
|
|
74
102
|
const child = spawn(cli, login.args, {
|
|
75
103
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
76
104
|
detached: true,
|
|
77
|
-
env
|
|
105
|
+
env,
|
|
78
106
|
});
|
|
79
107
|
const running = () => child.exitCode === null && child.signalCode === null;
|
|
80
108
|
const stop = () => killTree(child);
|
|
@@ -102,7 +130,7 @@ export class SignIns {
|
|
|
102
130
|
return;
|
|
103
131
|
held = true;
|
|
104
132
|
const steps = [{ kind: 'visit', label: 'Open this link and sign in', url: link }, step];
|
|
105
|
-
void holdSecretInput(this.house,
|
|
133
|
+
void holdSecretInput(this.house, call.input_id, steps, running).then((content) => {
|
|
106
134
|
if (!running())
|
|
107
135
|
return;
|
|
108
136
|
if (content === null)
|
|
@@ -119,6 +147,6 @@ export class SignIns {
|
|
|
119
147
|
});
|
|
120
148
|
signal.removeEventListener('abort', stop);
|
|
121
149
|
if (failed !== null && !signal.aborted)
|
|
122
|
-
process.stderr.write(`kit: the ${
|
|
150
|
+
process.stderr.write(`kit: the ${call.cli} sign-in failed: ${failed}\n`);
|
|
123
151
|
}
|
|
124
152
|
}
|
package/dist/silence.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const SYSTEM_CLOCK = {
|
|
2
|
+
later: (delay, callback) => setTimeout(callback, delay),
|
|
3
|
+
clear: (timer) => clearTimeout(timer),
|
|
4
|
+
};
|
|
5
|
+
export class Silence {
|
|
6
|
+
timer = null;
|
|
7
|
+
interval;
|
|
8
|
+
silent;
|
|
9
|
+
clock;
|
|
10
|
+
constructor(interval, silent, clock = SYSTEM_CLOCK) {
|
|
11
|
+
this.interval = interval;
|
|
12
|
+
this.silent = silent;
|
|
13
|
+
this.clock = clock;
|
|
14
|
+
}
|
|
15
|
+
heard() {
|
|
16
|
+
this.stop();
|
|
17
|
+
this.timer = this.clock.later(this.interval, () => {
|
|
18
|
+
this.timer = null;
|
|
19
|
+
this.silent();
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
stop() {
|
|
23
|
+
if (this.timer === null)
|
|
24
|
+
return;
|
|
25
|
+
this.clock.clear(this.timer);
|
|
26
|
+
this.timer = null;
|
|
27
|
+
}
|
|
28
|
+
}
|
package/dist/skills.js
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { cp, lstat, mkdir, readdir, realpath, rm, symlink, writeFile } from 'node:fs/promises';
|
|
1
|
+
import { cp, lstat, mkdir, realpath, rm, symlink, writeFile } from 'node:fs/promises';
|
|
3
2
|
import { homedir } from 'node:os';
|
|
4
3
|
import { join, relative } from 'node:path';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { GUIDE, listSkills } from '@agentshouse/skills';
|
|
5
|
+
import { parse } from 'yaml';
|
|
6
|
+
import { KIT_RELEASE } from './clis.js';
|
|
7
7
|
import { kitHome, readConfiguration, readHome, writeHome } from './home.js';
|
|
8
|
-
import { refusalOf } from './refusals.js';
|
|
9
|
-
export const HOW_WE_WORK = '/rooms/private/how-we-work.md';
|
|
10
|
-
const SKILL_SET = fileURLToPath(new URL('../skills/', import.meta.url));
|
|
11
|
-
const HOW_WE_WORK_TEXT = readFileSync(new URL('../how-we-work.md', import.meta.url), 'utf8');
|
|
12
8
|
const RECORD = 'skill-set.json';
|
|
13
|
-
const
|
|
14
|
-
const
|
|
9
|
+
const ENTRY = 'SKILL.md';
|
|
10
|
+
const FRONTMATTER = /^---\n([\s\S]*?)\n---\n/u;
|
|
11
|
+
const ORIGIN = '<origin>';
|
|
15
12
|
function skillsDirectory() {
|
|
16
13
|
return join(homedir(), '.agents', 'skills');
|
|
17
14
|
}
|
|
@@ -29,50 +26,67 @@ async function present(path) {
|
|
|
29
26
|
throw error;
|
|
30
27
|
}
|
|
31
28
|
}
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
29
|
+
function skillName(text) {
|
|
30
|
+
const front = FRONTMATTER.exec(text);
|
|
31
|
+
const fields = (front === null ? null : parse(front[1]));
|
|
32
|
+
return typeof fields?.name === 'string' && typeof fields.description === 'string' ? fields.name : undefined;
|
|
33
|
+
}
|
|
34
|
+
function skillSet(origin) {
|
|
35
|
+
const upstream = listSkills().map(({ name, directory }) => ({
|
|
36
|
+
name,
|
|
37
|
+
place: (destination) => cp(directory, destination, { recursive: true }),
|
|
38
|
+
}));
|
|
39
|
+
const pages = GUIDE.flatMap(({ text }) => {
|
|
40
|
+
const name = skillName(text);
|
|
41
|
+
if (name === undefined)
|
|
42
|
+
return [];
|
|
43
|
+
const place = async (destination) => {
|
|
44
|
+
await mkdir(destination);
|
|
45
|
+
await writeFile(join(destination, ENTRY), text.replaceAll(ORIGIN, origin));
|
|
46
|
+
};
|
|
47
|
+
return [{ name, place }];
|
|
48
|
+
});
|
|
49
|
+
return [...upstream, ...pages].sort((left, right) => (left.name < right.name ? -1 : 1));
|
|
50
|
+
}
|
|
51
|
+
async function install(origin) {
|
|
52
|
+
const placed = [];
|
|
53
|
+
for (const skill of skillSet(origin)) {
|
|
54
|
+
if (!(await present(join(skillsDirectory(), skill.name))) && !(await present(join(claudeDirectory(), skill.name)))) {
|
|
55
|
+
placed.push(skill);
|
|
37
56
|
}
|
|
38
57
|
}
|
|
58
|
+
const names = placed.map((skill) => skill.name);
|
|
39
59
|
await writeHome(RECORD, { names });
|
|
40
60
|
await mkdir(skillsDirectory(), { recursive: true });
|
|
41
61
|
await mkdir(claudeDirectory(), { recursive: true });
|
|
42
62
|
const skills = await realpath(skillsDirectory());
|
|
43
63
|
const claude = await realpath(claudeDirectory());
|
|
44
|
-
for (const name of
|
|
45
|
-
await
|
|
64
|
+
for (const { name, place } of placed) {
|
|
65
|
+
await place(join(skillsDirectory(), name));
|
|
46
66
|
if (claude !== skills)
|
|
47
67
|
await symlink(relative(claude, join(skills, name)), join(claudeDirectory(), name));
|
|
48
68
|
}
|
|
49
|
-
await writeHome(RECORD, { version:
|
|
69
|
+
await writeHome(RECORD, { version: KIT_RELEASE, origin, names });
|
|
70
|
+
}
|
|
71
|
+
export async function placeSkillSet(origin) {
|
|
72
|
+
try {
|
|
73
|
+
await replaceSkillSet(origin);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
process.stderr.write(`kit: the skills could not be placed: ${error.message}\n`);
|
|
77
|
+
}
|
|
50
78
|
}
|
|
51
|
-
|
|
79
|
+
async function replaceSkillSet(origin) {
|
|
52
80
|
const accepted = (await readConfiguration()).skills;
|
|
53
81
|
const installed = await readHome(RECORD);
|
|
54
|
-
if (accepted && installed?.version ===
|
|
82
|
+
if (accepted && installed?.version === KIT_RELEASE && installed.origin === origin)
|
|
55
83
|
return;
|
|
56
84
|
for (const name of installed?.names ?? []) {
|
|
57
85
|
await rm(join(claudeDirectory(), name), { recursive: true, force: true });
|
|
58
86
|
await rm(join(skillsDirectory(), name), { recursive: true, force: true });
|
|
59
87
|
}
|
|
60
88
|
if (accepted)
|
|
61
|
-
await install();
|
|
89
|
+
await install(origin);
|
|
62
90
|
else
|
|
63
91
|
await rm(join(kitHome(), RECORD), { force: true });
|
|
64
92
|
}
|
|
65
|
-
export async function createHowWeWork(bridge) {
|
|
66
|
-
if ((await readHome(RECORD)) === null || (await present(join(kitHome(), CREATED))))
|
|
67
|
-
return;
|
|
68
|
-
const created = await bridge.tool('edit', { changes: [{ op: 'create', path: HOW_WE_WORK, content: HOW_WE_WORK_TEXT }] });
|
|
69
|
-
if (created.isError === true) {
|
|
70
|
-
const text = created.content.map((content) => content.text).join('\n');
|
|
71
|
-
const refusal = refusalOf(text);
|
|
72
|
-
if (UNWRITABLE.has(refusal?.code ?? ''))
|
|
73
|
-
return;
|
|
74
|
-
if (refusal?.code !== 'edit_conflict' || !refusal.conflicts.includes('path_exists'))
|
|
75
|
-
throw new Error(text);
|
|
76
|
-
}
|
|
77
|
-
await writeFile(join(kitHome(), CREATED), '');
|
|
78
|
-
}
|
|
@@ -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
|
+
}
|