@springbrand/agent-runtime 0.2.0-alpha.71 → 0.3.0-alpha.1

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 (141) hide show
  1. package/package.json +3 -3
  2. package/src/{agent-tool-runtime.ts → adapter/cloudflare/agent-tool.ts} +1 -1
  3. package/src/adapter/cloudflare/agent.ts +1539 -0
  4. package/src/{db/index.ts → adapter/cloudflare/database.ts} +18 -55
  5. package/src/{runtime-agent.ts → adapter/cloudflare/define-agent.ts} +25 -35
  6. package/src/adapter/cloudflare/dynamic-worker-observability.ts +1 -1
  7. package/src/adapter/cloudflare/resources/runtime-resources.ts +4 -4
  8. package/src/adapter/cloudflare/sandbox/adapter.ts +1 -1
  9. package/src/{db → adapter/cloudflare}/schema.ts +16 -83
  10. package/src/adapter/cloudflare/subagent/definition.ts +63 -33
  11. package/src/adapter/cloudflare/subagent/runner.ts +174 -120
  12. package/src/adapter/cloudflare/subagent/tools.ts +13 -14
  13. package/src/adapter/cloudflare/universal-agent/preparation.ts +7 -7
  14. package/src/adapter/cloudflare/universal-agent/tools.ts +10 -10
  15. package/src/adapter/cloudflare/workspace/git-fs.ts +1 -1
  16. package/src/adapter/cloudflare/workspace/scoped-workspace.ts +1 -1
  17. package/src/adapter/cloudflare/workspace/version-control.ts +2 -2
  18. package/src/config/activation.ts +140 -0
  19. package/src/{runtime-agent-context.ts → config/agent-context.ts} +3 -3
  20. package/src/{runtime-assembler.ts → config/assembler.ts} +20 -19
  21. package/src/{kernel/runtime-assembly.ts → config/assembly-projection.ts} +4 -4
  22. package/src/{pi/assembly/snapshot.ts → config/assembly-snapshot.ts} +6 -6
  23. package/src/{runtime-definition.ts → config/definition.ts} +13 -13
  24. package/src/config/harness-runtime.ts +182 -0
  25. package/src/config/index.ts +14 -0
  26. package/src/{pi/runtime-adapter/assembly.ts → config/pinning.ts} +38 -16
  27. package/src/{kernel/bindings.ts → config/ports.ts} +9 -13
  28. package/src/{kernel → config}/profile.ts +7 -0
  29. package/src/{kernel → config}/public-contracts.ts +6 -6
  30. package/src/{kernel → config}/runtime-load.ts +1 -1
  31. package/src/{pi/assembly/context.ts → context/assembly.ts} +4 -206
  32. package/src/{pi/assembly/extensions.ts → context/extension/assembly.ts} +5 -5
  33. package/src/context/extension/index.ts +3 -0
  34. package/src/context/index.ts +6 -0
  35. package/src/{layers/context → context/memory}/budget/gate.ts +2 -2
  36. package/src/conversation/harness-resume.ts +288 -0
  37. package/src/conversation/harness-stream.ts +98 -0
  38. package/src/{pi/runtime-adapter/transcript.ts → conversation/history.ts} +206 -233
  39. package/src/conversation/index.ts +6 -0
  40. package/src/{pi → conversation}/message/contract.ts +3 -0
  41. package/src/{pi → conversation}/message/conversion.ts +75 -0
  42. package/src/{pi → conversation}/message/index.ts +1 -0
  43. package/src/{pi → conversation}/message/model-input.ts +1 -1
  44. package/src/{pi → conversation}/message/projection.ts +76 -10
  45. package/src/{db/message-ui.repo.ts → conversation/repository.ts} +4 -0
  46. package/src/conversation/state.ts +175 -0
  47. package/src/harness/index.ts +7 -0
  48. package/src/harness/session-harness.ts +481 -0
  49. package/src/harness/submission-operation.ts +49 -0
  50. package/src/index.ts +38 -43
  51. package/src/lib/mcp.ts +1 -1
  52. package/src/{pi/runtime-adapter/models.ts → models/index.ts} +65 -17
  53. package/src/{pi/turn/tool-recovery.ts → session/cloudflare/legacy-tool-recovery.ts} +33 -525
  54. package/src/{pi/session → session/cloudflare}/migration.ts +334 -8
  55. package/src/{pi/session → session/cloudflare}/storage.ts +8 -36
  56. package/src/session/index.ts +39 -0
  57. package/src/submission/admission.ts +275 -0
  58. package/src/{kernel/receipts.ts → submission/contracts.ts} +14 -0
  59. package/src/submission/delivery.ts +212 -0
  60. package/src/submission/finalization.ts +111 -0
  61. package/src/submission/index.ts +9 -0
  62. package/src/{kernel/submission-lifecycle.ts → submission/lifecycle.ts} +41 -28
  63. package/src/{db/submission.repo.ts → submission/repository.ts} +61 -118
  64. package/src/submission/steer-delivery.ts +167 -0
  65. package/src/{db/steer.repo.ts → submission/steer-repository.ts} +27 -5
  66. package/src/telemetry/contract.ts +12 -0
  67. package/src/telemetry/coordinator.ts +65 -0
  68. package/src/telemetry/delivery.ts +2 -2
  69. package/src/telemetry/index.ts +5 -0
  70. package/src/{model-usage.ts → telemetry/model-usage.ts} +1 -1
  71. package/src/telemetry/recorder.ts +1 -1
  72. package/src/telemetry/runtime-telemetry.ts +12 -0
  73. package/src/{kernel/durable-lifecycle.ts → telemetry/subagent-lifecycle.ts} +1 -1
  74. package/src/telemetry/turn-events.ts +279 -0
  75. package/src/{pi/tool → tools}/ai-adapter.ts +2 -2
  76. package/src/tools/approval/index.ts +1 -0
  77. package/src/{db/approval.repo.ts → tools/approval/repository.ts} +10 -30
  78. package/src/{pi/tool → tools}/base.ts +14 -30
  79. package/src/tools/compiler.ts +327 -0
  80. package/src/{pi/tool → tools}/core-host.ts +3 -3
  81. package/src/{pi/tool → tools}/core.ts +3 -3
  82. package/src/tools/decision-gate.ts +227 -0
  83. package/src/{pi/tool → tools}/gateway.ts +1 -1
  84. package/src/tools/human-decisions.ts +127 -0
  85. package/src/tools/index.ts +30 -0
  86. package/src/tools/interaction/index.ts +1 -0
  87. package/src/{pi/tool → tools}/mcp.ts +1 -1
  88. package/src/{pi/tool → tools}/schedule.ts +3 -3
  89. package/src/{pi/tool → tools}/skill.ts +4 -4
  90. package/src/{kernel → tools}/subagent-runtime.ts +1 -1
  91. package/src/{pi/tool → tools}/subagent.ts +4 -4
  92. package/src/tools/temporary-agent/approvals.ts +126 -0
  93. package/src/{layers/orchestration/temporary-agent/core.ts → tools/temporary-agent/coordinator.ts} +41 -49
  94. package/src/tools/temporary-agent/core.ts +53 -0
  95. package/src/{layers/orchestration → tools}/temporary-agent/runner.ts +1 -1
  96. package/src/{layers/orchestration → tools}/temporary-agent/workspace.ts +1 -1
  97. package/src/{pi/tool → tools}/web-search/api.ts +1 -1
  98. package/src/{pi/tool → tools}/workspace-revision.ts +2 -2
  99. package/src/{pi/tool → tools}/workspace-sandbox.ts +2 -2
  100. package/src/{pi/tool → tools}/workspace-state-connector.ts +1 -1
  101. package/src/db/milestone.repo.ts +0 -104
  102. package/src/db/settlement.repo.ts +0 -104
  103. package/src/kernel/approval-lifecycle.ts +0 -638
  104. package/src/kernel/interaction-lifecycle.ts +0 -433
  105. package/src/kernel/recoverable-chat-agent.ts +0 -1020
  106. package/src/kernel/state.ts +0 -91
  107. package/src/pi/assembly/index.ts +0 -19
  108. package/src/pi/runtime-adapter/execution.ts +0 -1163
  109. package/src/pi/runtime-adapter/index.ts +0 -293
  110. package/src/pi/runtime-adapter/recovery.ts +0 -982
  111. package/src/pi/session/index.ts +0 -24
  112. package/src/pi/tool/compiler.ts +0 -483
  113. package/src/pi/tool/index.ts +0 -38
  114. package/src/pi/turn/approval.ts +0 -181
  115. package/src/pi/turn/index.ts +0 -82
  116. package/src/pi/turn/interaction.ts +0 -181
  117. package/src/runtime.ts +0 -4760
  118. /package/src/{kernel/runtime-assembly-view.ts → config/assembly-view.ts} +0 -0
  119. /package/src/{kernel → config}/degradation.ts +0 -0
  120. /package/src/{kernel → config}/runtime-config.ts +0 -0
  121. /package/src/{kernel/extensions.ts → context/extension/contracts.ts} +0 -0
  122. /package/src/{db/ext-context.repo.ts → context/extension/repository.ts} +0 -0
  123. /package/src/{pi/runtime-adapter/openrouter-messages.ts → models/openrouter/messages.ts} +0 -0
  124. /package/src/{pi/runtime-adapter → models/openrouter}/test-fixtures/openrouter-tool-search.sse +0 -0
  125. /package/src/{db/submission-admission.repo.ts → submission/admission-repository.ts} +0 -0
  126. /package/src/{db/telemetry-outbox.repo.ts → telemetry/repository.ts} +0 -0
  127. /package/src/{db/runtime-event-outbox.repo.ts → telemetry/runtime-event-repository.ts} +0 -0
  128. /package/src/{pi/tool → tools}/declared.ts +0 -0
  129. /package/src/{db/interaction.repo.ts → tools/interaction/repository.ts} +0 -0
  130. /package/src/{pi/tool → tools}/nested-tools.ts +0 -0
  131. /package/src/{kernel/submission-authority.ts → tools/subagent-authority.ts} +0 -0
  132. /package/src/{db/agent-tool.repo.ts → tools/subagent-run-repository.ts} +0 -0
  133. /package/src/{layers/orchestration/subagents/agent-types → tools/subagent-types}/contract.ts +0 -0
  134. /package/src/{layers/orchestration/subagents/agent-types → tools/subagent-types}/extract/index.ts +0 -0
  135. /package/src/{layers/orchestration/subagents/agent-types → tools/subagent-types}/fanout/index.ts +0 -0
  136. /package/src/{layers/orchestration/subagents/agent-types → tools/subagent-types}/registry.ts +0 -0
  137. /package/src/{pi/tool → tools}/time.ts +0 -0
  138. /package/src/{pi/tool → tools}/web-fetch.ts +0 -0
  139. /package/src/{pi/tool → tools}/web-search/index.ts +0 -0
  140. /package/src/{pi/tool → tools}/web-search/web-search.ts +0 -0
  141. /package/src/{workspace-versioning.ts → tools/workspace-versioning.ts} +0 -0
@@ -3,12 +3,12 @@ import type {
3
3
  ChatCapableAgentClass,
4
4
  RunAgentToolOptions,
5
5
  } from "agents";
6
- import type { ExecutionLevel } from "./lib/execution-level";
7
- import type { RuntimeConfigUpdateResult } from "./kernel/runtime-config";
6
+ import type { ExecutionLevel } from "../lib/execution-level";
7
+ import type { RuntimeConfigUpdateResult } from "./runtime-config";
8
8
  import type {
9
9
  TemporaryAgentRequest,
10
10
  TemporaryAgentRunContext,
11
- } from "./layers/orchestration/temporary-agent/core";
11
+ } from "../tools/temporary-agent/core";
12
12
 
13
13
  /**
14
14
  * 描述 Agent 寻址路径中的一级父子节点。
@@ -14,7 +14,7 @@ import type {
14
14
  RuntimeBindings,
15
15
  RuntimeSkillScriptPolicy,
16
16
  WorkspacePort,
17
- } from "./kernel/bindings";
17
+ } from "./ports";
18
18
  import {
19
19
  freezeRuntimeProfile,
20
20
  type RuntimeDenyPolicy,
@@ -22,24 +22,24 @@ import {
22
22
  type RuntimeMemoryProfile,
23
23
  type RuntimeProfile,
24
24
  type ThinkingEffort,
25
- } from "./kernel/profile";
26
- import type { RuntimeExtensionConfig } from "./kernel/extensions";
27
- import type { RuntimeDegradation } from "./kernel/degradation";
25
+ } from "./profile";
26
+ import type { RuntimeExtensionConfig } from "../context/extension/contracts";
27
+ import type { RuntimeDegradation } from "./degradation";
28
28
  import {
29
29
  EXECUTION_LEVELS,
30
30
  type ExecutionLevel,
31
- } from "./lib/execution-level";
31
+ } from "../lib/execution-level";
32
32
  import {
33
33
  createPiRuntimeAssembly,
34
34
  type FinalizedPiToolSurface,
35
35
  type PiToolSurface,
36
36
  type PiRuntimeAssembly,
37
- } from "./pi/assembly/snapshot";
37
+ } from "./assembly-snapshot";
38
38
  import type { AgentTool } from "@earendil-works/pi-agent-core";
39
39
  import type {
40
40
  PiToolCandidate,
41
41
  ToolExposureMode,
42
- } from "./pi/tool/compiler";
42
+ } from "../tools/compiler";
43
43
  import type {
44
44
  AssembleRuntimeSnapshotInput,
45
45
  RuntimeSettings,
@@ -48,36 +48,36 @@ import type {
48
48
  RuntimeSkillBodyTransform,
49
49
  RuntimeSkillContribution,
50
50
  RuntimeToolSurfacePolicy,
51
- } from "./runtime-definition";
52
- import type { RuntimeCodeExecutionFactory } from "./kernel/bindings";
51
+ } from "./definition";
52
+ import type { RuntimeCodeExecutionFactory } from "./ports";
53
53
  import {
54
54
  basePiToolCandidates,
55
55
  memoryPiToolCandidate,
56
- } from "./pi/tool/base";
56
+ } from "../tools/base";
57
57
  import {
58
58
  BROWSER_EXECUTE_TOOL_NAME,
59
59
  browserExecutionPiToolCandidate,
60
60
  codeExecutionPiToolCandidate,
61
- } from "./pi/tool/core";
62
- import { skillPiToolCandidates } from "./pi/tool/skill";
63
- import { createWebSearch, WEB_SEARCH_MODEL } from "./pi/tool/web-search";
64
- import { subagentPiToolCandidates } from "./pi/tool/subagent";
65
- import { resolvePiModel } from "./pi/runtime-adapter/models";
61
+ } from "../tools/core";
62
+ import { skillPiToolCandidates } from "../tools/skill";
63
+ import { createWebSearch, WEB_SEARCH_MODEL } from "../tools/web-search";
64
+ import { subagentPiToolCandidates } from "../tools/subagent";
65
+ import { resolvePiModel } from "../models";
66
66
  import {
67
67
  normalizeAgentTelemetryBinding,
68
68
  type AgentTelemetryBinding,
69
- } from "./telemetry/contract";
70
- import { BUILT_IN_RUNTIME_SKILLS } from "./skills";
69
+ } from "../telemetry/contract";
70
+ import { BUILT_IN_RUNTIME_SKILLS } from "../skills";
71
71
 
72
72
  /** Runtime Assembler:校验一份扁平输入并生成可原子提交的 Snapshot。 */
73
73
 
74
- export type { RuntimeDegradation } from "./kernel/degradation";
74
+ export type { RuntimeDegradation } from "./degradation";
75
75
  export type {
76
76
  RuntimeExtensionContribution,
77
77
  RuntimeProfileContribution,
78
78
  RuntimeSkillContribution,
79
79
  RuntimeToolSurfacePolicy,
80
- } from "./runtime-definition";
80
+ } from "./definition";
81
81
 
82
82
  /** Host Planner 与 Runtime Assembler 之间的一次性内部 seam。 */
83
83
  export interface RuntimeAssemblyInput {
@@ -127,6 +127,7 @@ export interface RuntimeSnapshot {
127
127
  */
128
128
  export interface RuntimeCandidate {
129
129
  readonly snapshot: RuntimeSnapshot;
130
+ readonly runtimeKey?: string;
130
131
  readonly commitGuards: readonly (() => Promise<void>)[];
131
132
  readonly admission?: RuntimeSubmissionAdmissionHook;
132
133
  readonly telemetry?: AgentTelemetryBinding;
@@ -1,7 +1,7 @@
1
- import type { RuntimeSnapshot } from "../runtime-assembler";
2
- import type { PreparedPiRuntime } from "../pi/runtime-adapter";
3
- import type { RuntimeAssemblyView } from "./runtime-assembly-view";
4
- export type * from "./runtime-assembly-view";
1
+ import type { RuntimeSnapshot } from "./assembler";
2
+ import type { PreparedPiRuntime } from "./harness-runtime";
3
+ import type { RuntimeAssemblyView } from "./assembly-view";
4
+ export type * from "./assembly-view";
5
5
 
6
6
  /**
7
7
  * Serializable view of the Runtime Snapshot currently installed on a Session facet.
@@ -11,14 +11,14 @@ import type {
11
11
  RuntimeMcpServer,
12
12
  RuntimeProfile,
13
13
  ThinkingEffort,
14
- } from "../../kernel/profile";
14
+ } from "./profile";
15
15
  import type {
16
16
  RuntimeExtensionConfig,
17
- } from "../../kernel/extensions";
18
- import type { RuntimeProviderPort } from "../../kernel/bindings";
19
- import { assembleSystemPrompt } from "../../lib/prompt";
20
- import { resolvePiModel } from "../runtime-adapter/models";
21
- import type { PiToolCandidate } from "../tool/compiler";
17
+ } from "../context/extension/contracts";
18
+ import type { RuntimeProviderPort } from "./ports";
19
+ import { assembleSystemPrompt } from "../lib/prompt";
20
+ import { resolvePiModel } from "../models";
21
+ import type { PiToolCandidate } from "../tools/compiler";
22
22
 
23
23
  /**
24
24
  * 收集完动态 Extension/MCP 候选后,一次性生成最终 Tool Surface。
@@ -1,5 +1,5 @@
1
1
  import type { SkillSource } from "agents/skills";
2
- import type { ExecutionLevel } from "./lib/execution-level";
2
+ import type { ExecutionLevel } from "../lib/execution-level";
3
3
  import type {
4
4
  RuntimeGatewayPort,
5
5
  RuntimeMcpHost,
@@ -8,21 +8,21 @@ import type {
8
8
  RuntimeSkillScriptPolicy,
9
9
  RuntimeSubmissionAdmissionHook,
10
10
  RuntimeTurnEventsPort,
11
- } from "./kernel/bindings";
11
+ } from "./ports";
12
12
  import type {
13
13
  RuntimeDenyPolicy,
14
14
  RuntimeMcpServer,
15
15
  RuntimeMemoryProfile,
16
16
  ThinkingEffort,
17
- } from "./kernel/profile";
18
- import type { RuntimeExtensionConfig } from "./kernel/extensions";
19
- import type { RuntimeDegradation } from "./kernel/degradation";
20
- import type { PiToolCandidate } from "./pi/tool/compiler";
17
+ } from "./profile";
18
+ import type { RuntimeExtensionConfig } from "../context/extension/contracts";
19
+ import type { RuntimeDegradation } from "./degradation";
20
+ import type { PiToolCandidate } from "../tools/compiler";
21
21
  import type {
22
22
  RuntimeAgentContext,
23
23
  RuntimeAgentPlanningContext,
24
- } from "./runtime-agent-context";
25
- import type { AgentTelemetryBinding } from "./telemetry/contract";
24
+ } from "./agent-context";
25
+ import type { AgentTelemetryBinding } from "../telemetry/contract";
26
26
 
27
27
  /** Tool Surface 筛选策略(不含 Manifest deny 列表)。 */
28
28
  export interface ToolSurfaceSelectionPolicy {
@@ -33,10 +33,10 @@ export interface ToolSurfaceSelectionPolicy {
33
33
 
34
34
  /** Kernel 绑定仍需要的执行后端;不出现在 Definition 公开类型里。 */
35
35
  export interface RuntimeToolBindings {
36
- readonly workspace?: import("./kernel/bindings").WorkspacePort;
37
- readonly codeExecution?: import("./kernel/bindings").RuntimeCodeExecutionFactory;
38
- readonly memory?: import("./kernel/bindings").RuntimeMemoryPort;
39
- readonly subagents?: import("./kernel/bindings").RuntimeSubagentPort;
36
+ readonly workspace?: import("./ports").WorkspacePort;
37
+ readonly codeExecution?: import("./ports").RuntimeCodeExecutionFactory;
38
+ readonly memory?: import("./ports").RuntimeMemoryPort;
39
+ readonly subagents?: import("./ports").RuntimeSubagentPort;
40
40
  }
41
41
 
42
42
  /** Host 一次装配原子提交的 Tool candidates 与 Runtime 绑定。 */
@@ -75,7 +75,7 @@ export interface RuntimeToolSurfacePolicy extends ToolSurfaceSelectionPolicy {
75
75
  readonly denyPolicy?: RuntimeDenyPolicy;
76
76
  }
77
77
 
78
- export type { RuntimeAgentContext, RuntimeAssemblyContext } from "./runtime-agent-context";
78
+ export type { RuntimeAgentContext, RuntimeAssemblyContext } from "./agent-context";
79
79
 
80
80
  export interface ResolvedConnectors {
81
81
  readonly servers: readonly RuntimeMcpServer[];
@@ -0,0 +1,182 @@
1
+ import {
2
+ createModels,
3
+ type MutableModels,
4
+ } from "@earendil-works/pi-ai";
5
+ import {
6
+ configurePiModels,
7
+ createPiModels,
8
+ withProviderBoundary,
9
+ type PiGenerationLifecycleObserver,
10
+ } from "../models";
11
+ export {
12
+ MODEL_STREAM_STALL_TIMEOUT_MS,
13
+ modelRequestUrl,
14
+ readModelStreamStallDetails,
15
+ } from "../models";
16
+ import {
17
+ pinPiRuntime,
18
+ preparePiRuntime,
19
+ readPinnedPiRuntime,
20
+ readPreparedPiRuntime,
21
+ type PinPiRuntimeOptions,
22
+ type PinnedPiRuntime,
23
+ type PreparePiRuntimeOptions,
24
+ type PreparedPiRuntime,
25
+ } from "./pinning";
26
+
27
+ /**
28
+ * Runtime 通过这个配置入口准备、固定并读取 Harness 的模型、提示和工具输入。
29
+ *
30
+ * @remarks
31
+ * Runtime、Pi、Turn、Tool Candidate 和 canonical transcript 等核心术语见包入口 `../../index.ts`。
32
+ * 本适配器只补充以下局部术语。
33
+ *
34
+ * - Prepared Runtime 是配置加载期产生的不透明句柄。
35
+ * 它包含已加载 Extension、Tool candidate 和快照,但 Runtime 只能读取 revision descriptor 与 degradation。
36
+ * - revision descriptor 是影响执行行为的已排序 JSON 描述。
37
+ * Runtime 会对它取哈希,用来识别当前 Prepared Runtime 的基础版本。
38
+ * - Pinned Runtime 是 Submission 准入时固定的描述符。
39
+ * 它在 Prepared Runtime 基础上加入动态 system context、approval mode 和 base revision。
40
+ * 调用方:`AgentRuntimeKernel` 在配置加载、请求准入与打开 SessionHarness 前调用这些粗粒度能力。
41
+ *
42
+ * 执行位置仍由 `SessionHarness` 拥有,消息读写和投影由 `ConversationHistory` 拥有。
43
+ */
44
+ export class HarnessRuntimeConfig {
45
+ private readonly owner = {};
46
+ private readonly models: MutableModels;
47
+ private readonly managesModels: boolean;
48
+
49
+ /**
50
+ * 创建一个持有独立 Pi 模型目录的 Runtime 适配器。
51
+ *
52
+ * @remarks
53
+ * 调用方:生产 Runtime 使用零参数构造,测试或专用宿主可以注入已经配置好的模型目录。
54
+ *
55
+ * 实现理由:适配器只管理自己创建的目录;注入目录的生命周期和配置权仍属于注入方。
56
+ * 不要让 `activate()` 重配注入目录,否则会覆盖调用方准备的 provider 和模型替身。
57
+ */
58
+ constructor(options: { models?: MutableModels } = {}) {
59
+ this.models = options.models ?? createModels();
60
+ this.managesModels = !options.models;
61
+ }
62
+
63
+ /**
64
+ * 加载并预检一份可供后续准入使用的 Prepared Runtime。
65
+ *
66
+ * @remarks
67
+ * 调用方:Runtime 的 `initConfig()` 在取得新快照后调用,再用返回的 revision descriptor 判断是否可以切换配置。
68
+ *
69
+ * 实现理由:Extension 加载、Tool candidate 汇总和 Tool 编译预检在配置切换前一次完成。
70
+ * 返回值绑定当前 Adapter 的 owner,不能交给另一个 Adapter 创建 Turn。
71
+ */
72
+ prepare(
73
+ options: PreparePiRuntimeOptions,
74
+ ): Promise<PreparedPiRuntime> {
75
+ if (this.managesModels) {
76
+ createPiModels(options.snapshot.bindings.provider);
77
+ }
78
+ return preparePiRuntime(options, this.owner);
79
+ }
80
+
81
+ hasTool(prepared: PreparedPiRuntime, name: string): boolean {
82
+ return readPreparedPiRuntime(prepared, this.owner).candidates.some(
83
+ (candidate) => candidate.tool.name === name,
84
+ );
85
+ }
86
+
87
+ /** Read the immutable inputs consumed by the Session AgentHarness. */
88
+ harnessInputs(
89
+ prepared: PreparedPiRuntime,
90
+ pinnedDescriptor: string,
91
+ request: {
92
+ readonly modelSessionId?: string;
93
+ readonly onGeneration?: PiGenerationLifecycleObserver;
94
+ } = {},
95
+ ) {
96
+ const { state, descriptor } = readPinnedPiRuntime(
97
+ prepared,
98
+ this.owner,
99
+ pinnedDescriptor,
100
+ );
101
+ const pinnedModel = state.snapshot.pi.model;
102
+ let models!: MutableModels;
103
+ models = new Proxy(this.models, {
104
+ get: (target, property) => {
105
+ if (property === "getModel") {
106
+ return (provider: string, modelId: string) =>
107
+ provider === pinnedModel.provider && modelId === pinnedModel.id
108
+ ? pinnedModel
109
+ : target.getModel(provider, modelId);
110
+ }
111
+ if (property === "streamSimple") {
112
+ return withProviderBoundary(
113
+ target.streamSimple.bind(target),
114
+ request.modelSessionId,
115
+ request.onGeneration,
116
+ );
117
+ }
118
+ if (property === "completeSimple") {
119
+ return target.completeSimple.bind(models);
120
+ }
121
+ const value = Reflect.get(target, property, target);
122
+ return typeof value === "function" ? value.bind(target) : value;
123
+ },
124
+ });
125
+ return {
126
+ models,
127
+ model: state.snapshot.pi.model,
128
+ thinkingLevel: state.snapshot.pi.thinkingLevel,
129
+ systemPrompt: descriptor.systemPrompt,
130
+ candidates: state.candidates,
131
+ workspace: state.snapshot.bindings.workspace,
132
+ platform: state.snapshot.bindings.platform,
133
+ compaction: {
134
+ enabled: true,
135
+ // Pi compacts above contextWindow - reserveTokens. Keep the same
136
+ // 15% headroom within the available input budget as the pre-Harness runtime.
137
+ reserveTokens: Math.ceil(state.snapshot.pi.model.contextWindow -
138
+ (state.snapshot.pi.model.contextWindow - state.snapshot.pi.model.maxTokens) * 0.85),
139
+ keepRecentTokens: Math.min(
140
+ 20_000,
141
+ Math.floor(state.snapshot.pi.model.contextWindow * 0.15),
142
+ ),
143
+ },
144
+ } as const;
145
+ }
146
+
147
+ /**
148
+ * 用已经通过加载检查的快照启用当前部署声明的模型目录。
149
+ *
150
+ * @remarks
151
+ * 调用方:Runtime 的 `initConfig()` 在 Prepared Runtime 和 revision 检查成功后、发布新快照前调用。
152
+ *
153
+ * 实现理由:`prepare()` 已校验所有 endpoint 模型,这里只把同一份部署目录切换到长期 Models 实例。
154
+ * 注入模型目录时跳过此步骤,以保留调用方对目录的配置权。
155
+ */
156
+ activate(snapshot: PreparePiRuntimeOptions["snapshot"]): void {
157
+ if (this.managesModels) {
158
+ configurePiModels(this.models, snapshot.bindings.provider);
159
+ }
160
+ }
161
+
162
+ /**
163
+ * 在接受 Submission 前固定本次执行使用的动态上下文和审批模式。
164
+ *
165
+ * @remarks
166
+ * 调用方:Runtime 的准入流程为每个新 Submission 调用,并把 descriptor 与其哈希一起持久化。
167
+ *
168
+ * 实现理由:memory、workspace 和 Extension context 可能随时间变化,所以把准入时的 Turn 字段保存在 descriptor 中。
169
+ * 不要把 pin 推迟到 Turn 启动后,否则已持久化的 Submission 将失去可验证的执行基线。
170
+ */
171
+ pin(options: PinPiRuntimeOptions): Promise<PinnedPiRuntime> {
172
+ return pinPiRuntime(options, this.owner);
173
+ }
174
+
175
+ }
176
+
177
+ export type {
178
+ PinPiRuntimeOptions,
179
+ PinnedPiRuntime,
180
+ PreparePiRuntimeOptions,
181
+ PreparedPiRuntime,
182
+ } from "./pinning";
@@ -0,0 +1,14 @@
1
+ export * from "./activation";
2
+ export * from "./agent-context";
3
+ export * from "./assembler";
4
+ export * from "./assembly-projection";
5
+ export * from "./assembly-snapshot";
6
+ export * from "./assembly-view";
7
+ export * from "./definition";
8
+ export * from "./degradation";
9
+ export * from "./harness-runtime";
10
+ export * from "./pinning";
11
+ export * from "./ports";
12
+ export * from "./profile";
13
+ export * from "./runtime-config";
14
+ export * from "./runtime-load";
@@ -1,16 +1,17 @@
1
+ import { sha256Hex } from "../lib/artifacts";
1
2
  import type {
2
3
  RuntimeBindings,
3
4
  RuntimeGatewaySession,
4
5
  RuntimePlatformPort,
5
- } from "../../kernel/bindings";
6
- import type { RuntimeDegradation } from "../../kernel/degradation";
6
+ } from "./ports";
7
+ import type { RuntimeDegradation } from "./degradation";
7
8
  import type {
8
9
  RuntimeExtensionPermissions,
9
- } from "../../kernel/extensions";
10
- import type { RuntimeProfile } from "../../kernel/profile";
11
- import type { ExecutionLevel } from "../../lib/execution-level";
12
- import type { RuntimeSnapshot } from "../../runtime-assembler";
13
- import type { FinalizedPiToolSurface, PiRuntimeAssembly } from "../assembly";
10
+ } from "../context/extension/contracts";
11
+ import type { RuntimeProfile } from "./profile";
12
+ import type { ExecutionLevel } from "../lib/execution-level";
13
+ import type { RuntimeSnapshot } from "./assembler";
14
+ import type { FinalizedPiToolSurface, PiRuntimeAssembly } from "../context";
14
15
  import {
15
16
  assemblePiSystemContext,
16
17
  assemblePiExtensions,
@@ -18,14 +19,35 @@ import {
18
19
  type PiExtensionAssembly,
19
20
  type PiExtensionContextContribution,
20
21
  type PiLoadedExtension,
21
- } from "../assembly";
22
+ } from "../context";
22
23
  import {
23
24
  createPiGatewayToolCandidates,
24
25
  createPiMcpToolCandidates,
25
26
  listExtensionsPiToolCandidate,
26
27
  type PiMcpHost,
27
28
  type PiToolCandidate,
28
- } from "../tool";
29
+ } from "../tools";
30
+
31
+ export interface RuntimeAssemblyPin {
32
+ readonly revision: string;
33
+ readonly descriptor: string;
34
+ }
35
+
36
+ /** A saved pin is reusable only with its original Runtime key and intact descriptor. */
37
+ export async function validateRuntimeAssemblyPin(
38
+ runtimeKey: string,
39
+ pin: RuntimeAssemblyPin,
40
+ ): Promise<boolean> {
41
+ if (!runtimeKey) {
42
+ throw new Error("Cannot safely recover a pinned Submission without its original Runtime key");
43
+ }
44
+ if (!pin.revision && !pin.descriptor) return false;
45
+ if (!pin.revision || !pin.descriptor) throw new Error("Submission assembly pin is incomplete");
46
+ if (pin.revision !== await sha256Hex(pin.descriptor)) {
47
+ throw new Error("Pinned Runtime assembly descriptor is invalid");
48
+ }
49
+ return true;
50
+ }
29
51
 
30
52
  /**
31
53
  * 本文件负责准备 Pi 的扩展与工具,并为每次提交固定可恢复的运行描述。
@@ -85,7 +107,7 @@ interface PreparedPiAssembly {
85
107
  * 提供准备 Pi 所需的已校验 Runtime 快照和 Host 端口。
86
108
  *
87
109
  * @remarks
88
- * `PiRuntimeAdapter.prepare` 在加载 Runtime 配置时传入它。
110
+ * `HarnessRuntimeConfig.prepare` 在加载 Runtime 配置时传入它。
89
111
  *
90
112
  * 扩展绑定仍由 Host 创建,因为 Cloudflare 绑定属于 Worker 的能力边界。
91
113
  *
@@ -128,7 +150,7 @@ export interface PreparedPiRuntime {
128
150
  * 提供把一次提交固定到当前 Runtime 所需的值。
129
151
  *
130
152
  * @remarks
131
- * Runtime 在写入提交前的准入阶段把它交给 `PiRuntimeAdapter.pin`。
153
+ * Runtime 在写入提交前的准入阶段把它交给 `HarnessRuntimeConfig.pin`。
132
154
  *
133
155
  * 扩展没有可读上下文时不需要读取函数,所以该端口保持可选。
134
156
  */
@@ -165,7 +187,7 @@ export interface PinnedPiRuntime {
165
187
  * 描述一条已存提交可以恢复到哪份 Pi 配置。
166
188
  *
167
189
  * @remarks
168
- * `PreparedPiTurnAdapter` 在重建 Turn 前读取这个 JSON 结构。
190
+ * `HarnessRuntimeConfig.harnessInputs` 在打开或恢复 operation 前读取这个 JSON 结构。
169
191
  *
170
192
  * 基础版本作为准入时元数据保留,创建 Turn 时不与当前 Runtime 比较。
171
193
  */
@@ -282,7 +304,7 @@ function describeRuntime(
282
304
  class PreparedRuntime implements PreparedPiRuntime {
283
305
  // 把公开版本数据与受身份保护的内部准备状态放在同一个句柄里。
284
306
  // 只有 preparePiRuntime 会构造它,并在返回前冻结实例。
285
- // owner 使用对象身份区分 PiRuntimeAdapter,避免不同适配器的句柄被混用。
307
+ // owner 使用对象身份区分 HarnessRuntimeConfig,避免不同适配器的句柄被混用。
286
308
  constructor(
287
309
  readonly revisionDescriptor: string,
288
310
  readonly degradations: readonly RuntimeDegradation[],
@@ -305,7 +327,7 @@ class PreparedRuntime implements PreparedPiRuntime {
305
327
  }
306
328
 
307
329
  /**
308
- * 为创建它的 PiRuntimeAdapter 打开准备句柄。
330
+ * 为创建它的 HarnessRuntimeConfig 打开准备句柄。
309
331
  *
310
332
  * @remarks
311
333
  * 固定配置和创建 Turn 都会先调用它,再读取快照、装配结果或工具候选。
@@ -326,7 +348,7 @@ export function readPreparedPiRuntime(
326
348
  * 读取已存的固定描述。
327
349
  *
328
350
  * @remarks
329
- * `PreparedPiTurnAdapter` 在新建或恢复 Pi Turn 前调用它。
351
+ * `HarnessRuntimeConfig.harnessInputs` 在新建或恢复 Pi operation 前调用它。
330
352
  *
331
353
  * Prepared Runtime 提供当前执行能力,descriptor 仅保留 Submission 固定的 Turn 字段。
332
354
  */
@@ -415,7 +437,7 @@ async function preparePiAssembly(
415
437
  * 为一份已校验的 Runtime 快照准备扩展和工具状态。
416
438
  *
417
439
  * @remarks
418
- * `PiRuntimeAdapter.prepare` 在 Runtime 配置阶段、快照生效前调用它。
440
+ * `HarnessRuntimeConfig.prepare` 在 Runtime 配置阶段、快照生效前调用它。
419
441
  *
420
442
  * 冻结句柄会保存最终候选列表,对外只提供版本描述和降级结果。
421
443
  */
@@ -1,15 +1,17 @@
1
+ import type { RuntimeModelInputModality } from "./profile";
1
2
  import type { SkillSource } from "agents/skills";
2
- import type { ScheduleSpec } from "./receipts";
3
- import type { RuntimeActivityProjection } from "./state";
3
+ import type { ScheduleSpec } from "../submission/contracts";
4
+ import type { RuntimeActivityProjection } from "../conversation/state";
4
5
  import type { ExecutionLevel } from "../lib/execution-level";
5
6
  import type { AgentToolResult } from "@earendil-works/pi-agent-core";
6
- import type { PiToolCandidate } from "../pi/tool/compiler";
7
+ import type { PiToolCandidate } from "../tools/compiler";
7
8
  import type {
8
9
  RuntimeEventConfirmation,
9
10
  RuntimeLifecycleFact,
10
11
  RuntimeSubagentUsageEvent,
11
12
  RuntimeSubagentUsageMetrics,
12
- } from "./submission-authority";
13
+ } from "../tools/subagent-authority";
14
+ export type { RuntimeModelInputModality } from "./profile";
13
15
  export type {
14
16
  RuntimeEventConfirmation,
15
17
  RuntimeLifecycleFact,
@@ -19,7 +21,7 @@ export type {
19
21
  RuntimeSubmissionAdmissionHook,
20
22
  RuntimeSubmissionAdmissionInput,
21
23
  RuntimeSubmissionAdmissionResult,
22
- } from "./submission-authority";
24
+ } from "../tools/subagent-authority";
23
25
 
24
26
  // #region Workspace 端口
25
27
 
@@ -624,7 +626,7 @@ export interface RuntimeGatewayPort {
624
626
  }
625
627
 
626
628
  /** Safe catalog from an external Host; credentials and SDK clients stay there. */
627
- export type RuntimeMcpHost = import("../pi/tool/mcp").PiMcpHost & {
629
+ export type RuntimeMcpHost = import("../tools/mcp").PiMcpHost & {
628
630
  readonly servers: readonly import("./profile").RuntimeMcpServer[];
629
631
  };
630
632
 
@@ -827,7 +829,7 @@ export interface RuntimeModelUsageEvent {
827
829
  * 结算事件刻意不带参数,因为它只用来记账;开始事件必须带,因为它唯一的用处
828
830
  * 就是让 Host 立刻知道「正在写的是什么」——只有工具名的话,「write 开始了」
829
831
  * 和「一个网页正在生成」之间的差别就丢了,而后者才是盯着屏幕的人要看的。
830
- * 参数本来就随 Tool 输入里程碑落库,这里没有多暴露任何东西。
832
+ * 参数本来就随 Pi Tool 调用写入 Session,这里没有多暴露任何东西。
831
833
  */
832
834
  export interface RuntimeToolStartEvent {
833
835
  readonly submissionId: string;
@@ -942,12 +944,6 @@ export type RuntimeModelProtocol =
942
944
  | "google-generative-ai"
943
945
  | "openai-codex-responses";
944
946
 
945
- export type RuntimeModelInputModality =
946
- | "text"
947
- | "image"
948
- | "file"
949
- | "audio"
950
- | "video";
951
947
 
952
948
  /**
953
949
  * 描述一个已解析凭据的模型 API 端点。
@@ -1,5 +1,12 @@
1
1
  import type { ExecutionLevel } from "../lib/execution-level";
2
2
 
3
+ export type RuntimeModelInputModality =
4
+ | "text"
5
+ | "image"
6
+ | "file"
7
+ | "audio"
8
+ | "video";
9
+
3
10
  /**
4
11
  * 指定模型在一次 Turn 中可以使用的推理强度。
5
12
  *
@@ -11,13 +11,13 @@
11
11
  * @packageDocumentation
12
12
  */
13
13
  export * from "./profile";
14
- export * from "./runtime-assembly-view";
14
+ export * from "./assembly-view";
15
15
  export * from "./runtime-config";
16
- export * from "../pi/message";
17
- export * from "./receipts";
18
- export * from "./state";
16
+ export * from "../conversation/message";
17
+ export * from "../submission/contracts";
18
+ export * from "../conversation/state";
19
19
  export * from "../lib/execution-level";
20
- export type { WorkspaceRevision, WorkspaceRevisionManifest } from "../workspace-versioning";
20
+ export type { WorkspaceRevision, WorkspaceRevisionManifest } from "../tools/workspace-versioning";
21
21
  export type {
22
22
  RuntimeEventConfirmation,
23
23
  RuntimeLifecycleFact,
@@ -27,4 +27,4 @@ export type {
27
27
  RuntimeSubmissionAdmissionHook,
28
28
  RuntimeSubmissionAdmissionInput,
29
29
  RuntimeSubmissionAdmissionResult,
30
- } from "./submission-authority";
30
+ } from "../tools/subagent-authority";
@@ -1,4 +1,4 @@
1
- import type { RuntimeLoadPhase, RuntimeLoadState } from "./state";
1
+ import type { RuntimeLoadPhase, RuntimeLoadState } from "../conversation/state";
2
2
 
3
3
  /** Hard ceiling for one Host-owned Runtime definition phase. */
4
4
  export const RUNTIME_LOAD_TIMEOUT_MS = 180_000;