personal-bot 0.3.7 → 0.4.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 (172) hide show
  1. package/.claude-plugin/marketplace.json +3 -3
  2. package/.claude-plugin/plugin.json +2 -2
  3. package/README.md +5 -8
  4. package/README.zh-CN.md +5 -8
  5. package/bin/pbot-pi-acp.mjs +1 -1
  6. package/docs/architecture/design.md +2 -2
  7. package/docs/architecture/extensibility.md +4 -4
  8. package/docs/architecture/model-context.md +6 -3
  9. package/docs/guides/cross-harness-workers.md +5 -5
  10. package/docs/guides/history.md +1 -3
  11. package/docs/guides/workspace-configuration.md +5 -5
  12. package/docs/hosts/claude-code.md +3 -3
  13. package/docs/hosts/codex-qoder.md +4 -2
  14. package/hooks/hooks.json +3 -0
  15. package/lib/antigravity-plugin/bin/pbot-pi-acp.mjs +1 -1
  16. package/lib/antigravity-plugin/lib/extension/acp/member.d.ts +1 -1
  17. package/lib/antigravity-plugin/lib/extension/acp/member.js +11 -10
  18. package/lib/antigravity-plugin/lib/extension/acp-worker.js +349 -204
  19. package/lib/antigravity-plugin/lib/extension/cli.js +596 -572
  20. package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-member.js +1 -1
  21. package/lib/antigravity-plugin/lib/extension/hosts/pi/acp-member.js +3 -4
  22. package/lib/antigravity-plugin/lib/extension/hosts/plugin-runtime.js +1 -12
  23. package/lib/antigravity-plugin/lib/extension/mcp.js +463 -379
  24. package/lib/antigravity-plugin/lib/integrations/agent-resources.d.ts +2 -2
  25. package/lib/antigravity-plugin/lib/integrations/agent-resources.js +3 -3
  26. package/lib/antigravity-plugin/lib/integrations/hosts/antigravity/adapter.js +0 -1
  27. package/lib/antigravity-plugin/lib/integrations/hosts/claude/adapter.js +1 -2
  28. package/lib/antigravity-plugin/lib/integrations/hosts/dsh/resources.d.ts +1 -1
  29. package/lib/antigravity-plugin/lib/integrations/hosts/prompt-hooks.js +4 -7
  30. package/lib/antigravity-plugin/lib/model/acp.d.ts +29 -35
  31. package/lib/antigravity-plugin/lib/model/agent.d.ts +33 -17
  32. package/lib/antigravity-plugin/lib/model/agent.js +0 -1
  33. package/lib/antigravity-plugin/lib/services/history/summary-dimensions/monthly.js +1 -1
  34. package/lib/antigravity-plugin/lib/services/history/summary-dimensions/session.js +6 -3
  35. package/lib/antigravity-plugin/lib/services/history/summary-runner.js +3 -3
  36. package/lib/antigravity-plugin/lib/storage/acp-files.d.ts +5 -8
  37. package/lib/antigravity-plugin/lib/storage/acp-files.js +34 -40
  38. package/lib/antigravity-plugin/lib/storage/agent-files.d.ts +19 -10
  39. package/lib/antigravity-plugin/lib/storage/agent-files.js +147 -43
  40. package/lib/antigravity-plugin/lib/storage/history-files.js +3 -2
  41. package/lib/antigravity-plugin/package.json +1 -1
  42. package/lib/antigravity-plugin/plugin.json +2 -2
  43. package/lib/antigravity-plugin/resources/agents/coordinator.md +2 -4
  44. package/lib/antigravity-plugin/resources/agents/general-purpose.md +2 -4
  45. package/lib/antigravity-plugin/resources/prompts/history-monthly-summary.md +6 -11
  46. package/lib/antigravity-plugin/resources/prompts/history-summary.md +6 -11
  47. package/lib/antigravity-plugin/resources/prompts/plugin-coordinator.md +1 -1
  48. package/lib/antigravity-plugin/resources/prompts/workspace.md +16 -23
  49. package/lib/antigravity-plugin/resources/skills/project-maintenance/SKILL.md +14 -21
  50. package/lib/antigravity-plugin/resources/skills/provider-connections/SKILL.md +23 -36
  51. package/lib/antigravity-plugin/resources/templates/workspace-agents.md +5 -13
  52. package/lib/antigravity-plugin/skills/pbot/SKILL.md +6 -23
  53. package/lib/antigravity-plugin/skills/project-maintenance/SKILL.md +14 -21
  54. package/lib/antigravity-plugin/skills/provider-connections/SKILL.md +23 -36
  55. package/lib/codex-marketplace/plugins/personal-bot/.codex-plugin/plugin.json +2 -2
  56. package/lib/codex-marketplace/plugins/personal-bot/.mcp.json +2 -2
  57. package/lib/codex-marketplace/plugins/personal-bot/hooks/hooks.json +3 -3
  58. package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/SKILL.md +11 -3
  59. package/lib/extension/acp/member.d.ts +1 -1
  60. package/lib/extension/acp/member.js +11 -10
  61. package/lib/extension/acp/worker.js +34 -33
  62. package/lib/extension/acp-worker.js +349 -204
  63. package/lib/extension/cli.js +596 -572
  64. package/lib/extension/hosts/dsh/acp-member.js +1 -1
  65. package/lib/extension/hosts/dsh/command.js +2 -2
  66. package/lib/extension/hosts/dsh/workspace.js +5 -5
  67. package/lib/extension/hosts/pi/acp-member.js +3 -4
  68. package/lib/extension/hosts/plugin-runtime.js +1 -12
  69. package/lib/extension/mcp/tool-contract.d.ts +8 -50
  70. package/lib/extension/mcp/tool-contract.js +61 -107
  71. package/lib/extension/mcp/workers.d.ts +7 -27
  72. package/lib/extension/mcp/workers.js +40 -24
  73. package/lib/extension/mcp.js +463 -379
  74. package/lib/integrations/acp/agents.d.ts +2 -3
  75. package/lib/integrations/acp/agents.js +22 -22
  76. package/lib/integrations/acp/harnesses/claude.js +1 -1
  77. package/lib/integrations/agent-resources.d.ts +2 -2
  78. package/lib/integrations/agent-resources.js +3 -3
  79. package/lib/integrations/hosts/antigravity/adapter.js +0 -1
  80. package/lib/integrations/hosts/claude/adapter.js +1 -2
  81. package/lib/integrations/hosts/dsh/resources.d.ts +1 -1
  82. package/lib/integrations/hosts/prompt-hooks.js +4 -7
  83. package/lib/model/acp.d.ts +29 -35
  84. package/lib/model/agent.d.ts +33 -17
  85. package/lib/model/agent.js +0 -1
  86. package/lib/qoder-plugin/.mcp.json +2 -2
  87. package/lib/qoder-plugin/.qoder-plugin/plugin.json +2 -2
  88. package/lib/qoder-plugin/hooks/hooks.json +3 -3
  89. package/lib/qoder-plugin/skills/pbot/SKILL.md +11 -3
  90. package/lib/services/agents/acp-run.d.ts +6 -4
  91. package/lib/services/agents/acp-run.js +62 -43
  92. package/lib/services/agents/agent-run.d.ts +10 -6
  93. package/lib/services/agents/agent-run.js +142 -65
  94. package/lib/services/agents/agent-service.d.ts +3 -21
  95. package/lib/services/agents/agent-service.js +182 -128
  96. package/lib/services/history/summary-dimensions/monthly.js +1 -1
  97. package/lib/services/history/summary-dimensions/session.js +6 -3
  98. package/lib/services/history/summary-runner.js +3 -3
  99. package/lib/storage/acp-files.d.ts +5 -8
  100. package/lib/storage/acp-files.js +34 -40
  101. package/lib/storage/agent-files.d.ts +19 -10
  102. package/lib/storage/agent-files.js +147 -43
  103. package/lib/storage/history-files.js +3 -2
  104. package/lib/tools/history/contract.d.ts +1 -1
  105. package/lib/tools/history/contract.js +14 -16
  106. package/package.json +2 -2
  107. package/resources/agents/coordinator.md +2 -4
  108. package/resources/agents/general-purpose.md +2 -4
  109. package/resources/antigravity/skills/pbot/SKILL.md +6 -23
  110. package/resources/claude/skills/pbot/SKILL.md +6 -23
  111. package/resources/plugin/skills/pbot/SKILL.md +11 -3
  112. package/resources/prompts/history-coordinator.md +1 -1
  113. package/resources/prompts/history-monthly-summary.md +6 -11
  114. package/resources/prompts/history-summary.md +6 -11
  115. package/resources/prompts/plugin-coordinator.md +1 -1
  116. package/resources/prompts/workspace.md +16 -23
  117. package/resources/skills/project-maintenance/SKILL.md +14 -21
  118. package/resources/skills/provider-connections/SKILL.md +23 -36
  119. package/resources/templates/workspace-agents.md +5 -13
  120. package/src/extension/acp/member.ts +14 -11
  121. package/src/extension/acp/worker.ts +31 -28
  122. package/src/extension/cli/commands/mcp.ts +1 -1
  123. package/src/extension/cli/commands/upgrade.ts +8 -36
  124. package/src/extension/cli/commands/workspace.ts +3 -8
  125. package/src/extension/cli/index.ts +1 -2
  126. package/src/extension/hosts/dsh/acp-member.ts +1 -1
  127. package/src/extension/hosts/dsh/command.ts +2 -2
  128. package/src/extension/hosts/dsh/workspace.ts +5 -4
  129. package/src/extension/hosts/pi/acp-member.ts +3 -4
  130. package/src/extension/hosts/pi/command.ts +2 -2
  131. package/src/extension/hosts/pi/events.ts +1 -1
  132. package/src/extension/hosts/pi/lifecycle.ts +0 -2
  133. package/src/extension/hosts/plugin-runtime.ts +1 -11
  134. package/src/extension/mcp/tool-contract.ts +61 -108
  135. package/src/extension/mcp/workers.ts +39 -24
  136. package/src/integrations/acp/agents.ts +28 -27
  137. package/src/integrations/acp/harnesses/claude.ts +1 -1
  138. package/src/integrations/agent-resources.ts +5 -5
  139. package/src/integrations/hosts/antigravity/adapter.ts +0 -1
  140. package/src/integrations/hosts/claude/adapter.ts +1 -2
  141. package/src/integrations/hosts/dsh/resources.ts +1 -1
  142. package/src/integrations/hosts/dsh/session.ts +2 -2
  143. package/src/integrations/hosts/prompt-hooks.ts +3 -6
  144. package/src/integrations/installation/cli-update.ts +2 -2
  145. package/src/integrations/installation/installers/claude.ts +4 -4
  146. package/src/integrations/installation/installers/qoder.ts +5 -5
  147. package/src/integrations/installation/upgraders/antigravity.ts +57 -15
  148. package/src/integrations/installation/upgraders/claude.ts +1 -4
  149. package/src/integrations/installation/upgraders/codex.ts +85 -15
  150. package/src/integrations/installation/upgraders/dsh.ts +46 -24
  151. package/src/integrations/installation/upgraders/pi.ts +18 -11
  152. package/src/integrations/installation/upgraders/qoder.ts +95 -20
  153. package/src/model/acp.ts +27 -29
  154. package/src/model/agent.ts +80 -16
  155. package/src/model/installation.ts +0 -2
  156. package/src/pbot.ts +3 -3
  157. package/src/services/agents/acp-run.ts +81 -49
  158. package/src/services/agents/agent-run.ts +154 -56
  159. package/src/services/agents/agent-service.ts +206 -135
  160. package/src/services/history/summary-dimensions/monthly.ts +1 -1
  161. package/src/services/history/summary-dimensions/session.ts +6 -3
  162. package/src/services/history/summary-runner.ts +3 -3
  163. package/src/services/installation/upgrade-service.ts +1 -5
  164. package/src/storage/acp-files.ts +50 -45
  165. package/src/storage/agent-files.ts +159 -46
  166. package/src/storage/history-files.ts +3 -2
  167. package/src/tools/history/contract.ts +14 -16
  168. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.d.ts +0 -15
  169. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.js +0 -126
  170. package/lib/services/workspace/plugin-releases.d.ts +0 -15
  171. package/lib/services/workspace/plugin-releases.js +0 -126
  172. package/src/services/workspace/plugin-releases.ts +0 -121
@@ -4,9 +4,21 @@ import { resolve } from "node:path";
4
4
  import { object } from "../../common/json.js";
5
5
  import { packageVersion, releaseLine } from "../../common/version.js";
6
6
  import { requireThat } from "../../common/validation.js";
7
- import { AgentFiles, descriptor, launch, status, cycle } from "../../storage/agent-files.js";
8
- import type { AgentOwner, AgentOptions, AgentMessage, AgentPendingRequest, AgentResponseInput, AgentStartOptions, AgentDescriptor, AgentCycle, AgentHost, AgentView } from "../../model/agent.js";
9
- import { acpElicitationReply, acpModelState, acpPermissionReply, acpReference } from "../../storage/acp-files.js";
7
+ import { AgentFiles, outputRecord, status, toDescriptor, workerRecord } from "../../storage/agent-files.js";
8
+ import type {
9
+ AgentCycle,
10
+ AgentDescriptor,
11
+ AgentHost,
12
+ AgentMessage,
13
+ AgentOptions,
14
+ AgentOwner,
15
+ AgentPendingRequest,
16
+ AgentResponseInput,
17
+ AgentStartOptions,
18
+ AgentView,
19
+ AgentWorkerRecord,
20
+ } from "../../model/agent.js";
21
+ import { acpInteractionReply } from "../../storage/acp-files.js";
10
22
 
11
23
  export type { AgentHost } from "../../model/agent.js";
12
24
 
@@ -26,59 +38,60 @@ export class AgentService {
26
38
 
27
39
  private alive(id: string) { return this.#starting.has(id) || this.#releasing.has(id) || this.host.alive(id); }
28
40
  private check() { requireThat(!this.#closed, "worker 服务已关闭。"); }
29
- private owned(d: AgentDescriptor, owner: AgentOwner) {
30
- return d.workspace === this.root && d.parentSessionId === owner.sessionId && (d.parentLeaf === null || owner.branch.includes(d.parentLeaf));
41
+ private owned(w: AgentWorkerRecord, owner: AgentOwner) {
42
+ return w.workspace === this.root && w.parentSessionId === owner.sessionId;
31
43
  }
32
- private get(id: string, owner: AgentOwner) {
33
- this.check(); const d = this.files.read(id, "descriptor.json", descriptor);
34
- requireThat(this.owned(d, owner), "worker 不属于当前会话。");
35
- requireThat(releaseLine(d.release) === releaseLine(packageVersion), "worker 版本不兼容;请停止后使用匹配版本。"); return d;
44
+ private get(id: string, owner: AgentOwner): AgentWorkerRecord {
45
+ this.check();
46
+ const w = this.files.worker(id);
47
+ requireThat(this.owned(w, owner), "worker 不属于当前会话。");
48
+ requireThat(releaseLine(w.release) === releaseLine(packageVersion), "worker 版本不兼容;请停止后使用匹配版本。");
49
+ return w;
36
50
  }
37
51
  private records(owner: AgentOwner) {
38
52
  this.check();
39
- return this.files.list().filter(d => d.workspace === this.root && d.parentSessionId === owner.sessionId &&
40
- releaseLine(d.release) === releaseLine(packageVersion));
53
+ return this.files.list().filter(w => w.workspace === this.root && w.parentSessionId === owner.sessionId &&
54
+ releaseLine(w.release) === releaseLine(packageVersion));
55
+ }
56
+
57
+ has(id: string, owner: AgentOwner): boolean {
58
+ if (this.#closed) return false;
59
+ const w = this.files.optional(id, "worker.json", workerRecord);
60
+ return Boolean(w && this.owned(w, owner) && releaseLine(w.release) === releaseLine(packageVersion));
41
61
  }
42
62
 
43
- list(owner: AgentOwner): AgentView[] { return this.records(owner).map(d => this.view(d, owner)); }
63
+ list(owner: AgentOwner): AgentView[] { return this.records(owner).map(w => this.view(w, owner)); }
44
64
 
45
- private view(d: AgentDescriptor, owner: AgentOwner): AgentView {
46
- const run = this.files.read(d.agentId, "launch.json", launch);
47
- const saved = this.files.optional(d.agentId, "status.json", status);
48
- const current = saved?.generation === run.generation ? saved : undefined;
49
- const alive = this.alive(d.agentId), pending = this.#starting.get(d.agentId);
65
+ private view(w: AgentWorkerRecord, owner: AgentOwner): AgentView {
66
+ const saved = this.files.optional(w.agentId, "state.json", status);
67
+ const current = saved?.generation === w.generation ? saved : undefined;
68
+ const alive = this.alive(w.agentId), pending = this.#starting.get(w.agentId);
50
69
  const starting = pending && !pending.controller.signal.aborted;
51
- const reference = this.files.read(d.agentId, "acp-session.json", acpReference);
52
- const model = this.files.optional(d.agentId, "acp-model.json", acpModelState);
53
- const selection = model?.generation === run.generation ? {
54
- ...(model.provider ? { provider: model.provider } : {}),
55
- ...(model.model ? { model: model.model } : {}),
56
- ...(model.resolvedModel ? { resolvedModel: model.resolvedModel } : {}),
57
- ...(model.thinking ? { thinking: model.thinking } : {}),
58
- } : undefined;
59
- const pendingRequests: AgentPendingRequest[] = alive ? [
60
- ...this.files.pendingPermissions(d.agentId, run.generation)
61
- .map(item => ({ requestId: item.requestId, kind: "permission" as const, description: item.description, options: item.options })),
62
- ...this.files.pendingElicitations(d.agentId, run.generation)
63
- .map(item => (item.mode === "form"
64
- ? { requestId: item.requestId, kind: "form" as const, message: item.message, schema: item.schema ?? { type: "object", properties: {} } }
65
- : { requestId: item.requestId, kind: "url" as const, message: item.message, url: item.url ?? "" })),
66
- ] : [];
70
+ const selection = current?.selection && Object.keys(current.selection).length ? current.selection : undefined;
71
+ const pendingRequests: AgentPendingRequest[] = alive
72
+ ? this.files.pendingInteractions(w.agentId, w.generation).map(item => (
73
+ item.kind === "permission"
74
+ ? { requestId: item.requestId, kind: "permission" as const, description: item.description, options: item.options }
75
+ : item.kind === "form"
76
+ ? { requestId: item.requestId, kind: "form" as const, message: item.message, schema: item.schema }
77
+ : { requestId: item.requestId, kind: "url" as const, message: item.message, url: item.url }
78
+ ))
79
+ : [];
80
+ const capabilities = saved?.capabilities;
67
81
  return {
68
- ...d,
69
- name: run.name,
70
- generation: run.generation,
71
- owned: this.owned(d, owner),
82
+ ...toDescriptor(w),
83
+ generation: w.generation,
84
+ owned: this.owned(w, owner),
72
85
  alive,
73
- backend: reference.backend,
86
+ backend: w.acp.backend,
74
87
  transport: "acp",
75
88
  capabilities: {
76
89
  steer: false,
77
- resume: Boolean(reference.capabilities?.resume || reference.capabilities?.load),
90
+ resume: Boolean(capabilities?.resume || capabilities?.load),
78
91
  transcript: false,
79
92
  },
80
93
  ...(selection ? { selection } : {}),
81
- state: alive ? starting ? "starting" : run.stop || pending?.controller.signal.aborted ? "stopping" : current?.state ?? "starting" : current?.state === "error" ? "error" : "stopped",
94
+ state: alive ? starting ? "starting" : w.stop || pending?.controller.signal.aborted ? "stopping" : current?.state ?? "starting" : current?.state === "error" ? "error" : "stopped",
82
95
  activity: starting ? "启动" : alive ? current?.activity ?? "启动" : current?.error ?? "已停止",
83
96
  ...(current?.cycle ? { cycle: current.cycle } : {}),
84
97
  ...(current?.error ? { error: current.error } : {}),
@@ -88,17 +101,18 @@ export class AgentService {
88
101
 
89
102
  /** Answer a pending permission or elicitation request forwarded by the worker. */
90
103
  respond(id: string, input: AgentResponseInput, owner: AgentOwner) {
91
- const d = this.get(id, owner), run = this.files.read(id, "launch.json", launch);
92
- const pending = this.view(d, owner).pendingRequests ?? [];
104
+ const w = this.get(id, owner);
105
+ const pending = this.view(w, owner).pendingRequests ?? [];
93
106
  if (!input.requestId && pending.length > 1) {
94
107
  throw new Error("存在多条待回复请求,请指定 requestId:" + pending.map(item => item.requestId).join(", "));
95
108
  }
96
109
  const target = input.requestId ? pending.find(item => item.requestId === input.requestId) : pending[0];
97
110
  requireThat(target, input.requestId
98
- ? "没有待回复的请求:" + input.requestId + ";用 worker_discover 查看 pendingRequests。"
111
+ ? `没有待回复的请求 ${input.requestId}。用 worker_discover 查询 target="workers"、workerId="${id}" 的当前请求。`
99
112
  : "当前没有待回复的请求。");
113
+ this.files.ensureSubdirectory(id, "interactions");
100
114
  if (target.kind === "permission") {
101
- requireThat(input.content === undefined, "权限请求不使用 content;请用 action(accept / decline / cancel)或指定 optionId。");
115
+ requireThat(input.content === undefined, "请移除 content。权限请求用 action 回复,选择具体权限范围时加上 optionId。");
102
116
  let resolved: string | null = null;
103
117
  if (input.action === "cancel") {
104
118
  requireThat(input.optionId === undefined, "action 为 cancel 时不能同时指定 optionId。");
@@ -124,10 +138,9 @@ export class AgentService {
124
138
  resolved = pick("reject_once", "reject_always") ?? null;
125
139
  }
126
140
  }
127
- this.files.ensureSubdirectory(id, "permissions");
128
- this.files.write(id, "permissions/" + target.requestId + "-reply.json",
129
- { generation: run.generation, requestId: target.requestId, optionId: resolved }, acpPermissionReply, true);
130
- return this.view(d, owner);
141
+ this.files.write(id, "interactions/" + target.requestId + "-reply.json",
142
+ { generation: w.generation, requestId: target.requestId, kind: "permission", optionId: resolved }, acpInteractionReply, true);
143
+ return this.view(w, owner);
131
144
  }
132
145
  if (target.kind === "form") {
133
146
  requireThat(input.optionId === undefined, "问答/表单请求不使用 optionId;请将答案填入 content。");
@@ -136,29 +149,29 @@ export class AgentService {
136
149
  } else {
137
150
  requireThat(input.content === undefined || object(input.content), "问答/表单答复的 content 必须是对象。");
138
151
  }
139
- this.files.ensureSubdirectory(id, "elicitations");
140
- this.files.write(id, "elicitations/" + target.requestId + "-reply.json", {
141
- generation: run.generation,
152
+ this.files.write(id, "interactions/" + target.requestId + "-reply.json", {
153
+ generation: w.generation,
142
154
  requestId: target.requestId,
155
+ kind: "form",
143
156
  action: input.action,
144
157
  ...(input.action === "accept" ? { content: input.content ?? {} } : {}),
145
- }, acpElicitationReply, true);
146
- return this.view(d, owner);
158
+ }, acpInteractionReply, true);
159
+ return this.view(w, owner);
147
160
  }
148
161
  requireThat(input.optionId === undefined, "URL 确认请求不使用 optionId。");
149
162
  requireThat(input.content === undefined, "URL 确认请求不使用 content。");
150
- this.files.ensureSubdirectory(id, "elicitations");
151
- this.files.write(id, "elicitations/" + target.requestId + "-reply.json", {
152
- generation: run.generation,
163
+ this.files.write(id, "interactions/" + target.requestId + "-reply.json", {
164
+ generation: w.generation,
153
165
  requestId: target.requestId,
166
+ kind: "url",
154
167
  action: input.action,
155
- }, acpElicitationReply, true);
156
- return this.view(d, owner);
168
+ }, acpInteractionReply, true);
169
+ return this.view(w, owner);
157
170
  }
158
171
 
159
172
  async start(input: AgentStartOptions, owner: AgentOwner) {
160
173
  this.check();
161
- const { backend, ...supplied } = input;
174
+ const { backend, message: initialMessage, files: initialFiles, ...supplied } = input;
162
175
  this.host.available(backend);
163
176
  const clean = Object.fromEntries(Object.entries(supplied).filter(([, v]) => v !== undefined)) as AgentOptions;
164
177
  const options = this.host.filterOptions ? this.host.filterOptions(backend, clean) : clean;
@@ -166,21 +179,26 @@ export class AgentService {
166
179
  const prepared = this.files.control(() => {
167
180
  requireThat(this.list(owner).filter(v => v.alive).length < 50, "最多 50 个存活或启动中 worker;请先停止一个空闲会话。");
168
181
  const agentId = randomUUID();
169
- const cwd = realpathSync(resolve(this.root, options.cwd ?? "."));
170
- const files = (options.files ?? []).map(p => realpathSync(resolve(this.root, p)));
182
+ const files = (initialFiles ?? []).map(p => realpathSync(resolve(this.root, p)));
171
183
  const d: AgentDescriptor = {
172
- agentId, release: packageVersion, workspace: this.root, cwd,
173
- parentSessionId: owner.sessionId, parentLeaf: owner.leaf, createdAt: Date.now(), options: { ...options, files, cwd },
184
+ agentId,
185
+ release: packageVersion,
186
+ workspace: this.root,
187
+ parentSessionId: owner.sessionId,
188
+ options,
174
189
  };
175
- const run = { generation: randomUUID(), name: options.name ?? agentId.slice(0, 8), sequence: 0, stop: false, startedAt: Date.now() };
190
+ const run = { generation: randomUUID(), sequence: 0, stop: false, startedAt: Date.now() };
176
191
  this.files.create(d, run, reference);
177
- const initial = options.message ?? "";
192
+ const initial = initialMessage ?? "";
178
193
  const request = initial || files.length ? this.files.enqueue(agentId, initial, "steer", files) : undefined;
179
194
  this.#starting.set(agentId, { generation: run.generation, controller: new AbortController(), stage: "launch" });
180
195
  return { d, run, request };
181
196
  });
182
197
  await this.launch(prepared.d, prepared.run.generation);
183
- return { ...this.view(prepared.d, owner), ...(prepared.request ? { requestId: prepared.request.requestId, messageStatus: "queued" as const } : {}) };
198
+ return {
199
+ ...this.view(this.files.worker(prepared.d.agentId), owner),
200
+ ...(prepared.request ? { requestId: prepared.request.requestId, messageStatus: "queued" as const } : {}),
201
+ };
184
202
  }
185
203
 
186
204
  private async launch(d: AgentDescriptor, generation: string, resume = false) {
@@ -189,16 +207,23 @@ export class AgentService {
189
207
  const operation = (async () => {
190
208
  try {
191
209
  await this.host.launch(d, this.files.directory(d.agentId), generation, this.files.reference(d.agentId), resume, pending.controller.signal);
192
- requireThat(!this.#closed || this.#preserve, "worker 服务已关闭。"); pending.controller.signal.throwIfAborted();
193
- const run = this.files.read(d.agentId, "launch.json", launch);
194
- requireThat(run.generation === generation && !run.stop, "worker 启动已取消或代次变化。");
210
+ requireThat(!this.#closed || this.#preserve, "worker 服务已关闭。");
211
+ pending.controller.signal.throwIfAborted();
212
+ const w = this.files.worker(d.agentId);
213
+ requireThat(w.generation === generation && !w.stop, "worker 启动已取消或代次变化。");
195
214
  } catch (error) {
196
- const run = this.files.read(d.agentId, "launch.json", launch);
197
- if (run.generation === generation) {
215
+ const w = this.files.worker(d.agentId);
216
+ if (w.generation === generation) {
198
217
  if (this.host.alive(d.agentId)) await this.release(d.agentId);
199
- this.files.write(d.agentId, "status.json", {
200
- generation, state: run.stop ? "stopped" : "error",
201
- activity: run.stop ? "已停止" : "启动失败", updatedAt: Date.now(), ...(run.stop ? {} : { error: String(error) }),
218
+ const existing = this.files.optional(d.agentId, "state.json", status);
219
+ this.files.write(d.agentId, "state.json", {
220
+ generation,
221
+ state: w.stop ? "stopped" : "error",
222
+ activity: w.stop ? "已停止" : "启动失败",
223
+ updatedAt: Date.now(),
224
+ ...(existing?.sessionId !== undefined ? { sessionId: existing.sessionId } : {}),
225
+ ...(existing?.capabilities ? { capabilities: existing.capabilities } : {}),
226
+ ...(w.stop ? {} : { error: String(error) }),
202
227
  }, status);
203
228
  }
204
229
  throw error;
@@ -209,76 +234,90 @@ export class AgentService {
209
234
  }
210
235
 
211
236
  send(id: string, text: string, delivery: AgentMessage["delivery"], owner: AgentOwner, attachments: readonly string[] = []) {
212
- const d = this.get(id, owner);
237
+ const w = this.get(id, owner);
213
238
  return this.files.control(() => {
214
- requireThat(this.alive(id) && this.#starting.get(id)?.stage !== "validate" && !this.files.read(id, "launch.json", launch).stop, "worker 已停止或仍在恢复校验,请等待显式恢复完成。");
215
- const view = this.view(d, owner);
239
+ const latest = this.files.worker(id);
240
+ requireThat(this.alive(id) && this.#starting.get(id)?.stage !== "validate" && !latest.stop, "worker 已停止或仍在恢复校验,请等待显式恢复完成。");
241
+ const view = this.view(latest, owner);
216
242
  requireThat(!(delivery === "steer" && view.state !== "idle"), "该 ACP worker 运行中不支持 steer;请用 delivery: followUp 排队。");
217
243
  const files = attachments.map(path => realpathSync(resolve(this.root, path)));
218
244
  const request = this.files.enqueue(id, text, delivery, files);
219
- return { ...this.view(d, owner), requestId: request.requestId, messageStatus: "queued" as const };
245
+ return { ...this.view(this.files.worker(w.agentId), owner), requestId: request.requestId, messageStatus: "queued" as const };
220
246
  });
221
247
  }
222
248
 
223
- rename(id: string, name: string, owner: AgentOwner) {
224
- requireThat(name.trim().length > 0 && name.length <= 64 && !/[\x00-\x1f]/.test(name), "名称必须是 1 到 64 字符的单行文本。");
225
- const d = this.get(id, owner);
226
- this.files.control(() => { const run = this.files.read(id, "launch.json", launch); run.name = name; this.files.write(id, "launch.json", run, launch); });
227
- return this.view(d, owner);
228
- }
229
-
230
249
  stop(id: string, owner: AgentOwner) {
231
- const d = this.get(id, owner); this.requestStop(id); return this.view(d, owner);
250
+ this.get(id, owner);
251
+ this.requestStop(id);
252
+ return this.view(this.files.worker(id), owner);
232
253
  }
233
254
 
234
255
  private requestStop(id: string) {
235
- this.files.control(() => { const run = this.files.read(id, "launch.json", launch); run.stop = true; this.files.write(id, "launch.json", run, launch); });
256
+ this.files.control(() => {
257
+ const w = this.files.worker(id);
258
+ w.stop = true;
259
+ this.files.write(id, "worker.json", w, workerRecord);
260
+ });
236
261
  this.#starting.get(id)?.controller.abort();
237
262
  }
238
263
 
239
- async enter(id: string, owner: AgentOwner) {
240
- const d = this.get(id, owner);
241
- if (this.alive(id)) {
242
- requireThat(!this.files.read(id, "launch.json", launch).stop, "worker 正在停止,请稍后操作。");
243
- return this.view(d, owner);
244
- }
245
- return this.restart(id, owner);
246
- }
247
-
248
- resume(id: string, text: string, owner: AgentOwner) {
264
+ resume(id: string, text: string | undefined, owner: AgentOwner) {
249
265
  return this.restart(id, owner, text);
250
266
  }
251
267
 
252
268
  private async restart(id: string, owner: AgentOwner, text?: string) {
253
- const d = this.get(id, owner), reference = this.files.reference(id);
269
+ const w = this.get(id, owner), d = toDescriptor(w), reference = this.files.reference(id);
254
270
  this.host.available(reference.backend);
255
271
  const reserved = this.files.control(() => {
256
272
  requireThat(!this.alive(id), "worker 仍在运行。");
257
273
  requireThat(this.list(owner).filter(v => v.alive).length < 50, "最多 50 个存活或启动中 worker。");
258
- const previous = this.files.read(id, "launch.json", launch), interrupted = this.cycles(id, owner);
274
+ const previous = this.files.worker(id), interrupted = this.cycles(id, owner);
259
275
  const pending = { generation: previous.generation, controller: new AbortController(), stage: "validate" as "validate" | "launch" };
260
276
  this.#starting.set(id, pending);
261
277
  return { previous, interrupted, pending };
262
278
  });
263
279
  const operation = (async () => {
264
280
  await this.host.validateSession(d, this.files.reference(id));
265
- requireThat(!this.#closed || this.#preserve, "worker 服务已关闭。"); reserved.pending.controller.signal.throwIfAborted();
281
+ requireThat(!this.#closed || this.#preserve, "worker 服务已关闭。");
282
+ reserved.pending.controller.signal.throwIfAborted();
266
283
  const prepared = this.files.control(() => {
267
- requireThat(!this.host.alive(id) && this.files.read(id, "launch.json", launch).generation === reserved.previous.generation, "worker 已由其他操作恢复。");
268
- for (const ended of reserved.interrupted) {
269
- if (ended.generation === reserved.previous.generation && !this.files.completions(id).some(c => c.cycleId === ended.cycleId)) {
270
- this.files.write(id, "completions/" + ended.cycleId + ".json", ended, cycle, true);
271
- }
284
+ requireThat(!this.host.alive(id) && this.files.worker(id).generation === reserved.previous.generation, "worker 已由其他操作恢复。");
285
+ const existingOutput = this.files.output(id);
286
+ const lastInterrupted = reserved.interrupted.findLast(
287
+ ended => ended.generation === reserved.previous.generation && existingOutput?.cycleId !== ended.cycleId,
288
+ );
289
+ if (lastInterrupted && lastInterrupted.state !== "running") {
290
+ this.files.write(id, "output.json", {
291
+ cycleId: lastInterrupted.cycleId,
292
+ generation: lastInterrupted.generation,
293
+ startedAt: lastInterrupted.startedAt,
294
+ finishedAt: lastInterrupted.finishedAt ?? Date.now(),
295
+ state: lastInterrupted.state,
296
+ requestIds: lastInterrupted.requestIds,
297
+ text: "",
298
+ ...(lastInterrupted.error ? { error: lastInterrupted.error } : {}),
299
+ }, outputRecord);
272
300
  }
273
- const run = { ...reserved.previous, generation: randomUUID(), sequence: 0, stop: false, startedAt: Date.now() };
274
- this.files.write(id, "launch.json", run, launch);
301
+ this.files.clearTransient(id);
302
+ const next: AgentWorkerRecord = {
303
+ ...reserved.previous,
304
+ generation: randomUUID(),
305
+ sequence: 0,
306
+ stop: false,
307
+ startedAt: Date.now(),
308
+ };
309
+ delete next.pid;
310
+ this.files.write(id, "worker.json", next, workerRecord);
275
311
  const request = text === undefined ? undefined : this.files.enqueue(id, text, "steer");
276
- reserved.pending.generation = run.generation;
312
+ reserved.pending.generation = next.generation;
277
313
  reserved.pending.stage = "launch";
278
- return { run, request };
314
+ return { run: next, request };
279
315
  });
280
316
  await this.launch(d, prepared.run.generation, true);
281
- return { ...this.view(d, owner), ...(prepared.request ? { requestId: prepared.request.requestId, messageStatus: "queued" as const } : {}) };
317
+ return {
318
+ ...this.view(this.files.worker(id), owner),
319
+ ...(prepared.request ? { requestId: prepared.request.requestId, messageStatus: "queued" as const } : {}),
320
+ };
282
321
  })();
283
322
  this.#launches.add(operation);
284
323
  try { return await operation; } finally {
@@ -293,23 +332,47 @@ export class AgentService {
293
332
 
294
333
  private cyclesFor(view: AgentView): AgentCycle[] {
295
334
  const id = view.agentId;
296
- const values = this.files.completions(id);
335
+ const values: AgentCycle[] = [];
336
+ const output = this.files.output(id);
337
+ if (output) {
338
+ values.push({
339
+ cycleId: output.cycleId,
340
+ generation: output.generation,
341
+ startedAt: output.startedAt,
342
+ finishedAt: output.finishedAt,
343
+ state: output.state,
344
+ requestIds: output.requestIds,
345
+ ...(output.error ? { error: output.error } : {}),
346
+ });
347
+ }
297
348
  if (view.cycle && !values.some(c => c.cycleId === view.cycle!.cycleId)) {
298
- values.push(view.state === "stopped" || view.state === "error" ? { ...view.cycle, state: "interrupted", finishedAt: view.cycle.finishedAt ?? Date.now() } : view.cycle);
349
+ values.push(view.state === "stopped" || view.state === "error"
350
+ ? { ...view.cycle, state: "interrupted", finishedAt: view.cycle.finishedAt ?? Date.now() }
351
+ : view.cycle);
299
352
  }
300
353
  if (["stopped", "error"].includes(view.state)) {
301
- const run = this.files.read(id, "launch.json", launch), saved = this.files.optional(id, "status.json", status);
302
- for (const requestId of saved?.generation === run.generation ? saved.accepted ?? [] : []) {
303
- if (values.some(c => c.generation === run.generation && c.requestIds.includes(requestId))) continue;
354
+ const w = this.files.worker(id), saved = this.files.optional(id, "state.json", status);
355
+ for (const requestId of saved?.generation === w.generation ? saved.accepted ?? [] : []) {
356
+ if (values.some(c => c.generation === w.generation && c.requestIds.includes(requestId))) continue;
304
357
  values.push({
305
- cycleId: requestId, generation: run.generation, startedAt: run.startedAt, finishedAt: Date.now(),
306
- state: "interrupted", leaf: null, requestIds: [requestId], source: "parent", error: "领取后执行结果不明;未重跑。",
358
+ cycleId: requestId,
359
+ generation: w.generation,
360
+ startedAt: w.startedAt,
361
+ finishedAt: Date.now(),
362
+ state: "interrupted",
363
+ requestIds: [requestId],
364
+ error: "领取后执行结果不明;未重跑。",
307
365
  });
308
366
  }
309
- if (run.sequence > 0 && !values.some(c => c.generation === view.generation)) {
367
+ if (w.sequence > 0 && !values.some(c => c.generation === view.generation)) {
310
368
  values.push({
311
- cycleId: run.generation, generation: run.generation, startedAt: run.startedAt, finishedAt: run.startedAt,
312
- state: "interrupted", leaf: null, requestIds: [], source: "parent", error: view.error ?? "子进程在交付前退出。",
369
+ cycleId: w.generation,
370
+ generation: w.generation,
371
+ startedAt: w.startedAt,
372
+ finishedAt: w.startedAt,
373
+ state: "interrupted",
374
+ requestIds: [],
375
+ error: view.error ?? "子进程在交付前退出。",
313
376
  });
314
377
  }
315
378
  }
@@ -339,14 +402,15 @@ export class AgentService {
339
402
  }
340
403
 
341
404
  async read(id: string, owner: AgentOwner, cycleId?: string, offset = 0, limit = 16000) {
342
- const d = this.get(id, owner), cycles = this.cycles(id, owner);
405
+ const w = this.get(id, owner), cycles = this.cycles(id, owner);
343
406
  const c = cycleId ? cycles.find(v => v.cycleId === cycleId) : cycles.at(-1);
344
407
  requireThat(!cycleId || c, "周期不存在。");
345
- const report = c ? await this.report(d, c) : "";
408
+ const output = this.files.output(id);
409
+ const report = c && output?.cycleId === c.cycleId ? output.text : "";
346
410
  this.check();
347
411
  requireThat(offset <= report.length, "分页位置超出报告。");
348
412
  return {
349
- ...this.view(d, owner),
413
+ ...this.view(w, owner),
350
414
  ...(c ? { cycle: c } : {}),
351
415
  cycles: cycles.map(v => ({ cycleId: v.cycleId, state: v.state })),
352
416
  report: report.slice(offset, offset + limit),
@@ -355,17 +419,15 @@ export class AgentService {
355
419
  };
356
420
  }
357
421
 
358
- private report(d: AgentDescriptor, c: AgentCycle) {
359
- if (c.leaf === null) return "";
360
- return this.host.report(d, this.files.reference(d.agentId), c.leaf);
361
- }
362
-
363
422
  private release(id: string) {
364
423
  let pending = this.#releasing.get(id);
365
424
  if (!pending) {
366
- const generation = this.files.read(id, "launch.json", launch).generation;
425
+ const generation = this.files.worker(id).generation;
367
426
  pending = Promise.resolve().then(async () => {
368
- if (this.files.read(id, "launch.json", launch).generation === generation) await this.host.release(id, generation);
427
+ if (this.files.worker(id).generation === generation) {
428
+ await this.host.release(id, generation);
429
+ this.files.clearTransient(id);
430
+ }
369
431
  }).finally(() => this.#releasing.delete(id));
370
432
  this.#releasing.set(id, pending);
371
433
  }
@@ -378,16 +440,25 @@ export class AgentService {
378
440
 
379
441
  private async dispose(owner?: AgentOwner, preserve = false) {
380
442
  if (this.#closed) return;
381
- const ids = !preserve && owner ? this.list(owner).filter(v => v.alive).map(v => v.agentId) : [];
382
- this.#closed = true; this.#preserve = preserve;
443
+ const ownedRecords = !preserve && owner ? this.records(owner) : [];
444
+ const aliveIds = ownedRecords.filter(w => this.alive(w.agentId)).map(w => w.agentId);
445
+ this.#closed = true;
446
+ this.#preserve = preserve;
383
447
  const errors: unknown[] = [];
384
- for (const id of ids) { try { this.requestStop(id); } catch (error) { errors.push(error); } }
448
+ for (const id of aliveIds) { try { this.requestStop(id); } catch (error) { errors.push(error); } }
385
449
  if (!preserve) for (const pending of this.#starting.values()) pending.controller.abort();
386
450
  const deadline = Date.now() + 5000;
387
- while (ids.some(id => this.alive(id)) && Date.now() < deadline) await new Promise(r => setTimeout(r, 100));
388
- const released = await Promise.allSettled(ids.filter(id => this.host.alive(id)).map(id => this.release(id)));
451
+ while (aliveIds.some(id => this.alive(id)) && Date.now() < deadline) await new Promise(r => setTimeout(r, 100));
452
+ const released = await Promise.allSettled(aliveIds.filter(id => this.host.alive(id)).map(id => this.release(id)));
389
453
  for (const value of released) if (value.status === "rejected") errors.push(value.reason);
390
454
  await Promise.allSettled([...this.#launches]);
455
+ if (!preserve && owner) {
456
+ for (const w of ownedRecords) {
457
+ if (!this.host.alive(w.agentId)) {
458
+ try { this.files.removeWorker(w.agentId); } catch (error) { errors.push(error); }
459
+ }
460
+ }
461
+ }
391
462
  if (errors.length) throw new AggregateError(errors, "停止或释放 worker 失败;请核对仍存活的执行资源。");
392
463
  }
393
464
  }
@@ -57,7 +57,7 @@ function formatSessionSummaries(summaries: readonly SummaryArtifact[]): string {
57
57
  export const monthlySummaryDimension: SummaryDimension = {
58
58
  id: "monthly",
59
59
  title: "月度历史汇总",
60
- description: "按自然月(UTC YYYY/MM)归纳当月单会话摘要,提炼月度主题、关键决定与产出,支持逐层下钻。",
60
+ description: "按会话开始的 UTC 月份汇总会话摘要,可从月度结果查回各会话。",
61
61
  requiresMonthlyLayout: true,
62
62
 
63
63
  plan(
@@ -46,7 +46,7 @@ function isStrictUnchangedPrefix(
46
46
  export const sessionSummaryDimension: SummaryDimension = {
47
47
  id: "session",
48
48
  title: "会话任务摘要",
49
- description: "按单个会话提炼任务目标、关键决定、结果与未决事项,支持去重跳过与增量滚动更新。",
49
+ description: "汇总单次会话的目标、决定、结果和未决事项。",
50
50
 
51
51
  plan(
52
52
  raws: readonly RawHistory[],
@@ -119,9 +119,12 @@ export const sessionSummaryDimension: SummaryDimension = {
119
119
  ...heading,
120
120
  `【已有摘要(${task.existing.from} — ${task.existing.to})】\n${task.existing.text}`,
121
121
  `【新增对话】\n${formatExchanges(task.addedExchanges)}`,
122
- "在已有摘要基础上合并新增对话,输出更新后的完整摘要:保留仍有效的事实,用新决定覆盖旧决定,去重并保持简洁。",
123
122
  ].join("\n\n");
124
- return { system, prompt, maxTokens: 2048 };
123
+ return {
124
+ system: system + "\n合并已有摘要与新增对话,输出完整摘要。保留仍有效的事实,用后续明确的决定更新相应结论。",
125
+ prompt,
126
+ maxTokens: 2048,
127
+ };
125
128
  }
126
129
  const prompt = [
127
130
  ...heading,
@@ -94,9 +94,9 @@ function evaluateSummaryReminder(
94
94
 
95
95
  const rawAgeDays = Math.floor((nowMs - oldestRawMs) / dayMs);
96
96
  const daysSinceLastSummary = latestSummaryMs === undefined ? undefined : Math.floor((nowMs - latestSummaryMs) / dayMs);
97
- const message = daysSinceLastSummary === undefined
98
- ? `历史原文已累计 ${rawAgeDays} 天(待摘要 ${pendingCount} 项),尚未触发过摘要整理;可在终端运行 pbot history summary 生成摘要(Pi/dsh 可用 /pbot history summary)。`
99
- : `历史原文已累计 ${rawAgeDays} 天,且已 ${daysSinceLastSummary} 天未触发摘要整理(待摘要 ${pendingCount} 项);可在终端运行 pbot history summary 更新摘要(Pi/dsh 可用 /pbot history summary)。`;
97
+ const lastUpdate = daysSinceLastSummary === undefined ? "尚无摘要" : `上次更新摘要在 ${daysSinceLastSummary} 天前`;
98
+ const message = `工作区已积累 ${rawAgeDays} 天的历史,${lastUpdate},${pendingCount} 项待摘要。` +
99
+ "可在工作区根目录运行 pbot history summary,或在 Pi、dsh 会话中输入 /pbot history summary。";
100
100
 
101
101
  return {
102
102
  rawCount: raws.length,
@@ -19,13 +19,11 @@ export interface UpgradeArgs {
19
19
 
20
20
  export interface WorkspaceUpgradeTarget {
21
21
  host: string;
22
- currentVersion?: string;
23
- bindingPath?: string;
24
22
  }
25
23
 
26
24
  export const upgradeUsage =
27
25
  "用法:pbot upgrade|update [--workspace <目录>] [--version <MAJOR.MINOR.PATCH>] [--hosts <h1,h2>] [--dsh-profile <名称>] [--json]\n" +
28
- "将全局 CLI 与当前工作区已绑定的各宿主插件统一升级到 npm 最新稳定版(或指定版本)。";
26
+ "将全局 CLI 与本机/当前工作区已安装的各宿主插件统一升级到 npm 最新稳定版(或指定版本)。";
29
27
 
30
28
  export function parseUpgradeArgs(args: readonly string[]): UpgradeArgs {
31
29
  let workspace: string | undefined;
@@ -97,7 +95,6 @@ export async function executeWorkspaceUpgrade(
97
95
  result = {
98
96
  host: target.host,
99
97
  state: "skipped",
100
- ...(target.currentVersion ? { previousVersion: target.currentVersion } : {}),
101
98
  detail: "未注册该宿主的自动升级适配器,已跳过。",
102
99
  };
103
100
  } else {
@@ -108,7 +105,6 @@ export async function executeWorkspaceUpgrade(
108
105
  result = {
109
106
  host: target.host,
110
107
  state: "failed",
111
- ...(target.currentVersion ? { previousVersion: target.currentVersion } : {}),
112
108
  detail: error instanceof Error ? error.message : String(error),
113
109
  };
114
110
  }