@aiwayds/dsh-model-sync 0.2.1 → 0.2.2
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.
- package/README.md +2 -2
- package/README.zh-CN.md +2 -2
- package/lib/index.js +25 -3
- package/lib/index.js.map +1 -1
- package/lib/remote-catalog.d.ts +8 -0
- package/lib/remote-catalog.js +52 -6
- package/lib/remote-catalog.js.map +1 -1
- package/lib/writer.d.ts +65 -14
- package/lib/writer.js +186 -21
- package/lib/writer.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +23 -2
- package/src/remote-catalog.ts +58 -6
- package/src/writer.ts +272 -27
package/lib/writer.js
CHANGED
|
@@ -98,7 +98,91 @@ function mergeOverrides(target, overrides) {
|
|
|
98
98
|
const override = overrides[entry.id];
|
|
99
99
|
if (override === undefined)
|
|
100
100
|
return entry;
|
|
101
|
-
|
|
101
|
+
// Force the entry's own id back on: an override object that happens to
|
|
102
|
+
// carry an `id` field must not silently re-key the entry in the written
|
|
103
|
+
// models list.
|
|
104
|
+
return { ...entry, ...override, id: entry.id };
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Override ids that cannot fold into the target (no entry carries the id).
|
|
109
|
+
* Sorted so the report line is deterministic.
|
|
110
|
+
*/
|
|
111
|
+
function droppedOverrideIds(overrides, mergedTarget) {
|
|
112
|
+
if (overrides === undefined)
|
|
113
|
+
return undefined;
|
|
114
|
+
const targetIds = new Set(mergedTarget.map((entry) => entry.id));
|
|
115
|
+
return Object.keys(overrides).filter((id) => !targetIds.has(id)).sort();
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Resolve the overrides to fold for one route: a non-empty settings key wins
|
|
119
|
+
* (a re-added key is the user's latest intent); otherwise the models-store
|
|
120
|
+
* replay applies. An empty `{}` key counts as absent — the dsh validation
|
|
121
|
+
* only refuses non-empty overrides beside a models list, and there is nothing
|
|
122
|
+
* in it to preserve.
|
|
123
|
+
*/
|
|
124
|
+
async function resolveOverrides(desc, route, store) {
|
|
125
|
+
const raw = getRawUserModelOverrides(desc, route);
|
|
126
|
+
if (raw !== undefined && Object.keys(raw).length > 0) {
|
|
127
|
+
return { overrides: raw, source: 'settings' };
|
|
128
|
+
}
|
|
129
|
+
if (store !== undefined) {
|
|
130
|
+
// The store read may now throw on corruption / permission failures
|
|
131
|
+
// (remote-catalog readDoc propagates anything other than ENOENT). We
|
|
132
|
+
// bubble it up: with no settings overrides to fall back on, the caller
|
|
133
|
+
// must skip the round rather than fold an unreplayed target into
|
|
134
|
+
// settings and clobber the folded values. With a settings key in play
|
|
135
|
+
// (the earlier branch above) this code path is never reached, so a
|
|
136
|
+
// settings-side write always proceeds and the atomic writeDoc heals the
|
|
137
|
+
// corruption on its next write.
|
|
138
|
+
const stored = await store.read(route);
|
|
139
|
+
if (stored?.overrides !== undefined && Object.keys(stored.overrides).length > 0) {
|
|
140
|
+
return { overrides: stored.overrides, source: 'store' };
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return { overrides: undefined, source: undefined };
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Stage overrides in the models-store BEFORE the mutate that unsets them
|
|
147
|
+
* from settings (store-first). The read runs outside the store's serialized
|
|
148
|
+
* write queue — only the merged entry write goes through the queue — so the
|
|
149
|
+
* route's models/checkedAt/etag, written by the fetch earlier in this round,
|
|
150
|
+
* stay intact. While the settings key is present it always wins over the
|
|
151
|
+
* store (resolveOverrides order); the stored copy is only ever replayed
|
|
152
|
+
* once the key is gone. Callers must treat a throw from here as "skip the
|
|
153
|
+
* mutate": unsetting without a staged copy is the v0.1.5 data loss.
|
|
154
|
+
*
|
|
155
|
+
* The store read may now fail on corruption / permission errors (readDoc
|
|
156
|
+
* propagates everything except ENOENT). Swallowing it here is intentional:
|
|
157
|
+
* the settings key carries the only authoritative copy of the overrides, the
|
|
158
|
+
* atomic writeDoc below replaces the (possibly corrupt) file with a fresh
|
|
159
|
+
* entry that carries the overrides — which is what heals the store. The
|
|
160
|
+
* fetch side will re-populate models/checkedAt/etag on its next round.
|
|
161
|
+
* (A storeless call — store undefined — cannot stage anything and keeps the
|
|
162
|
+
* legacy unset-without-copy behavior; production wiring in index.ts always
|
|
163
|
+
* passes a store.)
|
|
164
|
+
*/
|
|
165
|
+
async function persistOverridesToStore(store, route, overrides) {
|
|
166
|
+
if (store === undefined)
|
|
167
|
+
return;
|
|
168
|
+
let stored;
|
|
169
|
+
try {
|
|
170
|
+
stored = await store.read(route);
|
|
171
|
+
}
|
|
172
|
+
catch (readErr) {
|
|
173
|
+
// Cache lost — fall through with no cached metadata. The atomic
|
|
174
|
+
// writeDoc below replaces the bad file with a fresh entry carrying
|
|
175
|
+
// only the overrides. The fetch side re-populates the rest on its
|
|
176
|
+
// next round. The settings key remains the authoritative source of
|
|
177
|
+
// truth in the meantime.
|
|
178
|
+
void readErr;
|
|
179
|
+
}
|
|
180
|
+
await store.write(route, {
|
|
181
|
+
models: stored?.models ?? [],
|
|
182
|
+
checkedAt: stored?.checkedAt ?? Date.now(),
|
|
183
|
+
lastModified: stored?.lastModified ?? 0,
|
|
184
|
+
etag: stored?.etag,
|
|
185
|
+
overrides,
|
|
102
186
|
});
|
|
103
187
|
}
|
|
104
188
|
// ---------------------------------------------------------------------------
|
|
@@ -107,22 +191,34 @@ function mergeOverrides(target, overrides) {
|
|
|
107
191
|
/**
|
|
108
192
|
* Write translated model profiles to settings for a given route.
|
|
109
193
|
*
|
|
110
|
-
* Implements the full §4 protocol
|
|
194
|
+
* Implements the full §4 protocol (revised for the modelOverrides mutual
|
|
195
|
+
* exclusion):
|
|
111
196
|
* 1. Get revision from describe()
|
|
112
|
-
* 2.
|
|
113
|
-
*
|
|
197
|
+
* 2. Resolve the overrides to fold: the settings key when non-empty, the
|
|
198
|
+
* models-store replay otherwise; merge into the target (override wins per
|
|
199
|
+
* field)
|
|
114
200
|
* 3. Change-only write: skip when the raw user-segment models already equal
|
|
115
|
-
* the merged target
|
|
116
|
-
*
|
|
117
|
-
*
|
|
201
|
+
* the merged target — except when the settings key is present, because the
|
|
202
|
+
* models+overrides combo is refused by the llm-pi-ai validation (the
|
|
203
|
+
* set+unset must run to move the document out of the refused shape)
|
|
204
|
+
* 4. Store-first: when the settings key is present, stage the raw overrides
|
|
205
|
+
* in the models-store BEFORE the mutate; a store write failure skips the
|
|
206
|
+
* mutate entirely and reports 'store-unavailable' (settings untouched —
|
|
207
|
+
* the key is never unset without a stored copy). Then call mutate with
|
|
208
|
+
* expectedRevision: one `set` on the models path, plus an `unset` on the
|
|
209
|
+
* modelOverrides path (atomic in a single mutate).
|
|
210
|
+
* 5. On SETTINGS_CONFLICT: re-read, re-translate (caller's job), re-resolve
|
|
211
|
+
* the overrides from the fresh descriptor, stage again (store-first),
|
|
212
|
+
* re-write once
|
|
118
213
|
*
|
|
119
214
|
* @param settings The settings service (injected, not imported)
|
|
120
215
|
* @param route The provider route id
|
|
121
216
|
* @param target Translated settings-writable entries (sorted by id)
|
|
122
217
|
* @param logger Logger for warnings/errors
|
|
123
218
|
* @param retranslate Callback to re-translate on conflict (receives new revision)
|
|
219
|
+
* @param store The models-store accessor for the overrides replay/persist
|
|
124
220
|
*/
|
|
125
|
-
export async function syncToSettings(settings, route, target, logger, retranslate) {
|
|
221
|
+
export async function syncToSettings(settings, route, target, logger, retranslate, store) {
|
|
126
222
|
if (settings === undefined) {
|
|
127
223
|
logger.debug('settings service unavailable; skip write for route %s', route);
|
|
128
224
|
return { wrote: false, reason: 'skipped' };
|
|
@@ -134,22 +230,60 @@ export async function syncToSettings(settings, route, target, logger, retranslat
|
|
|
134
230
|
return { wrote: false, reason: 'skipped' };
|
|
135
231
|
}
|
|
136
232
|
const rawModels = getRawUserModels(desc, route);
|
|
137
|
-
|
|
138
|
-
|
|
233
|
+
let effectiveOverrides;
|
|
234
|
+
let overridesSource;
|
|
235
|
+
try {
|
|
236
|
+
({ overrides: effectiveOverrides, source: overridesSource } =
|
|
237
|
+
await resolveOverrides(desc, route, store));
|
|
238
|
+
}
|
|
239
|
+
catch (readErr) {
|
|
240
|
+
// The store is unreadable (corrupted JSON, EACCES, …) and settings
|
|
241
|
+
// carries no overrides key — there is no authoritative source for the
|
|
242
|
+
// user's folded values other than the existing settings.models. Skip
|
|
243
|
+
// the round with reason 'store-unavailable': writing the raw pi.dev
|
|
244
|
+
// target now would overwrite settings.models with unreplayed values
|
|
245
|
+
// and silently clobber the user's fold (the same data-loss shape
|
|
246
|
+
// v0.1.5 hit, just via a different path). The next refresh attempts
|
|
247
|
+
// the same round again; a manual re-add of the key (or a fresh
|
|
248
|
+
// successful settings-side write that heals the file) unblocks it.
|
|
249
|
+
logger.warn('models-store read failed for route %s: %s', route, readErr instanceof Error ? readErr.message : String(readErr));
|
|
250
|
+
return { wrote: false, reason: 'store-unavailable' };
|
|
251
|
+
}
|
|
252
|
+
const hasSettingsOverrides = overridesSource === 'settings';
|
|
253
|
+
const mergedTarget = mergeOverrides(target, effectiveOverrides);
|
|
139
254
|
// Change-only write (§4.4): skip when the stored models already reflect
|
|
140
|
-
// target ⊕ overrides.
|
|
141
|
-
//
|
|
142
|
-
//
|
|
143
|
-
if (rawModels !== undefined && profilesEqual(rawModels, mergedTarget)) {
|
|
255
|
+
// target ⊕ overrides. Skipped when the settings key is present — the unset
|
|
256
|
+
// must run in this mutate to clear the refused models+overrides combo, and
|
|
257
|
+
// after it lands the replay keeps later rounds change-only.
|
|
258
|
+
if (!hasSettingsOverrides && rawModels !== undefined && profilesEqual(rawModels, mergedTarget)) {
|
|
144
259
|
logger.debug('no change for route %s; skip write', route);
|
|
145
260
|
return { wrote: false, reason: 'no-change' };
|
|
146
261
|
}
|
|
262
|
+
const dropped = droppedOverrideIds(effectiveOverrides, mergedTarget);
|
|
147
263
|
const ops = [
|
|
148
264
|
{ op: 'set', path: ['providers', route, 'models'], value: mergedTarget },
|
|
149
265
|
];
|
|
266
|
+
if (hasSettingsOverrides) {
|
|
267
|
+
ops.push({ op: 'unset', path: ['providers', route, 'modelOverrides'] });
|
|
268
|
+
}
|
|
269
|
+
// Store-first: stage the raw overrides in the models-store BEFORE the
|
|
270
|
+
// mutate that unsets them from settings. A store write failure must skip
|
|
271
|
+
// the mutate entirely — unsetting without a staged copy is the v0.1.5
|
|
272
|
+
// data loss — and settings stays untouched, so it remains the
|
|
273
|
+
// authoritative source. Reported honestly as 'store-unavailable': the
|
|
274
|
+
// mutate never ran, so 'mutate-rejected' would be a lie.
|
|
275
|
+
if (hasSettingsOverrides && effectiveOverrides !== undefined) {
|
|
276
|
+
try {
|
|
277
|
+
await persistOverridesToStore(store, route, effectiveOverrides);
|
|
278
|
+
}
|
|
279
|
+
catch (persistErr) {
|
|
280
|
+
logger.warn('models-store persist failed for route %s: %s', route, persistErr instanceof Error ? persistErr.message : String(persistErr));
|
|
281
|
+
return { wrote: false, reason: 'store-unavailable' };
|
|
282
|
+
}
|
|
283
|
+
}
|
|
150
284
|
try {
|
|
151
285
|
await settings.mutate('llm-pi-ai', ops, desc.revision);
|
|
152
|
-
return { wrote: true, reason: 'wrote' };
|
|
286
|
+
return { wrote: true, reason: 'wrote', overridesSource, droppedOverrideIds: dropped };
|
|
153
287
|
}
|
|
154
288
|
catch (err) {
|
|
155
289
|
// Check for SETTINGS_CONFLICT
|
|
@@ -172,15 +306,46 @@ export async function syncToSettings(settings, route, target, logger, retranslat
|
|
|
172
306
|
return { wrote: false, reason: 'conflict-retry-failed' };
|
|
173
307
|
}
|
|
174
308
|
const newTarget = await retranslate(newDesc.revision);
|
|
309
|
+
let retry;
|
|
310
|
+
try {
|
|
311
|
+
retry = await resolveOverrides(newDesc, route, store);
|
|
312
|
+
}
|
|
313
|
+
catch (readErr) {
|
|
314
|
+
// Same fail-closed contract as the main path: with the settings key
|
|
315
|
+
// gone between the two attempts, the store replay is the only way
|
|
316
|
+
// to recover the fold; an unreadable store leaves the user values
|
|
317
|
+
// stranded in settings.models. Skip the retry, report honestly.
|
|
318
|
+
logger.warn('models-store read failed for route %s on retry: %s', route, readErr instanceof Error ? readErr.message : String(readErr));
|
|
319
|
+
return { wrote: false, reason: 'store-unavailable' };
|
|
320
|
+
}
|
|
321
|
+
const retryHasSettingsOverrides = retry.source === 'settings';
|
|
322
|
+
const retryMerged = mergeOverrides(newTarget, retry.overrides);
|
|
323
|
+
const retryDropped = droppedOverrideIds(retry.overrides, retryMerged);
|
|
175
324
|
const retryOps = [
|
|
176
|
-
{
|
|
177
|
-
op: 'set',
|
|
178
|
-
path: ['providers', route, 'models'],
|
|
179
|
-
value: mergeOverrides(newTarget, getRawUserModelOverrides(newDesc, route)),
|
|
180
|
-
},
|
|
325
|
+
{ op: 'set', path: ['providers', route, 'models'], value: retryMerged },
|
|
181
326
|
];
|
|
327
|
+
if (retryHasSettingsOverrides) {
|
|
328
|
+
retryOps.push({ op: 'unset', path: ['providers', route, 'modelOverrides'] });
|
|
329
|
+
}
|
|
330
|
+
// Store-first again on the retry: the overrides were re-resolved from
|
|
331
|
+
// the fresh descriptor, so stage them before the retry mutate (the
|
|
332
|
+
// user may have changed or removed the key between the attempts).
|
|
333
|
+
if (retryHasSettingsOverrides && retry.overrides !== undefined) {
|
|
334
|
+
try {
|
|
335
|
+
await persistOverridesToStore(store, route, retry.overrides);
|
|
336
|
+
}
|
|
337
|
+
catch (persistErr) {
|
|
338
|
+
logger.warn('models-store persist failed for route %s: %s', route, persistErr instanceof Error ? persistErr.message : String(persistErr));
|
|
339
|
+
return { wrote: false, reason: 'store-unavailable' };
|
|
340
|
+
}
|
|
341
|
+
}
|
|
182
342
|
await settings.mutate('llm-pi-ai', retryOps, newDesc.revision);
|
|
183
|
-
return {
|
|
343
|
+
return {
|
|
344
|
+
wrote: true,
|
|
345
|
+
reason: 'conflict-retry-ok',
|
|
346
|
+
overridesSource: retry.source,
|
|
347
|
+
droppedOverrideIds: retryDropped,
|
|
348
|
+
};
|
|
184
349
|
}
|
|
185
350
|
catch (retryErr) {
|
|
186
351
|
logger.warn('conflict retry failed for route %s: %s', route, retryErr instanceof Error ? retryErr.message : String(retryErr));
|
package/lib/writer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"writer.js","sourceRoot":"","sources":["../src/writer.ts"],"names":[],"mappings":"AAAA,sFAAsF;
|
|
1
|
+
{"version":3,"file":"writer.js","sourceRoot":"","sources":["../src/writer.ts"],"names":[],"mappings":"AAAA,sFAAsF;AA6GtF,8EAA8E;AAC9E,YAAY;AACZ,8EAA8E;AAE9E,2DAA2D;AAC3D,MAAM,iBAAiB,GAAG,mBAAmB,CAAA;AAE7C,8EAA8E;AAC9E,kCAAkC;AAClC,8EAA8E;AAE9E;;;GAGG;AACH,MAAM,UAAU,aAAa,CAC3B,OAA+B,EAC/B,MAA8B;IAE9B,IAAI,OAAO,CAAC,MAAM,KAAK,MAAM,CAAC,MAAM;QAAE,OAAO,KAAK,CAAA;IAClD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACxC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;YAAE,OAAO,KAAK,CAAA;IACrD,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC;AAED,SAAS,SAAS,CAAC,CAAU,EAAE,CAAU;IACvC,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAA;IACxB,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,IAAI;QAAE,OAAO,KAAK,CAAA;IAC1C,IAAI,OAAO,CAAC,KAAK,OAAO,CAAC;QAAE,OAAO,KAAK,CAAA;IACvC,IAAI,OAAO,CAAC,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAA;IAEvC,MAAM,IAAI,GAAG,CAA4B,CAAA;IACzC,MAAM,IAAI,GAAG,CAA4B,CAAA;IACzC,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CA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AAC,CAAC,MAAM,CAAC,UAAU,CAAC,CACtE,CAAA;oBACD,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,mBAAmB,EAAE,CAAA;gBACtD,CAAC;YACH,CAAC;YAED,MAAM,QAAQ,CAAC,MAAM,CAAC,WAAW,EAAE,QAAQ,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAA;YAC9D,OAAO;gBACL,KAAK,EAAE,IAAI;gBACX,MAAM,EAAE,mBAAmB;gBAC3B,eAAe,EAAE,KAAK,CAAC,MAAM;gBAC7B,kBAAkB,EAAE,YAAY;aACjC,CAAA;QACH,CAAC;QAAC,OAAO,QAAiB,EAAE,CAAC;YAC3B,MAAM,CAAC,IAAI,CACT,wCAAwC,EACxC,KAAK,EACL,QAAQ,YAAY,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAChE,CAAA;YACD,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,uBAAuB,EAAE,CAAA;QAC1D,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aiwayds/dsh-model-sync",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "dsh plugin: keep llm-pi-ai provider routes' model lists in step with the pi.dev gateway, written through the official settings seam",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"build": "tsc -p tsconfig.json",
|
|
32
32
|
"check": "tsc --noEmit -p tsconfig.json",
|
|
33
33
|
"pretest": "tsc -p tsconfig.json",
|
|
34
|
-
"test": "node --test test/live-catalog.test.mjs test/diff.test.mjs test/translate.test.mjs test/writer.test.mjs test/remote-catalog.test.mjs test/serviceability.test.mjs test/command.test.mjs test/real-cordis.test.mjs"
|
|
34
|
+
"test": "node --test test/live-catalog.test.mjs test/diff.test.mjs test/translate.test.mjs test/writer.test.mjs test/writer-overrides.test.mjs test/remote-catalog.test.mjs test/serviceability.test.mjs test/command.test.mjs test/real-cordis.test.mjs"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {},
|
|
37
37
|
"peerDependencies": {
|
package/src/index.ts
CHANGED
|
@@ -288,7 +288,8 @@ export function apply(ctx: Context): void {
|
|
|
288
288
|
}
|
|
289
289
|
}
|
|
290
290
|
|
|
291
|
-
// Write to settings (change-only, with conflict retry)
|
|
291
|
+
// Write to settings (change-only, with conflict retry); the store
|
|
292
|
+
// carries the modelOverrides replay/persist (mutual-exclusion fix)
|
|
292
293
|
const syncResult = await syncToSettings(
|
|
293
294
|
settings,
|
|
294
295
|
route,
|
|
@@ -301,16 +302,36 @@ export function apply(ctx: Context): void {
|
|
|
301
302
|
void newRevision // revision is used by the caller
|
|
302
303
|
return translated.entries
|
|
303
304
|
},
|
|
305
|
+
store,
|
|
304
306
|
)
|
|
305
307
|
|
|
306
308
|
if (syncResult.wrote) {
|
|
307
|
-
|
|
309
|
+
// Detail suffix for the override flows; routes without overrides keep
|
|
310
|
+
// the classic "(wrote)" line byte-for-byte.
|
|
311
|
+
const reason = syncResult.reason === 'wrote' ? '' : `${syncResult.reason}; `
|
|
312
|
+
let detail: string | undefined
|
|
313
|
+
if (syncResult.overridesSource === 'settings') {
|
|
314
|
+
detail = `${reason}folded user modelOverrides, unset the key`
|
|
315
|
+
} else if (syncResult.overridesSource === 'store') {
|
|
316
|
+
detail = `${reason}applied stored modelOverrides`
|
|
317
|
+
}
|
|
318
|
+
if (detail !== undefined && syncResult.droppedOverrideIds !== undefined && syncResult.droppedOverrideIds.length > 0) {
|
|
319
|
+
detail += `; override ids not in target kept in models-store: ${syncResult.droppedOverrideIds.join(', ')}`
|
|
320
|
+
}
|
|
321
|
+
lines.push(detail !== undefined
|
|
322
|
+
? `${route}: wrote ${translated.entries.length} models (${detail})`
|
|
323
|
+
: `${route}: wrote ${translated.entries.length} models (${syncResult.reason})`)
|
|
308
324
|
} else if (syncResult.reason === 'no-change') {
|
|
309
325
|
lines.push(`${route}: up to date (${translated.entries.length} models)`)
|
|
310
326
|
} else if (syncResult.reason === 'skipped') {
|
|
311
327
|
lines.push(`${route}: skipped — settings service unavailable or llm-pi-ai namespace not registered`)
|
|
312
328
|
} else if (syncResult.reason === 'mutate-rejected') {
|
|
313
329
|
lines.push(`${route}: rejected by settings validation (see log)`)
|
|
330
|
+
} else if (syncResult.reason === 'store-unavailable') {
|
|
331
|
+
// The store failed (corrupt/permission on read, or filesystem on
|
|
332
|
+
// write); settings.models was the authoritative source and stays
|
|
333
|
+
// untouched either way — the user's folded values are preserved.
|
|
334
|
+
lines.push(`${route}: skipped (models-store unavailable; settings untouched)`)
|
|
314
335
|
} else {
|
|
315
336
|
lines.push(`${route}: ${syncResult.reason} (${translated.entries.length} models)`)
|
|
316
337
|
}
|
package/src/remote-catalog.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
|
|
14
14
|
import { homedir } from 'node:os'
|
|
15
15
|
import { dirname, join } from 'node:path'
|
|
16
|
-
import { mkdir, readFile, writeFile } from 'node:fs/promises'
|
|
16
|
+
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
|
|
17
17
|
|
|
18
18
|
// ---------------------------------------------------------------------------
|
|
19
19
|
// Constants (ported from docs-dsh-llm-pi-ai.patch:35-36)
|
|
@@ -65,6 +65,14 @@ export interface ModelsStoreEntry {
|
|
|
65
65
|
checkedAt: number
|
|
66
66
|
lastModified: number
|
|
67
67
|
etag?: string
|
|
68
|
+
/**
|
|
69
|
+
* User `modelOverrides` folded out of settings and unset there (the dsh
|
|
70
|
+
* llm-pi-ai validation refuses a models list beside non-empty
|
|
71
|
+
* modelOverrides). Replayed into the translated target on every later
|
|
72
|
+
* round so the user's values survive — the catalog refresh must carry this
|
|
73
|
+
* field through untouched, exactly like the ETag.
|
|
74
|
+
*/
|
|
75
|
+
overrides?: Record<string, Record<string, unknown>>
|
|
68
76
|
}
|
|
69
77
|
|
|
70
78
|
/** Top-level shape of ~/.dsh/models-store.json. */
|
|
@@ -147,14 +155,37 @@ export function loadModelsStore(storePath?: string): ModelsStoreAccessor {
|
|
|
147
155
|
const readDoc = async (): Promise<ModelsStore> => {
|
|
148
156
|
try {
|
|
149
157
|
return JSON.parse(await readFile(effectivePath, 'utf8')) as ModelsStore
|
|
150
|
-
} catch {
|
|
151
|
-
|
|
158
|
+
} catch (err: unknown) {
|
|
159
|
+
// ENOENT is a normal first run — the store has never been written.
|
|
160
|
+
// Anything else (SyntaxError on a corrupted file, EACCES on a
|
|
161
|
+
// permission-denied read, EIO on storage failure) bubbles up so the
|
|
162
|
+
// writer fails closed: treating an unreadable store as empty would
|
|
163
|
+
// let a later replay write the raw pi.dev target into settings and
|
|
164
|
+
// clobber user-folded values (the v0.1.5 data-loss shape). The
|
|
165
|
+
// failure propagates to the writer which skips the round and keeps
|
|
166
|
+
// settings.models — the authoritative, already-landed source —
|
|
167
|
+
// untouched.
|
|
168
|
+
if (err instanceof Error && (err as NodeJS.ErrnoException).code === 'ENOENT') {
|
|
169
|
+
return {}
|
|
170
|
+
}
|
|
171
|
+
throw err
|
|
152
172
|
}
|
|
153
173
|
}
|
|
154
174
|
|
|
155
175
|
const writeDoc = async (doc: ModelsStore): Promise<void> => {
|
|
156
176
|
await mkdir(dirname(effectivePath), { recursive: true })
|
|
157
|
-
|
|
177
|
+
// Atomic write: stage to a sibling temp file then rename(2) into place.
|
|
178
|
+
// rename(2) is atomic on POSIX, so a concurrent reader either sees the
|
|
179
|
+
// pre-rename doc or the post-rename doc — never a half-written file.
|
|
180
|
+
// JSON.stringify without spaces keeps the existing wire format (the
|
|
181
|
+
// reader is strict on the parse side; legacy stores carry no
|
|
182
|
+
// whitespace). A fresh write also *heals* a previously-corrupt store:
|
|
183
|
+
// the first successful write after corruption replaces the bad file
|
|
184
|
+
// atomically with a well-formed doc, so subsequent reads see the
|
|
185
|
+
// expected shape.
|
|
186
|
+
const tmpPath = `${effectivePath}.${process.pid}.tmp`
|
|
187
|
+
await writeFile(tmpPath, JSON.stringify(doc))
|
|
188
|
+
await rename(tmpPath, effectivePath)
|
|
158
189
|
}
|
|
159
190
|
|
|
160
191
|
let queue: Promise<void> = Promise.resolve()
|
|
@@ -171,13 +202,31 @@ export function loadModelsStore(storePath?: string): ModelsStoreAccessor {
|
|
|
171
202
|
},
|
|
172
203
|
write: (providerId: string, entry: ModelsStoreEntry): Promise<void> =>
|
|
173
204
|
enqueue(async () => {
|
|
174
|
-
|
|
205
|
+
// The read-modify-write pattern preserves concurrent routes'
|
|
206
|
+
// entries; if the file is corrupt or unreadable, treat the doc as
|
|
207
|
+
// empty and overwrite — the atomic writeDoc below replaces the
|
|
208
|
+
// bad file with a fresh, well-formed doc carrying this route's
|
|
209
|
+
// entry (and any other routes the caller writes later will
|
|
210
|
+
// re-stamp theirs). The store reads path stays strict (readDoc
|
|
211
|
+
// still throws) so the writer's fail-closed logic can react to
|
|
212
|
+
// the actual error instead of seeing a fake empty doc.
|
|
213
|
+
let doc: ModelsStore
|
|
214
|
+
try {
|
|
215
|
+
doc = await readDoc()
|
|
216
|
+
} catch {
|
|
217
|
+
doc = {}
|
|
218
|
+
}
|
|
175
219
|
doc[providerId] = entry
|
|
176
220
|
await writeDoc(doc)
|
|
177
221
|
}),
|
|
178
222
|
delete: (providerId: string): Promise<void> =>
|
|
179
223
|
enqueue(async () => {
|
|
180
|
-
|
|
224
|
+
let doc: ModelsStore
|
|
225
|
+
try {
|
|
226
|
+
doc = await readDoc()
|
|
227
|
+
} catch {
|
|
228
|
+
doc = {}
|
|
229
|
+
}
|
|
181
230
|
delete doc[providerId]
|
|
182
231
|
await writeDoc(doc)
|
|
183
232
|
}),
|
|
@@ -264,6 +313,7 @@ export async function fetchRemoteCatalog(
|
|
|
264
313
|
checkedAt,
|
|
265
314
|
lastModified: 0,
|
|
266
315
|
etag: undefined,
|
|
316
|
+
overrides: stored?.overrides,
|
|
267
317
|
}
|
|
268
318
|
await effectiveStore.write(route, entry)
|
|
269
319
|
return { entries: [], fromCache: false }
|
|
@@ -276,6 +326,7 @@ export async function fetchRemoteCatalog(
|
|
|
276
326
|
checkedAt,
|
|
277
327
|
lastModified: stored?.lastModified ?? 0,
|
|
278
328
|
etag: stored?.etag,
|
|
329
|
+
overrides: stored?.overrides,
|
|
279
330
|
}
|
|
280
331
|
await effectiveStore.write(route, entry)
|
|
281
332
|
return {
|
|
@@ -295,6 +346,7 @@ export async function fetchRemoteCatalog(
|
|
|
295
346
|
checkedAt,
|
|
296
347
|
lastModified: Number.isNaN(lastModified) ? 0 : lastModified,
|
|
297
348
|
etag,
|
|
349
|
+
overrides: stored?.overrides,
|
|
298
350
|
}
|
|
299
351
|
await effectiveStore.write(route, storeEntry)
|
|
300
352
|
|