@kirkchinese/harness-forge 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +42 -2
- package/assets/icon.svg +12 -0
- package/cordis.patch.yml +28 -0
- package/lib/agents/presets.d.ts +91 -0
- package/lib/agents/presets.js +282 -0
- package/lib/agents/profiles.d.ts +58 -0
- package/lib/agents/profiles.js +278 -0
- package/lib/agents/row.d.ts +17 -0
- package/lib/agents/row.js +146 -0
- package/lib/client/styles.d.ts +8 -0
- package/lib/client/styles.js +89 -0
- package/lib/client/typert.d.ts +14 -0
- package/lib/client/typert.js +110 -0
- package/lib/client.js +22500 -0
- package/lib/commands/definition.d.ts +40 -0
- package/lib/commands/definition.js +113 -0
- package/lib/commands/output-styles.d.ts +42 -0
- package/lib/commands/output-styles.js +152 -0
- package/lib/commands/row.d.ts +9 -0
- package/lib/commands/row.js +157 -0
- package/lib/context/instructions.d.ts +32 -0
- package/lib/context/instructions.js +88 -0
- package/lib/context/row.d.ts +16 -0
- package/lib/context/row.js +17 -0
- package/lib/dsh/commands.d.ts +30 -0
- package/lib/dsh/commands.js +61 -0
- package/lib/dsh/host.d.ts +107 -0
- package/lib/dsh/host.js +214 -0
- package/lib/dsh/model-route.d.ts +21 -0
- package/lib/dsh/model-route.js +37 -0
- package/lib/dsh/session-records.d.ts +78 -0
- package/lib/dsh/session-records.js +36 -0
- package/lib/dsh/subagents.d.ts +46 -0
- package/lib/dsh/subagents.js +14 -0
- package/lib/forge/contract.d.ts +231 -0
- package/lib/forge/contract.js +43 -0
- package/lib/forge/ledger.d.ts +143 -0
- package/lib/forge/ledger.js +733 -0
- package/lib/forge/live-mounts.d.ts +45 -0
- package/lib/forge/live-mounts.js +78 -0
- package/lib/forge/marketplace-install.d.ts +3 -0
- package/lib/forge/marketplace-install.js +64 -0
- package/lib/forge/npm.d.ts +31 -0
- package/lib/forge/npm.js +60 -0
- package/lib/forge/projects.d.ts +94 -0
- package/lib/forge/projects.js +336 -0
- package/lib/forge/receipts.d.ts +5 -0
- package/lib/forge/receipts.js +31 -0
- package/lib/forge/row.d.ts +32 -0
- package/lib/forge/row.js +59 -0
- package/lib/forge/service.d.ts +105 -0
- package/lib/forge/service.js +274 -0
- package/lib/forge/settings.d.ts +41 -0
- package/lib/forge/settings.js +115 -0
- package/lib/forge/state.d.ts +69 -0
- package/lib/forge/state.js +150 -0
- package/lib/forge/trust.d.ts +48 -0
- package/lib/forge/trust.js +107 -0
- package/lib/forge/turns.d.ts +13 -0
- package/lib/forge/turns.js +56 -0
- package/lib/hooks/broker/engine.d.ts +112 -0
- package/lib/hooks/broker/engine.js +728 -0
- package/lib/hooks/broker/helpers.d.ts +10 -0
- package/lib/hooks/broker/helpers.js +22 -0
- package/lib/hooks/broker/lifecycle.d.ts +65 -0
- package/lib/hooks/broker/lifecycle.js +342 -0
- package/lib/hooks/broker/session-events.d.ts +22 -0
- package/lib/hooks/broker/session-events.js +196 -0
- package/lib/hooks/broker/tools.d.ts +52 -0
- package/lib/hooks/broker/tools.js +424 -0
- package/lib/hooks/broker/types.d.ts +262 -0
- package/lib/hooks/broker/types.js +1 -0
- package/lib/hooks/broker.d.ts +80 -0
- package/lib/hooks/broker.js +95 -0
- package/lib/hooks/codex-user.d.ts +5 -0
- package/lib/hooks/codex-user.js +109 -0
- package/lib/hooks/condition.d.ts +11 -0
- package/lib/hooks/condition.js +181 -0
- package/lib/hooks/config.d.ts +85 -0
- package/lib/hooks/config.js +150 -0
- package/lib/hooks/matcher.d.ts +20 -0
- package/lib/hooks/matcher.js +39 -0
- package/lib/hooks/notice.d.ts +17 -0
- package/lib/hooks/notice.js +39 -0
- package/lib/hooks/output.d.ts +56 -0
- package/lib/hooks/output.js +214 -0
- package/lib/hooks/permissions.d.ts +14 -0
- package/lib/hooks/permissions.js +109 -0
- package/lib/hooks/process.d.ts +47 -0
- package/lib/hooks/process.js +134 -0
- package/lib/hooks/row.d.ts +17 -0
- package/lib/hooks/row.js +94 -0
- package/lib/hooks/runner.d.ts +85 -0
- package/lib/hooks/runner.js +293 -0
- package/lib/hooks/shell-commands.d.ts +19 -0
- package/lib/hooks/shell-commands.js +177 -0
- package/lib/hooks/shell.d.ts +43 -0
- package/lib/hooks/shell.js +126 -0
- package/lib/hooks/transcript.d.ts +28 -0
- package/lib/hooks/transcript.js +141 -0
- package/lib/hooks/vocabulary.d.ts +57 -0
- package/lib/hooks/vocabulary.js +364 -0
- package/lib/index.d.ts +31 -0
- package/lib/index.js +287 -0
- package/lib/mcp/auth.d.ts +35 -0
- package/lib/mcp/auth.js +178 -0
- package/lib/mcp/catalog.d.ts +36 -0
- package/lib/mcp/catalog.js +61 -0
- package/lib/mcp/mount.d.ts +75 -0
- package/lib/mcp/mount.js +217 -0
- package/lib/mcp/remote.d.ts +45 -0
- package/lib/mcp/remote.js +143 -0
- package/lib/mcp/row.d.ts +9 -0
- package/lib/mcp/row.js +131 -0
- package/lib/mods/bundler.d.ts +38 -0
- package/lib/mods/bundler.js +157 -0
- package/lib/mods/chain.d.ts +103 -0
- package/lib/mods/chain.js +436 -0
- package/lib/mods/claude.d.ts +27 -0
- package/lib/mods/claude.js +56 -0
- package/lib/mods/engine.d.ts +160 -0
- package/lib/mods/engine.js +701 -0
- package/lib/mods/expect.d.ts +8 -0
- package/lib/mods/expect.js +248 -0
- package/lib/mods/host.d.ts +105 -0
- package/lib/mods/host.js +462 -0
- package/lib/mods/loader.d.ts +53 -0
- package/lib/mods/loader.js +227 -0
- package/lib/mods/ops.d.ts +13 -0
- package/lib/mods/ops.js +117 -0
- package/lib/mods/row.d.ts +9 -0
- package/lib/mods/row.js +77 -0
- package/lib/mods/runtime.d.ts +60 -0
- package/lib/mods/runtime.js +133 -0
- package/lib/mods/service/commands.d.ts +17 -0
- package/lib/mods/service/commands.js +121 -0
- package/lib/mods/service/drawings.d.ts +36 -0
- package/lib/mods/service/drawings.js +64 -0
- package/lib/mods/service/sessions.d.ts +63 -0
- package/lib/mods/service/sessions.js +345 -0
- package/lib/mods/service/tools.d.ts +32 -0
- package/lib/mods/service/tools.js +253 -0
- package/lib/mods/service/turns.d.ts +32 -0
- package/lib/mods/service/turns.js +161 -0
- package/lib/mods/service/types.d.ts +151 -0
- package/lib/mods/service/types.js +1 -0
- package/lib/mods/service.d.ts +72 -0
- package/lib/mods/service.js +104 -0
- package/lib/mods/surfaces.d.ts +93 -0
- package/lib/mods/surfaces.js +309 -0
- package/lib/mods/values.d.ts +30 -0
- package/lib/mods/values.js +95 -0
- package/lib/opencode/bun.d.ts +40 -0
- package/lib/opencode/bun.js +156 -0
- package/lib/opencode/convert.d.ts +53 -0
- package/lib/opencode/convert.js +255 -0
- package/lib/opencode/row.d.ts +9 -0
- package/lib/opencode/row.js +86 -0
- package/lib/opencode/runtime/core.d.ts +74 -0
- package/lib/opencode/runtime/core.js +249 -0
- package/lib/opencode/runtime/events.d.ts +80 -0
- package/lib/opencode/runtime/events.js +281 -0
- package/lib/opencode/runtime/mount.d.ts +27 -0
- package/lib/opencode/runtime/mount.js +283 -0
- package/lib/opencode/runtime/server.d.ts +27 -0
- package/lib/opencode/runtime/server.js +353 -0
- package/lib/opencode/runtime/types.d.ts +109 -0
- package/lib/opencode/runtime/types.js +1 -0
- package/lib/opencode/runtime.d.ts +55 -0
- package/lib/opencode/runtime.js +79 -0
- package/lib/porting/port-gap.d.ts +30 -0
- package/lib/porting/port-gap.js +122 -0
- package/lib/porting/port-sessions.d.ts +41 -0
- package/lib/porting/port-sessions.js +98 -0
- package/lib/porting/prompt.d.ts +23 -0
- package/lib/porting/prompt.js +49 -0
- package/lib/porting/row.d.ts +16 -0
- package/lib/porting/row.js +25 -0
- package/lib/porting/verify-port.d.ts +53 -0
- package/lib/porting/verify-port.js +104 -0
- package/lib/presets/fs-search.d.ts +25 -0
- package/lib/presets/fs-search.js +17 -0
- package/lib/presets/tool-filter.d.ts +35 -0
- package/lib/presets/tool-filter.js +91 -0
- package/lib/sessions/claude.d.ts +29 -0
- package/lib/sessions/claude.js +83 -0
- package/lib/sessions/codex.d.ts +30 -0
- package/lib/sessions/codex.js +303 -0
- package/lib/sessions/opencode.d.ts +32 -0
- package/lib/sessions/opencode.js +320 -0
- package/lib/sessions/row.d.ts +14 -0
- package/lib/sessions/row.js +35 -0
- package/lib/shared/invocation.d.ts +169 -0
- package/lib/shared/invocation.js +325 -0
- package/lib/shared/render.d.ts +72 -0
- package/lib/shared/render.js +588 -0
- package/lib/shared/util.d.ts +21 -0
- package/lib/shared/util.js +32 -0
- package/lib/skills/bundled.d.ts +3 -0
- package/lib/skills/bundled.js +42 -0
- package/lib/skills/provider.d.ts +72 -0
- package/lib/skills/provider.js +340 -0
- package/lib/skills/row.d.ts +22 -0
- package/lib/skills/row.js +204 -0
- package/lib/workbench/adapter-coverage.d.ts +47 -0
- package/lib/workbench/adapter-coverage.js +102 -0
- package/lib/workbench/model-tools.d.ts +10 -0
- package/lib/workbench/model-tools.js +122 -0
- package/lib/workbench/overview.d.ts +103 -0
- package/lib/workbench/overview.js +159 -0
- package/lib/workbench/remote.d.ts +118 -0
- package/lib/workbench/remote.js +422 -0
- package/lib/workbench/row.d.ts +15 -0
- package/lib/workbench/row.js +16 -0
- package/lib/workbench/settings.d.ts +15 -0
- package/lib/workbench/settings.js +56 -0
- package/lib/workbench/workbench.d.ts +100 -0
- package/lib/workbench/workbench.js +260 -0
- package/locale/agents/en.json +6 -0
- package/locale/agents/zh.json +6 -0
- package/locale/commands/en.json +6 -0
- package/locale/commands/zh.json +6 -0
- package/locale/context/en.json +6 -0
- package/locale/context/zh.json +6 -0
- package/locale/en.json +6 -0
- package/locale/hooks/en.json +6 -0
- package/locale/hooks/zh.json +6 -0
- package/locale/mcp/en.json +6 -0
- package/locale/mcp/zh.json +6 -0
- package/locale/mods/en.json +6 -0
- package/locale/mods/zh.json +6 -0
- package/locale/opencode/en.json +6 -0
- package/locale/opencode/zh.json +6 -0
- package/locale/porter/en.json +6 -0
- package/locale/porter/zh.json +6 -0
- package/locale/sessions/en.json +6 -0
- package/locale/sessions/zh.json +6 -0
- package/locale/skills/en.json +6 -0
- package/locale/skills/zh.json +6 -0
- package/locale/workbench/en.json +6 -0
- package/locale/workbench/zh.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +246 -3
- package/runtime/opencode-host.mjs +270 -0
- package/skills/harness-forge/SKILL.md +47 -0
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared service every Harness Forge row reaches as `harnessForge`: the
|
|
3
|
+
* ledger and its mounters, runtime evidence, the imported settings and the
|
|
4
|
+
* turn-scoped effects of invoked skills and commands (both read by several
|
|
5
|
+
* rows), per-session project assets, and the optional APIs rows offer each
|
|
6
|
+
* other. It holds no DSH listener of its own; the `harness-forge` row does.
|
|
7
|
+
* @module @kirkchinese/harness-forge
|
|
8
|
+
*/
|
|
9
|
+
import { join, resolve } from 'node:path';
|
|
10
|
+
import { detectSource, dialectOf, HarnessError, readMarketplace, } from '@kirkchinese/harness-forge-core';
|
|
11
|
+
import { createForeignShell } from '../hooks/shell.js';
|
|
12
|
+
import { Invocations } from '../shared/invocation.js';
|
|
13
|
+
import { fetchGitSource } from './marketplace-install.js';
|
|
14
|
+
import { sessionDirectory } from '../dsh/host.js';
|
|
15
|
+
import { loggerOf, messageOf } from '../shared/util.js';
|
|
16
|
+
import { Ledger } from './ledger.js';
|
|
17
|
+
import { ProjectAssets } from './projects.js';
|
|
18
|
+
import { WorkspaceTrust } from './trust.js';
|
|
19
|
+
import { ImportedSettings } from './settings.js';
|
|
20
|
+
/** The service name the `harness-forge` row provides. */
|
|
21
|
+
export const FORGE_SERVICE = 'harnessForge';
|
|
22
|
+
const MAX_FAULTS = 50;
|
|
23
|
+
/** The shared service. */
|
|
24
|
+
export class HarnessForge {
|
|
25
|
+
ctx;
|
|
26
|
+
statePath;
|
|
27
|
+
dataRoot;
|
|
28
|
+
/** The shell imported hooks, commands and mods run in. */
|
|
29
|
+
shell;
|
|
30
|
+
settings;
|
|
31
|
+
/** Turn-scoped effects of invoked skills and commands (`allowed-tools`, `model`, `context: fork`, ...). */
|
|
32
|
+
invocations;
|
|
33
|
+
/** Claude Code and Codex project assets per session. */
|
|
34
|
+
projects;
|
|
35
|
+
ledger;
|
|
36
|
+
apis = new Map();
|
|
37
|
+
watchers = new Map();
|
|
38
|
+
recentFaults = [];
|
|
39
|
+
/**
|
|
40
|
+
* @param ctx - the `harness-forge` row's context.
|
|
41
|
+
* @param statePath - the ledger file.
|
|
42
|
+
* @param dataRoot - profile-local writable directory (shims, OAuth state, mod store).
|
|
43
|
+
*/
|
|
44
|
+
constructor(ctx, statePath, dataRoot) {
|
|
45
|
+
this.ctx = ctx;
|
|
46
|
+
this.statePath = statePath;
|
|
47
|
+
this.dataRoot = dataRoot;
|
|
48
|
+
this.shell = createForeignShell(process.env, dataRoot);
|
|
49
|
+
this.invocations = new Invocations(ctx, name => this.lookup('agents')?.presetOf(name), (agent, signal) => sessionDirectory(ctx, agent, signal));
|
|
50
|
+
this.settings = new ImportedSettings(sessionId => this.invocations.rules(sessionId));
|
|
51
|
+
this.ledger = new Ledger(ctx, statePath, dataRoot, error => this.fault('ledger', error));
|
|
52
|
+
this.projects = new ProjectAssets(this.ledger, dataRoot, new WorkspaceTrust(statePath), error => this.fault('projects', error));
|
|
53
|
+
this.ledger.registerMounter('settings', {
|
|
54
|
+
mount: async ({ component, scope }) => {
|
|
55
|
+
if (component.kind !== 'settings')
|
|
56
|
+
throw new HarnessError('component-failed', `action expects settings: ${component.id}`);
|
|
57
|
+
const dispose = this.settings.apply(component, scope.sessionId);
|
|
58
|
+
return {
|
|
59
|
+
outcome: { componentId: component.id, status: 'mounted', level: 'L1b', evidence: 'source', ...(component.description !== undefined ? { message: component.description } : {}) },
|
|
60
|
+
disposer: async () => { dispose(); },
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/** A session's working directory, as hooks and commands see it. */
|
|
66
|
+
directory(agent, signal) {
|
|
67
|
+
return sessionDirectory(this.ctx, agent, signal);
|
|
68
|
+
}
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
// Rows
|
|
71
|
+
// ---------------------------------------------------------------------------
|
|
72
|
+
/** Register the mounter of one kind of component; see {@link Ledger.registerMounter}. */
|
|
73
|
+
registerMounter(kind, mounter) {
|
|
74
|
+
return this.ledger.registerMounter(kind, mounter);
|
|
75
|
+
}
|
|
76
|
+
/** Offer a row's API to the other rows while the row runs. */
|
|
77
|
+
provide(name, api) {
|
|
78
|
+
if (this.apis.has(name))
|
|
79
|
+
throw new Error(`the ${name} row's API is already provided`);
|
|
80
|
+
this.apis.set(name, api);
|
|
81
|
+
for (const watcher of this.watchers.get(name) ?? [])
|
|
82
|
+
watcher.release = this.use(name, watcher.use, api);
|
|
83
|
+
return () => {
|
|
84
|
+
if (this.apis.get(name) !== api)
|
|
85
|
+
return;
|
|
86
|
+
this.apis.delete(name);
|
|
87
|
+
for (const watcher of this.watchers.get(name) ?? []) {
|
|
88
|
+
const release = watcher.release;
|
|
89
|
+
watcher.release = undefined;
|
|
90
|
+
this.release(name, release);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/** A row's API, or undefined while that row is off. */
|
|
95
|
+
lookup(name) {
|
|
96
|
+
return this.apis.get(name);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Use a row's API whenever it is there: `use` runs now if the row runs, and
|
|
100
|
+
* again each time it starts; what `use` returns is released when the row
|
|
101
|
+
* stops or the returned disposer runs.
|
|
102
|
+
*/
|
|
103
|
+
watch(name, use) {
|
|
104
|
+
const watcher = { use: use };
|
|
105
|
+
const watchers = this.watchers.get(name) ?? new Set();
|
|
106
|
+
this.watchers.set(name, watchers);
|
|
107
|
+
watchers.add(watcher);
|
|
108
|
+
const api = this.lookup(name);
|
|
109
|
+
if (api !== undefined)
|
|
110
|
+
watcher.release = this.use(name, use, api);
|
|
111
|
+
return () => {
|
|
112
|
+
watchers.delete(watcher);
|
|
113
|
+
this.release(name, watcher.release);
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
use(name, use, api) {
|
|
117
|
+
try {
|
|
118
|
+
return use(api);
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
this.fault(name, error);
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
release(name, release) {
|
|
126
|
+
try {
|
|
127
|
+
release?.();
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
this.fault(name, error);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/** Record a row's failure (a listener that threw, a part that did not start) for the workbench. */
|
|
134
|
+
fault(row, error) {
|
|
135
|
+
this.recentFaults.push({ row, at: new Date().toISOString(), message: messageOf(error) });
|
|
136
|
+
if (this.recentFaults.length > MAX_FAULTS)
|
|
137
|
+
this.recentFaults.splice(0, this.recentFaults.length - MAX_FAULTS);
|
|
138
|
+
}
|
|
139
|
+
/** The latest row failures, oldest first. */
|
|
140
|
+
faults() {
|
|
141
|
+
return [...this.recentFaults];
|
|
142
|
+
}
|
|
143
|
+
/** Runtime evidence for one component. */
|
|
144
|
+
observe(componentId, signal, ok, detail) {
|
|
145
|
+
this.ledger.observe(componentId, { signal, ok, at: new Date().toISOString(), detail });
|
|
146
|
+
}
|
|
147
|
+
/** Withdraw a condition that no longer holds, such as a sign-in that completed. */
|
|
148
|
+
clearObservation(componentId, signal) {
|
|
149
|
+
this.ledger.observe(componentId, { clear: signal });
|
|
150
|
+
}
|
|
151
|
+
// ---------------------------------------------------------------------------
|
|
152
|
+
// Ledger
|
|
153
|
+
// ---------------------------------------------------------------------------
|
|
154
|
+
/**
|
|
155
|
+
* Restore the durable ledger, then install `preload` roots. An entry may
|
|
156
|
+
* name the dialect to read a multi-harness root as: `codex:<path>`. It
|
|
157
|
+
* takes the ledger's queue at once, so no install or removal can run
|
|
158
|
+
* before the ledger is restored (and write a ledger without its records).
|
|
159
|
+
*/
|
|
160
|
+
start(preload = []) {
|
|
161
|
+
return this.ledger.exclusive(async () => {
|
|
162
|
+
await this.ledger.restoreNow();
|
|
163
|
+
for (const entry of preload) {
|
|
164
|
+
const prefix = /^([a-z][a-z0-9-]*):(.+)$/u.exec(entry);
|
|
165
|
+
const dialect = prefix !== null && dialectOf(prefix[1]) !== undefined ? prefix : undefined;
|
|
166
|
+
const root = dialect?.[2] ?? entry;
|
|
167
|
+
try {
|
|
168
|
+
await this.ledger.applyNow(this.ledger.planNow(root, dialect?.[1] ?? detectSource(resolve(root))));
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
loggerOf(this.ctx)?.warn?.(`harness-forge: preload failed for ${root}: ${messageOf(error)}`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
/** Settles once every queued install, restore and re-apply (a row that just started mounting) is done. */
|
|
177
|
+
async ready() {
|
|
178
|
+
let queue;
|
|
179
|
+
do {
|
|
180
|
+
queue = this.ledger.queue;
|
|
181
|
+
await queue;
|
|
182
|
+
} while (queue !== this.ledger.queue);
|
|
183
|
+
}
|
|
184
|
+
/** Build a pure plan for one plugin. */
|
|
185
|
+
plan(root, source) {
|
|
186
|
+
return this.ledger.planNow(root, source);
|
|
187
|
+
}
|
|
188
|
+
/** Apply a plan previously returned by {@link plan}. */
|
|
189
|
+
applyById(planId) {
|
|
190
|
+
const plan = this.ledger.plans.get(planId);
|
|
191
|
+
if (plan === undefined) {
|
|
192
|
+
return Promise.reject(new HarnessError('plan-not-found', `no plan with id ${planId}`, { suggestion: 'Scan again and review the new plan.' }));
|
|
193
|
+
}
|
|
194
|
+
return this.apply(plan);
|
|
195
|
+
}
|
|
196
|
+
/** Apply one reviewed plan. Re-scans the root and refuses a changed source. */
|
|
197
|
+
apply(plan) {
|
|
198
|
+
return this.ledger.exclusive(() => this.ledger.applyNow(plan));
|
|
199
|
+
}
|
|
200
|
+
/** Plan and apply one plugin root. */
|
|
201
|
+
async install(root, source) {
|
|
202
|
+
const result = await this.ledger.exclusive(() => this.ledger.applyNow(this.ledger.planNow(root, source)));
|
|
203
|
+
return this.ledger.report(result);
|
|
204
|
+
}
|
|
205
|
+
/** Read a marketplace index without installing anything. */
|
|
206
|
+
scanMarketplace(path) {
|
|
207
|
+
return readMarketplace(path);
|
|
208
|
+
}
|
|
209
|
+
/** Install one marketplace entry from a local path or git source. */
|
|
210
|
+
async installMarketplace(path, name) {
|
|
211
|
+
const index = readMarketplace(path);
|
|
212
|
+
const entry = index.entries.find(candidate => candidate.name === name);
|
|
213
|
+
if (entry === undefined)
|
|
214
|
+
throw new HarnessError('marketplace-failed', `marketplace ${index.name} has no entry "${name}"`);
|
|
215
|
+
if (entry.source.kind === 'path' && entry.source.path !== undefined)
|
|
216
|
+
return this.install(entry.source.path, marketplaceDialect(index));
|
|
217
|
+
if (entry.source.kind === 'git') {
|
|
218
|
+
const root = await fetchGitSource(entry.source, join(this.dataRoot, 'marketplace-cache'));
|
|
219
|
+
return this.install(root, marketplaceDialect(index));
|
|
220
|
+
}
|
|
221
|
+
throw new HarnessError('unsupported-source', `entry "${name}" uses ${entry.source.kind}; supported marketplace sources are local path and git`);
|
|
222
|
+
}
|
|
223
|
+
/** Undo the actions of one receipt and remove its live registrations. */
|
|
224
|
+
undo(receiptId) {
|
|
225
|
+
return this.ledger.exclusive(() => this.ledger.undoNow(receiptId));
|
|
226
|
+
}
|
|
227
|
+
/** Switch one part of an installed plugin off or on again; it stays so across restarts. */
|
|
228
|
+
setPartEnabled(pluginId, componentId, enabled) {
|
|
229
|
+
return this.ledger.exclusive(() => this.ledger.setEnabledNow(pluginId, componentId, enabled));
|
|
230
|
+
}
|
|
231
|
+
/** Remove one installation and every registration it owns. */
|
|
232
|
+
uninstall(id) {
|
|
233
|
+
return this.ledger.exclusive(async () => {
|
|
234
|
+
if (!await this.ledger.teardown(id))
|
|
235
|
+
return false;
|
|
236
|
+
this.ledger.persist();
|
|
237
|
+
return true;
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
/** Scan one plugin without writing anything. */
|
|
241
|
+
scan(root, source) {
|
|
242
|
+
return this.ledger.scan(root, source);
|
|
243
|
+
}
|
|
244
|
+
/** List current installations in stable name order. */
|
|
245
|
+
list() {
|
|
246
|
+
return this.ledger.list();
|
|
247
|
+
}
|
|
248
|
+
/** Drop every runtime registration without rewriting the durable ledger. */
|
|
249
|
+
async dispose() {
|
|
250
|
+
const ledger = this.ledger;
|
|
251
|
+
if (ledger.disposed)
|
|
252
|
+
return;
|
|
253
|
+
ledger.disposed = true;
|
|
254
|
+
await ledger.queue.catch(() => undefined);
|
|
255
|
+
if (ledger.persistTimer !== undefined) {
|
|
256
|
+
clearTimeout(ledger.persistTimer);
|
|
257
|
+
ledger.persistTimer = undefined;
|
|
258
|
+
ledger.persist();
|
|
259
|
+
}
|
|
260
|
+
// A withdrawal that fails here is recorded as a fault; the others still go.
|
|
261
|
+
for (const id of [...ledger.live.keys()])
|
|
262
|
+
await ledger.teardown(id).catch(() => undefined);
|
|
263
|
+
await this.projects.dispose();
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
/** Infer the dialect of a marketplace index from its manifest location. */
|
|
267
|
+
export function marketplaceDialect(index) {
|
|
268
|
+
const normalized = index.manifestPath.replace(/\\/gu, '/');
|
|
269
|
+
if (normalized.includes('/.agents/plugins/'))
|
|
270
|
+
return 'codex';
|
|
271
|
+
if (normalized.includes('/.claude-plugin/'))
|
|
272
|
+
return 'claude-code';
|
|
273
|
+
return 'agent-plugins';
|
|
274
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { PermissionRules, SettingsComponent } from '@kirkchinese/harness-forge-core';
|
|
2
|
+
/** The session facts settings scoping reads. */
|
|
3
|
+
export interface SessionHeaderLike {
|
|
4
|
+
readonly id: string;
|
|
5
|
+
readonly parentSession?: string;
|
|
6
|
+
}
|
|
7
|
+
/** The settings in effect, process-wide and per session. */
|
|
8
|
+
export declare class ImportedSettings {
|
|
9
|
+
private readonly turnRules;
|
|
10
|
+
private readonly entries;
|
|
11
|
+
/**
|
|
12
|
+
* @param turnRules - the allow and deny rules of a session's turn (an invoked skill's `allowed-tools`).
|
|
13
|
+
*/
|
|
14
|
+
constructor(turnRules: (sessionId: string) => {
|
|
15
|
+
readonly allow: readonly string[];
|
|
16
|
+
readonly deny: readonly string[];
|
|
17
|
+
});
|
|
18
|
+
/** Apply one settings component, process-wide or for one session. Returns the withdrawal. */
|
|
19
|
+
apply(component: SettingsComponent, sessionId?: string): () => void;
|
|
20
|
+
/** The `outputStyle` the process-wide settings name, the latest winning. */
|
|
21
|
+
outputStyle(): string | undefined;
|
|
22
|
+
/** The settings in effect for a session: global ones, then the session's (project) ones. */
|
|
23
|
+
private settingsFor;
|
|
24
|
+
/**
|
|
25
|
+
* Claude settings as `$.settings.read` returns them: every imported
|
|
26
|
+
* settings file in effect merged (later files win, objects merged), or one
|
|
27
|
+
* source's (`user`, `project`, `local`; `policy` is managed settings,
|
|
28
|
+
* which DSH has none of).
|
|
29
|
+
*/
|
|
30
|
+
claudeSettings(session: SessionHeaderLike | undefined, source: string | undefined): Record<string, unknown>;
|
|
31
|
+
/** The environment imported settings declare, later (project) settings winning. */
|
|
32
|
+
env(session?: SessionHeaderLike): Record<string, string>;
|
|
33
|
+
/**
|
|
34
|
+
* The Claude-syntax permission rules in effect for a session; deny from any
|
|
35
|
+
* scope holds. An invoked skill's `allowed-tools` and `disallowed-tools`
|
|
36
|
+
* count for the rest of its turn.
|
|
37
|
+
*/
|
|
38
|
+
permissionRules(session?: SessionHeaderLike): PermissionRules;
|
|
39
|
+
/** `disableAllHooks` from the settings in effect, the most specific scope winning. */
|
|
40
|
+
hooksDisabled(session?: SessionHeaderLike): boolean;
|
|
41
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Imported harness settings (Claude Code's `settings.json`, Codex's config):
|
|
3
|
+
* their environment reaches every hook, MCP server and shell command, their
|
|
4
|
+
* permission rules gate tool calls, `disableAllHooks` silences the hooks, and
|
|
5
|
+
* `outputStyle` is the starting style. Several rows read them, so the shared
|
|
6
|
+
* service keeps them.
|
|
7
|
+
* @module @kirkchinese/harness-forge
|
|
8
|
+
*/
|
|
9
|
+
import { readFileSync } from 'node:fs';
|
|
10
|
+
/** The settings in effect, process-wide and per session. */
|
|
11
|
+
export class ImportedSettings {
|
|
12
|
+
turnRules;
|
|
13
|
+
entries = new Map();
|
|
14
|
+
/**
|
|
15
|
+
* @param turnRules - the allow and deny rules of a session's turn (an invoked skill's `allowed-tools`).
|
|
16
|
+
*/
|
|
17
|
+
constructor(turnRules) {
|
|
18
|
+
this.turnRules = turnRules;
|
|
19
|
+
}
|
|
20
|
+
/** Apply one settings component, process-wide or for one session. Returns the withdrawal. */
|
|
21
|
+
apply(component, sessionId) {
|
|
22
|
+
const key = sessionId === undefined ? component.id : `${component.id}@${sessionId}`;
|
|
23
|
+
const entry = { component, ...(sessionId !== undefined ? { sessionId } : {}) };
|
|
24
|
+
this.entries.set(key, entry);
|
|
25
|
+
return () => { if (this.entries.get(key) === entry)
|
|
26
|
+
this.entries.delete(key); };
|
|
27
|
+
}
|
|
28
|
+
/** The `outputStyle` the process-wide settings name, the latest winning. */
|
|
29
|
+
outputStyle() {
|
|
30
|
+
let style;
|
|
31
|
+
for (const { component, sessionId } of this.entries.values())
|
|
32
|
+
if (sessionId === undefined && component.outputStyle !== undefined)
|
|
33
|
+
style = component.outputStyle;
|
|
34
|
+
return style;
|
|
35
|
+
}
|
|
36
|
+
/** The settings in effect for a session: global ones, then the session's (project) ones. */
|
|
37
|
+
settingsFor(session) {
|
|
38
|
+
const entries = [...this.entries.values()];
|
|
39
|
+
const applies = (sessionId) => sessionId === undefined
|
|
40
|
+
|| (session !== undefined && (session.id === sessionId || session.parentSession === sessionId));
|
|
41
|
+
return [...entries.filter(entry => entry.sessionId === undefined), ...entries.filter(entry => entry.sessionId !== undefined && applies(entry.sessionId))];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Claude settings as `$.settings.read` returns them: every imported
|
|
45
|
+
* settings file in effect merged (later files win, objects merged), or one
|
|
46
|
+
* source's (`user`, `project`, `local`; `policy` is managed settings,
|
|
47
|
+
* which DSH has none of).
|
|
48
|
+
*/
|
|
49
|
+
claudeSettings(session, source) {
|
|
50
|
+
if (source === 'policy' || source === 'flag')
|
|
51
|
+
return {};
|
|
52
|
+
const merged = {};
|
|
53
|
+
for (const { component } of this.settingsFor(session)) {
|
|
54
|
+
for (const file of component.files) {
|
|
55
|
+
const kind = /settings\.local\.json$/u.test(file) ? 'local' : component.id.startsWith('claude-code:user') ? 'user' : 'project';
|
|
56
|
+
if (source !== undefined && kind !== source)
|
|
57
|
+
continue;
|
|
58
|
+
try {
|
|
59
|
+
deepMerge(merged, JSON.parse(readFileSync(file, 'utf8')));
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// An unreadable file adds nothing.
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return merged;
|
|
67
|
+
}
|
|
68
|
+
/** The environment imported settings declare, later (project) settings winning. */
|
|
69
|
+
env(session) {
|
|
70
|
+
const env = {};
|
|
71
|
+
for (const { component } of this.settingsFor(session))
|
|
72
|
+
Object.assign(env, component.env);
|
|
73
|
+
return env;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The Claude-syntax permission rules in effect for a session; deny from any
|
|
77
|
+
* scope holds. An invoked skill's `allowed-tools` and `disallowed-tools`
|
|
78
|
+
* count for the rest of its turn.
|
|
79
|
+
*/
|
|
80
|
+
permissionRules(session) {
|
|
81
|
+
const allow = [];
|
|
82
|
+
const deny = [];
|
|
83
|
+
const ask = [];
|
|
84
|
+
for (const { component } of this.settingsFor(session)) {
|
|
85
|
+
allow.push(...component.permissions.allow);
|
|
86
|
+
deny.push(...component.permissions.deny);
|
|
87
|
+
ask.push(...component.permissions.ask);
|
|
88
|
+
}
|
|
89
|
+
if (session !== undefined) {
|
|
90
|
+
const turn = this.turnRules(session.id);
|
|
91
|
+
allow.push(...turn.allow);
|
|
92
|
+
deny.push(...turn.deny);
|
|
93
|
+
}
|
|
94
|
+
return { allow, deny, ask };
|
|
95
|
+
}
|
|
96
|
+
/** `disableAllHooks` from the settings in effect, the most specific scope winning. */
|
|
97
|
+
hooksDisabled(session) {
|
|
98
|
+
let disabled = false;
|
|
99
|
+
for (const { component } of this.settingsFor(session))
|
|
100
|
+
if (component.disableAllHooks !== undefined)
|
|
101
|
+
disabled = component.disableAllHooks;
|
|
102
|
+
return disabled;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function deepMerge(into, from) {
|
|
106
|
+
for (const [key, value] of Object.entries(from)) {
|
|
107
|
+
const current = into[key];
|
|
108
|
+
if (typeof value === 'object' && value !== null && !Array.isArray(value) && typeof current === 'object' && current !== null && !Array.isArray(current)) {
|
|
109
|
+
deepMerge(current, value);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
into[key] = value;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { CompatibilityVector, ComponentOutcome, SourceFingerprint } from '@kirkchinese/harness-forge-core';
|
|
2
|
+
/** One installed component identity. */
|
|
3
|
+
export interface InstalledComponentRecord {
|
|
4
|
+
readonly kind: 'skill' | 'command' | 'mcp' | 'hooks' | 'agent' | 'code' | 'ui' | 'settings' | 'other';
|
|
5
|
+
readonly name: string;
|
|
6
|
+
readonly path?: string;
|
|
7
|
+
}
|
|
8
|
+
/** One runtime mount outcome recorded for diagnostics. */
|
|
9
|
+
export interface MountRecord {
|
|
10
|
+
readonly kind: 'mcp' | 'hooks' | 'agent' | 'code';
|
|
11
|
+
readonly name: string;
|
|
12
|
+
readonly status: 'mounted' | 'failed';
|
|
13
|
+
readonly error?: string;
|
|
14
|
+
}
|
|
15
|
+
/** One installed foreign plugin. */
|
|
16
|
+
export interface InstalledPluginRecord {
|
|
17
|
+
readonly id: string;
|
|
18
|
+
readonly source: string;
|
|
19
|
+
readonly name: string;
|
|
20
|
+
readonly root: string;
|
|
21
|
+
readonly version?: string;
|
|
22
|
+
readonly license?: string;
|
|
23
|
+
readonly installedAt: string;
|
|
24
|
+
readonly planId?: string;
|
|
25
|
+
readonly fingerprint?: SourceFingerprint;
|
|
26
|
+
readonly components: readonly InstalledComponentRecord[];
|
|
27
|
+
/** Skill roots registered for this plugin; used for exact removal. */
|
|
28
|
+
readonly skillRoots?: readonly string[];
|
|
29
|
+
/** Runtime mount results, retained so the workbench can explain failures. */
|
|
30
|
+
readonly mounts?: readonly MountRecord[];
|
|
31
|
+
/** Current honest vector; recomputed from outcomes, not copied from scan. */
|
|
32
|
+
readonly compat?: CompatibilityVector;
|
|
33
|
+
readonly outcomes?: readonly ComponentOutcome[];
|
|
34
|
+
/** Receipt ids in application order; the files live beside the ledger. */
|
|
35
|
+
readonly receipts?: readonly string[];
|
|
36
|
+
/** Components the person switched off: never mounted until switched on again. */
|
|
37
|
+
readonly disabled?: readonly string[];
|
|
38
|
+
}
|
|
39
|
+
/** The complete ledger document. */
|
|
40
|
+
export interface CompatState {
|
|
41
|
+
readonly schemaVersion: 2;
|
|
42
|
+
readonly plugins: readonly InstalledPluginRecord[];
|
|
43
|
+
}
|
|
44
|
+
/** Resolve the Harness home without importing a DSH runtime package. */
|
|
45
|
+
export declare function resolveDshHome(env?: NodeJS.ProcessEnv): string;
|
|
46
|
+
/** Default ledger path. A launched profile keeps its own ledger; a bare
|
|
47
|
+
* composition falls back to the shared Harness Forge directory. */
|
|
48
|
+
export declare function defaultStatePath(env?: NodeJS.ProcessEnv, profile?: {
|
|
49
|
+
readonly dir?: string;
|
|
50
|
+
readonly name?: string;
|
|
51
|
+
}): string;
|
|
52
|
+
/** Whether a file operation failed in a way it may outlive. */
|
|
53
|
+
export declare function isTransient(error: unknown): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Run a file operation again while it fails transiently, for up to `forMs`. DSH itself can exhaust the process's
|
|
56
|
+
* file handles for seconds (it opens every session's cached projection at once); the waits must not block, since
|
|
57
|
+
* the handles are released by DSH's own callbacks.
|
|
58
|
+
*/
|
|
59
|
+
export declare function retryFile<T>(operation: () => T, forMs?: number): Promise<T>;
|
|
60
|
+
/**
|
|
61
|
+
* Read the ledger. A missing ledger is empty; one that cannot be read throws (an empty answer would be persisted
|
|
62
|
+
* over the real one); one that is not JSON is kept beside it as `state.json.corrupt-<time>` and a new one starts.
|
|
63
|
+
*/
|
|
64
|
+
export declare function readState(path?: string): CompatState;
|
|
65
|
+
/** Persist the ledger atomically. */
|
|
66
|
+
export declare function writeState(state: CompatState, path?: string): void;
|
|
67
|
+
/** Directory holding receipts beside the ledger. */
|
|
68
|
+
export declare function receiptsDirectory(statePath?: string): string;
|
|
69
|
+
export type { CompatibilityVector, ComponentOutcome };
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durable per-profile ledger. It records only what Harness Forge wrote or
|
|
3
|
+
* mounted; the foreign source stays read-only. Receipts live next to it and
|
|
4
|
+
* are referenced by id, never embedded wholesale.
|
|
5
|
+
* @module @kirkchinese/harness-forge
|
|
6
|
+
*/
|
|
7
|
+
import { copyFileSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
8
|
+
import { homedir } from 'node:os';
|
|
9
|
+
import { dirname, join } from 'node:path';
|
|
10
|
+
const EMPTY = { schemaVersion: 2, plugins: [] };
|
|
11
|
+
/** Resolve the Harness home without importing a DSH runtime package. */
|
|
12
|
+
export function resolveDshHome(env = process.env) {
|
|
13
|
+
const configured = env.DSH_HOME?.trim();
|
|
14
|
+
if (configured !== undefined && configured.length > 0) {
|
|
15
|
+
if (configured === '~')
|
|
16
|
+
return homedir();
|
|
17
|
+
if (configured.startsWith('~/') || configured.startsWith('~\\'))
|
|
18
|
+
return join(homedir(), configured.slice(2));
|
|
19
|
+
return configured;
|
|
20
|
+
}
|
|
21
|
+
return join(homedir(), '.dsh');
|
|
22
|
+
}
|
|
23
|
+
/** Default ledger path. A launched profile keeps its own ledger; a bare
|
|
24
|
+
* composition falls back to the shared Harness Forge directory. */
|
|
25
|
+
export function defaultStatePath(env = process.env, profile) {
|
|
26
|
+
if (profile?.dir !== undefined && profile.dir.length > 0) {
|
|
27
|
+
return join(profile.dir, '.harness-forge', 'state.json');
|
|
28
|
+
}
|
|
29
|
+
if (profile?.name !== undefined && profile.name.length > 0) {
|
|
30
|
+
return join(resolveDshHome(env), 'harness-forge', 'profiles', profile.name, 'state.json');
|
|
31
|
+
}
|
|
32
|
+
return join(resolveDshHome(env), 'harness-forge', 'state.json');
|
|
33
|
+
}
|
|
34
|
+
/** Read the ledger. A v1 ledger is migrated without trusting execution. */
|
|
35
|
+
/** Errors a file operation may outlive: the process out of handles, the file briefly locked (a scanner, a sync). */
|
|
36
|
+
const TRANSIENT = new Set(['EMFILE', 'ENFILE', 'EBUSY', 'EAGAIN', 'EPERM', 'EACCES']);
|
|
37
|
+
/** Whether a file operation failed in a way it may outlive. */
|
|
38
|
+
export function isTransient(error) {
|
|
39
|
+
const code = error?.code;
|
|
40
|
+
return typeof code === 'string' && TRANSIENT.has(code);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Run a file operation again while it fails transiently, for up to `forMs`. DSH itself can exhaust the process's
|
|
44
|
+
* file handles for seconds (it opens every session's cached projection at once); the waits must not block, since
|
|
45
|
+
* the handles are released by DSH's own callbacks.
|
|
46
|
+
*/
|
|
47
|
+
export async function retryFile(operation, forMs = 30_000) {
|
|
48
|
+
const deadline = Date.now() + forMs;
|
|
49
|
+
for (;;) {
|
|
50
|
+
try {
|
|
51
|
+
return operation();
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
if (!isTransient(error) || Date.now() > deadline)
|
|
55
|
+
throw error;
|
|
56
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Read the ledger. A missing ledger is empty; one that cannot be read throws (an empty answer would be persisted
|
|
62
|
+
* over the real one); one that is not JSON is kept beside it as `state.json.corrupt-<time>` and a new one starts.
|
|
63
|
+
*/
|
|
64
|
+
export function readState(path = defaultStatePath()) {
|
|
65
|
+
let text;
|
|
66
|
+
try {
|
|
67
|
+
text = readFileSync(path, 'utf8');
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
if (error.code === 'ENOENT')
|
|
71
|
+
return EMPTY;
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
let value;
|
|
75
|
+
try {
|
|
76
|
+
value = JSON.parse(text);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
copyFileSync(path, `${path}.corrupt-${String(Date.now())}`);
|
|
80
|
+
return EMPTY;
|
|
81
|
+
}
|
|
82
|
+
{
|
|
83
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
84
|
+
return EMPTY;
|
|
85
|
+
const record = value;
|
|
86
|
+
if (!Array.isArray(record.plugins))
|
|
87
|
+
return EMPTY;
|
|
88
|
+
if (record.schemaVersion === 2) {
|
|
89
|
+
return { schemaVersion: 2, plugins: record.plugins.filter(isV2Record).map(stripLegacyTrust) };
|
|
90
|
+
}
|
|
91
|
+
if (record.schemaVersion === 1) {
|
|
92
|
+
return {
|
|
93
|
+
schemaVersion: 2,
|
|
94
|
+
plugins: record.plugins.filter(isLegacyRecord).map(legacy => ({
|
|
95
|
+
id: legacy.id,
|
|
96
|
+
source: legacy.source,
|
|
97
|
+
name: legacy.name,
|
|
98
|
+
root: legacy.root,
|
|
99
|
+
...(typeof legacy.version === 'string' ? { version: legacy.version } : {}),
|
|
100
|
+
...(typeof legacy.license === 'string' ? { license: legacy.license } : {}),
|
|
101
|
+
installedAt: legacy.installedAt,
|
|
102
|
+
components: legacy.components.filter(isLegacyComponent),
|
|
103
|
+
...(Array.isArray(legacy.skillRoots) ? { skillRoots: legacy.skillRoots.filter((root) => typeof root === 'string') } : {}),
|
|
104
|
+
receipts: [],
|
|
105
|
+
})),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
return EMPTY;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/** Persist the ledger atomically. */
|
|
112
|
+
export function writeState(state, path = defaultStatePath()) {
|
|
113
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
114
|
+
const temporary = `${path}.tmp`;
|
|
115
|
+
writeFileSync(temporary, `${JSON.stringify(state, null, 2)}\n`, 'utf8');
|
|
116
|
+
renameSync(temporary, path);
|
|
117
|
+
}
|
|
118
|
+
/** Directory holding receipts beside the ledger. */
|
|
119
|
+
export function receiptsDirectory(statePath = defaultStatePath()) {
|
|
120
|
+
return join(dirname(statePath), 'receipts');
|
|
121
|
+
}
|
|
122
|
+
function isV2Record(value) {
|
|
123
|
+
return isBaseRecord(value);
|
|
124
|
+
}
|
|
125
|
+
/** Old v2 ledgers may still carry a trust object; it is no longer state. */
|
|
126
|
+
function stripLegacyTrust(record) {
|
|
127
|
+
const copy = { ...record };
|
|
128
|
+
delete copy['trust'];
|
|
129
|
+
return copy;
|
|
130
|
+
}
|
|
131
|
+
function isLegacyRecord(value) {
|
|
132
|
+
return isBaseRecord(value);
|
|
133
|
+
}
|
|
134
|
+
function isBaseRecord(value) {
|
|
135
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
136
|
+
return false;
|
|
137
|
+
const record = value;
|
|
138
|
+
return typeof record['id'] === 'string'
|
|
139
|
+
&& typeof record['source'] === 'string'
|
|
140
|
+
&& typeof record['name'] === 'string'
|
|
141
|
+
&& typeof record['root'] === 'string'
|
|
142
|
+
&& typeof record['installedAt'] === 'string'
|
|
143
|
+
&& Array.isArray(record['components']);
|
|
144
|
+
}
|
|
145
|
+
function isLegacyComponent(value) {
|
|
146
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
147
|
+
return false;
|
|
148
|
+
const component = value;
|
|
149
|
+
return typeof component['kind'] === 'string' && typeof component['name'] === 'string';
|
|
150
|
+
}
|