@springbrand/agent-runtime 0.3.0-alpha.1 → 0.3.0-alpha.10

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 (107) hide show
  1. package/package.json +6 -6
  2. package/src/adapter/cloudflare/agent.ts +157 -14
  3. package/src/adapter/cloudflare/define-agent.ts +12 -7
  4. package/src/adapter/cloudflare/index.ts +18 -0
  5. package/src/adapter/cloudflare/scheduling/schedule-rule.ts +322 -0
  6. package/src/adapter/cloudflare/scheduling/zoned-cron.ts +11 -6
  7. package/src/adapter/cloudflare/schema.ts +15 -0
  8. package/src/adapter/cloudflare/subagent/definition.ts +4 -4
  9. package/src/adapter/cloudflare/subagent/runner.ts +1 -1
  10. package/src/adapter/cloudflare/subagent/tools.ts +2 -2
  11. package/src/adapter/cloudflare/universal-agent/preparation.ts +3 -3
  12. package/src/adapter/cloudflare/universal-agent/tools.ts +4 -6
  13. package/src/adapter/cloudflare/workspace/version-control.ts +1 -1
  14. package/src/config/agent-context.ts +1 -1
  15. package/src/config/assembler.ts +29 -31
  16. package/src/config/assembly-snapshot.ts +1 -1
  17. package/src/config/definition.ts +1 -1
  18. package/src/config/harness-runtime.ts +9 -1
  19. package/src/config/model-guidance.ts +21 -0
  20. package/src/config/pinning.ts +22 -3
  21. package/src/config/ports.ts +27 -7
  22. package/src/config/public-contracts.ts +2 -2
  23. package/src/context/assembly.ts +1 -1
  24. package/src/context/extension/assembly.ts +2 -2
  25. package/src/context/memory/budget/gate.ts +19 -3
  26. package/src/conversation/harness-stream.ts +2 -1
  27. package/src/conversation/message/projection.ts +38 -21
  28. package/src/conversation/state.ts +23 -2
  29. package/src/harness/session-harness.ts +86 -5
  30. package/src/index.ts +7 -7
  31. package/src/lib/artifacts.ts +18 -0
  32. package/src/lib/execution-level.ts +13 -0
  33. package/src/lib/prompt.ts +7 -6
  34. package/src/lib/token-estimate.ts +8 -0
  35. package/src/models/index.ts +1 -0
  36. package/src/session/cloudflare/migration.ts +70 -6
  37. package/src/skills/springbrand-worker-website/index.ts +1 -1
  38. package/src/submission/admission.ts +12 -1
  39. package/src/submission/contracts.ts +42 -2
  40. package/src/submission/delivery.ts +1 -1
  41. package/src/submission/finalization.ts +1 -1
  42. package/src/submission/repository.ts +64 -4
  43. package/src/submission/unattended.ts +40 -0
  44. package/src/telemetry/coordinator.ts +1 -1
  45. package/src/telemetry/subagent-lifecycle.ts +1 -1
  46. package/src/telemetry/turn-events.ts +1 -1
  47. package/src/tools/README.md +44 -0
  48. package/src/tools/{ai-adapter.ts → adapters/ai-to-pi.ts} +6 -9
  49. package/src/tools/{declared.ts → adapters/declared.ts} +6 -2
  50. package/src/tools/{nested-tools.ts → adapters/pi-to-ai.ts} +2 -2
  51. package/src/tools/assistant/ask-user.ts +317 -0
  52. package/src/tools/assistant/candidate.ts +15 -0
  53. package/src/tools/assistant/followups.ts +34 -0
  54. package/src/tools/assistant/index.ts +19 -0
  55. package/src/tools/assistant/memory.ts +73 -0
  56. package/src/tools/assistant/plan.ts +98 -0
  57. package/src/tools/{time.ts → assistant/time.ts} +1 -1
  58. package/src/tools/{core-host.ts → codemode/adapter.ts} +50 -49
  59. package/src/tools/{core.ts → codemode/index.ts} +36 -92
  60. package/src/tools/codemode/workspace-connector.ts +198 -0
  61. package/src/tools/extension/index.ts +42 -0
  62. package/src/tools/index.ts +28 -26
  63. package/src/tools/{gateway.ts → mcp/gateway.ts} +19 -19
  64. package/src/tools/{mcp.ts → mcp/index.ts} +5 -5
  65. package/src/tools/{approval → runtime/approval}/repository.ts +13 -12
  66. package/src/tools/{compiler.ts → runtime/compiler.ts} +84 -147
  67. package/src/tools/runtime/contracts.ts +117 -0
  68. package/src/tools/{decision-gate.ts → runtime/decision-gate.ts} +147 -37
  69. package/src/tools/{human-decisions.ts → runtime/human-decisions.ts} +4 -4
  70. package/src/tools/{interaction → runtime/interaction}/repository.ts +1 -7
  71. package/src/tools/runtime/result.ts +22 -0
  72. package/src/tools/runtime/unattended-error.ts +4 -0
  73. package/src/tools/sandbox/index.ts +228 -0
  74. package/src/tools/schedule/index.ts +301 -0
  75. package/src/tools/skill/contracts.ts +20 -0
  76. package/src/tools/skill/index.ts +99 -0
  77. package/src/tools/skill/resources.ts +322 -0
  78. package/src/tools/skill/scripts.ts +168 -0
  79. package/src/tools/{subagent-authority.ts → subagent/authority.ts} +31 -31
  80. package/src/tools/{subagent.ts → subagent/index.ts} +34 -37
  81. package/src/tools/{subagent-run-repository.ts → subagent/repository.ts} +1 -1
  82. package/src/tools/{subagent-runtime.ts → subagent/runtime.ts} +1 -1
  83. package/src/tools/{temporary-agent → subagent/temporary}/approvals.ts +4 -7
  84. package/src/tools/{temporary-agent → subagent/temporary}/core.ts +1 -1
  85. package/src/tools/{temporary-agent → subagent/temporary}/runner.ts +1 -1
  86. package/src/tools/{temporary-agent → subagent/temporary}/workspace.ts +1 -4
  87. package/src/tools/{web-fetch.ts → web/fetch.ts} +4 -13
  88. package/src/tools/web/index.ts +2 -0
  89. package/src/tools/web/search/api.ts +1320 -0
  90. package/src/tools/web/search/index.ts +2 -0
  91. package/src/tools/{web-search/web-search.ts → web/search/search.ts} +5 -7
  92. package/src/tools/{workspace-sandbox.ts → workspace/index.ts} +12 -272
  93. package/src/tools/{workspace-revision.ts → workspace/revision.ts} +3 -3
  94. package/src/tools/base.ts +0 -534
  95. package/src/tools/schedule.ts +0 -261
  96. package/src/tools/skill.ts +0 -565
  97. package/src/tools/web-search/api.ts +0 -1318
  98. package/src/tools/web-search/index.ts +0 -2
  99. package/src/tools/workspace-state-connector.ts +0 -146
  100. package/src/tools/{approval → runtime/approval}/index.ts +0 -0
  101. package/src/tools/{interaction → runtime/interaction}/index.ts +0 -0
  102. package/src/tools/{temporary-agent → subagent/temporary}/coordinator.ts +0 -0
  103. package/src/tools/{subagent-types → subagent/types}/contract.ts +0 -0
  104. package/src/tools/{subagent-types → subagent/types}/extract/index.ts +0 -0
  105. package/src/tools/{subagent-types → subagent/types}/fanout/index.ts +0 -0
  106. package/src/tools/{subagent-types → subagent/types}/registry.ts +1 -1
  107. /package/src/tools/{workspace-versioning.ts → workspace/versioning.ts} +0 -0
@@ -4,9 +4,12 @@
4
4
  /** Stable narrow reference shared by messages, RPC responses and the UI. */
5
5
  export interface ArtifactRef {
6
6
  kind: "artifact_ref";
7
+ artifactId: string;
7
8
  path: string;
8
9
  bytes: number;
9
10
  hash: string;
11
+ workspaceRevision: string;
12
+ availability: "available" | "unavailable";
10
13
  preview: string;
11
14
  note: string;
12
15
  }
@@ -14,6 +17,15 @@ export interface ArtifactRef {
14
17
  export interface SpillWorkspace {
15
18
  /** 把溢出的 Artifact 内容写入指定 Workspace 路径。 */
16
19
  writeFile(path: string, content: string, mimeType?: string): Promise<void>;
20
+ /** 写入后验证物理文件,防止只持久化了一个不存在或被截断的引用。 */
21
+ stat?(path: string): Promise<{
22
+ type: "file" | "directory" | "symlink";
23
+ size: number;
24
+ updatedAt: number;
25
+ contentVersion?: string;
26
+ } | null>;
27
+ /** 写入后验证内容摘要;Artifact 全部是 UTF-8 文本。 */
28
+ readFileBytes?(path: string): Promise<Uint8Array | null>;
17
29
  }
18
30
 
19
31
  /**
@@ -33,6 +45,10 @@ export async function sha256Hex(text: string): Promise<string> {
33
45
  return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
34
46
  }
35
47
 
48
+ function artifactId(hash: string): string {
49
+ return `artifact_${hash.slice(0, 24)}`;
50
+ }
51
+
36
52
  /**
37
53
  * 把 Tool 结果转成可存储的文本,并选择 `.txt` 或 `.json` 扩展名。
38
54
  *
@@ -52,3 +68,5 @@ export function serializeOutput(output: unknown): { text: string; ext: "txt" | "
52
68
  return { text: String(output), ext: "txt" };
53
69
  }
54
70
  }
71
+
72
+ export { artifactId };
@@ -25,3 +25,16 @@ export function maxExecutionLevel(
25
25
  )
26
26
  ]!;
27
27
  }
28
+
29
+ /** The most permissive level shared by an Agent and a Scheduled Task ceiling. */
30
+ export function minExecutionLevel(
31
+ left: ExecutionLevel,
32
+ right: ExecutionLevel,
33
+ ): ExecutionLevel {
34
+ return EXECUTION_LEVELS[
35
+ Math.min(
36
+ EXECUTION_LEVELS.indexOf(left),
37
+ EXECUTION_LEVELS.indexOf(right),
38
+ )
39
+ ]!;
40
+ }
package/src/lib/prompt.ts CHANGED
@@ -17,9 +17,8 @@ export const PERSONA =
17
17
  // Names the stable runtime environment.
18
18
  export const RUNTIME =
19
19
  "Runtime: this agent runs on Cloudflare Workers. When execute is present, its Code Mode Dynamic Worker is your " +
20
- "instrument — code there runs with outbound network access (fetch), your " +
21
- "workspace filesystem (state.*), and the tools.* methods listed in its own description. Use it for raw or customized HTTP " +
22
- "requests, parsing a payload, hitting several known endpoints, or computing over a file. Write plain " +
20
+ "instrument — code there runs with your workspace filesystem (state.*) and the tools.* methods listed in its own description. " +
21
+ "Use it for tool composition, parsing a payload, or computing over a file. Write plain " +
23
22
  "JavaScript — the sandbox evaluates " +
24
23
  "it directly, so TypeScript type annotations (`: number`, `as Type`) are a syntax error, and there " +
25
24
  "is no Python interpreter or package manager to invoke. Use the JS/Workers equivalent of what you " +
@@ -58,8 +57,7 @@ export const TOOLS =
58
57
  "use state.* for workspace file operations, tools.* for other host capabilities, and sequential calls for repeated or durable operations. " +
59
58
  "Top-level-only Tools remain Direct even when called repeatedly. " +
60
59
  "For web tasks, use web_search for web discovery, current facts, cited research, " +
61
- "and public URL analysis. For web access specifically, use execute Code Mode only for raw or customized network requests, structured " +
62
- "API calls, or when web_search cannot retrieve the required content; do not use execute for ordinary web " +
60
+ "and public URL analysis; do not use execute for ordinary web " +
63
61
  "searches. When execute is absent, use the actually exposed Direct Tools. When sandbox_* tools are present, use that isolated Linux environment for Python/Node, " +
64
62
  "package managers, system commands, builds, tests and background processes; its filesystem is temporary, " +
65
63
  "persistent inputs are copied in automatically on first use, /userspace is read-only, and only explicitly " +
@@ -70,7 +68,10 @@ export const TOOLS =
70
68
  "prioritize verification, saving durable results, and the final response. " +
71
69
  "When related Tool calls can run independently, all are available inside execute, and execute is present, run them inside that execute rather than as parallel top-level calls; " +
72
70
  "only independent top-level-only Direct Tools should be issued together in one turn. When you reference " +
73
- "code, cite it as file_path:line_number.";
71
+ "code, cite it as file_path:line_number. " +
72
+ "When schedule tools are available, prepare a standalone Requirement (at most 1000 Unicode characters), a name (at most 50), a time rule and an explicit timezone. Ask for missing information. " +
73
+ "Use local once/daily/weekly rules for new scheduled tasks. The Host prepares the authorization scope and the user must confirm before saving; never claim a task was created before the Tool succeeds. " +
74
+ "One source Chat may create multiple tasks. Before editing, list tasks and use the exact Task ID; ask when the target is ambiguous. Never attach source history or hidden context to the Requirement.";
74
75
 
75
76
  // A real browser is the only way to observe what a page actually does, and the
76
77
  // shared failure mode is a browser tool that returns nothing and looks clean.
@@ -0,0 +1,8 @@
1
+ /** Conservative token estimate for prompt budgeting, not provider billing. */
2
+ export function estimateStringTokens(text: string): number {
3
+ if (!text) return 0;
4
+ return Math.ceil(Math.max(
5
+ text.length / 4,
6
+ text.split(/\s+/u).filter(Boolean).length * 1.3,
7
+ ));
8
+ }
@@ -790,6 +790,7 @@ function configuredModel(
790
790
  ? {
791
791
  compat: {
792
792
  ...anthropicMessagesCompat,
793
+ // OpenRouter rejects Anthropic's mid-conversation effort messages and block binding.
793
794
  supportsMidConvoEffort: false,
794
795
  supportsEagerToolInputStreaming: false,
795
796
  supportsStrictTools:
@@ -43,6 +43,13 @@ const RETIRED_SCHEDULE_CALLBACKS = [
43
43
  "_chatRecoveryRetry",
44
44
  ] as const;
45
45
 
46
+ const LEGACY_TERMINAL_SUBMISSION_STATUSES = new Set([
47
+ "completed",
48
+ "failed",
49
+ "aborted",
50
+ "skipped",
51
+ ]);
52
+
46
53
  type LegacyRecord = Record<string, unknown> & {
47
54
  id: string;
48
55
  parentId: string | null;
@@ -310,6 +317,34 @@ function hasToolExecution(value: ToolExecution | undefined): boolean {
310
317
  typeof value.args === "object" && !Array.isArray(value.args));
311
318
  }
312
319
 
320
+ /** A terminal Submission cannot resume, so its stale lane pointer is not active work. */
321
+ function clearTerminalOperationPointers(sql: SqlStorage): void {
322
+ if (!hasTable(sql, "pi_submissions")) return;
323
+ const lanes = sql.exec<{ key: string; body: string }>(
324
+ "SELECT key, body FROM pi_values WHERE namespace = ?",
325
+ laneState("").namespace,
326
+ ).toArray();
327
+ for (const lane of lanes) {
328
+ const state = JSON.parse(lane.body) as LaneState;
329
+ if (state.currentOperationId === null) continue;
330
+ const terminal = sql.exec(
331
+ `SELECT 1 FROM pi_submissions
332
+ WHERE submission_id = ?
333
+ AND status IN ('completed', 'failed', 'aborted', 'skipped')
334
+ LIMIT 1`,
335
+ state.currentOperationId,
336
+ ).toArray().length === 1;
337
+ if (!terminal) continue;
338
+ sql.exec(
339
+ `UPDATE pi_values
340
+ SET body = json_set(body, '$.currentOperationId', json('null'))
341
+ WHERE namespace = ? AND key = ?`,
342
+ laneState("").namespace,
343
+ lane.key,
344
+ );
345
+ }
346
+ }
347
+
313
348
  /** Old active tool outcomes cannot be reconstructed from error text or approval facts. */
314
349
  function assertActiveToolProvenance(sql: SqlStorage): void {
315
350
  const read = <T>(address: Value<T>): T | undefined => {
@@ -379,6 +414,7 @@ export function migrateCurrentPiSession(
379
414
  if (version !== 1 && version !== 2) {
380
415
  throw new Error(`Unsupported Cloudflare Pi storage version: ${version}`);
381
416
  }
417
+ clearTerminalOperationPointers(sql);
382
418
  assertActiveToolProvenance(sql);
383
419
  if (version === 1) removeRetiredRuntimeArtifacts(sql);
384
420
  repairDuplicatedUserSidecars(sql);
@@ -680,6 +716,20 @@ export function migrateLegacyPiSession(
680
716
  .map((row) => [row.message_id, row.submission_id] as const)
681
717
  : [],
682
718
  );
719
+ const submissionStatuses = new Map(
720
+ hasTable(sql, "pi_submissions")
721
+ ? sql
722
+ .exec<{ submission_id: string; status: string }>(
723
+ "SELECT submission_id, status FROM pi_submissions",
724
+ )
725
+ .toArray()
726
+ .map((row) => [row.submission_id, row.status] as const)
727
+ : [],
728
+ );
729
+ const terminalSubmission = (submissionId: string): boolean =>
730
+ LEGACY_TERMINAL_SUBMISSION_STATUSES.has(
731
+ submissionStatuses.get(submissionId) ?? "",
732
+ );
683
733
  const results = new Map<string, ToolResultMessage>();
684
734
  for (const entry of converted.values()) {
685
735
  if (entry.type !== "message" || entry.message.role !== "toolResult")
@@ -809,8 +859,12 @@ export function migrateLegacyPiSession(
809
859
  if (!knownSubmissions.has(row.submission_id))
810
860
  throw new Error("Dangling recovery submission");
811
861
  const record = decodePiToolRecoveryMilestone(row.body);
812
- if (!record || record.turnId !== row.submission_id)
862
+ if (!record || record.turnId !== row.submission_id) {
863
+ if (LEGACY_TERMINAL_SUBMISSION_STATUSES.has(
864
+ submissionStatuses.get(row.submission_id) ?? "",
865
+ )) continue;
813
866
  throw new Error("Invalid legacy recovery milestone");
867
+ }
814
868
  const bodies = bySubmission.get(row.submission_id) ?? [];
815
869
  bodies.push(row.body);
816
870
  bySubmission.set(row.submission_id, bodies);
@@ -819,8 +873,12 @@ export function migrateLegacyPiSession(
819
873
  .canonicalToolResults) {
820
874
  const key = `${row.submission_id}:${result.toolCallId}`;
821
875
  const previous = results.get(key);
822
- if (previous) assertSameToolResult(previous, result);
823
- results.set(key, result);
876
+ if (previous && !terminalSubmission(row.submission_id)) {
877
+ assertSameToolResult(previous, result);
878
+ }
879
+ if (!previous || !terminalSubmission(row.submission_id)) {
880
+ results.set(key, result);
881
+ }
824
882
  }
825
883
  }
826
884
  }
@@ -832,8 +890,12 @@ export function migrateLegacyPiSession(
832
890
  if (call.result) {
833
891
  const key = `${submissionId}:${call.toolCallId}`;
834
892
  const previous = results.get(key);
835
- if (previous) assertSameToolResult(previous, call.result);
836
- results.set(key, call.result);
893
+ if (previous && !terminalSubmission(submissionId)) {
894
+ assertSameToolResult(previous, call.result);
895
+ }
896
+ if (!previous || !terminalSubmission(submissionId)) {
897
+ results.set(key, call.result);
898
+ }
837
899
  }
838
900
  }
839
901
  }
@@ -901,7 +963,9 @@ export function migrateLegacyPiSession(
901
963
  const recorded = results.get(
902
964
  `${row.submission_id}:${row.tool_call_id}`,
903
965
  );
904
- if (recorded) assertSameToolResult(recorded, expected);
966
+ if (recorded && !terminalSubmission(row.submission_id)) {
967
+ assertSameToolResult(recorded, expected);
968
+ }
905
969
  }
906
970
  }
907
971
  // The old table remains an inert backup. Failure rolls back DDL, data and the version together.
@@ -163,7 +163,7 @@ export const SPRINGBRAND_WORKER_WEBSITE_SKILL = {
163
163
  "5. Call `get_browser_console_logs` for the home page and for every primary internal page. Check navigation responses, missing assets, exceptions, CSP or CORS failures, and client fetch failures.",
164
164
  "6. Open the Website with the available browser evidence tools. For a multi-page Website, follow real links from the home page to every primary internal page and verify navigation back to the home page. Check at least the normal desktop viewport and one narrow viewport; capture and inspect a screenshot when visual appearance matters.",
165
165
  "7. Fix every failure, redeploy, and repeat the same checks. A clean home page does not prove a multi-page Website works.",
166
- "8. After browser verification succeeds, call `save_creation` exactly once with the existing entry HTML Working Copy path. For the recommended layout, that is `/creations/<website-name>/public/index.html`.",
166
+ "8. After browser verification succeeds, call `save_creation` exactly once with a user-visible title and the existing entry HTML Working Copy path. For the recommended layout, that path is `/creations/<website-name>/public/index.html`.",
167
167
  "9. Do not call the Website complete until `save_creation` returns an `artifactId`. Do not save referenced assets as separate Creations.",
168
168
  "",
169
169
  "## Publication contract",
@@ -12,7 +12,9 @@ import {
12
12
  SubmissionQueueFullError,
13
13
  type SubmissionInput,
14
14
  } from "./lifecycle";
15
- import { isTerminalSubmissionStatus, submissionAbortReason, type StoredSubmission, type SubmissionRepository } from "./repository";
15
+ import { isTerminalSubmissionStatus, submissionAbortReason, type StoredSubmission, type SubmissionOrigin, type SubmissionRepository } from "./repository";
16
+ import type { RuntimeUnattendedExecutionGrant } from "./contracts";
17
+ import type { ExecutionLevel } from "../lib/execution-level";
16
18
 
17
19
  // Pending records retain identity indefinitely; only recent attempts reserve capacity.
18
20
  const ADMISSION_ATTEMPT_WINDOW_MS = 60_000;
@@ -23,6 +25,12 @@ export interface SubmitMessageOptions {
23
25
  userMessageId?: string;
24
26
  userMessage?: UIMessage & { role: "user" };
25
27
  regenerate?: boolean;
28
+ /** `scheduled` marks a Run injection that locks ordinary input until it ends. */
29
+ origin?: SubmissionOrigin;
30
+ /** Maximum execution level allowed by the Schedule Definition. */
31
+ scheduledMaxExecutionLevel?: ExecutionLevel;
32
+ /** Trusted Host grant; accepted only for a scheduled Submission. */
33
+ unattendedExecutionGrant?: RuntimeUnattendedExecutionGrant;
26
34
  /** Internal transfer of a durable accepted steer; never a public message option. */
27
35
  steerHandoff?: { submissionId: string; steerId: string };
28
36
  }
@@ -182,6 +190,9 @@ export class SubmissionAdmission {
182
190
  ? options.userMessageId ?? null
183
191
  : null,
184
192
  runId,
193
+ origin: options.origin,
194
+ scheduledMaxExecutionLevel: options.scheduledMaxExecutionLevel,
195
+ unattendedExecutionGrant: options.unattendedExecutionGrant,
185
196
  });
186
197
  const admitted = this.options.submissions.find(runId)!;
187
198
  this.options.onAdmitted(admitted, admission?.accepted === true);
@@ -13,6 +13,8 @@ export const APPROVAL_DECISIONS = [
13
13
  "allow_level",
14
14
  ] as const satisfies readonly ApprovalDecision["decision"][];
15
15
 
16
+ export type { RuntimeUnattendedExecutionGrant } from "./unattended";
17
+
16
18
  /**
17
19
  * 记录一次待审批 Tool 执行的身份、摘要和原始输入。
18
20
  *
@@ -58,12 +60,35 @@ export type ScheduleJob =
58
60
  *
59
61
  * 触发条件与工作放在同一份规格里,使更新时可以原地替换两者,不用改变任务标识。
60
62
  */
63
+ /**
64
+ * 一条按任务时区解释的当地时间规则。
65
+ *
66
+ * @remarks
67
+ * `once` 在单个本地日历日触发;`daily` 每个本地日触发;`weekly` 在选中的每个本地星期触发
68
+ * (0 = 周日 … 6 = 周六)。所有星期共用同一个本地钟点。
69
+ *
70
+ * 规则只描述当地墙上时间;缺失/重复钟点、起止日期和夏令时语义由
71
+ * `adapter/cloudflare/scheduling` 的公共计算边界处理。
72
+ */
73
+ export type LocalScheduleRule =
74
+ | { kind: "once"; date: string; time: string }
75
+ | { kind: "daily"; time: string }
76
+ | { kind: "weekly"; weekdays: number[]; time: string };
77
+
61
78
  export interface ScheduleSpec {
62
79
  trigger:
63
80
  | { kind: "once-at"; at: string }
64
81
  | { kind: "once-in"; seconds: number }
65
82
  | { kind: "cron"; cron: string }
66
- | { kind: "interval"; seconds: number };
83
+ | { kind: "interval"; seconds: number }
84
+ | {
85
+ kind: "local";
86
+ rule: LocalScheduleRule;
87
+ /** 含当天的最早生效本地日期 `YYYY-MM-DD`。 */
88
+ startDate?: string;
89
+ /** 含当天的最后生效本地日期 `YYYY-MM-DD`。 */
90
+ endDate?: string;
91
+ };
67
92
  job: ScheduleJob;
68
93
  label?: string;
69
94
  /**
@@ -115,7 +140,9 @@ export type MessageDelivery = "steer" | "enqueue";
115
140
  export type MessageDispatchErrorCode =
116
141
  | "queue_full"
117
142
  | "not_steerable"
118
- | "invalid_message";
143
+ | "invalid_message"
144
+ /** A scheduled Run owns this Session's input until it reaches a terminal state. */
145
+ | "input_locked";
119
146
 
120
147
  export type MessageDispatchReceipt =
121
148
  | {
@@ -139,3 +166,16 @@ export type MessageDispatchReceipt =
139
166
  code: MessageDispatchErrorCode;
140
167
  message: string;
141
168
  };
169
+
170
+ /** Complete, credential-free task version prepared by a trusted Host for human confirmation. */
171
+ export interface RuntimeSchedulePreparation {
172
+ candidateId: string;
173
+ candidateVersion: string;
174
+ id?: string;
175
+ prompt: string;
176
+ label: string;
177
+ trigger: ScheduleSpec["trigger"];
178
+ timezone: string;
179
+ nextRunAt?: number;
180
+ authorization: { version: string; missing: string[]; scope: string };
181
+ }
@@ -2,7 +2,7 @@ import type { UIMessage } from "ai";
2
2
  import type { PiCanonicalUserInput } from "../conversation/message/contract";
3
3
  import type { RuntimeTelemetryCoordinator } from "../telemetry/coordinator";
4
4
  import type { RuntimeTurnEvents } from "../telemetry/turn-events";
5
- import type { HarnessHumanDecisions } from "../tools/human-decisions";
5
+ import type { HarnessHumanDecisions } from "../tools/runtime/human-decisions";
6
6
  import type { SubmitMessageOptions } from "./admission";
7
7
  import {
8
8
  projectSubmissionReceipt,
@@ -4,7 +4,7 @@ import type { ConversationHistory } from "../conversation/history";
4
4
  import type { MessageUiRepository } from "../conversation/repository";
5
5
  import type { RuntimeTelemetryCoordinator } from "../telemetry/coordinator";
6
6
  import type { RuntimeTurnEvents } from "../telemetry/turn-events";
7
- import type { HarnessHumanDecisions } from "../tools/human-decisions";
7
+ import type { HarnessHumanDecisions } from "../tools/runtime/human-decisions";
8
8
  import { USER_STOP_REASON } from "./contracts";
9
9
  import type { SubmissionOutcome } from "./lifecycle";
10
10
  import { isTerminalSubmissionStatus, type StoredSubmission, type SubmissionRepository } from "./repository";
@@ -1,5 +1,7 @@
1
1
  import type { SqlTaggedTemplate } from "agents/chat";
2
2
  import type { LaneSnapshot } from "@earendil-works/pi-agent-core";
3
+ import type { ExecutionLevel } from "../lib/execution-level";
4
+ import type { RuntimeUnattendedExecutionGrant } from "./unattended";
3
5
 
4
6
  // Submission 的统一定义见 ./index.ts。
5
7
  // #region 状态与行类型
@@ -11,6 +13,14 @@ export type SubmissionStatus =
11
13
  | "skipped"
12
14
  | "error";
13
15
 
16
+ /**
17
+ * 谁发起了这条 Submission。
18
+ *
19
+ * `scheduled` 是定时 Run 的注入:它在本 Session 未结束期间独占输入,
20
+ * 普通 `user` Submission 必须被拒绝,而不是排队等它结束。
21
+ */
22
+ export type SubmissionOrigin = "user" | "scheduled";
23
+
14
24
  const TERMINAL_SUBMISSION_STATUSES: ReadonlySet<SubmissionStatus> = new Set([
15
25
  "completed",
16
26
  "aborted",
@@ -23,7 +33,7 @@ const TERMINAL_SUBMISSION_STATUSES: ReadonlySet<SubmissionStatus> = new Set([
23
33
  // 终态集合收口在一处防止各生命周期的判断漂移;增加状态时必须先确认它是否真的不可再继续。
24
34
  export function isTerminalSubmissionStatus(
25
35
  status: SubmissionStatus,
26
- ): boolean {
36
+ ): status is Exclude<SubmissionStatus, "pending" | "running"> {
27
37
  return TERMINAL_SUBMISSION_STATUSES.has(status);
28
38
  }
29
39
 
@@ -46,6 +56,10 @@ export interface StoredSubmission {
46
56
  userMessageId: string | null;
47
57
  regenerateMessageId: string | null;
48
58
  runId: string | null;
59
+ origin: SubmissionOrigin;
60
+ scheduledMaxExecutionLevel: ExecutionLevel | null;
61
+ unattendedExecutionGrant: RuntimeUnattendedExecutionGrant | null;
62
+ unattendedFailure: string | null;
49
63
  retryable?: boolean;
50
64
  }
51
65
 
@@ -69,6 +83,9 @@ export interface NewSubmission {
69
83
  userMessageId?: string | null;
70
84
  regenerateMessageId?: string | null;
71
85
  runId?: string | null;
86
+ origin?: SubmissionOrigin;
87
+ scheduledMaxExecutionLevel?: ExecutionLevel;
88
+ unattendedExecutionGrant?: RuntimeUnattendedExecutionGrant;
72
89
  }
73
90
 
74
91
  type SubmissionRow = {
@@ -90,7 +107,11 @@ type SubmissionRow = {
90
107
  user_message_id: string | null;
91
108
  regenerate_message_id: string | null;
92
109
  run_id: string | null;
110
+ origin: string | null;
111
+ scheduled_max_execution_level: string | null;
93
112
  admission_retryable: number | null;
113
+ unattended_execution_grant_json: string | null;
114
+ unattended_failure: string | null;
94
115
  };
95
116
 
96
117
  // #endregion
@@ -118,6 +139,12 @@ function mapRow(row: SubmissionRow): StoredSubmission {
118
139
  userMessageId: row.user_message_id,
119
140
  regenerateMessageId: row.regenerate_message_id,
120
141
  runId: row.run_id,
142
+ origin: row.origin === "scheduled" ? "scheduled" : "user",
143
+ scheduledMaxExecutionLevel: row.scheduled_max_execution_level as ExecutionLevel | null,
144
+ unattendedExecutionGrant: row.unattended_execution_grant_json
145
+ ? JSON.parse(row.unattended_execution_grant_json) as RuntimeUnattendedExecutionGrant
146
+ : null,
147
+ unattendedFailure: row.unattended_failure,
121
148
  retryable: row.admission_retryable === null
122
149
  ? undefined
123
150
  : row.admission_retryable === 1,
@@ -150,7 +177,11 @@ export class SubmissionRepository {
150
177
  queued_input_json, queued_ui_message_json, user_message_id,
151
178
  regenerate_message_id,
152
179
  run_id,
153
- admission_retryable
180
+ origin,
181
+ scheduled_max_execution_level,
182
+ admission_retryable,
183
+ unattended_execution_grant_json,
184
+ unattended_failure
154
185
  FROM pi_submissions
155
186
  WHERE submission_id = ${id}
156
187
  `[0];
@@ -251,7 +282,7 @@ export class SubmissionRepository {
251
282
  created_at, assembly_revision, assembly_descriptor, runtime_key,
252
283
  assistant_message_id, queued_input_json, queued_ui_message_json,
253
284
  user_message_id, regenerate_message_id,
254
- run_id
285
+ run_id, origin, scheduled_max_execution_level, unattended_execution_grant_json
255
286
  ) VALUES (
256
287
  ${s.submissionId},
257
288
  ${s.requestId},
@@ -267,11 +298,31 @@ export class SubmissionRepository {
267
298
  ${s.queuedUiMessageJson ?? null},
268
299
  ${s.userMessageId ?? null},
269
300
  ${s.regenerateMessageId ?? null},
270
- ${s.runId ?? s.submissionId}
301
+ ${s.runId ?? s.submissionId},
302
+ ${s.origin ?? "user"},
303
+ ${s.scheduledMaxExecutionLevel ?? null},
304
+ ${s.unattendedExecutionGrant
305
+ ? JSON.stringify(s.unattendedExecutionGrant)
306
+ : null}
271
307
  )
272
308
  `;
273
309
  }
274
310
 
311
+ /**
312
+ * 是否存在一条未结束的定时 Submission。
313
+ *
314
+ * 定时 Run 未结束时它独占本 Session 的输入:普通消息在准入前被拒绝,
315
+ * 不排队等待。Run 进入终态后查询自然为空,普通追问恢复。
316
+ */
317
+ hasUnfinishedScheduled(): boolean {
318
+ return (
319
+ this.sql<{ count: number }>`
320
+ SELECT COUNT(*) AS count FROM pi_submissions
321
+ WHERE status IN ('pending', 'running') AND origin = 'scheduled'
322
+ `[0]?.count ?? 0
323
+ ) > 0;
324
+ }
325
+
275
326
  insertRejected(s: {
276
327
  submissionId: string;
277
328
  requestId: string;
@@ -310,6 +361,15 @@ export class SubmissionRepository {
310
361
  return updated.length === 1;
311
362
  }
312
363
 
364
+ recordUnattendedFailure(id: string, reason: string): void {
365
+ this.sql`
366
+ UPDATE pi_submissions
367
+ SET unattended_failure = COALESCE(unattended_failure, ${reason})
368
+ WHERE submission_id = ${id}
369
+ AND status IN ('pending', 'running')
370
+ `;
371
+ }
372
+
313
373
  // 只在当前状态属于 from 时把 Submission 切换到 to,否则返回 false。
314
374
  // SubmissionLifecycle 开始 Turn 时调用,调用方用返回值识别过期或重复的状态切换。
315
375
  // Cloudflare SQLite 查询是同步的,两条语句之间不会让其他事件交错;UPDATE 再次带旧状态条件,不能去掉这层幂等保护。
@@ -0,0 +1,40 @@
1
+ /** Host-confirmed authority pinned to one scheduled task Requirement version.
2
+ * Credentials never belong in scope; only trusted Host ports consume it.
3
+ */
4
+ export interface RuntimeUnattendedExecutionGrant {
5
+ readonly taskId: string;
6
+ readonly requirementVersion: string;
7
+ readonly authorizationVersion: string;
8
+ readonly scope: Readonly<Record<string, unknown>>;
9
+ }
10
+
11
+ export function pinnedUnattendedExecutionGrant(
12
+ value: RuntimeUnattendedExecutionGrant,
13
+ ): RuntimeUnattendedExecutionGrant {
14
+ for (const [name, version] of [
15
+ ["task ID", value.taskId],
16
+ ["Requirement", value.requirementVersion],
17
+ ["authorization", value.authorizationVersion],
18
+ ] as const) {
19
+ if (typeof version !== "string" || !version.trim()) {
20
+ throw new Error(`Unattended execution ${name} is required`);
21
+ }
22
+ }
23
+ if (!value.scope || typeof value.scope !== "object" || Array.isArray(value.scope)) {
24
+ throw new Error("Unattended execution scope must be an object");
25
+ }
26
+ try {
27
+ const scope = JSON.parse(JSON.stringify(value.scope)) as unknown;
28
+ if (!scope || typeof scope !== "object" || Array.isArray(scope)) {
29
+ throw new Error("scope changed shape during serialization");
30
+ }
31
+ return Object.freeze({
32
+ taskId: value.taskId,
33
+ requirementVersion: value.requirementVersion,
34
+ authorizationVersion: value.authorizationVersion,
35
+ scope: scope as Readonly<Record<string, unknown>>,
36
+ });
37
+ } catch (error) {
38
+ throw new Error("Unattended execution scope must be JSON-serializable", { cause: error });
39
+ }
40
+ }
@@ -5,7 +5,7 @@ import { TelemetryRecorder } from "./recorder";
5
5
  import { RuntimeTelemetry } from "./runtime-telemetry";
6
6
  import type { AgentTelemetryBinding } from "./contract";
7
7
  import type { PiGenerationLifecycleEvent } from "../models";
8
- import type { TemporaryAgentLifecycleEvent } from "../tools/temporary-agent/coordinator";
8
+ import type { TemporaryAgentLifecycleEvent } from "../tools/subagent/temporary/coordinator";
9
9
 
10
10
  const TELEMETRY_COLD_BUFFER_LIMIT = 1_000;
11
11
 
@@ -2,7 +2,7 @@ import type {
2
2
  RuntimeEventConfirmation,
3
3
  RuntimeLifecycleFact,
4
4
  RuntimeSubagentUsageEvent,
5
- } from "../tools/subagent-authority";
5
+ } from "../tools/subagent/authority";
6
6
 
7
7
  export interface RuntimeLifecycleSink {
8
8
  save(
@@ -7,7 +7,7 @@ import type {
7
7
  RuntimeTurnEventsPort,
8
8
  } from "../config/ports";
9
9
  import type { StoredSubmission } from "../submission/repository";
10
- import { decodeSubagentCompletion, isZeroSubagentUsage, subagentTerminalStatus, type RuntimeSubagentRunMetadata } from "../tools/subagent-runtime";
10
+ import { decodeSubagentCompletion, isZeroSubagentUsage, subagentTerminalStatus, type RuntimeSubagentRunMetadata } from "../tools/subagent/runtime";
11
11
  import type { RuntimeTelemetryCoordinator } from "./coordinator";
12
12
  import { summarizeModelUsage } from "./model-usage";
13
13
  import type { RuntimeEventOutboxRepository } from "./runtime-event-repository";
@@ -0,0 +1,44 @@
1
+ # Runtime Tools
2
+
3
+ 工具按功能归组。根 `index.ts` 只维护包内统一导出;功能目录的 `index.ts` 定义或组合该功能的 Pi Tool。
4
+
5
+ | 目录 | 职责 |
6
+ | --- | --- |
7
+ | `runtime/` | 公共工具契约、Harness 编译、执行决策、审批与客户端交互结算 |
8
+ | `adapters/` | AI SDK ↔ Pi 转换,以及外部工具声明到 Pi Candidate 的适配 |
9
+ | `codemode/` | `execute` 和 `browser_execute` 的 Pi Tool 定义、Cloudflare 执行适配、Workspace Connector |
10
+ | `assistant/` | 用户问答、计划、后续建议、记忆与时间工具 |
11
+ | `workspace/` | 持久 Workspace 文件操作、写入串行化与版本操作工具 |
12
+ | `sandbox/` | 临时 Linux Sandbox 的命令、进程和显式文件发布工具 |
13
+ | `skill/` | Skill 工具组合、资源读取与复制、脚本运行与输出投影 |
14
+ | `subagent/` | 子 Agent 工具、权限、运行记录、类型和临时 Agent 协调 |
15
+ | `schedule/` | 定时任务的 Pi Tool 定义 |
16
+ | `web/` | 网页获取与 Provider-native 搜索 |
17
+ | `mcp/` | MCP Tool 接入及使用 MCP Session 的 Connector Gateway |
18
+ | `extension/` | 已加载 Extension 的发现工具;Extension 装载仍归 `context/extension/` |
19
+
20
+ ## Code Mode
21
+
22
+ ```text
23
+ codemode/index.ts Pi Tool 定义、执行级别与调用截止
24
+ |
25
+ v
26
+ RuntimeCodeExecutionPort 内部执行接口
27
+ |
28
+ v
29
+ codemode/adapter.ts Cloudflare 执行器组装、描述与结果适配
30
+ |
31
+ +-- workspace-connector.ts state.* 方法、发现与校验
32
+ +-- ../adapters/pi-to-ai.ts 现有 Pi 工具到 Cloudflare ToolSet 的转换
33
+ ```
34
+
35
+ `pi-to-ai.ts` 也被 Skill 脚本执行使用,因此放在共用 `adapters/` 中。工具声明仍以 Pi 为唯一事实源。
36
+
37
+ ## 修改位置
38
+
39
+ - 修改某个工具:进入对应功能目录,测试与实现放在一起。
40
+ - 修改所有工具共用的执行策略:进入 `runtime/`,不要复制到每个工具。
41
+ - 修改外部协议转换:进入 `adapters/`,不新增另一份工具定义。
42
+ - 小功能可以只有 `index.ts`,不强制拆分 Adapter、Service 或 Repository。
43
+ - `runtime/result.ts` 只共享结果包装与进度报告;`assistant/candidate.ts` 只共享助手工具已有的元数据默认值。
44
+ - 包的公共导出名及 `@springbrand/agent-runtime/declared-tool` 子路径保持不变。内部路径变更需要同步 Vitest 测试池配置。