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