@aiwayds/dsh-model-sync 0.12.1 → 0.13.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 (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 +116 -0
  10. package/lib/official-migrate.js +643 -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 +840 -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,282 +0,0 @@
1
- import { type GoModelRecord } from './opencode-go-source.ts';
2
- import { type GoRouteSlot } from './opencode-go-grouping.ts';
3
- import type { GoOwnershipStore } from './opencode-go-writer.ts';
4
- import type { GoJournalStore } from './opencode-go-journal.ts';
5
- import { type MigrateDeps, type MigrationPlan, type ReferenceRewrite, type RewriteRefMode } from './opencode-go-migrate.ts';
6
- import type { MigrationStateStore } from './opencode-go-migration.ts';
7
- import { type PatchFileIo, type PatchFs } from './opencode-go-patch-file.ts';
8
- import type { Logger, SettingsService } from './writer.ts';
9
- export interface AskOptionShape {
10
- label: string;
11
- description?: string;
12
- }
13
- export interface AskItemShape {
14
- id: string;
15
- question: string;
16
- detail?: string;
17
- header?: string;
18
- options?: AskOptionShape[];
19
- }
20
- export interface AskAnswerItemShape {
21
- id: string;
22
- selected?: string[];
23
- custom?: string;
24
- }
25
- export interface AskServiceShape {
26
- ask(request: {
27
- questions: AskItemShape[];
28
- agent?: unknown;
29
- signal?: AbortSignal;
30
- }): Promise<{
31
- answers?: AskAnswerItemShape[];
32
- }>;
33
- }
34
- export interface OneShotFs extends PatchFs {
35
- readdir(path: string): Promise<string[]>;
36
- /**
37
- * Exclusive-claim + atomic write in one step: creates `path` with O_EXCL
38
- * semantics (returns `false` when the name is already taken — the caller
39
- * retries the next sequence suffix), then replaces its content through a
40
- * sibling temp file + rename so a crash never leaves a torn backup. This is
41
- * how backups are born: the FILESYSTEM decides "name taken", never a
42
- * readdir snapshot two racers can read identically.
43
- */
44
- writeFileExclusive(path: string, data: string, mode?: number): Promise<boolean>;
45
- }
46
- export interface MigrateGoFlags {
47
- allProfiles: boolean;
48
- adopt: boolean;
49
- pruneUserExtra: boolean;
50
- rewriteRefs: RewriteRefMode;
51
- }
52
- export declare const MIGRATE_GO_USAGE: string;
53
- /**
54
- * Strict flag parsing: unknown flags (including --yes/--auto — there is no
55
- * bypass semantics on this command) and stray positional words are errors
56
- * carrying the usage text.
57
- */
58
- export declare function parseMigrateGoFlags(args: readonly string[]): {
59
- ok: true;
60
- flags: MigrateGoFlags;
61
- } | {
62
- ok: false;
63
- error: string;
64
- };
65
- export interface OneShotDeps {
66
- /** dsh home (resolveDshHome()); anchors the storages dir + profiles dir. */
67
- home: string;
68
- /** Absolute path of the shared models-store.json (MODELS_STORE_PATH). */
69
- modelsStorePath: string;
70
- /** Profiles directory; default `<home>/profiles`. */
71
- profilesDir?: string;
72
- /** Filesystem subset for discovery / backups / restore / log. */
73
- fs: OneShotFs;
74
- /**
75
- * Sync-read + atomic-write io for one profile patch file — the sync read is
76
- * what lets the file-backed settings adapter satisfy the SYNC describe()
77
- * contract the migration engine builds plans through.
78
- */
79
- patchIoFor(patchPath: string): PatchFileIo;
80
- /** Current profile identity (resolveProfileKey + dir derivation at wiring). */
81
- currentProfile?: {
82
- key?: string;
83
- dir?: string;
84
- };
85
- /** Host settings seam serving the CURRENT profile's settings document. */
86
- currentSettings?: SettingsService;
87
- /**
88
- * Host settings seam for the enabled flip (prefers settings.update patch
89
- * merge, falls back to mutate; undefined → manual guidance in the report).
90
- */
91
- hostSettings?: (SettingsService & {
92
- update?(ns: string, patch: object): Promise<void>;
93
- }) | undefined;
94
- /** Resolved go config projection (readGoRuntimeConfig at the wiring site). */
95
- goConfig: {
96
- routes: Partial<Record<GoRouteSlot, string>>;
97
- ref: string;
98
- pinnedModels: readonly string[];
99
- adoptDisabled: boolean;
100
- };
101
- /** Credential value resolution (resolveRouteCredentialValue at the wiring). */
102
- resolveCredential(): Promise<{
103
- value: string | undefined;
104
- ref: string;
105
- }>;
106
- /** Official listing (credential-gated; test seams win before any fetch). */
107
- resolveEntries(apiKey: string): Promise<readonly GoModelRecord[]>;
108
- /** Public catalog ids for id classification; undefined = fail-closed. */
109
- resolvePublicCatalogIds(apiKey: string, entries: readonly GoModelRecord[]): Promise<readonly string[] | undefined>;
110
- /**
111
- * 0.12.1: host runtime catalog ids for opencode-go. `null` FAILS THE RUN
112
- * CLOSED (zero writes) — without it the desired listing cannot be proven
113
- * servable and the host would reject the whole mutate anyway.
114
- */
115
- resolveHostCatalogIds(): Promise<readonly string[] | null>;
116
- /** ask-user seam, resolved lazily at invocation time. */
117
- resolveAsk(): AskServiceShape | undefined;
118
- /**
119
- * Per-profile store pair. The injected test seams win at the wiring and are
120
- * returned verbatim; the production fallback scopes both file-backed stores
121
- * with the given profile key in the shared models-store.json.
122
- */
123
- storesFor(profileKey: string): {
124
- ownership: GoOwnershipStore;
125
- migration: MigrationStateStore;
126
- };
127
- /** Write-ahead journal store for post-apply cleanup; optional. */
128
- journalStore?: GoJournalStore;
129
- /** M-5 single-flight check (go scheduler lock). */
130
- isSyncInFlight(): boolean;
131
- /** Reference rewriter seam — CURRENT profile only. */
132
- applyReferenceRewrite?: ((rewrite: ReferenceRewrite) => Promise<void>) | undefined;
133
- /**
134
- * TEST FAULT INJECTION ONLY (mirrors MigrateDeps.faultInjectOp). When set,
135
- * ownership-ledger writes whose key starts with this prefix throw, which is
136
- * how the mid-apply failure + byte-restore path is exercised. Production
137
- * callers never set it.
138
- */
139
- faultOwnershipWriteOnKeyPrefix?: string | undefined;
140
- logger: Logger;
141
- /** Clock (never reads Date.now() directly). */
142
- now(): number;
143
- }
144
- /** Local-time `YYYYMMDD-HHMMSS` stamp — the backup/log naming convention. */
145
- export declare function formatStamp(date: Date): string;
146
- /** sha256 hex digest of a text — the staleness / concurrent-modification detector. */
147
- export declare function sha256Of(text: string): string;
148
- /**
149
- * Create `<base>` / `<base>.<seq>` exclusively: `writeFileExclusive` returns
150
- * false when the name is taken (O_EXCL), and only then does the loop advance
151
- * the sequence suffix. No readdir snapshot is consulted, so two processes
152
- * racing inside the same second can never both "win" the same backup name —
153
- * the loser retries onto `.<seq+1>` and both files survive.
154
- */
155
- export declare function writeExclusiveBackup(fs: OneShotFs, base: string, text: string, mode?: number): Promise<string>;
156
- /**
157
- * Retention: keep the `keep` newest backups matching `pattern` in `dirPath`
158
- * (sorted by filename timestamp, then sequence) and unlink the rest. Returns
159
- * the removed names. Best-effort per file: unlink errors are reported, not
160
- * thrown — retention must never fail a migration that already backed up.
161
- */
162
- export declare function pruneBackups(fs: OneShotFs, dirPath: string, pattern: RegExp, keep?: number): Promise<string[]>;
163
- interface LegacyStub {
164
- route: string;
165
- ids: string[];
166
- }
167
- /**
168
- * The legacy stub definition from the approved behavior: an `opencode-go*`
169
- * section that carries a models array but NO api/baseURL — the handwritten
170
- * pre-migration entry. Any other provider section is ignored here.
171
- */
172
- export declare function legacyStubsOf(providers: unknown, goRouteIds: readonly string[]): LegacyStub[];
173
- interface Target {
174
- /** Profile key (store scoping + report label). */
175
- key: string;
176
- /** Profile directory (backup anchors). */
177
- dir: string;
178
- patchPath: string;
179
- /** True when this target is served by the host settings seam. */
180
- isCurrent: boolean;
181
- stubs: LegacyStub[];
182
- }
183
- interface ChangeTracker {
184
- patchChanged: boolean;
185
- storeChanged: boolean;
186
- /**
187
- * sha256 of the patch file's content recorded IMMEDIATELY after this
188
- * target's last settings write (the post-write baseline). Restore only
189
- * replaces the file while its current content still hashes to this value;
190
- * `undefined` (file unreadable after the write) makes restore refuse —
191
- * fail closed instead of clobbering an unbaselined file.
192
- */
193
- patchPostWriteSha?: string | undefined;
194
- }
195
- interface BackupRecord {
196
- source: string;
197
- backup: string;
198
- /** sha256 of the source content captured at backup time — the pre-apply
199
- * staleness check re-verifies the source against this before any write. */
200
- sourceSha: string;
201
- }
202
- interface RestoreOutcome {
203
- line: string;
204
- /** True only when the file was actually replaced/removed. */
205
- restored: boolean;
206
- }
207
- /**
208
- * Restore one changed patch file from its backup, but ONLY while the on-disk
209
- * content still hashes to the post-write baseline this run recorded right
210
- * after its last write. Anything else means another process touched the file
211
- * after us: never overwrite it — report "concurrent modification detected,
212
- * not restored" with the backup path so a human can decide. The write itself
213
- * goes through the SAME atomic io (tmp + rename, mode preserved) the apply
214
- * used — never a raw writeFile.
215
- */
216
- export declare function restorePatchIfUnchanged(deps: OneShotDeps, record: BackupRecord, postWriteSha: string | undefined): Promise<RestoreOutcome>;
217
- /**
218
- * Remove a models-store this run CREATED, but only while its content still
219
- * matches this run's post-write baseline; a store another process has touched
220
- * since is left in place and reported for manual handling.
221
- */
222
- export declare function removeStoreIfUnchanged(deps: OneShotDeps, postWriteSha: string | undefined): Promise<RestoreOutcome>;
223
- /**
224
- * Restore the models-store from its backup through the remote-catalog store
225
- * accessor — the SAME promise queue + cross-process lock every other
226
- * models-store writer uses — instead of overwriting the file behind its back.
227
- * Review M5: the restore is a whole-document BYTE replacement
228
- * (`restoreRawDocument`: exact backup bytes staged via tmp + rename under the
229
- * lock), not a per-key rebuild — the backup's key order survives verbatim and
230
- * the restore is transactional. The same post-write-baseline check as the
231
- * patch restore applies first, and a backup that is not valid JSON refuses
232
- * loudly before anything is touched. Round 3: the baseline is ALSO
233
- * re-verified INSIDE the store's queue + cross-process lock (the accessor's
234
- * `expectedCurrentSha`), so a writer that landed between the pre-lock check
235
- * and the restore is refused at the write site and reported as concurrent
236
- * modification instead of clobbering the foreign bytes.
237
- */
238
- export declare function restoreModelsStoreIfUnchanged(deps: OneShotDeps, backupPath: string, postWriteSha: string | undefined): Promise<RestoreOutcome>;
239
- /**
240
- * Re-hash every target's patch file and drop the ones whose content changed
241
- * after their backup was taken (the backup → apply window, review Major 2
242
- * item 3): a plan computed from stale bytes is never applied, and a dropped
243
- * target writes nothing.
244
- */
245
- export declare function dropStaleTargets(deps: OneShotDeps, prepared: readonly PreparedTarget[]): Promise<{
246
- ready: PreparedTarget[];
247
- stale: {
248
- key: string;
249
- patchPath: string;
250
- }[];
251
- }>;
252
- export type StoreFreshness = {
253
- ok: true;
254
- } | {
255
- ok: false;
256
- reason: string;
257
- };
258
- /**
259
- * Verify the shared models-store has not drifted inside the backup → apply
260
- * window: with a backup, its content must still hash to the backup-time
261
- * value; without one (the file did not exist at backup time), it must still
262
- * not exist — a store another process created meanwhile must never end up
263
- * deleted by this run's rollback path.
264
- */
265
- export declare function verifyStoreFreshness(deps: OneShotDeps, modelsStoreBackup: string | undefined, storeSourceSha: string | undefined, storeExistedBefore: boolean): Promise<StoreFreshness>;
266
- interface PreparedTarget {
267
- target: Target;
268
- plan: MigrationPlan;
269
- migrateDeps: MigrateDeps;
270
- tracker: ChangeTracker;
271
- stubs: LegacyStub[];
272
- patchBackup: BackupRecord;
273
- }
274
- export interface OneShotOutcome {
275
- kind: 'success' | 'error';
276
- text: string;
277
- }
278
- export declare function runMigrateGoOneShot(deps: OneShotDeps, flags: MigrateGoFlags, invocation: {
279
- agent?: unknown;
280
- signal?: AbortSignal;
281
- }): Promise<OneShotOutcome>;
282
- export {};