@lmzhen/dsh-evolution-core 0.59.6 → 0.60.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 (103) hide show
  1. package/README.md +3 -1
  2. package/lib/authority-plugin.js +3 -2
  3. package/lib/composition/index.js +2 -2
  4. package/lib/{composition-B_F4TYOD.js → composition-BSqh70z_.js} +1 -1
  5. package/lib/{ensure-BpVDuDnA.js → ensure-DTCuxAxR.js} +1 -1
  6. package/lib/{extract-plan-D3ALlAmN.js → extract-plan-CP__XlEs.js} +2 -2
  7. package/lib/extract.js +4 -4
  8. package/lib/http-routes.js +3 -2
  9. package/lib/index.js +15 -15
  10. package/lib/{io-Cpa8wuvj.js → io-DT4-MXLr.js} +25 -1
  11. package/lib/io-node.js +1 -1
  12. package/lib/io-platform.js +163 -0
  13. package/lib/io-registry.js +10 -0
  14. package/lib/{mutation-authority-CZpc9F20.js → mutation-authority-C3RYyiaT.js} +66 -41
  15. package/lib/{mutation-storage-adapter-Cq1bmS1E.js → mutation-storage-adapter-CSVn34dK.js} +327 -210
  16. package/lib/{mutations-Y-wKod7W.js → mutations-JKFlfhSO.js} +1 -1
  17. package/lib/policy.js +1 -1
  18. package/lib/{quality-NPGr3VzA.js → quality-C0ri2SgY.js} +2 -2
  19. package/lib/restrictor.js +1 -1
  20. package/lib/result.js +1 -1
  21. package/lib/{review-context-yNiZadPw.js → review-context-D2LIOEi-.js} +2 -2
  22. package/lib/runtime/plugin.js +2 -2
  23. package/lib/{session-projection-DM15fsRp.js → session-projection-Dnmm5bzt.js} +1 -1
  24. package/lib/{skill-history-BvLWFoOo.js → skill-history-BOZ4HCDB.js} +2 -2
  25. package/lib/skills.js +268 -37
  26. package/lib/state/domain/index.js +433 -0
  27. package/lib/state/index.js +136 -0
  28. package/lib/state/migrate.js +90 -0
  29. package/lib/state/storage.js +605 -0
  30. package/lib/state/store-plugin.js +23 -0
  31. package/lib/state/store.js +268 -0
  32. package/lib/subagent/index.js +1 -1
  33. package/lib/types/authority-plugin.d.ts +1 -1
  34. package/lib/types/authority-plugin.js +2 -1
  35. package/lib/types/authority.d.ts +1 -1
  36. package/lib/types/authority.js +75 -48
  37. package/lib/types/composition/ensure.js +1 -1
  38. package/lib/types/contract.d.ts +8 -0
  39. package/lib/types/http-routes.d.ts +1 -0
  40. package/lib/types/http-routes.js +2 -1
  41. package/lib/types/io-platform.d.ts +74 -0
  42. package/lib/types/io-platform.js +178 -0
  43. package/lib/types/io-registry.d.ts +4 -0
  44. package/lib/types/io-registry.js +10 -0
  45. package/lib/types/io.d.ts +7 -0
  46. package/lib/types/io.js +25 -1
  47. package/lib/types/mutation-authority.d.ts +1 -1
  48. package/lib/types/mutation-storage-adapter.d.ts +1 -1
  49. package/lib/types/opt-in.js +2 -2
  50. package/lib/types/params.js +2 -2
  51. package/lib/types/policy.js +1 -1
  52. package/lib/types/restrictor.js +2 -2
  53. package/lib/types/result.d.ts +1 -1
  54. package/lib/types/result.js +1 -1
  55. package/lib/types/runtime/coherence.js +1 -1
  56. package/lib/types/runtime/llm.d.ts +1 -1
  57. package/lib/types/runtime/llm.js +1 -1
  58. package/lib/types/runtime/platform.d.ts +3 -3
  59. package/lib/types/runtime/platform.js +2 -2
  60. package/lib/types/runtime/projections.d.ts +1 -1
  61. package/lib/types/scope.js +1 -1
  62. package/lib/types/session-projection.d.ts +1 -1
  63. package/lib/types/signals.d.ts +1 -1
  64. package/lib/types/skill-store.d.ts +37 -4
  65. package/lib/types/skill-store.js +240 -24
  66. package/lib/types/skill-write-plan.d.ts +1 -1
  67. package/lib/types/skill-write-service.js +8 -2
  68. package/lib/types/state/conformance.d.ts +47 -0
  69. package/lib/types/state/conformance.js +246 -0
  70. package/lib/types/state/constants.d.ts +52 -0
  71. package/lib/types/state/constants.js +52 -0
  72. package/lib/types/state/domain/compatibility.d.ts +40 -0
  73. package/lib/types/state/domain/compatibility.js +36 -0
  74. package/lib/types/state/domain/index.d.ts +81 -0
  75. package/lib/types/state/domain/index.js +592 -0
  76. package/lib/types/state/index.d.ts +63 -0
  77. package/lib/types/state/index.js +174 -0
  78. package/lib/types/state/medium-declaration.d.ts +37 -0
  79. package/lib/types/state/medium-declaration.js +53 -0
  80. package/lib/types/state/migrate.d.ts +34 -0
  81. package/lib/types/state/migrate.js +103 -0
  82. package/lib/types/state/record-contract.d.ts +56 -0
  83. package/lib/types/state/record-contract.js +120 -0
  84. package/lib/types/state/storage.d.ts +112 -0
  85. package/lib/types/state/storage.js +178 -0
  86. package/lib/types/state/store-plugin.d.ts +14 -0
  87. package/lib/types/state/store-plugin.js +18 -0
  88. package/lib/types/state/store.d.ts +39 -0
  89. package/lib/types/state/store.js +270 -0
  90. package/lib/types/store-ledger.js +2 -2
  91. package/lib/types/subagent/run.d.ts +1 -1
  92. package/lib/types/subagent/types.d.ts +4 -4
  93. package/lib/types/{write-kernel.d.ts → target-writer.d.ts} +32 -15
  94. package/lib/types/{write-kernel.js → target-writer.js} +40 -8
  95. package/lib/types/tool-dispatch.js +6 -3
  96. package/lib/types/{mutation-unit.d.ts → unit-kernel.d.ts} +40 -13
  97. package/lib/types/{mutation-unit.js → unit-kernel.js} +155 -118
  98. package/lib/types/write.d.ts +1 -2
  99. package/lib/types/write.js +1 -2
  100. package/lib/{usage-D7yHG2Cb.js → usage-BO4Zj_zB.js} +2 -2
  101. package/lib/write.js +4 -4
  102. package/package.json +30 -3
  103. package/persisted-write-inventory.json +14 -5
@@ -1,7 +1,7 @@
1
- import type { Agent, AgentIdentity, AgentOptions } from '@lmzhen/dsh-evolution-platform-contract';
2
- import type { SessionId } from '@lmzhen/dsh-evolution-platform-contract';
3
- import type { SubagentStopReason } from '@lmzhen/dsh-evolution-platform-contract';
4
- import type { ObjectJsonSchema } from '@lmzhen/dsh-evolution-platform-contract';
1
+ import type { Agent, AgentIdentity, AgentOptions } from '@lmzhen/dsh-evolution-platform/contract';
2
+ import type { SessionId } from '@lmzhen/dsh-evolution-platform/contract';
3
+ import type { SubagentStopReason } from '@lmzhen/dsh-evolution-platform/contract';
4
+ import type { ObjectJsonSchema } from '@lmzhen/dsh-evolution-platform/contract';
5
5
  export type ChannelCode = 'E-323' | 'E-324';
6
6
  export type ChannelKind = 'reviewer' | 'maintainer' | 'governance';
7
7
  export interface PresetStatus {
@@ -1,31 +1,35 @@
1
1
  import { transactIo, type EvolutionIoLike } from './io.ts';
2
2
  /** The four persistent writer kinds the family permits. */
3
+ /** A unit whose targets were read and verified by prepare(); it carries no bytes. */
4
+ export interface PreparedUnit {
5
+ readonly targets: readonly UnitTarget[];
6
+ }
3
7
  /** One planned write: expected is the baseline read before the plan, next is the new bytes. */
4
- export interface WriteTarget {
8
+ export interface UnitTarget {
5
9
  readonly path: string;
6
10
  readonly expected: string | null;
7
11
  readonly next: string | null;
8
12
  readonly skill?: string | undefined;
9
13
  }
10
14
  /** A structured failure; conflict always means the target changed before/inside the write. */
11
- export interface WriteKernelError {
15
+ export interface TargetWriterError {
12
16
  readonly code: 'cas-conflict' | 'write-failed' | 'compensation-blocked' | 'transact-not-invoked';
13
17
  readonly message: string;
14
18
  readonly target: string;
15
19
  }
16
20
  /** Outcome of one write or one multi-target commit. */
17
- export interface WriteKernelResult {
21
+ export interface TargetWriterResult {
18
22
  readonly ok: boolean;
19
23
  readonly changed: readonly string[];
20
- readonly errors: readonly WriteKernelError[];
24
+ readonly errors: readonly TargetWriterError[];
21
25
  readonly warnings?: readonly string[] | undefined;
22
26
  }
23
- export interface WriteKernelOptions {
27
+ export interface TargetWriterOptions {
24
28
  readonly io: EvolutionIoLike;
25
29
  readonly transact?: typeof transactIo | undefined;
26
30
  readonly now?: (() => number) | undefined;
27
31
  }
28
- export interface WriteOneOptions {
32
+ export interface UnitWriteOneOptions {
29
33
  /** false is the create-under-root-lock retry seam: a mismatch defers instead of failing. */
30
34
  readonly guardBaseline?: boolean | undefined;
31
35
  }
@@ -34,11 +38,11 @@ export interface WriteOneOptions {
34
38
  * transaction, never overwrite a concurrent value, and compensate only this
35
39
  * unit's own writes. Business state machines stay in their domain owners.
36
40
  */
37
- export declare class WriteKernel {
41
+ export declare class TargetWriter {
38
42
  private readonly io;
39
43
  private readonly transact;
40
44
  private readonly now;
41
- constructor(options: WriteKernelOptions);
45
+ constructor(options: TargetWriterOptions);
42
46
  /** @returns the current time source; exposed for adapters that stamp evidence. */
43
47
  clock(): number;
44
48
  /**
@@ -59,13 +63,26 @@ export declare class WriteKernel {
59
63
  * defers (returns the current bytes) so a retrying transact can re-observe;
60
64
  * if no retry ever writes, the result is still a conflict.
61
65
  */
62
- writeOne(target: WriteTarget, options?: WriteOneOptions): Promise<WriteKernelResult>;
66
+ writeOne(target: UnitTarget, options?: UnitWriteOneOptions): Promise<TargetWriterResult>;
67
+ /**
68
+ * Read and verify every target without writing. A drift returns one
69
+ * cas-conflict per target and leaves the filesystem untouched.
70
+ */
71
+ prepare(targets: readonly UnitTarget[]): Promise<{
72
+ readonly ok: true;
73
+ readonly prepared: PreparedUnit;
74
+ } | {
75
+ readonly ok: false;
76
+ readonly errors: readonly TargetWriterError[];
77
+ }>;
63
78
  /**
64
- * Write a plan in order. A preflight drift writes nothing; a later failure
65
- * compensates only the targets this commit wrote, and only while they still
66
- * hold this commit's bytes.
79
+ * Commit a unit that already passed {@link prepare}. This is the only write
80
+ * point; a later failure compensates only the targets this commit wrote, and
81
+ * only while they still hold this commit's bytes.
67
82
  */
68
- commit(targets: readonly WriteTarget[]): Promise<WriteKernelResult>;
83
+ commitPrepared(prepared: PreparedUnit): Promise<TargetWriterResult>;
84
+ /** Prepare then commit; a preflight drift writes nothing. */
85
+ commit(targets: readonly UnitTarget[]): Promise<TargetWriterResult>;
69
86
  /**
70
87
  * Restore a target to its before bytes only if it still holds the bytes this
71
88
  * unit wrote. A concurrent writer wins and the compensation is reported.
@@ -74,7 +91,7 @@ export declare class WriteKernel {
74
91
  readonly path: string;
75
92
  readonly before: string | null;
76
93
  readonly after: string | null;
77
- }): Promise<WriteKernelResult>;
94
+ }): Promise<TargetWriterResult>;
78
95
  private compensate;
79
96
  }
80
- //# sourceMappingURL=write-kernel.d.ts.map
97
+ //# sourceMappingURL=target-writer.d.ts.map
@@ -4,7 +4,7 @@ import { isCommittedWarning, TransactionNotInvokedError, transactIo } from "./io
4
4
  * transaction, never overwrite a concurrent value, and compensate only this
5
5
  * unit's own writes. Business state machines stay in their domain owners.
6
6
  */
7
- export class WriteKernel {
7
+ export class TargetWriter {
8
8
  io;
9
9
  transact;
10
10
  now;
@@ -57,6 +57,23 @@ export class WriteKernel {
57
57
  * if no retry ever writes, the result is still a conflict.
58
58
  */
59
59
  async writeOne(target, options = {}) {
60
+ // A missing target is published with the filesystem's no-replace primitive
61
+ // (hard link), so even a writer that ignores our locks cannot be silently
62
+ // overwritten. Expected-present writes still go through the CAS transaction.
63
+ if (target.expected === null && target.next !== null && this.io.createTextIfAbsent !== undefined) {
64
+ try {
65
+ await this.io.createTextIfAbsent(target.path, target.next);
66
+ return { ok: true, changed: [target.path], errors: [] };
67
+ }
68
+ catch (error) {
69
+ if (isCommittedWarning(error))
70
+ return { ok: true, changed: [target.path], errors: [], warnings: [error instanceof Error ? error.message : String(error)] };
71
+ const code = error?.code;
72
+ if (code === 'EEXIST' || code === 'ENOTEMPTY')
73
+ return { ok: false, changed: [], errors: [{ code: 'cas-conflict', message: 'target appeared before create: ' + target.path, target: target.path }] };
74
+ return { ok: false, changed: [], errors: [{ code: 'write-failed', message: error instanceof Error ? error.message : String(error), target: target.path }] };
75
+ }
76
+ }
60
77
  const guard = options.guardBaseline !== false;
61
78
  let wrote = false;
62
79
  let deferred = false;
@@ -96,18 +113,26 @@ export class WriteKernel {
96
113
  return { ok: true, changed: [target.path], errors: [], ...(transactionWarnings === undefined ? {} : { warnings: transactionWarnings }) };
97
114
  }
98
115
  /**
99
- * Write a plan in order. A preflight drift writes nothing; a later failure
100
- * compensates only the targets this commit wrote, and only while they still
101
- * hold this commit's bytes.
116
+ * Read and verify every target without writing. A drift returns one
117
+ * cas-conflict per target and leaves the filesystem untouched.
102
118
  */
103
- async commit(targets) {
119
+ async prepare(targets) {
120
+ const errors = [];
104
121
  for (const target of targets) {
105
122
  const current = await this.io.readText(target.path);
106
123
  if (current !== target.expected)
107
- return { ok: false, changed: [], errors: [{ code: 'cas-conflict', message: 'target changed after plan: ' + target.path, target: target.path }] };
124
+ errors.push({ code: 'cas-conflict', message: 'target changed after plan: ' + target.path, target: target.path });
108
125
  }
126
+ return errors.length === 0 ? { ok: true, prepared: { targets } } : { ok: false, errors };
127
+ }
128
+ /**
129
+ * Commit a unit that already passed {@link prepare}. This is the only write
130
+ * point; a later failure compensates only the targets this commit wrote, and
131
+ * only while they still hold this commit's bytes.
132
+ */
133
+ async commitPrepared(prepared) {
109
134
  const written = [];
110
- for (const target of targets) {
135
+ for (const target of prepared.targets) {
111
136
  const result = await this.writeOne(target);
112
137
  if (!result.ok) {
113
138
  const compensation = await this.compensate(written);
@@ -117,6 +142,13 @@ export class WriteKernel {
117
142
  }
118
143
  return { ok: true, changed: written.map(item => item.path), errors: [] };
119
144
  }
145
+ /** Prepare then commit; a preflight drift writes nothing. */
146
+ async commit(targets) {
147
+ const prepared = await this.prepare(targets);
148
+ if (!prepared.ok)
149
+ return { ok: false, changed: [], errors: prepared.errors };
150
+ return await this.commitPrepared(prepared.prepared);
151
+ }
120
152
  /**
121
153
  * Restore a target to its before bytes only if it still holds the bytes this
122
154
  * unit wrote. A concurrent writer wins and the compensation is reported.
@@ -140,4 +172,4 @@ export class WriteKernel {
140
172
  return errors;
141
173
  }
142
174
  }
143
- //# sourceMappingURL=write-kernel.js.map
175
+ //# sourceMappingURL=target-writer.js.map
@@ -126,9 +126,12 @@ function readDispatchRecord(event) {
126
126
  rootCallId: callId,
127
127
  name: '',
128
128
  arguments: undefined,
129
- // A failure is marked by the payload-level `error` OR a result block's own
130
- // `isError` (both shapes exist in persisted pre-rc.2 logs).
131
- outcome: { ok: data.isError !== true && data.error === undefined && block?.isError !== true },
129
+ // A failure is marked by the platform's `message.isError` (the live
130
+ // `createToolResultMessage` shape), by the payload-level `error` (HarnessError
131
+ // info), or by a result block's own `isError` (legacy/pre-rc.2 logs). The
132
+ // live result payload has no top-level `isError`; reading only that field
133
+ // counted a failed skill read as success and bypassed READ_BEFORE_WRITE.
134
+ outcome: { ok: data.isError !== true && data.message?.isError !== true && data.error === undefined && block?.isError !== true },
132
135
  };
133
136
  }
134
137
  const isPtc = event.type === PTC_DISPATCH_START_EVENT || event.type === PTC_DISPATCH_EVENT;
@@ -103,8 +103,8 @@ export interface MutationSnapshot {
103
103
  runId?: string;
104
104
  leaseId?: string;
105
105
  }
106
- /** Identity attached to one MutationUnit's snapshot/journal writes. */
107
- export interface MutationUnitContext {
106
+ /** Identity attached to one UnitKernel's snapshot/journal writes. */
107
+ export interface UnitKernelContext {
108
108
  readonly owner: MutationOwner;
109
109
  readonly runId: string;
110
110
  readonly leaseId: string;
@@ -114,18 +114,18 @@ export interface MutationSnapshotPort {
114
114
  read(unitId: string): Promise<MutationSnapshot | null>;
115
115
  listPrepared?(): Promise<readonly MutationSnapshot[]>;
116
116
  }
117
- export interface MutationUnitEvent {
117
+ export interface UnitKernelEvent {
118
118
  unitId: string;
119
119
  changed: readonly string[];
120
120
  ops: readonly MutationSnapshotOp[];
121
121
  roots?: readonly string[];
122
122
  }
123
- export interface MutationUnitHooks {
124
- committed?(event: MutationUnitEvent): Promise<void> | void;
125
- rolledBack?(event: MutationUnitEvent): Promise<void> | void;
126
- undone?(event: MutationUnitEvent): Promise<void> | void;
123
+ export interface UnitKernelHooks {
124
+ committed?(event: UnitKernelEvent): Promise<void> | void;
125
+ rolledBack?(event: UnitKernelEvent): Promise<void> | void;
126
+ undone?(event: UnitKernelEvent): Promise<void> | void;
127
127
  }
128
- export interface MutationUnitOptions {
128
+ export interface UnitKernelOptions {
129
129
  io: import('./io.ts').EvolutionIoLike;
130
130
  limits: MutationLimits;
131
131
  serial?: <T>(task: () => Promise<T>) => Promise<T>;
@@ -147,14 +147,32 @@ export interface MutationUnitOptions {
147
147
  /** Optional merge target claim, acquired after the fence and released in finally. */
148
148
  claim?: UnitClaimPort;
149
149
  claimRunId?: string;
150
- hooks?: MutationUnitHooks;
151
- context?: MutationUnitContext;
150
+ hooks?: UnitKernelHooks;
151
+ context?: UnitKernelContext;
152
152
  validateWrite?: (op: TreeChangeOp) => string | null;
153
153
  transact?: typeof transactIo;
154
154
  /** @deprecated the kernel always re-checks the baseline; kept for source compatibility. */
155
155
  transactGuard?: boolean;
156
156
  now?: () => number;
157
157
  }
158
+ export interface PreparedMutation {
159
+ readonly kind: 'prepared';
160
+ readonly unitId: string;
161
+ readonly request: MutationRequest;
162
+ readonly primary: string;
163
+ readonly validation: MutationValidation;
164
+ readonly snapshots: readonly {
165
+ target: string;
166
+ before: string | null;
167
+ after: string | null;
168
+ baseline: string | null;
169
+ }[];
170
+ readonly eventOps: readonly MutationSnapshotOp[];
171
+ readonly changed: string[];
172
+ readonly warnings: string[];
173
+ readonly bestEffort: (task: () => Promise<void>) => Promise<void>;
174
+ release(): Promise<void>;
175
+ }
158
176
  export interface MutationResult {
159
177
  ok: boolean;
160
178
  unitId: string;
@@ -170,7 +188,7 @@ export interface MutationPreviewResult {
170
188
  ops: TreeChangeOp[];
171
189
  errors: MutationValidationError[];
172
190
  }
173
- export declare class MutationUnit {
191
+ export declare class UnitKernel {
174
192
  private readonly io;
175
193
  private readonly limits;
176
194
  private readonly serial;
@@ -189,9 +207,18 @@ export declare class MutationUnit {
189
207
  private readonly transactGuard;
190
208
  private readonly now;
191
209
  private readonly journalId;
192
- constructor(options: MutationUnitOptions);
210
+ constructor(options: UnitKernelOptions);
193
211
  private resolveTargetKeys;
194
212
  preview(request: MutationRequest): MutationPreviewResult;
213
+ /**
214
+ * Phase 1: acquire the lease/claim and read every target, verifying CAS and
215
+ * write validation. This performs NO durable write (no journal, snapshot,
216
+ * audit or event). The returned handle holds the lease until commit() or
217
+ * release(); callers must use try/finally.
218
+ */
219
+ prepare(request: MutationRequest): Promise<PreparedMutation | MutationResult>;
220
+ /** Phase 2: the only durable write phase. Releases the prepare lease in all paths. */
221
+ commit(prepared: PreparedMutation): Promise<MutationResult>;
195
222
  apply(request: MutationRequest): Promise<MutationResult>;
196
223
  /** Recover every prepared snapshot known to the port; safe to call at boot. */
197
224
  recoverPrepared(): Promise<MutationResult[]>;
@@ -214,4 +241,4 @@ export interface FileMutationLockOptions {
214
241
  * tree lock so an ordinary skill write cannot deadlock a merge run.
215
242
  */
216
243
  export declare function fileMutationLock(io: import('./io.ts').EvolutionIoLike, root: string, options?: FileMutationLockOptions): MutationLockPort;
217
- //# sourceMappingURL=mutation-unit.d.ts.map
244
+ //# sourceMappingURL=unit-kernel.d.ts.map