@aiwayds/dsh-model-sync 0.9.0 → 0.11.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 +20 -3
- package/README.zh-CN.md +20 -3
- package/lib/builtin-catalog-snapshot.js +6 -3
- package/lib/builtin-catalog-snapshot.js.map +1 -1
- package/lib/index.d.ts +379 -0
- package/lib/index.js +897 -13
- package/lib/index.js.map +1 -1
- package/lib/opencode-go-diagnose.d.ts +74 -0
- package/lib/opencode-go-diagnose.js +146 -0
- package/lib/opencode-go-diagnose.js.map +1 -0
- package/lib/opencode-go-grouping.d.ts +29 -0
- package/lib/opencode-go-grouping.js +77 -0
- package/lib/opencode-go-grouping.js.map +1 -0
- package/lib/opencode-go-journal.d.ts +110 -0
- package/lib/opencode-go-journal.js +406 -0
- package/lib/opencode-go-journal.js.map +1 -0
- package/lib/opencode-go-migrate.d.ts +343 -0
- package/lib/opencode-go-migrate.js +1300 -0
- package/lib/opencode-go-migrate.js.map +1 -0
- package/lib/opencode-go-migration.d.ts +227 -0
- package/lib/opencode-go-migration.js +384 -0
- package/lib/opencode-go-migration.js.map +1 -0
- package/lib/opencode-go-one-shot.d.ts +276 -0
- package/lib/opencode-go-one-shot.js +1094 -0
- package/lib/opencode-go-one-shot.js.map +1 -0
- package/lib/opencode-go-patch-file.d.ts +123 -0
- package/lib/opencode-go-patch-file.js +1185 -0
- package/lib/opencode-go-patch-file.js.map +1 -0
- package/lib/opencode-go-planner.d.ts +153 -0
- package/lib/opencode-go-planner.js +339 -0
- package/lib/opencode-go-planner.js.map +1 -0
- package/lib/opencode-go-profile.d.ts +57 -0
- package/lib/opencode-go-profile.js +103 -0
- package/lib/opencode-go-profile.js.map +1 -0
- package/lib/opencode-go-scheduler.d.ts +128 -0
- package/lib/opencode-go-scheduler.js +241 -0
- package/lib/opencode-go-scheduler.js.map +1 -0
- package/lib/opencode-go-source.d.ts +117 -0
- package/lib/opencode-go-source.js +331 -0
- package/lib/opencode-go-source.js.map +1 -0
- package/lib/opencode-go-store.d.ts +18 -0
- package/lib/opencode-go-store.js +68 -0
- package/lib/opencode-go-store.js.map +1 -0
- package/lib/opencode-go-sync.d.ts +94 -0
- package/lib/opencode-go-sync.js +364 -0
- package/lib/opencode-go-sync.js.map +1 -0
- package/lib/opencode-go-writer.d.ts +132 -0
- package/lib/opencode-go-writer.js +966 -0
- package/lib/opencode-go-writer.js.map +1 -0
- package/lib/provider-native.d.ts +3 -1
- package/lib/provider-native.js +4 -2
- package/lib/provider-native.js.map +1 -1
- package/lib/remote-catalog.d.ts +61 -0
- package/lib/remote-catalog.js +307 -3
- package/lib/remote-catalog.js.map +1 -1
- package/package.json +15 -15
- package/src/builtin-catalog-snapshot.ts +6 -3
- package/src/index.ts +1060 -12
- package/src/opencode-go-diagnose.ts +222 -0
- package/src/opencode-go-grouping.ts +97 -0
- package/src/opencode-go-journal.ts +458 -0
- package/src/opencode-go-migrate.ts +1694 -0
- package/src/opencode-go-migration.ts +552 -0
- package/src/opencode-go-one-shot.ts +1354 -0
- package/src/opencode-go-patch-file.ts +1168 -0
- package/src/opencode-go-planner.ts +505 -0
- package/src/opencode-go-profile.ts +114 -0
- package/src/opencode-go-scheduler.ts +315 -0
- package/src/opencode-go-source.ts +424 -0
- package/src/opencode-go-store.ts +73 -0
- package/src/opencode-go-sync.ts +503 -0
- package/src/opencode-go-writer.ts +1189 -0
- package/src/provider-native.ts +3 -1
- package/src/remote-catalog.ts +359 -3
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/** Structural context seam: both services are optional peers in cordis. */
|
|
2
|
+
export interface ProfileIdentityContext {
|
|
3
|
+
get(name: 'profileContext'): {
|
|
4
|
+
name?: string;
|
|
5
|
+
dir?: string;
|
|
6
|
+
} | undefined;
|
|
7
|
+
get(name: 'launchEnvironment'): {
|
|
8
|
+
get(key: string): {
|
|
9
|
+
value: string;
|
|
10
|
+
} | undefined;
|
|
11
|
+
} | undefined;
|
|
12
|
+
get(name: string): unknown;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Resolve the stable profile key from the host, or `undefined` when no identity
|
|
16
|
+
* is available. Structural `ctx.get` keeps the optional peers optional (SDK
|
|
17
|
+
* hosts provide neither, tests provide fakes). Callers on a migration/ownership
|
|
18
|
+
* ledger path MUST treat `undefined` as fail-closed — see
|
|
19
|
+
* `PROFILE_IDENTITY_REQUIRED_MESSAGE` for the actionable error to surface.
|
|
20
|
+
*/
|
|
21
|
+
export declare function resolveProfileKey(ctx: ProfileIdentityContext): string | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Actionable error for a ledger path that cannot resolve a profile identity.
|
|
24
|
+
* Names verified against the dsh launcher source: `profileContext` ({ name,
|
|
25
|
+
* dir }) is a cordis service (profile-context.ts), `launchEnvironment` is the
|
|
26
|
+
* launcher snapshot service (`launchEnvironmentOf`), and `DSH_PROFILE` /
|
|
27
|
+
* `DSH_PROFILE_DIR` are the `DSH_` prefixed profile variables defined by the
|
|
28
|
+
* shell-env package (derived from `profileContext` for model-shell subprocesses).
|
|
29
|
+
*/
|
|
30
|
+
export declare const PROFILE_IDENTITY_REQUIRED_MESSAGE: string;
|
|
31
|
+
/**
|
|
32
|
+
* Compact, user-visible hint for a non-strict status refresh that degraded
|
|
33
|
+
* because the file-backed ledger could not be built without a profile identity
|
|
34
|
+
* (N2). Unlike the normal legacy hint, this states the fail-closed fact and how
|
|
35
|
+
* to fix it — it must never masquerade as a normal legacy state.
|
|
36
|
+
*/
|
|
37
|
+
export declare const PROFILE_IDENTITY_STATUS_HINT: string;
|
|
38
|
+
/** Prefix one opaque models-store slot with the profile dimension. */
|
|
39
|
+
export declare function scopedStoreKey(profileKey: string, key: string): string;
|
|
40
|
+
/** Base name of the opencode-go migration record slot (shared with migration.ts). */
|
|
41
|
+
export declare const GO_MIGRATION_STORE_KEY_BASE = "migration:opencode-go";
|
|
42
|
+
/**
|
|
43
|
+
* True only for an UNSCOPED legacy opaque key left in the shared store — one
|
|
44
|
+
* that carries NO profile dimension and whose ownership is therefore unknown.
|
|
45
|
+
*
|
|
46
|
+
* Explicit key shapes (design §6.1 / §14.1):
|
|
47
|
+
* `<profileKey>#<route>#<keyFingerprint>` → scoped ownership (3 parts) — NOT legacy.
|
|
48
|
+
* `<profileKey>#migration:opencode-go` → scoped migration record (2 parts,
|
|
49
|
+
* tail = base name) — NOT legacy.
|
|
50
|
+
* `<route>#<keyFingerprint>` → unscoped legacy ownership (2 parts) — legacy.
|
|
51
|
+
* `migration:opencode-go` → unscoped legacy migration (1 part),
|
|
52
|
+
* reported by the caller explicitly.
|
|
53
|
+
*
|
|
54
|
+
* Such keys are reported, never read or claimed. Scoped keys are NEVER legacy,
|
|
55
|
+
* regardless of how many '#' they contain.
|
|
56
|
+
*/
|
|
57
|
+
export declare function isUnscopedStoreKey(key: string): boolean;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// B1: profile identity for the shared ~/.dsh/models-store.json (design §14.1,
|
|
2
|
+
// §14.7.4, H9). Every dsh profile on a machine writes the SAME models-store.json;
|
|
3
|
+
// without a profile dimension in the ledger/migration-record keys, profile A's
|
|
4
|
+
// `applied` record lets never-migrated profile B through the migration gate and
|
|
5
|
+
// A's rollback clobbers B's ledger + settings. This module supplies the profile
|
|
6
|
+
// dimension and the key primitives used by the file-backed stores.
|
|
7
|
+
//
|
|
8
|
+
// Identity source (host-provided — never guessed from settings or file names):
|
|
9
|
+
// 1. cordis `profileContext.name` (dsh app-boot): the launched profile name.
|
|
10
|
+
// 2. launchEnvironment `DSH_PROFILE`: the same value, launcher-provided env.
|
|
11
|
+
// 3. cordis `profileContext.dir` / launchEnvironment `DSH_PROFILE_DIR`:
|
|
12
|
+
// absolute profile directory, used ONLY when no name is available.
|
|
13
|
+
// 4. NO identity → FAIL CLOSED. `resolveProfileKey` returns undefined and the
|
|
14
|
+
// migration/ownership ledger callers refuse to read or write. There is
|
|
15
|
+
// deliberately NO silent 'default' collapse: two profiles with no resolved
|
|
16
|
+
// identity would share one key and reopen the B1 failure (design H9).
|
|
17
|
+
//
|
|
18
|
+
// Stability: the primary key is the profile NAME. It is fixed for the lifetime
|
|
19
|
+
// of a profile across restarts and processes. The host derives
|
|
20
|
+
// `name = basename(dir)`, so MOVING a profile directory while keeping its
|
|
21
|
+
// basename leaves the key stable; a move that also RENAMES the basename changes
|
|
22
|
+
// the name, hence the key: the old record becomes an orphan in the shared store,
|
|
23
|
+
// the renamed profile reads no record (absent/legacy) and fails CLOSED — it must
|
|
24
|
+
// re-run `migrate plan/apply`. We never auto-claim a record that carries another
|
|
25
|
+
// profile's identity. The directory fallback is less stable (moving/renaming the
|
|
26
|
+
// directory changes it) and is used only as a last resort.
|
|
27
|
+
function nonEmpty(value) {
|
|
28
|
+
return typeof value === 'string' && value.trim().length > 0 ? value.trim() : undefined;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Resolve the stable profile key from the host, or `undefined` when no identity
|
|
32
|
+
* is available. Structural `ctx.get` keeps the optional peers optional (SDK
|
|
33
|
+
* hosts provide neither, tests provide fakes). Callers on a migration/ownership
|
|
34
|
+
* ledger path MUST treat `undefined` as fail-closed — see
|
|
35
|
+
* `PROFILE_IDENTITY_REQUIRED_MESSAGE` for the actionable error to surface.
|
|
36
|
+
*/
|
|
37
|
+
export function resolveProfileKey(ctx) {
|
|
38
|
+
const profile = ctx.get('profileContext');
|
|
39
|
+
const env = ctx.get('launchEnvironment');
|
|
40
|
+
return nonEmpty(profile?.name)
|
|
41
|
+
?? nonEmpty(env?.get('DSH_PROFILE')?.value)
|
|
42
|
+
?? nonEmpty(profile?.dir)
|
|
43
|
+
?? nonEmpty(env?.get('DSH_PROFILE_DIR')?.value);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Actionable error for a ledger path that cannot resolve a profile identity.
|
|
47
|
+
* Names verified against the dsh launcher source: `profileContext` ({ name,
|
|
48
|
+
* dir }) is a cordis service (profile-context.ts), `launchEnvironment` is the
|
|
49
|
+
* launcher snapshot service (`launchEnvironmentOf`), and `DSH_PROFILE` /
|
|
50
|
+
* `DSH_PROFILE_DIR` are the `DSH_` prefixed profile variables defined by the
|
|
51
|
+
* shell-env package (derived from `profileContext` for model-shell subprocesses).
|
|
52
|
+
*/
|
|
53
|
+
export const PROFILE_IDENTITY_REQUIRED_MESSAGE = [
|
|
54
|
+
'model-sync: cannot resolve the dsh profile identity for the opencode-go migration/ownership ledger.',
|
|
55
|
+
'Refusing to share a single key across profiles: without a stable per-profile key another profile could inherit this migration state.',
|
|
56
|
+
'Fix: launch the plugin inside a dsh profile so ctx.profileContext.name (or ctx.profileContext.dir) is available,',
|
|
57
|
+
'or expose the launcher-provided DSH_PROFILE / DSH_PROFILE_DIR values through ctx.launchEnvironment.',
|
|
58
|
+
'No migration record or ledger key was read or written.',
|
|
59
|
+
].join(' ');
|
|
60
|
+
/**
|
|
61
|
+
* Compact, user-visible hint for a non-strict status refresh that degraded
|
|
62
|
+
* because the file-backed ledger could not be built without a profile identity
|
|
63
|
+
* (N2). Unlike the normal legacy hint, this states the fail-closed fact and how
|
|
64
|
+
* to fix it — it must never masquerade as a normal legacy state.
|
|
65
|
+
*/
|
|
66
|
+
export const PROFILE_IDENTITY_STATUS_HINT = [
|
|
67
|
+
'profile 身份不可用 → opencode-go 迁移记录与账本已 fail-closed(未读未写,同步已跳过);',
|
|
68
|
+
'修复:在 dsh profile 内启动插件,或向 ctx.launchEnvironment 暴露 DSH_PROFILE / DSH_PROFILE_DIR',
|
|
69
|
+
].join(' ');
|
|
70
|
+
/** Prefix one opaque models-store slot with the profile dimension. */
|
|
71
|
+
export function scopedStoreKey(profileKey, key) {
|
|
72
|
+
return `${profileKey}#${key}`;
|
|
73
|
+
}
|
|
74
|
+
/** Base name of the opencode-go migration record slot (shared with migration.ts). */
|
|
75
|
+
export const GO_MIGRATION_STORE_KEY_BASE = 'migration:opencode-go';
|
|
76
|
+
/**
|
|
77
|
+
* True only for an UNSCOPED legacy opaque key left in the shared store — one
|
|
78
|
+
* that carries NO profile dimension and whose ownership is therefore unknown.
|
|
79
|
+
*
|
|
80
|
+
* Explicit key shapes (design §6.1 / §14.1):
|
|
81
|
+
* `<profileKey>#<route>#<keyFingerprint>` → scoped ownership (3 parts) — NOT legacy.
|
|
82
|
+
* `<profileKey>#migration:opencode-go` → scoped migration record (2 parts,
|
|
83
|
+
* tail = base name) — NOT legacy.
|
|
84
|
+
* `<route>#<keyFingerprint>` → unscoped legacy ownership (2 parts) — legacy.
|
|
85
|
+
* `migration:opencode-go` → unscoped legacy migration (1 part),
|
|
86
|
+
* reported by the caller explicitly.
|
|
87
|
+
*
|
|
88
|
+
* Such keys are reported, never read or claimed. Scoped keys are NEVER legacy,
|
|
89
|
+
* regardless of how many '#' they contain.
|
|
90
|
+
*/
|
|
91
|
+
export function isUnscopedStoreKey(key) {
|
|
92
|
+
const parts = key.split('#');
|
|
93
|
+
if (parts.length !== 2)
|
|
94
|
+
return false;
|
|
95
|
+
const [prefix, tail] = parts;
|
|
96
|
+
if (prefix.length === 0 || tail.length === 0)
|
|
97
|
+
return false;
|
|
98
|
+
// A two-part key whose tail is the migration base name is a profile-scoped
|
|
99
|
+
// migration record, not a legacy key. Only other two-part keys (an unscoped
|
|
100
|
+
// `<route>#<keyFingerprint>` ownership slot) are legacy.
|
|
101
|
+
return tail !== GO_MIGRATION_STORE_KEY_BASE;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=opencode-go-profile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"opencode-go-profile.js","sourceRoot":"","sources":["../src/opencode-go-profile.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,kFAAkF;AAClF,+EAA+E;AAC/E,gFAAgF;AAChF,gFAAgF;AAChF,mEAAmE;AACnE,EAAE;AACF,+EAA+E;AAC/E,+EAA+E;AAC/E,+EAA+E;AAC/E,0EAA0E;AAC1E,wEAAwE;AACxE,gFAAgF;AAChF,4EAA4E;AAC5E,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AAE,CAAA;AAC/B,CAAC;AAED,qFAAqF;AACrF,MAAM,CAAC,MAAM,2BAA2B,GAAG,uBAAuB,CAAA;AAElE;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,kBAAkB,CAAC,GAAW;IAC5C,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAC5B,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,KAAK,CAAA;IACpC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,KAAK,CAAA;IAC5B,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,KAAK,CAAA;IAC1D,2EAA2E;IAC3E,4EAA4E;IAC5E,yDAAyD;IACzD,OAAO,IAAI,KAAK,2BAA2B,CAAA;AAC7C,CAAC"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { GoFailureKind } from './opencode-go-source.ts';
|
|
2
|
+
/** Cordis service seams used by index.ts to inject test doubles. */
|
|
3
|
+
export declare const GO_SCHEDULER_TIMERS_SERVICE = "goSchedulerTimers";
|
|
4
|
+
export declare const GO_SCHEDULER_RANDOM_SERVICE = "goSchedulerRandom";
|
|
5
|
+
/** Trigger origin, recorded in every report so auto/manual rounds are distinguishable. */
|
|
6
|
+
export type GoTrigger = 'startup' | 'interval' | 'backoff' | 'manual';
|
|
7
|
+
/** The subset of Config the scheduler reads (live, via a callback). */
|
|
8
|
+
export interface GoSchedulerConfig {
|
|
9
|
+
/** Master switch. Design §6.2.6 default false (unchanged by P1d-1). */
|
|
10
|
+
enabled: boolean;
|
|
11
|
+
/** go.intervalMinutes — default 60, floor 15 (design §6.3 / ADR-007). */
|
|
12
|
+
intervalMinutes: number;
|
|
13
|
+
/** startupDelaySeconds — jitter is added on top (design §6.3). */
|
|
14
|
+
startupDelaySeconds: number;
|
|
15
|
+
}
|
|
16
|
+
/** Opaque timer handle (platform Timeout in production, an id in fake timers). */
|
|
17
|
+
export type GoTimerHandle = unknown;
|
|
18
|
+
/** Minimal clock surface the scheduler needs. Injectable for deterministic tests. */
|
|
19
|
+
export interface GoSchedulerTimers {
|
|
20
|
+
setTimeout(fn: () => void, ms: number): GoTimerHandle;
|
|
21
|
+
clearTimeout(handle: GoTimerHandle): void;
|
|
22
|
+
}
|
|
23
|
+
/** Injectable [0,1) source so startup jitter is deterministic in tests. */
|
|
24
|
+
export interface GoSchedulerRandom {
|
|
25
|
+
next(): number;
|
|
26
|
+
}
|
|
27
|
+
/** What the scheduler needs back from one round to drive backoff + reporting. */
|
|
28
|
+
export interface GoRoundOutcome {
|
|
29
|
+
ok: boolean;
|
|
30
|
+
reason: string;
|
|
31
|
+
/** Report text — passed through unchanged for real rounds (design item 3). */
|
|
32
|
+
text: string;
|
|
33
|
+
/** Source failure taxonomy; set only when the round reached the source. */
|
|
34
|
+
failureKind?: GoFailureKind;
|
|
35
|
+
}
|
|
36
|
+
export interface GoRoundRequest {
|
|
37
|
+
trigger: GoTrigger;
|
|
38
|
+
adopt: boolean;
|
|
39
|
+
/** Aborted by dispose() so an in-flight round stops before it can write settings. */
|
|
40
|
+
signal: AbortSignal;
|
|
41
|
+
}
|
|
42
|
+
export interface GoSchedulerDeps {
|
|
43
|
+
config: () => GoSchedulerConfig;
|
|
44
|
+
timers: GoSchedulerTimers;
|
|
45
|
+
random: GoSchedulerRandom;
|
|
46
|
+
runRound: (request: GoRoundRequest) => Promise<GoRoundOutcome>;
|
|
47
|
+
/** Sink for the per-round report (auto rounds have no caller to return it to). */
|
|
48
|
+
report: (text: string) => void;
|
|
49
|
+
/**
|
|
50
|
+
* M-5: while a migration apply is in progress every round drops immediately
|
|
51
|
+
* (no queue, no retry). Optional so existing scheduling-unit tests stay pure;
|
|
52
|
+
* index.ts wires it to the migration record's `applying` flag.
|
|
53
|
+
*/
|
|
54
|
+
isApplying?: () => boolean | Promise<boolean>;
|
|
55
|
+
}
|
|
56
|
+
/** A scheduled or manually triggered round result, including skips. */
|
|
57
|
+
export interface GoScheduledResult extends GoRoundOutcome {
|
|
58
|
+
/** True when the round was skipped (single-flight / disabled / disposed). */
|
|
59
|
+
skipped: boolean;
|
|
60
|
+
}
|
|
61
|
+
export declare const GO_INTERVAL_MIN_MINUTES = 15;
|
|
62
|
+
export declare const GO_INTERVAL_DEFAULT_MINUTES = 60;
|
|
63
|
+
export declare const GO_STARTUP_JITTER_MAX_MS = 5000;
|
|
64
|
+
export declare const GO_TRANSPORT_BACKOFF_BASE_MS = 60000;
|
|
65
|
+
export declare const GO_TRANSPORT_BACKOFF_CAP_MS: number;
|
|
66
|
+
export declare const GO_AUTH_BACKOFF_BASE_MS: number;
|
|
67
|
+
export declare const GO_AUTH_BACKOFF_CAP_MS: number;
|
|
68
|
+
/** Clamp an interval to the design floor; non-finite falls back to the default. */
|
|
69
|
+
export declare function clampGoIntervalMinutes(value: number): number;
|
|
70
|
+
/** startupDelaySeconds*1000 plus bounded jitter (random is in [0,1)). */
|
|
71
|
+
export declare function goStartupDelayMs(seconds: number, random: number): number;
|
|
72
|
+
/** Exponential backoff with per-class cap (design §6.3). */
|
|
73
|
+
export declare function goBackoffDelayMs(streak: number, failureKind?: GoFailureKind): number;
|
|
74
|
+
/** Report text for a skipped round — always states the reason explicitly. */
|
|
75
|
+
export declare function goSchedulerSkipReport(reason: string, trigger: GoTrigger): string;
|
|
76
|
+
/**
|
|
77
|
+
* The scheduler state machine. One instance per plugin activation; it owns a
|
|
78
|
+
* single timer handle and a single in-flight flag shared by auto and manual
|
|
79
|
+
* triggers (that is the single-flight lock).
|
|
80
|
+
*/
|
|
81
|
+
export declare class GoScheduler {
|
|
82
|
+
private readonly config;
|
|
83
|
+
private readonly timers;
|
|
84
|
+
private readonly random;
|
|
85
|
+
private readonly runRound;
|
|
86
|
+
private readonly report;
|
|
87
|
+
private readonly isApplying?;
|
|
88
|
+
private handle;
|
|
89
|
+
private started;
|
|
90
|
+
private inFlight;
|
|
91
|
+
private disposed;
|
|
92
|
+
private streak;
|
|
93
|
+
private lastFailureKind;
|
|
94
|
+
/** Single lifetime signal: dispose() aborts the round that is currently in flight. */
|
|
95
|
+
private readonly abort;
|
|
96
|
+
constructor(deps: GoSchedulerDeps);
|
|
97
|
+
/** Arm the startup timer. No-op when disabled — zero triggers at enabled=false. */
|
|
98
|
+
start(): void;
|
|
99
|
+
/**
|
|
100
|
+
* Live re-arm after a settings edit. Cancels the pending timer and reads
|
|
101
|
+
* fresh config; a round already in flight keeps its lock and picks up the
|
|
102
|
+
* new config when it schedules its own successor.
|
|
103
|
+
*/
|
|
104
|
+
rearm(): void;
|
|
105
|
+
/**
|
|
106
|
+
* Reset backoff after a credential change. Clears the in-memory streak and
|
|
107
|
+
* re-arms at the normal interval instead of waiting out a capped (up to 6h)
|
|
108
|
+
* backoff — a fixed key must not stay frozen for hours (design §6.3). The
|
|
109
|
+
* caller's listener is disposed with the plugin; a disposed scheduler is a
|
|
110
|
+
* no-op.
|
|
111
|
+
*/
|
|
112
|
+
resetBackoff(): void;
|
|
113
|
+
/** Cancel every timer and abort a round already in flight (design §6.3 卸载). */
|
|
114
|
+
dispose(): void;
|
|
115
|
+
get isInFlight(): boolean;
|
|
116
|
+
get failureStreak(): number;
|
|
117
|
+
/** Manual /model-sync go trigger — shares the single-flight lock with auto rounds. */
|
|
118
|
+
triggerManual(options?: {
|
|
119
|
+
adopt?: boolean;
|
|
120
|
+
}): Promise<GoScheduledResult>;
|
|
121
|
+
private arm;
|
|
122
|
+
private cancel;
|
|
123
|
+
private armFromConfig;
|
|
124
|
+
/** Forget the entire backoff history (transport === auth) — used by reset paths. */
|
|
125
|
+
private clearBackoff;
|
|
126
|
+
private fire;
|
|
127
|
+
private skip;
|
|
128
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
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
|
+
/** Cordis service seams used by index.ts to inject test doubles. */
|
|
19
|
+
export const GO_SCHEDULER_TIMERS_SERVICE = 'goSchedulerTimers';
|
|
20
|
+
export const GO_SCHEDULER_RANDOM_SERVICE = 'goSchedulerRandom';
|
|
21
|
+
// Implementation constants. Design §6.3 / ADR-007 fix the interval floor and
|
|
22
|
+
// the transport/auth caps; the backoff BASE and the startup jitter window are
|
|
23
|
+
// not specified numerically by the design, so these are the documented
|
|
24
|
+
// implementation choices (kept exported for tests).
|
|
25
|
+
export const GO_INTERVAL_MIN_MINUTES = 15;
|
|
26
|
+
export const GO_INTERVAL_DEFAULT_MINUTES = 60;
|
|
27
|
+
export const GO_STARTUP_JITTER_MAX_MS = 5_000;
|
|
28
|
+
export const GO_TRANSPORT_BACKOFF_BASE_MS = 60_000;
|
|
29
|
+
export const GO_TRANSPORT_BACKOFF_CAP_MS = 30 * 60_000;
|
|
30
|
+
export const GO_AUTH_BACKOFF_BASE_MS = 60 * 60_000;
|
|
31
|
+
export const GO_AUTH_BACKOFF_CAP_MS = 6 * 60 * 60_000;
|
|
32
|
+
/** Clamp an interval to the design floor; non-finite falls back to the default. */
|
|
33
|
+
export function clampGoIntervalMinutes(value) {
|
|
34
|
+
if (!Number.isFinite(value))
|
|
35
|
+
return GO_INTERVAL_DEFAULT_MINUTES;
|
|
36
|
+
return Math.max(GO_INTERVAL_MIN_MINUTES, Math.floor(value));
|
|
37
|
+
}
|
|
38
|
+
/** startupDelaySeconds*1000 plus bounded jitter (random is in [0,1)). */
|
|
39
|
+
export function goStartupDelayMs(seconds, random) {
|
|
40
|
+
const base = Number.isFinite(seconds) && seconds > 0 ? seconds * 1000 : 0;
|
|
41
|
+
const r = Math.min(1, Math.max(0, Number.isFinite(random) ? random : 0));
|
|
42
|
+
return base + Math.floor(r * GO_STARTUP_JITTER_MAX_MS);
|
|
43
|
+
}
|
|
44
|
+
/** Exponential backoff with per-class cap (design §6.3). */
|
|
45
|
+
export function goBackoffDelayMs(streak, failureKind) {
|
|
46
|
+
const n = Math.max(1, Math.floor(streak));
|
|
47
|
+
const auth = failureKind === 'auth' || failureKind === 'entitlement';
|
|
48
|
+
const base = auth ? GO_AUTH_BACKOFF_BASE_MS : GO_TRANSPORT_BACKOFF_BASE_MS;
|
|
49
|
+
const cap = auth ? GO_AUTH_BACKOFF_CAP_MS : GO_TRANSPORT_BACKOFF_CAP_MS;
|
|
50
|
+
return Math.min(cap, base * 2 ** (n - 1));
|
|
51
|
+
}
|
|
52
|
+
/** Report text for a skipped round — always states the reason explicitly. */
|
|
53
|
+
export function goSchedulerSkipReport(reason, trigger) {
|
|
54
|
+
const when = `opencode-go scheduler (${trigger})`;
|
|
55
|
+
switch (reason) {
|
|
56
|
+
case 'disabled':
|
|
57
|
+
// Keep the legacy phrase "disabled — set opencode.go.enabled=true" so
|
|
58
|
+
// manual /model-sync go reports stay byte-compatible with P1c-1.
|
|
59
|
+
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.)`;
|
|
60
|
+
case 'already-in-flight':
|
|
61
|
+
return `${when}: skipped — a go round is already in flight (single-flight); no queue, no retry, no settings write (design §14.6).`;
|
|
62
|
+
case 'migration-applying':
|
|
63
|
+
return `${when}: skipped — migration apply in progress (M-5); no queue, no retry, no settings write (design §14.6).`;
|
|
64
|
+
case 'disposed':
|
|
65
|
+
return `${when}: skipped — scheduler disposed (plugin unload); no request, no settings write.`;
|
|
66
|
+
default:
|
|
67
|
+
return `${when}: skipped — ${reason}; no request, no settings write.`;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function isBackoffFailure(outcome) {
|
|
71
|
+
// Configuration/skip states (disabled, no-key, settings-unavailable) are not
|
|
72
|
+
// failures — they keep the normal interval. Only source failures back off.
|
|
73
|
+
return outcome.reason === 'source-failed' || outcome.failureKind !== undefined;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The scheduler state machine. One instance per plugin activation; it owns a
|
|
77
|
+
* single timer handle and a single in-flight flag shared by auto and manual
|
|
78
|
+
* triggers (that is the single-flight lock).
|
|
79
|
+
*/
|
|
80
|
+
export class GoScheduler {
|
|
81
|
+
config;
|
|
82
|
+
timers;
|
|
83
|
+
random;
|
|
84
|
+
runRound;
|
|
85
|
+
report;
|
|
86
|
+
isApplying;
|
|
87
|
+
handle;
|
|
88
|
+
started = false;
|
|
89
|
+
inFlight = false;
|
|
90
|
+
disposed = false;
|
|
91
|
+
streak = 0;
|
|
92
|
+
lastFailureKind;
|
|
93
|
+
/** Single lifetime signal: dispose() aborts the round that is currently in flight. */
|
|
94
|
+
abort = new AbortController();
|
|
95
|
+
constructor(deps) {
|
|
96
|
+
this.config = deps.config;
|
|
97
|
+
this.timers = deps.timers;
|
|
98
|
+
this.random = deps.random;
|
|
99
|
+
this.runRound = deps.runRound;
|
|
100
|
+
this.report = deps.report;
|
|
101
|
+
this.isApplying = deps.isApplying;
|
|
102
|
+
}
|
|
103
|
+
/** Arm the startup timer. No-op when disabled — zero triggers at enabled=false. */
|
|
104
|
+
start() {
|
|
105
|
+
if (this.started || this.disposed)
|
|
106
|
+
return;
|
|
107
|
+
this.started = true;
|
|
108
|
+
const cfg = this.config();
|
|
109
|
+
if (!cfg.enabled)
|
|
110
|
+
return;
|
|
111
|
+
this.arm(goStartupDelayMs(cfg.startupDelaySeconds, this.random.next()), 'startup');
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Live re-arm after a settings edit. Cancels the pending timer and reads
|
|
115
|
+
* fresh config; a round already in flight keeps its lock and picks up the
|
|
116
|
+
* new config when it schedules its own successor.
|
|
117
|
+
*/
|
|
118
|
+
rearm() {
|
|
119
|
+
if (this.disposed || !this.started)
|
|
120
|
+
return;
|
|
121
|
+
this.cancel();
|
|
122
|
+
if (this.inFlight)
|
|
123
|
+
return;
|
|
124
|
+
this.armFromConfig();
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Reset backoff after a credential change. Clears the in-memory streak and
|
|
128
|
+
* re-arms at the normal interval instead of waiting out a capped (up to 6h)
|
|
129
|
+
* backoff — a fixed key must not stay frozen for hours (design §6.3). The
|
|
130
|
+
* caller's listener is disposed with the plugin; a disposed scheduler is a
|
|
131
|
+
* no-op.
|
|
132
|
+
*/
|
|
133
|
+
resetBackoff() {
|
|
134
|
+
this.clearBackoff();
|
|
135
|
+
if (this.disposed || !this.started || this.inFlight)
|
|
136
|
+
return;
|
|
137
|
+
this.armFromConfig();
|
|
138
|
+
}
|
|
139
|
+
/** Cancel every timer and abort a round already in flight (design §6.3 卸载). */
|
|
140
|
+
dispose() {
|
|
141
|
+
this.disposed = true;
|
|
142
|
+
this.abort.abort();
|
|
143
|
+
this.cancel();
|
|
144
|
+
}
|
|
145
|
+
get isInFlight() {
|
|
146
|
+
return this.inFlight;
|
|
147
|
+
}
|
|
148
|
+
get failureStreak() {
|
|
149
|
+
return this.streak;
|
|
150
|
+
}
|
|
151
|
+
/** Manual /model-sync go trigger — shares the single-flight lock with auto rounds. */
|
|
152
|
+
triggerManual(options = {}) {
|
|
153
|
+
return this.fire('manual', options.adopt === true);
|
|
154
|
+
}
|
|
155
|
+
arm(delayMs, trigger) {
|
|
156
|
+
this.cancel();
|
|
157
|
+
// Returning the promise is harmless to real setTimeout (return value is
|
|
158
|
+
// ignored) and lets a fake timer await the whole round deterministically.
|
|
159
|
+
this.handle = this.timers.setTimeout(() => this.fire(trigger), Math.max(0, delayMs));
|
|
160
|
+
}
|
|
161
|
+
cancel() {
|
|
162
|
+
if (this.handle !== undefined) {
|
|
163
|
+
this.timers.clearTimeout(this.handle);
|
|
164
|
+
this.handle = undefined;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
armFromConfig() {
|
|
168
|
+
const cfg = this.config();
|
|
169
|
+
if (!cfg.enabled)
|
|
170
|
+
return;
|
|
171
|
+
if (this.streak > 0) {
|
|
172
|
+
this.arm(goBackoffDelayMs(this.streak, this.lastFailureKind), 'backoff');
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
this.arm(clampGoIntervalMinutes(cfg.intervalMinutes) * 60_000, 'interval');
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/** Forget the entire backoff history (transport === auth) — used by reset paths. */
|
|
179
|
+
clearBackoff() {
|
|
180
|
+
this.streak = 0;
|
|
181
|
+
this.lastFailureKind = undefined;
|
|
182
|
+
}
|
|
183
|
+
async fire(trigger, adopt = false) {
|
|
184
|
+
if (this.disposed)
|
|
185
|
+
return this.skip('disposed', trigger);
|
|
186
|
+
if (!this.config().enabled)
|
|
187
|
+
return this.skip('disabled', trigger);
|
|
188
|
+
if (this.inFlight)
|
|
189
|
+
return this.skip('already-in-flight', trigger);
|
|
190
|
+
// M-5: an in-progress migration owns the routes; drop the round now rather
|
|
191
|
+
// than racing a half-applied settings document. Same non-queueing rule as
|
|
192
|
+
// the single-flight lock (§14.6).
|
|
193
|
+
if (this.isApplying !== undefined && await this.isApplying() === true)
|
|
194
|
+
return this.skip('migration-applying', trigger);
|
|
195
|
+
// The timer that fired is now consumed; a new one is armed after the round.
|
|
196
|
+
this.cancel();
|
|
197
|
+
this.inFlight = true;
|
|
198
|
+
// A manual round is an explicit force: forget any accumulated backoff
|
|
199
|
+
// before it runs (design §6.3 "手动 force 必须重置退避").
|
|
200
|
+
if (trigger === 'manual')
|
|
201
|
+
this.clearBackoff();
|
|
202
|
+
let result;
|
|
203
|
+
try {
|
|
204
|
+
const raw = await this.runRound({ trigger, adopt, signal: this.abort.signal });
|
|
205
|
+
if (raw.ok) {
|
|
206
|
+
this.streak = 0;
|
|
207
|
+
this.lastFailureKind = undefined;
|
|
208
|
+
}
|
|
209
|
+
else if (isBackoffFailure(raw)) {
|
|
210
|
+
this.streak += 1;
|
|
211
|
+
this.lastFailureKind = raw.failureKind;
|
|
212
|
+
}
|
|
213
|
+
result = { ...raw, skipped: false };
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
217
|
+
this.streak += 1;
|
|
218
|
+
this.lastFailureKind = 'transport';
|
|
219
|
+
result = {
|
|
220
|
+
ok: false,
|
|
221
|
+
reason: 'scheduler-error',
|
|
222
|
+
text: `opencode-go scheduler: round error — ${message}; kept last-good, no further settings write`,
|
|
223
|
+
failureKind: 'transport',
|
|
224
|
+
skipped: false,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
finally {
|
|
228
|
+
this.inFlight = false;
|
|
229
|
+
}
|
|
230
|
+
this.report(result.text);
|
|
231
|
+
if (!this.disposed)
|
|
232
|
+
this.armFromConfig();
|
|
233
|
+
return result;
|
|
234
|
+
}
|
|
235
|
+
skip(reason, trigger) {
|
|
236
|
+
const text = goSchedulerSkipReport(reason, trigger);
|
|
237
|
+
this.report(text);
|
|
238
|
+
return { ok: false, reason, text, skipped: true };
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
//# sourceMappingURL=opencode-go-scheduler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
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C,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;YACtE,IAAI,CAAC,MAAM,IAAI,CAAC,CAAA;YAChB,IAAI,CAAC,eAAe,GAAG,WAAW,CAAA;YAClC,MAAM,GAAG;gBACP,EAAE,EAAE,KAAK;gBACT,MAAM,EAAE,iBAAiB;gBACzB,IAAI,EAAE,wCAAwC,OAAO,6CAA6C;gBAClG,WAAW,EAAE,WAAW;gBACxB,OAAO,EAAE,KAAK;aACf,CAAA;QACH,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAA;QACvB,CAAC;QAED,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QACxB,IAAI,CAAC,IAAI,CAAC,QAAQ;YAAE,IAAI,CAAC,aAAa,EAAE,CAAA;QACxC,OAAO,MAAM,CAAA;IACf,CAAC;IAEO,IAAI,CAAC,MAAc,EAAE,OAAkB;QAC7C,MAAM,IAAI,GAAG,qBAAqB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;QACnD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QACjB,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAA;IACnD,CAAC;CACF"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/** Official model-listing endpoint (design §6.2.6 default). */
|
|
2
|
+
export declare const OPENCODE_GO_MODELS_URL = "https://opencode.ai/inference/go/v1/models";
|
|
3
|
+
/** P0 fixed default polling interval for the go source (design §6.2.6). */
|
|
4
|
+
export declare const OPENCODE_GO_DEFAULT_INTERVAL_MINUTES = 60;
|
|
5
|
+
/** Explicit UA — the gateway bans default library UAs with CF 1010 (05 §1). */
|
|
6
|
+
export declare const GO_USER_AGENT = "dsh-model-sync/0.10.0";
|
|
7
|
+
/** Auth states carried by the diagnostic report (design §6.1 SourceSnapshot). */
|
|
8
|
+
export type GoAuthStatus = 'unknown' | 'ok' | 'invalid' | 'not-entitled' | 'unreachable';
|
|
9
|
+
/** Failure taxonomy (design §6.1 lastError.kind + no-key for the opt-out path). */
|
|
10
|
+
export type GoFailureKind = 'auth' | 'entitlement' | 'transport' | 'parse' | 'offline' | 'no-key';
|
|
11
|
+
/** One normalized official model entry. `created` is intentionally absent. */
|
|
12
|
+
export interface GoModelRecord {
|
|
13
|
+
id: string;
|
|
14
|
+
name: string;
|
|
15
|
+
canonicalId?: string;
|
|
16
|
+
releaseDate?: string;
|
|
17
|
+
lastUpdated?: string;
|
|
18
|
+
contextWindow?: number;
|
|
19
|
+
maxOutput?: number;
|
|
20
|
+
inputModalities: string[];
|
|
21
|
+
reasoning?: {
|
|
22
|
+
supported: boolean;
|
|
23
|
+
efforts: string[];
|
|
24
|
+
};
|
|
25
|
+
/** Keys of the official `api` dictionary (the protocol names). */
|
|
26
|
+
protocols: string[];
|
|
27
|
+
recommendedProtocol?: string;
|
|
28
|
+
/** Official per-protocol `base_url`, used by P1 route planning only. */
|
|
29
|
+
baseUrls: Record<string, string>;
|
|
30
|
+
provenance: 'go-official';
|
|
31
|
+
}
|
|
32
|
+
/** Failure detail attached to a source result (never contains a key value). */
|
|
33
|
+
export interface GoSourceError {
|
|
34
|
+
kind: GoFailureKind;
|
|
35
|
+
status?: number;
|
|
36
|
+
message: string;
|
|
37
|
+
}
|
|
38
|
+
/** Outcome of one source read. */
|
|
39
|
+
export interface GoSourceResult {
|
|
40
|
+
ok: boolean;
|
|
41
|
+
entries: GoModelRecord[];
|
|
42
|
+
authStatus: GoAuthStatus;
|
|
43
|
+
/** sha256(key) first 16 hex; '-' when no key was available. */
|
|
44
|
+
keyFingerprint: string;
|
|
45
|
+
/** sha256 of canonical normalized JSON; excludes `created`. */
|
|
46
|
+
payloadHash?: string;
|
|
47
|
+
observedAt: number;
|
|
48
|
+
error?: GoSourceError;
|
|
49
|
+
}
|
|
50
|
+
/** Outcome of the unauthenticated *public* catalog read (classification only, C2). */
|
|
51
|
+
export interface GoPublicCatalogResult {
|
|
52
|
+
ok: boolean;
|
|
53
|
+
ids: string[];
|
|
54
|
+
error?: GoSourceError;
|
|
55
|
+
}
|
|
56
|
+
/** Minimal response shape the fetch accepts (injected in tests). */
|
|
57
|
+
export interface GoFetchResponse {
|
|
58
|
+
status: number;
|
|
59
|
+
json(): Promise<unknown>;
|
|
60
|
+
}
|
|
61
|
+
/** Injectable fetch: a plain function, like provider-native.ts's FetchLike. */
|
|
62
|
+
export type GoFetchLike = (url: string, init: {
|
|
63
|
+
headers: Record<string, string>;
|
|
64
|
+
signal: AbortSignal;
|
|
65
|
+
}) => Promise<GoFetchResponse>;
|
|
66
|
+
/** Options for one source read. */
|
|
67
|
+
export interface FetchGoOfficialOptions {
|
|
68
|
+
url?: string;
|
|
69
|
+
apiKey?: string;
|
|
70
|
+
timeoutMs?: number;
|
|
71
|
+
observedAt?: number;
|
|
72
|
+
fetchImpl?: GoFetchLike;
|
|
73
|
+
/** Plugin-lifetime signal: aborting it aborts the in-flight fetch (design §6.3 卸载). */
|
|
74
|
+
signal?: AbortSignal;
|
|
75
|
+
}
|
|
76
|
+
/** Irreversible key fingerprint — the only key-derived value ever retained. */
|
|
77
|
+
export declare function fingerprintKey(key: string): string;
|
|
78
|
+
/** Hash of the normalized set. `created` never reaches this function. */
|
|
79
|
+
export declare function computeGoPayloadHash(entries: readonly GoModelRecord[]): string;
|
|
80
|
+
/**
|
|
81
|
+
* Parse the official `{ object: 'list', data: [...] }` payload into normalized
|
|
82
|
+
* records. Unknown fields (cost, sdk, go.monthly_usage, …) are dropped; every
|
|
83
|
+
* field the diagnostic or P1 planning needs is mapped explicitly. `created`
|
|
84
|
+
* is deliberately not copied, so it can never enter the payload hash.
|
|
85
|
+
*/
|
|
86
|
+
export declare function parseGoModelsPayload(value: unknown): GoModelRecord[];
|
|
87
|
+
export interface GoHttpClassification {
|
|
88
|
+
authStatus: GoAuthStatus;
|
|
89
|
+
failureKind?: GoFailureKind;
|
|
90
|
+
/** Human-readable reason; never includes a key value. */
|
|
91
|
+
message: string;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Classify an official HTTP response. 401 = invalid key, 403 = either the
|
|
95
|
+
* documented EntitlementError (not-entitled) or the Cloudflare 1010 UA ban
|
|
96
|
+
* (transport), 5xx/429 = transport. Only 200 is a usable source.
|
|
97
|
+
*/
|
|
98
|
+
export declare function classifyGoHttpStatus(status: number, body: unknown): GoHttpClassification;
|
|
99
|
+
/**
|
|
100
|
+
* Read the official listing once. No key → no request (design V4), no entries,
|
|
101
|
+
* status 'unknown'. With a key the request carries Bearer + explicit UA; the
|
|
102
|
+
* key itself is never logged or stored, only its fingerprint.
|
|
103
|
+
*/
|
|
104
|
+
/**
|
|
105
|
+
* Read the official *unauthenticated* public listing for one purpose only:
|
|
106
|
+
* answering "is this id official?" during migration planning (design §14.2).
|
|
107
|
+
* Per C2 the public list is NEVER a servable source — no authorization header
|
|
108
|
+
* is sent, no model is written to settings from it. If the read fails the
|
|
109
|
+
* caller must fail closed (ids become `unclassified`, never deleted).
|
|
110
|
+
*/
|
|
111
|
+
export declare function fetchGoPublicCatalogIds(options?: {
|
|
112
|
+
url?: string;
|
|
113
|
+
timeoutMs?: number;
|
|
114
|
+
fetchImpl?: GoFetchLike;
|
|
115
|
+
signal?: AbortSignal;
|
|
116
|
+
}): Promise<GoPublicCatalogResult>;
|
|
117
|
+
export declare function fetchGoOfficialModels(options?: FetchGoOfficialOptions): Promise<GoSourceResult>;
|