@aiwayds/dsh-model-sync 0.12.0 → 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.
- package/README.md +18 -17
- package/README.zh-CN.md +18 -17
- package/lib/index.d.ts +54 -376
- package/lib/index.js +305 -1049
- package/lib/index.js.map +1 -1
- package/lib/official-adapter.d.ts +80 -0
- package/lib/official-adapter.js +34 -0
- package/lib/official-adapter.js.map +1 -0
- package/lib/official-migrate.d.ts +116 -0
- package/lib/official-migrate.js +643 -0
- package/lib/official-migrate.js.map +1 -0
- package/lib/official-scheduler.d.ts +72 -0
- package/lib/official-scheduler.js +195 -0
- package/lib/official-scheduler.js.map +1 -0
- package/lib/official-store.d.ts +41 -0
- package/lib/official-store.js +84 -0
- package/lib/official-store.js.map +1 -0
- package/lib/official-sync.d.ts +68 -0
- package/lib/official-sync.js +274 -0
- package/lib/official-sync.js.map +1 -0
- package/lib/opencode-go-adapter.d.ts +56 -0
- package/lib/opencode-go-adapter.js +455 -0
- package/lib/opencode-go-adapter.js.map +1 -0
- package/lib/opencode-go-patch-file.d.ts +11 -0
- package/lib/opencode-go-patch-file.js +175 -11
- package/lib/opencode-go-patch-file.js.map +1 -1
- package/lib/opencode-go-source.d.ts +1 -1
- package/lib/opencode-go-source.js +1 -1
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +3 -3
- package/src/index.ts +351 -1214
- package/src/official-adapter.ts +111 -0
- package/src/official-migrate.ts +840 -0
- package/src/official-scheduler.ts +235 -0
- package/src/official-store.ts +122 -0
- package/src/official-sync.ts +371 -0
- package/src/opencode-go-adapter.ts +494 -0
- package/src/opencode-go-patch-file.ts +175 -9
- package/src/opencode-go-source.ts +1 -1
- package/lib/opencode-go-diagnose.d.ts +0 -75
- package/lib/opencode-go-diagnose.js +0 -146
- package/lib/opencode-go-diagnose.js.map +0 -1
- package/lib/opencode-go-grouping.d.ts +0 -7
- package/lib/opencode-go-grouping.js +0 -21
- package/lib/opencode-go-grouping.js.map +0 -1
- package/lib/opencode-go-journal.d.ts +0 -110
- package/lib/opencode-go-journal.js +0 -406
- package/lib/opencode-go-journal.js.map +0 -1
- package/lib/opencode-go-migrate.d.ts +0 -345
- package/lib/opencode-go-migrate.js +0 -1371
- package/lib/opencode-go-migrate.js.map +0 -1
- package/lib/opencode-go-migration.d.ts +0 -227
- package/lib/opencode-go-migration.js +0 -384
- package/lib/opencode-go-migration.js.map +0 -1
- package/lib/opencode-go-one-shot.d.ts +0 -276
- package/lib/opencode-go-one-shot.js +0 -1096
- package/lib/opencode-go-one-shot.js.map +0 -1
- package/lib/opencode-go-planner.d.ts +0 -154
- package/lib/opencode-go-planner.js +0 -305
- package/lib/opencode-go-planner.js.map +0 -1
- package/lib/opencode-go-scheduler.d.ts +0 -128
- package/lib/opencode-go-scheduler.js +0 -241
- package/lib/opencode-go-scheduler.js.map +0 -1
- package/lib/opencode-go-store.d.ts +0 -18
- package/lib/opencode-go-store.js +0 -68
- package/lib/opencode-go-store.js.map +0 -1
- package/lib/opencode-go-sync.d.ts +0 -94
- package/lib/opencode-go-sync.js +0 -364
- package/lib/opencode-go-sync.js.map +0 -1
- package/lib/opencode-go-writer.d.ts +0 -132
- package/lib/opencode-go-writer.js +0 -977
- package/lib/opencode-go-writer.js.map +0 -1
- package/src/opencode-go-diagnose.ts +0 -223
- package/src/opencode-go-grouping.ts +0 -26
- package/src/opencode-go-journal.ts +0 -458
- package/src/opencode-go-migrate.ts +0 -1764
- package/src/opencode-go-migration.ts +0 -552
- package/src/opencode-go-one-shot.ts +0 -1356
- package/src/opencode-go-planner.ts +0 -472
- package/src/opencode-go-scheduler.ts +0 -315
- package/src/opencode-go-store.ts +0 -73
- package/src/opencode-go-sync.ts +0 -503
- package/src/opencode-go-writer.ts +0 -1201
|
@@ -1,472 +0,0 @@
|
|
|
1
|
-
// P1a pure planner: official opencode-go records + current route state +
|
|
2
|
-
// ownership ledger → a settings-independent write plan.
|
|
3
|
-
//
|
|
4
|
-
// Hard boundary (design 07 §6.2.1): this module does no I/O, never calls
|
|
5
|
-
// settings.mutate, never touches credentials, and never schedules timers. The
|
|
6
|
-
// caller in P1b turns the returned plan into settings operations. Everything
|
|
7
|
-
// here is a pure function of its inputs so it can be exercised with offline
|
|
8
|
-
// fixtures only.
|
|
9
|
-
//
|
|
10
|
-
// 0.12.0 single-route (catalog-reuse) shape: the host only reuses pi-ai's
|
|
11
|
-
// builtin catalog provider when a route carries no `api` field, and pi-ai
|
|
12
|
-
// 1.6.0's opencode-go catalog gives every model its own per-protocol baseUrl —
|
|
13
|
-
// so a managed route must never carry `api`/`baseURL`. The plan therefore
|
|
14
|
-
// writes ONE route (the `chat` slot route) holding ALL official entries, and
|
|
15
|
-
// plans cleanup for the three legacy protocol routes: unset when fully
|
|
16
|
-
// plugin-managed, keep+warn when they hold user content. Report 19's per-slot
|
|
17
|
-
// api/baseURL tables are gone with the topology they described.
|
|
18
|
-
|
|
19
|
-
import { createHash } from 'node:crypto'
|
|
20
|
-
import type { SettingsModelProfile } from './translate.ts'
|
|
21
|
-
import type { GoModelRecord } from './opencode-go-source.ts'
|
|
22
|
-
import { deriveKeyRef } from './route-credential.ts'
|
|
23
|
-
import {
|
|
24
|
-
GO_SLOT_ROUTES,
|
|
25
|
-
type GoRouteSlot,
|
|
26
|
-
} from './opencode-go-grouping.ts'
|
|
27
|
-
|
|
28
|
-
// ---------------------------------------------------------------------------
|
|
29
|
-
// Static route-field plan (0.12.0 catalog-reuse shape)
|
|
30
|
-
// ---------------------------------------------------------------------------
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Route fields a managed route must carry. `api`/`baseURL` are deliberately
|
|
34
|
-
* ABSENT: writing them makes the host build the provider from the bare
|
|
35
|
-
* PROTOCOLS implementation (missing `x-opencode-session` header → HTTP 400)
|
|
36
|
-
* instead of the pi-ai builtin catalog provider.
|
|
37
|
-
*/
|
|
38
|
-
export interface GoPlannedRouteFields {
|
|
39
|
-
displayName: string
|
|
40
|
-
apiKeyEnv: string
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** The display name planned for the single managed route (F22/13). */
|
|
44
|
-
export const GO_DISPLAY_NAME = 'OpenCode Go'
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Legacy route fields the writer must strip from the managed route (and that
|
|
48
|
-
* old 0.11.x routes carry). They are never part of a written plan.
|
|
49
|
-
*/
|
|
50
|
-
export const GO_LEGACY_ROUTE_FIELDS = ['api', 'baseURL'] as const
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Build the planned route fields for the single managed route. `entries` is
|
|
54
|
-
* kept in the signature for symmetry with the migration caller (and future
|
|
55
|
-
* payload-driven fields); with the catalog-reuse shape the fields no longer
|
|
56
|
-
* depend on the payload — the endpoint lives in pi-ai's builtin catalog.
|
|
57
|
-
* `ref` defaults to the P0-derived `OPENCODE_GO_API_KEY`.
|
|
58
|
-
*/
|
|
59
|
-
export function planGoRouteFields(
|
|
60
|
-
entries: readonly GoModelRecord[],
|
|
61
|
-
ref: string = deriveKeyRef('opencode-go'),
|
|
62
|
-
): GoPlannedRouteFields {
|
|
63
|
-
void entries
|
|
64
|
-
return {
|
|
65
|
-
displayName: GO_DISPLAY_NAME,
|
|
66
|
-
apiKeyEnv: ref,
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// ---------------------------------------------------------------------------
|
|
71
|
-
// Model mapping (design §6.1 field mapping)
|
|
72
|
-
// ---------------------------------------------------------------------------
|
|
73
|
-
|
|
74
|
-
/** Effort keys the host schema accepts (mirrors translate.ts THINKING_LEVELS). */
|
|
75
|
-
const THINKING_LEVELS = new Set(['off', 'minimal', 'low', 'medium', 'high', 'xhigh', 'max'])
|
|
76
|
-
|
|
77
|
-
/** Official GoModelRecord → host SettingsModelProfile. `created` never enters. */
|
|
78
|
-
export function mapGoModelToProfile(entry: GoModelRecord): SettingsModelProfile {
|
|
79
|
-
const profile: SettingsModelProfile = { id: entry.id }
|
|
80
|
-
if (entry.name.length > 0) profile.name = entry.name
|
|
81
|
-
if (entry.contextWindow !== undefined) profile.contextWindow = entry.contextWindow
|
|
82
|
-
if (entry.maxOutput !== undefined) profile.maxTokens = entry.maxOutput
|
|
83
|
-
if (entry.inputModalities.length > 0) profile.input = [...entry.inputModalities]
|
|
84
|
-
if (entry.reasoning !== undefined && entry.reasoning.efforts.length > 0) {
|
|
85
|
-
const efforts: Record<string, string | null> = {}
|
|
86
|
-
for (const effort of entry.reasoning.efforts) {
|
|
87
|
-
if (THINKING_LEVELS.has(effort)) efforts[effort] = effort
|
|
88
|
-
}
|
|
89
|
-
if (Object.keys(efforts).length > 0) profile.reasoningEfforts = efforts
|
|
90
|
-
}
|
|
91
|
-
return profile
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// ---------------------------------------------------------------------------
|
|
95
|
-
// Ownership ledger (ADR-004: keyed by <route>#<keyFingerprint>)
|
|
96
|
-
// ---------------------------------------------------------------------------
|
|
97
|
-
|
|
98
|
-
export interface GoManagedModel {
|
|
99
|
-
/** Hash of the profile the plugin last wrote for this id. */
|
|
100
|
-
hash: string
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export interface GoOwnershipEntry {
|
|
104
|
-
/** Route fields the plugin last wrote (used to recognize a fully-owned route). */
|
|
105
|
-
routeFields?: Partial<GoPlannedRouteFields>
|
|
106
|
-
/** Plugin-managed model ids and the hash of their last written profile. */
|
|
107
|
-
managed: Record<string, GoManagedModel>
|
|
108
|
-
/**
|
|
109
|
-
* Raw models present in the route before the plugin first claimed it (design
|
|
110
|
-
* §6.1 / ADR-004). Audit-only: never used as a whole-route write-back.
|
|
111
|
-
* Rollback restores from `preManagedSection` (§14.3), not from this field.
|
|
112
|
-
*/
|
|
113
|
-
preManagedSnapshot?: SettingsModelProfile[]
|
|
114
|
-
/**
|
|
115
|
-
* P1d-2: the COMPLETE raw provider section captured before `migrate apply`
|
|
116
|
-
* (all fields — api/baseURL/displayName/apiKeyEnv/models — exactly as found).
|
|
117
|
-
* `null` means the route did not exist before the migration. This is the
|
|
118
|
-
* authoritative rollback target (design §14.3 whole-section snapshot) and is
|
|
119
|
-
* persisted on the same models-store queue as the rest of the ledger entry;
|
|
120
|
-
* unlike the in-progress backup it is NOT released when apply succeeds.
|
|
121
|
-
*/
|
|
122
|
-
preManagedSection?: Record<string, unknown> | null
|
|
123
|
-
/**
|
|
124
|
-
* P1d-2: ids `migrate apply` newly introduced on this route (merged desired
|
|
125
|
-
* ids absent from the pre-apply section). Rollback removes these, while ids
|
|
126
|
-
* the user adds after apply are left untouched.
|
|
127
|
-
*/
|
|
128
|
-
appliedAddedIds?: string[]
|
|
129
|
-
/**
|
|
130
|
-
* Consecutive successful rounds an id has been absent from the desired set.
|
|
131
|
-
* The planner only flags a deletion-candidate; the writer owns this counter
|
|
132
|
-
* and deletes only once it reaches `confirmRemovals` (design §6.2.4 / V5).
|
|
133
|
-
*/
|
|
134
|
-
missing?: Record<string, number>
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export type GoOwnershipLedger = Readonly<Record<string, GoOwnershipEntry>>
|
|
138
|
-
|
|
139
|
-
/** Ledger key: one route's ownership is invisible under a different key. */
|
|
140
|
-
export function goOwnershipKey(route: string, keyFingerprint: string): string {
|
|
141
|
-
return `${route}#${keyFingerprint}`
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/** The raw current state of one provider route in settings. */
|
|
145
|
-
export interface GoRouteState {
|
|
146
|
-
exists: boolean
|
|
147
|
-
api?: string
|
|
148
|
-
baseURL?: string
|
|
149
|
-
displayName?: string
|
|
150
|
-
apiKeyEnv?: string
|
|
151
|
-
/** Raw models array (no schema defaults applied). */
|
|
152
|
-
models: readonly SettingsModelProfile[]
|
|
153
|
-
/** Ids covered by a non-empty modelOverrides key on this route. */
|
|
154
|
-
overrideIds?: readonly string[]
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// ---------------------------------------------------------------------------
|
|
158
|
-
// Plan output
|
|
159
|
-
// ---------------------------------------------------------------------------
|
|
160
|
-
|
|
161
|
-
export interface GoRouteConflict {
|
|
162
|
-
slot: GoRouteSlot
|
|
163
|
-
route: string
|
|
164
|
-
field: 'apiKeyEnv'
|
|
165
|
-
expected: string
|
|
166
|
-
actual: string
|
|
167
|
-
reason: string
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export type GoPlanReportLevel = 'info' | 'warn' | 'error'
|
|
171
|
-
|
|
172
|
-
export interface GoPlanReportEntry {
|
|
173
|
-
level: GoPlanReportLevel
|
|
174
|
-
/** Stable machine code (e.g. route-conflict, kept-user-owned, legacy-fields-strip). */
|
|
175
|
-
code: string
|
|
176
|
-
route?: string
|
|
177
|
-
slot?: GoRouteSlot
|
|
178
|
-
id?: string
|
|
179
|
-
message: string
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
export interface GoPlannedRoute {
|
|
183
|
-
slot: GoRouteSlot
|
|
184
|
-
route: string
|
|
185
|
-
fields: GoPlannedRouteFields
|
|
186
|
-
/** Complete merged models array, sorted by id (never an empty write — see unset/skip). */
|
|
187
|
-
models: SettingsModelProfile[]
|
|
188
|
-
/** Empty and fully plugin-managed (or superseded legacy): caller should unset providers.<route>. */
|
|
189
|
-
unset: boolean
|
|
190
|
-
/** Conflict, empty main route, or legacy route with user content: write nothing. */
|
|
191
|
-
skip: boolean
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
export interface GoPlanResult {
|
|
195
|
-
routes: GoPlannedRoute[]
|
|
196
|
-
/** Routes the caller should unset (never paired with `models: []`). */
|
|
197
|
-
unsetRoutes: string[]
|
|
198
|
-
conflicts: GoRouteConflict[]
|
|
199
|
-
report: GoPlanReportEntry[]
|
|
200
|
-
/** Shallow copy of the ledger updated only for this key's routes. */
|
|
201
|
-
nextOwnership: Record<string, GoOwnershipEntry>
|
|
202
|
-
/**
|
|
203
|
-
* The mapped desired profiles per route BEFORE the merge with current
|
|
204
|
-
* settings. The writer needs this to do a first-round conservative claim
|
|
205
|
-
* (official desired ∩ current, never ids outside the desired set) — the
|
|
206
|
-
* merged `routes[].models` already contains kept user entries and cannot be
|
|
207
|
-
* used for that distinction.
|
|
208
|
-
*/
|
|
209
|
-
desiredByRoute: Record<string, SettingsModelProfile[]>
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
export interface GoPlannerOptions {
|
|
213
|
-
/** sha256(key) first 16 hex from the source result; '-' when no key. */
|
|
214
|
-
keyFingerprint: string
|
|
215
|
-
entries: readonly GoModelRecord[]
|
|
216
|
-
/** Current state of the go routes, keyed by route id. */
|
|
217
|
-
currentRoutes: Readonly<Record<string, GoRouteState>>
|
|
218
|
-
ownership: GoOwnershipLedger
|
|
219
|
-
/** go.ref; defaults to P0's deriveKeyRef('opencode-go'). */
|
|
220
|
-
ref?: string
|
|
221
|
-
/** go.routes overrides (default GO_SLOT_ROUTES). */
|
|
222
|
-
routes?: Partial<Record<GoRouteSlot, string>>
|
|
223
|
-
/** go.pinnedModels — protected from deletion (design A4). */
|
|
224
|
-
pinnedModels?: readonly string[]
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// ---------------------------------------------------------------------------
|
|
228
|
-
// Helpers
|
|
229
|
-
// ---------------------------------------------------------------------------
|
|
230
|
-
|
|
231
|
-
function canonicalize(value: unknown): unknown {
|
|
232
|
-
if (Array.isArray(value)) return value.map(canonicalize)
|
|
233
|
-
if (typeof value !== 'object' || value === null) return value
|
|
234
|
-
const out: Record<string, unknown> = {}
|
|
235
|
-
for (const key of Object.keys(value as Record<string, unknown>).sort()) {
|
|
236
|
-
out[key] = canonicalize((value as Record<string, unknown>)[key])
|
|
237
|
-
}
|
|
238
|
-
return out
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/** Stable hash of a settings profile — the "现值哈希" of the deletion gates. */
|
|
242
|
-
export function profileHash(profile: SettingsModelProfile): string {
|
|
243
|
-
return createHash('sha256').update(JSON.stringify(canonicalize(profile))).digest('hex')
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
const OWNERSHIP_FIELDS = ['apiKeyEnv'] as const
|
|
247
|
-
|
|
248
|
-
/** A current route conflicts only when a field is explicitly set to a different value. */
|
|
249
|
-
function detectConflict(
|
|
250
|
-
slot: GoRouteSlot,
|
|
251
|
-
route: string,
|
|
252
|
-
fields: GoPlannedRouteFields,
|
|
253
|
-
current: GoRouteState | undefined,
|
|
254
|
-
): GoRouteConflict | undefined {
|
|
255
|
-
if (current === undefined || !current.exists) return undefined
|
|
256
|
-
for (const field of OWNERSHIP_FIELDS) {
|
|
257
|
-
const actual = current[field]
|
|
258
|
-
if (actual !== undefined && actual !== '' && actual !== fields[field]) {
|
|
259
|
-
return {
|
|
260
|
-
slot,
|
|
261
|
-
route,
|
|
262
|
-
field,
|
|
263
|
-
expected: fields[field],
|
|
264
|
-
actual,
|
|
265
|
-
reason: 'existing route field differs from plan; keep name, skip write (design §9.3)',
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
return undefined
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
function ownedFieldsMatch(
|
|
273
|
-
owned: GoOwnershipEntry | undefined,
|
|
274
|
-
fields: GoPlannedRouteFields,
|
|
275
|
-
): boolean {
|
|
276
|
-
if (owned === undefined || owned.routeFields === undefined) return false
|
|
277
|
-
return OWNERSHIP_FIELDS.every((field) => owned.routeFields![field] === fields[field])
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
// ---------------------------------------------------------------------------
|
|
281
|
-
// Planner
|
|
282
|
-
// ---------------------------------------------------------------------------
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* Build the write plan. Pure: identical inputs always produce an identical
|
|
286
|
-
* plan (report/order included). No mutation of `options` or its nested values.
|
|
287
|
-
*
|
|
288
|
-
* Topology (0.12.0): ONE managed route (the `chat` slot route) receives ALL
|
|
289
|
-
* official entries; the three legacy protocol routes (msg/resp/google) are
|
|
290
|
-
* cleanup targets — unset when fully plugin-managed, kept with a warn when
|
|
291
|
-
* they hold user content.
|
|
292
|
-
*/
|
|
293
|
-
export function planGoRoutes(options: GoPlannerOptions): GoPlanResult {
|
|
294
|
-
const ref = options.ref ?? deriveKeyRef('opencode-go')
|
|
295
|
-
const routes: Record<GoRouteSlot, string> = { ...GO_SLOT_ROUTES, ...(options.routes ?? {}) }
|
|
296
|
-
const pinned = new Set(options.pinnedModels ?? [])
|
|
297
|
-
const report: GoPlanReportEntry[] = []
|
|
298
|
-
const conflicts: GoRouteConflict[] = []
|
|
299
|
-
const plannedRoutes: GoPlannedRoute[] = []
|
|
300
|
-
const unsetRoutes: string[] = []
|
|
301
|
-
const nextOwnership: Record<string, GoOwnershipEntry> = { ...options.ownership }
|
|
302
|
-
const desiredByRoute: Record<string, SettingsModelProfile[]> = {}
|
|
303
|
-
|
|
304
|
-
const entryById = new Map(options.entries.map((entry) => [entry.id, entry]))
|
|
305
|
-
// Single-route desired set: every official entry, in stable id order.
|
|
306
|
-
const desiredIds = [...entryById.keys()].sort((a, b) => a.localeCompare(b))
|
|
307
|
-
|
|
308
|
-
// --- The single managed route (chat slot) --------------------------------
|
|
309
|
-
const managedSlot: GoRouteSlot = 'chat'
|
|
310
|
-
const managedRoute = routes[managedSlot]
|
|
311
|
-
const current = options.currentRoutes[managedRoute]
|
|
312
|
-
const ledgerKey = goOwnershipKey(managedRoute, options.keyFingerprint)
|
|
313
|
-
const owned = options.ownership[ledgerKey]
|
|
314
|
-
const fields = planGoRouteFields(options.entries, ref)
|
|
315
|
-
desiredByRoute[managedRoute] = []
|
|
316
|
-
|
|
317
|
-
if (desiredIds.length === 0) {
|
|
318
|
-
const currentModels = current?.models ?? []
|
|
319
|
-
if (current !== undefined && current.exists) {
|
|
320
|
-
// §9.4: an empty main route after a 200 is an upstream anomaly — keep
|
|
321
|
-
// last-good, write nothing.
|
|
322
|
-
plannedRoutes.push({ slot: managedSlot, route: managedRoute, fields, models: [...currentModels], unset: false, skip: true })
|
|
323
|
-
report.push({ level: 'warn', code: 'upstream-empty', route: managedRoute, slot: managedSlot, message: 'main route has no members; kept last-good, no write' })
|
|
324
|
-
}
|
|
325
|
-
} else {
|
|
326
|
-
const conflict = detectConflict(managedSlot, managedRoute, fields, current)
|
|
327
|
-
if (conflict !== undefined) {
|
|
328
|
-
conflicts.push(conflict)
|
|
329
|
-
report.push({
|
|
330
|
-
level: 'error',
|
|
331
|
-
code: 'route-conflict',
|
|
332
|
-
route: managedRoute,
|
|
333
|
-
slot: managedSlot,
|
|
334
|
-
message: `${managedRoute}.${conflict.field} is ${conflict.actual}, expected ${conflict.expected}`,
|
|
335
|
-
})
|
|
336
|
-
} else {
|
|
337
|
-
const desiredById = new Map<string, SettingsModelProfile>()
|
|
338
|
-
for (const id of desiredIds) {
|
|
339
|
-
const entry = entryById.get(id)
|
|
340
|
-
if (entry !== undefined) desiredById.set(id, mapGoModelToProfile(entry))
|
|
341
|
-
}
|
|
342
|
-
desiredByRoute[managedRoute] = [...desiredById.values()]
|
|
343
|
-
|
|
344
|
-
// Catalog-reuse strip notice: a route still carrying the old 0.11.x
|
|
345
|
-
// `api`/`baseURL` fields gets them unset this round (the writer emits
|
|
346
|
-
// the ops; this line explains why in the report).
|
|
347
|
-
if (current !== undefined && current.exists
|
|
348
|
-
&& GO_LEGACY_ROUTE_FIELDS.some((field) => current[field] !== undefined && current[field] !== '')) {
|
|
349
|
-
report.push({
|
|
350
|
-
level: 'info',
|
|
351
|
-
code: 'legacy-fields-strip',
|
|
352
|
-
route: managedRoute,
|
|
353
|
-
slot: managedSlot,
|
|
354
|
-
message: `route ${managedRoute} carries legacy api/baseURL fields; unsetting them so the host reuses the pi-ai builtin catalog provider (catalog-reuse fix)`,
|
|
355
|
-
})
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
const currentModels = current?.models ?? []
|
|
359
|
-
const managed = owned?.managed ?? {}
|
|
360
|
-
const nextManaged: Record<string, GoManagedModel> = {}
|
|
361
|
-
const kept: SettingsModelProfile[] = []
|
|
362
|
-
|
|
363
|
-
for (const model of currentModels) {
|
|
364
|
-
const id = model.id
|
|
365
|
-
const desired = desiredById.get(id)
|
|
366
|
-
const record = managed[id]
|
|
367
|
-
const hashMatches = record !== undefined && record.hash === profileHash(model)
|
|
368
|
-
const hasOverride = (current?.overrideIds ?? []).includes(id)
|
|
369
|
-
const isPinned = pinned.has(id)
|
|
370
|
-
|
|
371
|
-
if (desired !== undefined) {
|
|
372
|
-
if (record !== undefined && hashMatches) {
|
|
373
|
-
// Plugin-owned and untouched: overwrite with the freshly mapped value.
|
|
374
|
-
const next = desired
|
|
375
|
-
nextManaged[id] = { hash: profileHash(next) }
|
|
376
|
-
kept.push(next)
|
|
377
|
-
report.push({ level: 'info', code: 'overwrote-managed', route: managedRoute, slot: managedSlot, id, message: `refreshed plugin-managed model ${id}` })
|
|
378
|
-
} else {
|
|
379
|
-
// Not in ledger, or hash differs (user edited): keep as-is, never claim.
|
|
380
|
-
kept.push(model)
|
|
381
|
-
if (record !== undefined) nextManaged[id] = record
|
|
382
|
-
report.push({
|
|
383
|
-
level: 'info',
|
|
384
|
-
code: 'kept-user-owned',
|
|
385
|
-
route: managedRoute,
|
|
386
|
-
slot: managedSlot,
|
|
387
|
-
id,
|
|
388
|
-
message: record !== undefined ? `model ${id} was modified; kept user value` : `model ${id} is user-owned; kept`,
|
|
389
|
-
})
|
|
390
|
-
}
|
|
391
|
-
continue
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
// Not desired: the three deletion gates (ADR-005 / §6.2.4).
|
|
395
|
-
if (record !== undefined && hashMatches && !hasOverride && !isPinned) {
|
|
396
|
-
report.push({ level: 'info', code: 'deletion-candidate', route: managedRoute, slot: managedSlot, id, message: `model ${id} passed all three deletion gates` })
|
|
397
|
-
continue
|
|
398
|
-
}
|
|
399
|
-
kept.push(model)
|
|
400
|
-
if (record !== undefined) nextManaged[id] = record
|
|
401
|
-
const code = record === undefined
|
|
402
|
-
? 'kept-user-owned'
|
|
403
|
-
: !hashMatches
|
|
404
|
-
? 'kept-modified'
|
|
405
|
-
: hasOverride
|
|
406
|
-
? 'kept-override'
|
|
407
|
-
: 'kept-pinned'
|
|
408
|
-
report.push({ level: 'info', code, route: managedRoute, slot: managedSlot, id, message: `model ${id} kept (${code}); not removed` })
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
// New desired ids (absent from settings) are added and claimed.
|
|
412
|
-
const currentIds = new Set(currentModels.map((model) => model.id))
|
|
413
|
-
for (const id of desiredIds) {
|
|
414
|
-
if (currentIds.has(id)) continue
|
|
415
|
-
const profile = desiredById.get(id)
|
|
416
|
-
if (profile === undefined) continue
|
|
417
|
-
nextManaged[id] = { hash: profileHash(profile) }
|
|
418
|
-
kept.push(profile)
|
|
419
|
-
report.push({ level: 'info', code: 'added', route: managedRoute, slot: managedSlot, id, message: `added model ${id}` })
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
// `desiredIds` is non-empty here, so the merged array is never empty.
|
|
423
|
-
const merged = [...kept].sort((a, b) => a.id.localeCompare(b.id))
|
|
424
|
-
plannedRoutes.push({ slot: managedSlot, route: managedRoute, fields, models: merged, unset: false, skip: false })
|
|
425
|
-
// P1d-2 ownership: inherit the prior entry so migration-period fields
|
|
426
|
-
// (`preManagedSection` / `appliedAddedIds` and any future ones) survive the
|
|
427
|
-
// plan round-trip untouched. The planner never writes or clears these — it
|
|
428
|
-
// only refreshes the fields it owns (routeFields / managed). The go writer
|
|
429
|
-
// applies the same merge when it persists the ledger entry.
|
|
430
|
-
nextOwnership[ledgerKey] = { ...owned, routeFields: fields, managed: nextManaged }
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
// --- Legacy protocol routes: cleanup plan ---------------------------------
|
|
435
|
-
// Only on a healthy payload: an anomalous empty listing must not trigger
|
|
436
|
-
// route teardown on top of the upstream anomaly.
|
|
437
|
-
if (desiredIds.length > 0) {
|
|
438
|
-
for (const slot of ['msg', 'resp', 'google'] as const) {
|
|
439
|
-
const route = routes[slot]
|
|
440
|
-
const routeCurrent = options.currentRoutes[route]
|
|
441
|
-
const legacyLedgerKey = goOwnershipKey(route, options.keyFingerprint)
|
|
442
|
-
const legacyOwned = options.ownership[legacyLedgerKey]
|
|
443
|
-
if ((routeCurrent === undefined || !routeCurrent.exists) && legacyOwned === undefined) continue
|
|
444
|
-
|
|
445
|
-
const legacyFields = planGoRouteFields(options.entries, ref)
|
|
446
|
-
const currentModels = routeCurrent?.models ?? []
|
|
447
|
-
const managed = legacyOwned?.managed ?? {}
|
|
448
|
-
// Fully managed = the plugin owns every model present (an empty route
|
|
449
|
-
// with a ledger entry counts: it was emptied by a previous round) and
|
|
450
|
-
// the recorded ownership fields match this plan.
|
|
451
|
-
const fullyManaged = legacyOwned !== undefined
|
|
452
|
-
&& currentModels.every((model) => {
|
|
453
|
-
const record = managed[model.id]
|
|
454
|
-
return record !== undefined && record.hash === profileHash(model)
|
|
455
|
-
})
|
|
456
|
-
&& ownedFieldsMatch(legacyOwned, legacyFields)
|
|
457
|
-
if (fullyManaged) {
|
|
458
|
-
plannedRoutes.push({ slot, route, fields: legacyFields, models: [], unset: true, skip: false })
|
|
459
|
-
unsetRoutes.push(route)
|
|
460
|
-
report.push({ level: 'info', code: 'unset-legacy-route', route, slot, message: `legacy route ${route} is superseded by the single managed route and fully plugin-managed; unset` })
|
|
461
|
-
delete nextOwnership[legacyLedgerKey]
|
|
462
|
-
} else {
|
|
463
|
-
plannedRoutes.push({ slot, route, fields: legacyFields, models: [...currentModels], unset: false, skip: true })
|
|
464
|
-
if (currentModels.length > 0) {
|
|
465
|
-
report.push({ level: 'warn', code: 'legacy-route-kept', route, slot, message: `legacy route ${route} holds user content; kept as-is (superseded by the single managed route)` })
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
return { routes: plannedRoutes, unsetRoutes, conflicts, report, nextOwnership, desiredByRoute }
|
|
472
|
-
}
|