@capybara-hq/kernel 0.1.0-rc.30 → 0.1.0-rc.32

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 (85) hide show
  1. package/bootstrap.json +3 -3
  2. package/dist/catalog/marketplace-fetch.d.ts +5 -2
  3. package/dist/catalog/marketplace-fetch.js +4 -1
  4. package/dist/catalog/marketplaces.service.d.ts +1 -1
  5. package/dist/catalog/requests.service.d.ts +6 -5
  6. package/dist/catalog/requests.service.js +7 -6
  7. package/dist/config/load.d.ts +6 -0
  8. package/dist/config/load.js +26 -0
  9. package/dist/config/schema.js +5 -5
  10. package/dist/conversations/events.d.ts +10 -0
  11. package/dist/conversations/events.js +23 -1
  12. package/dist/ctx/create-ctx.d.ts +26 -4
  13. package/dist/ctx/create-ctx.js +54 -34
  14. package/dist/devices/devices.controller.js +3 -2
  15. package/dist/devices/devices.service.d.ts +17 -5
  16. package/dist/devices/devices.service.js +24 -13
  17. package/dist/devices/dto.d.ts +7 -6
  18. package/dist/devices/dto.js +6 -5
  19. package/dist/errors.d.ts +6 -1
  20. package/dist/errors.js +6 -1
  21. package/dist/evals/reconstruct.js +18 -8
  22. package/dist/extensions/bootstrap.d.ts +1 -1
  23. package/dist/extensions/bootstrap.js +7 -1
  24. package/dist/extensions/installs.js +4 -1
  25. package/dist/extensions/permission-set.d.ts +7 -6
  26. package/dist/extensions/permission-set.js +9 -6
  27. package/dist/extensions/permission-view.d.ts +4 -4
  28. package/dist/extensions/permission-view.js +10 -5
  29. package/dist/http/app.d.ts +8 -2
  30. package/dist/http/app.js +12 -2
  31. package/dist/http/plugin-dispatch.d.ts +55 -18
  32. package/dist/http/plugin-dispatch.js +124 -29
  33. package/dist/http/plugin-route-access.d.ts +56 -0
  34. package/dist/http/plugin-route-access.js +192 -0
  35. package/dist/identity/auth.guard.d.ts +26 -12
  36. package/dist/identity/auth.guard.js +62 -33
  37. package/dist/infra/throttler/named-throttlers.d.ts +3 -0
  38. package/dist/infra/throttler/named-throttlers.js +16 -0
  39. package/dist/infra/throttler/throttler.guard.d.ts +2 -1
  40. package/dist/infra/throttler/throttler.guard.js +2 -1
  41. package/dist/kernel.module.js +3 -0
  42. package/dist/lifecycle/kernel-lifecycle.js +1 -3
  43. package/dist/net/public-address.d.ts +14 -4
  44. package/dist/net/public-address.js +22 -12
  45. package/dist/net/safe-fetch.d.ts +2 -1
  46. package/dist/net/safe-fetch.js +16 -4
  47. package/dist/overrides.d.ts +3 -0
  48. package/dist/overrides.js +2 -0
  49. package/dist/packages/npm.d.ts +28 -9
  50. package/dist/packages/npm.js +30 -19
  51. package/dist/packages/provenance.d.ts +3 -1
  52. package/dist/packages/provenance.js +7 -3
  53. package/dist/platform/ops/devices.ops.js +1 -1
  54. package/dist/platform/ops/platform.ops.d.ts +3 -1
  55. package/dist/platform/ops/platform.ops.js +11 -13
  56. package/dist/runtime/context/build.d.ts +10 -1
  57. package/dist/runtime/context/build.js +44 -10
  58. package/dist/runtime/context/compaction.d.ts +27 -8
  59. package/dist/runtime/context/compaction.js +133 -58
  60. package/dist/runtime/context/fit.d.ts +54 -22
  61. package/dist/runtime/context/fit.js +116 -40
  62. package/dist/runtime/model-call.d.ts +0 -8
  63. package/dist/runtime/model-call.js +185 -83
  64. package/dist/sandbox/review/classifier.d.ts +2 -0
  65. package/dist/sandbox/review/classifier.js +12 -1
  66. package/dist/sandbox/review/index.d.ts +3 -3
  67. package/dist/sandbox/review/index.js +3 -3
  68. package/dist/sandbox/review/review.service.d.ts +12 -4
  69. package/dist/sandbox/review/review.service.js +88 -19
  70. package/dist/sandbox/review/rules.d.ts +6 -0
  71. package/dist/sandbox/review/rules.js +1013 -95
  72. package/dist/sandbox/review/types.d.ts +21 -4
  73. package/dist/sandbox/review/types.js +41 -5
  74. package/dist/sandbox/sandbox.service.d.ts +8 -7
  75. package/dist/sandbox/sandbox.service.js +16 -16
  76. package/dist/setup/kernel-checks.js +3 -2
  77. package/dist/setup/oauth-token-probe.d.ts +1 -8
  78. package/dist/setup/oauth-token-probe.js +0 -16
  79. package/dist/tools/builtins/sandbox/common.d.ts +1 -1
  80. package/dist/tools/builtins/sandbox/common.js +1 -1
  81. package/dist/tools/builtins/tool-search.js +4 -3
  82. package/dist/tools/oauth/discovery.d.ts +38 -16
  83. package/dist/tools/oauth/discovery.js +59 -33
  84. package/dist/tools/pipeline.js +17 -13
  85. package/package.json +3 -3
@@ -16,8 +16,8 @@ import { KERNEL_LOGGER } from "../../infra/tokens.js";
16
16
  import { ModelPurposesService } from "../../models/purposes.js";
17
17
  import { StepLog } from "../../runtime/step-log.js";
18
18
  import { boundMessages, newReviewTag, REVIEW_ASK_REASON, REVIEW_PURPOSE, runClassifier, } from "./classifier.js";
19
- import { reviewRules, reviewWrite } from "./rules.js";
20
- import { isSessionGrantableRule } from "./types.js";
19
+ import { reviewRules, reviewWrite, writtenPaths } from "./rules.js";
20
+ import { applySessionGrants, isSessionGrantableRule, } from "./types.js";
21
21
  /** The reasons the service itself answers with (the rule table's own reasons come from its hits). */
22
22
  export const REVIEW_REASONS = {
23
23
  /** The tool is not one the review covers. */
@@ -31,6 +31,8 @@ export const REVIEW_REASONS = {
31
31
  };
32
32
  /** The tools the review covers: the shell, and the file tools whose writes may leave the workspace. */
33
33
  const REVIEWED_TOOLS = new Set(["Bash", "Write", "Edit"]);
34
+ /** How many of a conversation tree's latest assistant messages the review reads for its writes. */
35
+ const WRITTEN_EVENTS_READ = 200;
34
36
  /** How many of the root conversation's latest person messages are read before bounding the text. */
35
37
  const PERSON_MESSAGES_READ = 20;
36
38
  const allow = (reason, rule) => ({
@@ -117,8 +119,14 @@ let CommandReviewService = class CommandReviewService {
117
119
  mode: input.mode,
118
120
  attended: input.attended,
119
121
  };
120
- const result = isWrite ? reviewWrite(target, facts) : reviewRules(target, facts);
121
- const hit = decidingHit(result.hits, result.outcome);
122
+ // running a file this conversation wrote is not a development command (hardening spec R1)
123
+ const result = isWrite
124
+ ? reviewWrite(target, facts)
125
+ : reviewRules(target, {
126
+ ...facts,
127
+ written: await this.writtenInTree(await rootOf(), facts),
128
+ });
129
+ let hit = decidingHit(result.hits, result.outcome);
122
130
  if (result.outcome === "deny") {
123
131
  return {
124
132
  decision: "deny",
@@ -131,14 +139,23 @@ let CommandReviewService = class CommandReviewService {
131
139
  return allow(hit?.reason ?? "The command review allows this command.", hit?.id ?? null);
132
140
  }
133
141
  if (result.outcome === "ask") {
134
- return (await this.sessionGranted(await rootOf(), hit?.id ?? null))
135
- ? allow(REVIEW_REASONS.sessionGrant, hit?.id ?? null)
136
- : {
142
+ // a session grant covers its own class only, and only while the person is there to have
143
+ // given it (hardening spec R4, R6): every ask hit granted and nothing unsure left
144
+ const granted = input.attended ? await this.grantedRules(await rootOf()) : new Set();
145
+ const after = applySessionGrants(result, (rule) => granted.has(rule));
146
+ if (after.outcome === "allow") {
147
+ return allow(REVIEW_REASONS.sessionGrant, after.hit?.id ?? null);
148
+ }
149
+ if (after.outcome === "ask") {
150
+ return {
137
151
  decision: "ask",
138
152
  by: "rule",
139
- reason: hit?.reason ?? "This command needs the person's approval.",
140
- rule: hit?.id ?? null,
153
+ reason: after.hit?.reason ?? "This command needs the person's approval.",
154
+ rule: after.hit?.id ?? null,
141
155
  };
156
+ }
157
+ // the granted asks are settled; what is left is unsure, which the classifier decides
158
+ hit = after.hit;
142
159
  }
143
160
  // unsure: the classifier decides when the purpose is mapped; without one the deciding hit is a
144
161
  // catch-all (`unsure.*`), which no person can reason about as a class: it is an ask with no
@@ -230,21 +247,58 @@ let CommandReviewService = class CommandReviewService {
230
247
  return id;
231
248
  }
232
249
  /**
233
- * Whether the root conversation holds a session grant for this rule class (`command:<ruleId>`).
250
+ * The rule classes the root conversation holds session grants for (`command:<ruleId>` rows).
234
251
  * Command grants carry no tool-spec fingerprint: the approval records the rule, not the command.
235
- * Only a concrete rule class can match: a catch-all (`unsure.*`) or a missing rule never does,
236
- * so a stored `command:unsure.*` row (old data) can cover nothing.
252
+ * Only a concrete grantable class counts: a stored catch-all or never-session class (old data)
253
+ * covers nothing.
237
254
  */
238
- async sessionGranted(rootConversationId, rule) {
239
- if (!isSessionGrantableRule(rule))
240
- return false;
241
- const grant = await this.db
255
+ async grantedRules(rootConversationId) {
256
+ const rows = await this.db
242
257
  .selectFrom("conversation_tool_grants")
243
258
  .select("tool_key")
244
259
  .where("conversation_id", "=", rootConversationId)
245
- .where("tool_key", "=", `command:${rule}`)
246
- .executeTakeFirst();
247
- return grant !== undefined;
260
+ .where("tool_key", "like", "command:%")
261
+ .execute();
262
+ const rules = rows.map((r) => r.tool_key.slice("command:".length));
263
+ return new Set(rules.filter((rule) => isSessionGrantableRule(rule)));
264
+ }
265
+ /**
266
+ * The absolute paths the conversation tree's own tool calls wrote — the root's and every
267
+ * sub-agent's: `Write` / `Edit` targets and the write targets (downloads and copies included) of
268
+ * their `Bash` lines, from the latest {@link WRITTEN_EVENTS_READ} assistant messages of the tree.
269
+ * All agents of a tree share the device, so a file one of them wrote is code the run produced,
270
+ * whoever runs it (hardening spec R1).
271
+ */
272
+ async writtenInTree(rootConversationId, facts) {
273
+ const rows = await this.db
274
+ .withRecursive("tree(id)", (db) => db
275
+ .selectFrom("conversations")
276
+ .select("id")
277
+ .where("id", "=", rootConversationId)
278
+ .unionAll((q) => q
279
+ .selectFrom("conversations as c")
280
+ .innerJoin("tree", "tree.id", "c.parent_conversation_id")
281
+ .select("c.id")))
282
+ .selectFrom("conversation_events")
283
+ .select("payload")
284
+ .where("conversation_id", "in", (q) => q.selectFrom("tree").select("id"))
285
+ .where("type", "=", "assistant_message")
286
+ .orderBy("created_at", "desc")
287
+ .limit(WRITTEN_EVENTS_READ)
288
+ .execute();
289
+ const written = new Set();
290
+ for (const row of rows) {
291
+ for (const call of toolCallsOf(row.payload)) {
292
+ if ((call.name === "Write" || call.name === "Edit") && typeof call.path === "string") {
293
+ written.add(call.path.startsWith("/") ? call.path : `${facts.workspace}/${call.path}`);
294
+ }
295
+ else if (call.name === "Bash" && typeof call.command === "string") {
296
+ for (const path of writtenPaths(call.command, facts))
297
+ written.add(path);
298
+ }
299
+ }
300
+ }
301
+ return [...written];
248
302
  }
249
303
  /**
250
304
  * The bounded text of the root conversation's last person messages: the `user_message` text blocks
@@ -274,6 +328,21 @@ CommandReviewService = __decorate([
274
328
  __param(6, Inject(ExtensionHost))
275
329
  ], CommandReviewService);
276
330
  export { CommandReviewService };
331
+ /** The tool calls of an `assistant_message` payload, with the fields the review reads. */
332
+ function toolCallsOf(payload) {
333
+ const content = payload?.content;
334
+ if (!Array.isArray(content))
335
+ return [];
336
+ return content.flatMap((b) => {
337
+ if (b === null || typeof b !== "object")
338
+ return [];
339
+ const block = b;
340
+ if (block.type !== "tool_call" || typeof block.name !== "string")
341
+ return [];
342
+ const input = (block.input ?? {});
343
+ return [{ name: block.name, path: input.file_path, command: input.command }];
344
+ });
345
+ }
277
346
  /** The text blocks of a `user_message` payload (its images, documents and attachments are skipped). */
278
347
  function textOfUserMessage(payload) {
279
348
  const content = payload?.content;
@@ -65,6 +65,12 @@ import type { ParsedCommand, ReviewFacts, ReviewResult } from "./types.js";
65
65
  export type { ParsedCommand, Redirect, ReviewFacts, ReviewResult, RuleHit, RuleOutcome, SimpleCommand, } from "./types.js";
66
66
  /** The public parser: the parsed command, or null when the tokenizer is not confident. */
67
67
  export declare function parseCommand(command: string): ParsedCommand | null;
68
+ /**
69
+ * The absolute paths a command line writes, for the review's record of what a conversation wrote
70
+ * (hardening spec R1: running one of them later is not a development command). An unparseable line
71
+ * writes nothing the review can name.
72
+ */
73
+ export declare function writtenPaths(command: string, facts: ReviewFacts): string[];
68
74
  /**
69
75
  * The rule table over a command line: the strictest outcome and every hit, in rule order. A parse
70
76
  * the tokenizer cannot make confidently yields only `unsure.unparsed`; a command no rule covers