omnicell-router 0.1.0 → 0.2.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/RELEASING.md +238 -238
- package/RELEASING.zh-CN.md +230 -230
- package/backend/dist/capture/recorder.js +2 -1
- package/backend/dist/cli/clients/base.js +8 -16
- package/backend/dist/cli/clients/dsh.js +8 -16
- package/backend/dist/cli/clients/pi.js +16 -9
- package/backend/dist/cli/types.d.ts +5 -0
- package/backend/dist/config/env.js +2 -1
- package/backend/dist/config/index.js +2 -0
- package/backend/dist/config/types.d.ts +23 -0
- package/backend/dist/harness/adapters/amp.d.ts +2 -0
- package/backend/dist/harness/adapters/amp.js +7 -0
- package/backend/dist/harness/adapters/claude.d.ts +2 -0
- package/backend/dist/harness/adapters/claude.js +7 -0
- package/backend/dist/harness/adapters/cline.d.ts +2 -0
- package/backend/dist/harness/adapters/cline.js +6 -0
- package/backend/dist/harness/adapters/codex.d.ts +2 -0
- package/backend/dist/harness/adapters/codex.js +6 -0
- package/backend/dist/harness/adapters/cursor.d.ts +5 -0
- package/backend/dist/harness/adapters/cursor.js +10 -0
- package/backend/dist/harness/adapters/declarative.d.ts +62 -0
- package/backend/dist/harness/adapters/declarative.js +202 -0
- package/backend/dist/harness/adapters/dsh.d.ts +5 -0
- package/backend/dist/harness/adapters/dsh.js +6 -0
- package/backend/dist/harness/adapters/gemini-cli.d.ts +2 -0
- package/backend/dist/harness/adapters/gemini-cli.js +7 -0
- package/backend/dist/harness/adapters/github-copilot.d.ts +5 -0
- package/backend/dist/harness/adapters/github-copilot.js +10 -0
- package/backend/dist/harness/adapters/index.d.ts +44 -0
- package/backend/dist/harness/adapters/index.js +99 -0
- package/backend/dist/harness/adapters/opencode.d.ts +2 -0
- package/backend/dist/harness/adapters/opencode.js +55 -0
- package/backend/dist/harness/adapters/pi.d.ts +1 -0
- package/backend/dist/harness/adapters/pi.js +22 -0
- package/backend/dist/harness/adapters/skills-paths.d.ts +15 -0
- package/backend/dist/harness/adapters/skills-paths.js +17 -0
- package/backend/dist/harness/adapters/types.d.ts +174 -0
- package/backend/dist/harness/adapters/types.js +12 -0
- package/backend/dist/harness/adapters/universal.d.ts +6 -0
- package/backend/dist/harness/adapters/universal.js +10 -0
- package/backend/dist/harness/adapters/windsurf.d.ts +2 -0
- package/backend/dist/harness/adapters/windsurf.js +10 -0
- package/backend/dist/harness/adapters/zed.d.ts +2 -0
- package/backend/dist/harness/adapters/zed.js +6 -0
- package/backend/dist/harness/awesome-opencode.d.ts +40 -0
- package/backend/dist/harness/awesome-opencode.js +75 -0
- package/backend/dist/harness/browse.d.ts +202 -0
- package/backend/dist/harness/browse.js +1214 -0
- package/backend/dist/harness/catalog-npm.d.ts +59 -0
- package/backend/dist/harness/catalog-npm.js +145 -0
- package/backend/dist/harness/clawhub.d.ts +212 -0
- package/backend/dist/harness/clawhub.js +426 -0
- package/backend/dist/harness/config-file.d.ts +29 -0
- package/backend/dist/harness/config-file.js +35 -0
- package/backend/dist/harness/detail.d.ts +74 -0
- package/backend/dist/harness/detail.js +330 -0
- package/backend/dist/harness/discover.d.ts +118 -0
- package/backend/dist/harness/discover.js +507 -0
- package/backend/dist/harness/drift.d.ts +21 -0
- package/backend/dist/harness/drift.js +25 -0
- package/backend/dist/harness/hash.d.ts +10 -0
- package/backend/dist/harness/hash.js +63 -0
- package/backend/dist/harness/hosts.d.ts +18 -0
- package/backend/dist/harness/hosts.js +15 -0
- package/backend/dist/harness/install.d.ts +88 -0
- package/backend/dist/harness/install.js +301 -0
- package/backend/dist/harness/inventory.d.ts +10 -0
- package/backend/dist/harness/inventory.js +198 -0
- package/backend/dist/harness/ledger.d.ts +39 -0
- package/backend/dist/harness/ledger.js +83 -0
- package/backend/dist/harness/maintenance.d.ts +115 -0
- package/backend/dist/harness/maintenance.js +603 -0
- package/backend/dist/harness/manifest.d.ts +31 -0
- package/backend/dist/harness/manifest.js +234 -0
- package/backend/dist/harness/net.d.ts +86 -0
- package/backend/dist/harness/net.js +113 -0
- package/backend/dist/harness/opencode-cafe.d.ts +40 -0
- package/backend/dist/harness/opencode-cafe.js +85 -0
- package/backend/dist/harness/opencode-ecosystem.d.ts +34 -0
- package/backend/dist/harness/opencode-ecosystem.js +246 -0
- package/backend/dist/harness/pi-dev.d.ts +81 -0
- package/backend/dist/harness/pi-dev.js +187 -0
- package/backend/dist/harness/probe.d.ts +33 -0
- package/backend/dist/harness/probe.js +85 -0
- package/backend/dist/harness/registry.d.ts +2 -0
- package/backend/dist/harness/registry.js +26 -0
- package/backend/dist/harness/scopes.d.ts +21 -0
- package/backend/dist/harness/scopes.js +22 -0
- package/backend/dist/harness/skill-md.d.ts +18 -0
- package/backend/dist/harness/skill-md.js +71 -0
- package/backend/dist/harness/skills-sh.d.ts +109 -0
- package/backend/dist/harness/skills-sh.js +178 -0
- package/backend/dist/harness/smithery.d.ts +64 -0
- package/backend/dist/harness/smithery.js +146 -0
- package/backend/dist/harness/sources/awesome-opencode.d.ts +11 -0
- package/backend/dist/harness/sources/awesome-opencode.js +39 -0
- package/backend/dist/harness/sources/clawhub.d.ts +2 -0
- package/backend/dist/harness/sources/clawhub.js +83 -0
- package/backend/dist/harness/sources/github.d.ts +9 -0
- package/backend/dist/harness/sources/github.js +202 -0
- package/backend/dist/harness/sources/mcp-official.d.ts +2 -0
- package/backend/dist/harness/sources/mcp-official.js +110 -0
- package/backend/dist/harness/sources/npm.d.ts +2 -0
- package/backend/dist/harness/sources/npm.js +152 -0
- package/backend/dist/harness/sources/opencode-cafe.d.ts +11 -0
- package/backend/dist/harness/sources/opencode-cafe.js +41 -0
- package/backend/dist/harness/sources/smithery.d.ts +2 -0
- package/backend/dist/harness/sources/smithery.js +81 -0
- package/backend/dist/harness/spi.d.ts +98 -0
- package/backend/dist/harness/spi.js +8 -0
- package/backend/dist/harness/stars.d.ts +70 -0
- package/backend/dist/harness/stars.js +202 -0
- package/backend/dist/harness/translation.d.ts +142 -0
- package/backend/dist/harness/translation.js +567 -0
- package/backend/dist/harness/tree.d.ts +13 -0
- package/backend/dist/harness/tree.js +19 -0
- package/backend/dist/harness/types.d.ts +286 -0
- package/backend/dist/harness/types.js +8 -0
- package/backend/dist/harness/writers/extension.d.ts +116 -0
- package/backend/dist/harness/writers/extension.js +219 -0
- package/backend/dist/harness/writers/mcp.d.ts +72 -0
- package/backend/dist/harness/writers/mcp.js +195 -0
- package/backend/dist/harness/writers/skill.d.ts +60 -0
- package/backend/dist/harness/writers/skill.js +146 -0
- package/backend/dist/opencode/agents.d.ts +5 -0
- package/backend/dist/opencode/agents.js +10 -1
- package/backend/dist/opencode/catalog/cache.js +2 -1
- package/backend/dist/opencode/catalog/logos.js +2 -1
- package/backend/dist/opencode/catalog/ocr-store.js +2 -1
- package/backend/dist/opencode/catalog/overrides-store.js +2 -1
- package/backend/dist/opencode/credentials/factory.js +10 -27
- package/backend/dist/opencode/credentials/ocr-store.js +2 -1
- package/backend/dist/opencode/provider-defs.js +2 -1
- package/backend/dist/opencode/user-config.js +8 -16
- package/backend/dist/providers/registry.d.ts +15 -4
- package/backend/dist/providers/registry.js +29 -5
- package/backend/dist/providers/virtual-meta.d.ts +13 -0
- package/backend/dist/providers/virtual-meta.js +18 -0
- package/backend/dist/quota/store.js +2 -1
- package/backend/dist/resilience/breaker-state.js +2 -1
- package/backend/dist/resilience/credential-state.js +2 -1
- package/backend/dist/routes/console.js +137 -15
- package/backend/dist/routes/harness.d.ts +2 -0
- package/backend/dist/routes/harness.js +622 -0
- package/backend/dist/server.js +4 -0
- package/backend/dist/session/session-manager.js +2 -1
- package/backend/dist/surfaces/systemone-probe.d.ts +25 -0
- package/backend/dist/surfaces/systemone-probe.js +74 -0
- package/backend/dist/utils/atomic-write.d.ts +12 -0
- package/backend/dist/utils/atomic-write.js +29 -0
- package/bin/ocr.js +2 -2
- package/frontend/dist/assets/{index-DEo2iAT7.css → index-1o7qI-KJ.css} +1 -1
- package/frontend/dist/assets/index-DvXKvNxl.js +962 -0
- package/frontend/dist/index.html +2 -2
- package/package.json +87 -87
- package/backend/dist/budget/budget-manager.d.ts +0 -25
- package/backend/dist/budget/budget-manager.js +0 -54
- package/backend/dist/config/governance-limits.d.ts +0 -68
- package/backend/dist/config/governance-limits.js +0 -261
- package/backend/dist/flywheel/collector.d.ts +0 -74
- package/backend/dist/flywheel/collector.js +0 -235
- package/backend/dist/flywheel/dataset.d.ts +0 -130
- package/backend/dist/flywheel/dataset.js +0 -638
- package/backend/dist/flywheel/decision-capture.d.ts +0 -68
- package/backend/dist/flywheel/decision-capture.js +0 -85
- package/backend/dist/flywheel/prune.d.ts +0 -133
- package/backend/dist/flywheel/prune.js +0 -495
- package/backend/dist/flywheel/schema.d.ts +0 -108
- package/backend/dist/flywheel/schema.js +0 -117
- package/backend/dist/flywheel/trainer.d.ts +0 -84
- package/backend/dist/flywheel/trainer.js +0 -198
- package/backend/dist/opencode/catalog/custom-store.d.ts +0 -50
- package/backend/dist/opencode/catalog/custom-store.js +0 -144
- package/backend/dist/opencode/credentials/push.d.ts +0 -38
- package/backend/dist/opencode/credentials/push.js +0 -116
- package/backend/dist/opencode/credentials/reconcile.d.ts +0 -49
- package/backend/dist/opencode/credentials/reconcile.js +0 -216
- package/backend/dist/opencode/sync.d.ts +0 -29
- package/backend/dist/opencode/sync.js +0 -186
- package/backend/dist/pipeline/account-selection.d.ts +0 -101
- package/backend/dist/pipeline/account-selection.js +0 -112
- package/backend/dist/providers/systemone.d.ts +0 -29
- package/backend/dist/providers/systemone.js +0 -170
- package/backend/dist/ratelimit/duration.d.ts +0 -24
- package/backend/dist/ratelimit/duration.js +0 -43
- package/backend/dist/ratelimit/governance-runtime.d.ts +0 -121
- package/backend/dist/ratelimit/governance-runtime.js +0 -183
- package/backend/dist/ratelimit/index.d.ts +0 -4
- package/backend/dist/ratelimit/index.js +0 -4
- package/backend/dist/ratelimit/limiter.d.ts +0 -157
- package/backend/dist/ratelimit/limiter.js +0 -244
- package/backend/dist/ratelimit/sliding-window-counter.d.ts +0 -63
- package/backend/dist/ratelimit/sliding-window-counter.js +0 -103
- package/backend/dist/ratelimit/subject.d.ts +0 -26
- package/backend/dist/ratelimit/subject.js +0 -28
- package/backend/dist/resilience/account-state.d.ts +0 -84
- package/backend/dist/resilience/account-state.js +0 -228
- package/backend/dist/router/layer1-classifier.d.ts +0 -111
- package/backend/dist/router/layer1-classifier.js +0 -467
- package/backend/dist/router/layer2-judge.d.ts +0 -111
- package/backend/dist/router/layer2-judge.js +0 -373
- package/backend/dist/router/retry-escalation.d.ts +0 -89
- package/backend/dist/router/retry-escalation.js +0 -99
- package/backend/dist/routes/flywheel.d.ts +0 -3
- package/backend/dist/routes/flywheel.js +0 -268
- package/backend/dist/routes/systemone.d.ts +0 -3
- package/backend/dist/routes/systemone.js +0 -90
- package/backend/dist/surfaces/governance-envelope.d.ts +0 -34
- package/backend/dist/surfaces/governance-envelope.js +0 -47
- package/backend/dist/utils/token-estimator.d.ts +0 -7
- package/backend/dist/utils/token-estimator.js +0 -20
- package/frontend/dist/assets/index-nwgWkmU1.js +0 -840
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { McpLaunch } from '../spi.js';
|
|
2
|
+
import type { InstallSkipped } from './skill.js';
|
|
3
|
+
import type { HarnessHostId, HarnessScope, Pin, TrustTier } from '../types.js';
|
|
4
|
+
/**
|
|
5
|
+
* MCP kind writer (ADR-0015 §8) — CONFIG MERGE, never overwrite.
|
|
6
|
+
*
|
|
7
|
+
* The two data-loss gates, enforced here:
|
|
8
|
+
* - gate 2 (no silent merge): `planMcp` computes the exact added/changed keys per host file; the
|
|
9
|
+
* orchestrator refuses to apply until the caller confirms. A file that cannot be round-tripped as
|
|
10
|
+
* plain JSON (comments, trailing garbage) is REFUSED, not rewritten — stringify-after-parse would
|
|
11
|
+
* silently drop hand-written comments.
|
|
12
|
+
* - gate 4 (secrets are named, not stored): env/header values never reach this module — the launch
|
|
13
|
+
* carries key NAMES only, and the merge materializes `{env:KEY}` references for the operator to
|
|
14
|
+
* satisfy in their own runtime (ADR-0006 stays untouched).
|
|
15
|
+
*/
|
|
16
|
+
export interface McpApplyRequest {
|
|
17
|
+
sourceId: string;
|
|
18
|
+
/** `<sourceId>:<ref>` — the manifest canonicalKey. */
|
|
19
|
+
canonicalKey: string;
|
|
20
|
+
/** Server key inside the host's MCP container (normalized, stable identifier). */
|
|
21
|
+
name: string;
|
|
22
|
+
/** The source's one-line description; persisted so the installed row is not a nameless entry. */
|
|
23
|
+
description?: string | null;
|
|
24
|
+
launch: McpLaunch;
|
|
25
|
+
trust: TrustTier;
|
|
26
|
+
/** Adapter provenance (an npm source pins `dist.integrity`); absent = a manual entry, recorded
|
|
27
|
+
* as `adopted` rather than given an invented pin. */
|
|
28
|
+
pin?: Pin;
|
|
29
|
+
/** Listing surfaces recorded with the manifest (writer source first); absent = `[sourceId]`. */
|
|
30
|
+
listedOn?: string[];
|
|
31
|
+
hosts: HarnessHostId[];
|
|
32
|
+
scope: HarnessScope;
|
|
33
|
+
projectRoot?: string;
|
|
34
|
+
}
|
|
35
|
+
export interface McpTargetPlan {
|
|
36
|
+
host: HarnessHostId;
|
|
37
|
+
file: string;
|
|
38
|
+
exists: boolean;
|
|
39
|
+
/** Container key the servers merge into, from the host adapter (`mcp`). */
|
|
40
|
+
container?: string;
|
|
41
|
+
/** Refusal for THIS host — apply skips it, the rest proceed. */
|
|
42
|
+
refused?: string;
|
|
43
|
+
/** Server keys the merge would add / change (gate 2). */
|
|
44
|
+
addedKeys: string[];
|
|
45
|
+
changedKeys: string[];
|
|
46
|
+
}
|
|
47
|
+
export type McpPlan = {
|
|
48
|
+
ok: true;
|
|
49
|
+
targets: McpTargetPlan[];
|
|
50
|
+
} | {
|
|
51
|
+
ok: false;
|
|
52
|
+
code: 'bad_request';
|
|
53
|
+
message: string;
|
|
54
|
+
};
|
|
55
|
+
export type McpApplyResult = {
|
|
56
|
+
ok: true;
|
|
57
|
+
artifactId: string;
|
|
58
|
+
name: string;
|
|
59
|
+
/** Hosts whose config received the server entry. */
|
|
60
|
+
copies: {
|
|
61
|
+
host: string;
|
|
62
|
+
path: string;
|
|
63
|
+
}[];
|
|
64
|
+
skipped: InstallSkipped[];
|
|
65
|
+
} | {
|
|
66
|
+
ok: false;
|
|
67
|
+
code: 'no_target' | 'exists' | 'foreign' | 'bad_request' | 'upstream';
|
|
68
|
+
message: string;
|
|
69
|
+
skipped?: InstallSkipped[];
|
|
70
|
+
};
|
|
71
|
+
export declare function planMcp(req: McpApplyRequest): McpPlan;
|
|
72
|
+
export declare function applyMcp(req: McpApplyRequest): McpApplyResult;
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { hashFile } from '../hash.js';
|
|
3
|
+
import { adapterFor, isMcpHost } from '../adapters/index.js';
|
|
4
|
+
import { claimPath, readManifest, releaseClaim, writeManifest } from '../manifest.js';
|
|
5
|
+
import { createBackup, safeWriteJson } from '../../cli/clients/base.js';
|
|
6
|
+
const BOM = '\uFEFF';
|
|
7
|
+
/** The opencode-shaped entry for one launch. Values reference the operator's environment. */
|
|
8
|
+
function desiredEntry(launch) {
|
|
9
|
+
if ('command' in launch) {
|
|
10
|
+
return {
|
|
11
|
+
type: 'local',
|
|
12
|
+
command: [launch.command, ...(launch.args ?? [])],
|
|
13
|
+
enabled: true,
|
|
14
|
+
environment: Object.fromEntries((launch.envKeys ?? []).map((key) => [key, `{env:${key}}`])),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
type: 'remote',
|
|
19
|
+
url: launch.url,
|
|
20
|
+
enabled: true,
|
|
21
|
+
headers: Object.fromEntries((launch.headerKeys ?? []).map((key) => [key, `{env:${key}}`])),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** Key-order-insensitive equality — a re-merged identical server must read as a no-op. */
|
|
25
|
+
function sameEntry(a, b) {
|
|
26
|
+
const stable = (value) => {
|
|
27
|
+
if (Array.isArray(value))
|
|
28
|
+
return value.map(stable);
|
|
29
|
+
if (value && typeof value === 'object') {
|
|
30
|
+
return Object.fromEntries(Object.entries(value)
|
|
31
|
+
.sort(([x], [y]) => (x < y ? -1 : x > y ? 1 : 0))
|
|
32
|
+
.map(([k, v]) => [k, stable(v)]));
|
|
33
|
+
}
|
|
34
|
+
return value;
|
|
35
|
+
};
|
|
36
|
+
return JSON.stringify(stable(a)) === JSON.stringify(stable(b));
|
|
37
|
+
}
|
|
38
|
+
/** Read + validate one config file. Refuses anything a JSON round-trip would silently mangle. */
|
|
39
|
+
function readPlainJson(file, exists) {
|
|
40
|
+
if (!exists)
|
|
41
|
+
return { ok: true, config: {} };
|
|
42
|
+
let text;
|
|
43
|
+
try {
|
|
44
|
+
text = fs.readFileSync(file, 'utf8');
|
|
45
|
+
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
return { ok: false, reason: `cannot read config: ${e instanceof Error ? e.message : e}` };
|
|
48
|
+
}
|
|
49
|
+
if (text.startsWith(BOM))
|
|
50
|
+
text = text.slice(1);
|
|
51
|
+
try {
|
|
52
|
+
const parsed = JSON.parse(text);
|
|
53
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
54
|
+
return { ok: false, reason: 'config root is not a JSON object' };
|
|
55
|
+
}
|
|
56
|
+
return { ok: true, config: parsed };
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
const commentish = /(^|[^:"'\\])\/\/|\/\*/.test(text);
|
|
60
|
+
return {
|
|
61
|
+
ok: false,
|
|
62
|
+
reason: commentish
|
|
63
|
+
? 'config contains comments (JSONC) — a JSON rewrite would drop them; refusing'
|
|
64
|
+
: 'config is not valid JSON; refusing',
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
export function planMcp(req) {
|
|
69
|
+
const targets = [];
|
|
70
|
+
for (const host of req.hosts) {
|
|
71
|
+
const adapter = adapterFor(host);
|
|
72
|
+
if (!isMcpHost(adapter)) {
|
|
73
|
+
targets.push({
|
|
74
|
+
host,
|
|
75
|
+
file: '',
|
|
76
|
+
exists: false,
|
|
77
|
+
refused: `host '${host}' has no verified MCP landing`,
|
|
78
|
+
addedKeys: [],
|
|
79
|
+
changedKeys: [],
|
|
80
|
+
});
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const landing = adapter.configPath({ projectRoot: req.projectRoot ?? process.cwd() }, req.scope);
|
|
84
|
+
if (!landing.ok) {
|
|
85
|
+
targets.push({ host, file: '', exists: false, refused: landing.reason, addedKeys: [], changedKeys: [] });
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const exists = fs.existsSync(landing.path);
|
|
89
|
+
const parsed = readPlainJson(landing.path, exists);
|
|
90
|
+
if (!parsed.ok) {
|
|
91
|
+
targets.push({ host, file: landing.path, exists, refused: parsed.reason, addedKeys: [], changedKeys: [] });
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const container = parsed.config[adapter.container];
|
|
95
|
+
const existing = container && typeof container === 'object' && !Array.isArray(container)
|
|
96
|
+
? container[req.name]
|
|
97
|
+
: undefined;
|
|
98
|
+
const desired = desiredEntry(req.launch);
|
|
99
|
+
targets.push({
|
|
100
|
+
host,
|
|
101
|
+
file: landing.path,
|
|
102
|
+
exists,
|
|
103
|
+
container: adapter.container,
|
|
104
|
+
refused: undefined,
|
|
105
|
+
addedKeys: existing === undefined ? [req.name] : [],
|
|
106
|
+
changedKeys: existing !== undefined && !sameEntry(existing, desired) ? [req.name] : [],
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return { ok: true, targets };
|
|
110
|
+
}
|
|
111
|
+
export function applyMcp(req) {
|
|
112
|
+
const plan = planMcp(req);
|
|
113
|
+
if (!plan.ok)
|
|
114
|
+
return { ok: false, code: plan.code, message: plan.message };
|
|
115
|
+
const artifactId = `mcp:${req.canonicalKey}`;
|
|
116
|
+
const copies = [];
|
|
117
|
+
const skipped = [];
|
|
118
|
+
const hashedFiles = [];
|
|
119
|
+
for (const target of plan.targets) {
|
|
120
|
+
if (target.refused) {
|
|
121
|
+
skipped.push({ host: target.host, code: 'bad_request', message: target.refused });
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
// One materialized path, one owning environment (§D4): a shared config file is claimed like any
|
|
125
|
+
// other write target, so two OCR envs cannot interleave merges into one file.
|
|
126
|
+
const claim = claimPath(target.file, artifactId);
|
|
127
|
+
if (!claim.ok) {
|
|
128
|
+
skipped.push({ host: target.host, code: 'foreign', message: `${target.file} is claimed by environment '${claim.env}'` });
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const parsed = readPlainJson(target.file, target.exists);
|
|
132
|
+
if (!parsed.ok) {
|
|
133
|
+
// Nothing was written, so the claim taken above must not outlive this skip.
|
|
134
|
+
releaseClaim(target.file);
|
|
135
|
+
skipped.push({ host: target.host, code: 'bad_request', message: parsed.reason });
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const key = target.container ?? 'mcp';
|
|
139
|
+
const container = parsed.config[key] && typeof parsed.config[key] === 'object' && !Array.isArray(parsed.config[key])
|
|
140
|
+
? parsed.config[key]
|
|
141
|
+
: (parsed.config[key] = {});
|
|
142
|
+
container[req.name] = desiredEntry(req.launch);
|
|
143
|
+
try {
|
|
144
|
+
// ADR-0015 transactional apply: snapshot the pre-image BEFORE touching the target, then the
|
|
145
|
+
// house atomic rename (retries around Windows rename flakiness included).
|
|
146
|
+
createBackup(target.file);
|
|
147
|
+
safeWriteJson(target.file, parsed.config);
|
|
148
|
+
}
|
|
149
|
+
catch (e) {
|
|
150
|
+
skipped.push({ host: target.host, code: 'upstream', message: e instanceof Error ? e.message : String(e) });
|
|
151
|
+
releaseClaim(target.file);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
hashedFiles.push({ path: target.file, hash: hashFile(target.file) });
|
|
155
|
+
copies.push({ host: target.host, path: target.file });
|
|
156
|
+
}
|
|
157
|
+
if (copies.length === 0) {
|
|
158
|
+
return { ok: false, code: skipped[0]?.code ?? 'no_target', message: skipped.map((s) => `${s.host}: ${s.message}`).join('; ') || 'no host config to merge into', skipped };
|
|
159
|
+
}
|
|
160
|
+
const manifest = readManifest();
|
|
161
|
+
const now = new Date().toISOString();
|
|
162
|
+
const previous = manifest.artifacts.find((a) => a.id === artifactId);
|
|
163
|
+
const kept = (previous?.copies ?? []).filter((c) => !copies.some((item) => item.path === c.path));
|
|
164
|
+
const contentHash = hashedFiles[0].hash;
|
|
165
|
+
manifest.artifacts = [
|
|
166
|
+
...manifest.artifacts.filter((a) => a.id !== artifactId),
|
|
167
|
+
{
|
|
168
|
+
id: artifactId,
|
|
169
|
+
canonicalKey: req.canonicalKey,
|
|
170
|
+
kind: 'mcp',
|
|
171
|
+
name: req.name,
|
|
172
|
+
...(req.description ? { description: req.description } : {}),
|
|
173
|
+
sourceId: req.sourceId,
|
|
174
|
+
ref: req.canonicalKey.slice(req.sourceId.length + 1),
|
|
175
|
+
pin: req.pin ?? { adopted: true },
|
|
176
|
+
contentHash,
|
|
177
|
+
listedOn: req.listedOn ?? [req.sourceId],
|
|
178
|
+
trust: req.trust,
|
|
179
|
+
copies: [
|
|
180
|
+
...kept,
|
|
181
|
+
...copies.map((item, i) => ({
|
|
182
|
+
scope: req.scope,
|
|
183
|
+
host: item.host,
|
|
184
|
+
rootId: 'default',
|
|
185
|
+
path: item.path,
|
|
186
|
+
contentHash: hashedFiles[i].hash,
|
|
187
|
+
placement: 'materialized',
|
|
188
|
+
installedAt: now,
|
|
189
|
+
})),
|
|
190
|
+
],
|
|
191
|
+
},
|
|
192
|
+
];
|
|
193
|
+
writeManifest(manifest);
|
|
194
|
+
return { ok: true, artifactId, name: req.name, copies, skipped };
|
|
195
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { SkillFile } from '../spi.js';
|
|
2
|
+
import type { HarnessHostId, HarnessScope, Pin, TrustTier } from '../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Skill kind writer (ADR-0015 §8) — the disk half of a skill install.
|
|
5
|
+
*
|
|
6
|
+
* Order matters: every byte is written into the content-addressed store BEFORE anything appears in a
|
|
7
|
+
* host directory, and the manifest is written last. A failure at any point leaves no half-installed
|
|
8
|
+
* skill; the store copy is content-addressed, so a retry re-uses it and costs no network.
|
|
9
|
+
*
|
|
10
|
+
* One host can refuse (no skills location, path already occupied, claimed by another environment)
|
|
11
|
+
* while the rest proceed — refusals are reported per host, never silently dropped. Nine hosts read
|
|
12
|
+
* the same project directory, so identical target paths are installed ONCE, labeled by the owner
|
|
13
|
+
* host; the console can then never show "installed under Codex" for a directory the scan reports as
|
|
14
|
+
* universal.
|
|
15
|
+
*/
|
|
16
|
+
/** One host that did not receive a copy, with the reason. */
|
|
17
|
+
export interface InstallSkipped {
|
|
18
|
+
host: string;
|
|
19
|
+
code: 'bad_request' | 'exists' | 'foreign' | 'upstream';
|
|
20
|
+
message: string;
|
|
21
|
+
}
|
|
22
|
+
export interface SkillApplyRequest {
|
|
23
|
+
sourceId: string;
|
|
24
|
+
/** `<sourceId>:<ref>[#detail]` — the manifest canonicalKey. */
|
|
25
|
+
canonicalKey: string;
|
|
26
|
+
/** Raw artifact name (vendor spelling); normalized here for target paths. */
|
|
27
|
+
rawName: string;
|
|
28
|
+
/** Filesystem-safe store token from the resolving adapter. */
|
|
29
|
+
storeKey: string;
|
|
30
|
+
files: SkillFile[];
|
|
31
|
+
pin: Pin;
|
|
32
|
+
trust: TrustTier;
|
|
33
|
+
/** Listing surfaces recorded with the manifest (writer source first); absent = `[sourceId]`. */
|
|
34
|
+
listedOn?: string[];
|
|
35
|
+
hosts: HarnessHostId[];
|
|
36
|
+
scope: HarnessScope;
|
|
37
|
+
/** Project root for `project` scope resolution; defaults to this process's cwd. */
|
|
38
|
+
projectRoot?: string;
|
|
39
|
+
}
|
|
40
|
+
export type SkillApplyResult = {
|
|
41
|
+
ok: true;
|
|
42
|
+
artifactId: string;
|
|
43
|
+
name: string;
|
|
44
|
+
storeRef: string;
|
|
45
|
+
files: number;
|
|
46
|
+
bytes: number;
|
|
47
|
+
/** Hosts that were materialized. */
|
|
48
|
+
copies: {
|
|
49
|
+
host: string;
|
|
50
|
+
path: string;
|
|
51
|
+
}[];
|
|
52
|
+
/** Hosts that were refused; a partial install succeeds, installing nowhere does not. */
|
|
53
|
+
skipped: InstallSkipped[];
|
|
54
|
+
} | {
|
|
55
|
+
ok: false;
|
|
56
|
+
code: 'no_target' | 'invalid_name' | 'exists' | 'foreign' | 'bad_request' | 'upstream';
|
|
57
|
+
message: string;
|
|
58
|
+
skipped?: InstallSkipped[];
|
|
59
|
+
};
|
|
60
|
+
export declare function applySkill(req: SkillApplyRequest): SkillApplyResult;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { getOcrHomeDir } from '../../cli/paths.js';
|
|
4
|
+
import { hashTree } from '../hash.js';
|
|
5
|
+
import { adapterFor, isSkillsHost, skillOwner } from '../adapters/index.js';
|
|
6
|
+
import { claimPath, readManifest, releaseClaim, writeManifest } from '../manifest.js';
|
|
7
|
+
import { normalizeName } from '../spi.js';
|
|
8
|
+
export function applySkill(req) {
|
|
9
|
+
const name = normalizeName(req.rawName);
|
|
10
|
+
if (!name) {
|
|
11
|
+
return { ok: false, code: 'invalid_name', message: `derived an unusable skill name from '${req.rawName}'` };
|
|
12
|
+
}
|
|
13
|
+
// The store key becomes a path segment under <ocrHome>/harness/store — a key containing a
|
|
14
|
+
// separator or `..` would escape the store (the install.ts fallback builds it from a raw name).
|
|
15
|
+
if (!/^[\w@.-]+$/.test(req.storeKey) || req.storeKey.includes('..')) {
|
|
16
|
+
return { ok: false, code: 'invalid_name', message: `refusing unsafe store key '${req.storeKey}'` };
|
|
17
|
+
}
|
|
18
|
+
// 1. Resolve every requested target + ownership checks BEFORE any write. All-target refusals fail
|
|
19
|
+
// with the first refusal's code so the wire keeps distinguishing "exists" from "foreign".
|
|
20
|
+
const artifactId = `skill:${req.canonicalKey}`;
|
|
21
|
+
const targets = [];
|
|
22
|
+
const skipped = [];
|
|
23
|
+
for (const host of req.hosts) {
|
|
24
|
+
const adapter = adapterFor(host);
|
|
25
|
+
const target = isSkillsHost(adapter)
|
|
26
|
+
? adapter.skillTarget({ projectRoot: req.projectRoot ?? process.cwd() }, req.scope, name)
|
|
27
|
+
: { ok: false, reason: `host '${host}' has no skills location` };
|
|
28
|
+
if (!target.ok) {
|
|
29
|
+
skipped.push({ host, code: 'bad_request', message: target.reason });
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (fs.existsSync(target.path)) {
|
|
33
|
+
skipped.push({ host, code: 'exists', message: `${target.path} already exists — uninstall it first` });
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const owner = skillOwner(target.root.root, req.scope, req.projectRoot) ?? host;
|
|
37
|
+
if (targets.some((t) => t.path === target.path))
|
|
38
|
+
continue;
|
|
39
|
+
const claim = claimPath(target.path, artifactId);
|
|
40
|
+
if (!claim.ok) {
|
|
41
|
+
skipped.push({ host: owner, code: 'foreign', message: `${target.path} is claimed by environment '${claim.env}'` });
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
targets.push({ host: owner, path: target.path });
|
|
45
|
+
}
|
|
46
|
+
if (targets.length === 0) {
|
|
47
|
+
return {
|
|
48
|
+
ok: false,
|
|
49
|
+
code: skipped[0]?.code ?? 'no_target',
|
|
50
|
+
message: skipped.map((s) => `${s.host}: ${s.message}`).join('; ') || 'no host location to install into',
|
|
51
|
+
skipped,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
// 2. Stage the payload into the store, then promote it atomically. A store-phase throw must not
|
|
55
|
+
// leak the phase-1 claims or the staging dir — release + clean, then fail as upstream.
|
|
56
|
+
const storeRef = path.join(getOcrHomeDir(), 'harness', 'store', req.storeKey);
|
|
57
|
+
const staging = `${storeRef}.tmp-${process.pid.toString(36)}${Date.now().toString(36)}`;
|
|
58
|
+
let written = 0;
|
|
59
|
+
try {
|
|
60
|
+
fs.mkdirSync(staging, { recursive: true });
|
|
61
|
+
for (const entry of req.files) {
|
|
62
|
+
// Normalize before the traversal check: on win32 `\` is ALSO a separator, so a tarball entry
|
|
63
|
+
// named `..\..\evil` would sail past a `/`-only split and land outside the staging dir.
|
|
64
|
+
const rel = entry.path.replace(/\\/g, '/');
|
|
65
|
+
if (!rel || rel.startsWith('/') || /^[A-Za-z]:/.test(rel) || rel.split('/').includes('..'))
|
|
66
|
+
continue;
|
|
67
|
+
const dest = path.join(staging, rel);
|
|
68
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
69
|
+
fs.writeFileSync(dest, entry.data);
|
|
70
|
+
written += entry.data.length;
|
|
71
|
+
}
|
|
72
|
+
fs.rmSync(storeRef, { recursive: true, force: true });
|
|
73
|
+
fs.renameSync(staging, storeRef);
|
|
74
|
+
}
|
|
75
|
+
catch (e) {
|
|
76
|
+
fs.rmSync(staging, { recursive: true, force: true });
|
|
77
|
+
for (const t of targets)
|
|
78
|
+
releaseClaim(t.path);
|
|
79
|
+
return { ok: false, code: 'upstream', message: `store write failed: ${e instanceof Error ? e.message : e}`, skipped };
|
|
80
|
+
}
|
|
81
|
+
const contentHash = hashTree(storeRef).hash;
|
|
82
|
+
// 3. Materialize one copy per accepted host. A copy that fails is rolled back to nothing rather
|
|
83
|
+
// than left half-written, and reported instead of hidden.
|
|
84
|
+
const copies = [];
|
|
85
|
+
for (const item of targets) {
|
|
86
|
+
try {
|
|
87
|
+
fs.mkdirSync(path.dirname(item.path), { recursive: true });
|
|
88
|
+
fs.cpSync(storeRef, item.path, { recursive: true });
|
|
89
|
+
copies.push(item);
|
|
90
|
+
}
|
|
91
|
+
catch (e) {
|
|
92
|
+
fs.rmSync(item.path, { recursive: true, force: true });
|
|
93
|
+
releaseClaim(item.path);
|
|
94
|
+
skipped.push({ host: item.host, code: 'upstream', message: e instanceof Error ? e.message : String(e) });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (copies.length === 0) {
|
|
98
|
+
return { ok: false, code: 'no_target', message: 'no host copy could be written', skipped };
|
|
99
|
+
}
|
|
100
|
+
// 4. Intent last: a manifest entry only exists for a skill that is fully on disk. Copies already
|
|
101
|
+
// recorded for this artifact (another host or scope from an earlier install) are preserved.
|
|
102
|
+
const manifest = readManifest();
|
|
103
|
+
const now = new Date().toISOString();
|
|
104
|
+
const previous = manifest.artifacts.find((a) => a.id === artifactId);
|
|
105
|
+
const kept = (previous?.copies ?? []).filter((c) => !copies.some((item) => item.path === c.path));
|
|
106
|
+
manifest.artifacts = [
|
|
107
|
+
...manifest.artifacts.filter((a) => a.id !== artifactId),
|
|
108
|
+
{
|
|
109
|
+
id: artifactId,
|
|
110
|
+
canonicalKey: req.canonicalKey,
|
|
111
|
+
kind: 'skill',
|
|
112
|
+
name,
|
|
113
|
+
sourceId: req.sourceId,
|
|
114
|
+
ref: req.canonicalKey.slice(req.sourceId.length + 1),
|
|
115
|
+
pin: req.pin,
|
|
116
|
+
contentHash,
|
|
117
|
+
storeRef,
|
|
118
|
+
listedOn: req.listedOn ?? [req.sourceId],
|
|
119
|
+
trust: req.trust,
|
|
120
|
+
copies: [
|
|
121
|
+
...kept,
|
|
122
|
+
...copies.map((item) => ({
|
|
123
|
+
scope: req.scope,
|
|
124
|
+
host: item.host,
|
|
125
|
+
rootId: 'default',
|
|
126
|
+
path: item.path,
|
|
127
|
+
contentHash,
|
|
128
|
+
placement: 'materialized',
|
|
129
|
+
bytes: written,
|
|
130
|
+
installedAt: now,
|
|
131
|
+
})),
|
|
132
|
+
],
|
|
133
|
+
},
|
|
134
|
+
];
|
|
135
|
+
writeManifest(manifest);
|
|
136
|
+
return {
|
|
137
|
+
ok: true,
|
|
138
|
+
artifactId,
|
|
139
|
+
name,
|
|
140
|
+
storeRef,
|
|
141
|
+
files: req.files.length,
|
|
142
|
+
bytes: written,
|
|
143
|
+
copies: copies.map((item) => ({ host: item.host, path: item.path })),
|
|
144
|
+
skipped,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
@@ -18,6 +18,10 @@ export interface OpenCodeAgentView {
|
|
|
18
18
|
source: 'builtin' | 'file' | 'config' | 'service';
|
|
19
19
|
/** Bound model (router-prefixed values stripped); undefined = inherit default model. */
|
|
20
20
|
model?: string;
|
|
21
|
+
/** The agent's own description (file frontmatter / config node / live
|
|
22
|
+
* roster). Feeds the console's smart-assign role context — absent means
|
|
23
|
+
* unknown, never invented here. */
|
|
24
|
+
description?: string;
|
|
21
25
|
}
|
|
22
26
|
export interface ListAgentsOverrides {
|
|
23
27
|
/** opencode.json(c) path under test (default: resolved user config). */
|
|
@@ -32,6 +36,7 @@ interface RawServiceAgent {
|
|
|
32
36
|
name?: string;
|
|
33
37
|
mode?: string;
|
|
34
38
|
hidden?: boolean;
|
|
39
|
+
description?: string;
|
|
35
40
|
model?: {
|
|
36
41
|
providerID?: string;
|
|
37
42
|
id?: string;
|
|
@@ -47,11 +47,13 @@ function mergeConfigNode(byName, node) {
|
|
|
47
47
|
const prev = byName.get(name);
|
|
48
48
|
const mode = VALID_MODES.has(d.mode) ? d.mode : prev?.mode ?? 'all';
|
|
49
49
|
const configModel = typeof d.model === 'string' && d.model ? d.model : undefined;
|
|
50
|
+
const configDescription = typeof d.description === 'string' && d.description.trim() ? d.description.trim() : undefined;
|
|
50
51
|
byName.set(name, {
|
|
51
52
|
name,
|
|
52
53
|
mode,
|
|
53
54
|
source: 'config',
|
|
54
55
|
model: configModel !== undefined ? stripRouterPrefix(configModel) ?? configModel : prev?.model,
|
|
56
|
+
description: configDescription ?? prev?.description,
|
|
55
57
|
});
|
|
56
58
|
}
|
|
57
59
|
}
|
|
@@ -89,6 +91,7 @@ export function listOpenCodeAgents(overrides = {}) {
|
|
|
89
91
|
mode,
|
|
90
92
|
source: 'file',
|
|
91
93
|
model: fileModel !== undefined ? stripRouterPrefix(fileModel) ?? fileModel : prev?.model,
|
|
94
|
+
description: typeof fm.description === 'string' && fm.description.trim() ? fm.description.trim() : prev?.description,
|
|
92
95
|
});
|
|
93
96
|
}
|
|
94
97
|
}
|
|
@@ -125,7 +128,13 @@ export function mapServiceAgents(raw) {
|
|
|
125
128
|
}
|
|
126
129
|
if (model)
|
|
127
130
|
model = stripRouterPrefix(model) ?? model;
|
|
128
|
-
out.push({
|
|
131
|
+
out.push({
|
|
132
|
+
name: a.id,
|
|
133
|
+
mode,
|
|
134
|
+
source: 'service',
|
|
135
|
+
model,
|
|
136
|
+
description: typeof a.description === 'string' && a.description.trim() ? a.description.trim() : undefined,
|
|
137
|
+
});
|
|
129
138
|
}
|
|
130
139
|
return out.sort((a, b) => a.name.localeCompare(b.name));
|
|
131
140
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import os from 'node:os';
|
|
3
3
|
import path from 'node:path';
|
|
4
|
+
import { renameWithRetry } from '../../utils/atomic-write.js';
|
|
4
5
|
/**
|
|
5
6
|
* Per-source disk cache under ~/.cache/omnicell-router/ (XDG_CACHE_HOME aware).
|
|
6
7
|
* Reads return even-stale payloads so callers can degrade gracefully offline.
|
|
@@ -29,7 +30,7 @@ export function writeCacheFile(name, data) {
|
|
|
29
30
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
30
31
|
const tmp = `${file}.tmp.${process.pid}.${Date.now()}`;
|
|
31
32
|
fs.writeFileSync(tmp, JSON.stringify({ fetchedAt: Date.now(), data }), 'utf8');
|
|
32
|
-
|
|
33
|
+
renameWithRetry(tmp, file);
|
|
33
34
|
}
|
|
34
35
|
catch {
|
|
35
36
|
// best effort
|
|
@@ -3,6 +3,7 @@ import os from 'node:os';
|
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import crypto from 'node:crypto';
|
|
5
5
|
import { proxiedFetch } from '../../utils/proxy.js';
|
|
6
|
+
import { renameWithRetry } from '../../utils/atomic-write.js';
|
|
6
7
|
import { CATALOG_USER_AGENT } from './constants.js';
|
|
7
8
|
/**
|
|
8
9
|
* Local disk cache + HTTP proxy for remote catalog logos (models.dev, OpenRouter).
|
|
@@ -61,7 +62,7 @@ export function writeLogoCache(url, state) {
|
|
|
61
62
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
62
63
|
const tmp = `${file}.tmp.${process.pid}.${Date.now()}`;
|
|
63
64
|
fs.writeFileSync(tmp, JSON.stringify(state), 'utf8');
|
|
64
|
-
|
|
65
|
+
renameWithRetry(tmp, file);
|
|
65
66
|
}
|
|
66
67
|
catch {
|
|
67
68
|
// best effort
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { getEnvPaths } from '../../config/env.js';
|
|
4
|
+
import { renameWithRetry } from '../../utils/atomic-write.js';
|
|
4
5
|
export function getOcrStorePath() {
|
|
5
6
|
const override = process.env.OCR_CATALOG_PATH;
|
|
6
7
|
if (override)
|
|
@@ -30,7 +31,7 @@ export function writeOcrStore(sig, providers) {
|
|
|
30
31
|
const store = { version: 4, sig, builtAt: Date.now(), providers };
|
|
31
32
|
const tmp = `${file}.tmp.${process.pid}.${Date.now()}`;
|
|
32
33
|
fs.writeFileSync(tmp, JSON.stringify(store), 'utf8');
|
|
33
|
-
|
|
34
|
+
renameWithRetry(tmp, file);
|
|
34
35
|
}
|
|
35
36
|
catch {
|
|
36
37
|
// best effort — the in-memory copy still serves; next rebuild retries
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { getOcrStorePath } from './ocr-store.js';
|
|
4
|
+
import { renameWithRetry } from '../../utils/atomic-write.js';
|
|
4
5
|
export const overrideKey = (providerId, modelId) => `${providerId}||${modelId}`;
|
|
5
6
|
export function getOverridesStorePath() {
|
|
6
7
|
return path.join(path.dirname(getOcrStorePath()), 'overrides.json');
|
|
@@ -28,7 +29,7 @@ function persist(store) {
|
|
|
28
29
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
29
30
|
const tmp = `${file}.tmp.${process.pid}.${Date.now()}`;
|
|
30
31
|
fs.writeFileSync(tmp, JSON.stringify(store), 'utf8');
|
|
31
|
-
|
|
32
|
+
renameWithRetry(tmp, file);
|
|
32
33
|
}
|
|
33
34
|
export function upsertOverride(providerId, modelId, entry) {
|
|
34
35
|
try {
|
|
@@ -3,6 +3,7 @@ import path from 'node:path';
|
|
|
3
3
|
import { PROVIDER_DEFS_IMPORT_VERSION, importProviderDefsAtBoot } from '../provider-defs.js';
|
|
4
4
|
import { credentialStoreStateFile, ocrCredentialsFile, resolveDefaultPaths, } from './paths.js';
|
|
5
5
|
import { OcrStore } from './ocr-store.js';
|
|
6
|
+
import { renameWithRetry } from '../../utils/atomic-write.js';
|
|
6
7
|
import { isStoreKind, StoreUnreadableError, EmptyCredentialTableError, keyHashPrefix, } from './types.js';
|
|
7
8
|
import { authSnapshot } from './auth-snapshot.js';
|
|
8
9
|
const VERSION_REGEX = /v?(\d+)\.(\d+)(?:\.\d+)?/;
|
|
@@ -43,36 +44,18 @@ export function atomicWriteWithRetry(file, content) {
|
|
|
43
44
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
44
45
|
const tmp = `${file}.tmp.${process.pid}.${Date.now()}`;
|
|
45
46
|
fs.writeFileSync(tmp, content, 'utf8');
|
|
46
|
-
let lastError;
|
|
47
|
-
for (let attempt = 0; attempt < 5; attempt++) {
|
|
48
|
-
try {
|
|
49
|
-
fs.renameSync(tmp, file);
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
catch (e) {
|
|
53
|
-
lastError = e; // EPERM/EBUSY on Windows usually clears within a few ms
|
|
54
|
-
if (attempt < 4)
|
|
55
|
-
blockingSleep((attempt + 1) * 10);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
try {
|
|
59
|
-
fs.unlinkSync(tmp);
|
|
60
|
-
}
|
|
61
|
-
catch {
|
|
62
|
-
// best-effort tmp cleanup
|
|
63
|
-
}
|
|
64
|
-
throw lastError;
|
|
65
|
-
}
|
|
66
|
-
/** Sync sleep on the boot write path — Atomics.wait, spin fallback. */
|
|
67
|
-
function blockingSleep(ms) {
|
|
68
47
|
try {
|
|
69
|
-
|
|
48
|
+
renameWithRetry(tmp, file);
|
|
49
|
+
return;
|
|
70
50
|
}
|
|
71
|
-
catch {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
51
|
+
catch (e) {
|
|
52
|
+
try {
|
|
53
|
+
fs.unlinkSync(tmp);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// best-effort tmp cleanup
|
|
75
57
|
}
|
|
58
|
+
throw e;
|
|
76
59
|
}
|
|
77
60
|
}
|
|
78
61
|
/**
|
|
@@ -3,6 +3,7 @@ import fs from 'node:fs';
|
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { maskSecret } from '../user-config.js';
|
|
5
5
|
import { ocrCredentialsFile, resolveDefaultPaths } from './paths.js';
|
|
6
|
+
import { renameWithRetry } from '../../utils/atomic-write.js';
|
|
6
7
|
import { CredentialNotFoundError, CapabilityUnsupportedError, RESERVED_PROVIDER_IDS, StoreUnreadableError, keyHashPrefix, sha256KeyHash, } from './types.js';
|
|
7
8
|
const OCR_CAPABILITIES = {
|
|
8
9
|
multiCredential: true,
|
|
@@ -255,7 +256,7 @@ export class OcrStore {
|
|
|
255
256
|
catch {
|
|
256
257
|
// best effort
|
|
257
258
|
}
|
|
258
|
-
|
|
259
|
+
renameWithRetry(tmpPath, this.file);
|
|
259
260
|
// Fire-and-forget notification AFTER the durable write — the sync engine
|
|
260
261
|
// debounces a push from here. The hook must never break the mutation.
|
|
261
262
|
try {
|