@ftai/dsh-plugin-acn 0.0.0-stage → 0.1.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 (272) hide show
  1. package/CHANGELOG.md +79 -0
  2. package/LICENSE-APACHE +201 -0
  3. package/LICENSE-MIT +19 -0
  4. package/README.md +55 -2
  5. package/README_EN.md +56 -0
  6. package/cordis.patch.yml +29 -0
  7. package/docs/acn-compat.md +33 -0
  8. package/docs/architecture.md +129 -0
  9. package/docs/assets/activity-view.png +0 -0
  10. package/docs/assets/install-package.png +0 -0
  11. package/docs/assets/knowledge-model.png +0 -0
  12. package/docs/assets/knowledge-view.png +0 -0
  13. package/docs/assets/personal-configuration.png +0 -0
  14. package/docs/assets/session-upstream.png +0 -0
  15. package/docs/assets/upstream-settings.png +0 -0
  16. package/docs/assets/upstream-team.png +0 -0
  17. package/docs/command-reference.md +201 -0
  18. package/docs/config.md +243 -0
  19. package/docs/development.md +197 -0
  20. package/docs/dsh-compat.md +59 -0
  21. package/docs/index.md +16 -0
  22. package/docs/outstanding-issues.md +61 -0
  23. package/docs/user_guide.md +183 -0
  24. package/icon.svg +8 -0
  25. package/lib/activities-LRoCwpzw.js +327 -0
  26. package/lib/alignment-CwH0nBf-.js +80 -0
  27. package/lib/auth-DFI9yowX.js +71 -0
  28. package/lib/checkpoint-Bmt8q2vB.js +188 -0
  29. package/lib/checkpoint-RgcdqifN.js +318 -0
  30. package/lib/claim-DOAGe8ZK.js +168 -0
  31. package/lib/client.js +18420 -0
  32. package/lib/deliver-BuYQaPRW.js +168 -0
  33. package/lib/delivery-status-D5m3178s.js +87 -0
  34. package/lib/dispute-CtrInHzf.js +20 -0
  35. package/lib/disputes-ByEmi43j.js +51 -0
  36. package/lib/dream-agent-DBszTHXY.js +249 -0
  37. package/lib/dream.js +4 -0
  38. package/lib/effects-DJ4Xa7Tx.js +268 -0
  39. package/lib/errors-Bp_2WXBr.js +19 -0
  40. package/lib/errors-pPumDOGD.js +18 -0
  41. package/lib/evidence-store-DLFvSuNa.js +1030 -0
  42. package/lib/formation-D4LO31WW.js +187 -0
  43. package/lib/formation-replay-CEOy5FkI.js +87 -0
  44. package/lib/formation.js +9 -0
  45. package/lib/inbox-2CugZZQm.js +188 -0
  46. package/lib/inbox-DFtE2fOp.js +71 -0
  47. package/lib/inbox.js +3 -0
  48. package/lib/index.js +1377 -0
  49. package/lib/lock-DbmlKOoX.js +332 -0
  50. package/lib/pages-BEWC3kmZ.js +51 -0
  51. package/lib/paths-CDYV5UH9.js +154 -0
  52. package/lib/plugins/commands.js +590 -0
  53. package/lib/plugins/dream.js +66 -0
  54. package/lib/plugins/formation.js +66 -0
  55. package/lib/plugins/inbox.js +53 -0
  56. package/lib/plugins/knowledge.js +1111 -0
  57. package/lib/plugins/prompt.js +102 -0
  58. package/lib/plugins/tools.js +280 -0
  59. package/lib/policy-DnyHh7Bl.js +16 -0
  60. package/lib/prepare-DwHZudbA.js +134 -0
  61. package/lib/presentation-BlLjgDjP.js +47 -0
  62. package/lib/protocol.js +8 -0
  63. package/lib/queue-M917xHPS.js +153 -0
  64. package/lib/regular-file-BFpqMoub.js +136 -0
  65. package/lib/render-CJaVcKKC.js +48 -0
  66. package/lib/reported-sets-DdEKdeVb.js +122 -0
  67. package/lib/revision-DeoL_IUN.js +11 -0
  68. package/lib/scheduler-MaUEbXr3.js +64 -0
  69. package/lib/service-B7AI5jds.js +344 -0
  70. package/lib/service-BcHV5mc7.js +305 -0
  71. package/lib/service-CNjqHCBR.js +505 -0
  72. package/lib/service-C_i3BzdP.js +1106 -0
  73. package/lib/session-binding-BQrOGhZI.js +151 -0
  74. package/lib/session-state-CYqUhC95.js +38 -0
  75. package/lib/sessions-C2xuPFQv.js +218 -0
  76. package/lib/store.js +16 -0
  77. package/lib/structured-lyQCK-Zk.js +452 -0
  78. package/lib/subagent-yJsFxHmH.js +9 -0
  79. package/lib/team-credentials-C9xHrPIi.js +579 -0
  80. package/lib/team.js +3 -0
  81. package/lib/tool-schema-3b4gH1zf.js +28 -0
  82. package/lib/trace-Da0RnL5R.js +14 -0
  83. package/lib/traces-CHBo8y1z.js +11 -0
  84. package/lib/types/activities.d.ts +71 -0
  85. package/lib/types/advanced-configuration.d.ts +11 -0
  86. package/lib/types/client/ActivityDetails.d.ts +18 -0
  87. package/lib/types/client/AdvancedPreferences.d.ts +8 -0
  88. package/lib/types/client/BlankCommandFeedback.d.ts +10 -0
  89. package/lib/types/client/ComposerMenu.d.ts +24 -0
  90. package/lib/types/client/ConfigurationPage.d.ts +11 -0
  91. package/lib/types/client/ConnectionCheckDialog.d.ts +12 -0
  92. package/lib/types/client/KnowledgeDetail.d.ts +6 -0
  93. package/lib/types/client/KnowledgeDirectoryField.d.ts +11 -0
  94. package/lib/types/client/KnowledgePanel.d.ts +12 -0
  95. package/lib/types/client/KnowledgeReference.d.ts +13 -0
  96. package/lib/types/client/SessionToolbar.d.ts +16 -0
  97. package/lib/types/client/SettingsToast.d.ts +3 -0
  98. package/lib/types/client/SourceSessionLink.d.ts +15 -0
  99. package/lib/types/client/TeamCredentialField.d.ts +16 -0
  100. package/lib/types/client/TeamSettingsPage.d.ts +30 -0
  101. package/lib/types/client/configuration-errors.d.ts +2 -0
  102. package/lib/types/client/interaction-styles.d.ts +1 -0
  103. package/lib/types/client/interaction-ui.d.ts +30 -0
  104. package/lib/types/client/knowledge-labels.d.ts +5 -0
  105. package/lib/types/client/locales.d.ts +51 -0
  106. package/lib/types/commands/claim.d.ts +4 -0
  107. package/lib/types/commands/import.d.ts +10 -0
  108. package/lib/types/commands/status.d.ts +5 -0
  109. package/lib/types/config.d.ts +122 -0
  110. package/lib/types/configuration.d.ts +259 -0
  111. package/lib/types/credentials.d.ts +65 -0
  112. package/lib/types/dream/audit.d.ts +4 -0
  113. package/lib/types/dream/autonomous.d.ts +185 -0
  114. package/lib/types/dream/candidates.d.ts +34 -0
  115. package/lib/types/dream/checkpoint.d.ts +35 -0
  116. package/lib/types/dream/evidence-files.d.ts +10 -0
  117. package/lib/types/dream/evidence-store.d.ts +103 -0
  118. package/lib/types/dream/evidence.d.ts +30 -0
  119. package/lib/types/dream/index.d.ts +10 -0
  120. package/lib/types/dream/plan.d.ts +18 -0
  121. package/lib/types/dream/preservation.d.ts +6 -0
  122. package/lib/types/dream/review.d.ts +10 -0
  123. package/lib/types/dream/schema.d.ts +1475 -0
  124. package/lib/types/dream/service.d.ts +56 -0
  125. package/lib/types/dream/sync.d.ts +147 -0
  126. package/lib/types/dsh-adapter/configuration-remote.d.ts +1 -0
  127. package/lib/types/dsh-adapter/configuration-service.d.ts +28 -0
  128. package/lib/types/dsh-adapter/dream-agent.d.ts +4 -0
  129. package/lib/types/dsh-adapter/dream-evidence.d.ts +3 -0
  130. package/lib/types/dsh-adapter/formation-archive.d.ts +3 -0
  131. package/lib/types/dsh-adapter/formation-replay.d.ts +5 -0
  132. package/lib/types/dsh-adapter/formation.d.ts +4 -0
  133. package/lib/types/dsh-adapter/interaction-service.d.ts +13 -0
  134. package/lib/types/dsh-adapter/knowledge-paths.d.ts +3 -0
  135. package/lib/types/dsh-adapter/llm.d.ts +10 -0
  136. package/lib/types/dsh-adapter/project-upstream.d.ts +4 -0
  137. package/lib/types/dsh-adapter/runtime-pool.d.ts +110 -0
  138. package/lib/types/dsh-adapter/session-binding.d.ts +43 -0
  139. package/lib/types/dsh-adapter/source-sessions.d.ts +3 -0
  140. package/lib/types/dsh-adapter/team-credentials.d.ts +5 -0
  141. package/lib/types/dsh-adapter/tool-schema.d.ts +4 -0
  142. package/lib/types/dsh-adapter/tools.d.ts +5 -0
  143. package/lib/types/formation/capture.d.ts +19 -0
  144. package/lib/types/formation/checkpoint.d.ts +136 -0
  145. package/lib/types/formation/index.d.ts +10 -0
  146. package/lib/types/formation/prepare.d.ts +16 -0
  147. package/lib/types/formation/schema.d.ts +45 -0
  148. package/lib/types/formation/service.d.ts +58 -0
  149. package/lib/types/formation/transcript.d.ts +75 -0
  150. package/lib/types/formation/triggers.d.ts +3 -0
  151. package/lib/types/inbox/cascade.d.ts +3 -0
  152. package/lib/types/inbox/effects.d.ts +275 -0
  153. package/lib/types/inbox/index.d.ts +5 -0
  154. package/lib/types/inbox/prepare.d.ts +380 -0
  155. package/lib/types/inbox/service.d.ts +26 -0
  156. package/lib/types/inbox/sync.d.ts +9 -0
  157. package/lib/types/index.d.ts +1 -0
  158. package/lib/types/interaction-messages.d.ts +69 -0
  159. package/lib/types/interaction.d.ts +550 -0
  160. package/lib/types/knowledge/alignment.d.ts +49 -0
  161. package/lib/types/knowledge/catalog.d.ts +5 -0
  162. package/lib/types/knowledge/changes.d.ts +32 -0
  163. package/lib/types/knowledge/disputes.d.ts +3 -0
  164. package/lib/types/knowledge/errors.d.ts +9 -0
  165. package/lib/types/knowledge/ordering.d.ts +7 -0
  166. package/lib/types/knowledge/pages.d.ts +7 -0
  167. package/lib/types/knowledge/safety.d.ts +2 -0
  168. package/lib/types/knowledge/service.d.ts +52 -0
  169. package/lib/types/knowledge/sessions.d.ts +59 -0
  170. package/lib/types/knowledge/sources.d.ts +2 -0
  171. package/lib/types/knowledge/timestamps.d.ts +1 -0
  172. package/lib/types/knowledge/types.d.ts +84 -0
  173. package/lib/types/llm/response.d.ts +1 -0
  174. package/lib/types/llm/structured.d.ts +27 -0
  175. package/lib/types/local-name.d.ts +1 -0
  176. package/lib/types/plugins/client.d.ts +9 -0
  177. package/lib/types/plugins/commands.d.ts +11 -0
  178. package/lib/types/plugins/configuration.d.ts +8 -0
  179. package/lib/types/plugins/dream.d.ts +16 -0
  180. package/lib/types/plugins/formation.d.ts +16 -0
  181. package/lib/types/plugins/inbox.d.ts +16 -0
  182. package/lib/types/plugins/knowledge.d.ts +19 -0
  183. package/lib/types/plugins/prompt.d.ts +17 -0
  184. package/lib/types/plugins/tools.d.ts +10 -0
  185. package/lib/types/prompt/render.d.ts +2 -0
  186. package/lib/types/protocol/arbitration.d.ts +154 -0
  187. package/lib/types/protocol/auth.d.ts +31 -0
  188. package/lib/types/protocol/claim.d.ts +36 -0
  189. package/lib/types/protocol/dispute.d.ts +17 -0
  190. package/lib/types/protocol/ids.d.ts +20 -0
  191. package/lib/types/protocol/inbox.d.ts +141 -0
  192. package/lib/types/protocol/index.d.ts +11 -0
  193. package/lib/types/protocol/policy.d.ts +21 -0
  194. package/lib/types/protocol/revision.d.ts +2 -0
  195. package/lib/types/protocol/router.d.ts +114 -0
  196. package/lib/types/protocol/schema.d.ts +4 -0
  197. package/lib/types/protocol/time.d.ts +5 -0
  198. package/lib/types/protocol/trace.d.ts +11 -0
  199. package/lib/types/store/atomic.d.ts +8 -0
  200. package/lib/types/store/claim-edits.d.ts +33 -0
  201. package/lib/types/store/claims.d.ts +7 -0
  202. package/lib/types/store/context.d.ts +13 -0
  203. package/lib/types/store/delivery-status.d.ts +47 -0
  204. package/lib/types/store/disputes.d.ts +6 -0
  205. package/lib/types/store/entities.d.ts +16 -0
  206. package/lib/types/store/errors.d.ts +8 -0
  207. package/lib/types/store/inbox.d.ts +27 -0
  208. package/lib/types/store/index.d.ts +16 -0
  209. package/lib/types/store/jobs.d.ts +5 -0
  210. package/lib/types/store/lock.d.ts +11 -0
  211. package/lib/types/store/path-names.d.ts +2 -0
  212. package/lib/types/store/paths.d.ts +45 -0
  213. package/lib/types/store/presentation.d.ts +13 -0
  214. package/lib/types/store/regular-file.d.ts +2 -0
  215. package/lib/types/store/rejected-disputes.d.ts +18 -0
  216. package/lib/types/store/rename.d.ts +1 -0
  217. package/lib/types/store/reported-sets.d.ts +24 -0
  218. package/lib/types/store/session-state.d.ts +17 -0
  219. package/lib/types/store/traces.d.ts +6 -0
  220. package/lib/types/store/uploads.d.ts +312 -0
  221. package/lib/types/store/yaml.d.ts +8 -0
  222. package/lib/types/team/errors.d.ts +10 -0
  223. package/lib/types/team/http.d.ts +20 -0
  224. package/lib/types/team/index.d.ts +5 -0
  225. package/lib/types/team/maintainer-client.d.ts +18 -0
  226. package/lib/types/team/router-client.d.ts +14 -0
  227. package/lib/types/team/service.d.ts +28 -0
  228. package/lib/types/team/system-proxy.d.ts +9 -0
  229. package/lib/types/tools/claim.d.ts +63 -0
  230. package/lib/types/tools/consult-router.d.ts +11 -0
  231. package/lib/types/tools/subagent.d.ts +8 -0
  232. package/lib/types/tools/validation.d.ts +2 -0
  233. package/lib/types/upload/consolidation.d.ts +63 -0
  234. package/lib/types/upload/deliver.d.ts +15 -0
  235. package/lib/types/upload/index.d.ts +2 -0
  236. package/lib/types/upload/scheduler.d.ts +19 -0
  237. package/lib/types/upstreams.d.ts +5 -0
  238. package/lib/types/worker/queue.d.ts +17 -0
  239. package/lib/types/worker/types.d.ts +51 -0
  240. package/lib/types/worker/worker.d.ts +39 -0
  241. package/lib/types-C2ST2Zdg.js +17 -0
  242. package/lib/upload.js +3 -0
  243. package/lib/uploads-BCA2p92b.js +210 -0
  244. package/lib/worker-BBTYP2RE.js +185 -0
  245. package/locale/commands/en.json +1 -0
  246. package/locale/commands/zh.json +1 -0
  247. package/locale/dream/en.json +1 -0
  248. package/locale/dream/zh.json +1 -0
  249. package/locale/en.json +7 -0
  250. package/locale/formation/en.json +1 -0
  251. package/locale/formation/zh.json +1 -0
  252. package/locale/inbox/en.json +1 -0
  253. package/locale/inbox/zh.json +1 -0
  254. package/locale/knowledge/en.json +1 -0
  255. package/locale/knowledge/zh.json +1 -0
  256. package/locale/prompt/en.json +1 -0
  257. package/locale/prompt/zh.json +1 -0
  258. package/locale/tools/en.json +1 -0
  259. package/locale/tools/zh.json +1 -0
  260. package/locale/zh.json +7 -0
  261. package/package.json +262 -3
  262. package/prompts/claim_dream.j2 +77 -0
  263. package/prompts/claim_dream_legacy.j2 +63 -0
  264. package/prompts/dream_compaction.j2 +11 -0
  265. package/prompts/dream_evidence.j2 +5 -0
  266. package/prompts/inbox_claim_attribute_update_internalize.j2 +112 -0
  267. package/prompts/inbox_policy_update_internalize.j2 +87 -0
  268. package/prompts/knowledge.j2 +38 -0
  269. package/prompts/session_recap.j2 +113 -0
  270. package/prompts/what_is_claim.j2 +51 -0
  271. package/prompts/what_is_dispute.j2 +21 -0
  272. package/prompts/what_is_policy.j2 +22 -0
@@ -0,0 +1,268 @@
1
+ import { d as InboxIdSchema, f as PolicyIdSchema, m as SourceIdSchema, t as ClaimSchema, v as deriveTraceId, x as nowSeconds } from "./claim-DOAGe8ZK.js";
2
+ import { C as StoreError, c as readYamlOr, l as writeYamlAtomic, n as withFileLock, s as readYaml, w as hasCode } from "./lock-DbmlKOoX.js";
3
+ import { a as analysisIsStale, c as observeApplied, i as STALE_PLAN_WARNING, l as sameClaim, o as claimChanges, s as ensureKnowledgeReady, t as AnalysisChanged } from "./alignment-CwH0nBf-.js";
4
+ import { i as ClaimStore } from "./reported-sets-DdEKdeVb.js";
5
+ import { t as TraceSchema } from "./trace-Da0RnL5R.js";
6
+ import { t as TraceStore } from "./traces-CHBo8y1z.js";
7
+ import { t as DisputeSchema } from "./dispute-CtrInHzf.js";
8
+ import { r as UploadStore } from "./uploads-BCA2p92b.js";
9
+ import { t as DreamStore } from "./checkpoint-RgcdqifN.js";
10
+ import { i as inboxEventAt, n as InboxMessageSchema } from "./inbox-DFtE2fOp.js";
11
+ import { n as ClaimEditStore, t as DisputeStore } from "./disputes-ByEmi43j.js";
12
+ import { t as InboxStore } from "./inbox-2CugZZQm.js";
13
+ import { n as FormationStore, r as newDisputes } from "./checkpoint-Bmt8q2vB.js";
14
+ import { z } from "zod";
15
+ import { createHash } from "node:crypto";
16
+ import fs from "node:fs/promises";
17
+ import { join } from "node:path";
18
+ //#region src/inbox/effects.ts
19
+ const HashSchema = z.string().regex(/^[0-9a-f]{64}$/);
20
+ const MemberSchema = z.strictObject({
21
+ id: InboxIdSchema,
22
+ hash: HashSchema,
23
+ message: InboxMessageSchema
24
+ });
25
+ const InboxEffectSchema = z.strictObject({
26
+ schemaVersion: z.literal(1),
27
+ type: z.literal("effect"),
28
+ canonicalId: InboxIdSchema,
29
+ batchHash: HashSchema,
30
+ members: z.array(MemberSchema).min(1),
31
+ status: z.enum(["prepared", "applied"]),
32
+ analysisClaims: z.array(ClaimSchema).nullable(),
33
+ preparedClaims: z.array(ClaimSchema),
34
+ appliedClaims: z.array(ClaimSchema),
35
+ teamAuthorized: z.boolean().optional(),
36
+ syncClaims: z.array(ClaimSchema).optional(),
37
+ disputes: z.array(DisputeSchema),
38
+ trace: TraceSchema.nullable(),
39
+ warnings: z.array(z.string()),
40
+ retiredPolicies: z.array(PolicyIdSchema)
41
+ });
42
+ const RefSchema = z.strictObject({
43
+ schemaVersion: z.literal(1),
44
+ type: z.literal("ref"),
45
+ id: InboxIdSchema,
46
+ canonicalId: InboxIdSchema,
47
+ batchHash: HashSchema,
48
+ messageHash: HashSchema
49
+ });
50
+ const JournalSchema = z.discriminatedUnion("type", [InboxEffectSchema, RefSchema]);
51
+ function canonicalJson(value) {
52
+ return JSON.stringify(value, (_key, item) => typeof item === "object" && item !== null && !Array.isArray(item) ? Object.fromEntries(Object.entries(item).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) : item);
53
+ }
54
+ const hash = (value) => createHash("sha256").update(canonicalJson(value)).digest("hex");
55
+ const inboxMessageHash = (message) => hash(JSON.parse(JSON.stringify({
56
+ ...message,
57
+ handled_at: void 0
58
+ })));
59
+ const batchHash = (members) => hash(members.map(({ id, hash }) => ({
60
+ id,
61
+ hash
62
+ })));
63
+ function integrity(condition) {
64
+ if (!condition) throw new StoreError("conflict", "Inbox effect journal integrity check failed.");
65
+ }
66
+ var InboxEffectStore = class {
67
+ context;
68
+ constructor(context) {
69
+ this.context = context;
70
+ }
71
+ async journals() {
72
+ let names;
73
+ try {
74
+ names = (await fs.readdir(join(this.context.paths.inbox, "effects"))).filter((name) => name.endsWith(".yaml") && !name.includes(".tmp."));
75
+ } catch (error) {
76
+ if (hasCode(error, "ENOENT")) return /* @__PURE__ */ new Map();
77
+ throw error;
78
+ }
79
+ const entries = await Promise.all(names.sort().map(async (name) => {
80
+ const journal = await readYaml(join(this.context.paths.inbox, "effects", name), JournalSchema);
81
+ integrity(name === `${journal.type === "effect" ? journal.canonicalId : journal.id}.yaml`);
82
+ return [name.slice(0, -5), journal];
83
+ }));
84
+ const result = new Map(entries);
85
+ for (const [id, journal] of result) if (journal.type === "ref") {
86
+ const canonical = result.get(journal.canonicalId);
87
+ integrity(canonical?.type === "effect" && canonical.canonicalId !== id);
88
+ const member = canonical.members.find((member) => member.id === id);
89
+ integrity(member !== void 0 && member.hash === journal.messageHash && canonical.batchHash === journal.batchHash);
90
+ } else {
91
+ integrity(journal.members[0]?.id === journal.canonicalId && new Set(journal.members.map((member) => member.id)).size === journal.members.length && batchHash(journal.members) === journal.batchHash);
92
+ integrity(journal.members.every((member) => member.id === member.message.id && member.hash === inboxMessageHash(member.message)));
93
+ integrity([...journal.preparedClaims, ...journal.appliedClaims].every((claim) => claim.holder === this.context.agentId));
94
+ integrity((journal.syncClaims ?? []).every((claim) => claim.holder === this.context.agentId && claim.status === "deprecated" && journal.retiredPolicies.some((id) => claim.source_claim_ids.includes(id))));
95
+ integrity(journal.trace === null || journal.trace.agent === this.context.agentId);
96
+ integrity(journal.disputes.every((dispute) => dispute.reporter_agent_id === this.context.agentId));
97
+ for (const member of journal.members.slice(1)) {
98
+ const ref = result.get(member.id);
99
+ integrity(ref === void 0 || ref.type === "ref" && ref.canonicalId === id && ref.batchHash === journal.batchHash && ref.messageHash === member.hash);
100
+ }
101
+ }
102
+ return result;
103
+ }
104
+ write(effect) {
105
+ return writeYamlAtomic(this.context.paths.inboxEffect(effect.canonicalId), effect, InboxEffectSchema, this.context.logger);
106
+ }
107
+ async references(effect) {
108
+ for (const member of effect.members.slice(1)) {
109
+ const ref = {
110
+ schemaVersion: 1,
111
+ type: "ref",
112
+ id: member.id,
113
+ canonicalId: effect.canonicalId,
114
+ batchHash: effect.batchHash,
115
+ messageHash: member.hash
116
+ };
117
+ const existing = await readYamlOr(this.context.paths.inboxEffect(member.id), JournalSchema, () => null);
118
+ if (existing === null) await writeYamlAtomic(this.context.paths.inboxEffect(member.id), ref, RefSchema, this.context.logger);
119
+ else integrity(canonicalJson(existing) === canonicalJson(ref));
120
+ }
121
+ }
122
+ async recover() {
123
+ if ((await this.journals()).size === 0) return;
124
+ await withFileLock(this.context.paths.knowledgeLock, () => this.recoverLocked(), { timeoutMs: this.context.lockTimeoutMs });
125
+ }
126
+ async recoverLocked() {
127
+ await new DreamStore(this.context).recoverLocked();
128
+ const effects = [...(await this.journals()).values()].filter((journal) => journal.type === "effect").sort((a, b) => (a.members[0] === void 0 ? "" : inboxEventAt(a.members[0].message)).localeCompare(b.members[0] === void 0 ? "" : inboxEventAt(b.members[0].message)));
129
+ for (const effect of effects) for (const member of effect.members) await new InboxStore(this.context).validateSnapshotLocked(member.message);
130
+ for (const effect of effects) {
131
+ await this.references(effect);
132
+ if (effect.status === "prepared") await this.applyLocked(effect);
133
+ else for (const member of effect.members) await new InboxStore(this.context).completeSnapshotLocked(member.message);
134
+ }
135
+ }
136
+ async commit(messages, analysisClaims, prepared, control) {
137
+ return withFileLock(this.context.paths.knowledgeLock, async () => {
138
+ await new DreamStore(this.context).recoverLocked();
139
+ await ensureKnowledgeReady(this.context.paths.root);
140
+ await new ClaimEditStore(this.context).recoverLocked();
141
+ await new FormationStore(this.context).recoverLocked();
142
+ await this.recoverLocked();
143
+ const first = messages[0];
144
+ integrity(first !== void 0);
145
+ for (const message of messages) await new InboxStore(this.context).validateSnapshotLocked(message);
146
+ const existing = await this.journals();
147
+ if (messages.some((message) => existing.has(message.id))) {
148
+ integrity(messages.every((message) => {
149
+ const journal = existing.get(message.id);
150
+ const canonical = journal?.type === "effect" ? journal : journal === void 0 ? void 0 : existing.get(journal.canonicalId);
151
+ return canonical?.type === "effect" && canonical.members.some((member) => member.id === message.id && member.hash === inboxMessageHash(message));
152
+ }));
153
+ return {
154
+ applied: 0,
155
+ warnings: []
156
+ };
157
+ }
158
+ if (claimChanges(analysisClaims, prepared.claims, [], await new ClaimStore(this.context).list()).length > 0) throw new AnalysisChanged();
159
+ const members = messages.map((message) => ({
160
+ id: message.id,
161
+ hash: inboxMessageHash(message),
162
+ message
163
+ }));
164
+ const traceInputs = [.../* @__PURE__ */ new Set([
165
+ ...messages.map((message) => message.policy.id),
166
+ ...messages.flatMap((message) => message.message_type === "claim_attribute_update" ? message.arbitration_resolution?.direct_claim_snapshots.map((claim) => claim.id) ?? [] : []),
167
+ ...prepared.usedClaimIds,
168
+ ...prepared.claims.flatMap((claim) => claim.source_claim_ids)
169
+ ])].map((id) => SourceIdSchema.parse(id)).sort();
170
+ const createdAt = nowSeconds();
171
+ const name = first.message_type;
172
+ const traceIds = new Set((await new TraceStore(this.context).list()).map((trace) => trace.id));
173
+ let traceId = deriveTraceId(createdAt, name, traceInputs, prepared.claims.map((claim) => claim.id));
174
+ for (let attempt = 0; traceIds.has(traceId); attempt += 1) {
175
+ if (attempt >= 7) throw new StoreError("conflict", "Inbox Trace ID collision; retry the task.");
176
+ traceId = deriveTraceId(createdAt, name, traceInputs, prepared.claims.map((claim) => claim.id));
177
+ }
178
+ const trace = prepared.claims.length === 0 && prepared.disputes.length === 0 ? null : TraceSchema.parse({
179
+ id: traceId,
180
+ name,
181
+ task: `Inbox ${messages.map((message) => message.id).join(", ")}`,
182
+ agent: this.context.agentId,
183
+ input_claims: traceInputs,
184
+ output_claims: prepared.claims.map((claim) => claim.id),
185
+ created_at: createdAt
186
+ });
187
+ const existingDisputes = await new DisputeStore(this.context).list();
188
+ const disputes = await newDisputes(this.context, prepared.disputes, existingDisputes);
189
+ if (disputes.some((dispute) => existingDisputes.some((existing) => existing.id === dispute.id))) throw new StoreError("conflict", "Inbox Dispute ID collision; retry the task.");
190
+ const effect = InboxEffectSchema.parse({
191
+ schemaVersion: 1,
192
+ type: "effect",
193
+ canonicalId: first.id,
194
+ batchHash: batchHash(members),
195
+ members,
196
+ status: "prepared",
197
+ teamAuthorized: this.context.mode === "team",
198
+ syncClaims: prepared.syncClaims ?? [],
199
+ analysisClaims,
200
+ preparedClaims: prepared.claims,
201
+ appliedClaims: [],
202
+ disputes,
203
+ trace,
204
+ warnings: prepared.warnings,
205
+ retiredPolicies: messages.filter((message) => message.message_type === "policy_update" && message.policy.status === "deprecated").map((message) => message.policy.id)
206
+ });
207
+ control.signal.throwIfAborted();
208
+ control.prepared();
209
+ await this.write(effect);
210
+ await this.references(effect);
211
+ await this.applyLocked(effect);
212
+ return {
213
+ applied: effect.appliedClaims.length,
214
+ warnings: effect.warnings,
215
+ entityIds: [
216
+ ...effect.appliedClaims.map((claim) => claim.id),
217
+ ...effect.disputes.map((dispute) => dispute.id),
218
+ ...effect.trace === null ? [] : [effect.trace.id]
219
+ ]
220
+ };
221
+ }, {
222
+ timeoutMs: this.context.lockTimeoutMs,
223
+ signal: control.signal
224
+ });
225
+ }
226
+ async applyLocked(effect) {
227
+ await ensureKnowledgeReady(this.context.paths.root);
228
+ const current = await new ClaimStore(this.context).list();
229
+ observeApplied(effect.preparedClaims, current, effect.appliedClaims);
230
+ if (analysisIsStale(effect.analysisClaims, claimChanges(effect.analysisClaims, effect.preparedClaims, effect.appliedClaims, current))) {
231
+ effect.warnings.push(STALE_PLAN_WARNING);
232
+ this.context.logger.warn(STALE_PLAN_WARNING);
233
+ effect.preparedClaims = [];
234
+ effect.syncClaims = [];
235
+ effect.disputes = [];
236
+ effect.analysisClaims = current;
237
+ await this.write(effect);
238
+ }
239
+ for (const target of [...effect.preparedClaims].sort((a, b) => Number(a.status === "deprecated") - Number(b.status === "deprecated"))) {
240
+ if (effect.appliedClaims.some((claim) => claim.id === target.id)) continue;
241
+ await new ClaimStore(this.context).write(target);
242
+ effect.appliedClaims.push(target);
243
+ await this.write(effect);
244
+ }
245
+ if (effect.trace !== null) {
246
+ effect.trace.output_claims = effect.appliedClaims.map((claim) => claim.id);
247
+ await new TraceStore(this.context).write(effect.trace);
248
+ }
249
+ for (const dispute of effect.disputes) await new DisputeStore(this.context).write(dispute);
250
+ if (effect.teamAuthorized === void 0 && this.context.mode === "team") {
251
+ const warning = "旧 Inbox 事务缺少团队共享授权,仅恢复本地效果。";
252
+ effect.warnings.push(warning);
253
+ this.context.logger.warn(warning);
254
+ }
255
+ if (effect.teamAuthorized === true && this.context.mode === "team") {
256
+ const uploads = new UploadStore(this.context);
257
+ for (const policy of effect.retiredPolicies) await uploads.withdrawPolicy(policy);
258
+ const actual = await new ClaimStore(this.context).list();
259
+ const staged = [...new Map([...effect.appliedClaims, ...effect.syncClaims ?? []].map((claim) => [claim.id, claim])).values()];
260
+ await uploads.enqueue(staged.filter((claim) => actual.some((value) => sameClaim(value, claim))), effect.disputes, true);
261
+ }
262
+ effect.status = "applied";
263
+ await this.write(effect);
264
+ for (const member of effect.members) await new InboxStore(this.context).completeSnapshotLocked(member.message);
265
+ }
266
+ };
267
+ //#endregion
268
+ export { InboxEffectStore as n, inboxMessageHash as r, InboxEffectSchema as t };
@@ -0,0 +1,19 @@
1
+ //#region src/knowledge/errors.ts
2
+ var KnowledgeError = class extends Error {
3
+ kind;
4
+ constructor(kind, message) {
5
+ super(message);
6
+ this.kind = kind;
7
+ this.name = "KnowledgeError";
8
+ }
9
+ };
10
+ var RevisionConflict = class extends KnowledgeError {
11
+ currentRevision;
12
+ constructor(currentRevision) {
13
+ super("conflict", `Claim revision conflict; current revision: ${currentRevision}`);
14
+ this.currentRevision = currentRevision;
15
+ this.name = "RevisionConflict";
16
+ }
17
+ };
18
+ //#endregion
19
+ export { RevisionConflict as n, KnowledgeError as t };
@@ -0,0 +1,18 @@
1
+ //#region src/team/errors.ts
2
+ var TeamError = class extends Error {
3
+ kind;
4
+ service;
5
+ operation;
6
+ status;
7
+ connectionFailure;
8
+ constructor(kind, service, operation, status, connectionFailure) {
9
+ super(`${service} ${operation}:${kind === "auth" ? "鉴权失败,请在 ACN 设置检查 Agent ID 和团队鉴权密钥。" : kind === "legacy_route" ? "服务端不支持 Inbox receipt ACK,请联系团队管理员检查服务版本。" : kind === "retryable" ? "服务暂时不可用,本地知识和待投递任务已保留。" : kind === "protocol" ? "响应不符合团队协议,请检查服务版本。" : kind === "cancelled" ? "请求已取消。" : "请求被服务端拒绝,请检查请求权限与参数。"}${status === void 0 ? "" : ` status=${status}`}`);
10
+ this.kind = kind;
11
+ this.service = service;
12
+ this.operation = operation;
13
+ this.status = status;
14
+ this.connectionFailure = connectionFailure;
15
+ }
16
+ };
17
+ //#endregion
18
+ export { TeamError as t };