@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,315 +0,0 @@
|
|
|
1
|
-
// P1c-2: async timer + scheduling layer for the opencode-go sync round.
|
|
2
|
-
//
|
|
3
|
-
// This module owns ONLY scheduling (design §6.3 / §14.6): startup delay +
|
|
4
|
-
// jitter, the 60min (floor 15) interval with live re-arm, single-flight with
|
|
5
|
-
// the manual /model-sync go command, failure backoff with caps, and timer
|
|
6
|
-
// cleanup on plugin unload. It does NOT run the round itself — `runRound` is
|
|
7
|
-
// injected and is normally index.ts's runGoRound (source → planner → writer).
|
|
8
|
-
//
|
|
9
|
-
// Every clock/random input is injected (GoSchedulerTimers / GoSchedulerRandom),
|
|
10
|
-
// so the whole state machine runs deterministically under a fake timer: no
|
|
11
|
-
// real sleep, no network. The production adapters live in index.ts.
|
|
12
|
-
//
|
|
13
|
-
// Last-good safety: scheduling never persists anything. Backoff state is an
|
|
14
|
-
// in-memory streak only. A failed round returns before the writer, so settings
|
|
15
|
-
// and the ownership ledger keep their last-good values (design §6.3 "离线:
|
|
16
|
-
// settings 中 last-good 继续可用"). M-4 zero-write is enforced by routeWriter
|
|
17
|
-
// inside the round (P1d-1); this layer only adds the M-5 `applying` drop.
|
|
18
|
-
|
|
19
|
-
import type { GoFailureKind } from './opencode-go-source.ts'
|
|
20
|
-
|
|
21
|
-
/** Cordis service seams used by index.ts to inject test doubles. */
|
|
22
|
-
export const GO_SCHEDULER_TIMERS_SERVICE = 'goSchedulerTimers'
|
|
23
|
-
export const GO_SCHEDULER_RANDOM_SERVICE = 'goSchedulerRandom'
|
|
24
|
-
|
|
25
|
-
/** Trigger origin, recorded in every report so auto/manual rounds are distinguishable. */
|
|
26
|
-
export type GoTrigger = 'startup' | 'interval' | 'backoff' | 'manual'
|
|
27
|
-
|
|
28
|
-
/** The subset of Config the scheduler reads (live, via a callback). */
|
|
29
|
-
export interface GoSchedulerConfig {
|
|
30
|
-
/** Master switch. Design §6.2.6 default false (unchanged by P1d-1). */
|
|
31
|
-
enabled: boolean
|
|
32
|
-
/** go.intervalMinutes — default 60, floor 15 (design §6.3 / ADR-007). */
|
|
33
|
-
intervalMinutes: number
|
|
34
|
-
/** startupDelaySeconds — jitter is added on top (design §6.3). */
|
|
35
|
-
startupDelaySeconds: number
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** Opaque timer handle (platform Timeout in production, an id in fake timers). */
|
|
39
|
-
export type GoTimerHandle = unknown
|
|
40
|
-
|
|
41
|
-
/** Minimal clock surface the scheduler needs. Injectable for deterministic tests. */
|
|
42
|
-
export interface GoSchedulerTimers {
|
|
43
|
-
setTimeout(fn: () => void, ms: number): GoTimerHandle
|
|
44
|
-
clearTimeout(handle: GoTimerHandle): void
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** Injectable [0,1) source so startup jitter is deterministic in tests. */
|
|
48
|
-
export interface GoSchedulerRandom {
|
|
49
|
-
next(): number
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** What the scheduler needs back from one round to drive backoff + reporting. */
|
|
53
|
-
export interface GoRoundOutcome {
|
|
54
|
-
ok: boolean
|
|
55
|
-
reason: string
|
|
56
|
-
/** Report text — passed through unchanged for real rounds (design item 3). */
|
|
57
|
-
text: string
|
|
58
|
-
/** Source failure taxonomy; set only when the round reached the source. */
|
|
59
|
-
failureKind?: GoFailureKind
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export interface GoRoundRequest {
|
|
63
|
-
trigger: GoTrigger
|
|
64
|
-
adopt: boolean
|
|
65
|
-
/** Aborted by dispose() so an in-flight round stops before it can write settings. */
|
|
66
|
-
signal: AbortSignal
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export interface GoSchedulerDeps {
|
|
70
|
-
config: () => GoSchedulerConfig
|
|
71
|
-
timers: GoSchedulerTimers
|
|
72
|
-
random: GoSchedulerRandom
|
|
73
|
-
runRound: (request: GoRoundRequest) => Promise<GoRoundOutcome>
|
|
74
|
-
/** Sink for the per-round report (auto rounds have no caller to return it to). */
|
|
75
|
-
report: (text: string) => void
|
|
76
|
-
/**
|
|
77
|
-
* M-5: while a migration apply is in progress every round drops immediately
|
|
78
|
-
* (no queue, no retry). Optional so existing scheduling-unit tests stay pure;
|
|
79
|
-
* index.ts wires it to the migration record's `applying` flag.
|
|
80
|
-
*/
|
|
81
|
-
isApplying?: () => boolean | Promise<boolean>
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** A scheduled or manually triggered round result, including skips. */
|
|
85
|
-
export interface GoScheduledResult extends GoRoundOutcome {
|
|
86
|
-
/** True when the round was skipped (single-flight / disabled / disposed). */
|
|
87
|
-
skipped: boolean
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Implementation constants. Design §6.3 / ADR-007 fix the interval floor and
|
|
91
|
-
// the transport/auth caps; the backoff BASE and the startup jitter window are
|
|
92
|
-
// not specified numerically by the design, so these are the documented
|
|
93
|
-
// implementation choices (kept exported for tests).
|
|
94
|
-
export const GO_INTERVAL_MIN_MINUTES = 15
|
|
95
|
-
export const GO_INTERVAL_DEFAULT_MINUTES = 60
|
|
96
|
-
export const GO_STARTUP_JITTER_MAX_MS = 5_000
|
|
97
|
-
export const GO_TRANSPORT_BACKOFF_BASE_MS = 60_000
|
|
98
|
-
export const GO_TRANSPORT_BACKOFF_CAP_MS = 30 * 60_000
|
|
99
|
-
export const GO_AUTH_BACKOFF_BASE_MS = 60 * 60_000
|
|
100
|
-
export const GO_AUTH_BACKOFF_CAP_MS = 6 * 60 * 60_000
|
|
101
|
-
|
|
102
|
-
/** Clamp an interval to the design floor; non-finite falls back to the default. */
|
|
103
|
-
export function clampGoIntervalMinutes(value: number): number {
|
|
104
|
-
if (!Number.isFinite(value)) return GO_INTERVAL_DEFAULT_MINUTES
|
|
105
|
-
return Math.max(GO_INTERVAL_MIN_MINUTES, Math.floor(value))
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/** startupDelaySeconds*1000 plus bounded jitter (random is in [0,1)). */
|
|
109
|
-
export function goStartupDelayMs(seconds: number, random: number): number {
|
|
110
|
-
const base = Number.isFinite(seconds) && seconds > 0 ? seconds * 1000 : 0
|
|
111
|
-
const r = Math.min(1, Math.max(0, Number.isFinite(random) ? random : 0))
|
|
112
|
-
return base + Math.floor(r * GO_STARTUP_JITTER_MAX_MS)
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/** Exponential backoff with per-class cap (design §6.3). */
|
|
116
|
-
export function goBackoffDelayMs(streak: number, failureKind?: GoFailureKind): number {
|
|
117
|
-
const n = Math.max(1, Math.floor(streak))
|
|
118
|
-
const auth = failureKind === 'auth' || failureKind === 'entitlement'
|
|
119
|
-
const base = auth ? GO_AUTH_BACKOFF_BASE_MS : GO_TRANSPORT_BACKOFF_BASE_MS
|
|
120
|
-
const cap = auth ? GO_AUTH_BACKOFF_CAP_MS : GO_TRANSPORT_BACKOFF_CAP_MS
|
|
121
|
-
return Math.min(cap, base * 2 ** (n - 1))
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/** Report text for a skipped round — always states the reason explicitly. */
|
|
125
|
-
export function goSchedulerSkipReport(reason: string, trigger: GoTrigger): string {
|
|
126
|
-
const when = `opencode-go scheduler (${trigger})`
|
|
127
|
-
switch (reason) {
|
|
128
|
-
case 'disabled':
|
|
129
|
-
// Keep the legacy phrase "disabled — set opencode.go.enabled=true" so
|
|
130
|
-
// manual /model-sync go reports stay byte-compatible with P1c-1.
|
|
131
|
-
return `${when}: skipped — opencode.go sync is disabled — set opencode.go.enabled=true; no request, no settings write. (The migration gate is enforced separately by routeWriter on every write path.)`
|
|
132
|
-
case 'already-in-flight':
|
|
133
|
-
return `${when}: skipped — a go round is already in flight (single-flight); no queue, no retry, no settings write (design §14.6).`
|
|
134
|
-
case 'migration-applying':
|
|
135
|
-
return `${when}: skipped — migration apply in progress (M-5); no queue, no retry, no settings write (design §14.6).`
|
|
136
|
-
case 'disposed':
|
|
137
|
-
return `${when}: skipped — scheduler disposed (plugin unload); no request, no settings write.`
|
|
138
|
-
default:
|
|
139
|
-
return `${when}: skipped — ${reason}; no request, no settings write.`
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function isBackoffFailure(outcome: GoRoundOutcome): boolean {
|
|
144
|
-
// Configuration/skip states (disabled, no-key, settings-unavailable) are not
|
|
145
|
-
// failures — they keep the normal interval. Only source failures back off.
|
|
146
|
-
return outcome.reason === 'source-failed' || outcome.failureKind !== undefined
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* The scheduler state machine. One instance per plugin activation; it owns a
|
|
151
|
-
* single timer handle and a single in-flight flag shared by auto and manual
|
|
152
|
-
* triggers (that is the single-flight lock).
|
|
153
|
-
*/
|
|
154
|
-
export class GoScheduler {
|
|
155
|
-
private readonly config: () => GoSchedulerConfig
|
|
156
|
-
private readonly timers: GoSchedulerTimers
|
|
157
|
-
private readonly random: GoSchedulerRandom
|
|
158
|
-
private readonly runRound: (request: GoRoundRequest) => Promise<GoRoundOutcome>
|
|
159
|
-
private readonly report: (text: string) => void
|
|
160
|
-
private readonly isApplying?: () => boolean | Promise<boolean>
|
|
161
|
-
|
|
162
|
-
private handle: GoTimerHandle | undefined
|
|
163
|
-
private started = false
|
|
164
|
-
private inFlight = false
|
|
165
|
-
private disposed = false
|
|
166
|
-
private streak = 0
|
|
167
|
-
private lastFailureKind: GoFailureKind | undefined
|
|
168
|
-
/** Single lifetime signal: dispose() aborts the round that is currently in flight. */
|
|
169
|
-
private readonly abort = new AbortController()
|
|
170
|
-
|
|
171
|
-
constructor(deps: GoSchedulerDeps) {
|
|
172
|
-
this.config = deps.config
|
|
173
|
-
this.timers = deps.timers
|
|
174
|
-
this.random = deps.random
|
|
175
|
-
this.runRound = deps.runRound
|
|
176
|
-
this.report = deps.report
|
|
177
|
-
this.isApplying = deps.isApplying
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/** Arm the startup timer. No-op when disabled — zero triggers at enabled=false. */
|
|
181
|
-
start(): void {
|
|
182
|
-
if (this.started || this.disposed) return
|
|
183
|
-
this.started = true
|
|
184
|
-
const cfg = this.config()
|
|
185
|
-
if (!cfg.enabled) return
|
|
186
|
-
this.arm(goStartupDelayMs(cfg.startupDelaySeconds, this.random.next()), 'startup')
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Live re-arm after a settings edit. Cancels the pending timer and reads
|
|
191
|
-
* fresh config; a round already in flight keeps its lock and picks up the
|
|
192
|
-
* new config when it schedules its own successor.
|
|
193
|
-
*/
|
|
194
|
-
rearm(): void {
|
|
195
|
-
if (this.disposed || !this.started) return
|
|
196
|
-
this.cancel()
|
|
197
|
-
if (this.inFlight) return
|
|
198
|
-
this.armFromConfig()
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Reset backoff after a credential change. Clears the in-memory streak and
|
|
203
|
-
* re-arms at the normal interval instead of waiting out a capped (up to 6h)
|
|
204
|
-
* backoff — a fixed key must not stay frozen for hours (design §6.3). The
|
|
205
|
-
* caller's listener is disposed with the plugin; a disposed scheduler is a
|
|
206
|
-
* no-op.
|
|
207
|
-
*/
|
|
208
|
-
resetBackoff(): void {
|
|
209
|
-
this.clearBackoff()
|
|
210
|
-
if (this.disposed || !this.started || this.inFlight) return
|
|
211
|
-
this.armFromConfig()
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/** Cancel every timer and abort a round already in flight (design §6.3 卸载). */
|
|
215
|
-
dispose(): void {
|
|
216
|
-
this.disposed = true
|
|
217
|
-
this.abort.abort()
|
|
218
|
-
this.cancel()
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
get isInFlight(): boolean {
|
|
222
|
-
return this.inFlight
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
get failureStreak(): number {
|
|
226
|
-
return this.streak
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/** Manual /model-sync go trigger — shares the single-flight lock with auto rounds. */
|
|
230
|
-
triggerManual(options: { adopt?: boolean } = {}): Promise<GoScheduledResult> {
|
|
231
|
-
return this.fire('manual', options.adopt === true)
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
private arm(delayMs: number, trigger: GoTrigger): void {
|
|
235
|
-
this.cancel()
|
|
236
|
-
// Returning the promise is harmless to real setTimeout (return value is
|
|
237
|
-
// ignored) and lets a fake timer await the whole round deterministically.
|
|
238
|
-
this.handle = this.timers.setTimeout(() => this.fire(trigger), Math.max(0, delayMs))
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
private cancel(): void {
|
|
242
|
-
if (this.handle !== undefined) {
|
|
243
|
-
this.timers.clearTimeout(this.handle)
|
|
244
|
-
this.handle = undefined
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
private armFromConfig(): void {
|
|
249
|
-
const cfg = this.config()
|
|
250
|
-
if (!cfg.enabled) return
|
|
251
|
-
if (this.streak > 0) {
|
|
252
|
-
this.arm(goBackoffDelayMs(this.streak, this.lastFailureKind), 'backoff')
|
|
253
|
-
} else {
|
|
254
|
-
this.arm(clampGoIntervalMinutes(cfg.intervalMinutes) * 60_000, 'interval')
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/** Forget the entire backoff history (transport === auth) — used by reset paths. */
|
|
259
|
-
private clearBackoff(): void {
|
|
260
|
-
this.streak = 0
|
|
261
|
-
this.lastFailureKind = undefined
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
private async fire(trigger: GoTrigger, adopt = false): Promise<GoScheduledResult> {
|
|
265
|
-
if (this.disposed) return this.skip('disposed', trigger)
|
|
266
|
-
if (!this.config().enabled) return this.skip('disabled', trigger)
|
|
267
|
-
if (this.inFlight) return this.skip('already-in-flight', trigger)
|
|
268
|
-
// M-5: an in-progress migration owns the routes; drop the round now rather
|
|
269
|
-
// than racing a half-applied settings document. Same non-queueing rule as
|
|
270
|
-
// the single-flight lock (§14.6).
|
|
271
|
-
if (this.isApplying !== undefined && await this.isApplying() === true) return this.skip('migration-applying', trigger)
|
|
272
|
-
|
|
273
|
-
// The timer that fired is now consumed; a new one is armed after the round.
|
|
274
|
-
this.cancel()
|
|
275
|
-
this.inFlight = true
|
|
276
|
-
// A manual round is an explicit force: forget any accumulated backoff
|
|
277
|
-
// before it runs (design §6.3 "手动 force 必须重置退避").
|
|
278
|
-
if (trigger === 'manual') this.clearBackoff()
|
|
279
|
-
let result: GoScheduledResult
|
|
280
|
-
try {
|
|
281
|
-
const raw = await this.runRound({ trigger, adopt, signal: this.abort.signal })
|
|
282
|
-
if (raw.ok) {
|
|
283
|
-
this.streak = 0
|
|
284
|
-
this.lastFailureKind = undefined
|
|
285
|
-
} else if (isBackoffFailure(raw)) {
|
|
286
|
-
this.streak += 1
|
|
287
|
-
this.lastFailureKind = raw.failureKind
|
|
288
|
-
}
|
|
289
|
-
result = { ...raw, skipped: false }
|
|
290
|
-
} catch (error) {
|
|
291
|
-
const message = error instanceof Error ? error.message : String(error)
|
|
292
|
-
this.streak += 1
|
|
293
|
-
this.lastFailureKind = 'transport'
|
|
294
|
-
result = {
|
|
295
|
-
ok: false,
|
|
296
|
-
reason: 'scheduler-error',
|
|
297
|
-
text: `opencode-go scheduler: round error — ${message}; kept last-good, no further settings write`,
|
|
298
|
-
failureKind: 'transport',
|
|
299
|
-
skipped: false,
|
|
300
|
-
}
|
|
301
|
-
} finally {
|
|
302
|
-
this.inFlight = false
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
this.report(result.text)
|
|
306
|
-
if (!this.disposed) this.armFromConfig()
|
|
307
|
-
return result
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
private skip(reason: string, trigger: GoTrigger): GoScheduledResult {
|
|
311
|
-
const text = goSchedulerSkipReport(reason, trigger)
|
|
312
|
-
this.report(text)
|
|
313
|
-
return { ok: false, reason, text, skipped: true }
|
|
314
|
-
}
|
|
315
|
-
}
|
package/src/opencode-go-store.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
// P1c file adapter: persist the opencode-go ownership ledger in the SAME file
|
|
2
|
-
// and through the SAME serialized queue as the pi.dev fetch cache, so a ledger
|
|
3
|
-
// write can never clobber a concurrent fetch/override stage (design §6.1,
|
|
4
|
-
// ADR-004; verification 21 §9 concern ②).
|
|
5
|
-
//
|
|
6
|
-
// Wire format (design §6.1, B1): a top-level slot per ledger key,
|
|
7
|
-
// <profileKey>#<route>#<keyFingerprint> -> GoOwnershipEntry
|
|
8
|
-
// in ~/.dsh/models-store.json (unscoped callers still use <route>#<keyFingerprint>).
|
|
9
|
-
// Keys containing `#` are never valid provider route ids, so the existing
|
|
10
|
-
// `read(route)` / `update(route)` paths cannot see these entries. Atomic
|
|
11
|
-
// temp+rename and the write queue come from `loadModelsStore()` — this module
|
|
12
|
-
// adds no second persistence mechanism.
|
|
13
|
-
//
|
|
14
|
-
// Failure direction: a corrupt/unreadable document reads as "no ledger"
|
|
15
|
-
// (`undefined`). The writer then does a conservative first-round claim — it
|
|
16
|
-
// never deletes without a hash (ADR-004: store loss degrades to no-delete).
|
|
17
|
-
// The next write replaces the bad file atomically and heals it.
|
|
18
|
-
|
|
19
|
-
import type { GoOwnershipEntry } from './opencode-go-planner.ts'
|
|
20
|
-
import { scopedStoreKey } from './opencode-go-profile.ts'
|
|
21
|
-
import { loadModelsStore, type ModelsStoreAccessor } from './remote-catalog.ts'
|
|
22
|
-
import type { GoOwnershipStore } from './opencode-go-writer.ts'
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Build a `GoOwnershipStore` on top of an existing models-store accessor.
|
|
26
|
-
*
|
|
27
|
-
* `profileKey` prefixes every opaque key, turning `<route>#<keyFingerprint>`
|
|
28
|
-
* into `<profileKey>#<route>#<keyFingerprint>` (B1). Two profiles using the
|
|
29
|
-
* same Go key occupy disjoint slots in the shared document, and a rollback
|
|
30
|
-
* deletes only the calling profile's slots. Omitting `profileKey` keeps the
|
|
31
|
-
* pre-fix unscoped behavior for legacy callers/tests only.
|
|
32
|
-
*/
|
|
33
|
-
export function createFileOwnershipStore(
|
|
34
|
-
accessor: ModelsStoreAccessor,
|
|
35
|
-
profileKey?: string,
|
|
36
|
-
): GoOwnershipStore {
|
|
37
|
-
const slot = (key: string): string => profileKey === undefined ? key : scopedStoreKey(profileKey, key)
|
|
38
|
-
return {
|
|
39
|
-
async read(key: string): Promise<GoOwnershipEntry | undefined> {
|
|
40
|
-
let raw: unknown
|
|
41
|
-
try {
|
|
42
|
-
raw = await accessor.readRaw(slot(key))
|
|
43
|
-
} catch {
|
|
44
|
-
// Corrupt file / EACCES: no ledger. Never infer one, never delete.
|
|
45
|
-
return undefined
|
|
46
|
-
}
|
|
47
|
-
// readRaw now returns the slot verbatim; a present primitive/null slot is
|
|
48
|
-
// NOT a ledger entry (same conservative "no ledger" direction as before).
|
|
49
|
-
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) return undefined
|
|
50
|
-
const record = raw as Record<string, unknown>
|
|
51
|
-
if (typeof record.managed !== 'object' || record.managed === null
|
|
52
|
-
|| Array.isArray(record.managed)) {
|
|
53
|
-
return undefined
|
|
54
|
-
}
|
|
55
|
-
return record as unknown as GoOwnershipEntry
|
|
56
|
-
},
|
|
57
|
-
async write(key: string, entry: GoOwnershipEntry): Promise<void> {
|
|
58
|
-
await accessor.writeRaw(slot(key), entry as unknown as Record<string, unknown>)
|
|
59
|
-
},
|
|
60
|
-
async delete(key: string): Promise<void> {
|
|
61
|
-
await accessor.deleteRaw(slot(key))
|
|
62
|
-
},
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Production factory: the ledger lives in ~/.dsh/models-store.json. Tests pass
|
|
68
|
-
* `loadModelsStore(tempPath)` through `createFileOwnershipStore` instead, so no
|
|
69
|
-
* test ever touches the real ~/.dsh.
|
|
70
|
-
*/
|
|
71
|
-
export function createModelsStoreOwnershipStore(storePath?: string, profileKey?: string): GoOwnershipStore {
|
|
72
|
-
return createFileOwnershipStore(loadModelsStore(storePath), profileKey)
|
|
73
|
-
}
|