@lmzhen/dsh-evolution-core 0.59.5 → 0.59.6

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.
@@ -11,7 +11,6 @@
11
11
  */
12
12
  import { contentHash } from "./mutations.js";
13
13
  import { skillBodyTarget, skillSupportTarget } from "./skill-observation.js";
14
- import { SkillTargetLocks } from "./skill-target-locks.js";
15
14
  /**
16
15
  * Read body/support bytes and record the observation for the exact owner.
17
16
  *
@@ -21,7 +20,6 @@ import { SkillTargetLocks } from "./skill-target-locks.js";
21
20
  export class SkillWriteService {
22
21
  library;
23
22
  observations;
24
- locks = new SkillTargetLocks();
25
23
  constructor(library, observations) {
26
24
  this.library = library;
27
25
  this.observations = observations;
@@ -33,7 +31,7 @@ export class SkillWriteService {
33
31
  * @returns the body, or null when absent.
34
32
  */
35
33
  async readBody(owner, name) {
36
- const target = skillBodyTarget(name);
34
+ const target = await this.library.identify(skillBodyTarget(name));
37
35
  const content = await this.library.read(name);
38
36
  this.record(owner, target, content);
39
37
  return content;
@@ -46,11 +44,188 @@ export class SkillWriteService {
46
44
  * @returns the bytes, or null when absent.
47
45
  */
48
46
  async readSupport(owner, name, filePath) {
49
- const target = skillSupportTarget(name, filePath);
47
+ const target = await this.library.identify(skillSupportTarget(name, filePath));
50
48
  const content = await this.library.readSupportFile(name, filePath);
51
49
  this.record(owner, target, content);
52
50
  return content;
53
51
  }
52
+ /**
53
+ * Plan a create. Creation is createIfAbsent by definition and needs no prior
54
+ * read; the provider's `create` path keeps managed/protection semantics.
55
+ * @param owner - writer identity (unused for create, kept for a uniform API).
56
+ * @param name - skill name.
57
+ * @param content - full body.
58
+ * @param managed - add the managed marker (non-foreground origin).
59
+ * @returns the plan or a typed refusal.
60
+ */
61
+ async planCreate(owner, name, content, origin = 'foreground', managed = false) {
62
+ void owner;
63
+ return {
64
+ ok: true,
65
+ plan: {
66
+ origin,
67
+ ops: [{ kind: 'createSkill', skill: name, content, ...(managed ? { managed: true } : {}) }],
68
+ },
69
+ };
70
+ }
71
+ /**
72
+ * Plan a full body replacement from this owner's observation.
73
+ * @param owner - exact writer.
74
+ * @param name - skill name.
75
+ * @param content - next body.
76
+ * @param origin - write face.
77
+ * @returns the plan or a typed refusal.
78
+ */
79
+ async planUpdate(owner, name, content, origin = 'foreground') {
80
+ const target = await this.library.identify(skillBodyTarget(name));
81
+ const observed = this.observations.requirePresent(owner, target);
82
+ if (!('version' in observed))
83
+ return planRefusal(observed.kind);
84
+ return {
85
+ ok: true,
86
+ plan: {
87
+ origin,
88
+ ops: [{ kind: 'write', target, content, expect: { kind: 'present', version: observed.version } }],
89
+ },
90
+ };
91
+ }
92
+ /**
93
+ * Plan a literal patch against a body or support target.
94
+ *
95
+ * A section patch always targets the body and ignores `filePath`; that is the
96
+ * same contract the library has always had, but now it is explicit in the
97
+ * plan instead of a tool branch.
98
+ * @param owner - exact writer.
99
+ * @param name - skill name.
100
+ * @param input - literal edit or section edit.
101
+ * @param origin - write face.
102
+ * @returns the plan or a typed refusal.
103
+ */
104
+ async planPatch(owner, name, input, origin = 'foreground') {
105
+ if (input.section !== undefined) {
106
+ const target = await this.library.identify(skillBodyTarget(name));
107
+ const observed = this.observations.requirePresent(owner, target);
108
+ if (!('version' in observed))
109
+ return planRefusal(observed.kind);
110
+ return {
111
+ ok: true,
112
+ plan: {
113
+ origin,
114
+ ops: [{
115
+ kind: 'patchSection',
116
+ target,
117
+ section: input.section,
118
+ expect: { kind: 'present', version: observed.version },
119
+ }],
120
+ },
121
+ };
122
+ }
123
+ const filePath = input.filePath ?? '';
124
+ const logicalTarget = filePath === '' ? skillBodyTarget(name) : skillSupportTarget(name, filePath);
125
+ const target = await this.library.identify(logicalTarget);
126
+ const observed = this.observations.requirePresent(owner, target);
127
+ if (!('version' in observed))
128
+ return planRefusal(observed.kind);
129
+ return {
130
+ ok: true,
131
+ plan: {
132
+ origin,
133
+ ops: [{
134
+ kind: 'patch',
135
+ target,
136
+ oldString: input.oldString ?? '',
137
+ newString: input.newString ?? '',
138
+ replaceAll: input.replaceAll === true,
139
+ expect: { kind: 'present', version: observed.version },
140
+ }],
141
+ },
142
+ };
143
+ }
144
+ /**
145
+ * Plan a full support-file write. A newly observed/absent file is
146
+ * createIfAbsent; an already observed file is replaceIfVersion. An unread
147
+ * existing file is refused instead of overwritten.
148
+ * @param owner - exact writer.
149
+ * @param name - skill name.
150
+ * @param filePath - support path.
151
+ * @param content - next bytes.
152
+ * @param origin - write face.
153
+ * @returns the plan or a typed refusal.
154
+ */
155
+ async planWriteSupport(owner, name, filePath, content, origin = 'foreground') {
156
+ const target = await this.library.identify(skillSupportTarget(name, filePath));
157
+ const observed = this.observations.get(owner, target);
158
+ if (observed === undefined) {
159
+ // This read decides absent vs existing; it is not recorded as a read
160
+ // observation. The real createIfAbsent guard is the kernel anchor.
161
+ const current = await this.library.readSupportFile(name, filePath);
162
+ if (current !== null)
163
+ return notObserved();
164
+ return { ok: true, plan: { origin, ops: [{ kind: 'write', target, content, expect: { kind: 'absent' } }] } };
165
+ }
166
+ if (observed.kind === 'absent') {
167
+ return { ok: true, plan: { origin, ops: [{ kind: 'write', target, content, expect: { kind: 'absent' } }] } };
168
+ }
169
+ return {
170
+ ok: true,
171
+ plan: {
172
+ origin,
173
+ ops: [{ kind: 'write', target, content, expect: { kind: 'present', version: observed.version } }],
174
+ },
175
+ };
176
+ }
177
+ /**
178
+ * Plan removing one support file from this owner's observation.
179
+ * @param owner - exact writer.
180
+ * @param name - skill name.
181
+ * @param filePath - support path.
182
+ * @param origin - write face.
183
+ * @returns the plan or a typed refusal.
184
+ */
185
+ async planRemoveSupport(owner, name, filePath, origin = 'foreground') {
186
+ const target = await this.library.identify(skillSupportTarget(name, filePath));
187
+ const observed = this.observations.requirePresent(owner, target);
188
+ if (!('version' in observed))
189
+ return planRefusal(observed.kind);
190
+ return {
191
+ ok: true,
192
+ plan: {
193
+ origin,
194
+ ops: [{ kind: 'remove', target, expect: { kind: 'present', version: observed.version } }],
195
+ },
196
+ };
197
+ }
198
+ /**
199
+ * Apply one plan through the provider and record the committed result for
200
+ * this owner. The re-read is temporary H2 bookkeeping; H5 replaces it with
201
+ * the provider's `SkillWriteOutcome`, so a post-commit read failure can never
202
+ * turn a landed write into a tool failure.
203
+ * @param plan - the write plan.
204
+ * @param owner - exact writer identity; omitted by internal callers that do
205
+ * not own observations.
206
+ * @returns the service-level result.
207
+ */
208
+ async apply(plan, owner) {
209
+ const result = await this.library.apply(plan);
210
+ if (result.ok && owner !== undefined) {
211
+ try {
212
+ await this.observeOutcomes(result, owner);
213
+ }
214
+ catch {
215
+ // The write is already committed; an observation-cache failure must not
216
+ // turn a landed write into a tool failure (K6).
217
+ }
218
+ }
219
+ return actionResult(result);
220
+ }
221
+ async observeOutcomes(result, owner) {
222
+ for (const outcome of result.outcomes) {
223
+ if (outcome.version === undefined)
224
+ continue;
225
+ const target = outcome.target.identityKey === undefined ? await this.library.identify(outcome.target) : outcome.target;
226
+ this.observations.record(owner, target, outcome.version === '' ? { kind: 'absent' } : { kind: 'present', version: outcome.version });
227
+ }
228
+ }
54
229
  /**
55
230
  * Replace SKILL.md only when this owner observed its current version.
56
231
  * @param owner - exact writer identity.
@@ -59,16 +234,14 @@ export class SkillWriteService {
59
234
  * @returns write outcome or a typed refusal.
60
235
  */
61
236
  async updateBody(owner, name, content, origin = 'foreground') {
62
- const target = skillBodyTarget(name);
237
+ const target = await this.library.identify(skillBodyTarget(name));
63
238
  const observed = this.observations.requirePresent(owner, target);
64
239
  if (!('version' in observed))
65
240
  return refusal(observed.kind);
66
- return await this.locks.withLock(target.targetKey, async () => {
67
- const result = await this.library.update(name, content, origin, anchorOf(observed.version));
68
- if (result.ok)
69
- await this.readBody(owner, name);
70
- return actionResult(result);
71
- });
241
+ const result = await this.library.update(name, content, origin, anchorOf(observed.version));
242
+ if (result.ok)
243
+ await this.readBody(owner, name);
244
+ return actionResult(result);
72
245
  }
73
246
  /**
74
247
  * Patch SKILL.md only when this owner observed its current version.
@@ -80,16 +253,14 @@ export class SkillWriteService {
80
253
  * @returns write outcome or a typed refusal.
81
254
  */
82
255
  async patchBody(owner, name, oldString, newString, replaceAll = false, origin = 'foreground') {
83
- const target = skillBodyTarget(name);
256
+ const target = await this.library.identify(skillBodyTarget(name));
84
257
  const observed = this.observations.requirePresent(owner, target);
85
258
  if (!('version' in observed))
86
259
  return refusal(observed.kind);
87
- return await this.locks.withLock(target.targetKey, async () => {
88
- const result = await this.library.patch(name, oldString, newString, '', replaceAll, origin, anchorOf(observed.version));
89
- if (result.ok)
90
- await this.readBody(owner, name);
91
- return actionResult(result);
92
- });
260
+ const result = await this.library.patch(name, oldString, newString, '', replaceAll, origin, anchorOf(observed.version));
261
+ if (result.ok)
262
+ await this.readBody(owner, name);
263
+ return actionResult(result);
93
264
  }
94
265
  /**
95
266
  * Replace one support file only when this owner observed its current version.
@@ -100,29 +271,25 @@ export class SkillWriteService {
100
271
  * @returns write outcome or a typed refusal.
101
272
  */
102
273
  async updateSupport(owner, name, filePath, content, origin = 'foreground') {
103
- const target = skillSupportTarget(name, filePath);
274
+ const target = await this.library.identify(skillSupportTarget(name, filePath));
104
275
  const observed = this.observations.requirePresent(owner, target);
105
276
  if (!('version' in observed))
106
277
  return refusal(observed.kind);
107
- return await this.locks.withLock(target.targetKey, async () => {
108
- const result = await this.library.writeSupportFile(name, filePath, content, origin, anchorOf(observed.version));
109
- if (result.ok)
110
- await this.readSupport(owner, name, filePath);
111
- return actionResult(result);
112
- });
278
+ const result = await this.library.writeSupportFile(name, filePath, content, origin, anchorOf(observed.version));
279
+ if (result.ok)
280
+ await this.readSupport(owner, name, filePath);
281
+ return actionResult(result);
113
282
  }
114
283
  /** Remove a support file only after this owner read it at the observed version. */
115
284
  async removeSupport(owner, name, filePath, origin = 'foreground') {
116
- const target = skillSupportTarget(name, filePath);
285
+ const target = await this.library.identify(skillSupportTarget(name, filePath));
117
286
  const observed = this.observations.requirePresent(owner, target);
118
287
  if (!('version' in observed))
119
288
  return refusal(observed.kind);
120
- return await this.locks.withLock(target.targetKey, async () => {
121
- const result = await this.library.removeSupportFile(name, filePath, origin, anchorOf(observed.version));
122
- if (result.ok)
123
- this.observations.record(owner, target, { kind: 'absent' });
124
- return actionResult(result);
125
- });
289
+ const result = await this.library.removeSupportFile(name, filePath, origin, anchorOf(observed.version));
290
+ if (result.ok)
291
+ this.observations.record(owner, target, { kind: 'absent' });
292
+ return actionResult(result);
126
293
  }
127
294
  /**
128
295
  * Create a support file when absent, or update it only after this owner read it.
@@ -130,7 +297,7 @@ export class SkillWriteService {
130
297
  * existing unread file is refused rather than overwritten.
131
298
  */
132
299
  async writeSupport(owner, name, filePath, content, origin = 'foreground') {
133
- const target = skillSupportTarget(name, filePath);
300
+ const target = await this.library.identify(skillSupportTarget(name, filePath));
134
301
  const observed = this.observations.get(owner, target);
135
302
  if (observed === undefined) {
136
303
  const current = await this.library.readSupportFile(name, filePath);
@@ -169,10 +336,16 @@ function refusal(kind) {
169
336
  ? { ok: false, code: 'SKILL_NOT_OBSERVED', message: 'read the skill target before writing it' }
170
337
  : { ok: false, code: 'SKILL_NOT_FOUND', message: 'skill target does not exist' };
171
338
  }
339
+ function planRefusal(kind) {
340
+ return refusal(kind);
341
+ }
342
+ function notObserved() {
343
+ return planRefusal('unobserved');
344
+ }
172
345
  function anchorOf(version) { return { sha256: version }; }
173
346
  function actionResult(result) {
174
347
  if (result.ok)
175
- return { ok: true, message: result.message, ...(result.noop === true ? { noop: true } : {}) };
348
+ return { ok: true, message: result.message, ...(result.noop === true ? { noop: true } : {}), ...(result.path === undefined ? {} : { path: result.path }), ...(result.normalizedFrontmatterFields === undefined ? {} : { normalizedFrontmatterFields: result.normalizedFrontmatterFields }) };
176
349
  if (result.stale === true)
177
350
  return { ok: false, code: 'SKILL_STALE_VERSION', message: result.message };
178
351
  return { ok: false, code: 'SKILL_INVALID', message: result.message };
@@ -7,7 +7,9 @@ export * from './skill-reads.ts';
7
7
  export * from './skill-observation.ts';
8
8
  export * from './skill-observation-source.ts';
9
9
  export * from './skill-write-intent.ts';
10
+ export * from './skill-write-plan.ts';
10
11
  export * from './skill-write-service.ts';
12
+ export * from './system-skill-writer.ts';
11
13
  export * from './skill-target-locks.ts';
12
14
  export * from './skill-root-gate.ts';
13
15
  export * from './skill-row-cells.ts';
@@ -7,7 +7,9 @@ export * from "./skill-reads.js";
7
7
  export * from "./skill-observation.js";
8
8
  export * from "./skill-observation-source.js";
9
9
  export * from "./skill-write-intent.js";
10
+ export * from "./skill-write-plan.js";
10
11
  export * from "./skill-write-service.js";
12
+ export * from "./system-skill-writer.js";
11
13
  export * from "./skill-target-locks.js";
12
14
  export * from "./skill-root-gate.js";
13
15
  export * from "./skill-row-cells.js";
@@ -0,0 +1,21 @@
1
+ /**
2
+ * System-owned skill writes: the same provider, without session observations.
3
+ *
4
+ * Curator/merge/undo callers build a plan with an explicit snapshot/anchor; the
5
+ * write still takes the provider's target lock, unit transaction and audit.
6
+ *
7
+ * @module @lmzhen/dsh-evolution-core/system-skill-writer
8
+ */
9
+ import type { SkillWritePlan, SkillWriteResult } from './skill-write-plan.ts';
10
+ import type { SkillLibrary } from './skill-store.ts';
11
+ export declare class SystemSkillWriter {
12
+ private readonly library;
13
+ constructor(library: SkillLibrary);
14
+ /**
15
+ * Apply one explicit plan through the single provider.
16
+ * @param plan - origin, ops and the caller's snapshot anchors.
17
+ * @returns the provider result; no observation is recorded.
18
+ */
19
+ apply(plan: SkillWritePlan): Promise<SkillWriteResult>;
20
+ }
21
+ //# sourceMappingURL=system-skill-writer.d.ts.map
@@ -0,0 +1,15 @@
1
+ export class SystemSkillWriter {
2
+ library;
3
+ constructor(library) {
4
+ this.library = library;
5
+ }
6
+ /**
7
+ * Apply one explicit plan through the single provider.
8
+ * @param plan - origin, ops and the caller's snapshot anchors.
9
+ * @returns the provider result; no observation is recorded.
10
+ */
11
+ async apply(plan) {
12
+ return await this.library.apply(plan);
13
+ }
14
+ }
15
+ //# sourceMappingURL=system-skill-writer.js.map
package/lib/write.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { r as contentHash } from "./mutations-Y-wKod7W.js";
2
2
  import { p as entryTarget } from "./skill-history-BvLWFoOo.js";
3
- import { C as writeUnitEvidence, D as fileMutationLock, E as MutationUnit, O as validateMutationRequest, S as upsertUnitIndex, T as writeUnitSnapshot, _ as unitIndexPath, a as UNIT_JOURNAL_SCHEMA_VERSION, b as unitSnapshotsDir, c as unitJournalPath, d as UNIT_STORAGE_SCHEMA_VERSION, f as readUnitEvidence, g as unitEvidencePath, h as readUnitSnapshot, i as UNIT_JOURNAL_PHASES, k as WriteKernel, l as unitJournalPhaseRank, m as readUnitReceipt, n as unitJournalPort, o as assertUnitJournalTransition, p as readUnitIndex, r as unitSnapshotPort, s as readUnitJournal, t as pruneTreeChangeUnits, u as writeUnitJournal, v as unitReceiptPath, w as writeUnitReceipt, x as unitStorageKey, y as unitSnapshotPath } from "./mutation-storage-adapter-z1_hyrQn.js";
3
+ import { C as writeUnitEvidence, D as fileMutationLock, E as MutationUnit, O as validateMutationRequest, S as upsertUnitIndex, T as writeUnitSnapshot, _ as unitIndexPath, a as UNIT_JOURNAL_SCHEMA_VERSION, b as unitSnapshotsDir, c as unitJournalPath, d as UNIT_STORAGE_SCHEMA_VERSION, f as readUnitEvidence, g as unitEvidencePath, h as readUnitSnapshot, i as UNIT_JOURNAL_PHASES, k as WriteKernel, l as unitJournalPhaseRank, m as readUnitReceipt, n as unitJournalPort, o as assertUnitJournalTransition, p as readUnitIndex, r as unitSnapshotPort, s as readUnitJournal, t as pruneTreeChangeUnits, u as writeUnitJournal, v as unitReceiptPath, w as writeUnitReceipt, x as unitStorageKey, y as unitSnapshotPath } from "./mutation-storage-adapter-Cq1bmS1E.js";
4
4
  //#region lib/types/unit-undo.js
5
5
  /**
6
6
  * Cross-skill unit undo executor.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@lmzhen/dsh-evolution-core",
3
3
  "description": "Shared stores, prompts, signals and lifecycle logic for the dsh-evolution plugin family (community build)",
4
- "version": "0.59.5",
4
+ "version": "0.59.6",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -98,8 +98,8 @@
98
98
  "yaml": "^2.4.2",
99
99
  "zod": "^4.4.3",
100
100
  "@deepseek-ai/schemastery": "^3.18.5",
101
- "@lmzhen/dsh-evolution-adapter": "0.59.5",
102
- "@lmzhen/dsh-evolution-platform-contract": "0.59.5"
101
+ "@lmzhen/dsh-evolution-adapter": "0.59.6",
102
+ "@lmzhen/dsh-evolution-platform-contract": "0.59.6"
103
103
  },
104
104
  "peerDependencies": {
105
105
  "@deepseek-ai/cordis": "^4.0.5"