@capybara-hq/kernel 0.1.0-rc.89 → 0.1.0-rc.90

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 (47) hide show
  1. package/dist/background/poll.job.d.ts +10 -0
  2. package/dist/background/poll.job.js +41 -10
  3. package/dist/background/stop.d.ts +39 -7
  4. package/dist/background/stop.js +107 -19
  5. package/dist/background/store.d.ts +25 -3
  6. package/dist/background/store.js +32 -2
  7. package/dist/background/tasks.service.d.ts +16 -2
  8. package/dist/background/tasks.service.js +11 -2
  9. package/dist/catalog/marketplaces.service.d.ts +1 -1
  10. package/dist/config/schema.d.ts +1 -0
  11. package/dist/config/schema.js +11 -1
  12. package/dist/conversations/events.d.ts +5 -0
  13. package/dist/conversations/events.js +11 -0
  14. package/dist/db/kernel-schema.d.ts +4 -0
  15. package/dist/device-access/device-access.service.d.ts +4 -1
  16. package/dist/device-access/device-access.service.js +40 -11
  17. package/dist/device-access/viewer.js +3 -2
  18. package/dist/dial-in/gateway.d.ts +7 -0
  19. package/dist/dial-in/gateway.js +43 -4
  20. package/dist/dial-in/relay.d.ts +27 -5
  21. package/dist/dial-in/relay.js +83 -21
  22. package/dist/dial-in/streams.d.ts +13 -5
  23. package/dist/dial-in/streams.js +43 -30
  24. package/dist/overrides.d.ts +4 -0
  25. package/dist/overrides.js +2 -0
  26. package/dist/platform/ops/background.ops.js +5 -2
  27. package/dist/sandbox/device-services.d.ts +4 -2
  28. package/dist/sandbox/device-services.js +9 -7
  29. package/dist/sandbox/review/review.service.d.ts +5 -0
  30. package/dist/sandbox/review/review.service.js +1 -0
  31. package/dist/sandbox/review/rules.js +24 -0
  32. package/dist/sandbox/review/types.d.ts +6 -0
  33. package/dist/sandbox/sandbox.service.d.ts +23 -0
  34. package/dist/sandbox/sandbox.service.js +57 -9
  35. package/dist/tools/builtins/sandbox/common.js +6 -1
  36. package/dist/tools/builtins/sandbox/task-stop.d.ts +2 -1
  37. package/dist/tools/builtins/sandbox/task-stop.js +21 -3
  38. package/dist/tools/call-scope.d.ts +21 -0
  39. package/dist/tools/call-scope.js +25 -0
  40. package/dist/tools/pipeline.d.ts +20 -0
  41. package/dist/tools/pipeline.js +67 -5
  42. package/dist/tools/questions.service.d.ts +6 -0
  43. package/dist/tools/questions.service.js +1 -0
  44. package/dist/tools/registry.d.ts +15 -8
  45. package/dist/tools/registry.js +21 -10
  46. package/package.json +3 -3
  47. package/src/migrations/0184_background_cancel_failure.sql +10 -0
@@ -12,6 +12,8 @@ export function attachCallScope(ctx, facts) {
12
12
  device: null,
13
13
  review: null,
14
14
  blocked: null,
15
+ starting: 0,
16
+ reached: false,
15
17
  ...(facts !== undefined && { facts }),
16
18
  };
17
19
  ctx[CALL_SCOPE] = scope;
@@ -21,3 +23,26 @@ export function attachCallScope(ctx, facts) {
21
23
  export function callScopeOf(ctx) {
22
24
  return ctx[CALL_SCOPE] ?? null;
23
25
  }
26
+ /**
27
+ * Runs `fn` as a device start of the call ({@link CallScope.starting}); a context without a scope
28
+ * runs it as it is.
29
+ */
30
+ export async function whileDeviceStarting(ctx, fn) {
31
+ const scope = callScopeOf(ctx);
32
+ if (scope === null)
33
+ return fn();
34
+ scope.starting++;
35
+ try {
36
+ return await fn();
37
+ }
38
+ finally {
39
+ scope.starting--;
40
+ }
41
+ }
42
+ /**
43
+ * Whether a timeout of this call happened while its device was still starting and before it sent
44
+ * anything to a device service (the only case the pipeline's "still starting" text may describe).
45
+ */
46
+ export function stillStartingUnsent(scope) {
47
+ return scope != null && scope.starting > 0 && !scope.reached;
48
+ }
@@ -117,6 +117,12 @@ export declare const secureInputCancelledText: (id: string) => string;
117
117
  export declare const secureInputExpiredText: (id: string, hours: number) => string;
118
118
  /** 调用执行中 run 被中止 / 到达截止时间。 */
119
119
  export declare const RUN_STOPPED_DURING_TEXT = "The run was stopped while this tool call was running; its outcome is unknown. Verify before retrying.";
120
+ /**
121
+ * The timeout of a call whose device was still starting (its instance being created or resumed,
122
+ * or the device service being launched) and that had sent nothing to a device service: its
123
+ * target was not reached. The start was stopped with the call, so a retry starts it again.
124
+ */
125
+ export declare const deviceStartingTimeoutText: (key: string, ms: number) => string;
120
126
  /**
121
127
  * A plugin policy hook denied the call (model-visible). `reason` is the sanitized deny reason, or the
122
128
  * generic sentence for a hook error / timeout; truncated to 500 characters.
@@ -246,6 +252,11 @@ interface PipelineDeps {
246
252
  * otherwise it is dropped with a warning.
247
253
  */
248
254
  inlineViews(plugin: string): ReadonlySet<string>;
255
+ /**
256
+ * Ids of an extension's `conversation.panel` contributions (read live, no alias): a plugin
257
+ * tool's `InputRequired.open` must name one of them, otherwise it is dropped with a warning.
258
+ */
259
+ panels(plugin: string): ReadonlySet<string>;
249
260
  /**
250
261
  * 连接器在调用途中报告工具清单变了(MCP `notifications/tools/list_changed`):内核安排一次该连接器的漂移检查。
251
262
  * 不等待、不抛错(实现自己吞掉错误)。
@@ -368,6 +379,8 @@ export type ExecOutcome =
368
379
  kind: "input_required";
369
380
  form: InputForm;
370
381
  state: unknown;
382
+ /** A plugin tool's `InputRequired.open`, unchecked (`openOf` checks it before asking). */
383
+ open?: unknown;
371
384
  blocked?: ServiceBlocked;
372
385
  review?: ReviewRecord;
373
386
  }
@@ -618,6 +631,13 @@ export declare class ToolPipeline {
618
631
  * else is dropped with a warning and the result is written as usual.
619
632
  */
620
633
  private cardOf;
634
+ /**
635
+ * A plugin tool's `InputRequired.open` (display only): `contribution` must be a
636
+ * `conversation.panel` of the same extension and `item` at most 200 characters; anything else is
637
+ * dropped with a warning and the form is asked as usual. The plugin is the tool's, never the
638
+ * extension's word.
639
+ */
640
+ private openOf;
621
641
  /**
622
642
  * A builtin's structured view (`tool_result.view`, display only): checked against
623
643
  * {@link ToolViewSchema}; an invalid one is dropped with a warning and the result is written as usual.
@@ -61,7 +61,7 @@ import { DEVICE_CODE_PATTERN, deviceCodeIdemKey, GITHUB_DEVICE_LOGIN_URL, } from
61
61
  import { DEVICE_OF_ANOTHER_ACCOUNT_TEXT } from "./builtins/sandbox/device-owner.js";
62
62
  import { SendMessageInput } from "./builtins/send-message.js";
63
63
  import { isAlwaysLoaded, notLoadedText } from "./builtins/tool-search.js";
64
- import { attachCallScope } from "./call-scope.js";
64
+ import { attachCallScope, stillStartingUnsent } from "./call-scope.js";
65
65
  import { canonicalJson } from "./canonical-json.js";
66
66
  import { CredentialMissing, Credentials } from "./credentials.js";
67
67
  import { ToolDrift } from "./drift.js";
@@ -190,6 +190,12 @@ const skippedResult = (toolKey) => toolKey === "AskUserQuestion"
190
190
  },
191
191
  };
192
192
  const timeoutText = (key, ms) => `Tool ${key} timed out after ${ms / 1000}s. The operation may or may not have completed; verify before retrying.`;
193
+ /**
194
+ * The timeout of a call whose device was still starting (its instance being created or resumed,
195
+ * or the device service being launched) and that had sent nothing to a device service: its
196
+ * target was not reached. The start was stopped with the call, so a retry starts it again.
197
+ */
198
+ export const deviceStartingTimeoutText = (key, ms) => `Tool ${key} timed out after ${ms / 1000}s while the device was still starting (its sandbox or the device service), before the call sent anything to its target. The start was stopped with the call; a retry starts the device again, and if it times out the same way the tool's time limit is too short for this device's first start: tell the user.`;
193
199
  const failedText = (key, errName) => `Tool ${key} failed (${errName}). The operation may not have completed; check before retrying or try another approach.`;
194
200
  const unknownToolText = (name, keys, toolSearch) => `Unknown tool "${name}". Available tools: ${keys.length === 0 ? "(none)" : keys.join(", ")}.` +
195
201
  (toolSearch ? " Use ToolSearch to load other tools." : "");
@@ -626,6 +632,9 @@ let ToolPipeline = class ToolPipeline {
626
632
  inlineViews: (plugin) => new Set((host?.current(plugin)?.ui ?? [])
627
633
  .filter((r) => r.slot.id === "conversation.tool-result.card")
628
634
  .map((r) => r.contribution.id)),
635
+ panels: (plugin) => new Set((host?.current(plugin)?.ui ?? [])
636
+ .filter((r) => r.slot.id === "conversation.panel")
637
+ .map((r) => r.contribution.id)),
629
638
  onToolsChanged: (orgId, connectorId) => {
630
639
  void drift?.onToolsChanged(orgId, connectorId);
631
640
  },
@@ -1246,7 +1255,14 @@ let ToolPipeline = class ToolPipeline {
1246
1255
  }
1247
1256
  }
1248
1257
  if (outcome.kind === "input_required") {
1249
- const asked = await this.askInput(ctx, call.id, tool.key, attempt, outcome);
1258
+ const open = tool.source.kind === "plugin" && outcome.open !== undefined
1259
+ ? this.openOf(outcome.open, tool.source.plugin, tool.key)
1260
+ : undefined;
1261
+ const { open: _raw, ...asking } = outcome;
1262
+ const asked = await this.askInput(ctx, call.id, tool.key, attempt, {
1263
+ ...asking,
1264
+ ...(open !== undefined && { open }),
1265
+ });
1250
1266
  if (typeof asked !== "string")
1251
1267
  return asked;
1252
1268
  outcome = {
@@ -2478,6 +2494,7 @@ let ToolPipeline = class ToolPipeline {
2478
2494
  toolKey,
2479
2495
  form: req.form,
2480
2496
  state: JSON.parse(stateJson),
2497
+ ...(req.open !== undefined && { open: req.open }),
2481
2498
  // kept for the call's result, which a later round writes
2482
2499
  ...((req.review !== undefined || req.blocked !== undefined) && {
2483
2500
  carried: {
@@ -2565,7 +2582,14 @@ let ToolPipeline = class ToolPipeline {
2565
2582
  }),
2566
2583
  };
2567
2584
  // 内置工具可按输入给自己的时限(沙箱 bash 按命令超时加余量);缺省 tools.timeoutMs
2568
- const timeoutMs = tool.timeoutMs?.(input) ?? this.config.get().tools.timeoutMs;
2585
+ const toolsCfg = this.config.get().tools;
2586
+ const ownMs = tool.timeoutMs?.(input);
2587
+ // an extension tool's declared limit never exceeds the deployment's ceiling
2588
+ const timeoutMs = ownMs === undefined
2589
+ ? toolsCfg.timeoutMs
2590
+ : tool.source.kind === "plugin"
2591
+ ? Math.min(ownMs, toolsCfg.maxTimeoutMs)
2592
+ : ownMs;
2569
2593
  let scope;
2570
2594
  try {
2571
2595
  // a plugin tool runs with the extension's effective config for the org, never without it
@@ -2611,13 +2635,27 @@ let ToolPipeline = class ToolPipeline {
2611
2635
  return handler(input, call);
2612
2636
  }, undefined, timeoutMs);
2613
2637
  if (r === "timeout") {
2614
- return withBlocked({ kind: "error", message: timeoutText(tool.key, timeoutMs) }, scope);
2638
+ const starting = stillStartingUnsent(scope);
2639
+ if (starting)
2640
+ this.deps.log.info("tool call timed out while its device was still starting", {
2641
+ toolKey: tool.key,
2642
+ timeoutMs,
2643
+ });
2644
+ const message = starting
2645
+ ? deviceStartingTimeoutText(tool.key, timeoutMs)
2646
+ : timeoutText(tool.key, timeoutMs);
2647
+ return withBlocked({ kind: "error", message }, scope);
2615
2648
  }
2616
2649
  // a launch reviewed before the call stopped or asked is kept with its result
2617
2650
  if (r === "aborted")
2618
2651
  return withBlocked({ kind: "error", message: RUN_STOPPED_DURING_TEXT }, scope);
2619
2652
  if (isInputRequired(r.value)) {
2620
- return withBlocked({ kind: "input_required", form: r.value.form, state: r.value.state ?? null }, scope);
2653
+ return withBlocked({
2654
+ kind: "input_required",
2655
+ form: r.value.form,
2656
+ state: r.value.state ?? null,
2657
+ ...(r.value.open !== undefined && { open: r.value.open }),
2658
+ }, scope);
2621
2659
  }
2622
2660
  return withBlocked({ kind: "result", result: r.value }, scope);
2623
2661
  }
@@ -2913,6 +2951,30 @@ let ToolPipeline = class ToolPipeline {
2913
2951
  }
2914
2952
  return { plugin, view: x.view, ...(x.key !== undefined && { key: x.key }) };
2915
2953
  }
2954
+ /**
2955
+ * A plugin tool's `InputRequired.open` (display only): `contribution` must be a
2956
+ * `conversation.panel` of the same extension and `item` at most 200 characters; anything else is
2957
+ * dropped with a warning and the form is asked as usual. The plugin is the tool's, never the
2958
+ * extension's word.
2959
+ */
2960
+ openOf(open, plugin, toolKey) {
2961
+ const x = (typeof open === "object" && open !== null ? open : {});
2962
+ const itemOk = x.item === undefined || (typeof x.item === "string" && x.item !== "" && x.item.length <= 200);
2963
+ if (typeof x.contribution !== "string" ||
2964
+ !this.deps.panels(plugin).has(x.contribution) ||
2965
+ !itemOk) {
2966
+ this.deps.log.warn("tool asked for input with an open target that is not a panel of its extension; dropped", {
2967
+ toolKey,
2968
+ contribution: typeof x.contribution === "string" ? x.contribution.slice(0, 100) : undefined,
2969
+ });
2970
+ return undefined;
2971
+ }
2972
+ return {
2973
+ plugin,
2974
+ contribution: x.contribution,
2975
+ ...(x.item !== undefined && { item: x.item }),
2976
+ };
2977
+ }
2916
2978
  /**
2917
2979
  * A builtin's structured view (`tool_result.view`, display only): checked against
2918
2980
  * {@link ToolViewSchema}; an invalid one is dropped with a warning and the result is written as usual.
@@ -56,6 +56,12 @@ export interface QuestionAskInput {
56
56
  form: InputForm;
57
57
  state: unknown;
58
58
  carried?: QuestionCarried;
59
+ /** A checked `InputRequired.open` of a plugin tool: written to `question_asked` only. */
60
+ open?: {
61
+ plugin: string;
62
+ contribution: string;
63
+ item?: string;
64
+ };
59
65
  }
60
66
  /**
61
67
  * The web's view of an input request: without the plugin's state (the tool's own resume data)
@@ -129,6 +129,7 @@ let QuestionsService = class QuestionsService {
129
129
  questionId: rec.id,
130
130
  form: rec.form,
131
131
  expiresAt: rec.expiresAt,
132
+ ...(input.open !== undefined && { open: input.open }),
132
133
  },
133
134
  },
134
135
  ]);
@@ -302,18 +302,25 @@ export interface UnavailableTool {
302
302
  /** The first 16 hex digits of sha256(canonicalJson(v)): a fingerprint independent of key order. */
303
303
  export declare function contentHash(v: unknown): string;
304
304
  /**
305
- * `ToolSpec` fields that are display only (UI refresh B): never sent to the model and left out of
306
- * the definition fingerprint, so adding or changing them never changes `model_call.request.tools`,
307
- * never sends a connector tool to drift review and never invalidates a remembered grant.
305
+ * `ToolSpec` fields the model never sees: the display-only `title` and `keyArgs` (UI refresh B)
306
+ * and an extension tool's own time limit `timeoutMs`. Left out of the definition fingerprint, so
307
+ * adding or changing them never changes `model_call.request.tools`, never sends a connector tool
308
+ * to drift review and never invalidates a remembered grant.
308
309
  */
309
- export declare const DISPLAY_ONLY_SPEC_FIELDS: readonly ["title", "keyArgs"];
310
- /** A tool spec without its {@link DISPLAY_ONLY_SPEC_FIELDS} (non-objects unchanged). */
311
- export declare function withoutDisplayFields<T>(spec: T): T;
310
+ export declare const NON_MODEL_SPEC_FIELDS: readonly ["title", "keyArgs", "timeoutMs"];
311
+ /** A tool spec without its {@link NON_MODEL_SPEC_FIELDS} (non-objects unchanged). */
312
+ export declare function withoutNonModelFields<T>(spec: T): T;
312
313
  /**
313
- * Tool definition fingerprint: {@link contentHash} of the spec without its display-only fields
314
- * (`title`, `keyArgs`). Specs without them hash exactly as before.
314
+ * Tool definition fingerprint: {@link contentHash} of the spec without the fields the model never
315
+ * sees ({@link NON_MODEL_SPEC_FIELDS}). Specs without them hash exactly as before.
315
316
  */
316
317
  export declare function specHash(spec: unknown): string;
318
+ /**
319
+ * An extension tool's own time limit ({@link ToolSpec.timeoutMs}, checked at registration), as
320
+ * the resolved tool's `timeoutMs`; nothing when it declares none (the pipeline then uses
321
+ * `tools.timeoutMs`).
322
+ */
323
+ export declare function pluginToolTimeout(spec: ToolSpec): Pick<ResolvedTool, "timeoutMs">;
317
324
  /** 由注解推导默认策略:只读 → allow;其余(破坏性 / 无注解)→ ask。 */
318
325
  export declare function policyFromAnnotations(a: ToolAnnotations | undefined): "allow" | "ask";
319
326
  /** Why an extension tool limited by `spec.sources` is not offered in a `source` conversation. */
@@ -35,26 +35,36 @@ export function contentHash(v) {
35
35
  return createHash("sha256").update(canonicalJson(v)).digest("hex").slice(0, 16);
36
36
  }
37
37
  /**
38
- * `ToolSpec` fields that are display only (UI refresh B): never sent to the model and left out of
39
- * the definition fingerprint, so adding or changing them never changes `model_call.request.tools`,
40
- * never sends a connector tool to drift review and never invalidates a remembered grant.
38
+ * `ToolSpec` fields the model never sees: the display-only `title` and `keyArgs` (UI refresh B)
39
+ * and an extension tool's own time limit `timeoutMs`. Left out of the definition fingerprint, so
40
+ * adding or changing them never changes `model_call.request.tools`, never sends a connector tool
41
+ * to drift review and never invalidates a remembered grant.
41
42
  */
42
- export const DISPLAY_ONLY_SPEC_FIELDS = ["title", "keyArgs"];
43
- /** A tool spec without its {@link DISPLAY_ONLY_SPEC_FIELDS} (non-objects unchanged). */
44
- export function withoutDisplayFields(spec) {
43
+ export const NON_MODEL_SPEC_FIELDS = ["title", "keyArgs", "timeoutMs"];
44
+ /** A tool spec without its {@link NON_MODEL_SPEC_FIELDS} (non-objects unchanged). */
45
+ export function withoutNonModelFields(spec) {
45
46
  if (spec === null || typeof spec !== "object" || Array.isArray(spec))
46
47
  return spec;
47
48
  const out = { ...spec };
48
- for (const f of DISPLAY_ONLY_SPEC_FIELDS)
49
+ for (const f of NON_MODEL_SPEC_FIELDS)
49
50
  delete out[f];
50
51
  return out;
51
52
  }
52
53
  /**
53
- * Tool definition fingerprint: {@link contentHash} of the spec without its display-only fields
54
- * (`title`, `keyArgs`). Specs without them hash exactly as before.
54
+ * Tool definition fingerprint: {@link contentHash} of the spec without the fields the model never
55
+ * sees ({@link NON_MODEL_SPEC_FIELDS}). Specs without them hash exactly as before.
55
56
  */
56
57
  export function specHash(spec) {
57
- return contentHash(withoutDisplayFields(spec));
58
+ return contentHash(withoutNonModelFields(spec));
59
+ }
60
+ /**
61
+ * An extension tool's own time limit ({@link ToolSpec.timeoutMs}, checked at registration), as
62
+ * the resolved tool's `timeoutMs`; nothing when it declares none (the pipeline then uses
63
+ * `tools.timeoutMs`).
64
+ */
65
+ export function pluginToolTimeout(spec) {
66
+ const ms = spec.timeoutMs;
67
+ return ms === undefined ? {} : { timeoutMs: () => ms };
58
68
  }
59
69
  /** 由注解推导默认策略:只读 → allow;其余(破坏性 / 无注解)→ ask。 */
60
70
  export function policyFromAnnotations(a) {
@@ -465,6 +475,7 @@ let ToolRegistry = class ToolRegistry {
465
475
  spec: t.spec,
466
476
  specHash: specHash(t.spec),
467
477
  defaultPolicy: t.spec.defaultPolicy ?? policyFromAnnotations(t.spec.annotations),
478
+ ...pluginToolTimeout(t.spec),
468
479
  });
469
480
  }
470
481
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capybara-hq/kernel",
3
- "version": "0.1.0-rc.89",
3
+ "version": "0.1.0-rc.90",
4
4
  "description": "Capybara kernel: config, database, plugin host and HTTP API of the Capybara agent platform",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -66,7 +66,7 @@
66
66
  "yaml": "^2.9.1",
67
67
  "yauzl": "3.4.0",
68
68
  "zod": "^4.6.7",
69
- "@capybara-hq/plugin-sdk": "^0.1.0-rc.71"
69
+ "@capybara-hq/plugin-sdk": "^0.1.0-rc.72"
70
70
  },
71
71
  "devDependencies": {
72
72
  "@langchain/langgraph-checkpoint-validation": "^1.2.0",
@@ -75,7 +75,7 @@
75
75
  "@types/ws": "^8.18.2",
76
76
  "@types/yauzl": "3.4.0",
77
77
  "ws": "^8.22.0",
78
- "@capybara-hq/testing": "0.1.0-rc.40"
78
+ "@capybara-hq/testing": "0.1.0-rc.41"
79
79
  },
80
80
  "scripts": {
81
81
  "build": "tsc -b"
@@ -0,0 +1,10 @@
1
+ -- Why a managed task's owed stop did not settle at its last attempt (fix 2026-11-25, core#846):
2
+ -- 'unreachable' when the device did not answer in time or its machine is not connected, 'failed'
3
+ -- when the device or its provider refused or failed the stop; NULL when no attempt failed (none
4
+ -- yet, or the device recorded the cancellation and applies it later). The task view and the
5
+ -- TaskStop result say which; the detail is the failure's message, clipped.
6
+ ALTER TABLE background_tasks ADD COLUMN IF NOT EXISTS cancel_failure text;
7
+ ALTER TABLE background_tasks DROP CONSTRAINT IF EXISTS background_tasks_cancel_failure_check;
8
+ ALTER TABLE background_tasks ADD CONSTRAINT background_tasks_cancel_failure_check
9
+ CHECK (cancel_failure IS NULL OR cancel_failure IN ('unreachable','failed'));
10
+ ALTER TABLE background_tasks ADD COLUMN IF NOT EXISTS cancel_failure_detail text;