@aiwayds/dsh-model-sync 0.12.1 → 0.13.1

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 (88) hide show
  1. package/README.md +18 -17
  2. package/README.zh-CN.md +18 -17
  3. package/lib/index.d.ts +54 -384
  4. package/lib/index.js +306 -1107
  5. package/lib/index.js.map +1 -1
  6. package/lib/official-adapter.d.ts +80 -0
  7. package/lib/official-adapter.js +34 -0
  8. package/lib/official-adapter.js.map +1 -0
  9. package/lib/official-migrate.d.ts +122 -0
  10. package/lib/official-migrate.js +653 -0
  11. package/lib/official-migrate.js.map +1 -0
  12. package/lib/official-scheduler.d.ts +72 -0
  13. package/lib/official-scheduler.js +195 -0
  14. package/lib/official-scheduler.js.map +1 -0
  15. package/lib/official-store.d.ts +41 -0
  16. package/lib/official-store.js +84 -0
  17. package/lib/official-store.js.map +1 -0
  18. package/lib/official-sync.d.ts +68 -0
  19. package/lib/official-sync.js +274 -0
  20. package/lib/official-sync.js.map +1 -0
  21. package/lib/opencode-go-adapter.d.ts +56 -0
  22. package/lib/opencode-go-adapter.js +455 -0
  23. package/lib/opencode-go-adapter.js.map +1 -0
  24. package/lib/opencode-go-patch-file.d.ts +11 -0
  25. package/lib/opencode-go-patch-file.js +175 -11
  26. package/lib/opencode-go-patch-file.js.map +1 -1
  27. package/lib/opencode-go-source.d.ts +1 -1
  28. package/lib/opencode-go-source.js +1 -1
  29. package/locale/en.json +1 -1
  30. package/locale/zh.json +1 -1
  31. package/package.json +3 -3
  32. package/src/index.ts +352 -1281
  33. package/src/official-adapter.ts +111 -0
  34. package/src/official-migrate.ts +863 -0
  35. package/src/official-scheduler.ts +235 -0
  36. package/src/official-store.ts +122 -0
  37. package/src/official-sync.ts +371 -0
  38. package/src/opencode-go-adapter.ts +494 -0
  39. package/src/opencode-go-patch-file.ts +175 -9
  40. package/src/opencode-go-source.ts +1 -1
  41. package/lib/opencode-go-diagnose.d.ts +0 -96
  42. package/lib/opencode-go-diagnose.js +0 -177
  43. package/lib/opencode-go-diagnose.js.map +0 -1
  44. package/lib/opencode-go-grouping.d.ts +0 -7
  45. package/lib/opencode-go-grouping.js +0 -21
  46. package/lib/opencode-go-grouping.js.map +0 -1
  47. package/lib/opencode-go-host-catalog.d.ts +0 -42
  48. package/lib/opencode-go-host-catalog.js +0 -98
  49. package/lib/opencode-go-host-catalog.js.map +0 -1
  50. package/lib/opencode-go-journal.d.ts +0 -114
  51. package/lib/opencode-go-journal.js +0 -406
  52. package/lib/opencode-go-journal.js.map +0 -1
  53. package/lib/opencode-go-migrate.d.ts +0 -378
  54. package/lib/opencode-go-migrate.js +0 -1529
  55. package/lib/opencode-go-migrate.js.map +0 -1
  56. package/lib/opencode-go-migration.d.ts +0 -270
  57. package/lib/opencode-go-migration.js +0 -473
  58. package/lib/opencode-go-migration.js.map +0 -1
  59. package/lib/opencode-go-one-shot.d.ts +0 -282
  60. package/lib/opencode-go-one-shot.js +0 -1131
  61. package/lib/opencode-go-one-shot.js.map +0 -1
  62. package/lib/opencode-go-planner.d.ts +0 -168
  63. package/lib/opencode-go-planner.js +0 -407
  64. package/lib/opencode-go-planner.js.map +0 -1
  65. package/lib/opencode-go-scheduler.d.ts +0 -128
  66. package/lib/opencode-go-scheduler.js +0 -241
  67. package/lib/opencode-go-scheduler.js.map +0 -1
  68. package/lib/opencode-go-store.d.ts +0 -18
  69. package/lib/opencode-go-store.js +0 -68
  70. package/lib/opencode-go-store.js.map +0 -1
  71. package/lib/opencode-go-sync.d.ts +0 -107
  72. package/lib/opencode-go-sync.js +0 -388
  73. package/lib/opencode-go-sync.js.map +0 -1
  74. package/lib/opencode-go-writer.d.ts +0 -148
  75. package/lib/opencode-go-writer.js +0 -1061
  76. package/lib/opencode-go-writer.js.map +0 -1
  77. package/src/opencode-go-diagnose.ts +0 -276
  78. package/src/opencode-go-grouping.ts +0 -26
  79. package/src/opencode-go-host-catalog.ts +0 -125
  80. package/src/opencode-go-journal.ts +0 -460
  81. package/src/opencode-go-migrate.ts +0 -1951
  82. package/src/opencode-go-migration.ts +0 -666
  83. package/src/opencode-go-one-shot.ts +0 -1399
  84. package/src/opencode-go-planner.ts +0 -582
  85. package/src/opencode-go-scheduler.ts +0 -315
  86. package/src/opencode-go-store.ts +0 -73
  87. package/src/opencode-go-sync.ts +0 -541
  88. package/src/opencode-go-writer.ts +0 -1301
@@ -1,378 +0,0 @@
1
- import { type GoRouteSlot } from './opencode-go-grouping.ts';
2
- import { type GoManagedModel, type GoPlannedRouteFields } from './opencode-go-planner.ts';
3
- import type { GoModelRecord } from './opencode-go-source.ts';
4
- import type { SettingsModelProfile } from './translate.ts';
5
- import { type MigrationRecord, type MigrationState, type MigrationStateStore, type RouteWriterConfig, type UnclaimedMigrationRecord } from './opencode-go-migration.ts';
6
- import type { GoOwnershipStore } from './opencode-go-writer.ts';
7
- import type { GoJournalStore } from './opencode-go-journal.ts';
8
- import type { Logger, SettingsDescriptor, SettingsService } from './writer.ts';
9
- /** What apply will do with one route. */
10
- export type MigrationAction = 'create' | 'update' | 'keep' | 'unset';
11
- export interface MigrationIdClass {
12
- id: string;
13
- kind: MigrationIdKind;
14
- /** Conservative (adopt=false) keeps every existing id; adopt may move/delete. */
15
- action: 'keep' | 'move' | 'delete' | 'adopt';
16
- reason: string;
17
- }
18
- export interface MigrationDeletion {
19
- route: string;
20
- id: string;
21
- kind: MigrationIdKind;
22
- /** The three deletion gates (§6.2.4 / ADR-005), reported with the reason. */
23
- gates: {
24
- managedHash: string;
25
- override: string;
26
- pinned: string;
27
- };
28
- /** Only an explicitly confirmed id in adopt mode is actually deleted. */
29
- willDelete: boolean;
30
- reason: string;
31
- }
32
- /**
33
- * One row of the plan's per-id action table (design §14.2 / 31 B2). This is the
34
- * SINGLE source of truth consumed by apply: it is hashed into planHash, so a
35
- * plan and its apply can never disagree. `keep` rows are never destructive.
36
- */
37
- export type MigrationIdActionOp = 'keep' | 'add' | 'update' | 'move' | 'delete';
38
- export interface MigrationIdPlan {
39
- id: string;
40
- op: MigrationIdActionOp;
41
- /** Route the op operates on (source route for `move`/`delete`). */
42
- route: string;
43
- /** Destination route for `move` (the id is removed from `route`). */
44
- to?: string;
45
- kind: MigrationIdKind;
46
- /** True when the id is in pinnedModels — forced `keep`, never moved/deleted. */
47
- pinned: boolean;
48
- /** Exact profile to write for add/move. Set on `update` rows as the official
49
- * target only for the plan's before/after report — apply never writes it. */
50
- profile?: SettingsModelProfile;
51
- /** Current profile on an `update` row (user-modified) — for before/after output. */
52
- before?: SettingsModelProfile;
53
- /** True iff applying this row puts the id under plugin ownership (managed ledger).
54
- * `update` and user-owned `keep` rows are false by construction (B6 + user ruling). */
55
- managed?: boolean;
56
- reason: string;
57
- }
58
- /**
59
- * Classification of one id that exists in the current route (design §14.2).
60
- * 0.12.1 adds `host-catalog-lag`: an OFFICIAL id the host runtime catalog does
61
- * not know (the host would reject any route write carrying it — fail-closed;
62
- * incident migrate-go 20261129-114103: beam-free).
63
- */
64
- export type MigrationIdKind =
65
- /** Official desired id — always targets the single managed route (0.12.0). */
66
- 'desired' | 'legacy-other-protocol' | 'unknown-retired' | 'user-extra'
67
- /** Public catalog unavailable (offline/401/403) — fail-closed, never deleted (§14.2). */
68
- | 'unclassified'
69
- /** Official but absent from the host runtime catalog (0.12.1): not written;
70
- * plugin-written leftovers are removed to keep the route writable. */
71
- | 'host-catalog-lag';
72
- export type RewriteRefMode = 'none' | 'default-model' | 'config';
73
- /** A reference candidate reported by plan. Only verified config keys may be rewritten. */
74
- export interface ReferenceCandidate {
75
- surface: string;
76
- location: string;
77
- before: string;
78
- after: string;
79
- verified: boolean;
80
- rewritable: boolean;
81
- /** Session storage / external config: always report-only (never rewritten). */
82
- reportOnly: boolean;
83
- }
84
- /** A rewrite request that an injected patch accessor can execute (opt-in only). */
85
- export interface ReferenceRewrite {
86
- surface: string;
87
- location: string;
88
- before: string;
89
- after: string;
90
- }
91
- /** The hashed body of the plan. `planHash`/`confirmCode` are derived, not hashed. */
92
- export interface MigrationPlanBody {
93
- version: 1;
94
- keyFingerprint: string;
95
- adopt: boolean;
96
- rewriteRefs: RewriteRefMode;
97
- /** Fingerprint of the pre-apply go sections — apply refuses if it changed. */
98
- preFingerprint: string;
99
- state: MigrationState;
100
- stateByRoute: Record<string, MigrationState>;
101
- routes: MigrationPlannedRoute[];
102
- deletions: MigrationDeletion[];
103
- /** The complete per-id action table apply executes verbatim (31 B2). */
104
- idActions: MigrationIdPlan[];
105
- /** Fixed B6 informed-consent text; hashed into planHash so edits invalidate it. */
106
- managedAfterMigration: string;
107
- /** True when the official public catalog was unavailable at plan time. Every
108
- * non-desired id is then `unclassified` and never deleted (fail-closed). */
109
- publicCatalogAvailable: boolean;
110
- /** True when apply cannot guarantee verbatim action-table execution: plan mode
111
- * is `adopt-disabled` and `--adopt` is rejected at the command layer (§14.2). */
112
- adoptDisabled: boolean;
113
- /**
114
- * 0.12.1 host-catalog intersection. `true` when the host runtime catalog ids
115
- * were supplied; `hostCatalogLagIds` lists the OFFICIAL ids they exclude from
116
- * the desired write (the host rejects any route carrying them — fail-closed).
117
- * `false` (including the explicit null fail-closed input) keeps the 0.12.0
118
- * full-listing desired set and an empty lag list.
119
- */
120
- hostCatalogAvailable: boolean;
121
- /** Official ids excluded from the desired write because the host runtime catalog does not know them. */
122
- hostCatalogLagIds: string[];
123
- references: ReferenceCandidate[];
124
- pinnedSuggestions: string[];
125
- backupPath: string;
126
- rollbackCommand: string;
127
- unverified: string[];
128
- }
129
- export interface MigrationPlan extends MigrationPlanBody {
130
- planHash: string;
131
- confirmCode: string;
132
- }
133
- export interface MigrationPlannedRoute {
134
- slot: GoRouteSlot;
135
- route: string;
136
- state: MigrationState;
137
- action: MigrationAction;
138
- fields: GoPlannedRouteFields;
139
- desired: SettingsModelProfile[];
140
- existing: MigrationIdClass[];
141
- }
142
- export interface BuildPlanInput {
143
- descriptor: SettingsDescriptor | undefined;
144
- entries: readonly GoModelRecord[];
145
- routes: Partial<Record<GoRouteSlot, string>>;
146
- ref: string;
147
- keyFingerprint: string;
148
- adopt?: boolean;
149
- rewriteRefs?: RewriteRefMode;
150
- record?: MigrationRecord;
151
- /** Reference candidates from the (offline) reference-surface scan. Report-only by default. */
152
- references?: ReferenceCandidate[];
153
- /** go.pinnedModels: ids forced to `keep` (never moved or deleted, 31 B2). */
154
- pinnedModels?: readonly string[];
155
- /** Only with this flag may user-extra ids be deleted — and only if plan-listed. */
156
- pruneUserExtra?: boolean;
157
- /** Official *public* catalog ids (may include ids the current key cannot use).
158
- * Ids present here but absent from `entries` are `unknown-retired`; ids in
159
- * neither set are `user-extra` (user-written, never auto-deleted). `undefined`
160
- * means the catalog was unavailable → fail-closed `unclassified`. */
161
- publicCatalogIds?: readonly string[];
162
- /**
163
- * 0.12.1 host runtime catalog ids for opencode-go. `undefined` = not
164
- * supplied (legacy 0.12.0 behavior, no filtering); an array intersects the
165
- * desired set — excluded official ids become `host-catalog-lag` (not
166
- * written; plugin-written leftovers on routes are removed to keep the route
167
- * writable, user/pinned/override leftovers are kept with an explicit
168
- * warning); `null` = the catalog could not be read → FAIL-CLOSED: the
169
- * builder refuses (writes must never carry an unverified listing).
170
- */
171
- hostCatalogIds?: readonly string[] | null;
172
- /** Apply cannot execute the per-id table → plan mode `adopt-disabled` and
173
- * `--adopt` rejected at the command layer (design §14.2 fallback). */
174
- adoptDisabled?: boolean;
175
- now: number;
176
- }
177
- /** sha256 of canonical JSON — the planHash algorithm (V17: changes with the plan). */
178
- export declare function hashCanonical(value: unknown): string;
179
- /** Deterministic 6-digit confirmation code derived from a planHash. */
180
- export declare function confirmCodeOf(planHash: string): string;
181
- export declare function planHashOf(body: MigrationPlanBody): string;
182
- /** The action-table subset both plan-time simulation and apply consume. */
183
- export interface MigrationActionTable {
184
- routes: readonly MigrationPlannedRoute[];
185
- idActions: readonly MigrationIdPlan[];
186
- }
187
- /**
188
- * THE single source of truth for the per-route "managed after apply" id set.
189
- *
190
- * It simulates the action table against the SAME settings descriptor apply
191
- * will use, so the ids the plan advertises are exactly the ids apply actually
192
- * writes or adopts into the ownership ledger. An id already sitting on its
193
- * target with a user-edited value (an `update` row, or a `move` onto a
194
- * different-valued target) is NEVER claimed: it is not written, so it must not
195
- * be advertised as managed anywhere (B6 + user ruling: update never overwrites
196
- * and never manages).
197
- *
198
- * `buildMigrationPlan` derives the informed-consent text from this function,
199
- * and `applyMigration` derives the ledger `managed` / `appliedManagedIds` from
200
- * the very same `buildRouteWriteStates` computation, so the four surfaces
201
- * cannot disagree.
202
- */
203
- export declare function managedIdsAfterApply(table: MigrationActionTable, desc: SettingsDescriptor | undefined): Record<string, string[]>;
204
- /**
205
- * Fail-closed guard: any op outside the closed `MigrationIdActionOp` union is
206
- * rejected before a single write. Returns the first offending row, if any.
207
- */
208
- export declare function findUnknownAction(actions: readonly MigrationIdPlan[]): {
209
- id: string;
210
- op: string;
211
- } | undefined;
212
- export type ConsentResult = {
213
- ok: true;
214
- } | {
215
- ok: false;
216
- reason: 'consent-required' | 'plan-hash-mismatch' | 'confirm-code-mismatch' | 'ack-required';
217
- };
218
- /**
219
- * The consent gate (V17 + 31 B6). THREE values are mandatory: the matching
220
- * planHash, the 6-digit confirmCode and `--ack-managed` (acknowledgement of the
221
- * `[managed-after-migration]` notice). A missing/incorrect value never reaches
222
- * a write. Deliberately there is no accept-anything branch — `--yes`/`--auto`
223
- * cannot be expressed.
224
- */
225
- export declare function verifyConsent(plan: MigrationPlan, planHash: string | undefined, confirmCode: string | undefined, ackManaged?: boolean): ConsentResult;
226
- /** Build the full change plan. Pure: identical inputs → identical plan/hash. */
227
- export declare function buildMigrationPlan(input: BuildPlanInput): MigrationPlan;
228
- export declare function formatMigrationPlan(plan: MigrationPlan): string;
229
- export interface MigrateDeps {
230
- settings: SettingsService | undefined;
231
- ownershipStore: GoOwnershipStore;
232
- migrationStore: MigrationStateStore;
233
- logger: Logger;
234
- /** Frozen official listing captured for plan/apply/rollback (no live network here). */
235
- entries: readonly GoModelRecord[];
236
- routes: Partial<Record<GoRouteSlot, string>>;
237
- ref: string;
238
- keyFingerprint: string;
239
- adopt?: boolean;
240
- rewriteRefs?: RewriteRefMode;
241
- references?: ReferenceCandidate[];
242
- /** go.pinnedModels: forced `keep` (31 B2). */
243
- pinnedModels?: readonly string[];
244
- /** Only with this flag may user-extra ids be deleted — and only if plan-listed. */
245
- pruneUserExtra?: boolean;
246
- /** Official public catalog ids, used to distinguish unknown-retired from user-extra.
247
- * `undefined` → fail-closed `unclassified` (never deleted). */
248
- publicCatalogIds?: readonly string[];
249
- /**
250
- * 0.12.1 host runtime catalog ids (see BuildPlanInput). `null` fails the
251
- * whole apply CLOSED before anything is built or written.
252
- */
253
- hostCatalogIds?: readonly string[] | null;
254
- /** Capability flag: `--adopt` is refused at the command layer when the per-id
255
- * action table cannot be executed verbatim (design §14.2 fallback). */
256
- adoptDisabled?: boolean;
257
- /** TEST FAULT INJECTION ONLY. When set, `applyMigration` appends one bogus
258
- * action with this op to the rebuilt table so the fail-closed gate can be
259
- * exercised. Production callers never set it. */
260
- faultInjectOp?: string;
261
- now: number;
262
- /** M-5: true when a go sync round currently holds the single-flight lock. */
263
- isSyncInFlight?: () => boolean;
264
- /** Optional: execute an opt-in rewrite of a verified reference key. Default: none. */
265
- applyReferenceRewrite?: (rewrite: ReferenceRewrite) => Promise<void>;
266
- /**
267
- * O1/C2: after apply/rollback the profile's write-ahead journals are stale
268
- * (the migration rewrote or undid those settings). Clearing them prevents a
269
- * later round re-accounting a journal left by the pre-migration writer.
270
- */
271
- journalStore?: GoJournalStore;
272
- /** O1/C2: stable profile key whose journals are cleared. */
273
- profileKey?: string;
274
- }
275
- /**
276
- * The ONE plan-construction path used by both `/migrate plan` and `apply`.
277
- * Sharing it is what makes the printed plan and the plan apply re-builds
278
- * byte-identical — a prerequisite for the consent gate (V17) and V19.
279
- */
280
- export declare function buildMigrationPlanFromDeps(deps: MigrateDeps, record: MigrationRecord | undefined): MigrationPlan;
281
- interface RouteWriteState {
282
- /** Desired end state of this route's models, seeded from the live section. */
283
- models: Map<string, SettingsModelProfile>;
284
- /** ids under plugin ownership after this apply (B6 ledger `managed`). */
285
- managed: Record<string, GoManagedModel>;
286
- /** ids this apply actually created on this route (ledger `appliedAddedIds`). */
287
- addedIds: Set<string>;
288
- }
289
- /**
290
- * Translate the plan's per-id action table into per-route desired states. This
291
- * is the ONLY place that decides what apply writes: there is no
292
- * `merged = [...desired]` re-derivation anywhere (31 B2). Every row yields
293
- * exactly one result, in table order, so plan ↔ apply is checkable row by row.
294
- *
295
- * This same function powers plan-time simulation (`managedIdsAfterApply`) and
296
- * apply-time execution, so "will be managed" and "was managed" use ONE source.
297
- *
298
- * Semantics (user rulings + §14.2/§14.3):
299
- * keep → untouched; adopted only when the row is flagged `managed` AND the
300
- * id is actually present on the route.
301
- * add → write the official profile unless the id is already present; an
302
- * already-present id is only adopted (managed) if its value matches.
303
- * update → NOTHING written, never managed (user-edited value is preserved).
304
- * move → remove from source route; on the destination, write/adopt the
305
- * official profile ONLY if the target is empty or already holds the
306
- * same value. A different-valued target is a user edit: never
307
- * overwritten, never claimed, and reported as `skipped`.
308
- * delete → remove from the route (only rows explicitly listed in the plan).
309
- * pinned → expressed by keep (+ add on the target) rows; never moved/deleted.
310
- * default → FAIL-CLOSED: an unknown op throws, it is never silently skipped.
311
- */
312
- export declare function buildRouteWriteStates(table: MigrationActionTable, desc: SettingsDescriptor | undefined): {
313
- byRoute: Map<string, RouteWriteState>;
314
- results: MigrateApplyActionResult[];
315
- };
316
- export type MigrateApplyReason = 'applied' | 'consent-required' | 'plan-hash-mismatch' | 'confirm-code-mismatch' | 'ack-required' | 'adopt-disabled' | 'unknown-action' | 'nothing-to-apply' | 'sync-in-flight' | 'host-catalog-unavailable' | 'host-catalog-unknown-models' | 'write-failed';
317
- /** Result of ONE row of the plan's action table — mirrors it one-to-one (V19). */
318
- export interface MigrateApplyActionResult {
319
- id: string;
320
- op: MigrationIdActionOp;
321
- route: string;
322
- to?: string;
323
- status: 'applied' | 'kept' | 'skipped' | 'failed';
324
- reason: string;
325
- }
326
- export interface MigrateApplyResult {
327
- ok: boolean;
328
- reason: MigrateApplyReason;
329
- planHash: string;
330
- confirmCode: string;
331
- routes: {
332
- route: string;
333
- status: 'wrote' | 'failed' | 'skipped';
334
- reason?: string;
335
- }[];
336
- /** One entry per `plan.idActions` row, in the same order. */
337
- actions: MigrateApplyActionResult[];
338
- backupPath?: string;
339
- text: string;
340
- }
341
- export declare function applyMigration(args: {
342
- planHash?: string;
343
- confirmCode?: string;
344
- ackManaged?: boolean;
345
- }, deps: MigrateDeps): Promise<MigrateApplyResult>;
346
- export type MigrateRollbackReason = 'rolled-back' | 'consent-required' | 'plan-hash-mismatch' | 'no-record' | 'snapshot-missing' | 'hash-mismatch';
347
- export interface MigrateRollbackResult {
348
- ok: boolean;
349
- reason: MigrateRollbackReason;
350
- routes: string[];
351
- text: string;
352
- }
353
- export declare function rollbackMigration(args: {
354
- planHash?: string;
355
- }, deps: MigrateDeps): Promise<MigrateRollbackResult>;
356
- export interface MigrationRouteStatus {
357
- route: string;
358
- state: MigrationState;
359
- owner: string;
360
- decision: 'write' | 'skip';
361
- reason: string;
362
- }
363
- export interface MigrateStatusResult {
364
- overall: MigrationState;
365
- /**
366
- * D6: structured write-authority summary for automation. `none` whenever an
367
- * unclaimed record is present (gate closed), otherwise whether any route was
368
- * granted write authority. Per-route authority is in `routes[].owner`.
369
- */
370
- writeAuthority: 'none' | 'go';
371
- /** D6: unclaimed migration records (foreign profile / legacy shape) — never claimed. */
372
- unclaimed: UnclaimedMigrationRecord[];
373
- applying: boolean;
374
- routes: MigrationRouteStatus[];
375
- text: string;
376
- }
377
- export declare function migrationStatusReport(deps: Pick<MigrateDeps, 'settings' | 'migrationStore' | 'keyFingerprint' | 'routes' | 'now'>, config?: Partial<RouteWriterConfig>): Promise<MigrateStatusResult>;
378
- export {};