@capybara-hq/kernel 0.1.0-rc.113 → 0.1.0-rc.115

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 (96) hide show
  1. package/bootstrap.json +7 -7
  2. package/dist/admin/admin.schemas.d.ts +1 -0
  3. package/dist/catalog/marketplaces.service.d.ts +1 -1
  4. package/dist/channels/actions.js +2 -0
  5. package/dist/channels/cards.d.ts +4 -3
  6. package/dist/channels/cards.js +18 -3
  7. package/dist/channels/phrases.d.ts +15 -0
  8. package/dist/channels/phrases.js +9 -0
  9. package/dist/conversations/conversations.service.js +7 -0
  10. package/dist/conversations/events.d.ts +21 -0
  11. package/dist/conversations/events.js +35 -3
  12. package/dist/ctx/create-ctx.d.ts +1 -1
  13. package/dist/ctx/create-ctx.js +6 -1
  14. package/dist/db/kernel-schema.d.ts +29 -0
  15. package/dist/device-access/device-access.service.d.ts +74 -3
  16. package/dist/device-access/device-access.service.js +221 -7
  17. package/dist/devices/devices.service.js +3 -0
  18. package/dist/errors.d.ts +6 -0
  19. package/dist/errors.js +6 -0
  20. package/dist/extensions/permission-set.d.ts +11 -1
  21. package/dist/extensions/permission-set.js +15 -1
  22. package/dist/extensions/permission-view.d.ts +3 -2
  23. package/dist/extensions/permission-view.js +15 -2
  24. package/dist/identity/settings.d.ts +5 -0
  25. package/dist/identity/settings.js +4 -0
  26. package/dist/infra/outbox/registry.d.ts +14 -0
  27. package/dist/infra/outbox/registry.js +14 -0
  28. package/dist/models/purposes.js +5 -0
  29. package/dist/orgs/offboarding.js +5 -1
  30. package/dist/orgs/settings.service.d.ts +2 -0
  31. package/dist/platform/ops/org-settings.ops.js +1 -1
  32. package/dist/roles/worker.module.js +3 -0
  33. package/dist/runtime/compaction-model.d.ts +57 -0
  34. package/dist/runtime/compaction-model.js +87 -0
  35. package/dist/runtime/compaction.service.d.ts +23 -8
  36. package/dist/runtime/compaction.service.js +15 -10
  37. package/dist/runtime/context/compaction.d.ts +19 -2
  38. package/dist/runtime/context/compaction.js +23 -2
  39. package/dist/runtime/context/idle-compaction-rules.d.ts +17 -2
  40. package/dist/runtime/context/idle-compaction-rules.js +20 -5
  41. package/dist/runtime/idle-arm.d.ts +12 -2
  42. package/dist/runtime/idle-arm.js +66 -18
  43. package/dist/runtime/idle-compaction-jobs.d.ts +24 -3
  44. package/dist/runtime/idle-compaction.d.ts +11 -3
  45. package/dist/runtime/idle-compaction.js +61 -30
  46. package/dist/runtime/maintenance.d.ts +4 -2
  47. package/dist/runtime/maintenance.js +14 -4
  48. package/dist/runtime/model-call.d.ts +52 -16
  49. package/dist/runtime/model-call.js +71 -24
  50. package/dist/runtime/run-finish.d.ts +12 -3
  51. package/dist/runtime/run-finish.js +14 -7
  52. package/dist/runtime/run-queue.d.ts +5 -2
  53. package/dist/runtime/run-queue.js +7 -4
  54. package/dist/runtime/run-steps.d.ts +4 -1
  55. package/dist/runtime/run-steps.js +9 -2
  56. package/dist/runtime/run-worker.js +4 -2
  57. package/dist/runtime/runtime.module.js +3 -0
  58. package/dist/sandbox/device-sandbox.d.ts +12 -0
  59. package/dist/sandbox/device-sandbox.js +2 -0
  60. package/dist/sandbox/device-services.d.ts +42 -2
  61. package/dist/sandbox/device-services.js +74 -14
  62. package/dist/sandbox/loopback-expiry.job.d.ts +21 -0
  63. package/dist/sandbox/loopback-expiry.job.js +50 -0
  64. package/dist/sandbox/loopback-grant-records.d.ts +143 -0
  65. package/dist/sandbox/loopback-grant-records.js +239 -0
  66. package/dist/sandbox/loopback-grants.d.ts +119 -0
  67. package/dist/sandbox/loopback-grants.js +454 -0
  68. package/dist/sandbox/loopback-live-ports.d.ts +12 -0
  69. package/dist/sandbox/loopback-live-ports.js +43 -0
  70. package/dist/sandbox/permission-call-types.d.ts +3 -2
  71. package/dist/sandbox/permission-ceiling-writes.js +13 -1
  72. package/dist/sandbox/permission-facts.d.ts +4 -0
  73. package/dist/sandbox/permission-facts.js +4 -0
  74. package/dist/sandbox/review/classifier.d.ts +26 -4
  75. package/dist/sandbox/review/classifier.js +31 -4
  76. package/dist/sandbox/review/index.d.ts +3 -3
  77. package/dist/sandbox/review/index.js +2 -2
  78. package/dist/sandbox/review/permission-candidate.d.ts +5 -4
  79. package/dist/sandbox/review/permission-candidate.js +6 -5
  80. package/dist/sandbox/review/review.service.d.ts +69 -0
  81. package/dist/sandbox/review/review.service.js +117 -0
  82. package/dist/sandbox/sandbox.module.d.ts +1 -1
  83. package/dist/sandbox/sandbox.module.js +13 -2
  84. package/dist/tools/approvals.service.d.ts +82 -6
  85. package/dist/tools/approvals.service.js +233 -11
  86. package/dist/tools/call-scope.d.ts +9 -1
  87. package/dist/tools/loopback-asks.d.ts +52 -0
  88. package/dist/tools/loopback-asks.js +70 -0
  89. package/dist/tools/network-asks.d.ts +2 -1
  90. package/dist/tools/network-asks.js +10 -5
  91. package/dist/tools/pipeline.d.ts +59 -3
  92. package/dist/tools/pipeline.js +403 -40
  93. package/dist/tools/tools.module.js +3 -0
  94. package/package.json +2 -2
  95. package/src/migrations/0199_device_loopback_grants.sql +59 -0
  96. package/src/migrations/0200_runs_idle_mode.sql +4 -0
@@ -4,6 +4,7 @@ import { NETWORK_ASK_MAX_HOSTS, NETWORK_HOST_MAX_CHARS } from "../conversations/
4
4
  import { coversHost } from "../sandbox/modes.js";
5
5
  import { ESCALATION_UNATTENDED_WHY } from "../sandbox/review/permission-candidate.js";
6
6
  import { NETWORK_ASK_CANCELLED_NOTE, networkAskHosts, } from "./approvals.service.js";
7
+ import { settledLoopbackNotes } from "./loopback-asks.js";
7
8
  /**
8
9
  * Network asks in the tool pipeline (network ask-to-allow spec §6.2, §6.6–§6.9, §8.3): what a
9
10
  * command's reported refusals turn into (an ask to the device owner, or notes written at once) and
@@ -359,18 +360,22 @@ const NO_OFFER_WHY = "the device's owner changes the device's network on its pag
359
360
  * The note of a settled network ask (spec §6.6): one line per outcome. An approval allows
360
361
  * `detail.grantedHosts` for the run (`once`) or added them to the device's own list (`always`,
361
362
  * applying at `detail.appliesAt`) and declines the hosts left unchecked; `hours` is the ask's own
362
- * wait (created to expiry). Null while it is pending.
363
+ * wait (created to expiry). Null while it is pending. A loopback port ask's notes are
364
+ * {@link settledLoopbackNotes}.
363
365
  */
364
366
  export function settledNetworkNotes(a, hours) {
365
- const asked = a.detail === null ? [] : networkAskHosts(a.detail);
367
+ if (a.detail?.kind === "loopback")
368
+ return settledLoopbackNotes(a, hours);
369
+ const detail = a.detail;
370
+ const asked = detail === null ? [] : networkAskHosts(detail);
366
371
  switch (a.status) {
367
372
  case "approved": {
368
- const granted = a.detail?.grantedHosts ?? asked;
373
+ const granted = detail?.grantedHosts ?? asked;
369
374
  const rest = asked.filter((h) => !granted.includes(h));
370
375
  const notes = [];
371
376
  if (granted.length > 0) {
372
- notes.push(a.scope === "always" && a.detail !== null
373
- ? addedToDeviceNote(granted, a.detail.appliesAt)
377
+ notes.push(a.scope === "always" && detail !== null
378
+ ? addedToDeviceNote(granted, detail.appliesAt)
374
379
  : allowedOnceNote(granted));
375
380
  }
376
381
  if (rest.length > 0)
@@ -19,11 +19,13 @@ import { type SubagentParent, type WaitingFor } from "../runtime/loop.js";
19
19
  import type { DispatchArgs, DispatchOutcome } from "../runtime/subagents/dispatch.js";
20
20
  import { SubagentMessages } from "../runtime/subagents/messages.js";
21
21
  import { SubagentsService } from "../runtime/subagents/subagents.service.js";
22
+ import { LoopbackGrants } from "../sandbox/loopback-grants.js";
22
23
  import { type NetworkRunState } from "../sandbox/network-grants.js";
23
24
  import { SandboxOperationAuthority } from "../sandbox/operation-authority.js";
24
25
  import { PermissionAuthorityLock } from "../sandbox/permission-authority-lock.js";
25
26
  import { PermissionAutoGrants } from "../sandbox/permission-auto-grants.js";
26
27
  import { PermissionCallGrants } from "../sandbox/permission-call-grants.js";
28
+ import { PermissionFacts } from "../sandbox/permission-facts.js";
27
29
  import { type CommandReview } from "../sandbox/review/index.js";
28
30
  import { SandboxService } from "../sandbox/sandbox.service.js";
29
31
  import { ApprovalsService, type NetworkAskDetail } from "./approvals.service.js";
@@ -243,7 +245,7 @@ interface PipelineDeps {
243
245
  readonly breaker: Pick<Breaker, "check" | "failure" | "success">;
244
246
  readonly outputs: Pick<ToolOutputs, "save" | "load">;
245
247
  /** 策略为 `ask` 时建审批;续跑时读该调用的审批结果。 A refused network host: a network ask. */
246
- readonly approvals: Pick<ApprovalsService, "request" | "forCall" | "networkForCall" | "requestNetwork" | "commandForCall" | "requestCommand" | "requestSecureInput" | "secureInputForCall">;
248
+ readonly approvals: Pick<ApprovalsService, "request" | "forCall" | "networkForCall" | "requestNetwork" | "requestLoopback" | "commandForCall" | "requestCommand" | "requestSecureInput" | "secureInputForCall">;
247
249
  /** 处理函数返回 `input_required` 时建输入请求;续跑时读该调用的各轮输入请求。 */
248
250
  readonly questions: Pick<QuestionsService, "ask" | "forCall">;
249
251
  readonly log: Logger;
@@ -382,6 +384,16 @@ export interface ServiceBlocked {
382
384
  hosts: ServiceBlockedHost[];
383
385
  /** The page the refusals came from, when the service said. */
384
386
  url: string | null;
387
+ /**
388
+ * The call's loopback refusal of an extension declaring `permissions.deviceAccess.loopbackPorts`
389
+ * (`CallScope.blocked.loopback`): the service (`<extension>.<name>`), the host and the port. Its
390
+ * host is in `hosts` too, as an address host. Absent: none.
391
+ */
392
+ loopback?: {
393
+ serviceId: string;
394
+ host: string;
395
+ port: number;
396
+ };
385
397
  }
386
398
  export type ExecOutcome =
387
399
  /** 工具返回了结果(可能是工具自己报告的错误,或畸形结果,由渲染阶段处理)。 */
@@ -586,13 +598,20 @@ export declare class ToolPipeline {
586
598
  * result images design §2); absent (hand-built pipelines): images are named as not stored.
587
599
  */
588
600
  private readonly resultImages?;
601
+ /**
602
+ * A device service's loopback port grants (browser loopback port grants); absent (hand-built
603
+ * pipelines): a loopback refusal keeps the address note.
604
+ */
605
+ private readonly loopbackGrants?;
606
+ /** The device's sandbox and its provider's support a loopback grant is checked against. */
607
+ private readonly permissionFacts?;
589
608
  private readonly decide;
590
609
  private readonly deps;
591
610
  constructor(kdb: KernelDb, eventLog: EventLog,
592
611
  /** The `Agent` tool's sub-agent dispatch; absent (tests): `Agent` calls are refused. */
593
612
  subagents: Pick<SubagentsService, "dispatch"> | undefined,
594
613
  /** The message tools between agents; absent (tests): their calls are refused. */
595
- messages: Pick<SubagentMessages, "toChild" | "toParent"> | undefined, registry: Pick<ToolRegistry, "forAgent" | "findConnector" | "handler" | "watcher">, credentials: Pick<Credentials, "injection" | "onUnauthorized">, breaker: Pick<Breaker, "check" | "failure" | "success">, outputs: Pick<ToolOutputs, "save" | "load">, approvals: Pick<ApprovalsService, "request" | "forCall" | "networkForCall" | "requestNetwork" | "commandForCall" | "requestCommand" | "requestSecureInput" | "secureInputForCall">, questions: Pick<QuestionsService, "ask" | "forCall">, config: Pick<KernelConfigService, "get">, log: Logger & Partial<Pick<KernelLogger, "debug">>,
614
+ messages: Pick<SubagentMessages, "toChild" | "toParent"> | undefined, registry: Pick<ToolRegistry, "forAgent" | "findConnector" | "handler" | "watcher">, credentials: Pick<Credentials, "injection" | "onUnauthorized">, breaker: Pick<Breaker, "check" | "failure" | "success">, outputs: Pick<ToolOutputs, "save" | "load">, approvals: Pick<ApprovalsService, "request" | "forCall" | "networkForCall" | "requestNetwork" | "requestLoopback" | "commandForCall" | "requestCommand" | "requestSecureInput" | "secureInputForCall">, questions: Pick<QuestionsService, "ask" | "forCall">, config: Pick<KernelConfigService, "get">, log: Logger & Partial<Pick<KernelLogger, "debug">>,
596
615
  /** Plugin hooks and inline ui contributions; absent (tests): no hooks, cards dropped. */
597
616
  host: Pick<ExtensionHost, "hooks" | "current"> | undefined, drift: Pick<ToolDrift, "onToolsChanged"> | undefined, effective: Pick<EffectiveConfigs, "get">, switches: Pick<OrgToolSwitches, "isToolOff">, txHost: TransactionHost, hooks?: ToolPipelineHooks, networkGrants?: {
598
617
  runState(runId: string, deviceId?: string): Promise<NetworkRunState>;
@@ -616,7 +635,14 @@ export declare class ToolPipeline {
616
635
  * Stores the images a tool result carries as `read` copies of the calling conversation (tool
617
636
  * result images design §2); absent (hand-built pipelines): images are named as not stored.
618
637
  */
619
- resultImages?: Pick<AttachmentsService, "storeRead"> | undefined);
638
+ resultImages?: Pick<AttachmentsService, "storeRead"> | undefined,
639
+ /**
640
+ * A device service's loopback port grants (browser loopback port grants); absent (hand-built
641
+ * pipelines): a loopback refusal keeps the address note.
642
+ */
643
+ loopbackGrants?: Pick<LoopbackGrants, "grantIn" | "forCall" | "liveForRoot"> | undefined,
644
+ /** The device's sandbox and its provider's support a loopback grant is checked against. */
645
+ permissionFacts?: Pick<PermissionFacts, "deviceSandboxFacts"> | undefined);
620
646
  /**
621
647
  * tools 节点:按顺序处理本步 assistant 消息里的全部 `tool_call`,每个调用都以一条 `tool_result` 结束
622
648
  * (之后才回到 prepare 构建下一次请求,保持「已记录调用、未记录结果之间不构建请求」的不变式)。
@@ -712,6 +738,36 @@ export declare class ToolPipeline {
712
738
  * settles (P-D2).
713
739
  */
714
740
  private networkBlocked;
741
+ /** The call's live loopback port grant (null: none, ended, or no grants service). */
742
+ private loopbackGrantOf;
743
+ /**
744
+ * A device service's loopback refusal (browser loopback port grants): the port of the person's
745
+ * own device a service of an extension declaring `permissions.deviceAccess.loopbackPorts` was
746
+ * refused. Refused by the device's ceilings, its provider or an unattended run → a note
747
+ * (`write`). A live grant the service had not applied yet → the call runs again, once. Else the
748
+ * conversation's permission profile decides (`approverForCrossing`, crossing `loopback-port`):
749
+ * Full access grants it, the review model reviews it (allow grants, deny is a note, an unsure
750
+ * answer asks), the person is asked (the run waits, the result kept in the ask). A grant is
751
+ * written with its `permission_reviewed`, the call's display events and its `tool.called` in one
752
+ * transaction, naming this attempt: the call then runs again in the next one (`rerun`), also
753
+ * after a crash. `none`: not decidable here (no device of the call's account, a hand-built
754
+ * pipeline, no facts): the host stays an address host with its note.
755
+ */
756
+ private loopbackBlocked;
757
+ /**
758
+ * A loopback port grant to this call's attempt and what goes with it, in one transaction: the
759
+ * grant first (it reads the root conversation `FOR SHARE`, before the conversation row the
760
+ * appends lock), then `permission_reviewed`, the call's display-only events and its
761
+ * `tool.called`. The call then runs again in the next attempt.
762
+ */
763
+ private grantLoopbackIn;
764
+ /** A review model's loopback verdict that grants nothing (a deny, an unsure answer). */
765
+ private recordLoopbackReview;
766
+ /**
767
+ * A kept result's content within what an ask keeps: output under the spill threshold but over
768
+ * {@link NETWORK_ASK_KEPT_MAX_BYTES} is archived after all.
769
+ */
770
+ private keptContent;
715
771
  /**
716
772
  * A device service's report (the call scope, browser use spec §7.1) on the device the call's
717
773
  * conversation works in (the root conversation's for a sub-agent, as the sandbox tools resolve