@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,733 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ledger: installing, restoring, undoing and removing plugins, the record
|
|
3
|
+
* that survives restarts, and the mounters each row registers for its kind
|
|
4
|
+
* of component. A component whose row is off waits as `inactive` and mounts
|
|
5
|
+
* as soon as the row's mounter is registered; a row that goes away withdraws
|
|
6
|
+
* its components back to `inactive`.
|
|
7
|
+
* @module @kirkchinese/harness-forge
|
|
8
|
+
*/
|
|
9
|
+
import { randomUUID } from 'node:crypto';
|
|
10
|
+
import { existsSync } from 'node:fs';
|
|
11
|
+
import { homedir } from 'node:os';
|
|
12
|
+
import { dirname, resolve } from 'node:path';
|
|
13
|
+
import { assess, dialects, fingerprintPlugin, HarnessError, planPlugin, scanRoot, shortHash, } from '@kirkchinese/harness-forge-core';
|
|
14
|
+
import { pluginDataDir } from '../mcp/mount.js';
|
|
15
|
+
import { writeReceipt, readReceipt } from './receipts.js';
|
|
16
|
+
import { messageOf } from '../shared/util.js';
|
|
17
|
+
import { mountKindOf, ROW_OF_KIND } from './contract.js';
|
|
18
|
+
import { LiveMounts } from './live-mounts.js';
|
|
19
|
+
import { isTransient, readState, receiptsDirectory, retryFile, writeState, } from './state.js';
|
|
20
|
+
/** Observations kept per component: the newest per signal, bounded. */
|
|
21
|
+
const MAX_OBSERVATIONS = 96;
|
|
22
|
+
const PERSIST_DELAY_MS = 500;
|
|
23
|
+
const GLOBAL_SCOPE = {};
|
|
24
|
+
/** The outcome of a component whose row is off. */
|
|
25
|
+
export function inactive(componentId, kind) {
|
|
26
|
+
return { componentId, status: 'inactive', level: 'L0', evidence: 'source', message: `inactive because the ${ROW_OF_KIND[kind]} row is off` };
|
|
27
|
+
}
|
|
28
|
+
/** What an outcome's message says when the person switched the component off. */
|
|
29
|
+
export const SWITCHED_OFF = 'switched off';
|
|
30
|
+
/** The outcome of a component the person switched off. */
|
|
31
|
+
export function switchedOff(componentId) {
|
|
32
|
+
return { componentId, status: 'inactive', level: 'L0', evidence: 'source', message: SWITCHED_OFF };
|
|
33
|
+
}
|
|
34
|
+
/** The outcome of a component whose withdrawal failed: it stays live, and switching it off again retries. */
|
|
35
|
+
export function failedToSwitchOff(componentId, reason) {
|
|
36
|
+
return { componentId, status: 'failed', level: 'L0', evidence: 'runtime', message: `failed to switch off: ${reason}` };
|
|
37
|
+
}
|
|
38
|
+
/** The outcome of a component a failed install could not withdraw: it stays live until removing the plugin (or installing it again) withdraws it. */
|
|
39
|
+
export function failedToCleanUp(componentId, reason) {
|
|
40
|
+
return { componentId, status: 'failed', level: 'L0', evidence: 'runtime', message: `failed to clean up after a failed install: ${reason}` };
|
|
41
|
+
}
|
|
42
|
+
function skipped(componentId, message) {
|
|
43
|
+
return { componentId, status: 'skipped', level: 'L0', evidence: 'source', message };
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The installations: the in-memory registry, plan application, undo and
|
|
47
|
+
* teardown, runtime evidence and the durable ledger, every mutation through
|
|
48
|
+
* one queue.
|
|
49
|
+
*/
|
|
50
|
+
export class Ledger {
|
|
51
|
+
ctx;
|
|
52
|
+
statePath;
|
|
53
|
+
dataRoot;
|
|
54
|
+
fault;
|
|
55
|
+
live = new Map();
|
|
56
|
+
plans = new Map();
|
|
57
|
+
mounters = new Map();
|
|
58
|
+
disposed = false;
|
|
59
|
+
queue = Promise.resolve();
|
|
60
|
+
persistTimer;
|
|
61
|
+
/** Why the ledger could not be read at startup; while set, it is never written. */
|
|
62
|
+
unreadLedger;
|
|
63
|
+
constructor(ctx, statePath, dataRoot,
|
|
64
|
+
/** Where a withdrawal that failed is recorded (the shared service's faults). */
|
|
65
|
+
fault) {
|
|
66
|
+
this.ctx = ctx;
|
|
67
|
+
this.statePath = statePath;
|
|
68
|
+
this.dataRoot = dataRoot;
|
|
69
|
+
this.fault = fault;
|
|
70
|
+
}
|
|
71
|
+
faultOf = (error) => { this.fault?.(error); };
|
|
72
|
+
/**
|
|
73
|
+
* Register the mounter of one kind: components of that kind waiting as
|
|
74
|
+
* inactive mount now. The disposer withdraws them back to inactive.
|
|
75
|
+
*/
|
|
76
|
+
registerMounter(kind, mounter) {
|
|
77
|
+
if (this.mounters.has(kind))
|
|
78
|
+
throw new HarnessError('internal', `a mounter for ${kind} is already registered`);
|
|
79
|
+
this.mounters.set(kind, mounter);
|
|
80
|
+
this.reapply();
|
|
81
|
+
return () => {
|
|
82
|
+
if (this.mounters.get(kind) !== mounter)
|
|
83
|
+
return;
|
|
84
|
+
this.mounters.delete(kind);
|
|
85
|
+
if (!this.disposed)
|
|
86
|
+
void this.exclusive(() => this.withdraw(kind));
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Re-apply installed plans; only actions without a live registration run.
|
|
91
|
+
* Called when a mounter appears.
|
|
92
|
+
*/
|
|
93
|
+
reapply() {
|
|
94
|
+
if (this.disposed)
|
|
95
|
+
return;
|
|
96
|
+
void this.exclusive(async () => {
|
|
97
|
+
if (this.disposed)
|
|
98
|
+
return;
|
|
99
|
+
for (const install of [...this.live.values()]) {
|
|
100
|
+
if (install.plan === undefined)
|
|
101
|
+
continue;
|
|
102
|
+
try {
|
|
103
|
+
await this.applyNow(install.plan);
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
// The ledger keeps the previous outcome; the next re-apply runs again.
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Withdraw every mount of one kind (its row went away): those components
|
|
113
|
+
* wait as inactive. The live registrations say what to withdraw, so a source
|
|
114
|
+
* that no longer scans is withdrawn as well.
|
|
115
|
+
*/
|
|
116
|
+
async withdraw(kind) {
|
|
117
|
+
if (this.disposed)
|
|
118
|
+
return;
|
|
119
|
+
let changed = false;
|
|
120
|
+
for (const install of this.live.values()) {
|
|
121
|
+
const components = new Set();
|
|
122
|
+
const { released, failed } = await install.mounts.release(candidate => candidate.kind === kind);
|
|
123
|
+
for (const mount of released)
|
|
124
|
+
components.add(mount.componentId);
|
|
125
|
+
const stuck = new Set(failed.map(failure => failure.mount.componentId));
|
|
126
|
+
// A component of the kind that mounted nothing to withdraw (it failed, say) waits as inactive too.
|
|
127
|
+
const plugin = this.tryScan(install);
|
|
128
|
+
if (plugin !== undefined) {
|
|
129
|
+
const byId = new Map(plugin.components.map(component => [component.id, component]));
|
|
130
|
+
for (const action of install.plan?.actions ?? []) {
|
|
131
|
+
const component = action.componentId === undefined ? undefined : byId.get(action.componentId);
|
|
132
|
+
if (component !== undefined && mountKindOf(action, component) === kind)
|
|
133
|
+
components.add(component.id);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
let touched = false;
|
|
137
|
+
for (const componentId of components) {
|
|
138
|
+
if (install.record.disabled?.includes(componentId) === true || stuck.has(componentId))
|
|
139
|
+
continue;
|
|
140
|
+
install.outcomes.set(componentId, inactive(componentId, kind));
|
|
141
|
+
touched = true;
|
|
142
|
+
}
|
|
143
|
+
for (const { mount, reason } of failed) {
|
|
144
|
+
install.outcomes.set(mount.componentId, failedToSwitchOff(mount.componentId, reason));
|
|
145
|
+
touched = true;
|
|
146
|
+
}
|
|
147
|
+
if (touched) {
|
|
148
|
+
this.refreshRecord(install, plugin);
|
|
149
|
+
changed = true;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (changed)
|
|
153
|
+
this.persist();
|
|
154
|
+
}
|
|
155
|
+
/** The plugin as its source scans now, or undefined when it no longer does. */
|
|
156
|
+
tryScan(install) {
|
|
157
|
+
try {
|
|
158
|
+
return this.scanRoot(install.record.root, install.record.source);
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* The record's derived fields after its outcomes changed. Without the
|
|
166
|
+
* scanned plugin (its source is gone) only the outcomes are updated.
|
|
167
|
+
*/
|
|
168
|
+
refreshRecord(install, plugin = this.tryScan(install)) {
|
|
169
|
+
if (plugin === undefined) {
|
|
170
|
+
install.record = { ...install.record, outcomes: [...install.outcomes.values()] };
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
install.record = {
|
|
174
|
+
...install.record,
|
|
175
|
+
skillRoots: skillRootsOf(plugin, install.outcomes),
|
|
176
|
+
outcomes: [...install.outcomes.values()],
|
|
177
|
+
compat: assess(plugin, [...install.outcomes.values()]),
|
|
178
|
+
mounts: mountsFor(plugin, install.outcomes),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
/** Scan one plugin without writing anything. */
|
|
182
|
+
scan(root, source) {
|
|
183
|
+
return this.scanRoot(resolve(root), source);
|
|
184
|
+
}
|
|
185
|
+
/** List current installations in stable name order. */
|
|
186
|
+
list() {
|
|
187
|
+
return [...this.live.values()].map(install => install.record).sort((a, b) => a.name.localeCompare(b.name, 'en'));
|
|
188
|
+
}
|
|
189
|
+
// ---------------------------------------------------------------------------
|
|
190
|
+
// Queue-owned operations
|
|
191
|
+
// ---------------------------------------------------------------------------
|
|
192
|
+
exclusive(operation) {
|
|
193
|
+
const run = this.queue.then(operation, operation);
|
|
194
|
+
this.queue = run.catch(() => undefined);
|
|
195
|
+
return run;
|
|
196
|
+
}
|
|
197
|
+
async restoreNow() {
|
|
198
|
+
let state;
|
|
199
|
+
try {
|
|
200
|
+
state = await retryFile(() => readState(this.statePath));
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
// Writing now would replace the ledger with what is live: nothing. Every write is refused until it is read.
|
|
204
|
+
this.unreadLedger = messageOf(error);
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
this.unreadLedger = undefined;
|
|
208
|
+
for (const record of state.plugins) {
|
|
209
|
+
if (this.live.has(record.id))
|
|
210
|
+
continue;
|
|
211
|
+
if (!existsSync(record.root)) {
|
|
212
|
+
this.keepUnrestored(record, `source directory is missing: ${record.root}`);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
const plan = this.planNow(record.root, record.source);
|
|
217
|
+
if (record.fingerprint !== undefined && plan.fingerprint.digest !== record.fingerprint.digest) {
|
|
218
|
+
// The source changed while DSH was closed; apply its current content.
|
|
219
|
+
this.live.set(record.id, { record, mounts: new LiveMounts(this.faultOf), outcomes: new Map() });
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
this.live.set(record.id, {
|
|
223
|
+
record,
|
|
224
|
+
mounts: new LiveMounts(this.faultOf),
|
|
225
|
+
outcomes: new Map((record.outcomes ?? []).map(outcome => [outcome.componentId, outcome])),
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
await this.applyNow(plan);
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
this.keepUnrestored(record, messageOf(error));
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
this.persist();
|
|
235
|
+
}
|
|
236
|
+
/** Keep a record that could not be restored visible, with the reason. */
|
|
237
|
+
keepUnrestored(record, reason) {
|
|
238
|
+
const outcomes = (record.outcomes ?? []).map(outcome => ({ ...outcome, status: 'failed', message: `not restored: ${reason}` }));
|
|
239
|
+
this.live.set(record.id, {
|
|
240
|
+
record: { ...record, outcomes, mounts: [] },
|
|
241
|
+
mounts: new LiveMounts(this.faultOf),
|
|
242
|
+
outcomes: new Map(outcomes.map(outcome => [outcome.componentId, outcome])),
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
planNow(root, source) {
|
|
246
|
+
const plugin = this.scanRoot(resolve(root), source);
|
|
247
|
+
// A manifest that does not parse means the plugin is not what its author wrote: nothing of it is installed.
|
|
248
|
+
const invalid = plugin.warnings.find(warning => warning.code === 'manifest-invalid' || warning.code === 'manifest-schema-unknown');
|
|
249
|
+
if (invalid !== undefined) {
|
|
250
|
+
throw new HarnessError('manifest-invalid', `${plugin.root}: ${invalid.message}; the plugin was not imported`, { suggestion: 'Fix the manifest and import it again.' });
|
|
251
|
+
}
|
|
252
|
+
const plan = planPlugin(plugin);
|
|
253
|
+
this.plans.set(plan.id, plan);
|
|
254
|
+
return plan;
|
|
255
|
+
}
|
|
256
|
+
async applyNow(plan) {
|
|
257
|
+
this.assertAlive();
|
|
258
|
+
const plugin = this.scanRoot(plan.root, plan.source);
|
|
259
|
+
const fingerprint = fingerprintPlugin(plugin);
|
|
260
|
+
if (fingerprint.digest !== plan.fingerprint.digest) {
|
|
261
|
+
throw new HarnessError('fingerprint-mismatch', `source changed since the plan was created: ${plan.root}`, {
|
|
262
|
+
suggestion: 'Scan again and review the new plan.',
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
// A failed install left registrations live: they are withdrawn before anything is installed over them.
|
|
266
|
+
if (this.live.get(plan.pluginId)?.unrecorded === true)
|
|
267
|
+
await this.teardown(plan.pluginId);
|
|
268
|
+
const existing = this.live.get(plan.pluginId);
|
|
269
|
+
const components = new Map(plugin.components.map(component => [component.id, component]));
|
|
270
|
+
const outcomes = new Map(existing?.outcomes ?? []);
|
|
271
|
+
const mounts = existing?.mounts ?? new LiveMounts(this.faultOf);
|
|
272
|
+
/** The actions this apply registered: withdrawn again when the apply cannot be recorded. */
|
|
273
|
+
const added = new Set();
|
|
274
|
+
const disabled = new Set(existing?.record.disabled ?? []);
|
|
275
|
+
const commandNames = new Map();
|
|
276
|
+
for (const component of plugin.components) {
|
|
277
|
+
const prior = outcomes.get(component.id);
|
|
278
|
+
// A mounted command keeps its name across restarts; a failed one carries an error instead.
|
|
279
|
+
if (component.kind === 'command' && (prior?.status === 'mounted' || prior?.status === 'verified') && prior.message !== undefined) {
|
|
280
|
+
commandNames.set(component.id, prior.message);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
const receiptId = randomUUID();
|
|
284
|
+
const startedAt = new Date().toISOString();
|
|
285
|
+
const receiptActions = [];
|
|
286
|
+
const used = new Set();
|
|
287
|
+
for (const action of plan.actions) {
|
|
288
|
+
const component = action.componentId === undefined ? undefined : components.get(action.componentId);
|
|
289
|
+
if (component === undefined) {
|
|
290
|
+
receiptActions.push({ actionId: action.id, kind: action.kind, name: action.name, status: 'failed' });
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
if (mounts.has(action.id)) {
|
|
294
|
+
receiptActions.push({ actionId: action.id, kind: action.kind, name: action.name, status: 'skipped' });
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
if (disabled.has(component.id)) {
|
|
298
|
+
outcomes.set(component.id, switchedOff(component.id));
|
|
299
|
+
receiptActions.push({ actionId: action.id, kind: action.kind, name: action.name, status: 'skipped' });
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
const prior = outcomes.get(component.id);
|
|
303
|
+
const kind = mountKindOf(action, component);
|
|
304
|
+
if (typeof kind !== 'string') {
|
|
305
|
+
outcomes.set(component.id, skipped(component.id, kind.skipped));
|
|
306
|
+
receiptActions.push({ actionId: action.id, kind: action.kind, name: action.name, status: 'skipped' });
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
const mounter = this.mounters.get(kind);
|
|
310
|
+
if (mounter === undefined) {
|
|
311
|
+
outcomes.set(component.id, inactive(component.id, kind));
|
|
312
|
+
receiptActions.push({ actionId: action.id, kind: action.kind, name: action.name, status: 'skipped' });
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
used.add(mounter);
|
|
316
|
+
try {
|
|
317
|
+
const applied = await mounter.mount({
|
|
318
|
+
action,
|
|
319
|
+
component,
|
|
320
|
+
plugin,
|
|
321
|
+
dataDir: pluginDataDir(this.dataRoot, plugin.id),
|
|
322
|
+
scope: GLOBAL_SCOPE,
|
|
323
|
+
commandNames,
|
|
324
|
+
takenNames: () => this.allCommandNames(plugin.id),
|
|
325
|
+
});
|
|
326
|
+
if (applied.disposer !== undefined) {
|
|
327
|
+
mounts.add(action.id, { componentId: component.id, kind, dispose: applied.disposer });
|
|
328
|
+
added.add(action.id);
|
|
329
|
+
}
|
|
330
|
+
// Runtime evidence gathered before a restart stays attached to the component.
|
|
331
|
+
outcomes.set(component.id, prior?.observations === undefined || applied.outcome.status === 'skipped'
|
|
332
|
+
? applied.outcome
|
|
333
|
+
: withObservations(applied.outcome, prior.observations));
|
|
334
|
+
receiptActions.push({
|
|
335
|
+
actionId: action.id,
|
|
336
|
+
kind: action.kind,
|
|
337
|
+
name: action.name,
|
|
338
|
+
status: applied.outcome.status === 'skipped' ? 'skipped' : 'applied',
|
|
339
|
+
...(applied.outcome.message !== undefined ? { undoToken: applied.outcome.message } : {}),
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
catch (error) {
|
|
343
|
+
const info = error instanceof HarnessError ? error.toJSON() : { code: 'runtime-failed', message: messageOf(error) };
|
|
344
|
+
outcomes.set(component.id, { componentId: component.id, status: 'failed', level: 'L0', evidence: 'runtime', message: info.message });
|
|
345
|
+
receiptActions.push({ actionId: action.id, kind: action.kind, name: action.name, status: 'failed', error: info });
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
// The delegation tool restarts on each agent change; the next request must see them all.
|
|
349
|
+
for (const mounter of used)
|
|
350
|
+
await mounter.settle?.(GLOBAL_SCOPE).catch(() => undefined);
|
|
351
|
+
let record;
|
|
352
|
+
let receipt;
|
|
353
|
+
let applied;
|
|
354
|
+
try {
|
|
355
|
+
record = this.buildRecord(plugin, plan, existing, outcomes, commandNames, receiptId);
|
|
356
|
+
const receiptComponents = [...outcomes.values()].map(outcome => ({
|
|
357
|
+
componentId: outcome.componentId,
|
|
358
|
+
status: outcome.status,
|
|
359
|
+
level: outcome.level,
|
|
360
|
+
...(outcome.message !== undefined ? { message: outcome.message } : {}),
|
|
361
|
+
}));
|
|
362
|
+
const failed = receiptActions.some(action => action.status === 'failed');
|
|
363
|
+
applied = receiptActions.some(action => action.status === 'applied');
|
|
364
|
+
receipt = {
|
|
365
|
+
id: receiptId,
|
|
366
|
+
planId: plan.id,
|
|
367
|
+
pluginId: plugin.id,
|
|
368
|
+
root: plugin.root,
|
|
369
|
+
status: failed ? (applied ? 'partial' : 'failed') : 'applied',
|
|
370
|
+
startedAt,
|
|
371
|
+
finishedAt: new Date().toISOString(),
|
|
372
|
+
actions: receiptActions,
|
|
373
|
+
components: receiptComponents,
|
|
374
|
+
achieved: record.compat?.achieved ?? assess(plugin, [...outcomes.values()]).achieved,
|
|
375
|
+
};
|
|
376
|
+
await retryFile(() => writeReceipt(receipt, receiptsDirectory(this.statePath)));
|
|
377
|
+
this.live.set(plugin.id, { record, mounts, outcomes, plan });
|
|
378
|
+
await retryFile(() => this.writeLedger());
|
|
379
|
+
}
|
|
380
|
+
catch (error) {
|
|
381
|
+
// Nothing of this apply is recorded: what it registered is withdrawn, so no live code is left untracked.
|
|
382
|
+
const { failed } = await mounts.release((_mount, actionId) => added.has(actionId));
|
|
383
|
+
for (const mounter of used)
|
|
384
|
+
await mounter.settle?.(GLOBAL_SCOPE).catch(() => undefined);
|
|
385
|
+
if (failed.length > 0) {
|
|
386
|
+
// A registration whose withdrawal failed is never dropped: its install stays listed, with why, and owns it.
|
|
387
|
+
const kept = existing ?? { record: { ...this.buildRecord(plugin, plan, undefined, new Map(), commandNames, receiptId), receipts: [] }, mounts, outcomes: new Map(), unrecorded: true };
|
|
388
|
+
for (const { mount, reason } of failed)
|
|
389
|
+
kept.outcomes.set(mount.componentId, failedToCleanUp(mount.componentId, reason));
|
|
390
|
+
this.refreshRecord(kept, plugin);
|
|
391
|
+
this.live.set(plugin.id, kept);
|
|
392
|
+
}
|
|
393
|
+
else if (existing !== undefined)
|
|
394
|
+
this.live.set(plugin.id, existing);
|
|
395
|
+
else
|
|
396
|
+
this.live.delete(plugin.id);
|
|
397
|
+
throw error;
|
|
398
|
+
}
|
|
399
|
+
return {
|
|
400
|
+
plugin,
|
|
401
|
+
compat: record.compat ?? assess(plugin, [...outcomes.values()]),
|
|
402
|
+
receipt,
|
|
403
|
+
outcomes: [...outcomes.values()],
|
|
404
|
+
alreadyInstalled: existing !== undefined && !applied,
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
async undoNow(receiptId) {
|
|
408
|
+
let found;
|
|
409
|
+
for (const install of this.live.values()) {
|
|
410
|
+
if (!install.record.receipts?.includes(receiptId))
|
|
411
|
+
continue;
|
|
412
|
+
const receipt = readReceipt(receiptId, receiptsDirectory(this.statePath));
|
|
413
|
+
if (receipt !== undefined) {
|
|
414
|
+
found = { install, receipt };
|
|
415
|
+
break;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (found === undefined)
|
|
419
|
+
return false;
|
|
420
|
+
const { install, receipt } = found;
|
|
421
|
+
const planActions = new Map(install.plan?.actions.map(action => [action.id, action]) ?? []);
|
|
422
|
+
const stuck = [];
|
|
423
|
+
for (const action of receipt.actions) {
|
|
424
|
+
const componentId = planActions.get(action.actionId)?.componentId;
|
|
425
|
+
if (action.status !== 'applied') {
|
|
426
|
+
if (componentId !== undefined && install.outcomes.get(componentId)?.status === 'skipped')
|
|
427
|
+
install.outcomes.delete(componentId);
|
|
428
|
+
continue;
|
|
429
|
+
}
|
|
430
|
+
const { failed } = await install.mounts.release((_mount, actionId) => actionId === action.actionId);
|
|
431
|
+
for (const { mount, reason } of failed) {
|
|
432
|
+
install.outcomes.set(mount.componentId, failedToSwitchOff(mount.componentId, reason));
|
|
433
|
+
stuck.push(`${mount.componentId}: failed to switch off: ${reason}`);
|
|
434
|
+
}
|
|
435
|
+
if (componentId !== undefined && failed.length === 0)
|
|
436
|
+
install.outcomes.delete(componentId);
|
|
437
|
+
}
|
|
438
|
+
if (stuck.length > 0) {
|
|
439
|
+
// The receipt stays applied: undoing it again retries.
|
|
440
|
+
this.refreshRecord(install);
|
|
441
|
+
this.persist();
|
|
442
|
+
throw new HarnessError('runtime-failed', `undo incomplete: ${stuck.join('; ')}`, { suggestion: 'Undo it again to retry.' });
|
|
443
|
+
}
|
|
444
|
+
await retryFile(() => writeReceipt({ ...receipt, status: 'undone', finishedAt: new Date().toISOString() }, receiptsDirectory(this.statePath)));
|
|
445
|
+
if (install.outcomes.size === 0) {
|
|
446
|
+
this.live.delete(install.record.id);
|
|
447
|
+
this.persist();
|
|
448
|
+
return true;
|
|
449
|
+
}
|
|
450
|
+
this.refreshRecord(install);
|
|
451
|
+
install.record = { ...install.record, receipts: install.record.receipts?.filter(id => id !== receiptId) ?? [] };
|
|
452
|
+
this.persist();
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Switch one component of an installed plugin off (its registrations are
|
|
457
|
+
* released and it stays off across restarts) or on again (it mounts as at
|
|
458
|
+
* install). Returns whether anything changed.
|
|
459
|
+
*/
|
|
460
|
+
async setEnabledNow(pluginId, componentId, enabled) {
|
|
461
|
+
this.assertAlive();
|
|
462
|
+
const install = this.live.get(pluginId);
|
|
463
|
+
if (install === undefined)
|
|
464
|
+
throw new HarnessError('not-installed', `no installed plugin ${pluginId}`);
|
|
465
|
+
// Switching off needs no scan: a source that changed or went away is still switched off.
|
|
466
|
+
const known = install.outcomes.has(componentId)
|
|
467
|
+
|| install.plan?.actions.some(action => action.componentId === componentId) === true
|
|
468
|
+
|| (enabled && this.scanRoot(install.record.root, install.record.source).components.some(candidate => candidate.id === componentId));
|
|
469
|
+
if (!known)
|
|
470
|
+
throw new HarnessError('not-installed', `${pluginId} has no part ${componentId}`);
|
|
471
|
+
const disabled = new Set(install.record.disabled ?? []);
|
|
472
|
+
if (disabled.has(componentId) === !enabled)
|
|
473
|
+
return false;
|
|
474
|
+
if (enabled)
|
|
475
|
+
disabled.delete(componentId);
|
|
476
|
+
else
|
|
477
|
+
disabled.add(componentId);
|
|
478
|
+
install.record = { ...install.record, disabled: [...disabled] };
|
|
479
|
+
if (enabled) {
|
|
480
|
+
install.outcomes.delete(componentId);
|
|
481
|
+
await this.applyNow(install.plan ?? this.planNow(install.record.root, install.record.source));
|
|
482
|
+
return true;
|
|
483
|
+
}
|
|
484
|
+
const settle = new Set();
|
|
485
|
+
const { released, failed } = await install.mounts.release(candidate => candidate.componentId === componentId);
|
|
486
|
+
for (const mount of released) {
|
|
487
|
+
const mounter = this.mounters.get(mount.kind);
|
|
488
|
+
if (mounter !== undefined)
|
|
489
|
+
settle.add(mounter);
|
|
490
|
+
}
|
|
491
|
+
for (const mounter of settle)
|
|
492
|
+
await mounter.settle?.(GLOBAL_SCOPE).catch(() => undefined);
|
|
493
|
+
if (failed.length > 0) {
|
|
494
|
+
// Still live: it is not recorded as off, so switching it off again retries.
|
|
495
|
+
disabled.delete(componentId);
|
|
496
|
+
install.record = { ...install.record, disabled: [...disabled] };
|
|
497
|
+
const outcome = failedToSwitchOff(componentId, failed.map(failure => failure.reason).join('; '));
|
|
498
|
+
install.outcomes.set(componentId, outcome);
|
|
499
|
+
this.refreshRecord(install);
|
|
500
|
+
this.persist();
|
|
501
|
+
throw new HarnessError('runtime-failed', `${componentId}: ${outcome.message ?? ''}`, { suggestion: 'Switch it off again to retry.' });
|
|
502
|
+
}
|
|
503
|
+
install.outcomes.set(componentId, switchedOff(componentId));
|
|
504
|
+
this.refreshRecord(install);
|
|
505
|
+
this.persist();
|
|
506
|
+
return true;
|
|
507
|
+
}
|
|
508
|
+
/** Release one installation without deciding whether the ledger survives. */
|
|
509
|
+
async teardown(id) {
|
|
510
|
+
const install = this.live.get(id);
|
|
511
|
+
if (install === undefined)
|
|
512
|
+
return false;
|
|
513
|
+
const { failed } = await install.mounts.release();
|
|
514
|
+
if (failed.length > 0) {
|
|
515
|
+
// The installation stays, with what is still live shown as failed: removing it again retries.
|
|
516
|
+
const stillLive = install.unrecorded === true ? failedToCleanUp : failedToSwitchOff;
|
|
517
|
+
for (const { mount, reason } of failed)
|
|
518
|
+
install.outcomes.set(mount.componentId, stillLive(mount.componentId, reason));
|
|
519
|
+
this.refreshRecord(install);
|
|
520
|
+
throw new HarnessError('runtime-failed', `${id}: ${failed.map(({ mount }) => `${mount.componentId}: ${install.outcomes.get(mount.componentId)?.message ?? ''}`).join('; ')}`, { suggestion: 'Remove it again to retry.' });
|
|
521
|
+
}
|
|
522
|
+
this.live.delete(id);
|
|
523
|
+
return true;
|
|
524
|
+
}
|
|
525
|
+
// ---------------------------------------------------------------------------
|
|
526
|
+
// Evidence
|
|
527
|
+
// ---------------------------------------------------------------------------
|
|
528
|
+
/** Record one runtime observation; the component's status follows its evidence. */
|
|
529
|
+
observe(componentId, observation) {
|
|
530
|
+
for (const install of this.live.values()) {
|
|
531
|
+
const prior = install.outcomes.get(componentId);
|
|
532
|
+
if (prior === undefined)
|
|
533
|
+
continue;
|
|
534
|
+
const next = 'clear' in observation
|
|
535
|
+
? withObservations(prior, (prior.observations ?? []).filter(item => item.signal !== observation.clear))
|
|
536
|
+
: withObservations(prior, [...prior.observations ?? [], observation]);
|
|
537
|
+
install.outcomes.set(componentId, next);
|
|
538
|
+
this.refreshRecord(install);
|
|
539
|
+
this.persistSoon();
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
// ---------------------------------------------------------------------------
|
|
544
|
+
// Helpers
|
|
545
|
+
// ---------------------------------------------------------------------------
|
|
546
|
+
scanRoot(root, source) {
|
|
547
|
+
// A harness's user directory (`~/.claude`, `~/.codex`) is the user's own assets, not a plugin.
|
|
548
|
+
for (const dialect of dialects()) {
|
|
549
|
+
if (dialect.user === undefined || resolve(root) !== resolve(dialect.user.root(homedir())) || (source !== undefined && source !== dialect.id))
|
|
550
|
+
continue;
|
|
551
|
+
const user = dialect.user.scan(homedir());
|
|
552
|
+
if (user === undefined)
|
|
553
|
+
throw new HarnessError('manifest-not-found', `${root} ${dialect.user.emptyMessage}`);
|
|
554
|
+
return user;
|
|
555
|
+
}
|
|
556
|
+
const plugin = scanRoot(root, source);
|
|
557
|
+
// Another installed plugin has this name: this one gets a stable suffix from its root, so both are installed.
|
|
558
|
+
const holder = this.live.get(plugin.id);
|
|
559
|
+
if (holder === undefined || samePath(holder.record.root, root))
|
|
560
|
+
return plugin;
|
|
561
|
+
const name = `${plugin.name}-${shortHash(resolve(root).toLowerCase(), 6)}`;
|
|
562
|
+
const renamed = scanRoot(root, plugin.source, { name });
|
|
563
|
+
return { ...renamed, warnings: [...renamed.warnings, { code: 'name-conflict', message: `a plugin named ${plugin.name} is already installed from ${holder.record.root}; this one is installed as ${name}` }] };
|
|
564
|
+
}
|
|
565
|
+
buildRecord(plugin, plan, existing, outcomes, commandNames, receiptId) {
|
|
566
|
+
const components = plugin.components.map(component => ({
|
|
567
|
+
kind: component.kind,
|
|
568
|
+
name: component.kind === 'command'
|
|
569
|
+
? commandNames.get(component.id) ?? component.name
|
|
570
|
+
: component.kind === 'mcp' || component.kind === 'agent'
|
|
571
|
+
? outcomes.get(component.id)?.message ?? component.name
|
|
572
|
+
: component.name,
|
|
573
|
+
...(component.path !== undefined ? { path: component.path } : {}),
|
|
574
|
+
}));
|
|
575
|
+
return {
|
|
576
|
+
id: plugin.id,
|
|
577
|
+
source: plugin.source,
|
|
578
|
+
name: plugin.name,
|
|
579
|
+
root: plugin.root,
|
|
580
|
+
...(plugin.version !== undefined ? { version: plugin.version } : {}),
|
|
581
|
+
...(plugin.license !== undefined ? { license: plugin.license } : {}),
|
|
582
|
+
installedAt: existing?.record.installedAt ?? new Date().toISOString(),
|
|
583
|
+
planId: plan.id,
|
|
584
|
+
fingerprint: plan.fingerprint,
|
|
585
|
+
components,
|
|
586
|
+
skillRoots: skillRootsOf(plugin, outcomes),
|
|
587
|
+
mounts: mountsFor(plugin, outcomes),
|
|
588
|
+
compat: assess(plugin, [...outcomes.values()]),
|
|
589
|
+
outcomes: [...outcomes.values()],
|
|
590
|
+
receipts: [...(existing?.record.receipts ?? []), receiptId],
|
|
591
|
+
...(existing?.record.disabled !== undefined && existing.record.disabled.length > 0 ? { disabled: existing.record.disabled } : {}),
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
report(result) {
|
|
595
|
+
const components = new Map(result.plugin.components.map(component => [component.id, component]));
|
|
596
|
+
const mcp = [];
|
|
597
|
+
const hooks = [];
|
|
598
|
+
const agents = [];
|
|
599
|
+
for (const outcome of result.outcomes) {
|
|
600
|
+
if (outcome.status === 'skipped' || outcome.status === 'inactive')
|
|
601
|
+
continue;
|
|
602
|
+
const component = components.get(outcome.componentId);
|
|
603
|
+
if (component === undefined)
|
|
604
|
+
continue;
|
|
605
|
+
const status = outcome.status === 'failed' ? 'failed' : 'mounted';
|
|
606
|
+
const error = outcome.status === 'failed' && outcome.message !== undefined ? { error: outcome.message } : {};
|
|
607
|
+
if (component.kind === 'mcp')
|
|
608
|
+
mcp.push({ name: outcome.message ?? component.name, status, ...error });
|
|
609
|
+
if (component.kind === 'hooks')
|
|
610
|
+
hooks.push({ name: component.name, dialect: result.plugin.source === 'codex' ? 'codex' : 'claude-code', status, ...error });
|
|
611
|
+
if (component.kind === 'agent')
|
|
612
|
+
agents.push({ name: component.name, presetId: outcome.message ?? '', status, ...error });
|
|
613
|
+
}
|
|
614
|
+
const record = this.live.get(result.plugin.id)?.record;
|
|
615
|
+
return {
|
|
616
|
+
...result,
|
|
617
|
+
skillRoots: record?.skillRoots ?? [],
|
|
618
|
+
commands: record?.components.filter(component => component.kind === 'command').map(component => component.name) ?? [],
|
|
619
|
+
mcp,
|
|
620
|
+
hooks,
|
|
621
|
+
agents,
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
allCommandNames(excludePluginId) {
|
|
625
|
+
const names = new Set();
|
|
626
|
+
for (const [id, install] of this.live) {
|
|
627
|
+
if (id === excludePluginId)
|
|
628
|
+
continue;
|
|
629
|
+
for (const component of install.record.components)
|
|
630
|
+
if (component.kind === 'command')
|
|
631
|
+
names.add(component.name);
|
|
632
|
+
}
|
|
633
|
+
return names;
|
|
634
|
+
}
|
|
635
|
+
assertAlive() {
|
|
636
|
+
if (this.disposed)
|
|
637
|
+
throw new HarnessError('internal', 'Harness Forge service is disposed');
|
|
638
|
+
}
|
|
639
|
+
persistSoon() {
|
|
640
|
+
if (this.persistTimer !== undefined)
|
|
641
|
+
return;
|
|
642
|
+
this.persistTimer = setTimeout(() => {
|
|
643
|
+
this.persistTimer = undefined;
|
|
644
|
+
this.persist();
|
|
645
|
+
}, PERSIST_DELAY_MS);
|
|
646
|
+
this.persistTimer.unref?.();
|
|
647
|
+
}
|
|
648
|
+
/** Write the ledger, or try again shortly when the write fails transiently. */
|
|
649
|
+
persist() {
|
|
650
|
+
try {
|
|
651
|
+
this.writeLedger();
|
|
652
|
+
}
|
|
653
|
+
catch (error) {
|
|
654
|
+
if (!isTransient(error))
|
|
655
|
+
throw error;
|
|
656
|
+
this.persistSoon();
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
writeLedger() {
|
|
660
|
+
if (this.unreadLedger !== undefined) {
|
|
661
|
+
throw new HarnessError('internal', `the ledger could not be read at startup (${this.unreadLedger}); nothing is written over it`, {
|
|
662
|
+
suggestion: 'Fix or move aside .harness-forge/state.json, then restart DSH.',
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
const state = { schemaVersion: 2, plugins: [...this.live.values()].filter(install => install.unrecorded !== true).map(install => install.record) };
|
|
666
|
+
writeState(state, this.statePath);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
/** The same directory, whatever the case or separators (Windows paths are case-insensitive). */
|
|
670
|
+
function samePath(a, b) {
|
|
671
|
+
return process.platform === 'win32' ? resolve(a).toLowerCase() === resolve(b).toLowerCase() : resolve(a) === resolve(b);
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Attach observations to an outcome: the newest per signal, bounded. A
|
|
675
|
+
* component with any currently failing signal is `failed`; one whose every
|
|
676
|
+
* observed signal succeeded is `verified`. Mount alone stays `mounted`.
|
|
677
|
+
*/
|
|
678
|
+
function withObservations(outcome, observations) {
|
|
679
|
+
const latest = new Map();
|
|
680
|
+
for (const observation of observations) {
|
|
681
|
+
latest.delete(observation.signal);
|
|
682
|
+
latest.set(observation.signal, observation);
|
|
683
|
+
}
|
|
684
|
+
const kept = [...latest.values()].slice(-MAX_OBSERVATIONS);
|
|
685
|
+
if (outcome.status === 'skipped' || outcome.status === 'inactive')
|
|
686
|
+
return outcome;
|
|
687
|
+
if (kept.length === 0) {
|
|
688
|
+
const { observations: _dropped, ...rest } = outcome;
|
|
689
|
+
return { ...rest, status: 'mounted', evidence: 'source' };
|
|
690
|
+
}
|
|
691
|
+
// `message` keeps the mount identity (command name, server, preset); the
|
|
692
|
+
// failing observations carry their own details.
|
|
693
|
+
return {
|
|
694
|
+
...outcome,
|
|
695
|
+
observations: kept,
|
|
696
|
+
status: kept.some(observation => !observation.ok) ? 'failed' : 'verified',
|
|
697
|
+
evidence: 'runtime',
|
|
698
|
+
};
|
|
699
|
+
}
|
|
700
|
+
/** The directory a skill provider root must contain for this skill body. */
|
|
701
|
+
export function skillRootOf(bodyPath) {
|
|
702
|
+
return /[\\/]SKILL\.md$/iu.test(bodyPath) ? dirname(dirname(bodyPath)) : dirname(bodyPath);
|
|
703
|
+
}
|
|
704
|
+
/** The skill roots of a plugin's mounted skills. */
|
|
705
|
+
function skillRootsOf(plugin, outcomes) {
|
|
706
|
+
const roots = new Set();
|
|
707
|
+
for (const component of plugin.components) {
|
|
708
|
+
const status = outcomes.get(component.id)?.status;
|
|
709
|
+
if (component.kind === 'skill' && (status === 'mounted' || status === 'verified'))
|
|
710
|
+
roots.add(skillRootOf(component.bodyPath));
|
|
711
|
+
}
|
|
712
|
+
return [...roots].sort((a, b) => a.localeCompare(b, 'en'));
|
|
713
|
+
}
|
|
714
|
+
/** Project the current component outcomes into the workbench mount list. */
|
|
715
|
+
export function mountsFor(plugin, outcomes) {
|
|
716
|
+
const mounts = [];
|
|
717
|
+
for (const [componentId, outcome] of outcomes) {
|
|
718
|
+
const component = plugin.components.find(candidate => candidate.id === componentId);
|
|
719
|
+
if (component === undefined)
|
|
720
|
+
continue;
|
|
721
|
+
if (component.kind !== 'mcp' && component.kind !== 'hooks' && component.kind !== 'agent' && component.kind !== 'code')
|
|
722
|
+
continue;
|
|
723
|
+
if (outcome.status === 'skipped' || outcome.status === 'inactive')
|
|
724
|
+
continue;
|
|
725
|
+
mounts.push({
|
|
726
|
+
kind: component.kind === 'agent' ? 'agent' : component.kind === 'code' ? 'code' : component.kind === 'mcp' ? 'mcp' : 'hooks',
|
|
727
|
+
name: component.name,
|
|
728
|
+
status: outcome.status === 'failed' ? 'failed' : 'mounted',
|
|
729
|
+
...(outcome.status === 'failed' && outcome.message !== undefined ? { error: outcome.message } : {}),
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
return mounts;
|
|
733
|
+
}
|