personal-bot 0.3.6 → 0.4.0

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 (143) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +1 -1
  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/README.md +1 -1
  7. package/docs/architecture/design.md +2 -2
  8. package/docs/architecture/extensibility.md +1 -1
  9. package/docs/architecture/model-context.md +1 -2
  10. package/docs/guides/capabilities.md +4 -8
  11. package/docs/guides/cross-harness-workers.md +4 -4
  12. package/docs/guides/history.md +1 -3
  13. package/docs/guides/workspace-configuration.md +4 -4
  14. package/docs/hosts/claude-code.md +2 -2
  15. package/docs/hosts/codex-qoder.md +1 -1
  16. package/lib/antigravity-plugin/bin/pbot-pi-acp.mjs +1 -1
  17. package/lib/antigravity-plugin/lib/extension/acp/member.d.ts +1 -1
  18. package/lib/antigravity-plugin/lib/extension/acp/member.js +11 -10
  19. package/lib/antigravity-plugin/lib/extension/acp-worker.js +348 -203
  20. package/lib/antigravity-plugin/lib/extension/cli.js +574 -549
  21. package/lib/antigravity-plugin/lib/extension/hosts/dsh/acp-member.js +1 -1
  22. package/lib/antigravity-plugin/lib/extension/hosts/pi/acp-member.js +3 -4
  23. package/lib/antigravity-plugin/lib/extension/hosts/plugin-runtime.js +0 -11
  24. package/lib/antigravity-plugin/lib/extension/mcp.js +462 -326
  25. package/lib/antigravity-plugin/lib/integrations/agent-resources.js +2 -2
  26. package/lib/antigravity-plugin/lib/model/acp.d.ts +29 -35
  27. package/lib/antigravity-plugin/lib/model/agent.d.ts +33 -17
  28. package/lib/antigravity-plugin/lib/model/agent.js +0 -1
  29. package/lib/antigravity-plugin/lib/storage/acp-files.d.ts +5 -8
  30. package/lib/antigravity-plugin/lib/storage/acp-files.js +34 -40
  31. package/lib/antigravity-plugin/lib/storage/agent-files.d.ts +19 -10
  32. package/lib/antigravity-plugin/lib/storage/agent-files.js +147 -43
  33. package/lib/antigravity-plugin/package.json +1 -1
  34. package/lib/antigravity-plugin/plugin.json +1 -1
  35. package/lib/antigravity-plugin/resources/agents/coordinator.md +3 -7
  36. package/lib/antigravity-plugin/resources/agents/general-purpose.md +3 -3
  37. package/lib/antigravity-plugin/resources/prompts/plugin-coordinator.md +1 -2
  38. package/lib/antigravity-plugin/resources/prompts/workspace.md +12 -16
  39. package/lib/antigravity-plugin/resources/skills/provider-connections/SKILL.md +7 -6
  40. package/lib/antigravity-plugin/resources/templates/workspace-agents.md +0 -4
  41. package/lib/antigravity-plugin/skills/pbot/SKILL.md +14 -6
  42. package/lib/antigravity-plugin/skills/provider-connections/SKILL.md +7 -6
  43. package/lib/codex-marketplace/plugins/personal-bot/.codex-plugin/plugin.json +1 -1
  44. package/lib/codex-marketplace/plugins/personal-bot/.mcp.json +2 -2
  45. package/lib/codex-marketplace/plugins/personal-bot/hooks/hooks.json +3 -3
  46. package/lib/codex-marketplace/plugins/personal-bot/skills/pbot/SKILL.md +2 -2
  47. package/lib/extension/acp/member.d.ts +1 -1
  48. package/lib/extension/acp/member.js +11 -10
  49. package/lib/extension/acp/worker.js +34 -33
  50. package/lib/extension/acp-worker.js +348 -203
  51. package/lib/extension/cli.js +574 -549
  52. package/lib/extension/hosts/dsh/acp-member.js +1 -1
  53. package/lib/extension/hosts/dsh/command.js +1 -1
  54. package/lib/extension/hosts/dsh/workspace.js +1 -3
  55. package/lib/extension/hosts/pi/acp-member.js +3 -4
  56. package/lib/extension/hosts/plugin-runtime.js +0 -11
  57. package/lib/extension/mcp/tool-contract.d.ts +8 -50
  58. package/lib/extension/mcp/tool-contract.js +48 -62
  59. package/lib/extension/mcp/workers.d.ts +13 -27
  60. package/lib/extension/mcp/workers.js +60 -25
  61. package/lib/extension/mcp.js +462 -326
  62. package/lib/integrations/acp/agents.d.ts +2 -3
  63. package/lib/integrations/acp/agents.js +22 -22
  64. package/lib/integrations/acp/harnesses/claude.js +1 -1
  65. package/lib/integrations/agent-resources.js +2 -2
  66. package/lib/model/acp.d.ts +29 -35
  67. package/lib/model/agent.d.ts +33 -17
  68. package/lib/model/agent.js +0 -1
  69. package/lib/qoder-plugin/.mcp.json +2 -2
  70. package/lib/qoder-plugin/.qoder-plugin/plugin.json +1 -1
  71. package/lib/qoder-plugin/hooks/hooks.json +3 -3
  72. package/lib/qoder-plugin/skills/pbot/SKILL.md +2 -2
  73. package/lib/services/agents/acp-run.d.ts +6 -4
  74. package/lib/services/agents/acp-run.js +62 -43
  75. package/lib/services/agents/agent-run.d.ts +10 -6
  76. package/lib/services/agents/agent-run.js +142 -65
  77. package/lib/services/agents/agent-service.d.ts +3 -21
  78. package/lib/services/agents/agent-service.js +180 -126
  79. package/lib/storage/acp-files.d.ts +5 -8
  80. package/lib/storage/acp-files.js +34 -40
  81. package/lib/storage/agent-files.d.ts +19 -10
  82. package/lib/storage/agent-files.js +147 -43
  83. package/lib/tools/history/contract.js +12 -12
  84. package/package.json +1 -1
  85. package/resources/agents/coordinator.md +3 -7
  86. package/resources/agents/general-purpose.md +3 -3
  87. package/resources/antigravity/skills/pbot/SKILL.md +14 -6
  88. package/resources/claude/skills/pbot/SKILL.md +14 -6
  89. package/resources/plugin/skills/pbot/SKILL.md +2 -2
  90. package/resources/prompts/history-coordinator.md +1 -2
  91. package/resources/prompts/plugin-coordinator.md +1 -2
  92. package/resources/prompts/workspace.md +12 -16
  93. package/resources/skills/provider-connections/SKILL.md +7 -6
  94. package/resources/templates/workspace-agents.md +0 -4
  95. package/src/extension/acp/member.ts +14 -11
  96. package/src/extension/acp/worker.ts +31 -28
  97. package/src/extension/cli/commands/upgrade.ts +8 -36
  98. package/src/extension/cli/commands/workspace.ts +3 -8
  99. package/src/extension/cli/index.ts +1 -2
  100. package/src/extension/hosts/dsh/acp-member.ts +1 -1
  101. package/src/extension/hosts/dsh/command.ts +1 -1
  102. package/src/extension/hosts/dsh/workspace.ts +1 -3
  103. package/src/extension/hosts/pi/acp-member.ts +3 -4
  104. package/src/extension/hosts/pi/command.ts +1 -1
  105. package/src/extension/hosts/pi/lifecycle.ts +0 -2
  106. package/src/extension/hosts/plugin-runtime.ts +0 -10
  107. package/src/extension/mcp/tool-contract.ts +48 -62
  108. package/src/extension/mcp/workers.ts +66 -27
  109. package/src/integrations/acp/agents.ts +28 -27
  110. package/src/integrations/acp/harnesses/claude.ts +1 -1
  111. package/src/integrations/agent-resources.ts +2 -2
  112. package/src/integrations/hosts/dsh/session.ts +2 -2
  113. package/src/integrations/installation/cli-update.ts +2 -2
  114. package/src/integrations/installation/installers/claude.ts +4 -4
  115. package/src/integrations/installation/installers/qoder.ts +5 -5
  116. package/src/integrations/installation/upgraders/antigravity.ts +57 -15
  117. package/src/integrations/installation/upgraders/claude.ts +1 -4
  118. package/src/integrations/installation/upgraders/codex.ts +85 -15
  119. package/src/integrations/installation/upgraders/dsh.ts +46 -24
  120. package/src/integrations/installation/upgraders/pi.ts +18 -11
  121. package/src/integrations/installation/upgraders/qoder.ts +95 -20
  122. package/src/model/acp.ts +27 -29
  123. package/src/model/agent.ts +80 -16
  124. package/src/model/installation.ts +0 -2
  125. package/src/pbot.ts +2 -2
  126. package/src/services/agents/acp-run.ts +81 -49
  127. package/src/services/agents/agent-run.ts +154 -56
  128. package/src/services/agents/agent-service.ts +204 -133
  129. package/src/services/installation/upgrade-service.ts +1 -5
  130. package/src/storage/acp-files.ts +50 -45
  131. package/src/storage/agent-files.ts +159 -46
  132. package/src/tools/history/contract.ts +12 -12
  133. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.d.ts +0 -15
  134. package/lib/antigravity-plugin/lib/services/workspace/plugin-releases.js +0 -126
  135. package/lib/antigravity-plugin/resources/references/compute-finance.md +0 -22
  136. package/lib/antigravity-plugin/resources/references/compute.md +0 -16
  137. package/lib/antigravity-plugin/resources/references/task-conductor.md +0 -25
  138. package/lib/services/workspace/plugin-releases.d.ts +0 -15
  139. package/lib/services/workspace/plugin-releases.js +0 -126
  140. package/resources/references/compute-finance.md +0 -22
  141. package/resources/references/compute.md +0 -16
  142. package/resources/references/task-conductor.md +0 -25
  143. package/src/services/workspace/plugin-releases.ts +0 -121
@@ -1,7 +1,7 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import { setTimeout as delay } from "node:timers/promises";
3
- import { AgentFiles, launch } from "../../storage/agent-files.js";
4
- import { acpAnswer, acpElicitation, acpElicitationReply, acpInput, acpPermission, acpPermissionReply, acpReport } from "../../storage/acp-files.js";
3
+ import { AgentFiles } from "../../storage/agent-files.js";
4
+ import { acpAnswer, acpInteraction, acpInteractionReply } from "../../storage/acp-files.js";
5
5
  import { AgentRun } from "./agent-run.js";
6
6
  export class AcpAnswerBuffer {
7
7
  #text = "";
@@ -19,7 +19,7 @@ export class AcpAnswerBuffer {
19
19
  this.#text += value.text ?? "";
20
20
  }
21
21
  }
22
- /** ACP prompt completion is the receipt boundary; final reports publish before completions. */
22
+ /** ACP prompt completion is the receipt boundary; output publishes before state returns to idle. */
23
23
  export class AcpAgentRun {
24
24
  files;
25
25
  descriptor;
@@ -33,16 +33,16 @@ export class AcpAgentRun {
33
33
  #work;
34
34
  #stop;
35
35
  #failure;
36
- constructor(files, descriptor, generation, work, exactAnswer) {
36
+ constructor(files, descriptor, generation, work, exactAnswer, execution) {
37
37
  this.files = files;
38
38
  this.descriptor = descriptor;
39
39
  this.generation = generation;
40
40
  this.work = work;
41
41
  this.exactAnswer = exactAnswer;
42
- this.execution = new AgentRun(files, descriptor.agentId, generation);
42
+ this.execution = execution ?? new AgentRun(files, descriptor.agentId, generation);
43
43
  }
44
44
  get id() { return this.descriptor.agentId; }
45
- current() { return this.files.read(this.id, "launch.json", launch).generation === this.generation; }
45
+ current() { return this.files.worker(this.id).generation === this.generation; }
46
46
  update(value) {
47
47
  if (!this.execution.current || !this.current())
48
48
  return;
@@ -53,44 +53,49 @@ export class AcpAgentRun {
53
53
  }
54
54
  }
55
55
  async permission(description, options, signal) {
56
- const requestId = randomUUID(), request = { generation: this.generation, requestId, description, options };
57
- this.files.ensureSubdirectory(this.id, "permissions");
58
- this.files.write(this.id, "permissions/" + requestId + ".json", request, acpPermission, true);
56
+ const requestId = randomUUID();
57
+ const request = { generation: this.generation, requestId, kind: "permission", description, options };
58
+ this.files.ensureSubdirectory(this.id, "interactions");
59
+ this.files.write(this.id, "interactions/" + requestId + ".json", request, acpInteraction, true);
59
60
  this.execution.save({ state: "busy", activity: "等待确认" });
60
- while (!signal.aborted && !this.#stopping && this.current() && !this.files.read(this.id, "launch.json", launch).stop) {
61
- const reply = this.files.optional(this.id, "permissions/" + requestId + "-reply.json", acpPermissionReply);
62
- if (reply?.generation === this.generation) {
63
- this.execution.save({ state: this.#stopping ? "stopping" : "busy", activity: reply.optionId ? "已获确认" : "确认被拒绝" });
64
- return reply.optionId;
61
+ try {
62
+ while (!signal.aborted && !this.#stopping && this.current() && !this.files.worker(this.id).stop) {
63
+ const reply = this.files.optional(this.id, "interactions/" + requestId + "-reply.json", acpInteractionReply);
64
+ if (reply?.generation === this.generation && reply.kind === "permission") {
65
+ this.execution.save({ state: this.#stopping ? "stopping" : "busy", activity: reply.optionId ? "已获确认" : "确认被拒绝" });
66
+ return reply.optionId;
67
+ }
68
+ await delay(100);
65
69
  }
66
- await delay(100);
70
+ return null;
71
+ }
72
+ finally {
73
+ this.files.removeInteraction(this.id, requestId);
67
74
  }
68
- // Close the request so it no longer shows as pending once the turn or worker has ended.
69
- if (this.current())
70
- this.files.write(this.id, "permissions/" + requestId + "-reply.json", { generation: this.generation, requestId, optionId: null }, acpPermissionReply, true);
71
- return null;
72
75
  }
73
76
  async elicitation(input, signal) {
74
77
  const requestId = randomUUID();
75
78
  const request = input.mode === "form"
76
- ? { generation: this.generation, requestId, mode: "form", message: input.message, schema: input.schema }
77
- : { generation: this.generation, requestId, mode: "url", message: input.message, url: input.url };
78
- this.files.ensureSubdirectory(this.id, "elicitations");
79
- this.files.write(this.id, "elicitations/" + requestId + ".json", request, acpElicitation, true);
79
+ ? { generation: this.generation, requestId, kind: "form", message: input.message, schema: input.schema }
80
+ : { generation: this.generation, requestId, kind: "url", message: input.message, url: input.url };
81
+ this.files.ensureSubdirectory(this.id, "interactions");
82
+ this.files.write(this.id, "interactions/" + requestId + ".json", request, acpInteraction, true);
80
83
  this.execution.save({ state: "busy", activity: "等待回复" });
81
- while (!signal.aborted && !this.#stopping && this.current() && !this.files.read(this.id, "launch.json", launch).stop) {
82
- const reply = this.files.optional(this.id, "elicitations/" + requestId + "-reply.json", acpElicitationReply);
83
- if (reply?.generation === this.generation) {
84
- const label = reply.action === "accept" ? "已收到回复" : reply.action === "decline" ? "回复已拒绝" : "回复已取消";
85
- this.execution.save({ state: this.#stopping ? "stopping" : "busy", activity: label });
86
- return { action: reply.action, ...(reply.content ? { content: reply.content } : {}) };
84
+ try {
85
+ while (!signal.aborted && !this.#stopping && this.current() && !this.files.worker(this.id).stop) {
86
+ const reply = this.files.optional(this.id, "interactions/" + requestId + "-reply.json", acpInteractionReply);
87
+ if (reply?.generation === this.generation && reply.kind !== "permission") {
88
+ const label = reply.action === "accept" ? "已收到回复" : reply.action === "decline" ? "回复已拒绝" : "回复已取消";
89
+ this.execution.save({ state: this.#stopping ? "stopping" : "busy", activity: label });
90
+ return { action: reply.action, ...(reply.kind === "form" && reply.content ? { content: reply.content } : {}) };
91
+ }
92
+ await delay(100);
87
93
  }
88
- await delay(100);
94
+ return { action: "cancel" };
89
95
  }
90
- if (this.current()) {
91
- this.files.write(this.id, "elicitations/" + requestId + "-reply.json", { generation: this.generation, requestId, action: "cancel" }, acpElicitationReply, true);
96
+ finally {
97
+ this.files.removeInteraction(this.id, requestId);
92
98
  }
93
- return { action: "cancel" };
94
99
  }
95
100
  async run() {
96
101
  this.execution.recover();
@@ -99,7 +104,7 @@ export class AcpAgentRun {
99
104
  this.files.check();
100
105
  if (this.#failure)
101
106
  throw this.#failure;
102
- if (this.files.read(this.id, "launch.json", launch).stop) {
107
+ if (this.files.worker(this.id).stop) {
103
108
  await this.stop();
104
109
  break;
105
110
  }
@@ -126,13 +131,14 @@ export class AcpAgentRun {
126
131
  this.execution.claim(request.requestId);
127
132
  this.execution.enter(request.requestId);
128
133
  this.execution.begin();
129
- this.files.write(this.id, "acp-input.json", { generation: this.generation, requestId: request.requestId }, acpInput);
134
+ this.execution.setActiveRequest(request.requestId);
130
135
  let result;
131
136
  try {
132
137
  const end = await this.work.prompt(request);
133
138
  let snapshot;
134
139
  if (this.exactAnswer) {
135
140
  snapshot = this.files.optional(this.id, "acp-answer.json", acpAnswer);
141
+ this.files.remove(this.id, "acp-answer.json");
136
142
  if (snapshot?.generation !== this.generation || snapshot.requestId !== request.requestId)
137
143
  snapshot = undefined;
138
144
  }
@@ -141,19 +147,32 @@ export class AcpAgentRun {
141
147
  (!this.exactAnswer || snapshot?.state === "completed") && answer.trim() ? "completed" : "failed";
142
148
  if (state === "completed")
143
149
  this.execution.consume(request.requestId);
144
- result = { generation: this.generation, requestId: request.requestId, state, answer,
145
- ...(state === "failed" ? { error: snapshot?.error ?? "ACP 工作未产生有效最终回答:" + end.stopReason } : {}) };
150
+ result = {
151
+ generation: this.generation,
152
+ requestId: request.requestId,
153
+ state,
154
+ answer,
155
+ ...(state === "failed" ? { error: snapshot?.error ?? "ACP 工作未产生有效最终回答:" + end.stopReason } : {}),
156
+ };
146
157
  }
147
158
  catch (error) {
148
- result = { generation: this.generation, requestId: request.requestId, answer: this.#answer.text,
149
- state: this.#stopping || this.files.read(this.id, "launch.json", launch).stop ? "cancelled" : "failed", error: String(error) };
159
+ if (this.exactAnswer)
160
+ this.files.remove(this.id, "acp-answer.json");
161
+ result = {
162
+ generation: this.generation,
163
+ requestId: request.requestId,
164
+ answer: this.#answer.text,
165
+ state: this.#stopping || this.files.worker(this.id).stop ? "cancelled" : "failed",
166
+ error: String(error),
167
+ };
150
168
  }
151
169
  if (!this.current())
152
170
  return;
153
- const cycleId = this.execution.current.cycleId;
154
- this.files.write(this.id, "reports/" + cycleId + ".json", { generation: this.generation, cycleId, text: result.answer }, acpReport, true);
155
- this.execution.finish({ state: this.work.signal.aborted && !this.#stopping ? "interrupted" : result.state,
156
- position: "acp:" + cycleId, answer: result.answer, ...(result.error ? { error: result.error } : {}) });
171
+ this.execution.finish({
172
+ state: this.work.signal.aborted && !this.#stopping ? "interrupted" : result.state,
173
+ answer: result.answer,
174
+ ...(result.error ? { error: result.error } : {}),
175
+ });
157
176
  }
158
177
  settled() { return this.#work; }
159
178
  stop() {
@@ -1,6 +1,7 @@
1
1
  import { AgentFiles } from "../../storage/agent-files.js";
2
+ import type { AcpModelSelection } from "../../model/acp.js";
2
3
  import type { AgentCycle, AgentStatus } from "../../model/agent.js";
3
- /** Durable request receipts and work cycles, independent of a host's event vocabulary. */
4
+ /** Durable request receipts and latest work cycle in state.json + output.json. */
4
5
  export declare class AgentRun {
5
6
  #private;
6
7
  readonly files: AgentFiles;
@@ -10,24 +11,27 @@ export declare class AgentRun {
10
11
  get current(): Readonly<AgentCycle> | undefined;
11
12
  get state(): "error" | "starting" | "busy" | "idle" | "stopping" | "stopped";
12
13
  get pending(): number;
13
- get interactive(): boolean;
14
14
  accepted(requestId: string): boolean;
15
+ setSession(sessionId: string | null, capabilities?: {
16
+ resume: boolean;
17
+ load: boolean;
18
+ close: boolean;
19
+ }): void;
20
+ setSelection(selection: AcpModelSelection): void;
21
+ setActiveRequest(requestId: string | undefined): void;
15
22
  save(patch: Pick<AgentStatus, "state" | "activity"> & {
16
23
  error?: string;
17
24
  }): void;
25
+ private persist;
18
26
  /** A receipt is durable before input is passed to the native execution boundary. */
19
27
  claim(requestId: string): void;
20
28
  /** Native ownership associates work even when preparation fails before model admission. */
21
29
  enter(requestId: string): void;
22
30
  /** Model admission is a separate fact from a native inbox claim. */
23
31
  consume(requestId: string): void;
24
- cancelQueued(requestId: string): void;
25
- humanInput(): void;
26
- enterHuman(): void;
27
32
  begin(): void;
28
33
  finish(end: {
29
34
  state: Exclude<AgentCycle["state"], "running">;
30
- position: string | null;
31
35
  answer: string;
32
36
  error?: string;
33
37
  }): void;
@@ -1,47 +1,105 @@
1
1
  import { randomUUID } from "node:crypto";
2
- import { AgentFiles, launch, status, cycle } from "../../storage/agent-files.js";
2
+ import { AgentFiles, outputRecord, status } from "../../storage/agent-files.js";
3
3
  import { requireThat } from "../../common/validation.js";
4
- /** Durable request receipts and work cycles, independent of a host's event vocabulary. */
4
+ /** Durable request receipts and latest work cycle in state.json + output.json. */
5
5
  export class AgentRun {
6
6
  files;
7
7
  id;
8
8
  generation;
9
9
  #accepted = new Set();
10
+ #inflight = new Set();
10
11
  #assigned = new Set();
11
12
  #consumed = new Set();
12
13
  #queued = [];
13
14
  #saved;
14
- #interactive = false;
15
- #humanConsumed = false;
16
15
  #current;
17
16
  #snapshot;
18
17
  constructor(files, id, generation) {
19
18
  this.files = files;
20
19
  this.id = id;
21
20
  this.generation = generation;
22
- const saved = files.optional(id, "status.json", status);
23
- this.#saved = saved?.generation === generation ? saved : undefined;
21
+ const existing = files.optional(id, "state.json", status);
22
+ this.#saved = existing?.generation === generation ? existing : undefined;
24
23
  const requests = new Set(files.messages(id).filter(m => m.generation === generation).map(m => m.requestId));
25
- for (const requestId of this.#saved?.accepted ?? [])
26
- if (requests.has(requestId))
24
+ for (const requestId of this.#saved?.accepted ?? []) {
25
+ if (requests.has(requestId)) {
27
26
  this.#accepted.add(requestId);
28
- this.#snapshot = { generation, state: "starting", activity: "启动", updatedAt: Date.now() };
27
+ this.#inflight.add(requestId);
28
+ }
29
+ }
30
+ this.#snapshot = {
31
+ generation,
32
+ state: "starting",
33
+ activity: "启动",
34
+ updatedAt: Date.now(),
35
+ ...(existing?.sessionId !== undefined ? { sessionId: existing.sessionId } : {}),
36
+ ...(existing?.capabilities ? { capabilities: existing.capabilities } : {}),
37
+ ...(this.#saved?.selection ? { selection: this.#saved.selection } : {}),
38
+ ...(this.#saved?.activeRequestId ? { activeRequestId: this.#saved.activeRequestId } : {}),
39
+ };
29
40
  }
30
41
  get current() { return this.#current; }
31
42
  get state() { return this.#snapshot.state; }
32
43
  get pending() { return this.#queued.length; }
33
- get interactive() { return this.#interactive; }
34
44
  accepted(requestId) { return this.#accepted.has(requestId); }
45
+ setSession(sessionId, capabilities) {
46
+ this.#snapshot = {
47
+ ...this.#snapshot,
48
+ sessionId,
49
+ ...(capabilities ? { capabilities } : {}),
50
+ updatedAt: Date.now(),
51
+ };
52
+ this.persist();
53
+ }
54
+ setSelection(selection) {
55
+ const clean = Object.fromEntries(Object.entries(selection).filter(([, value]) => value !== undefined));
56
+ this.#snapshot = {
57
+ ...this.#snapshot,
58
+ ...(Object.keys(clean).length ? { selection: clean } : {}),
59
+ updatedAt: Date.now(),
60
+ };
61
+ this.persist();
62
+ }
63
+ setActiveRequest(requestId) {
64
+ const next = { ...this.#snapshot, updatedAt: Date.now() };
65
+ if (requestId)
66
+ next.activeRequestId = requestId;
67
+ else
68
+ delete next.activeRequestId;
69
+ this.#snapshot = next;
70
+ this.persist();
71
+ }
35
72
  save(patch) {
36
- this.#snapshot = { ...this.#snapshot, ...patch, updatedAt: Date.now(), accepted: [...this.#accepted] };
37
- if (this.#current)
38
- this.#snapshot.cycle = structuredClone(this.#current);
39
- this.files.write(this.id, "status.json", this.#snapshot, status);
73
+ const next = {
74
+ ...this.#snapshot,
75
+ state: patch.state,
76
+ activity: patch.activity,
77
+ updatedAt: Date.now(),
78
+ };
79
+ if (patch.error !== undefined)
80
+ next.error = patch.error;
81
+ else
82
+ delete next.error;
83
+ this.#snapshot = next;
84
+ this.persist();
85
+ }
86
+ persist() {
87
+ const record = {
88
+ ...this.#snapshot,
89
+ ...(this.#inflight.size ? { accepted: [...this.#inflight] } : {}),
90
+ ...(this.#current ? { cycle: structuredClone(this.#current) } : {}),
91
+ };
92
+ if (!this.#inflight.size)
93
+ delete record.accepted;
94
+ if (!this.#current)
95
+ delete record.cycle;
96
+ this.files.write(this.id, "state.json", record, status);
40
97
  }
41
98
  /** A receipt is durable before input is passed to the native execution boundary. */
42
99
  claim(requestId) {
43
100
  requireThat(this.files.messages(this.id).some(m => m.requestId === requestId && m.generation === this.generation), "请求不属于当前启动代次。");
44
101
  this.#accepted.add(requestId);
102
+ this.#inflight.add(requestId);
45
103
  this.save({ state: "busy", activity: "消息已领取" });
46
104
  }
47
105
  /** Native ownership associates work even when preparation fails before model admission. */
@@ -53,11 +111,10 @@ export class AgentRun {
53
111
  if (this.#current) {
54
112
  if (!this.#current.requestIds.includes(requestId))
55
113
  this.#current.requestIds.push(requestId);
56
- if (this.#current.source === "interactive")
57
- this.#current.source = "mixed";
58
114
  }
59
- else if (!this.#queued.includes(requestId))
115
+ else if (!this.#queued.includes(requestId)) {
60
116
  this.#queued.push(requestId);
117
+ }
61
118
  this.save({ state: "busy", activity: "消息已接收" });
62
119
  }
63
120
  /** Model admission is a separate fact from a native inbox claim. */
@@ -66,73 +123,93 @@ export class AgentRun {
66
123
  this.#consumed.add(requestId);
67
124
  this.enter(requestId);
68
125
  }
69
- cancelQueued(requestId) {
70
- if (!this.#accepted.has(requestId) || this.#assigned.has(requestId))
71
- return;
72
- const value = { cycleId: requestId, generation: this.generation,
73
- startedAt: this.files.read(this.id, "launch.json", launch).startedAt, finishedAt: Date.now(),
74
- state: "cancelled", leaf: null, requestIds: [requestId], source: "parent" };
75
- this.files.write(this.id, "completions/" + requestId + ".json", value, cycle, true);
76
- this.#assigned.add(requestId);
77
- }
78
- humanInput() { this.#humanConsumed = true; this.enterHuman(); }
79
- enterHuman() {
80
- if (this.#current) {
81
- if (this.#current.source === "parent")
82
- this.#current.source = this.#current.requestIds.length ? "mixed" : "interactive";
83
- }
84
- else
85
- this.#interactive = true;
86
- this.save({ state: "busy", activity: "人工输入" });
87
- }
88
126
  begin() {
89
127
  if (!this.#current) {
90
128
  const requestIds = this.#queued.splice(0);
91
- this.#current = { cycleId: randomUUID(), generation: this.generation, startedAt: Date.now(), state: "running", leaf: null,
92
- requestIds, source: this.#interactive ? requestIds.length ? "mixed" : "interactive" : "parent" };
93
- this.#interactive = false;
129
+ this.#current = {
130
+ cycleId: randomUUID(),
131
+ generation: this.generation,
132
+ startedAt: Date.now(),
133
+ state: "running",
134
+ requestIds,
135
+ };
94
136
  }
95
137
  this.save({ state: "busy", activity: "思考" });
96
138
  }
97
139
  finish(end) {
98
140
  if (!this.#current)
99
141
  return;
100
- const valid = end.position !== null && end.answer.trim().length > 0 &&
101
- (this.#current.source !== "parent" || this.#current.requestIds.length > 0) &&
102
- this.#current.requestIds.every(id => this.#consumed.has(id)) &&
103
- (this.#current.source === "parent" || this.#humanConsumed);
104
- this.#current = { ...this.#current, finishedAt: Date.now(), leaf: end.position,
105
- state: end.state === "completed" && !valid ? "failed" : end.state,
106
- ...(end.error ? { error: end.error } : end.state === "completed" && !valid ? { error: "没有已消费的请求或有效最终回答。" } : {}) };
107
- this.files.write(this.id, "completions/" + this.#current.cycleId + ".json", this.#current, cycle, true);
108
- this.save({ state: "idle", activity: this.#current.state });
142
+ const valid = end.answer.trim().length > 0 &&
143
+ this.#current.requestIds.length > 0 &&
144
+ this.#current.requestIds.every(id => this.#consumed.has(id));
145
+ const finalState = end.state === "completed" && !valid ? "failed" : end.state;
146
+ const error = end.error ?? (end.state === "completed" && !valid ? "没有已消费的请求或有效最终回答。" : undefined);
147
+ const finished = {
148
+ cycleId: this.#current.cycleId,
149
+ generation: this.generation,
150
+ startedAt: this.#current.startedAt,
151
+ finishedAt: Date.now(),
152
+ state: finalState,
153
+ requestIds: [...this.#current.requestIds],
154
+ text: end.answer,
155
+ ...(error ? { error } : {}),
156
+ };
157
+ const completedRequests = [...this.#current.requestIds];
158
+ for (const requestId of completedRequests) {
159
+ this.#inflight.delete(requestId);
160
+ this.files.removeMessage(this.id, requestId);
161
+ }
162
+ this.files.write(this.id, "output.json", finished, outputRecord);
109
163
  this.#current = undefined;
110
- this.#humanConsumed = false;
164
+ delete this.#snapshot.activeRequestId;
165
+ this.save({ state: "idle", activity: finished.state });
111
166
  }
112
167
  /** Reload seals uncertainty. It never turns a receipt into permission to replay. */
113
168
  recover(receipts = []) {
114
169
  const requests = new Set(this.files.messages(this.id).filter(m => m.generation === this.generation).map(m => m.requestId));
115
- for (const receipt of receipts)
116
- if (receipt.generation === this.generation && requests.has(receipt.requestId))
170
+ for (const receipt of receipts) {
171
+ if (receipt.generation === this.generation && requests.has(receipt.requestId)) {
117
172
  this.#accepted.add(receipt.requestId);
118
- const completed = this.files.completions(this.id);
119
- for (const c of completed)
120
- if (c.generation === this.generation)
121
- for (const id of c.requestIds)
122
- this.#assigned.add(id);
123
- if (this.#saved?.cycle?.state === "running" && !completed.some(c => c.cycleId === this.#saved.cycle.cycleId)) {
124
- const interrupted = { ...this.#saved.cycle, state: "interrupted", finishedAt: Date.now() };
125
- this.files.write(this.id, "completions/" + interrupted.cycleId + ".json", interrupted, cycle, true);
126
- completed.push(interrupted);
173
+ this.#inflight.add(receipt.requestId);
174
+ }
175
+ }
176
+ let latest = this.files.output(this.id);
177
+ if (latest?.generation === this.generation) {
178
+ for (const id of latest.requestIds)
179
+ this.#assigned.add(id);
180
+ }
181
+ if (this.#saved?.cycle?.state === "running" && latest?.cycleId !== this.#saved.cycle.cycleId) {
182
+ const interrupted = {
183
+ cycleId: this.#saved.cycle.cycleId,
184
+ generation: this.#saved.cycle.generation,
185
+ startedAt: this.#saved.cycle.startedAt,
186
+ finishedAt: Date.now(),
187
+ state: "interrupted",
188
+ requestIds: [...this.#saved.cycle.requestIds],
189
+ text: "",
190
+ ...(this.#saved.cycle.error ? { error: this.#saved.cycle.error } : {}),
191
+ };
192
+ this.files.write(this.id, "output.json", interrupted, outputRecord);
193
+ latest = interrupted;
194
+ for (const id of interrupted.requestIds)
195
+ this.#assigned.add(id);
127
196
  }
128
197
  for (const requestId of this.#accepted) {
129
198
  this.#assigned.add(requestId);
130
- if (completed.some(c => c.generation === this.generation && c.requestIds.includes(requestId)))
199
+ if (latest?.generation === this.generation && latest.requestIds.includes(requestId))
131
200
  continue;
132
- const interrupted = { cycleId: requestId, generation: this.generation,
133
- startedAt: this.files.read(this.id, "launch.json", launch).startedAt, finishedAt: Date.now(),
134
- state: "interrupted", leaf: null, requestIds: [requestId], source: "parent", error: "领取后执行结果不明;未重跑。" };
135
- this.files.write(this.id, "completions/" + requestId + ".json", interrupted, cycle, true);
201
+ const interrupted = {
202
+ cycleId: requestId,
203
+ generation: this.generation,
204
+ startedAt: this.files.worker(this.id).startedAt,
205
+ finishedAt: Date.now(),
206
+ state: "interrupted",
207
+ requestIds: [requestId],
208
+ text: "",
209
+ error: "领取后执行结果不明;未重跑。",
210
+ };
211
+ this.files.write(this.id, "output.json", interrupted, outputRecord);
212
+ latest = interrupted;
136
213
  }
137
214
  }
138
215
  }
@@ -1,4 +1,4 @@
1
- import type { AgentOwner, AgentOptions, AgentMessage, AgentPendingRequest, AgentResponseInput, AgentStartOptions, AgentCycle, AgentHost, AgentView } from "../../model/agent.js";
1
+ import type { AgentCycle, AgentHost, AgentMessage, AgentOptions, AgentOwner, AgentPendingRequest, AgentResponseInput, AgentStartOptions, AgentView } from "../../model/agent.js";
2
2
  export type { AgentHost } from "../../model/agent.js";
3
3
  /** Host-neutral lifecycle manager for cross-harness ACP workers. */
4
4
  export declare class AgentService {
@@ -12,6 +12,7 @@ export declare class AgentService {
12
12
  private owned;
13
13
  private get;
14
14
  private records;
15
+ has(id: string, owner: AgentOwner): boolean;
15
16
  list(owner: AgentOwner): AgentView[];
16
17
  private view;
17
18
  /** Answer a pending permission or elicitation request forwarded by the worker. */
@@ -19,7 +20,6 @@ export declare class AgentService {
19
20
  start(input: AgentStartOptions, owner: AgentOwner): Promise<{
20
21
  requestId?: string;
21
22
  messageStatus?: "queued";
22
- name: string;
23
23
  generation: string;
24
24
  state: import("../../model/agent.js").AgentStatus["state"];
25
25
  activity: string;
@@ -39,17 +39,13 @@ export declare class AgentService {
39
39
  agentId: string;
40
40
  release: string;
41
41
  workspace: string;
42
- cwd: string;
43
42
  parentSessionId: string;
44
- parentLeaf: string | null;
45
- createdAt: number;
46
43
  options: AgentOptions;
47
44
  }>;
48
45
  private launch;
49
46
  send(id: string, text: string, delivery: AgentMessage["delivery"], owner: AgentOwner, attachments?: readonly string[]): {
50
47
  requestId: string;
51
48
  messageStatus: "queued";
52
- name: string;
53
49
  generation: string;
54
50
  state: import("../../model/agent.js").AgentStatus["state"];
55
51
  activity: string;
@@ -69,20 +65,14 @@ export declare class AgentService {
69
65
  agentId: string;
70
66
  release: string;
71
67
  workspace: string;
72
- cwd: string;
73
68
  parentSessionId: string;
74
- parentLeaf: string | null;
75
- createdAt: number;
76
69
  options: AgentOptions;
77
70
  };
78
- rename(id: string, name: string, owner: AgentOwner): AgentView;
79
71
  stop(id: string, owner: AgentOwner): AgentView;
80
72
  private requestStop;
81
- enter(id: string, owner: AgentOwner): Promise<AgentView>;
82
- resume(id: string, text: string, owner: AgentOwner): Promise<{
73
+ resume(id: string, text: string | undefined, owner: AgentOwner): Promise<{
83
74
  requestId?: string;
84
75
  messageStatus?: "queued";
85
- name: string;
86
76
  generation: string;
87
77
  state: import("../../model/agent.js").AgentStatus["state"];
88
78
  activity: string;
@@ -102,10 +92,7 @@ export declare class AgentService {
102
92
  agentId: string;
103
93
  release: string;
104
94
  workspace: string;
105
- cwd: string;
106
95
  parentSessionId: string;
107
- parentLeaf: string | null;
108
- createdAt: number;
109
96
  options: AgentOptions;
110
97
  }>;
111
98
  private restart;
@@ -127,7 +114,6 @@ export declare class AgentService {
127
114
  totalLength: number;
128
115
  nextOffset: number | null;
129
116
  cycle?: AgentCycle;
130
- name: string;
131
117
  generation: string;
132
118
  state: import("../../model/agent.js").AgentStatus["state"];
133
119
  activity: string;
@@ -146,13 +132,9 @@ export declare class AgentService {
146
132
  agentId: string;
147
133
  release: string;
148
134
  workspace: string;
149
- cwd: string;
150
135
  parentSessionId: string;
151
- parentLeaf: string | null;
152
- createdAt: number;
153
136
  options: AgentOptions;
154
137
  }>;
155
- private report;
156
138
  private release;
157
139
  close(owner?: AgentOwner, preserve?: boolean): Promise<void>;
158
140
  private dispose;