@aiwayds/dsh-model-sync 0.12.0 → 0.12.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 (41) hide show
  1. package/lib/index.d.ts +8 -0
  2. package/lib/index.js +90 -33
  3. package/lib/index.js.map +1 -1
  4. package/lib/opencode-go-diagnose.d.ts +22 -1
  5. package/lib/opencode-go-diagnose.js +36 -5
  6. package/lib/opencode-go-diagnose.js.map +1 -1
  7. package/lib/opencode-go-host-catalog.d.ts +42 -0
  8. package/lib/opencode-go-host-catalog.js +98 -0
  9. package/lib/opencode-go-host-catalog.js.map +1 -0
  10. package/lib/opencode-go-journal.d.ts +6 -2
  11. package/lib/opencode-go-journal.js +1 -1
  12. package/lib/opencode-go-journal.js.map +1 -1
  13. package/lib/opencode-go-migrate.d.ts +40 -7
  14. package/lib/opencode-go-migrate.js +192 -34
  15. package/lib/opencode-go-migrate.js.map +1 -1
  16. package/lib/opencode-go-migration.d.ts +43 -0
  17. package/lib/opencode-go-migration.js +91 -2
  18. package/lib/opencode-go-migration.js.map +1 -1
  19. package/lib/opencode-go-one-shot.d.ts +6 -0
  20. package/lib/opencode-go-one-shot.js +36 -1
  21. package/lib/opencode-go-one-shot.js.map +1 -1
  22. package/lib/opencode-go-planner.d.ts +14 -0
  23. package/lib/opencode-go-planner.js +112 -10
  24. package/lib/opencode-go-planner.js.map +1 -1
  25. package/lib/opencode-go-sync.d.ts +16 -3
  26. package/lib/opencode-go-sync.js +24 -0
  27. package/lib/opencode-go-sync.js.map +1 -1
  28. package/lib/opencode-go-writer.d.ts +17 -1
  29. package/lib/opencode-go-writer.js +94 -10
  30. package/lib/opencode-go-writer.js.map +1 -1
  31. package/package.json +2 -2
  32. package/src/index.ts +100 -34
  33. package/src/opencode-go-diagnose.ts +58 -5
  34. package/src/opencode-go-host-catalog.ts +125 -0
  35. package/src/opencode-go-journal.ts +5 -3
  36. package/src/opencode-go-migrate.ts +231 -44
  37. package/src/opencode-go-migration.ts +116 -2
  38. package/src/opencode-go-one-shot.ts +44 -1
  39. package/src/opencode-go-planner.ts +120 -10
  40. package/src/opencode-go-sync.ts +40 -2
  41. package/src/opencode-go-writer.ts +110 -10
@@ -27,6 +27,7 @@ import {
27
27
  type GoPlanResult,
28
28
  type GoPlannedRoute,
29
29
  } from './opencode-go-planner.ts'
30
+ import { HOST_CATALOG_PROVIDER } from './opencode-go-host-catalog.ts'
30
31
  import {
31
32
  profilesEqual,
32
33
  type Logger,
@@ -92,6 +93,16 @@ export interface GoWriterDeps {
92
93
  logger: Logger
93
94
  /** sha256(key) prefix from the source result; `-` when no key. */
94
95
  keyFingerprint: string
96
+ /**
97
+ * Host runtime catalog ids for opencode-go (0.12.1). `undefined` keeps the
98
+ * 0.12.0 writer behavior. With an array the writer (H5): removes
99
+ * deletion-candidates the host catalog does not know IMMEDIATELY (the
100
+ * double-round confirmation deadlocks when round 1's write-back is rejected
101
+ * wholesale and the counter can never persist), and refuses to write any
102
+ * route whose final models array still carries ids outside the catalog
103
+ * (fail-closed with an actionable message instead of a raw host rejection).
104
+ */
105
+ hostCatalogIds?: readonly string[]
95
106
  /** go.confirmRemovals; default 2 (schema min 2, design §6.2.6). */
96
107
  confirmRemovals?: number
97
108
  /**
@@ -130,8 +141,12 @@ export interface GoWriterDeps {
130
141
  * both succeeded, with the ledger-managed ids just committed. The callback
131
142
  * re-persists `appliedFingerprint` through the same store queue. Never called
132
143
  * for a failed write, a no-change round, or another writer's no-op success.
144
+ * H4/M2: the third argument marks a whole-section unset (the ledger key was
145
+ * deleted, not written) so the record update can DROP the route's
146
+ * `appliedManagedIds` key instead of persisting a phantom `[]` entry no
147
+ * ledger backs.
133
148
  */
134
- onOwnWrite?: (route: string, managedIds: string[]) => Promise<void> | void
149
+ onOwnWrite?: (route: string, managedIds: string[], info?: { unset: boolean }) => Promise<void> | void
135
150
  /**
136
151
  * O1/C2: write-ahead journal store. When present together with `profileKey`,
137
152
  * each settings mutate is preceded by a journal slot recording the intended
@@ -400,10 +415,11 @@ async function recordOwnWrite(
400
415
  route: string,
401
416
  managedIds: string[],
402
417
  report: GoPlanReportEntry[],
418
+ unset = false,
403
419
  ): Promise<boolean> {
404
420
  if (deps.onOwnWrite === undefined) return true
405
421
  try {
406
- await deps.onOwnWrite(route, managedIds)
422
+ await deps.onOwnWrite(route, managedIds, { unset })
407
423
  return true
408
424
  } catch (err) {
409
425
  if (isStoreLockTimeout(err)) {
@@ -418,9 +434,13 @@ async function recordOwnWrite(
418
434
  })
419
435
  return false
420
436
  }
421
- // Settings + ledger already landed; a failed fingerprint write is healed at
422
- // the next round (§14.7.4 fingerprint-healed), never a failed route. O1:
423
- // still report it truthfully instead of leaving the record gap silent.
437
+ // Settings + ledger already landed; a failed fingerprint write is NOT a
438
+ // failed route. H4: this failure must reach recordOwnWrite (report +
439
+ // `false`) so the C2 write-ahead journal is KEPT and the next round's
440
+ // reconciliation re-commits the record — "fingerprint-healed" cannot cover
441
+ // it (it only fires on an identity match, and this write changed the
442
+ // identity). O1: still report it truthfully instead of leaving the record
443
+ // gap silent.
424
444
  deps.logger.warn('appliedFingerprint update failed for %s: %s', route, errorText(err))
425
445
  report.push({
426
446
  level: 'warn',
@@ -558,6 +578,12 @@ async function buildRouteOps(
558
578
  overrideStorer: GoOverrideStorer | undefined,
559
579
  ): Promise<RouteOps | undefined> {
560
580
  const data = rawRoute(desc, planned.route)
581
+ // M3: a planned whole-route unset with NO live section needs only the LEDGER
582
+ // delete — a settings unset of an absent section is a pointless mutate (and
583
+ // a revision bump). Return no ops; the caller's no-change path still deletes
584
+ // the ledger key via persistLedger's unset branch. Checked before any
585
+ // override-store read so the cleanup does zero pointless I/O.
586
+ if (planned.unset && data === undefined) return undefined
561
587
  const settingsOverrides = rawOverrides(desc, planned.route)
562
588
 
563
589
  // Settings key wins; store replay is the fallback (§6.2.4 store-first).
@@ -585,12 +611,18 @@ async function buildRouteOps(
585
611
  ops.push({ op: 'set', path: ['providers', planned.route, field], value: planned.fields[field] })
586
612
  }
587
613
  }
588
- // Catalog-reuse: a route still carrying the 0.11.x `api`/`baseURL` fields
589
- // gets them unset — with any `api` present the host leaves pi-ai's builtin
590
- // catalog provider (and its x-opencode-session wrapping) behind.
614
+ // Catalog-reuse (H3 scope): only the BUILTIN catalog provider route
615
+ // ('opencode-go') may lose non-empty `api`/`baseURL` — the host reuses
616
+ // pi-ai's builtin catalog provider (and its x-opencode-session wrapping)
617
+ // for that id alone. A custom go.routes.chat name or any other route
618
+ // NEEDS api/baseURL to be servable, so they are preserved. Empty-string
619
+ // fields are broken values the host rejects outright and are stripped
620
+ // everywhere.
621
+ const stripCatalogReuse = planned.route === HOST_CATALOG_PROVIDER
591
622
  for (const field of GO_LEGACY_ROUTE_FIELDS) {
592
623
  const value = data?.[field]
593
- if (value !== undefined && value !== '') {
624
+ if (value === undefined) continue
625
+ if (value === '' || stripCatalogReuse) {
594
626
  ops.push({ op: 'unset', path: ['providers', planned.route, field] })
595
627
  }
596
628
  }
@@ -649,6 +681,17 @@ export async function writeGoPlan(plan: GoPlanResult, deps: GoWriterDeps): Promi
649
681
  })
650
682
  }
651
683
 
684
+ // H2: whether the main (chat-slot) route's write landed healthy this round.
685
+ // Legacy route unsets are gated on it — the writer executes routes
686
+ // independently and the host validates route REMOVAL against no catalog, so
687
+ // a failed/conflicted main-route write followed by successful legacy unsets
688
+ // would silently lose the user's non-chat-protocol models.
689
+ const mainRouteReady = (): boolean => {
690
+ const rows = result.routes.filter((row) => row.slot === 'chat')
691
+ return rows.some((row) => row.status === 'wrote' || row.status === 'no-change')
692
+ && !rows.some((row) => row.status === 'failed' || row.status === 'conflict')
693
+ }
694
+
652
695
  const desc = settings === undefined ? undefined : findLlmDescriptor(settings)
653
696
  if (settings === undefined || desc === undefined) {
654
697
  for (const planned of plan.routes) {
@@ -674,6 +717,20 @@ export async function writeGoPlan(plan: GoPlanResult, deps: GoWriterDeps): Promi
674
717
  continue
675
718
  }
676
719
 
720
+ // H2: legacy teardown is gated on a healthy main-route write this round
721
+ // (see mainRouteReady above). No settings write, no ledger touch.
722
+ if (planned.unset && planned.slot !== 'chat' && !mainRouteReady()) {
723
+ result.routes.push({ route: planned.route, slot: planned.slot, status: 'skipped', reason: 'main-route-not-ready' })
724
+ report.push({
725
+ level: 'warn',
726
+ code: 'main-route-not-ready',
727
+ route: planned.route,
728
+ slot: planned.slot,
729
+ message: `legacy route ${planned.route} unset deferred: the main route write did not land this round; no teardown while the managed route is not healthy`,
730
+ })
731
+ continue
732
+ }
733
+
677
734
  // Single-writer second gate (§14.7.5): re-read the persisted status here,
678
735
  // never reuse the round-level snapshot. A route whose authority was revoked
679
736
  // between the round gate and this point is skipped with a report line —
@@ -716,10 +773,32 @@ export async function writeGoPlan(plan: GoPlanResult, deps: GoWriterDeps): Promi
716
773
  // --- Double-round deletion confirmation (V5) ---------------------------
717
774
  // The planner flags a candidate after its three gates pass; only the
718
775
  // writer knows how many consecutive successful rounds have flagged it.
776
+ // H5: a candidate the HOST RUNTIME CATALOG does not know skips the counter
777
+ // entirely — the host rejects the whole models array on such an id, so
778
+ // round 1's write-back fails and the counter can never persist (a
779
+ // permanent write-failed loop). Removing it immediately is what restores
780
+ // writability; the id auto-returns when the host upgrades.
781
+ //
782
+ // P1-1 scope: that rejection exists only for the route reusing the builtin
783
+ // catalog (chat slot + the builtin provider id). A custom go.routes.chat
784
+ // name keeps its own api/baseURL and is never validated against the
785
+ // runtime catalog, so its deletions follow the normal two-round path.
786
+ const enforcesHostCatalog = planned.slot === 'chat' && planned.route === HOST_CATALOG_PROVIDER
787
+ const hostSet = enforcesHostCatalog && deps.hostCatalogIds !== undefined
788
+ ? new Set(deps.hostCatalogIds)
789
+ : undefined
719
790
  const missing: Record<string, number> = { ...(prior?.missing ?? {}) }
720
791
  const finalModels = [...planned.models]
721
792
  for (const candidate of candidates) {
722
793
  const id = candidate.id!
794
+ const hostUnknown = hostSet !== undefined && !hostSet.has(id)
795
+ if (hostUnknown) {
796
+ // Confirmed immediately: let the planner's exclusion stand — delete.
797
+ delete missing[id]
798
+ candidate.code = 'deleted'
799
+ candidate.message = `model ${id} removed immediately: the host runtime catalog does not know it (its presence would make the host reject every mutate); it auto-returns after the host upgrades`
800
+ continue
801
+ }
723
802
  const seen = missing[id] ?? 0
724
803
  if (seen + 1 < confirmRemovals) {
725
804
  // Round 1 of 2 (or earlier for a larger threshold): keep the model.
@@ -746,6 +825,27 @@ export async function writeGoPlan(plan: GoPlanResult, deps: GoWriterDeps): Promi
746
825
  }
747
826
  const sortedFinal = sortedModels(finalModels)
748
827
 
828
+ // H5 (fail-closed): refuse the route write when the FINAL models array
829
+ // would still carry ids the host runtime catalog does not know — the host
830
+ // rejects the whole mutate, so writing is guaranteed to fail while
831
+ // pretending otherwise. Zero writes for this route; the report names the
832
+ // ids and both actionable exits.
833
+ if (hostSet !== undefined && !planned.unset) {
834
+ const unknownIds = sortedFinal.filter((model) => !hostSet.has(model.id)).map((model) => model.id)
835
+ if (unknownIds.length > 0) {
836
+ result.routes.push({ route: planned.route, slot: planned.slot, status: 'failed', reason: 'host-catalog-unknown-models' })
837
+ result.failedRoutes.push({ route: planned.route, reason: 'host-catalog-unknown-models' })
838
+ report.push({
839
+ level: 'error',
840
+ code: 'host-catalog-unknown-models',
841
+ route: planned.route,
842
+ slot: planned.slot,
843
+ message: `route ${planned.route} not written: ${unknownIds.join(', ')} are not in the host runtime catalog and the host would reject the whole mutate — keep them on a custom route (with api/baseURL) or wait for the host upgrade; zero settings writes for this route`,
844
+ })
845
+ continue
846
+ }
847
+ }
848
+
749
849
  // --- Ledger to persist if this route's write succeeds ------------------
750
850
  const nextManaged: Record<string, GoManagedModel> = { ...(plan.nextOwnership[key]?.managed ?? {}) }
751
851
  // A pending candidate is still plugin-managed: keep it in the ledger so a
@@ -1076,7 +1176,7 @@ export async function writeGoPlan(plan: GoPlanResult, deps: GoWriterDeps): Promi
1076
1176
  }
1077
1177
  if (commit.committed) {
1078
1178
  const recordPersisted = await recordOwnWrite(
1079
- deps, planned.route, planned.unset ? [] : Object.keys(nextEntry.managed).sort(), report,
1179
+ deps, planned.route, planned.unset ? [] : Object.keys(nextEntry.managed).sort(), report, planned.unset,
1080
1180
  )
1081
1181
  // C2: only a fully accounted write (settings + ledger + record) closes the
1082
1182
  // journal. A failed record persist (S1b) keeps it so the next round's