@agentshouse/kit 0.2.1-cde47fd72fe59 → 0.2.1-ce0af1aee9a57

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.
Files changed (149) hide show
  1. package/README.md +28 -29
  2. package/dist/acp.js +59 -8
  3. package/dist/afk.js +17 -0
  4. package/dist/agents.js +59 -33
  5. package/dist/api.js +5 -5
  6. package/dist/authority-main.js +3 -3
  7. package/dist/bridge.js +5 -5
  8. package/dist/building.js +50 -0
  9. package/dist/calls.js +71 -0
  10. package/dist/chunks.js +49 -0
  11. package/dist/clis.js +47 -45
  12. package/dist/{stream.js → connection.js} +46 -49
  13. package/dist/content.js +63 -0
  14. package/dist/conversations.js +614 -206
  15. package/dist/copies.js +308 -176
  16. package/dist/end.js +14 -4
  17. package/dist/events.js +742 -210
  18. package/dist/files.js +139 -37
  19. package/dist/git.js +47 -13
  20. package/dist/grok.js +39 -0
  21. package/dist/home.js +0 -7
  22. package/dist/house-cli.js +97 -89
  23. package/dist/house-main.js +3 -3
  24. package/dist/instructions.js +29 -11
  25. package/dist/kit-cli.js +5 -8
  26. package/dist/kit-main.js +3 -3
  27. package/dist/login.js +6 -6
  28. package/dist/logout.js +1 -1
  29. package/dist/mcp.js +6 -2
  30. package/dist/placement.js +21 -20
  31. package/dist/relay.js +21 -0
  32. package/dist/resident.js +69 -48
  33. package/dist/scope.js +15 -4
  34. package/dist/scripts.js +255 -0
  35. package/dist/secret-input.js +1 -1
  36. package/dist/service.js +1 -1
  37. package/dist/sign-in.js +22 -21
  38. package/dist/silence.js +28 -0
  39. package/dist/skills.js +42 -97
  40. package/dist/transcription.js +88 -45
  41. package/dist/translate.js +18 -9
  42. package/dist/update.js +113 -124
  43. package/dist/wire.js +37 -3
  44. package/dist/work.js +9 -2
  45. package/package.json +22 -21
  46. package/dist/bootstrap.json +0 -24
  47. package/dist/chatgpt.js +0 -32
  48. package/dist/clis-main.js +0 -23
  49. package/dist/inputs.js +0 -37
  50. package/dist/parts.js +0 -82
  51. package/how-we-work.md +0 -33
  52. package/skills/ask-matt/LICENSE +0 -21
  53. package/skills/ask-matt/PHASE-BOUNDARIES.md +0 -55
  54. package/skills/ask-matt/SKILL.md +0 -97
  55. package/skills/ask-matt/agents/openai.yaml +0 -5
  56. package/skills/code-review/LICENSE +0 -21
  57. package/skills/code-review/SKILL.md +0 -89
  58. package/skills/code-review/agents/openai.yaml +0 -3
  59. package/skills/codebase-design/DEEPENING.md +0 -37
  60. package/skills/codebase-design/DESIGN-IT-TWICE.md +0 -44
  61. package/skills/codebase-design/LICENSE +0 -21
  62. package/skills/codebase-design/SKILL.md +0 -114
  63. package/skills/codebase-design/agents/openai.yaml +0 -3
  64. package/skills/diagnosing-bugs/LICENSE +0 -21
  65. package/skills/diagnosing-bugs/SKILL.md +0 -138
  66. package/skills/diagnosing-bugs/agents/openai.yaml +0 -3
  67. package/skills/diagnosing-bugs/scripts/hitl-loop.template.sh +0 -44
  68. package/skills/domain-modeling/ADR-FORMAT.md +0 -47
  69. package/skills/domain-modeling/GLOSSARY-FORMAT.md +0 -60
  70. package/skills/domain-modeling/LICENSE +0 -21
  71. package/skills/domain-modeling/SKILL.md +0 -74
  72. package/skills/domain-modeling/agents/openai.yaml +0 -3
  73. package/skills/grill-me/LICENSE +0 -21
  74. package/skills/grill-me/SKILL.md +0 -7
  75. package/skills/grill-me/agents/openai.yaml +0 -5
  76. package/skills/grill-with-docs/LICENSE +0 -21
  77. package/skills/grill-with-docs/SKILL.md +0 -7
  78. package/skills/grill-with-docs/agents/openai.yaml +0 -5
  79. package/skills/grilling/LICENSE +0 -21
  80. package/skills/grilling/SKILL.md +0 -30
  81. package/skills/grilling/agents/openai.yaml +0 -3
  82. package/skills/handoff/LICENSE +0 -21
  83. package/skills/handoff/SKILL.md +0 -16
  84. package/skills/handoff/agents/openai.yaml +0 -5
  85. package/skills/implement/LICENSE +0 -21
  86. package/skills/implement/SKILL.md +0 -17
  87. package/skills/implement/agents/openai.yaml +0 -5
  88. package/skills/implement-spec/LICENSE +0 -21
  89. package/skills/implement-spec/SKILL.md +0 -40
  90. package/skills/implement-spec/agents/openai.yaml +0 -5
  91. package/skills/improve-codebase-architecture/HTML-REPORT.md +0 -123
  92. package/skills/improve-codebase-architecture/LICENSE +0 -21
  93. package/skills/improve-codebase-architecture/SKILL.md +0 -71
  94. package/skills/improve-codebase-architecture/agents/openai.yaml +0 -5
  95. package/skills/pr/CREDITS.md +0 -3
  96. package/skills/pr/LICENSE +0 -21
  97. package/skills/pr/SKILL.md +0 -170
  98. package/skills/pr/agents/openai.yaml +0 -3
  99. package/skills/prototype/LICENSE +0 -21
  100. package/skills/prototype/LOGIC.md +0 -67
  101. package/skills/prototype/SKILL.md +0 -26
  102. package/skills/prototype/UI.md +0 -112
  103. package/skills/prototype/agents/openai.yaml +0 -3
  104. package/skills/research/LICENSE +0 -21
  105. package/skills/research/SKILL.md +0 -12
  106. package/skills/research/agents/openai.yaml +0 -3
  107. package/skills/retro/LICENSE +0 -21
  108. package/skills/retro/SKILL.md +0 -44
  109. package/skills/retro/agents/openai.yaml +0 -5
  110. package/skills/tdd/LICENSE +0 -21
  111. package/skills/tdd/SKILL.md +0 -38
  112. package/skills/tdd/agents/openai.yaml +0 -3
  113. package/skills/tdd/mocking.md +0 -59
  114. package/skills/tdd/tests.md +0 -77
  115. package/skills/teach/GLOSSARY-FORMAT.md +0 -35
  116. package/skills/teach/LEARNING-RECORD-FORMAT.md +0 -46
  117. package/skills/teach/LICENSE +0 -21
  118. package/skills/teach/MISSION-FORMAT.md +0 -31
  119. package/skills/teach/RESOURCES-FORMAT.md +0 -32
  120. package/skills/teach/SKILL.md +0 -140
  121. package/skills/teach/agents/openai.yaml +0 -5
  122. package/skills/to-questionnaire/LICENSE +0 -21
  123. package/skills/to-questionnaire/SKILL.md +0 -54
  124. package/skills/to-questionnaire/agents/openai.yaml +0 -5
  125. package/skills/to-spec/LICENSE +0 -21
  126. package/skills/to-spec/SKILL.md +0 -75
  127. package/skills/to-spec/agents/openai.yaml +0 -5
  128. package/skills/to-tickets/LICENSE +0 -21
  129. package/skills/to-tickets/SKILL.md +0 -105
  130. package/skills/to-tickets/agents/openai.yaml +0 -5
  131. package/skills/triage/AGENT-BRIEF.md +0 -207
  132. package/skills/triage/LICENSE +0 -21
  133. package/skills/triage/OUT-OF-SCOPE.md +0 -105
  134. package/skills/triage/SKILL.md +0 -112
  135. package/skills/triage/agents/openai.yaml +0 -5
  136. package/skills/wait-what/LICENSE +0 -21
  137. package/skills/wait-what/SKILL.md +0 -7
  138. package/skills/wait-what/agents/openai.yaml +0 -5
  139. package/skills/wayfinder/LICENSE +0 -21
  140. package/skills/wayfinder/SKILL.md +0 -128
  141. package/skills/wayfinder/agents/openai.yaml +0 -5
  142. package/skills/wizard/LICENSE +0 -21
  143. package/skills/wizard/SKILL.md +0 -44
  144. package/skills/wizard/agents/openai.yaml +0 -3
  145. package/skills/wizard/template.sh +0 -223
  146. package/skills/writing-for-agents/LICENSE +0 -21
  147. package/skills/writing-for-agents/SKILL-MECHANICS.md +0 -22
  148. package/skills/writing-for-agents/SKILL.md +0 -81
  149. package/skills/writing-for-agents/agents/openai.yaml +0 -3
package/dist/files.js CHANGED
@@ -1,42 +1,144 @@
1
- import { createHash } from 'node:crypto';
2
- import { mkdir, writeFile } from 'node:fs/promises';
3
- import { join } from 'node:path';
4
- import { HouseRefusal, retrying } from './api.js';
1
+ import { createHash, randomUUID } from 'node:crypto';
2
+ import { createWriteStream } from 'node:fs';
3
+ import { mkdir, rename, rm } from 'node:fs/promises';
4
+ import { basename, dirname, join } from 'node:path';
5
+ import { Readable, Transform } from 'node:stream';
6
+ import { pipeline } from 'node:stream/promises';
7
+ import { parseDocument } from 'yaml';
8
+ import { retryDelay } from './api.js';
9
+ import { Unsettled } from './mcp.js';
5
10
  import { relayed } from './relay.js';
6
- export function verified(bytes, sha256, name) {
7
- if (createHash('sha256').update(bytes).digest('hex') !== sha256)
8
- throw new Error(`${name} did not arrive whole; download it again`);
9
- return bytes;
10
- }
11
- export async function placeFiles(house, directory, files, signal) {
12
- const paths = [];
13
- for (const file of files) {
14
- const original = await retrying(() => house.post('/kit/conversation/originals/get', { version: file.version, download: true }, signal), signal);
15
- const answer = await fetch(relayed(original.download.url), { signal });
16
- if (!answer.ok)
17
- throw new Error(`${file.name} did not download: ${answer.status}`);
18
- const folder = join(directory, '.house', 'files', original.message, file.version);
19
- await mkdir(join(directory, '.house')).catch((error) => {
20
- if (error.code !== 'EEXIST')
11
+ const STALLED_ROUNDS = 8;
12
+ const PART_CHECKSUM = 'x-amz-checksum-sha256';
13
+ const LINK = /\]\((\/rooms\/[^/)\s]+\/[^)\s]+)\)/gu;
14
+ export function linkedPaths(text) {
15
+ return [...new Set(Array.from(text.matchAll(LINK), (link) => link[1]))];
16
+ }
17
+ export function inspectedFile(answer) {
18
+ const document = parseDocument(answer);
19
+ if (document.errors.length > 0)
20
+ return null;
21
+ const inspected = document.toJS();
22
+ if (typeof inspected?.download !== 'string')
23
+ return null;
24
+ return {
25
+ download: inspected.download,
26
+ sha256: String(inspected.sha256),
27
+ media_type: typeof inspected.media_type === 'string' ? inspected.media_type : null,
28
+ };
29
+ }
30
+ export function paused(milliseconds) {
31
+ return new Promise((resolve) => setTimeout(resolve, milliseconds));
32
+ }
33
+ export function storeRefusal(act, status) {
34
+ return new Error(`the ${act} answered ${status}; ${act} the file again`);
35
+ }
36
+ async function partDigests(uploading, partBytes) {
37
+ const whole = createHash('sha256');
38
+ const digests = [];
39
+ for (let start = 0; digests.length === 0 || start < uploading.bytes; start += partBytes) {
40
+ const end = Math.min(start + partBytes, uploading.bytes);
41
+ const digest = createHash('sha256');
42
+ for await (const chunk of uploading.read(start, end)) {
43
+ digest.update(chunk);
44
+ whole.update(chunk);
45
+ }
46
+ digests.push(digest.digest());
47
+ }
48
+ if (whole.digest('hex') !== uploading.sha256)
49
+ throw new Error(`${uploading.name} changed while it was uploaded; upload it again`);
50
+ return digests;
51
+ }
52
+ export async function uploaded(declared, uploading) {
53
+ const partBytes = declared.part_bytes;
54
+ const digests = await partDigests(uploading, partBytes);
55
+ const partSha256 = digests.map((digest) => digest.toString('hex'));
56
+ let answer = await finishing(uploading, declared.id, partSha256);
57
+ let idle = 0;
58
+ let stalled = 0;
59
+ while ('upload' in answer) {
60
+ const { id, parts } = answer.upload;
61
+ let sent = 0;
62
+ let failure = null;
63
+ for (const { part, url } of parts ?? []) {
64
+ const start = (part - 1) * partBytes;
65
+ try {
66
+ await uploading.send(url, start, Math.min(start + partBytes, uploading.bytes), {
67
+ [PART_CHECKSUM]: digests[part - 1].toString('base64'),
68
+ });
69
+ sent++;
70
+ }
71
+ catch (error) {
72
+ failure = error;
73
+ break;
74
+ }
75
+ }
76
+ if (sent > 0) {
77
+ idle = 0;
78
+ stalled = 0;
79
+ }
80
+ else {
81
+ if (failure !== null && ++stalled >= STALLED_ROUNDS)
82
+ throw failure;
83
+ // A round that sent no part, its store unreachable, is followed by the next finishing call on the jittered curve.
84
+ await paused(retryDelay(idle++));
85
+ }
86
+ answer = await finishing(uploading, id, partSha256);
87
+ }
88
+ return answer;
89
+ }
90
+ async function finishing(uploading, upload, partSha256) {
91
+ for (let unsettled = 1;; unsettled++) {
92
+ try {
93
+ return await uploading.finish(upload, partSha256);
94
+ }
95
+ catch (error) {
96
+ if (!(error instanceof Unsettled) || unsettled === STALLED_ROUNDS)
21
97
  throw error;
22
- });
23
- await mkdir(folder, { recursive: true });
24
- const path = join(folder, file.name);
25
- await writeFile(path, verified(Buffer.from(await answer.arrayBuffer()), file.sha256, file.name));
26
- paths.push(path);
98
+ }
99
+ // A finishing call House did not settle, unreached or refused short of final, is sent again on the jittered curve as a conversation resends it, so a dropped connection or a House restart resumes the upload instead of ending it.
100
+ await paused(retryDelay(unsettled - 1));
27
101
  }
28
- return paths;
29
- }
30
- export function uploadBytes(upload, bytes) {
31
- return retrying(async () => {
32
- const answer = await fetch(relayed(upload.url), {
33
- method: 'POST',
34
- headers: { 'content-type': 'application/octet-stream', 'x-house-byte-operation': upload.operation },
35
- body: bytes,
102
+ }
103
+ async function drained(body) {
104
+ for await (const _chunk of body)
105
+ ;
106
+ }
107
+ export async function downloaded(address, fetching, destination, name) {
108
+ for (let asked = 1;; asked++) {
109
+ const { download, sha256 } = await address();
110
+ const answer = await fetching(download);
111
+ if (answer.status === 403 && asked === 1) {
112
+ await drained(answer.body);
113
+ continue;
114
+ }
115
+ if (answer.status !== 200) {
116
+ await drained(answer.body);
117
+ throw storeRefusal('download', answer.status);
118
+ }
119
+ await mkdir(dirname(destination), { recursive: true });
120
+ const partial = join(dirname(destination), `.${basename(destination)}.${randomUUID()}`);
121
+ const digest = createHash('sha256');
122
+ const hashed = new Transform({
123
+ transform(chunk, _encoding, done) {
124
+ digest.update(chunk);
125
+ done(null, chunk);
126
+ },
36
127
  });
37
- const text = await answer.text();
38
- if (!answer.ok)
39
- throw new HouseRefusal(upload.url, answer.status, text);
40
- return JSON.parse(text);
41
- });
128
+ try {
129
+ await pipeline(answer.body, hashed, createWriteStream(partial, { flags: 'wx' }));
130
+ if (digest.digest('hex') !== sha256)
131
+ throw new Error(`${name} did not arrive whole; download it again`);
132
+ await rename(partial, destination);
133
+ }
134
+ catch (error) {
135
+ await rm(partial, { force: true });
136
+ throw error;
137
+ }
138
+ return;
139
+ }
140
+ }
141
+ export async function fetched(url, signal) {
142
+ const answer = await fetch(relayed(url), { signal });
143
+ return { status: answer.status, body: answer.body === null ? Readable.from([]) : Readable.fromWeb(answer.body) };
42
144
  }
package/dist/git.js CHANGED
@@ -2,16 +2,16 @@ import { spawn } from 'node:child_process';
2
2
  import { isUtf8 } from 'node:buffer';
3
3
  import { createHash } from 'node:crypto';
4
4
  import { join } from 'node:path';
5
- import { withoutProxy } from './clis.js';
6
5
  import { workspaceRoot } from './home.js';
6
+ import { childEnvironment } from './relay.js';
7
7
  export function localCopies() {
8
8
  return join(workspaceRoot(), 'local-copies');
9
9
  }
10
10
  export const HOUSE_REF = 'refs/house/received';
11
- const COMMITTER = 'House Kit <house-kit@localhost>';
11
+ const COMMITTER = 'Kit <house-kit@localhost>';
12
12
  export function run(cwd, args, input, env = {}) {
13
13
  return new Promise((resolve, reject) => {
14
- const child = spawn('git', args, { cwd, env: { ...withoutProxy(), ...env } });
14
+ const child = spawn('git', args, { cwd, env: { ...childEnvironment(), ...env } });
15
15
  const stdout = [];
16
16
  let stderr = '';
17
17
  child.stdout.on('data', (chunk) => stdout.push(chunk));
@@ -70,18 +70,52 @@ function data(text) {
70
70
  const bytes = Buffer.from(text, 'utf8');
71
71
  return [Buffer.from(`data ${bytes.length}\n`), bytes, Buffer.from('\n')];
72
72
  }
73
- export async function importCommits(repository, commits) {
74
- const stream = [];
73
+ function written(stream, bytes) {
74
+ return new Promise((resolve, reject) => {
75
+ stream.write(bytes, (error) => (error ? reject(error) : resolve()));
76
+ });
77
+ }
78
+ async function imported(stream, commits) {
79
+ await written(stream, Buffer.from('feature done\n'));
75
80
  for (const [index, commit] of commits.entries()) {
76
- stream.push(Buffer.from(`commit ${HOUSE_REF}\nmark :${index + 1}\ncommitter ${COMMITTER} ${Math.floor(Date.now() / 1000)} +0000\n`), ...data(commit.message), ...(commit.from === null ? [] : [Buffer.from(`from ${commit.from}\n`)]), ...(commit.merge === undefined ? [] : [Buffer.from(`merge ${commit.merge}\n`)]), ...(commit.replaces === true ? [Buffer.from('deleteall\n')] : []));
77
- for (const [path, content] of commit.files) {
78
- if (content === null)
79
- stream.push(Buffer.from(`D ${quoted(path)}\n`));
80
- else
81
- stream.push(Buffer.from(`M 100644 inline ${quoted(path)}\n`), ...data(content));
81
+ await written(stream, Buffer.concat([
82
+ Buffer.from(`commit ${HOUSE_REF}\nmark :${index + 1}\ncommitter ${COMMITTER} ${Math.floor(Date.now() / 1000)} +0000\n`),
83
+ ...data(commit.message),
84
+ ...(commit.from === null ? [] : [Buffer.from(`from ${commit.from}\n`)]),
85
+ ...(commit.merge === undefined ? [] : [Buffer.from(`merge ${commit.merge}\n`)]),
86
+ ...(commit.replaces === true ? [Buffer.from('deleteall\n')] : []),
87
+ ]));
88
+ for await (const [path, content] of commit.files) {
89
+ await written(stream, content === null
90
+ ? Buffer.from(`D ${quoted(path)}\n`)
91
+ : Buffer.concat([Buffer.from(`M 100644 inline ${quoted(path)}\n`), ...data(content)]));
82
92
  }
83
- stream.push(Buffer.from('\n'));
93
+ await written(stream, Buffer.from('\n'));
94
+ }
95
+ await written(stream, Buffer.from('done\n'));
96
+ }
97
+ export async function importCommits(repository, commits) {
98
+ const child = spawn('git', ['fast-import', '--quiet', '--force'], { cwd: repository, env: childEnvironment() });
99
+ let stderr = '';
100
+ child.stdout.resume();
101
+ child.stderr.on('data', (chunk) => {
102
+ stderr += chunk.toString('utf8');
103
+ });
104
+ child.stdin.on('error', () => undefined);
105
+ const closed = new Promise((resolve, reject) => {
106
+ child.on('error', reject);
107
+ child.on('close', resolve);
108
+ });
109
+ try {
110
+ await imported(child.stdin, commits);
111
+ child.stdin.end();
112
+ }
113
+ catch (error) {
114
+ child.stdin.destroy();
115
+ await closed;
116
+ throw error.code === 'EPIPE' ? new Error(`git fast-import failed: ${stderr.trim()}`) : error;
84
117
  }
85
- await git(repository, ['fast-import', '--quiet', '--force'], Buffer.concat(stream));
118
+ if ((await closed) !== 0)
119
+ throw new Error(`git fast-import failed: ${stderr.trim()}`);
86
120
  return revision(repository, HOUSE_REF);
87
121
  }
package/dist/grok.js ADDED
@@ -0,0 +1,39 @@
1
+ const PLAN = 'plan';
2
+ const PLAN_ABANDONED = 'abandoned';
3
+ const PLAN_FIELD = {
4
+ type: 'string',
5
+ title: 'Approve this plan?',
6
+ oneOf: [
7
+ { const: 'approved', title: 'Approve' },
8
+ { const: PLAN_ABANDONED, title: 'Abandon' },
9
+ ],
10
+ };
11
+ function form(sessionId, message, properties) {
12
+ return { mode: 'form', sessionId, message, requestedSchema: { type: 'object', properties } };
13
+ }
14
+ function questionField({ options, multiSelect }, title) {
15
+ const choices = options.map(({ label, description }) => ({ const: label, title: label, description }));
16
+ return multiSelect === true ? { type: 'array', title, items: { anyOf: choices } } : { type: 'string', title, oneOf: choices };
17
+ }
18
+ export function grokQuestionForm({ sessionId, questions }) {
19
+ const lone = questions.length === 1;
20
+ return form(sessionId, questions.map(({ question }) => question).join('\n\n'), Object.fromEntries(questions.map((one) => [one.question, questionField(one, lone ? 'Answer' : one.question)])));
21
+ }
22
+ function answerText(value) {
23
+ return Array.isArray(value) ? value.join(', ') : String(value);
24
+ }
25
+ function contentOf(response) {
26
+ return response.action === 'accept' ? (response.content ?? {}) : {};
27
+ }
28
+ export function grokQuestionAnswer(response) {
29
+ if (response.action !== 'accept')
30
+ return { outcome: 'cancelled' };
31
+ const answers = Object.entries(contentOf(response)).map(([question, value]) => [question, answerText(value)]);
32
+ return { outcome: 'accepted', answers: Object.fromEntries(answers) };
33
+ }
34
+ export function grokPlanForm({ sessionId, planContent }) {
35
+ return form(sessionId, planContent, { [PLAN]: PLAN_FIELD });
36
+ }
37
+ export function grokPlanAnswer(response) {
38
+ return { outcome: contentOf(response)[PLAN] ?? PLAN_ABANDONED };
39
+ }
package/dist/home.js CHANGED
@@ -8,7 +8,6 @@ const ENROLMENT = 'credential.json';
8
8
  const DISCONNECTED = 'disconnected.json';
9
9
  const CONFIGURATION = 'kit.json';
10
10
  const OWN_AGENT = 'own-agent.json';
11
- const RESTART = 'restart';
12
11
  let defaulted;
13
12
  function writable(directory) {
14
13
  try {
@@ -132,12 +131,6 @@ export async function forgetRejected(rejected) {
132
131
  export function enrolled() {
133
132
  return existsSync(join(kitHome(), ENROLMENT));
134
133
  }
135
- export function restartAsked() {
136
- return existsSync(join(kitHome(), RESTART));
137
- }
138
- export function forgetRestart() {
139
- return rm(join(kitHome(), RESTART), { force: true });
140
- }
141
134
  export function readOwnAgent() {
142
135
  return readHome(OWN_AGENT);
143
136
  }
package/dist/house-cli.js CHANGED
@@ -1,14 +1,17 @@
1
1
  import { createHash } from 'node:crypto';
2
- import { mkdir, readFile, writeFile } from 'node:fs/promises';
3
- import { request, setGlobalProxyFromEnv } from 'node:http';
4
- import { basename, dirname, extname, join, resolve } from 'node:path';
5
- import { verified } from './files.js';
2
+ import { createReadStream } from 'node:fs';
3
+ import { request } from 'node:http';
4
+ import { request as httpsRequest } from 'node:https';
5
+ import { extname, join, resolve } from 'node:path';
6
+ import { Readable } from 'node:stream';
7
+ import { pipeline } from 'node:stream/promises';
8
+ import { downloaded, inspectedFile, storeRefusal, uploaded } from './files.js';
6
9
  import { HOUSE_REF } from './git.js';
7
10
  import { ownerSocket, readOwnAgent } from './home.js';
8
- import { callHouse, rpc, UNREACHABLE } from './mcp.js';
9
- import { refusalOf } from './refusals.js';
10
- import { relayed, selectRelay } from './relay.js';
11
- import { parse, stringify } from 'yaml';
11
+ import { callHouse, rpc, UNREACHABLE, Unsettled } from './mcp.js';
12
+ import { refusalOf, retryable } from './refusals.js';
13
+ import { proxyUnlessRelayed, relayed, selectRelay } from './relay.js';
14
+ import { parse } from 'yaml';
12
15
  const USAGE = "usage: house <tool> ['<arguments as JSON>']";
13
16
  const PUSH_USAGE = 'usage: house push [commit] [--owner]';
14
17
  const PUSH = "push [commit]: sends the Local copy's committed changes to House";
@@ -20,8 +23,8 @@ const ARGUMENTS = `the arguments are one JSON object in single quotes, like hous
20
23
  const CLOSED = "this conversation's House connection is closed; nothing to do from here";
21
24
  const SANDBOXED = "your CLI's sandbox kept house from House; run house directly, not through another program";
22
25
  const REVOKED = 'this conversation no longer has House access; nothing to do from here';
23
- const UNCONNECTED = "this computer's own agent has no House connection; run kit login, or kit chatgpt --key <key> with a script key";
24
- const DISCONNECTED = "House no longer accepts this computer's own agent; run kit login, or kit chatgpt --key <key> with a script key";
26
+ const UNCONNECTED = "this computer's own agent has no House connection; run kit login";
27
+ const DISCONNECTED = "House no longer accepts this computer's own agent; run kit login";
25
28
  const UNCONFIRMED = 'with no result; a write may or may not have taken effect, so read before you repeat it';
26
29
  const socketPath = process.env.HOUSE_BRIDGE;
27
30
  const MEDIA_TYPES = new Map([
@@ -51,15 +54,12 @@ const MEDIA_TYPES = new Map([
51
54
  ['xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
52
55
  ['zip', 'application/zip'],
53
56
  ]);
54
- function refused(path, method, status, text, local) {
57
+ function refused(path, status, text, local) {
55
58
  const refusal = refusalOf(text);
56
59
  if (refusal !== null)
57
60
  return refusal.text;
58
61
  if (status === 401)
59
62
  return REVOKED;
60
- const act = method === 'GET' ? 'download' : 'upload';
61
- if (URL.canParse(path))
62
- return `the ${act} answered ${status}; ${act} the file again`;
63
63
  if (status === 502 && local)
64
64
  return text;
65
65
  return path === '/' ? unconfirmed(status) : `${path} answered ${status}`;
@@ -67,38 +67,37 @@ function refused(path, method, status, text, local) {
67
67
  function unconfirmed(status) {
68
68
  return `House answered ${status} ${UNCONFIRMED}`;
69
69
  }
70
- function exchanged(path, method, body, headers = { 'content-type': 'application/json' }, to = socketPath) {
70
+ function bridged(path, body, to = socketPath) {
71
71
  return new Promise((resolve, reject) => {
72
- const sent = request({ socketPath: to, path, method, headers }, (answer) => {
72
+ const sent = request({ socketPath: to, path, method: 'POST', headers: { 'content-type': 'application/json' } }, (answer) => {
73
73
  const chunks = [];
74
74
  answer.on('data', (chunk) => chunks.push(chunk));
75
75
  answer.on('end', () => {
76
- const bytes = Buffer.concat(chunks);
76
+ const text = Buffer.concat(chunks).toString('utf8');
77
77
  if (answer.statusCode === 200)
78
- resolve(bytes);
78
+ resolve(text);
79
79
  else
80
- reject(new Error(refused(path, method, answer.statusCode, bytes.toString('utf8'), answer.headers['content-type'] === undefined)));
80
+ reject(new Error(refused(path, answer.statusCode, text, answer.headers['content-type'] === undefined)));
81
81
  });
82
82
  });
83
83
  sent.on('error', (error) => reject(new Error(error.code === 'EPERM' || error.code === 'EACCES' ? SANDBOXED : CLOSED)));
84
- sent.end(body === undefined ? undefined : body instanceof Buffer ? body : JSON.stringify(body));
84
+ sent.end(JSON.stringify(body));
85
85
  });
86
86
  }
87
- async function bridged(path, body, headers, to = socketPath) {
88
- return (await exchanged(path, 'POST', body, headers, to)).toString('utf8');
89
- }
90
87
  async function direct(message) {
91
88
  const ownAgent = await readOwnAgent();
92
89
  if (ownAgent === null)
93
90
  throw new Error(UNCONNECTED);
94
91
  selectRelay(process.env.HOUSE_KIT_RELAY || ownAgent.relay);
95
- setGlobalProxyFromEnv();
96
92
  const answer = await callHouse(ownAgent.house, ownAgent.credential, message).catch(() => {
97
- throw new Error(UNREACHABLE);
93
+ throw new Unsettled(UNREACHABLE);
98
94
  });
99
95
  if (answer.status === 200)
100
96
  return answer.text;
101
- throw new Error(answer.status === 401 ? DISCONNECTED : (refusalOf(answer.text)?.text ?? unconfirmed(answer.status)));
97
+ if (answer.status === 401)
98
+ throw new Error(DISCONNECTED);
99
+ const refusal = refusalOf(answer.text)?.text ?? unconfirmed(answer.status);
100
+ throw retryable(answer.text) === false ? new Error(refusal) : new Unsettled(refusal);
102
101
  }
103
102
  async function mcp(method, params) {
104
103
  const message = rpc(method, params);
@@ -110,31 +109,43 @@ async function mcp(method, params) {
110
109
  async function tools() {
111
110
  return (await mcp('tools/list', {})).tools;
112
111
  }
113
- async function localFile(path) {
112
+ function unreadable(path, error) {
113
+ return new Error(error.code === 'EISDIR'
114
+ ? `${path} is a folder; upload one file at a time`
115
+ : `${path} is not a readable file`);
116
+ }
117
+ function opened(url, method, headers, body) {
118
+ return new Promise((answered, failed) => {
119
+ const local = socketPath !== undefined;
120
+ const target = local ? null : relayed(url);
121
+ if (!local)
122
+ proxyUnlessRelayed();
123
+ const sent = target === null
124
+ ? request({ socketPath, path: url, method, headers }, answered)
125
+ : (target.protocol === 'https:' ? httpsRequest : request)(target, { method, headers }, answered);
126
+ sent.on('error', (error) => failed(new Error(!local ? UNREACHABLE : error.code === 'EPERM' || error.code === 'EACCES' ? SANDBOXED : CLOSED)));
127
+ if (body === undefined)
128
+ sent.end();
129
+ else
130
+ pipeline(body, sent).catch(() => undefined);
131
+ });
132
+ }
133
+ function ranged(file, start, end) {
134
+ return end === start ? undefined : createReadStream(file, { start, end: end - 1 });
135
+ }
136
+ async function* read(file, start, end) {
114
137
  try {
115
- return await readFile(path);
138
+ yield* ranged(file, start, end) ?? [];
116
139
  }
117
140
  catch (error) {
118
- throw new Error(error.code === 'EISDIR'
119
- ? `${path} is a folder; upload one file at a time`
120
- : `${path} is not a readable file`);
141
+ throw unreadable(file, error);
121
142
  }
122
143
  }
123
- async function transferred(url, method, bytes, headers) {
124
- if (socketPath !== undefined)
125
- return exchanged(url, method, bytes, headers);
126
- setGlobalProxyFromEnv();
127
- const answer = await fetch(relayed(url), { method, ...(headers === undefined ? {} : { headers }), ...(bytes === undefined ? {} : { body: bytes }) }).catch(() => {
128
- throw new Error(UNREACHABLE);
129
- });
130
- const received = Buffer.from(await answer.arrayBuffer());
131
- if (answer.status === 200)
132
- return received;
133
- throw new Error(refused(url, method, answer.status, received.toString('utf8'), false));
134
- }
135
- async function sent(url, bytes, operation) {
136
- const headers = { 'content-type': 'application/octet-stream', 'x-house-byte-operation': operation };
137
- return (await transferred(url, 'POST', bytes, headers)).toString('utf8');
144
+ async function sentPart(url, file, start, end, headers) {
145
+ const answer = await opened(url, 'PUT', { ...headers, 'content-length': String(end - start) }, ranged(file, start, end));
146
+ await answer.toArray();
147
+ if (answer.statusCode !== 200)
148
+ throw storeRefusal('upload', answer.statusCode);
138
149
  }
139
150
  async function download(args) {
140
151
  const { flags, positionals: [argument] } = accepted(args, ['--help'], 1, DOWNLOAD_USAGE);
@@ -143,58 +154,55 @@ async function download(args) {
143
154
  const { file, ...selected } = argumentsOf(argument);
144
155
  if (typeof file !== 'string' || file === '')
145
156
  throw new Error('download needs "file", the local file to write');
146
- const inspected = parse(await called('inspect', selected));
147
- if (typeof inspected?.download !== 'string')
148
- throw new Error(`${String(selected.path)} holds no binary file; shell reads its text`);
149
- const bytes = verified(await transferred(inspected.download, 'GET'), String(inspected.sha256), String(selected.path));
150
157
  const written = resolve(file);
151
- await mkdir(dirname(written), { recursive: true });
152
- await writeFile(written, bytes);
158
+ await downloaded(async () => {
159
+ const file = inspectedFile(await called('inspect', selected));
160
+ if (file === null)
161
+ throw new Error(`${String(selected.path)} holds no binary file; shell reads its text`);
162
+ return file;
163
+ }, async (url) => {
164
+ const answer = await opened(url, 'GET', {});
165
+ return { status: answer.statusCode, body: answer };
166
+ }, written, String(selected.path));
153
167
  return written;
154
168
  }
155
169
  async function described(file) {
156
- const bytes = await localFile(file);
170
+ const digest = createHash('sha256');
171
+ let bytes = 0;
172
+ try {
173
+ for await (const chunk of createReadStream(file)) {
174
+ digest.update(chunk);
175
+ bytes += chunk.length;
176
+ }
177
+ }
178
+ catch (error) {
179
+ throw unreadable(file, error);
180
+ }
157
181
  const mediaType = MEDIA_TYPES.get(extname(file).slice(1).toLowerCase());
158
- return {
159
- bytes,
160
- description: {
161
- bytes: bytes.length,
162
- sha256: createHash('sha256').update(bytes).digest('hex'),
163
- ...(mediaType === undefined ? {} : { media_type: mediaType }),
164
- },
165
- };
182
+ return { bytes, sha256: digest.digest('hex'), ...(mediaType === undefined ? {} : { media_type: mediaType }) };
183
+ }
184
+ async function finished(upload, room, partSha256) {
185
+ const text = await called('upload', { upload, room_ref: room, part_sha256: partSha256 });
186
+ const answer = parse(text);
187
+ return answer !== null && 'upload' in answer ? answer : { text };
166
188
  }
167
189
  async function upload({ file, ...destination }) {
168
190
  if (typeof file !== 'string' || file === '')
169
191
  throw new Error('upload needs "file", a local file');
170
- const { bytes, description } = await described(file);
192
+ const description = await described(file);
171
193
  const answer = await called('upload', { ...destination, ...description });
172
194
  const declared = parse(answer);
173
- if (declared.upload === undefined)
195
+ if (declared?.upload === undefined)
174
196
  return answer;
175
- const written = JSON.parse(await sent(declared.upload.url, bytes, declared.upload.operation));
176
- return stringify(written, { lineWidth: 0 }).trimEnd();
177
- }
178
- async function appendRecord(args) {
179
- const held = new Map();
180
- const attachments = Array.isArray(args.attachments)
181
- ? await Promise.all(args.attachments.map(async (attachment) => {
182
- if (typeof attachment !== 'string')
183
- return attachment;
184
- const { bytes, description } = await described(attachment);
185
- held.set(description.sha256, bytes);
186
- return { name: basename(attachment), ...description };
187
- }))
188
- : args.attachments;
189
- const record = { ...args, attachments };
190
- for (;;) {
191
- const answer = await called('append_record', record);
192
- const { upload } = parse(answer);
193
- if (upload === undefined)
194
- return answer;
195
- for (const grant of upload)
196
- await sent(grant.url, held.get(grant.sha256), grant.operation);
197
- }
197
+ const accepted = await uploaded(declared.upload, {
198
+ name: file,
199
+ bytes: description.bytes,
200
+ sha256: description.sha256,
201
+ read: (start, end) => read(file, start, end),
202
+ send: (url, start, end, headers) => sentPart(url, file, start, end, headers),
203
+ finish: (id, partSha256) => finished(id, destination.room_ref, partSha256),
204
+ });
205
+ return accepted.text;
198
206
  }
199
207
  function unexpected(argument, usage) {
200
208
  return new Error(`unexpected argument ${argument}; ${usage}`);
@@ -220,7 +228,7 @@ async function push(args) {
220
228
  throw new Error('--owner is not for Agent conversations; run house push without it');
221
229
  if (!owner && socketPath === undefined)
222
230
  throw new Error('outside an Agent conversation, house push needs --owner');
223
- const pushed = JSON.parse(await bridged('/git/push', { cwd: process.cwd(), commit }, undefined, owner ? ownerSocket() : socketPath));
231
+ const pushed = JSON.parse(await bridged('/git/push', { cwd: process.cwd(), commit }, owner ? ownerSocket() : socketPath));
224
232
  if (pushed.refused)
225
233
  throw new Error(pushed.text);
226
234
  return pushed.text;
@@ -228,7 +236,7 @@ async function push(args) {
228
236
  async function fetched(args) {
229
237
  if (accepted(args, ['--help'], 0, FETCH_USAGE).flags.has('--help'))
230
238
  return FETCH_USAGE;
231
- const answer = JSON.parse(await bridged('/git/fetch', {}, undefined, socketPath ?? ownerSocket()));
239
+ const answer = JSON.parse(await bridged('/git/fetch', {}, socketPath ?? ownerSocket()));
232
240
  if (answer.refused)
233
241
  throw new Error(answer.text);
234
242
  return answer.text;
@@ -263,7 +271,7 @@ async function house(args) {
263
271
  return [...(tool.description === undefined ? [] : [tool.description]), JSON.stringify(tool.inputSchema)].join('\n');
264
272
  }
265
273
  const parsed = argumentsOf(argument);
266
- return verb === 'upload' ? upload(parsed) : verb === 'append_record' ? appendRecord(parsed) : called(verb, parsed);
274
+ return verb === 'upload' ? upload(parsed) : called(verb, parsed);
267
275
  }
268
276
  async function called(name, args) {
269
277
  const result = await mcp('tools/call', { name, arguments: args });
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
- const [major = 0, minor = 0] = process.versions.node.split('.').map(Number);
3
- if (major < 24 || (major === 24 && minor < 14)) {
4
- process.stderr.write(`house: the Kit needs Node 24.14.0 or newer; this is ${process.versions.node}\n`);
2
+ const [major = 0, minor = 0, patch = 0] = process.versions.node.split('.').map(Number);
3
+ if (major < 24 || (major === 24 && (minor < 22 || (minor === 22 && patch < 1)))) {
4
+ process.stderr.write(`house: the Kit needs Node 24.22.1 or newer; this is ${process.versions.node}\n`);
5
5
  process.exitCode = 1;
6
6
  }
7
7
  else {