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
@@ -3,72 +3,77 @@ import { object } from "../common/json.js";
3
3
  import { isAcpBackend, isOneOf, requireThat } from "../common/validation.js";
4
4
  import { fileId } from "./file-access.js";
5
5
  import { acpAnswerStates, acpElicitationActions, acpPermissionPolicies, isAcpElicitationValue } from "../model/acp.js";
6
- import type { AcpAnswer, AcpElicitation, AcpElicitationReply, AcpInput, AcpPermission, AcpPermissionReply, AcpModelState, AcpProcess, AcpReport, AcpSessionReference } from "../model/acp.js";
6
+ import type { AcpAnswer, AcpInput, AcpInteraction, AcpInteractionReply, AcpSessionReference } from "../model/acp.js";
7
+ import type { AgentAcpConfig } from "../model/agent.js";
7
8
 
8
9
  const text = (value: unknown): value is string => typeof value === "string" && value.length > 0 && !value.includes("\0");
9
- export function acpReference(value: unknown): AcpSessionReference {
10
- requireThat(object(value) && value.host === "acp" && isAcpBackend(value.backend) &&
11
- text(value.command) && isAbsolute(value.command) && Array.isArray(value.args) && value.args.every(arg => typeof arg === "string" && !arg.includes("\0")) &&
12
- (value.sessionId === null || text(value.sessionId)), "ACP 会话引用无效。");
10
+
11
+ export function acpConfig(value: unknown): AgentAcpConfig {
12
+ requireThat(object(value) && isAcpBackend(value.backend) &&
13
+ text(value.command) && isAbsolute(value.command) &&
14
+ Array.isArray(value.args) && value.args.every(arg => typeof arg === "string" && !arg.includes("\0")), "ACP 启动配置无效。");
13
15
  if (value.env !== undefined) {
14
16
  requireThat(object(value.env) && Object.entries(value.env).every(([k, v]) => text(k) && typeof v === "string" && !v.includes("\0")), "ACP 环境变量配置无效。");
15
17
  }
16
18
  requireThat(value.permission === undefined || isOneOf(value.permission, acpPermissionPolicies), "ACP 权限策略无效。");
19
+ return value as unknown as AgentAcpConfig;
20
+ }
21
+
22
+ export function acpReference(value: unknown): AcpSessionReference {
23
+ requireThat(object(value) && value.host === "acp" && (value.sessionId === null || text(value.sessionId)), "ACP 会话引用无效。");
24
+ acpConfig(value);
17
25
  const capabilities = value.capabilities;
18
- if (capabilities !== undefined) requireThat(object(capabilities) &&
19
- ["resume", "load", "close"].every(key => typeof capabilities[key] === "boolean"), "ACP 会话能力无效。");
26
+ if (capabilities !== undefined) {
27
+ requireThat(object(capabilities) &&
28
+ ["resume", "load", "close"].every(key => typeof capabilities[key] === "boolean"), "ACP 会话能力无效。");
29
+ }
20
30
  return value as unknown as AcpSessionReference;
21
31
  }
22
- export function acpModelState(value: unknown): AcpModelState {
23
- requireThat(object(value) && text(value.generation) && ["provider", "model", "resolvedModel", "thinking"].every(key => value[key] === undefined || text(value[key])), "ACP 模型选择无效。");
24
- return value as unknown as AcpModelState;
25
- }
26
- export function acpProcess(value: unknown): AcpProcess {
27
- requireThat(object(value) && text(value.generation) && Number.isSafeInteger(value.pid) && Number(value.pid) > 1 &&
28
- typeof value.startedAt === "number" && Number.isFinite(value.startedAt), "ACP 进程记录无效。");
29
- return value as unknown as AcpProcess;
30
- }
32
+
31
33
  export function acpInput(value: unknown): AcpInput {
32
34
  requireThat(object(value) && text(value.generation) && text(value.requestId), "ACP 输入关联无效。");
33
- fileId(value.requestId); return value as unknown as AcpInput;
35
+ fileId(value.requestId);
36
+ return value as unknown as AcpInput;
34
37
  }
38
+
35
39
  export function acpAnswer(value: unknown): AcpAnswer {
36
40
  acpInput(value);
37
41
  requireThat(object(value) && typeof value.answer === "string" && isOneOf(value.state, acpAnswerStates) &&
38
42
  (value.error === undefined || typeof value.error === "string"), "ACP 最终回答无效。");
39
43
  return value as unknown as AcpAnswer;
40
44
  }
41
- export function acpReport(value: unknown): AcpReport {
42
- requireThat(object(value) && text(value.generation) && text(value.cycleId) && typeof value.text === "string", "ACP 周期报告无效。");
43
- fileId(value.cycleId); return value as unknown as AcpReport;
44
- }
45
- export function acpPermission(value: unknown): AcpPermission {
46
- acpInput(value);
47
- requireThat(object(value) && typeof value.description === "string" && Array.isArray(value.options) && value.options.every(option =>
48
- object(option) && text(option.optionId) && text(option.name) && isOneOf(option.kind, ["allow_once", "allow_always", "reject_once", "reject_always"])), "ACP 确认请求无效。");
49
- return value as unknown as AcpPermission;
50
- }
51
- export function acpPermissionReply(value: unknown): AcpPermissionReply {
52
- acpInput(value);
53
- requireThat(object(value) && (value.optionId === null || text(value.optionId)), "ACP 确认答复无效。");
54
- return value as unknown as AcpPermissionReply;
55
- }
56
- export function acpElicitation(value: unknown): AcpElicitation {
45
+
46
+ export function acpInteraction(value: unknown): AcpInteraction {
57
47
  acpInput(value);
58
- requireThat(object(value) && (value.mode === "form" || value.mode === "url") && typeof value.message === "string", "ACP 交互请求无效。");
59
- if (value.mode === "form") {
60
- requireThat(object(value.schema) && value.url === undefined, "ACP 表单交互请求缺少有效 schema。");
61
- } else {
62
- requireThat(text(value.url) && value.schema === undefined, "ACP URL 交互请求缺少有效 url。");
48
+ requireThat(object(value), "ACP 交互请求无效。");
49
+ if (value.kind === "permission") {
50
+ requireThat(typeof value.description === "string" && Array.isArray(value.options) && value.options.every(option =>
51
+ object(option) && text(option.optionId) && text(option.name) && isOneOf(option.kind, ["allow_once", "allow_always", "reject_once", "reject_always"])), "ACP 确认请求无效。");
52
+ return value as unknown as AcpInteraction;
53
+ }
54
+ if (value.kind === "form") {
55
+ requireThat(typeof value.message === "string" && object(value.schema), "ACP 表单交互请求缺少有效 schema。");
56
+ return value as unknown as AcpInteraction;
63
57
  }
64
- return value as unknown as AcpElicitation;
58
+ requireThat(value.kind === "url" && typeof value.message === "string" && text(value.url), "ACP URL 交互请求无效。");
59
+ return value as unknown as AcpInteraction;
65
60
  }
66
- export function acpElicitationReply(value: unknown): AcpElicitationReply {
61
+
62
+ export function acpInteractionReply(value: unknown): AcpInteractionReply {
67
63
  acpInput(value);
68
- requireThat(object(value) && isOneOf(value.action, acpElicitationActions), "ACP 交互答复无效。");
69
- if (value.content !== undefined) {
70
- requireThat(value.action === "accept" && object(value.content) &&
71
- Object.values(value.content).every(isAcpElicitationValue), "ACP 表单答复 content 无效。");
64
+ requireThat(object(value), "ACP 交互答复无效。");
65
+ if (value.kind === "permission") {
66
+ requireThat(value.optionId === null || text(value.optionId), "ACP 确认答复无效。");
67
+ return value as unknown as AcpInteractionReply;
68
+ }
69
+ if (value.kind === "form") {
70
+ requireThat(isOneOf(value.action, acpElicitationActions), "ACP 表单答复 action 无效。");
71
+ if (value.content !== undefined) {
72
+ requireThat(value.action === "accept" && object(value.content) &&
73
+ Object.values(value.content).every(isAcpElicitationValue), "ACP 表单答复 content 无效。");
74
+ }
75
+ return value as unknown as AcpInteractionReply;
72
76
  }
73
- return value as unknown as AcpElicitationReply;
77
+ requireThat(value.kind === "url" && isOneOf(value.action, acpElicitationActions), "ACP URL 交互答复无效。");
78
+ return value as unknown as AcpInteractionReply;
74
79
  }
@@ -1,43 +1,103 @@
1
- import { existsSync, mkdirSync, readdirSync, rmSync } from "node:fs";
1
+ import { existsSync, mkdirSync, readdirSync, rmdirSync, rmSync, unlinkSync } from "node:fs";
2
2
  import { join } from "node:path";
3
3
  import { randomUUID } from "node:crypto";
4
4
  import { canonical, object } from "../common/json.js";
5
5
  import { isOneOf, requireThat } from "../common/validation.js";
6
6
  import { readJsonFile, writeJsonFile } from "./atomic-json.js";
7
7
  import { fileId, privateDirectory } from "./file-access.js";
8
- import { agentStates, cycleSources, cycleStates, messageDeliveries } from "../model/agent.js";
9
- import type { AgentDescriptor, AgentLaunch, AgentMessage, AgentStatus, AgentCycle, AgentSessionReference } from "../model/agent.js";
8
+ import { agentStates, cycleStates, messageDeliveries } from "../model/agent.js";
9
+ import type {
10
+ AgentCycle,
11
+ AgentDescriptor,
12
+ AgentMessage,
13
+ AgentOutput,
14
+ AgentSessionReference,
15
+ AgentStatus,
16
+ AgentWorkerRecord,
17
+ } from "../model/agent.js";
10
18
  import type { AcpSessionReference } from "../model/acp.js";
11
- import { acpElicitation, acpPermission, acpReference } from "./acp-files.js";
19
+ import { acpConfig, acpInteraction, acpReference } from "./acp-files.js";
12
20
 
13
21
  const text = (v: unknown): v is string => typeof v === "string" && v.length > 0;
14
22
  const number = (v: unknown): v is number => typeof v === "number" && Number.isFinite(v);
15
- export function descriptor(v: unknown): AgentDescriptor {
16
- requireThat(object(v) && text(v.agentId) && text(v.release) && text(v.workspace) && text(v.cwd) && text(v.parentSessionId) &&
17
- (v.parentLeaf === null || text(v.parentLeaf)) && number(v.createdAt) && object(v.options), "子会话身份无效。");
18
- fileId(v.agentId as string); return v as unknown as AgentDescriptor;
19
- }
20
- export function launch(v: unknown): AgentLaunch {
21
- requireThat(object(v) && text(v.generation) && text(v.name) && Number.isSafeInteger(v.sequence) && typeof v.stop === "boolean" && number(v.startedAt), "子会话启动状态无效。");
22
- return v as unknown as AgentLaunch;
23
+
24
+ export function workerRecord(v: unknown): AgentWorkerRecord {
25
+ requireThat(object(v) && text(v.agentId) && text(v.release) && text(v.workspace) && text(v.parentSessionId) &&
26
+ text(v.generation) && Number.isSafeInteger(v.sequence) && typeof v.stop === "boolean" && number(v.startedAt) &&
27
+ (v.pid === undefined || (Number.isSafeInteger(v.pid) && Number(v.pid) > 1)) &&
28
+ (v.options === undefined || object(v.options)), "worker 控制记录无效。");
29
+ fileId(v.agentId as string);
30
+ acpConfig(v.acp);
31
+ return v as unknown as AgentWorkerRecord;
23
32
  }
33
+
24
34
  export function message(v: unknown): AgentMessage {
25
35
  requireThat(object(v) && text(v.requestId) && text(v.generation) && Number.isSafeInteger(v.sequence) && typeof v.message === "string" &&
26
36
  isOneOf(v.delivery, messageDeliveries) && Array.isArray(v.files) && v.files.every(text), "子会话消息无效。");
27
37
  return v as unknown as AgentMessage;
28
38
  }
39
+
29
40
  export function cycle(v: unknown): AgentCycle {
30
41
  requireThat(object(v) && text(v.cycleId) && text(v.generation) && number(v.startedAt) &&
31
- isOneOf(v.state, cycleStates) &&
32
- (v.leaf === null || text(v.leaf)) && Array.isArray(v.requestIds) && v.requestIds.every(text) &&
33
- isOneOf(v.source, cycleSources) && (v.finishedAt === undefined || number(v.finishedAt)), "子会话周期无效。");
42
+ isOneOf(v.state, cycleStates) && Array.isArray(v.requestIds) && v.requestIds.every(text) &&
43
+ (v.finishedAt === undefined || number(v.finishedAt)) &&
44
+ (v.error === undefined || typeof v.error === "string"), "子会话周期无效。");
34
45
  return v as unknown as AgentCycle;
35
46
  }
47
+
36
48
  export function status(v: unknown): AgentStatus {
37
49
  requireThat(object(v) && text(v.generation) && isOneOf(v.state, agentStates) &&
38
50
  typeof v.activity === "string" && number(v.updatedAt), "子会话状态无效。");
39
- if (v.cycle) cycle(v.cycle); return v as unknown as AgentStatus;
51
+ if (v.sessionId !== undefined) requireThat(v.sessionId === null || text(v.sessionId), "ACP 会话 ID 无效。");
52
+ if (v.capabilities !== undefined) {
53
+ requireThat(object(v.capabilities) &&
54
+ ["resume", "load", "close"].every(key => typeof (v.capabilities as Record<string, unknown>)[key] === "boolean"), "ACP 会话能力无效。");
55
+ }
56
+ if (v.selection !== undefined) {
57
+ requireThat(object(v.selection) &&
58
+ ["provider", "model", "resolvedModel", "thinking"].every(key => (v.selection as Record<string, unknown>)[key] === undefined || text((v.selection as Record<string, unknown>)[key])), "ACP 模型选择无效。");
59
+ }
60
+ if (v.activeRequestId !== undefined) {
61
+ requireThat(text(v.activeRequestId), "ACP 输入关联无效。");
62
+ fileId(v.activeRequestId);
63
+ }
64
+ if (v.accepted !== undefined) requireThat(Array.isArray(v.accepted) && v.accepted.every(text), "已领取请求列表无效。");
65
+ if (v.cycle !== undefined) cycle(v.cycle);
66
+ if (v.error !== undefined) requireThat(typeof v.error === "string", "子会话错误信息无效。");
67
+ return v as unknown as AgentStatus;
40
68
  }
69
+
70
+ export function outputRecord(v: unknown): AgentOutput {
71
+ requireThat(object(v) && text(v.cycleId) && text(v.generation) && number(v.startedAt) && number(v.finishedAt) &&
72
+ isOneOf(v.state, ["completed", "failed", "cancelled", "interrupted"] as const) &&
73
+ Array.isArray(v.requestIds) && v.requestIds.every(text) &&
74
+ typeof v.text === "string" && (v.error === undefined || typeof v.error === "string"), "子会话输出记录无效。");
75
+ fileId(v.cycleId as string);
76
+ return v as unknown as AgentOutput;
77
+ }
78
+
79
+ export function toDescriptor(record: AgentWorkerRecord): AgentDescriptor {
80
+ return {
81
+ agentId: record.agentId,
82
+ release: record.release,
83
+ workspace: record.workspace,
84
+ parentSessionId: record.parentSessionId,
85
+ options: record.options ?? {},
86
+ };
87
+ }
88
+
89
+ function tryUnlink(path: string) {
90
+ try { unlinkSync(path); } catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; }
91
+ }
92
+
93
+ function tryRmdir(path: string) {
94
+ try { rmdirSync(path); }
95
+ catch (error) {
96
+ const code = (error as NodeJS.ErrnoException).code;
97
+ if (code !== "ENOENT" && code !== "ENOTEMPTY" && code !== "EEXIST") throw error;
98
+ }
99
+ }
100
+
41
101
  export class AgentFiles {
42
102
  #failure: unknown;
43
103
  constructor(readonly root: string) {}
@@ -55,51 +115,101 @@ export class AgentFiles {
55
115
  catch (error) { this.fail(error); throw error; }
56
116
  requireThat(published, "子会话文件已存在。");
57
117
  }
58
- create(value: AgentDescriptor, run: AgentLaunch, acp: AcpSessionReference) {
118
+ remove(id: string, path: string) {
119
+ tryUnlink(join(this.directory(id), path));
120
+ }
121
+ create(value: AgentDescriptor, run: { generation: string; sequence: number; stop: boolean; startedAt: number }, acp: AcpSessionReference) {
59
122
  this.check();
60
123
  acpReference(acp);
61
124
  mkdirSync(this.root, { recursive: true, mode: 0o700 });
62
125
  mkdirSync(this.directory(value.agentId), { mode: 0o700 });
63
- for (const dir of ["inbox", "completions", "reports", "permissions", "elicitations"]) privateDirectory(this.directory(value.agentId), dir);
64
- this.write(value.agentId, "descriptor.json", value, descriptor, true);
65
- this.write(value.agentId, "launch.json", run, launch, true);
66
- this.write(value.agentId, "acp-session.json", acp, acpReference, true);
126
+ const hasOptions = Object.keys(value.options).length > 0;
127
+ const record: AgentWorkerRecord = {
128
+ agentId: value.agentId,
129
+ release: value.release,
130
+ workspace: value.workspace,
131
+ parentSessionId: value.parentSessionId,
132
+ generation: run.generation,
133
+ sequence: run.sequence,
134
+ stop: run.stop,
135
+ startedAt: run.startedAt,
136
+ acp: {
137
+ backend: acp.backend,
138
+ command: acp.command,
139
+ args: acp.args,
140
+ ...(acp.env ? { env: acp.env } : {}),
141
+ ...(acp.permission ? { permission: acp.permission } : {}),
142
+ },
143
+ ...(hasOptions ? { options: value.options } : {}),
144
+ };
145
+ this.write(value.agentId, "worker.json", record, workerRecord, true);
67
146
  }
68
- list() {
147
+ worker(id: string): AgentWorkerRecord {
148
+ return this.read(id, "worker.json", workerRecord);
149
+ }
150
+ list(): AgentWorkerRecord[] {
69
151
  if (!existsSync(this.root)) return [];
70
152
  return readdirSync(this.root, { withFileTypes: true }).filter(e => e.isDirectory() && !e.name.startsWith(".")).flatMap(e => {
71
- const d = this.optional(e.name, "descriptor.json", descriptor); return d ? [d] : [];
153
+ const record = this.optional(e.name, "worker.json", workerRecord);
154
+ return record ? [record] : [];
72
155
  });
73
156
  }
74
- messages(id: string) {
75
- return readdirSync(join(this.directory(id), "inbox")).filter(n => n.endsWith(".json"))
157
+ messages(id: string): AgentMessage[] {
158
+ const dir = join(this.directory(id), "inbox");
159
+ if (!existsSync(dir)) return [];
160
+ return readdirSync(dir).filter(n => n.endsWith(".json"))
76
161
  .map(n => this.read(id, "inbox/" + n, message)).sort((a, b) => a.sequence - b.sequence);
77
162
  }
78
- completions(id: string) {
79
- return readdirSync(join(this.directory(id), "completions")).filter(n => n.endsWith(".json"))
80
- .map(n => this.read(id, "completions/" + n, cycle)).sort((a, b) => (a.finishedAt ?? 0) - (b.finishedAt ?? 0));
163
+ removeMessage(id: string, requestId: string) {
164
+ fileId(requestId);
165
+ const dir = join(this.directory(id), "inbox");
166
+ tryUnlink(join(dir, requestId + ".json"));
167
+ tryRmdir(dir);
168
+ }
169
+ output(id: string): AgentOutput | undefined {
170
+ return this.optional(id, "output.json", outputRecord);
81
171
  }
82
- /** Permission requests of a generation that have no reply yet. */
83
- pendingPermissions(id: string, generation: string) {
84
- const dir = join(this.directory(id), "permissions");
172
+ pendingInteractions(id: string, generation: string) {
173
+ const dir = join(this.directory(id), "interactions");
85
174
  if (!existsSync(dir)) return [];
86
175
  const names = readdirSync(dir);
87
176
  return names.filter(n => n.endsWith(".json") && !n.endsWith("-reply.json") && !names.includes(n.slice(0, -5) + "-reply.json"))
88
- .map(n => this.read(id, "permissions/" + n, acpPermission)).filter(item => item.generation === generation);
177
+ .map(n => this.read(id, "interactions/" + n, acpInteraction)).filter(item => item.generation === generation);
89
178
  }
90
- /** Elicitation requests (forms, choices, text inputs, URL confirmations) of a generation that have no reply yet. */
91
- pendingElicitations(id: string, generation: string) {
92
- const dir = join(this.directory(id), "elicitations");
93
- if (!existsSync(dir)) return [];
94
- const names = readdirSync(dir);
95
- return names.filter(n => n.endsWith(".json") && !n.endsWith("-reply.json") && !names.includes(n.slice(0, -5) + "-reply.json"))
96
- .map(n => this.read(id, "elicitations/" + n, acpElicitation)).filter(item => item.generation === generation);
179
+ removeInteraction(id: string, requestId: string) {
180
+ fileId(requestId);
181
+ const dir = join(this.directory(id), "interactions");
182
+ tryUnlink(join(dir, requestId + ".json"));
183
+ tryUnlink(join(dir, requestId + "-reply.json"));
184
+ tryRmdir(dir);
185
+ }
186
+ clearTransient(id: string) {
187
+ const base = this.directory(id);
188
+ if (!existsSync(base)) return;
189
+ rmSync(join(base, "inbox"), { recursive: true, force: true });
190
+ rmSync(join(base, "interactions"), { recursive: true, force: true });
191
+ tryUnlink(join(base, "acp-answer.json"));
192
+ }
193
+ removeWorker(id: string) {
194
+ rmSync(this.directory(id), { recursive: true, force: true });
195
+ tryRmdir(this.root);
97
196
  }
98
197
  ensureSubdirectory(id: string, name: string) {
99
198
  privateDirectory(this.directory(id), name);
100
199
  }
101
200
  reference(id: string): AgentSessionReference {
102
- return this.read(id, "acp-session.json", acpReference);
201
+ const w = this.worker(id);
202
+ const saved = this.optional(id, "state.json", status);
203
+ return {
204
+ host: "acp",
205
+ backend: w.acp.backend,
206
+ command: w.acp.command,
207
+ args: w.acp.args,
208
+ ...(w.acp.env ? { env: w.acp.env } : {}),
209
+ ...(w.acp.permission ? { permission: w.acp.permission } : {}),
210
+ sessionId: saved?.sessionId ?? null,
211
+ ...(saved?.capabilities ? { capabilities: saved.capabilities } : {}),
212
+ };
103
213
  }
104
214
  /** Short controller edits are serialized; an abandoned lock fails closed. */
105
215
  control<T>(work: () => T): T {
@@ -115,14 +225,17 @@ export class AgentFiles {
115
225
  }
116
226
  enqueue(id: string, text: string, delivery: AgentMessage["delivery"], files: string[] = [], requestId: string = randomUUID()) {
117
227
  fileId(requestId);
118
- const run = this.read(id, "launch.json", launch), previous = this.optional(id, "inbox/" + requestId + ".json", message);
228
+ const w = this.worker(id);
229
+ const previous = this.optional(id, "inbox/" + requestId + ".json", message);
119
230
  if (previous) {
120
- requireThat(previous.generation === run.generation && previous.message === text && previous.delivery === delivery && canonical(previous.files) === canonical(files), "请求 ID 已被不同输入使用。");
231
+ requireThat(previous.generation === w.generation && previous.message === text && previous.delivery === delivery && canonical(previous.files) === canonical(files), "请求 ID 已被不同输入使用。");
121
232
  return previous;
122
233
  }
123
- run.sequence++;
124
- this.write(id, "launch.json", run, launch);
125
- const value: AgentMessage = { requestId, generation: run.generation, sequence: run.sequence, message: text, delivery, files };
126
- this.write(id, "inbox/" + value.requestId + ".json", value, message, true); return value;
234
+ w.sequence++;
235
+ this.write(id, "worker.json", w, workerRecord);
236
+ this.ensureSubdirectory(id, "inbox");
237
+ const value: AgentMessage = { requestId, generation: w.generation, sequence: w.sequence, message: text, delivery, files };
238
+ this.write(id, "inbox/" + value.requestId + ".json", value, message, true);
239
+ return value;
127
240
  }
128
241
  }
@@ -50,24 +50,24 @@ export const historyResultSchema = Type.Object({
50
50
  });
51
51
 
52
52
  export const historyDescription =
53
- "查询当前工作区的历史问答与各维度摘要,host 可筛选来源(如 pi、dsh、claude、antigravity),dimension 可筛选 raw 原文或特定摘要维度(如 monthly 月度汇总、session 会话摘要)。" +
54
- "search 按不区分大小写的连续关键词匹配原文和摘要,省略 query 列出各会话或指定维度摘要;无匹配时换词。" +
55
- "read 按节点 id 展开正文与上下级(可从 monthly 下钻至 session 摘要,再下钻至 exchange 原文),nextOffset 续读正文,nextChildrenOffset 续读子节点。归档不含工具调用日志。";
53
+ "查询当前工作区跨宿主历史问答与摘要归档。" +
54
+ "search 按不区分大小写的连续关键词匹配原文与摘要,省略 query 列出各会话或指定维度摘要;" +
55
+ "read 按 id 读取正文与上下级(可从 monthly 汇总下钻至 session 摘要和 exchange 原文),用 nextOffset 与 nextChildrenOffset 分页续读。";
56
56
 
57
57
  export const historyParameters = Type.Object({
58
58
  action: Type.Union([Type.Literal("search"), Type.Literal("read")], {
59
- description: "search 搜索或列会话;read 读取指定节点正文与上下级。各动作仅读取自身字段,其余字段忽略。",
59
+ description: "search 搜索或列表;read 按 id 读取正文与上下级。仅读取对应动作参数,其余忽略。",
60
60
  }),
61
- host: nullable(Type.String({ pattern: historyIdentifierPattern.source, description: "可选来源筛选(如 pi、dsh、claude、antigravity)。" })),
62
- dimension: nullable(Type.String({ pattern: historyIdentifierPattern.source, description: "仅 search:可选范围筛选(raw 表示仅原文,或指定摘要维度如 monthly、session;省略查全部)。" })),
63
- query: nullable(Type.String({ description: "仅 search:不区分大小写的连续关键词;省略时列出各会话(或指定维度摘要)。" })),
64
- sessionId: nullable(Type.String({ description: "仅 search:限定在指定会话 ID 内搜索。" })),
61
+ host: nullable(Type.String({ pattern: historyIdentifierPattern.source, description: "来源宿主筛选(如 pi、dsh、claude、antigravity、codex、qoder);省略查全部。" })),
62
+ dimension: nullable(Type.String({ pattern: historyIdentifierPattern.source, description: "仅 search:raw 仅原文,或指定摘要维度(如 monthly、session);省略查全部。" })),
63
+ query: nullable(Type.String({ description: "仅 search:连续关键词(不区分大小写);省略列会话或指定维度摘要。" })),
64
+ sessionId: nullable(Type.String({ description: "仅 search:限定在指定原始会话 ID 内搜索。" })),
65
65
  from: nullable(Type.String({ description: "仅 search:ISO 日期或时间下界。" })),
66
66
  to: nullable(Type.String({ description: "仅 search:ISO 日期或时间上界。" })),
67
- id: nullable(Type.String({ description: "仅 read,必填:search 或上下级返回的节点 id。" })),
68
- offset: nullable(Type.Integer({ minimum: 0, description: "search:匹配结果偏移;read:正文字符偏移。默认 0。" })),
69
- limit: nullable(Type.Integer({ minimum: 1, maximum: historyPageLimits.read.max, description: `search:最多返回条数(默认 ${historyPageLimits.search.default},上限 ${historyPageLimits.search.max});read:正文字符数(默认 ${historyPageLimits.read.default})。` })),
70
- childrenOffset: nullable(Type.Integer({ minimum: 0, description: "仅 read:子节点列表偏移,默认 0;续读传上页 nextChildrenOffset。" })),
67
+ id: nullable(Type.String({ description: "仅 read,必填:节点 id(来自 search 或上下级)。" })),
68
+ offset: nullable(Type.Integer({ minimum: 0, description: "search 为结果偏移,read 为正文字符偏移;默认 0。" })),
69
+ limit: nullable(Type.Integer({ minimum: 1, maximum: historyPageLimits.read.max, description: `search:返回条数(1–${historyPageLimits.search.max},默认 ${historyPageLimits.search.default});read:正文字符数(默认 ${historyPageLimits.read.default})。` })),
70
+ childrenOffset: nullable(Type.Integer({ minimum: 0, description: "仅 read:子节点偏移,默认 0;续读传上次 nextChildrenOffset。" })),
71
71
  }, { additionalProperties: false });
72
72
 
73
73
  export type HistoryInput =
@@ -1,15 +0,0 @@
1
- /** Each host owns its file, including when two hosts activate the same canonical workspace concurrently. */
2
- export declare function bindWorkspacePlugin(root: string, host: string, binding?: string): void;
3
- export interface WorkspacePluginBinding {
4
- version: string;
5
- bindingPath?: string;
6
- }
7
- export interface WorkspacePluginDrift {
8
- outOfSync: boolean;
9
- highest?: string;
10
- lowest?: string;
11
- versions: Record<string, string>;
12
- }
13
- export declare function workspacePluginBindings(root: string): Record<string, WorkspacePluginBinding>;
14
- export declare function workspacePluginReleases(root: string): Record<string, string>;
15
- export declare function workspacePluginDrift(releases: Record<string, string>): WorkspacePluginDrift;
@@ -1,126 +0,0 @@
1
- import { createHash } from "node:crypto";
2
- import { mkdirSync, readdirSync, readFileSync } from "node:fs";
3
- import { homedir } from "node:os";
4
- import { isAbsolute, join, resolve } from "node:path";
5
- import { object } from "../../common/json.js";
6
- import { resourceRoot } from "../../common/paths.js";
7
- import { requireThat } from "../../common/validation.js";
8
- import { releaseLine } from "../../common/version.js";
9
- import { readJsonFile, writeJsonFile } from "../../storage/atomic-json.js";
10
- function compareSemver(a, b) {
11
- const parse = (value) => {
12
- const match = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z.-]+))?/.exec(value);
13
- return match
14
- ? { major: Number(match[1]), minor: Number(match[2]), patch: Number(match[3]), pre: match[4] ?? "" }
15
- : undefined;
16
- };
17
- const left = parse(a);
18
- const right = parse(b);
19
- if (!left || !right)
20
- return a.localeCompare(b);
21
- if (left.major !== right.major)
22
- return left.major - right.major;
23
- if (left.minor !== right.minor)
24
- return left.minor - right.minor;
25
- if (left.patch !== right.patch)
26
- return left.patch - right.patch;
27
- if (left.pre === right.pre)
28
- return 0;
29
- if (!left.pre)
30
- return 1;
31
- if (!right.pre)
32
- return -1;
33
- return left.pre.localeCompare(right.pre);
34
- }
35
- function bindingDirectory(root) {
36
- const key = createHash("sha256").update(root).digest("hex");
37
- return join(homedir(), ".pbot", "workspaces", key);
38
- }
39
- // Full plugins own a local manifest. Thin clients can supply the release stamped into their cached hooks.
40
- function pluginBinding(value) {
41
- requireThat(typeof value === "string", "无效的插件安装记录。");
42
- if (!isAbsolute(value))
43
- releaseLine(value);
44
- return value;
45
- }
46
- function permissionDenied(error) {
47
- return ["EACCES", "EPERM", "EROFS"].includes(error.code ?? "");
48
- }
49
- /** Each host owns its file, including when two hosts activate the same canonical workspace concurrently. */
50
- export function bindWorkspacePlugin(root, host, binding = resolve(resourceRoot, "../package.json")) {
51
- const directory = bindingDirectory(root);
52
- const file = encodeURIComponent(host) + ".json";
53
- try {
54
- if (readJsonFile(directory, file, pluginBinding) === binding)
55
- return;
56
- }
57
- catch (error) {
58
- if (permissionDenied(error))
59
- return;
60
- if (error.code !== "ENOENT")
61
- throw error;
62
- }
63
- try {
64
- mkdirSync(directory, { recursive: true, mode: 0o700 });
65
- writeJsonFile(directory, file, binding, pluginBinding);
66
- }
67
- catch (error) {
68
- if (!permissionDenied(error))
69
- throw error;
70
- }
71
- }
72
- export function workspacePluginBindings(root) {
73
- const directory = bindingDirectory(root);
74
- let files;
75
- try {
76
- files = readdirSync(directory);
77
- }
78
- catch (error) {
79
- if (error.code === "ENOENT" || permissionDenied(error))
80
- return {};
81
- throw error;
82
- }
83
- const bindings = Object.create(null);
84
- for (const file of files.filter(name => name.endsWith(".json"))) {
85
- const host = decodeURIComponent(file.slice(0, -5));
86
- try {
87
- const binding = readJsonFile(directory, file, pluginBinding);
88
- if (!isAbsolute(binding)) {
89
- bindings[host] = { version: binding };
90
- continue;
91
- }
92
- const manifest = JSON.parse(readFileSync(binding, "utf8"));
93
- requireThat(object(manifest) && typeof manifest.version === "string", "无效的插件安装清单:" + binding);
94
- releaseLine(manifest.version);
95
- bindings[host] = { version: manifest.version, bindingPath: binding };
96
- }
97
- catch (error) {
98
- if (error.code !== "ENOENT" && !permissionDenied(error))
99
- throw error;
100
- }
101
- }
102
- return bindings;
103
- }
104
- export function workspacePluginReleases(root) {
105
- const bindings = workspacePluginBindings(root);
106
- const releases = Object.create(null);
107
- for (const [host, entry] of Object.entries(bindings))
108
- releases[host] = entry.version;
109
- return releases;
110
- }
111
- export function workspacePluginDrift(releases) {
112
- let highest;
113
- let lowest;
114
- for (const version of Object.values(releases)) {
115
- if (!highest || compareSemver(version, highest) > 0)
116
- highest = version;
117
- if (!lowest || compareSemver(version, lowest) < 0)
118
- lowest = version;
119
- }
120
- return {
121
- outOfSync: Boolean(highest && lowest && highest !== lowest),
122
- ...(highest ? { highest } : {}),
123
- ...(lowest ? { lowest } : {}),
124
- versions: releases,
125
- };
126
- }
@@ -1,22 +0,0 @@
1
- # 指标与回测
2
-
3
- ## TA-Lib
4
-
5
- 通过 `import talib` 使用原生函数。用 `talib.get_function_groups()`、`talib.get_functions()` 和 `help(talib.函数名)` 核对能力、参数与返回值。
6
-
7
- 计算前核对时间顺序、复权、缺失数据、单位与样本长度,多输入 OHLCV 数组必须按同一时间轴对齐。保留影响结果解释的原生规则:
8
-
9
- - `MACD` 返回 `(macd, signal, hist)`,其中 `hist = macd - signal`,不乘 2。
10
- - `BBANDS` 返回 `(upper, middle, lower)`,默认使用 SMA 中轨和两倍总体标准差。
11
- - 暖机期为 NaN,例如 SMA(20) 前 19 根、RSI(14) 前 14 根不可用。EMA、RSI 的种子和平滑规则不等同于直接 pandas ewm;持平序列的 RSI 暖机后为 0,不是 50。
12
- - NaN 输入可能影响后续输出,不擅自填零或把暖机值当成信号。严格 JSON 中将非有限值转为 null。
13
-
14
- 按需记录参数、库版本和数据来源,便于复现。
15
-
16
- ## backtesting
17
-
18
- 用 `from backtesting import Backtest, Strategy`,通过 Strategy 的 `init()`、`next()` 定义策略,再运行 `Backtest(data, 策略类, ...).run()`。数据为按时间升序的 OHLC DataFrame,可带 Volume;参数以当前版本 API 为准。TA-Lib 指标可用 `self.I(talib.SMA, self.data.Close, 20)` 注册。
19
-
20
- 显式确定资金、手续费、价差、成交时点与仓位假设。原生 Backtest 不自动提供 A 股 T+1、整手买入、涨跌停或交易日历约束;任务需要时明确建模和验证,无法表达时说明限制,不把默认值当作市场规则。
21
-
22
- 按研究问题选择样本内外划分及暖机方法,避免未来数据泄漏。交付时说明参数、版本与限制,按需保留统计和交易记录;导出时处理 Series、DataFrame、时间和非有限值。
@@ -1,16 +0,0 @@
1
- # Python 计算
2
-
3
- ## 运行方式
4
-
5
- 通过当前宿主的原生 Bash,沿用项目声明的解释器、依赖、工作目录、入口和输出位置。尚无约定时,先检查可用解释器和所需库再执行。
6
-
7
- 包内相对导入在项目根目录用 `python -m 包.模块`,不靠改 `sys.path` 绕过导入错误。交付时核对结果并给出实际产物路径。
8
-
9
- 解释器或依赖异常时,检查实际路径、导入错误与项目配置,继续不受影响的工作;专家需要主会话协助时交回具体阻碍和所需条件。
10
-
11
- ## 常见约束
12
-
13
- - 无图形界面绘图:导入 `pyplot`/`seaborn` 前设置 Matplotlib `Agg` 后端,保存文件后关闭 figure。
14
- - 表格与二进制文件(Excel/Parquet/Feather):核对读写后的字段类型、时间单位与时区;Excel 不直接保存带时区的 `datetime`,转换前先确定目标时区。
15
- - 统计与建模:记录数据口径和假设;时间序列按时间切分,预处理只在训练集拟合,避免未来数据泄漏。
16
- - 技术指标或回测:另读同目录 [compute-finance.md](compute-finance.md),显式确定数据口径和交易假设。