@agentshouse/kit 0.1.0-alpha.4 → 0.1.0-alpha.41

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 (150) hide show
  1. package/README.md +41 -1671
  2. package/bin/connect-linux.sh +501 -53
  3. package/bin/connect-windows.ps1 +440 -0
  4. package/bin/kit.js +200 -159
  5. package/bootstrap.json +28 -0
  6. package/docs/configuration.md +104 -0
  7. package/docs/google.md +33 -0
  8. package/docs/linux.md +77 -0
  9. package/docs/macos.md +26 -0
  10. package/docs/telegram.md +29 -0
  11. package/docs/windows.md +37 -0
  12. package/package.json +7 -2
  13. package/src/adapter-mcp.js +92 -12
  14. package/src/agents.js +63 -8
  15. package/src/binding.js +52 -0
  16. package/src/blocks.js +2 -26
  17. package/src/bridge.js +28 -14
  18. package/src/bytes.js +19 -8
  19. package/src/configuration.js +14 -211
  20. package/src/connector-login.js +3 -84
  21. package/src/contract.js +44 -12
  22. package/src/conversation-originals.js +1 -3
  23. package/src/delivery.js +4 -7
  24. package/src/directory.js +6 -26
  25. package/src/draft.js +4 -82
  26. package/src/git-main.js +138 -0
  27. package/src/git.js +62 -0
  28. package/src/google/calendar.js +39 -45
  29. package/src/google/drive.js +61 -186
  30. package/src/google/entries.js +349 -0
  31. package/src/google/gateway.js +3 -6
  32. package/src/google/gmail.js +33 -23
  33. package/src/google/oauth.js +106 -66
  34. package/src/google/source.js +21 -23
  35. package/src/host.js +6 -2
  36. package/src/house-main.js +65 -8
  37. package/src/house.js +446 -127
  38. package/src/how-we-work.md +25 -0
  39. package/src/inbound.js +2 -18
  40. package/src/kit-work-stream.js +120 -0
  41. package/src/launcher.js +46 -10
  42. package/src/local-files.js +3 -11
  43. package/src/login.js +27 -14
  44. package/src/mcp.js +18 -0
  45. package/src/mount.js +9 -18
  46. package/src/original.js +4 -4
  47. package/src/pacing.js +41 -0
  48. package/src/plan.js +37 -45
  49. package/src/repository.js +3 -61
  50. package/src/session-bin/git +2 -0
  51. package/src/setup.js +14 -15
  52. package/src/skill-set/ask-matt/PHASE-BOUNDARIES.md +55 -0
  53. package/src/skill-set/ask-matt/SKILL.md +91 -0
  54. package/src/skill-set/ask-matt/agents/openai.yaml +4 -0
  55. package/src/skill-set/code-review/LICENSE +21 -0
  56. package/src/skill-set/code-review/SKILL.md +89 -0
  57. package/src/skill-set/code-review/agents/openai.yaml +3 -0
  58. package/src/skill-set/codebase-design/DEEPENING.md +37 -0
  59. package/src/skill-set/codebase-design/DESIGN-IT-TWICE.md +44 -0
  60. package/src/skill-set/codebase-design/LICENSE +21 -0
  61. package/src/skill-set/codebase-design/SKILL.md +116 -0
  62. package/src/skill-set/codebase-design/agents/openai.yaml +3 -0
  63. package/src/skill-set/diagnosing-bugs/LICENSE +21 -0
  64. package/src/skill-set/diagnosing-bugs/SKILL.md +140 -0
  65. package/src/skill-set/diagnosing-bugs/agents/openai.yaml +3 -0
  66. package/src/skill-set/diagnosing-bugs/scripts/hitl-loop.template.sh +44 -0
  67. package/src/skill-set/domain-modeling/ADR-FORMAT.md +47 -0
  68. package/src/skill-set/domain-modeling/CONTEXT-FORMAT.md +60 -0
  69. package/src/skill-set/domain-modeling/LICENSE +21 -0
  70. package/src/skill-set/domain-modeling/SKILL.md +76 -0
  71. package/src/skill-set/domain-modeling/agents/openai.yaml +3 -0
  72. package/src/skill-set/grill-me/LICENSE +21 -0
  73. package/src/skill-set/grill-me/SKILL.md +8 -0
  74. package/src/skill-set/grill-me/agents/openai.yaml +4 -0
  75. package/src/skill-set/grill-with-docs/LICENSE +21 -0
  76. package/src/skill-set/grill-with-docs/SKILL.md +8 -0
  77. package/src/skill-set/grill-with-docs/agents/openai.yaml +4 -0
  78. package/src/skill-set/grilling/LICENSE +21 -0
  79. package/src/{starter-set → skill-set}/grilling/SKILL.md +1 -1
  80. package/src/skill-set/handoff/LICENSE +21 -0
  81. package/src/skill-set/handoff/SKILL.md +17 -0
  82. package/src/skill-set/handoff/agents/openai.yaml +4 -0
  83. package/src/skill-set/implement/LICENSE +21 -0
  84. package/src/skill-set/implement/SKILL.md +16 -0
  85. package/src/skill-set/implement/agents/openai.yaml +4 -0
  86. package/src/skill-set/improve-codebase-architecture/HTML-REPORT.md +123 -0
  87. package/src/skill-set/improve-codebase-architecture/LICENSE +21 -0
  88. package/src/skill-set/improve-codebase-architecture/SKILL.md +72 -0
  89. package/src/skill-set/improve-codebase-architecture/agents/openai.yaml +4 -0
  90. package/src/skill-set/prototype/LICENSE +21 -0
  91. package/src/skill-set/prototype/LOGIC.md +67 -0
  92. package/src/skill-set/prototype/SKILL.md +28 -0
  93. package/src/skill-set/prototype/UI.md +112 -0
  94. package/src/skill-set/prototype/agents/openai.yaml +3 -0
  95. package/src/skill-set/research/LICENSE +21 -0
  96. package/src/skill-set/research/SKILL.md +14 -0
  97. package/src/skill-set/research/agents/openai.yaml +3 -0
  98. package/src/skill-set/resolving-merge-conflicts/LICENSE +21 -0
  99. package/src/skill-set/resolving-merge-conflicts/SKILL.md +16 -0
  100. package/src/skill-set/resolving-merge-conflicts/agents/openai.yaml +3 -0
  101. package/src/skill-set/tdd/LICENSE +21 -0
  102. package/src/skill-set/tdd/SKILL.md +40 -0
  103. package/src/skill-set/tdd/agents/openai.yaml +3 -0
  104. package/src/skill-set/tdd/mocking.md +59 -0
  105. package/src/skill-set/tdd/tests.md +77 -0
  106. package/src/skill-set/teach/GLOSSARY-FORMAT.md +35 -0
  107. package/src/skill-set/teach/LEARNING-RECORD-FORMAT.md +46 -0
  108. package/src/skill-set/teach/LICENSE +21 -0
  109. package/src/skill-set/teach/MISSION-FORMAT.md +31 -0
  110. package/src/skill-set/teach/RESOURCES-FORMAT.md +32 -0
  111. package/src/skill-set/teach/SKILL.md +141 -0
  112. package/src/skill-set/teach/agents/openai.yaml +4 -0
  113. package/src/skill-set/to-questionnaire/LICENSE +21 -0
  114. package/src/skill-set/to-questionnaire/SKILL.md +55 -0
  115. package/src/skill-set/to-questionnaire/agents/openai.yaml +4 -0
  116. package/src/skill-set/to-spec/LICENSE +21 -0
  117. package/src/skill-set/to-spec/SKILL.md +76 -0
  118. package/src/skill-set/to-spec/agents/openai.yaml +4 -0
  119. package/src/skill-set/to-tickets/LICENSE +21 -0
  120. package/src/skill-set/to-tickets/SKILL.md +106 -0
  121. package/src/skill-set/to-tickets/agents/openai.yaml +4 -0
  122. package/src/skill-set/wait-what/LICENSE +21 -0
  123. package/src/skill-set/wait-what/SKILL.md +8 -0
  124. package/src/skill-set/wait-what/agents/openai.yaml +4 -0
  125. package/src/skill-set/wayfinder/LICENSE +21 -0
  126. package/src/skill-set/wayfinder/SKILL.md +129 -0
  127. package/src/skill-set/wayfinder/agents/openai.yaml +4 -0
  128. package/src/skill-set/writing-for-agents/LICENSE +21 -0
  129. package/src/skill-set/writing-for-agents/SKILL-MECHANICS.md +22 -0
  130. package/src/skill-set/writing-for-agents/SKILL.md +83 -0
  131. package/src/skill-set/writing-for-agents/agents/openai.yaml +3 -0
  132. package/src/skill-set.js +85 -0
  133. package/src/source.js +93 -54
  134. package/src/state.js +19 -1
  135. package/src/sync-library.js +7 -11
  136. package/src/sync-state.js +12 -3
  137. package/src/sync.js +142 -43
  138. package/src/tasks.js +92 -0
  139. package/src/telegram-view.js +217 -0
  140. package/src/telegram.js +204 -69
  141. package/src/turn-frames.js +69 -0
  142. package/src/worker.js +738 -189
  143. package/src/working-copy.js +1274 -0
  144. package/src/workspace.js +299 -88
  145. package/src/google/declared.js +0 -146
  146. package/src/google/id.js +0 -1
  147. package/src/instance/google.js +0 -1
  148. package/src/starter-set.js +0 -17
  149. /package/src/{starter-set/grilling → skill-set/ask-matt}/LICENSE +0 -0
  150. /package/src/{starter-set → skill-set}/grilling/agents/openai.yaml +0 -0
@@ -84,7 +84,7 @@ async function googleResponse(options, dependencies) {
84
84
  attempt -= 1;
85
85
  continue;
86
86
  }
87
- await revokeGoogleAuthorization(options.directory, options.instance, options.requiredScopes);
87
+ await revokeGoogleAuthorization(options.directory, options.account, options.requiredScopes);
88
88
  throw new KitRefusal("google_authority_revoked");
89
89
  }
90
90
  if (response.status === 403) {
@@ -98,13 +98,10 @@ async function googleResponse(options, dependencies) {
98
98
  throw new KitRefusal("google_provider_unavailable");
99
99
  }
100
100
  if (reasons.includes("insufficientPermissions")) {
101
- await revokeGoogleAuthorization(
102
- options.directory,
103
- options.instance,
104
- options.requiredScopes,
105
- );
101
+ await revokeGoogleAuthorization(options.directory, options.account, options.requiredScopes);
106
102
  throw new KitRefusal("google_scope_revoked");
107
103
  }
104
+ if (reasons.includes("accessNotConfigured")) throw new KitRefusal("api_disabled");
108
105
  throw new KitRefusal("google_provider_unavailable");
109
106
  }
110
107
  if (RETRYABLE_STATUS.has(response.status) && attempt + 1 < RETRY_LIMIT) {
@@ -4,6 +4,7 @@ import { googleJson } from "./gateway.js";
4
4
  import { GOOGLE_SCOPES } from "./oauth.js";
5
5
 
6
6
  const GMAIL_ORIGIN = "https://gmail.googleapis.com";
7
+ const EXCLUDED_LABELS = ["SPAM", "TRASH"];
7
8
 
8
9
  function cursorOf(value) {
9
10
  if (value === null) return { mode: "messages", pageToken: null, historyId: null };
@@ -88,18 +89,36 @@ function normalizedMessage(message) {
88
89
  };
89
90
  }
90
91
 
91
- async function currentHistoryId(common, dependencies) {
92
- const profile = await googleJson(
93
- { ...common, url: `${GMAIL_ORIGIN}/gmail/v1/users/me/profile` },
94
- dependencies,
95
- );
92
+ function common(options) {
93
+ return {
94
+ directory: options.directory,
95
+ account: options.account,
96
+ requiredScopes: [GOOGLE_SCOPES.gmail],
97
+ signal: options.signal,
98
+ };
99
+ }
100
+
101
+ async function profileOf(shared, dependencies) {
102
+ return googleJson({ ...shared, url: `${GMAIL_ORIGIN}/gmail/v1/users/me/profile` }, dependencies);
103
+ }
104
+
105
+ async function currentHistoryId(shared, dependencies) {
106
+ const profile = await profileOf(shared, dependencies);
96
107
  if (typeof profile.historyId !== "string" || !/^\d+$/.test(profile.historyId)) {
97
108
  throw new KitRefusal("google_provider_response_invalid", "gmail profile");
98
109
  }
99
110
  return profile.historyId;
100
111
  }
101
112
 
102
- function listUrl(selection, cursor, limit, backfillFloor) {
113
+ export async function gmailEmail(options, dependencies = {}) {
114
+ const profile = await profileOf(common(options), dependencies);
115
+ if (typeof profile.emailAddress !== "string") {
116
+ throw new KitRefusal("google_provider_response_invalid", "gmail profile");
117
+ }
118
+ return profile.emailAddress;
119
+ }
120
+
121
+ function listUrl(cursor, limit, backfillFloor) {
103
122
  const url = new URL(
104
123
  cursor.mode === "messages"
105
124
  ? "/gmail/v1/users/me/messages"
@@ -109,7 +128,6 @@ function listUrl(selection, cursor, limit, backfillFloor) {
109
128
  url.searchParams.set("maxResults", `${limit}`);
110
129
  if (cursor.pageToken !== null) url.searchParams.set("pageToken", cursor.pageToken);
111
130
  if (cursor.mode === "messages") {
112
- for (const label of selection.labels) url.searchParams.append("labelIds", label);
113
131
  if (backfillFloor !== null && backfillFloor !== undefined) {
114
132
  url.searchParams.set("q", `after:${Math.floor(Date.parse(backfillFloor) / 1000)}`);
115
133
  }
@@ -151,22 +169,16 @@ function referencesOf(answer, mode) {
151
169
 
152
170
  export async function gmailPage(options, dependencies = {}) {
153
171
  let cursor = cursorOf(options.cursor);
154
- const common = {
155
- directory: options.directory,
156
- instance: options.instance,
157
- client: options.client,
158
- requiredScopes: [GOOGLE_SCOPES.gmail],
159
- signal: options.signal,
160
- };
172
+ const shared = common(options);
161
173
  if (cursor.mode === "messages" && cursor.historyId === null) {
162
- cursor = { ...cursor, historyId: await currentHistoryId(common, dependencies) };
174
+ cursor = { ...cursor, historyId: await currentHistoryId(shared, dependencies) };
163
175
  }
164
176
  let answer;
165
177
  try {
166
178
  answer = await googleJson(
167
179
  {
168
- ...common,
169
- url: listUrl(options.selection, cursor, options.limit, options.backfillFloor),
180
+ ...shared,
181
+ url: listUrl(cursor, options.limit, options.backfillFloor),
170
182
  ...(cursor.mode === "history" ? { notFoundCode: "google_history_expired" } : {}),
171
183
  },
172
184
  dependencies,
@@ -176,10 +188,10 @@ export async function gmailPage(options, dependencies = {}) {
176
188
  cursor = {
177
189
  mode: "messages",
178
190
  pageToken: null,
179
- historyId: await currentHistoryId(common, dependencies),
191
+ historyId: await currentHistoryId(shared, dependencies),
180
192
  };
181
193
  answer = await googleJson(
182
- { ...common, url: listUrl(options.selection, cursor, options.limit, options.backfillFloor) },
194
+ { ...shared, url: listUrl(cursor, options.limit, options.backfillFloor) },
183
195
  dependencies,
184
196
  );
185
197
  }
@@ -197,16 +209,14 @@ export async function gmailPage(options, dependencies = {}) {
197
209
  try {
198
210
  detail = await googleJson(
199
211
  {
200
- ...common,
212
+ ...shared,
201
213
  notFoundCode: "google_gmail_item_missing",
202
214
  url: `${GMAIL_ORIGIN}/gmail/v1/users/me/messages/${encodeURIComponent(identifier)}?format=full`,
203
215
  },
204
216
  dependencies,
205
217
  );
206
218
  const normalized = normalizedMessage(detail);
207
- if (options.selection.labels.every((label) => detail.labelIds.includes(label))) {
208
- items.push(normalized);
209
- }
219
+ if (!detail.labelIds.some((label) => EXCLUDED_LABELS.includes(label))) items.push(normalized);
210
220
  } catch (error) {
211
221
  if (
212
222
  error instanceof KitRefusal &&
@@ -1,8 +1,10 @@
1
1
  import { createHash, randomBytes, randomInt } from "node:crypto";
2
- import { readFile } from "node:fs/promises";
3
- import { join } from "node:path";
2
+ import { readFile, rm } from "node:fs/promises";
4
3
  import { KitRefusal } from "../refusal.js";
5
- import { writeNote } from "../state.js";
4
+ import { notePath, writeNote } from "../state.js";
5
+ import { exclusively } from "../tasks.js";
6
+
7
+ export const GOOGLE_SOURCES = "google";
6
8
 
7
9
  export const GOOGLE_SCOPES = Object.freeze({
8
10
  gmail: "https://www.googleapis.com/auth/gmail.readonly",
@@ -12,26 +14,30 @@ export const GOOGLE_SCOPES = Object.freeze({
12
14
 
13
15
  const AUTHORIZATION_ENDPOINT = "https://accounts.google.com/o/oauth2/v2/auth";
14
16
  const TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token";
17
+ const REVOCATION_ENDPOINT = "https://oauth2.googleapis.com/revoke";
15
18
  const REFRESH_MARGIN_MS = 60_000;
16
19
  const LOOPBACK_PORT_FLOOR = 49152;
17
20
  const LOOPBACK_PORT_SPAN = 16384;
18
21
 
19
- function credentialPath(directory, instance) {
20
- return join(directory, "state", instance, "credentials", "google.json");
22
+ function credentialPath(directory, account) {
23
+ return notePath(directory, GOOGLE_SOURCES, account, "credentials");
21
24
  }
22
25
 
23
- async function rawAuthorization(directory, instance) {
24
- const path = credentialPath(directory, instance);
26
+ async function rawAccount(directory, account) {
27
+ const path = credentialPath(directory, account);
25
28
  let document;
26
29
  try {
27
30
  document = JSON.parse(await readFile(path, "utf8"));
28
31
  } catch (error) {
29
32
  if (error.code === "ENOENT") return null;
30
- throw new KitRefusal("google_authorization_unreadable", instance);
33
+ throw new KitRefusal("google_authorization_unreadable", account);
31
34
  }
32
35
  if (
33
36
  typeof document !== "object" ||
34
37
  document === null ||
38
+ typeof document.client?.id !== "string" ||
39
+ typeof document.client.secret !== "string" ||
40
+ (document.email !== null && typeof document.email !== "string") ||
35
41
  !Array.isArray(document.grants) ||
36
42
  document.grants.some(
37
43
  (grant) =>
@@ -46,21 +52,27 @@ async function rawAuthorization(directory, instance) {
46
52
  grant.scopes.some((scope) => typeof scope !== "string"),
47
53
  )
48
54
  ) {
49
- throw new KitRefusal("google_authorization_unreadable", instance);
55
+ throw new KitRefusal("google_authorization_unreadable", account);
50
56
  }
51
57
  return document;
52
58
  }
53
59
 
54
- async function writeAuthorization(directory, instance, authorization) {
55
- await writeNote(credentialPath(directory, instance), authorization);
60
+ function updateAccount(directory, account, change) {
61
+ return exclusively(credentialPath(directory, account), async () => {
62
+ const stored = await rawAccount(directory, account);
63
+ const changed = change(stored);
64
+ if (changed !== null) await writeNote(credentialPath(directory, account), changed);
65
+ });
56
66
  }
57
67
 
58
- export async function googleAuthorization(directory, instance) {
59
- const authorization = await rawAuthorization(directory, instance);
60
- if (authorization === null || authorization.grants.length === 0) return null;
68
+ export async function googleAccount(directory, account) {
69
+ const stored = await rawAccount(directory, account);
70
+ if (stored === null) return null;
61
71
  return {
62
- scopes: [...new Set(authorization.grants.flatMap((grant) => grant.scopes))].sort(),
63
- grants: authorization.grants.map((grant) => ({
72
+ client: { id: stored.client.id, secret: stored.client.secret },
73
+ email: stored.email,
74
+ scopes: [...new Set(stored.grants.flatMap((grant) => grant.scopes))].sort(),
75
+ grants: stored.grants.map((grant) => ({
64
76
  accessToken: grant.access_token,
65
77
  refreshToken: grant.refresh_token,
66
78
  expiresAt: grant.expires_at,
@@ -69,6 +81,15 @@ export async function googleAuthorization(directory, instance) {
69
81
  };
70
82
  }
71
83
 
84
+ export async function googleSignedIn(directory, account, service) {
85
+ const stored = await rawAccount(directory, account);
86
+ return stored?.grants.some((grant) => grant.scopes.includes(GOOGLE_SCOPES[service])) === true;
87
+ }
88
+
89
+ export function recordGoogleEmail(directory, account, email) {
90
+ return updateAccount(directory, account, (stored) => (stored === null ? null : { ...stored, email }));
91
+ }
92
+
72
93
  function requiredScopesOf(value) {
73
94
  if (
74
95
  !Array.isArray(value) ||
@@ -80,19 +101,51 @@ function requiredScopesOf(value) {
80
101
  return value;
81
102
  }
82
103
 
83
- export async function revokeGoogleAuthorization(directory, instance, scopes) {
104
+ export async function revokeGoogleAuthorization(directory, account, scopes) {
84
105
  const requiredScopes = requiredScopesOf(scopes);
85
- const authorization = await rawAuthorization(directory, instance);
86
- if (authorization !== null) {
87
- await writeAuthorization(directory, instance, {
88
- grants: authorization.grants
89
- .map((grant) => ({
90
- ...grant,
91
- scopes: grant.scopes.filter((scope) => !requiredScopes.includes(scope)),
92
- }))
93
- .filter((grant) => grant.scopes.length > 0),
106
+ await updateAccount(directory, account, (stored) =>
107
+ stored === null
108
+ ? null
109
+ : {
110
+ ...stored,
111
+ grants: stored.grants
112
+ .map((grant) => ({
113
+ ...grant,
114
+ scopes: grant.scopes.filter((scope) => !requiredScopes.includes(scope)),
115
+ }))
116
+ .filter((grant) => grant.scopes.length > 0),
117
+ },
118
+ );
119
+ }
120
+
121
+ async function revokedAtProvider(fetcher, token) {
122
+ let response;
123
+ try {
124
+ response = await fetcher(REVOCATION_ENDPOINT, {
125
+ method: "POST",
126
+ headers: { "content-type": "application/x-www-form-urlencoded" },
127
+ body: new URLSearchParams({ token }).toString(),
94
128
  });
129
+ } catch {
130
+ throw new KitRefusal("google_provider_unavailable");
95
131
  }
132
+ if (response.status !== 200 && response.status !== 400) {
133
+ throw new KitRefusal("google_provider_unavailable");
134
+ }
135
+ }
136
+
137
+ export async function forgetGoogleAccount(directory, account, kept, dependencies = {}) {
138
+ const stored = await rawAccount(directory, account);
139
+ if (stored === null) return;
140
+ const others = await Promise.all(kept.map((other) => rawAccount(directory, other)));
141
+ if (stored.email === null || !others.some((held) => held?.email === stored.email)) {
142
+ for (const grant of stored.grants) {
143
+ await revokedAtProvider(dependencies.fetch ?? fetch, grant.refresh_token);
144
+ }
145
+ }
146
+ await exclusively(credentialPath(directory, account), () =>
147
+ rm(credentialPath(directory, account), { force: true }),
148
+ );
96
149
  }
97
150
 
98
151
  function oauthValue() {
@@ -167,26 +220,9 @@ async function tokenRequest(fetcher, body) {
167
220
  return answer;
168
221
  }
169
222
 
170
- function configuredClient(client) {
171
- if (
172
- typeof client?.id !== "string" ||
173
- client.id.length === 0 ||
174
- typeof client.secret !== "string" ||
175
- client.secret.length === 0
176
- ) {
177
- throw new KitRefusal("google_oauth_client_unconfigured");
178
- }
179
- return client;
180
- }
181
-
182
223
  export async function authorizeGoogle(options, dependencies = {}) {
183
- const client = configuredClient(options.client);
184
- const stored = await rawAuthorization(options.directory, options.instance);
224
+ const { client } = options;
185
225
  const scopes = [...new Set(options.scopes)];
186
- const covered = stored?.grants.some((grant) =>
187
- scopes.every((scope) => grant.scopes.includes(scope)),
188
- );
189
- if (covered === true) return { status: "skipped", requestedScopes: [] };
190
226
  const verifier = oauthValue();
191
227
  const state = oauthValue();
192
228
  const redirectUri = loopbackRedirect(dependencies.loopbackPort);
@@ -215,10 +251,11 @@ export async function authorizeGoogle(options, dependencies = {}) {
215
251
  }
216
252
  const grantedScopes = [...new Set(returnedScopes.length === 0 ? scopes : returnedScopes)].sort();
217
253
  const key = grantedScopes.join("\n");
218
- const grants = (stored?.grants ?? []).filter((grant) => [...grant.scopes].sort().join("\n") !== key);
219
- await writeAuthorization(options.directory, options.instance, {
254
+ await updateAccount(options.directory, options.account, (stored) => ({
255
+ client: { id: client.id, secret: client.secret },
256
+ email: stored?.email ?? null,
220
257
  grants: [
221
- ...grants,
258
+ ...(stored?.grants ?? []).filter((grant) => [...grant.scopes].sort().join("\n") !== key),
222
259
  {
223
260
  access_token: token.access_token,
224
261
  refresh_token: refreshToken,
@@ -226,48 +263,51 @@ export async function authorizeGoogle(options, dependencies = {}) {
226
263
  scopes: grantedScopes,
227
264
  },
228
265
  ],
229
- });
230
- return { status: "ready", requestedScopes: scopes };
266
+ }));
231
267
  }
232
268
 
233
269
  export async function googleAccessToken(options, dependencies = {}) {
234
- const stored = await rawAuthorization(options.directory, options.instance);
270
+ const stored = await rawAccount(options.directory, options.account);
235
271
  const requiredScopes = requiredScopesOf(options.requiredScopes);
236
272
  const grant = stored?.grants
237
273
  .filter((candidate) => requiredScopes.every((scope) => candidate.scopes.includes(scope)))
238
274
  .sort((left, right) => left.scopes.length - right.scopes.length)[0];
239
275
  if (grant === undefined) {
240
- throw new KitRefusal("google_authorization_required", options.instance);
276
+ throw new KitRefusal("google_authorization_required", options.account);
241
277
  }
242
278
  const now = dependencies.now?.() ?? new Date();
243
279
  if (options.forceRefresh !== true && Date.parse(grant.expires_at) - now.getTime() > REFRESH_MARGIN_MS) {
244
280
  return grant.access_token;
245
281
  }
246
- const client = configuredClient(options.client);
247
282
  let token;
248
283
  try {
249
284
  token = await tokenRequest(dependencies.fetch ?? fetch, {
250
- client_id: client.id,
251
- client_secret: client.secret,
285
+ client_id: stored.client.id,
286
+ client_secret: stored.client.secret,
252
287
  refresh_token: grant.refresh_token,
253
288
  grant_type: "refresh_token",
254
289
  });
255
290
  } catch (error) {
256
291
  if (error instanceof KitRefusal && error.code === "google_authority_revoked") {
257
- await revokeGoogleAuthorization(options.directory, options.instance, grant.scopes);
292
+ await revokeGoogleAuthorization(options.directory, options.account, grant.scopes);
258
293
  }
259
294
  throw error;
260
295
  }
261
- await writeAuthorization(options.directory, options.instance, {
262
- grants: stored.grants.map((candidate) =>
263
- candidate === grant
264
- ? {
265
- ...candidate,
266
- access_token: token.access_token,
267
- expires_at: new Date(now.getTime() + token.expires_in * 1000).toISOString(),
268
- }
269
- : candidate,
270
- ),
271
- });
296
+ await updateAccount(options.directory, options.account, (current) =>
297
+ current === null
298
+ ? null
299
+ : {
300
+ ...current,
301
+ grants: current.grants.map((candidate) =>
302
+ candidate.refresh_token === grant.refresh_token
303
+ ? {
304
+ ...candidate,
305
+ access_token: token.access_token,
306
+ expires_at: new Date(now.getTime() + token.expires_in * 1000).toISOString(),
307
+ }
308
+ : candidate,
309
+ ),
310
+ },
311
+ );
272
312
  return token.access_token;
273
313
  }
@@ -1,25 +1,23 @@
1
- import { KitRefusal } from "../refusal.js";
2
- import { gmailPage } from "./gmail.js";
3
- import { calendarPage } from "./calendar.js";
4
- import { drivePage } from "./drive.js";
5
- import { declaredPage } from "./declared.js";
1
+ import { calendarEmail, calendarPage } from "./calendar.js";
2
+ import { driveEmail, drivePage } from "./drive.js";
3
+ import { gmailEmail, gmailPage } from "./gmail.js";
4
+ import { googleAccount, recordGoogleEmail } from "./oauth.js";
6
5
 
7
- export async function googlePage(options, dependencies = {}) {
8
- const common = {
9
- directory: options.directory,
10
- instance: options.instance,
11
- client: options.client,
12
- selection: options.service.selection,
13
- cursor: options.cursor,
14
- limit: options.limit,
15
- backfillFloor: options.backfillFloor,
16
- signal: options.signal,
17
- };
18
- if (options.service.service === "gmail") return gmailPage(common, dependencies);
19
- if (options.service.service === "calendar") return calendarPage(common, dependencies);
20
- if (options.service.service === "drive") return drivePage(common, dependencies);
21
- if (options.service.declaration !== null) {
22
- return declaredPage({ ...common, service: options.service }, dependencies);
23
- }
24
- throw new KitRefusal("google_service_unsupported", options.service.service);
6
+ const SERVICES = {
7
+ gmail: { page: gmailPage, email: gmailEmail },
8
+ calendar: { page: calendarPage, email: calendarEmail },
9
+ drive: { page: drivePage, email: driveEmail },
10
+ };
11
+
12
+ export function googlePage(options, dependencies = {}) {
13
+ return SERVICES[options.service].page(options, dependencies);
14
+ }
15
+
16
+ export async function recordAccountEmail(context, google) {
17
+ if ((await googleAccount(context.directory, google.account))?.email !== null) return;
18
+ const email = await SERVICES[google.service].email(
19
+ { directory: context.directory, account: google.account, signal: context.signal },
20
+ context.dependencies,
21
+ );
22
+ await recordGoogleEmail(context.directory, google.account, email);
25
23
  }
package/src/host.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { readFile, statfs } from "node:fs/promises";
2
2
  import { readConfiguration } from "./configuration.js";
3
3
  import { HOST_CPU_SAMPLE_MS } from "./contract.js";
4
- import { openSession, reportHostCapacity } from "./house.js";
4
+ import { maintainControl, openSession, reportHostCapacity } from "./house.js";
5
5
  import { KitRefusal } from "./refusal.js";
6
6
 
7
7
  const PROC_STAT = "/proc/stat";
@@ -116,6 +116,10 @@ export async function reportHost(options, dependencies = {}) {
116
116
  ? observed
117
117
  : { ...observed, maxima: declaredMaxima(configuration.host.maxima) };
118
118
  const session = await openSession(options, dependencies);
119
- await reportHostCapacity(session, declaration);
119
+ await maintainControl(
120
+ session,
121
+ (signal) => reportHostCapacity(session, declaration, signal),
122
+ options.signal,
123
+ );
120
124
  return declaration;
121
125
  }
package/src/house-main.js CHANGED
@@ -1,9 +1,19 @@
1
- import { resolve } from "node:path";
2
- import { BRIDGE_NONCE_ENVIRONMENT, BRIDGE_SOCKET_ENVIRONMENT } from "./contract.js";
3
- import { bridgeCall } from "./mcp.js";
4
- import { KitRefusal } from "./refusal.js";
1
+ import { homedir } from "node:os";
2
+ import { join, resolve } from "node:path";
3
+ import {
4
+ BRIDGE_NONCE_ENVIRONMENT,
5
+ BRIDGE_SOCKET_ENVIRONMENT,
6
+ WORKING_COPY_PUSH_HELP,
7
+ WORKING_COPY_PUSH_METHOD,
8
+ } from "./contract.js";
9
+ import { storedHouse } from "./login.js";
10
+ import { answerText, bridgeCall } from "./mcp.js";
11
+ import { formatCommandError, KitRefusal } from "./refusal.js";
12
+ import { ownerPush } from "./working-copy.js";
5
13
 
6
14
  const HELP = ["--help", "-h"];
15
+ const OWNER = "--owner";
16
+ const GIT_PUSH_USAGE = `usage: house git push [commit] [${OWNER}]`;
7
17
  const usage = (verb) => `usage: house ${verb} ['<arguments as one JSON object>' | - for standard input]`;
8
18
 
9
19
  async function answered(method, params) {
@@ -49,10 +59,59 @@ function located(verb, args) {
49
59
  return args;
50
60
  }
51
61
 
62
+ function printed(result) {
63
+ const text = answerText(result);
64
+ return result?.isError === true ? { err: text, status: 1 } : { out: text };
65
+ }
66
+
67
+ async function ownerPushed(request) {
68
+ const directory = process.env.HOUSE_KIT_HOME ?? join(homedir(), ".house-kit");
69
+ const cancellation = new AbortController();
70
+ const cancel = () => cancellation.abort();
71
+ process.once("SIGINT", cancel);
72
+ process.once("SIGTERM", cancel);
73
+ try {
74
+ const pushed = await ownerPush(
75
+ { house: await storedHouse(directory), directory, signal: cancellation.signal },
76
+ request,
77
+ );
78
+ return pushed.refused ? { err: pushed.text, status: 1 } : { out: pushed.text };
79
+ } catch (refusal) {
80
+ return { err: formatCommandError(refusal), status: 1 };
81
+ } finally {
82
+ process.removeListener("SIGINT", cancel);
83
+ process.removeListener("SIGTERM", cancel);
84
+ }
85
+ }
86
+
87
+ async function gitPush([action, ...rest]) {
88
+ if (action !== "push") throw new KitRefusal("kit_house_arguments_invalid", GIT_PUSH_USAGE);
89
+ if (rest.some((argument) => HELP.includes(argument))) return { out: `${GIT_PUSH_USAGE}\n\n${WORKING_COPY_PUSH_HELP}` };
90
+ const owner = rest.includes(OWNER);
91
+ const named = rest.filter((argument) => argument !== OWNER);
92
+ if (named.length > 1 || named.some((argument) => argument.startsWith("-"))) {
93
+ throw new KitRefusal("kit_house_arguments_invalid", GIT_PUSH_USAGE);
94
+ }
95
+ const request = { cwd: process.cwd(), commit: named[0] ?? "HEAD" };
96
+ if (process.env[BRIDGE_SOCKET_ENVIRONMENT] === undefined) {
97
+ if (!owner) throw new KitRefusal("kit_bridge_unavailable");
98
+ return ownerPushed(request);
99
+ }
100
+ if (owner) throw new KitRefusal("kit_house_arguments_invalid", `${OWNER} is only for use outside an Agent Session`);
101
+ return printed(await answered(WORKING_COPY_PUSH_METHOD, request));
102
+ }
103
+
52
104
  async function house([verb, argument, ...rest]) {
105
+ if (verb === "git") return gitPush([argument, ...rest].filter((part) => part !== undefined));
53
106
  if (verb === undefined || HELP.includes(verb)) {
54
107
  const tools = await listed();
55
- return { out: [usage("<verb>"), ...tools.map((tool) => `${tool.name}\n${tool.description}`)].join("\n\n") };
108
+ return {
109
+ out: [
110
+ usage("<verb>"),
111
+ ...tools.map((tool) => `${tool.name}\n${tool.description}`),
112
+ `git push [commit]\n${WORKING_COPY_PUSH_HELP}`,
113
+ ].join("\n\n"),
114
+ };
56
115
  }
57
116
  if (HELP.includes(argument)) {
58
117
  const tool = (await listed()).find((one) => one.name === verb);
@@ -61,9 +120,7 @@ async function house([verb, argument, ...rest]) {
61
120
  }
62
121
  if (rest.length > 0) throw new KitRefusal("kit_house_arguments_invalid", usage(verb));
63
122
  const args = located(verb, await argumentsOf(verb, argument));
64
- const result = await answered("tools/call", { name: verb, arguments: args });
65
- const text = (result?.content ?? []).filter((item) => item.type === "text").map((item) => item.text).join("\n");
66
- return result?.isError === true ? { err: text, status: 1 } : { out: text };
123
+ return printed(await answered("tools/call", { name: verb, arguments: args }));
67
124
  }
68
125
 
69
126
  house(process.argv.slice(2)).then(