@lmzhen/dsh-evolution-core 0.40.1 → 0.42.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 (62) hide show
  1. package/lib/composition/index.js +2 -2
  2. package/lib/{composition-dy0gJWDd.js → composition-CVafrlEw.js} +2 -2
  3. package/lib/{ensure-B4fjEI0w.js → ensure-BjToTMMj.js} +2 -2
  4. package/lib/errors-BxzLTVbS.js +399 -0
  5. package/lib/guards-3f02mnf2.js +29 -0
  6. package/lib/http-routes.js +205 -1
  7. package/lib/index.js +3588 -15049
  8. package/lib/mutations-jUuOPaCn.js +718 -0
  9. package/lib/platform/index.js +2 -2
  10. package/lib/{platform-CFp373_0.js → platform-B06_h8t-.js} +4 -1
  11. package/lib/restrictor.js +1 -1
  12. package/lib/runtime/plugin.js +2 -2
  13. package/lib/{session-projection-CXUNPkq9.js → session-projection-EB7mUOlJ.js} +63 -63
  14. package/lib/skill-health-BLGZTIv8.js +3773 -0
  15. package/lib/skill-history-_Cssts5o.js +1912 -0
  16. package/lib/skills.js +4067 -0
  17. package/lib/subagent/index.js +1 -1
  18. package/lib/types/errors.d.ts +12 -0
  19. package/lib/types/errors.js +17 -0
  20. package/lib/types/generated/errors.localized.d.ts +320 -0
  21. package/lib/types/generated/errors.localized.js +318 -0
  22. package/lib/types/home-fence.d.ts +7 -0
  23. package/lib/types/home-fence.js +58 -3
  24. package/lib/types/index.d.ts +1 -18
  25. package/lib/types/index.js +1 -18
  26. package/lib/types/io.js +1 -1
  27. package/lib/types/mutation-authority.d.ts +14 -37
  28. package/lib/types/mutation-authority.js +16 -103
  29. package/lib/types/mutation-storage-adapter.js +9 -6
  30. package/lib/types/mutation-unit.d.ts +14 -0
  31. package/lib/types/mutation-unit.js +30 -10
  32. package/lib/types/opt-in.js +1 -1
  33. package/lib/types/platform/index.d.ts +8 -1
  34. package/lib/types/platform/index.js +3 -0
  35. package/lib/types/review-channel.js +3 -1
  36. package/lib/types/review-context.js +3 -1
  37. package/lib/types/run-registry.d.ts +2 -24
  38. package/lib/types/scope.js +1 -1
  39. package/lib/types/signals.d.ts +1 -1
  40. package/lib/types/skill-store.d.ts +7 -1
  41. package/lib/types/skill-store.js +43 -80
  42. package/lib/types/skills.d.ts +11 -0
  43. package/lib/types/skills.js +11 -0
  44. package/lib/types/subagent/types.d.ts +3 -3
  45. package/lib/types/tree-lock.js +4 -1
  46. package/lib/types/unit-claim.d.ts +8 -0
  47. package/lib/types/unit-claim.js +3 -0
  48. package/lib/types/unit-journal.d.ts +3 -1
  49. package/lib/types/unit-journal.js +12 -3
  50. package/lib/types/unit-storage.d.ts +1 -0
  51. package/lib/types/write-gates.d.ts +1 -1
  52. package/lib/types/write-gates.js +3 -1
  53. package/lib/types/write.d.ts +11 -0
  54. package/lib/types/write.js +11 -0
  55. package/lib/write.js +357 -0
  56. package/package.json +16 -4
  57. package/lib/errors-DUcEoTLR.js +0 -58
  58. package/lib/http-routes-_anZVlPf.js +0 -233
  59. package/lib/types/unit-backend.d.ts +0 -5
  60. package/lib/types/unit-backend.js +0 -55
  61. package/lib/types/unit-transaction.d.ts +0 -225
  62. package/lib/types/unit-transaction.js +0 -501
@@ -545,28 +545,15 @@ export class SkillLibrary {
545
545
  return { moves, sources: roots.map(skill => ({ skill })) };
546
546
  }
547
547
  async withMutationLock(task) {
548
- let lease;
549
- try {
550
- lease = await this.lockPort().acquire();
551
- }
552
- catch (error) {
553
- const message = error instanceof Error ? error.message : String(error);
554
- if (message.startsWith('mutation-lock-busy'))
555
- throw error;
556
- // Coordination failure: the per-file transact/serial path remains the
557
- // fallback; a lock sidecar must not change the mutation's business result.
558
- return await task();
559
- }
548
+ // Fail closed: a missing authority/lock is an assembly error, not a reason to run
549
+ // the write outside the fence. The old catch-and-fallback inverted this guard: the
550
+ // "refusing an unfenced production write" error itself took the fallback path.
551
+ const lease = await this.lockPort().acquire();
560
552
  try {
561
553
  return await task();
562
554
  }
563
555
  finally {
564
- try {
565
- await lease.release();
566
- }
567
- catch {
568
- // A failed lock cleanup is a coordination degradation, not a write failure.
569
- }
556
+ await lease.release();
570
557
  }
571
558
  }
572
559
  lockPort() {
@@ -1550,7 +1537,7 @@ export class SkillLibrary {
1550
1537
  const unit = new MutationUnit({
1551
1538
  io: this.io,
1552
1539
  limits: { maxSkillFileBytes: Number.MAX_SAFE_INTEGER, maxSkillContentChars: Number.MAX_SAFE_INTEGER },
1553
- lock: { acquire: async () => ({ release: async () => { } }) },
1540
+ nested: true,
1554
1541
  journal: unitJournalPort(this.io, dirname(this.root)),
1555
1542
  snapshots: unitSnapshotPort(this.io, dirname(this.root)),
1556
1543
  transactGuard: false,
@@ -2144,7 +2131,9 @@ export class SkillLibrary {
2144
2131
  * Every failure rolls back what already landed; a rollback failure names the
2145
2132
  * unrestored skills instead of claiming success.
2146
2133
  */
2147
- async commitMerge(plan) {
2134
+ /** Expose the store's writer queue so a caller can acquire it BEFORE the home fence. */
2135
+ async runSerial(task) { return await this.serial(task); }
2136
+ async commitMerge(plan, options) {
2148
2137
  const target = plan.target.trim();
2149
2138
  const sources = [...new Set(plan.sources.map(name => name.trim()))].filter(name => name !== target);
2150
2139
  if (sources.length === 0)
@@ -2160,7 +2149,7 @@ export class SkillLibrary {
2160
2149
  if (protectedSource !== undefined)
2161
2150
  return { ok: false, message: `来源技能「${protectedSource}」是受保护的内置技能。` };
2162
2151
  const limits = plan.limits ?? this.limits;
2163
- return await this.serial(async () => {
2152
+ const run = async () => {
2164
2153
  const dir = this.dirOf(target);
2165
2154
  const targetPath = join(dir, 'SKILL.md');
2166
2155
  if (!plan.targetExists && await this.io.exists(dir)) {
@@ -2312,6 +2301,7 @@ export class SkillLibrary {
2312
2301
  name: target,
2313
2302
  origin: plan.origin,
2314
2303
  protection: 'write',
2304
+ nested: plan.nested === true,
2315
2305
  writes,
2316
2306
  auditAction: 'merge-rollback',
2317
2307
  auditSummary: 'merge rollback: restore target snapshot',
@@ -2348,6 +2338,7 @@ export class SkillLibrary {
2348
2338
  name: target,
2349
2339
  origin: plan.origin,
2350
2340
  protection: 'write',
2341
+ nested: plan.nested === true,
2351
2342
  writes,
2352
2343
  auditAction: 'merge',
2353
2344
  auditSummary: plan.summary,
@@ -2458,6 +2449,7 @@ export class SkillLibrary {
2458
2449
  name: target,
2459
2450
  origin: plan.origin,
2460
2451
  protection: 'write',
2452
+ nested: plan.nested === true,
2461
2453
  writes,
2462
2454
  auditAction: 'merge',
2463
2455
  auditSummary: plan.summary,
@@ -2477,7 +2469,8 @@ export class SkillLibrary {
2477
2469
  await plan.onPhase?.('sources-archived');
2478
2470
  await plan.onPhase?.('unit-written');
2479
2471
  return { ok: true, message: `已把 ${sources.join(', ')} 合并进「${target}」。`, unitId, targetPath: dir, archived, mutations: 1 + sources.length };
2480
- });
2472
+ };
2473
+ return options?.serialized === true ? await run() : await this.serial(run);
2481
2474
  }
2482
2475
  /**
2483
2476
  * V2 (design §16.7): what an apply-mode consolidation would do for ONE source,
@@ -3157,63 +3150,32 @@ export class SkillLibrary {
3157
3150
  const rel = relative(owner.dir, op.target).split(String.fromCharCode(92)).join('/');
3158
3151
  landing.push({ kind: op.kind, target: op.target, content: op.kind === 'write' ? op.content : null, previous, expected: op.expected, skill, dir: owner.dir, rel });
3159
3152
  }
3160
- const written = [];
3161
- let durabilityWarning = '';
3162
- try {
3163
- for (const entry of landing) {
3164
- try {
3165
- if (this.transact) {
3166
- const baseline = entry.expected === undefined ? entry.previous : entry.expected;
3167
- const drift = { seen: false };
3168
- const ran = { done: false };
3169
- await this.transact(this.io, entry.target, (current) => {
3170
- drift.seen = false;
3171
- ran.done = true;
3172
- if (current !== baseline) {
3173
- drift.seen = true;
3174
- return current;
3175
- }
3176
- return entry.kind === 'delete' ? null : entry.content;
3177
- });
3178
- if (!ran.done)
3179
- throw new Error('internal error: the write transaction for ' + entry.target + ' did not invoke the task; nothing was written');
3180
- if (drift.seen)
3181
- throw new Error('concurrent modification detected: ' + entry.target + ' changed after the plan was computed (a concurrent writer won the race); no further writes were performed');
3182
- }
3183
- else if (entry.kind === 'delete') {
3184
- await this.io.remove(entry.target);
3185
- }
3186
- else {
3187
- await this.io.writeText(entry.target, entry.content ?? '');
3188
- }
3189
- }
3190
- catch (error) {
3191
- if (!isCommittedOnly(error))
3192
- throw error;
3193
- durabilityWarning = error instanceof Error ? error.message : String(error);
3194
- }
3195
- written.push({ target: entry.target, previous: entry.previous });
3196
- }
3197
- }
3198
- catch (error) {
3199
- const stuck = [];
3200
- for (const entry of written.reverse()) {
3201
- try {
3202
- if (entry.previous === null)
3203
- await this.io.remove(entry.target);
3204
- else
3205
- await this.io.writeText(entry.target, entry.previous);
3206
- }
3207
- catch {
3208
- stuck.push(entry.target);
3209
- }
3210
- }
3211
- const reason = error instanceof Error ? error.message : String(error);
3212
- if (stuck.length === 0)
3213
- return { ok: false, message: '目录树变更失败,已回滚:' + reason };
3214
- return { ok: false, message: '目录树变更失败:' + reason + '。回滚无法恢复 ' + stuck.join(', ') + ':可恢复性由当前快照决定:' + (await this.snapshotRecoveryAvailability()) + ' —— 若上面不是「最新快照」,请重新应用这次变更。' };
3215
- }
3216
3153
  const unitId = plan.unitId ?? 'u-' + Date.now().toString(36) + '-' + (nextUnitSeq += 1).toString(36);
3154
+ const unit = new MutationUnit({
3155
+ io: this.io,
3156
+ limits: this.limits,
3157
+ ...(plan.nested === true ? { nested: true } : { lock: this.lockPort() }),
3158
+ journal: unitJournalPort(this.io, dirname(this.root)),
3159
+ snapshots: unitSnapshotPort(this.io, dirname(this.root)),
3160
+ ...(this.transact === undefined ? {} : { transact: this.transact }),
3161
+ now: () => Date.now(),
3162
+ });
3163
+ const applied = await unit.apply({
3164
+ name: primaryName,
3165
+ roots: roots.map(root => ({ name: root.name, protection: root.protection })),
3166
+ ops: landing.map(entry => entry.kind === 'delete'
3167
+ ? { kind: 'delete', target: entry.target, ...(entry.expected === undefined ? {} : { expected: entry.expected }) }
3168
+ : { kind: 'write', target: entry.target, content: entry.content ?? '', ...(entry.expected === undefined ? {} : { expected: entry.expected }) }),
3169
+ rootDirs: Object.fromEntries(roots.map(root => [root.name, root.dir])),
3170
+ origin: plan.origin,
3171
+ auditAction: plan.auditAction,
3172
+ auditSummary: plan.auditSummary,
3173
+ eventAction: plan.eventAction,
3174
+ unitId,
3175
+ });
3176
+ if (!applied.ok) {
3177
+ return { ok: false, message: '目录树变更失败:' + applied.errors.map(error => error.message).join('; ') };
3178
+ }
3217
3179
  const defaultHead = {
3218
3180
  moves: landing
3219
3181
  .filter(entry => basename(entry.target) !== 'SKILL.md')
@@ -3239,11 +3201,12 @@ export class SkillLibrary {
3239
3201
  this.notifyMutation({ action: plan.eventAction, name: skill, skillDir: dir });
3240
3202
  }
3241
3203
  const primaryDir = roots.find(root => root.name === primaryName)?.dir ?? roots[0]?.dir ?? '';
3204
+ const warnings = applied.warnings ?? [];
3242
3205
  return {
3243
3206
  ok: true,
3244
- message: durabilityWarning === ''
3207
+ message: warnings.length === 0
3245
3208
  ? plan.eventAction + ' "' + primaryName + '" succeeded.'
3246
- : plan.eventAction + ' "' + primaryName + '" succeeded (警告:写入已落盘,但目录 fsync 失败 —— 持久性未确认:' + durabilityWarning + ')',
3209
+ : plan.eventAction + ' "' + primaryName + '" succeeded (警告:' + warnings.join('; ') + ')',
3247
3210
  path: primaryDir,
3248
3211
  };
3249
3212
  }
@@ -0,0 +1,11 @@
1
+ /** Skill-domain surface. Consumers import it from the documented subpath. */
2
+ export * from './skill-lint.ts';
3
+ export * from './skill-health.ts';
4
+ export * from './skill-reads.ts';
5
+ export * from './skill-row-cells.ts';
6
+ export * from './skill-history.ts';
7
+ export * from './skill-store.ts';
8
+ export * from './skill-model.ts';
9
+ export * from './skill-revision.ts';
10
+ export * from './frontmatter.ts';
11
+ //# sourceMappingURL=skills.d.ts.map
@@ -0,0 +1,11 @@
1
+ /** Skill-domain surface. Consumers import it from the documented subpath. */
2
+ export * from "./skill-lint.js";
3
+ export * from "./skill-health.js";
4
+ export * from "./skill-reads.js";
5
+ export * from "./skill-row-cells.js";
6
+ export * from "./skill-history.js";
7
+ export * from "./skill-store.js";
8
+ export * from "./skill-model.js";
9
+ export * from "./skill-revision.js";
10
+ export * from "./frontmatter.js";
11
+ //# sourceMappingURL=skills.js.map
@@ -1,6 +1,6 @@
1
- import type { Agent, AgentOptions } from '@deepseek-ai/dsh-agent';
2
- import type { SessionId } from '@deepseek-ai/dsh-session';
3
- import type { SubagentStopReason } from '@deepseek-ai/dsh-subagent';
1
+ import type { Agent, AgentOptions } from '../platform/index.ts';
2
+ import type { SessionId } from '../platform/index.ts';
3
+ import type { SubagentStopReason } from '../platform/index.ts';
4
4
  import type { ObjectJsonSchema } from '../platform/index.ts';
5
5
  export type ChannelCode = 'E-323' | 'E-324';
6
6
  export type ChannelKind = 'reviewer' | 'maintainer';
@@ -89,11 +89,14 @@ export async function acquireTreeWriteLock(options) {
89
89
  async release() {
90
90
  if (released)
91
91
  return;
92
- released = true;
93
92
  await transactIo(options.io, path, (current) => {
94
93
  const existing = parseLock(current);
95
94
  return existing?.token === token && existing.epoch === epoch ? null : current;
96
95
  });
96
+ // Mark released only after the delete committed. Marking first made every retry a
97
+ // silent no-op, so a transient unlink failure left the lock on disk while the
98
+ // caller believed it was released.
99
+ released = true;
97
100
  },
98
101
  };
99
102
  return { ok: true, lock };
@@ -0,0 +1,8 @@
1
+ /** Shared target-claim port: one live unit per target across processes. */
2
+ export interface UnitClaim {
3
+ release(): Promise<void>;
4
+ }
5
+ export interface UnitClaimPort {
6
+ acquire(target: string, runId: string): Promise<UnitClaim>;
7
+ }
8
+ //# sourceMappingURL=unit-claim.d.ts.map
@@ -0,0 +1,3 @@
1
+ /** Shared target-claim port: one live unit per target across processes. */
2
+ export {};
3
+ //# sourceMappingURL=unit-claim.js.map
@@ -1,6 +1,6 @@
1
1
  import type { EvolutionIoLike } from './io.ts';
2
2
  export declare const UNIT_JOURNAL_SCHEMA_VERSION = 1;
3
- export declare const UNIT_JOURNAL_PHASES: readonly ["prepared", "target-pending", "target-written", "source-archiving", "sources-archived", "support-pending", "unit-written", "committed", "failed", "rolled-back"];
3
+ export declare const UNIT_JOURNAL_PHASES: readonly ["prepared", "target-pending", "target-written", "source-archiving", "support-pending", "sources-archived", "unit-written", "committed", "failed", "rolled-back"];
4
4
  export type UnitJournalPhase = typeof UNIT_JOURNAL_PHASES[number];
5
5
  export interface UnitJournalCheckpoint {
6
6
  readonly source: string;
@@ -11,6 +11,8 @@ export interface UnitJournalCheckpoint {
11
11
  export interface UnitJournal {
12
12
  readonly schemaVersion: typeof UNIT_JOURNAL_SCHEMA_VERSION;
13
13
  readonly unitId: string;
14
+ /** Nested journal key; unitId stays the user-visible identity. */
15
+ readonly journalId?: string;
14
16
  readonly runId: string;
15
17
  readonly planDigest: string;
16
18
  readonly target: string;
@@ -7,8 +7,8 @@ export const UNIT_JOURNAL_PHASES = [
7
7
  'target-pending',
8
8
  'target-written',
9
9
  'source-archiving',
10
- 'sources-archived',
11
10
  'support-pending',
11
+ 'sources-archived',
12
12
  'unit-written',
13
13
  'committed',
14
14
  'failed',
@@ -22,15 +22,24 @@ export function unitJournalPhaseRank(phase) {
22
22
  return UNIT_JOURNAL_PHASES.indexOf(phase);
23
23
  }
24
24
  export function assertUnitJournalTransition(from, to) {
25
+ if (from === to)
26
+ return;
27
+ // A committed unit is final: a late failure must never rewrite its journal.
28
+ if (from === 'committed')
29
+ throw new Error('unit-journal-terminal:' + from);
25
30
  if (to === 'failed' || to === 'rolled-back')
26
31
  return;
27
- if (from === 'failed' || from === 'rolled-back' || from === 'committed')
32
+ if (from === 'failed' || from === 'rolled-back')
28
33
  throw new Error('unit-journal-terminal:' + from);
29
34
  if (unitJournalPhaseRank(to) <= unitJournalPhaseRank(from))
30
35
  throw new Error('unit-journal-nonmonotonic:' + from + '->' + to);
31
36
  }
32
37
  export async function writeUnitJournal(io, home, journal, scope = 'merge') {
33
- const path = unitJournalPath(home, journal.unitId, scope);
38
+ const key = journal.journalId ?? journal.unitId;
39
+ const path = unitJournalPath(home, key, scope);
40
+ const existing = await readUnitJournal(io, home, key, scope);
41
+ if (existing !== null)
42
+ assertUnitJournalTransition(existing.phase, journal.phase);
34
43
  const content = JSON.stringify(journal, null, 2) + '\n';
35
44
  if (io.transact !== undefined)
36
45
  await io.transact(path, () => content);
@@ -3,6 +3,7 @@ export declare const UNIT_STORAGE_SCHEMA_VERSION = 1;
3
3
  export interface UnitSnapshot {
4
4
  readonly schemaVersion: typeof UNIT_STORAGE_SCHEMA_VERSION;
5
5
  readonly unitId: string;
6
+ readonly journalId?: string;
6
7
  readonly planDigest: string;
7
8
  readonly runId: string;
8
9
  readonly target: string;
@@ -24,7 +24,7 @@
24
24
  */
25
25
  import { type WriteConfirm, type WriteConfirmMode } from './write-authority.ts';
26
26
  import type { ConfirmContext } from './write-authority.ts';
27
- import { type WriteOrigin } from './index.ts';
27
+ import type { WriteOrigin } from './skill-store.ts';
28
28
  /** The point whose verdict is being taken. */
29
29
  export type WriteGatePoint = 'admission' | 'execution';
30
30
  /** What every gate reads. */
@@ -23,7 +23,9 @@
23
23
  * @module @lmzhen/dsh-evolution-core/write-gates
24
24
  */
25
25
  import { DEFAULT_WRITE_CONFIRM_MODE, DEFAULT_WRITE_CONFIRM_TIMEOUT_SECONDS } from "./write-authority.js";
26
- import { SKILL_ACTION_REQUIRED_FIELDS, errorText, isUnreadWrite } from "./index.js";
26
+ import { SKILL_ACTION_REQUIRED_FIELDS } from "./constants.js";
27
+ import { errorText } from "./errors.js";
28
+ import { isUnreadWrite } from "./skill-reads.js";
27
29
  /** The confirm question's stable id — one question per write, so an answer to an earlier prompt
28
30
  * can never be read as the answer to a later one. */
29
31
  const CONFIRM_QUESTION_ID = 'evolution-skill-write';
@@ -0,0 +1,11 @@
1
+ /** The one byte-write engine surface. Consumers must import it from the documented subpath. */
2
+ export * from './write-kernel.ts';
3
+ export * from './mutation-unit.ts';
4
+ export * from './mutation-storage-adapter.ts';
5
+ export * from './unit-claim.ts';
6
+ export * from './unit-journal.ts';
7
+ export * from './unit-storage.ts';
8
+ export * from './unit-undo.ts';
9
+ export * from './unit-undo-plan.ts';
10
+ export * from './unit-members.ts';
11
+ //# sourceMappingURL=write.d.ts.map
@@ -0,0 +1,11 @@
1
+ /** The one byte-write engine surface. Consumers must import it from the documented subpath. */
2
+ export * from "./write-kernel.js";
3
+ export * from "./mutation-unit.js";
4
+ export * from "./mutation-storage-adapter.js";
5
+ export * from "./unit-claim.js";
6
+ export * from "./unit-journal.js";
7
+ export * from "./unit-storage.js";
8
+ export * from "./unit-undo.js";
9
+ export * from "./unit-undo-plan.js";
10
+ export * from "./unit-members.js";
11
+ //# sourceMappingURL=write.js.map