@kirkchinese/harness-forge 0.0.0-stage → 0.5.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 +46 -2
- package/assets/icon.svg +12 -0
- package/cordis.patch.yml +28 -0
- package/docs/DETAIL.zh.md +47 -0
- package/lib/agents/presets.d.ts +98 -0
- package/lib/agents/presets.js +292 -0
- package/lib/agents/profiles.d.ts +58 -0
- package/lib/agents/profiles.js +279 -0
- package/lib/agents/row.d.ts +17 -0
- package/lib/agents/row.js +148 -0
- package/lib/client/paths.d.ts +12 -0
- package/lib/client/paths.js +68 -0
- package/lib/client/styles.d.ts +8 -0
- package/lib/client/styles.js +89 -0
- package/lib/client/typert.d.ts +25 -0
- package/lib/client/typert.js +158 -0
- package/lib/client.js +23823 -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 +110 -0
- package/lib/dsh/host.js +217 -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 +271 -0
- package/lib/forge/contract.js +45 -0
- package/lib/forge/follow.d.ts +30 -0
- package/lib/forge/follow.js +73 -0
- package/lib/forge/ledger.d.ts +206 -0
- package/lib/forge/ledger.js +1005 -0
- package/lib/forge/live-mounts.d.ts +45 -0
- package/lib/forge/live-mounts.js +78 -0
- package/lib/forge/npm.d.ts +31 -0
- package/lib/forge/npm.js +60 -0
- package/lib/forge/projects.d.ts +97 -0
- package/lib/forge/projects.js +343 -0
- package/lib/forge/receipts.d.ts +5 -0
- package/lib/forge/receipts.js +31 -0
- package/lib/forge/remote/archive.d.ts +65 -0
- package/lib/forge/remote/archive.js +565 -0
- package/lib/forge/remote/code-parts.d.ts +18 -0
- package/lib/forge/remote/code-parts.js +76 -0
- package/lib/forge/remote/fetch.d.ts +265 -0
- package/lib/forge/remote/fetch.js +1133 -0
- package/lib/forge/remote/service.d.ts +256 -0
- package/lib/forge/remote/service.js +868 -0
- package/lib/forge/row.d.ts +32 -0
- package/lib/forge/row.js +59 -0
- package/lib/forge/service.d.ts +115 -0
- package/lib/forge/service.js +290 -0
- package/lib/forge/settings.d.ts +43 -0
- package/lib/forge/settings.js +122 -0
- package/lib/forge/state.d.ts +129 -0
- package/lib/forge/state.js +266 -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 +722 -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 +27 -0
- package/lib/hooks/broker/session-events.js +205 -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 +264 -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 +87 -0
- package/lib/hooks/condition.js +602 -0
- package/lib/hooks/config.d.ts +85 -0
- package/lib/hooks/config.js +154 -0
- package/lib/hooks/glob.d.ts +41 -0
- package/lib/hooks/glob.js +114 -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 +19 -0
- package/lib/hooks/permissions.js +202 -0
- package/lib/hooks/powershell-aliases.d.ts +19 -0
- package/lib/hooks/powershell-aliases.js +58 -0
- package/lib/hooks/process.d.ts +64 -0
- package/lib/hooks/process.js +289 -0
- package/lib/hooks/row.d.ts +17 -0
- package/lib/hooks/row.js +96 -0
- package/lib/hooks/runner.d.ts +98 -0
- package/lib/hooks/runner.js +322 -0
- package/lib/hooks/shell-commands.d.ts +28 -0
- package/lib/hooks/shell-commands.js +241 -0
- package/lib/hooks/shell.d.ts +44 -0
- package/lib/hooks/shell.js +129 -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 +366 -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 +39 -0
- package/lib/mcp/catalog.js +69 -0
- package/lib/mcp/mount.d.ts +113 -0
- package/lib/mcp/mount.js +317 -0
- package/lib/mcp/policy.d.ts +42 -0
- package/lib/mcp/policy.js +43 -0
- package/lib/mcp/remote.d.ts +45 -0
- package/lib/mcp/remote.js +133 -0
- package/lib/mcp/row.d.ts +9 -0
- package/lib/mcp/row.js +225 -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 +231 -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 +33 -0
- package/lib/mods/service/turns.js +136 -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 +168 -0
- package/lib/opencode/convert.d.ts +53 -0
- package/lib/opencode/convert.js +255 -0
- package/lib/opencode/row.d.ts +22 -0
- package/lib/opencode/row.js +104 -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 +354 -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/adapter-paths.d.ts +13 -0
- package/lib/porting/adapter-paths.js +120 -0
- package/lib/porting/port-gap.d.ts +36 -0
- package/lib/porting/port-gap.js +158 -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 +51 -0
- package/lib/porting/row.d.ts +16 -0
- package/lib/porting/row.js +27 -0
- package/lib/porting/verify-port.d.ts +46 -0
- package/lib/porting/verify-port.js +82 -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/chat-import.d.ts +48 -0
- package/lib/sessions/chat-import.js +44 -0
- package/lib/sessions/claude.d.ts +35 -0
- package/lib/sessions/claude.js +104 -0
- package/lib/sessions/codex-discover.d.ts +34 -0
- package/lib/sessions/codex-discover.js +205 -0
- package/lib/sessions/codex-meta.d.ts +63 -0
- package/lib/sessions/codex-meta.js +148 -0
- package/lib/sessions/codex-text.d.ts +9 -0
- package/lib/sessions/codex-text.js +44 -0
- package/lib/sessions/codex.d.ts +42 -0
- package/lib/sessions/codex.js +270 -0
- package/lib/sessions/opencode.d.ts +38 -0
- package/lib/sessions/opencode.js +339 -0
- package/lib/sessions/persist.d.ts +162 -0
- package/lib/sessions/persist.js +377 -0
- package/lib/sessions/row.d.ts +15 -0
- package/lib/sessions/row.js +101 -0
- package/lib/sessions/turns.d.ts +31 -0
- package/lib/sessions/turns.js +63 -0
- package/lib/sessions/zstd.d.ts +15 -0
- package/lib/sessions/zstd.js +152 -0
- package/lib/shared/invocation.d.ts +169 -0
- package/lib/shared/invocation.js +325 -0
- package/lib/shared/render.d.ts +101 -0
- package/lib/shared/render.js +1353 -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 +74 -0
- package/lib/skills/provider.js +346 -0
- package/lib/skills/row.d.ts +22 -0
- package/lib/skills/row.js +204 -0
- package/lib/workbench/adapter-coverage.d.ts +65 -0
- package/lib/workbench/adapter-coverage.js +141 -0
- package/lib/workbench/model-tools.d.ts +10 -0
- package/lib/workbench/model-tools.js +122 -0
- package/lib/workbench/overview.d.ts +153 -0
- package/lib/workbench/overview.js +246 -0
- package/lib/workbench/remote.d.ts +161 -0
- package/lib/workbench/remote.js +561 -0
- package/lib/workbench/row.d.ts +15 -0
- package/lib/workbench/row.js +16 -0
- package/lib/workbench/settings.d.ts +19 -0
- package/lib/workbench/settings.js +60 -0
- package/lib/workbench/workbench.d.ts +130 -0
- package/lib/workbench/workbench.js +361 -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 +7 -0
- package/package.json +247 -3
- package/runtime/opencode-host.mjs +270 -0
- package/skills/harness-forge/SKILL.md +47 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What every capability row does the same way: find the shared service,
|
|
3
|
+
* start isolated (a row that throws while starting is logged and recorded,
|
|
4
|
+
* and the other rows go on), and report its listeners' failures.
|
|
5
|
+
* @module @kirkchinese/harness-forge
|
|
6
|
+
*/
|
|
7
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
8
|
+
import type { ComponentOutcome, ForeignComponent } from '@kirkchinese/harness-forge-core';
|
|
9
|
+
import type { Disposer } from './contract.js';
|
|
10
|
+
import { type HarnessForge } from './service.js';
|
|
11
|
+
/** A component's outcome once mounted: `mounted` at most; only a runtime observation upgrades it. */
|
|
12
|
+
export declare function mounted(component: ForeignComponent, level: ComponentOutcome['level'], message?: string): ComponentOutcome;
|
|
13
|
+
/**
|
|
14
|
+
* Run one row on the shared service for as long as the row's context lives.
|
|
15
|
+
* A start that throws is logged and recorded as the row's fault; its
|
|
16
|
+
* listeners' failures are recorded too.
|
|
17
|
+
*/
|
|
18
|
+
export declare function rowEffect(ctx: Context, row: string, start: (forge: HarnessForge) => Disposer): void;
|
|
19
|
+
/**
|
|
20
|
+
* The parts one row starts, each isolated: a part that throws is logged and
|
|
21
|
+
* recorded and the row's other parts start; teardown runs every part's
|
|
22
|
+
* disposer, last started first, each isolated too.
|
|
23
|
+
*/
|
|
24
|
+
export declare class Parts {
|
|
25
|
+
private readonly ctx;
|
|
26
|
+
private readonly forge;
|
|
27
|
+
private readonly row;
|
|
28
|
+
private readonly disposers;
|
|
29
|
+
constructor(ctx: Context, forge: HarnessForge, row: string);
|
|
30
|
+
add(label: string, start: () => Disposer | void): void;
|
|
31
|
+
dispose(): Promise<void>;
|
|
32
|
+
}
|
package/lib/forge/row.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { reportFaults } from '../dsh/host.js';
|
|
2
|
+
import { FORGE_SERVICE } from './service.js';
|
|
3
|
+
import { disposeAll, loggerOf, messageOf } from '../shared/util.js';
|
|
4
|
+
/** A component's outcome once mounted: `mounted` at most; only a runtime observation upgrades it. */
|
|
5
|
+
export function mounted(component, level, message) {
|
|
6
|
+
return { componentId: component.id, status: 'mounted', level, evidence: 'source', ...(message !== undefined ? { message } : {}) };
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Run one row on the shared service for as long as the row's context lives.
|
|
10
|
+
* A start that throws is logged and recorded as the row's fault; its
|
|
11
|
+
* listeners' failures are recorded too.
|
|
12
|
+
*/
|
|
13
|
+
export function rowEffect(ctx, row, start) {
|
|
14
|
+
ctx.effect(() => {
|
|
15
|
+
const forge = ctx.get(FORGE_SERVICE);
|
|
16
|
+
if (forge === undefined)
|
|
17
|
+
return () => { };
|
|
18
|
+
const stopReporting = reportFaults(ctx, (error) => { forge.fault(row, error); });
|
|
19
|
+
let stop = () => { };
|
|
20
|
+
try {
|
|
21
|
+
stop = start(forge);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
loggerOf(ctx)?.warn?.(`harness-forge: the ${row} row did not start: ${messageOf(error)}`);
|
|
25
|
+
forge.fault(row, error);
|
|
26
|
+
}
|
|
27
|
+
return () => disposeAll(loggerOf(ctx), row, [stop, stopReporting]);
|
|
28
|
+
}, `harness-forge.${row}`);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The parts one row starts, each isolated: a part that throws is logged and
|
|
32
|
+
* recorded and the row's other parts start; teardown runs every part's
|
|
33
|
+
* disposer, last started first, each isolated too.
|
|
34
|
+
*/
|
|
35
|
+
export class Parts {
|
|
36
|
+
ctx;
|
|
37
|
+
forge;
|
|
38
|
+
row;
|
|
39
|
+
disposers = [];
|
|
40
|
+
constructor(ctx, forge, row) {
|
|
41
|
+
this.ctx = ctx;
|
|
42
|
+
this.forge = forge;
|
|
43
|
+
this.row = row;
|
|
44
|
+
}
|
|
45
|
+
add(label, start) {
|
|
46
|
+
try {
|
|
47
|
+
const dispose = start();
|
|
48
|
+
if (dispose !== undefined)
|
|
49
|
+
this.disposers.push(dispose);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
loggerOf(this.ctx)?.warn?.(`harness-forge: ${this.row}: ${label} did not start: ${messageOf(error)}`);
|
|
53
|
+
this.forge.fault(this.row, error);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
dispose() {
|
|
57
|
+
return disposeAll(loggerOf(this.ctx), this.row, this.disposers.splice(0).reverse());
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
2
|
+
import { type ForeignPlugin, type Plan } from '@kirkchinese/harness-forge-core';
|
|
3
|
+
import { type ForeignShell } from '../hooks/shell.js';
|
|
4
|
+
import { Invocations } from '../shared/invocation.js';
|
|
5
|
+
import type { InstalledPluginRecord } from './state.js';
|
|
6
|
+
import type { Disposer, MountKind, Mounter, RowApis } from './contract.js';
|
|
7
|
+
import { Ledger, type ApplyProgress, type ApplyResult, type InstallReport } from './ledger.js';
|
|
8
|
+
import { ProjectAssets } from './projects.js';
|
|
9
|
+
import { ImportedSettings } from './settings.js';
|
|
10
|
+
import { RemotePlugins } from './remote/service.js';
|
|
11
|
+
import type { FetchDeps, FetchLimits } from './remote/fetch.js';
|
|
12
|
+
export type { AgentReport, ApplyResult, HookReport, InstallReport, McpReport } from './ledger.js';
|
|
13
|
+
/** The service name the `harness-forge` row provides. */
|
|
14
|
+
export declare const FORGE_SERVICE = "harnessForge";
|
|
15
|
+
declare module '@deepseek-ai/cordis' {
|
|
16
|
+
interface Context {
|
|
17
|
+
harnessForge: HarnessForge;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** A failure of one row's listener or part, kept for the workbench. */
|
|
21
|
+
export interface RowFault {
|
|
22
|
+
readonly row: string;
|
|
23
|
+
readonly at: string;
|
|
24
|
+
readonly message: string;
|
|
25
|
+
}
|
|
26
|
+
/** The shared service. */
|
|
27
|
+
export declare class HarnessForge {
|
|
28
|
+
readonly ctx: Context;
|
|
29
|
+
readonly statePath: string;
|
|
30
|
+
readonly dataRoot: string;
|
|
31
|
+
/** The shell imported hooks, commands and mods run in. */
|
|
32
|
+
readonly shell: ForeignShell;
|
|
33
|
+
readonly settings: ImportedSettings;
|
|
34
|
+
/** Turn-scoped effects of invoked skills and commands (`allowed-tools`, `model`, `context: fork`, ...). */
|
|
35
|
+
readonly invocations: Invocations;
|
|
36
|
+
/** Claude Code and Codex project assets per session. */
|
|
37
|
+
readonly projects: ProjectAssets;
|
|
38
|
+
readonly ledger: Ledger;
|
|
39
|
+
/** Plugins fetched from the network into `<dataRoot>/remote`. */
|
|
40
|
+
readonly remote: RemotePlugins;
|
|
41
|
+
private readonly apis;
|
|
42
|
+
private readonly watchers;
|
|
43
|
+
private readonly recentFaults;
|
|
44
|
+
/**
|
|
45
|
+
* @param ctx - the `harness-forge` row's context.
|
|
46
|
+
* @param statePath - the ledger file.
|
|
47
|
+
* @param dataRoot - profile-local writable directory (shims, OAuth state, mod store).
|
|
48
|
+
*/
|
|
49
|
+
/** `remote`: what plugins from the network are fetched with (tests replace git, npm, fetch and the resolver). */
|
|
50
|
+
constructor(ctx: Context, statePath: string, dataRoot: string, options?: {
|
|
51
|
+
readonly remote?: {
|
|
52
|
+
readonly deps?: FetchDeps;
|
|
53
|
+
readonly limits?: FetchLimits;
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
/** A session's working directory, as hooks and commands see it. */
|
|
57
|
+
directory(agent: unknown, signal: AbortSignal): Promise<string>;
|
|
58
|
+
/** Register the mounter of one kind of component; see {@link Ledger.registerMounter}. */
|
|
59
|
+
registerMounter(kind: MountKind, mounter: Mounter): Disposer;
|
|
60
|
+
/** Offer a row's API to the other rows while the row runs. */
|
|
61
|
+
provide<K extends keyof RowApis>(name: K, api: RowApis[K]): Disposer;
|
|
62
|
+
/** A row's API, or undefined while that row is off. */
|
|
63
|
+
lookup<K extends keyof RowApis>(name: K): RowApis[K] | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* Use a row's API whenever it is there: `use` runs now if the row runs, and
|
|
66
|
+
* again each time it starts; what `use` returns is released when the row
|
|
67
|
+
* stops or the returned disposer runs.
|
|
68
|
+
*/
|
|
69
|
+
watch<K extends keyof RowApis>(name: K, use: (api: RowApis[K]) => Disposer | void): Disposer;
|
|
70
|
+
private use;
|
|
71
|
+
private release;
|
|
72
|
+
/** Record a row's failure (a listener that threw, a part that did not start) for the workbench. */
|
|
73
|
+
fault(row: string, error: unknown): void;
|
|
74
|
+
/** The latest row failures, oldest first. */
|
|
75
|
+
faults(): readonly RowFault[];
|
|
76
|
+
/** Runtime evidence for one component. */
|
|
77
|
+
observe(componentId: string, signal: string, ok: boolean, detail: string): void;
|
|
78
|
+
/** Withdraw a condition that no longer holds, such as a sign-in that completed. */
|
|
79
|
+
clearObservation(componentId: string, signal: string): void;
|
|
80
|
+
/**
|
|
81
|
+
* Restore the durable ledger, then install `preload` roots. An entry may
|
|
82
|
+
* name the dialect to read a multi-harness root as: `codex:<path>`. It
|
|
83
|
+
* takes the ledger's queue at once, so no install or removal can run
|
|
84
|
+
* before the ledger is restored (and write a ledger without its records).
|
|
85
|
+
*/
|
|
86
|
+
start(preload?: readonly string[]): Promise<void>;
|
|
87
|
+
/** Settles once every queued install, restore and re-apply (a row that just started mounting) is done. */
|
|
88
|
+
ready(): Promise<void>;
|
|
89
|
+
/** Build a pure plan for one plugin. */
|
|
90
|
+
plan(root: string, source?: string): Plan;
|
|
91
|
+
/** Apply a plan previously returned by {@link plan}. */
|
|
92
|
+
applyById(planId: string): Promise<ApplyResult>;
|
|
93
|
+
/** Apply one reviewed plan. Re-scans the root and refuses a changed source. */
|
|
94
|
+
apply(plan: Plan): Promise<ApplyResult>;
|
|
95
|
+
/** Plan and apply one plugin root. */
|
|
96
|
+
install(root: string, source?: string): Promise<InstallReport>;
|
|
97
|
+
/** Undo the actions of one receipt and remove its live registrations. */
|
|
98
|
+
undo(receiptId: string): Promise<boolean>;
|
|
99
|
+
/** Switch one part of an installed plugin off or on again; it stays so across restarts. */
|
|
100
|
+
setPartEnabled(pluginId: string, componentId: string, enabled: boolean): Promise<boolean>;
|
|
101
|
+
/** Remove one installation and every registration it owns. */
|
|
102
|
+
uninstall(id: string): Promise<boolean>;
|
|
103
|
+
/** Keep a plugin its harness no longer runs as the person's own copy: it mounts again and follows no harness. */
|
|
104
|
+
keep(id: string): Promise<ApplyResult>;
|
|
105
|
+
/** Follow the harnesses' updates and removals of migrated plugins; see `follow.ts`. Settles once applied. */
|
|
106
|
+
follow(): Promise<void>;
|
|
107
|
+
/** How far applying the ledger's records has got, while some still are (at startup, after a harness change). */
|
|
108
|
+
applying(): ApplyProgress | undefined;
|
|
109
|
+
/** Scan one plugin without writing anything. */
|
|
110
|
+
scan(root: string, source?: string): ForeignPlugin;
|
|
111
|
+
/** List current installations in stable name order. */
|
|
112
|
+
list(): readonly InstalledPluginRecord[];
|
|
113
|
+
/** Drop every runtime registration without rewriting the durable ledger. */
|
|
114
|
+
dispose(): Promise<void>;
|
|
115
|
+
}
|
|
@@ -0,0 +1,290 @@
|
|
|
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, scanRoot as scanForeignRoot, } from '@kirkchinese/harness-forge-core';
|
|
11
|
+
import { createForeignShell } from '../hooks/shell.js';
|
|
12
|
+
import { Invocations } from '../shared/invocation.js';
|
|
13
|
+
import { sessionDirectory } from '../dsh/host.js';
|
|
14
|
+
import { loggerOf, messageOf } from '../shared/util.js';
|
|
15
|
+
import { Ledger } from './ledger.js';
|
|
16
|
+
import { ProjectAssets } from './projects.js';
|
|
17
|
+
import { WorkspaceTrust } from './trust.js';
|
|
18
|
+
import { ImportedSettings } from './settings.js';
|
|
19
|
+
import { RemotePlugins } from './remote/service.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
|
+
/** Plugins fetched from the network into `<dataRoot>/remote`. */
|
|
37
|
+
remote;
|
|
38
|
+
apis = new Map();
|
|
39
|
+
watchers = new Map();
|
|
40
|
+
recentFaults = [];
|
|
41
|
+
/**
|
|
42
|
+
* @param ctx - the `harness-forge` row's context.
|
|
43
|
+
* @param statePath - the ledger file.
|
|
44
|
+
* @param dataRoot - profile-local writable directory (shims, OAuth state, mod store).
|
|
45
|
+
*/
|
|
46
|
+
/** `remote`: what plugins from the network are fetched with (tests replace git, npm, fetch and the resolver). */
|
|
47
|
+
constructor(ctx, statePath, dataRoot, options = {}) {
|
|
48
|
+
this.ctx = ctx;
|
|
49
|
+
this.statePath = statePath;
|
|
50
|
+
this.dataRoot = dataRoot;
|
|
51
|
+
this.shell = createForeignShell(process.env, dataRoot, (path) => {
|
|
52
|
+
const message = `could not remove the hook input file ${path}`;
|
|
53
|
+
loggerOf(ctx)?.warn?.(`harness-forge: ${message}`);
|
|
54
|
+
this.fault('hooks', new Error(message));
|
|
55
|
+
});
|
|
56
|
+
this.invocations = new Invocations(ctx, name => this.lookup('agents')?.presetOf(name), (agent, signal) => sessionDirectory(ctx, agent, signal));
|
|
57
|
+
this.settings = new ImportedSettings(sessionId => this.invocations.rules(sessionId));
|
|
58
|
+
this.ledger = new Ledger(ctx, statePath, dataRoot, error => this.fault('ledger', error));
|
|
59
|
+
this.projects = new ProjectAssets(this.ledger, dataRoot, new WorkspaceTrust(statePath), error => this.fault('projects', error));
|
|
60
|
+
this.remote = new RemotePlugins(join(dataRoot, 'remote'), {
|
|
61
|
+
scan: (root, source, replacing) => {
|
|
62
|
+
// The new copy of an installed plugin keeps its name, which the installed copy otherwise holds.
|
|
63
|
+
if (replacing !== undefined && this.ledger.live.has(replacing)) {
|
|
64
|
+
const plain = scanForeignRoot(root, source);
|
|
65
|
+
if (plain.id === replacing)
|
|
66
|
+
return plain;
|
|
67
|
+
}
|
|
68
|
+
return this.scan(root, source);
|
|
69
|
+
},
|
|
70
|
+
install: async (root, source) => {
|
|
71
|
+
const report = await this.install(root, source);
|
|
72
|
+
return { pluginId: report.plugin.id, report };
|
|
73
|
+
},
|
|
74
|
+
reroot: (pluginId, root, source) => this.ledger.exclusive(() => this.ledger.rerootNow(pluginId, root, source)),
|
|
75
|
+
installed: pluginId => this.ledger.live.has(pluginId),
|
|
76
|
+
}, options.remote?.deps, options.remote?.limits);
|
|
77
|
+
this.ledger.registerMounter('settings', {
|
|
78
|
+
mount: async ({ component, scope }) => {
|
|
79
|
+
if (component.kind !== 'settings')
|
|
80
|
+
throw new HarnessError('component-failed', `action expects settings: ${component.id}`);
|
|
81
|
+
const dispose = this.settings.apply(component, scope.sessionId);
|
|
82
|
+
return {
|
|
83
|
+
outcome: { componentId: component.id, status: 'mounted', level: 'L1b', evidence: 'source', ...(component.description !== undefined ? { message: component.description } : {}) },
|
|
84
|
+
disposer: async () => { dispose(); },
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
/** A session's working directory, as hooks and commands see it. */
|
|
90
|
+
directory(agent, signal) {
|
|
91
|
+
return sessionDirectory(this.ctx, agent, signal);
|
|
92
|
+
}
|
|
93
|
+
// ---------------------------------------------------------------------------
|
|
94
|
+
// Rows
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
/** Register the mounter of one kind of component; see {@link Ledger.registerMounter}. */
|
|
97
|
+
registerMounter(kind, mounter) {
|
|
98
|
+
return this.ledger.registerMounter(kind, mounter);
|
|
99
|
+
}
|
|
100
|
+
/** Offer a row's API to the other rows while the row runs. */
|
|
101
|
+
provide(name, api) {
|
|
102
|
+
if (this.apis.has(name))
|
|
103
|
+
throw new Error(`the ${name} row's API is already provided`);
|
|
104
|
+
this.apis.set(name, api);
|
|
105
|
+
for (const watcher of this.watchers.get(name) ?? [])
|
|
106
|
+
watcher.release = this.use(name, watcher.use, api);
|
|
107
|
+
return () => {
|
|
108
|
+
if (this.apis.get(name) !== api)
|
|
109
|
+
return;
|
|
110
|
+
this.apis.delete(name);
|
|
111
|
+
for (const watcher of this.watchers.get(name) ?? []) {
|
|
112
|
+
const release = watcher.release;
|
|
113
|
+
watcher.release = undefined;
|
|
114
|
+
this.release(name, release);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/** A row's API, or undefined while that row is off. */
|
|
119
|
+
lookup(name) {
|
|
120
|
+
return this.apis.get(name);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Use a row's API whenever it is there: `use` runs now if the row runs, and
|
|
124
|
+
* again each time it starts; what `use` returns is released when the row
|
|
125
|
+
* stops or the returned disposer runs.
|
|
126
|
+
*/
|
|
127
|
+
watch(name, use) {
|
|
128
|
+
const watcher = { use: use };
|
|
129
|
+
const watchers = this.watchers.get(name) ?? new Set();
|
|
130
|
+
this.watchers.set(name, watchers);
|
|
131
|
+
watchers.add(watcher);
|
|
132
|
+
const api = this.lookup(name);
|
|
133
|
+
if (api !== undefined)
|
|
134
|
+
watcher.release = this.use(name, use, api);
|
|
135
|
+
return () => {
|
|
136
|
+
watchers.delete(watcher);
|
|
137
|
+
this.release(name, watcher.release);
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
use(name, use, api) {
|
|
141
|
+
try {
|
|
142
|
+
return use(api);
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
this.fault(name, error);
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
release(name, release) {
|
|
150
|
+
try {
|
|
151
|
+
release?.();
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
this.fault(name, error);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/** Record a row's failure (a listener that threw, a part that did not start) for the workbench. */
|
|
158
|
+
fault(row, error) {
|
|
159
|
+
this.recentFaults.push({ row, at: new Date().toISOString(), message: messageOf(error) });
|
|
160
|
+
if (this.recentFaults.length > MAX_FAULTS)
|
|
161
|
+
this.recentFaults.splice(0, this.recentFaults.length - MAX_FAULTS);
|
|
162
|
+
}
|
|
163
|
+
/** The latest row failures, oldest first. */
|
|
164
|
+
faults() {
|
|
165
|
+
return [...this.recentFaults];
|
|
166
|
+
}
|
|
167
|
+
/** Runtime evidence for one component. */
|
|
168
|
+
observe(componentId, signal, ok, detail) {
|
|
169
|
+
this.ledger.observe(componentId, { signal, ok, at: new Date().toISOString(), detail });
|
|
170
|
+
}
|
|
171
|
+
/** Withdraw a condition that no longer holds, such as a sign-in that completed. */
|
|
172
|
+
clearObservation(componentId, signal) {
|
|
173
|
+
this.ledger.observe(componentId, { clear: signal });
|
|
174
|
+
}
|
|
175
|
+
// ---------------------------------------------------------------------------
|
|
176
|
+
// Ledger
|
|
177
|
+
// ---------------------------------------------------------------------------
|
|
178
|
+
/**
|
|
179
|
+
* Restore the durable ledger, then install `preload` roots. An entry may
|
|
180
|
+
* name the dialect to read a multi-harness root as: `codex:<path>`. It
|
|
181
|
+
* takes the ledger's queue at once, so no install or removal can run
|
|
182
|
+
* before the ledger is restored (and write a ledger without its records).
|
|
183
|
+
*/
|
|
184
|
+
start(preload = []) {
|
|
185
|
+
return this.ledger.exclusive(async () => {
|
|
186
|
+
await this.ledger.restoreNow();
|
|
187
|
+
for (const entry of preload) {
|
|
188
|
+
const prefix = /^([a-z][a-z0-9-]*):(.+)$/u.exec(entry);
|
|
189
|
+
const dialect = prefix !== null && dialectOf(prefix[1]) !== undefined ? prefix : undefined;
|
|
190
|
+
const root = dialect?.[2] ?? entry;
|
|
191
|
+
try {
|
|
192
|
+
await this.ledger.applyNow(this.ledger.planNow(root, dialect?.[1] ?? detectSource(resolve(root))));
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
loggerOf(this.ctx)?.warn?.(`harness-forge: preload failed for ${root}: ${messageOf(error)}`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
/** Settles once every queued install, restore and re-apply (a row that just started mounting) is done. */
|
|
201
|
+
async ready() {
|
|
202
|
+
let queue;
|
|
203
|
+
do {
|
|
204
|
+
queue = this.ledger.queue;
|
|
205
|
+
await queue;
|
|
206
|
+
} while (queue !== this.ledger.queue);
|
|
207
|
+
}
|
|
208
|
+
/** Build a pure plan for one plugin. */
|
|
209
|
+
plan(root, source) {
|
|
210
|
+
return this.ledger.planNow(root, source);
|
|
211
|
+
}
|
|
212
|
+
/** Apply a plan previously returned by {@link plan}. */
|
|
213
|
+
applyById(planId) {
|
|
214
|
+
const plan = this.ledger.plans.get(planId);
|
|
215
|
+
if (plan === undefined) {
|
|
216
|
+
return Promise.reject(new HarnessError('plan-not-found', `no plan with id ${planId}`, { suggestion: 'Scan again and review the new plan.' }));
|
|
217
|
+
}
|
|
218
|
+
return this.apply(plan);
|
|
219
|
+
}
|
|
220
|
+
/** Apply one reviewed plan. Re-scans the root and refuses a changed source. */
|
|
221
|
+
apply(plan) {
|
|
222
|
+
return this.ledger.exclusive(() => this.ledger.applyNow(plan));
|
|
223
|
+
}
|
|
224
|
+
/** Plan and apply one plugin root. */
|
|
225
|
+
async install(root, source) {
|
|
226
|
+
const result = await this.ledger.exclusive(() => this.ledger.applyNow(this.ledger.planNow(root, source)));
|
|
227
|
+
return this.ledger.report(result);
|
|
228
|
+
}
|
|
229
|
+
/** Undo the actions of one receipt and remove its live registrations. */
|
|
230
|
+
undo(receiptId) {
|
|
231
|
+
return this.ledger.exclusive(() => this.ledger.undoNow(receiptId));
|
|
232
|
+
}
|
|
233
|
+
/** Switch one part of an installed plugin off or on again; it stays so across restarts. */
|
|
234
|
+
setPartEnabled(pluginId, componentId, enabled) {
|
|
235
|
+
return this.ledger.exclusive(() => this.ledger.setEnabledNow(pluginId, componentId, enabled));
|
|
236
|
+
}
|
|
237
|
+
/** Remove one installation and every registration it owns. */
|
|
238
|
+
uninstall(id) {
|
|
239
|
+
return this.ledger.exclusive(async () => {
|
|
240
|
+
if (!await this.ledger.teardown(id))
|
|
241
|
+
return false;
|
|
242
|
+
this.ledger.persist();
|
|
243
|
+
// A plugin fetched from the network takes its files with it; a failure there leaves the removal done.
|
|
244
|
+
try {
|
|
245
|
+
this.remote.released(id);
|
|
246
|
+
}
|
|
247
|
+
catch (error) {
|
|
248
|
+
this.fault('remote', error);
|
|
249
|
+
}
|
|
250
|
+
return true;
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
/** Keep a plugin its harness no longer runs as the person's own copy: it mounts again and follows no harness. */
|
|
254
|
+
keep(id) {
|
|
255
|
+
return this.ledger.exclusive(() => this.ledger.keepNow(id));
|
|
256
|
+
}
|
|
257
|
+
/** Follow the harnesses' updates and removals of migrated plugins; see `follow.ts`. Settles once applied. */
|
|
258
|
+
follow() {
|
|
259
|
+
return this.ledger.follow();
|
|
260
|
+
}
|
|
261
|
+
/** How far applying the ledger's records has got, while some still are (at startup, after a harness change). */
|
|
262
|
+
applying() {
|
|
263
|
+
return this.ledger.applying();
|
|
264
|
+
}
|
|
265
|
+
/** Scan one plugin without writing anything. */
|
|
266
|
+
scan(root, source) {
|
|
267
|
+
return this.ledger.scan(root, source);
|
|
268
|
+
}
|
|
269
|
+
/** List current installations in stable name order. */
|
|
270
|
+
list() {
|
|
271
|
+
return this.ledger.list();
|
|
272
|
+
}
|
|
273
|
+
/** Drop every runtime registration without rewriting the durable ledger. */
|
|
274
|
+
async dispose() {
|
|
275
|
+
const ledger = this.ledger;
|
|
276
|
+
if (ledger.disposed)
|
|
277
|
+
return;
|
|
278
|
+
ledger.disposed = true;
|
|
279
|
+
await ledger.queue.catch(() => undefined);
|
|
280
|
+
if (ledger.persistTimer !== undefined) {
|
|
281
|
+
clearTimeout(ledger.persistTimer);
|
|
282
|
+
ledger.persistTimer = undefined;
|
|
283
|
+
ledger.persist();
|
|
284
|
+
}
|
|
285
|
+
// A withdrawal that fails here is recorded as a fault; the others still go.
|
|
286
|
+
for (const id of [...ledger.live.keys()])
|
|
287
|
+
await ledger.teardown(id).catch(() => undefined);
|
|
288
|
+
await this.projects.dispose();
|
|
289
|
+
}
|
|
290
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type SettingsComponent } from '@kirkchinese/harness-forge-core';
|
|
2
|
+
import type { PermissionRuleSet } from '../hooks/permissions.js';
|
|
3
|
+
/** The session facts settings scoping reads. */
|
|
4
|
+
export interface SessionHeaderLike {
|
|
5
|
+
readonly id: string;
|
|
6
|
+
readonly parentSession?: string;
|
|
7
|
+
}
|
|
8
|
+
/** The settings in effect, process-wide and per session. */
|
|
9
|
+
export declare class ImportedSettings {
|
|
10
|
+
private readonly turnRules;
|
|
11
|
+
private readonly entries;
|
|
12
|
+
/**
|
|
13
|
+
* @param turnRules - the allow and deny rules of a session's turn (an invoked skill's `allowed-tools`).
|
|
14
|
+
*/
|
|
15
|
+
constructor(turnRules: (sessionId: string) => {
|
|
16
|
+
readonly allow: readonly string[];
|
|
17
|
+
readonly deny: readonly string[];
|
|
18
|
+
});
|
|
19
|
+
/** Apply one settings component, process-wide or for one session. Returns the withdrawal. */
|
|
20
|
+
apply(component: SettingsComponent, sessionId?: string): () => void;
|
|
21
|
+
/** The `outputStyle` the process-wide settings name, the latest winning. */
|
|
22
|
+
outputStyle(): string | undefined;
|
|
23
|
+
/** The settings in effect for a session: global ones, then the session's (project) ones. */
|
|
24
|
+
private settingsFor;
|
|
25
|
+
/**
|
|
26
|
+
* Claude settings as `$.settings.read` returns them: every imported
|
|
27
|
+
* settings file in effect merged (later files win, objects merged), or one
|
|
28
|
+
* source's (`user`, `project`, `local`; `policy` is managed settings,
|
|
29
|
+
* which DSH has none of).
|
|
30
|
+
*/
|
|
31
|
+
claudeSettings(session: SessionHeaderLike | undefined, source: string | undefined): Record<string, unknown>;
|
|
32
|
+
/** The environment imported settings declare, later (project) settings winning. */
|
|
33
|
+
env(session?: SessionHeaderLike): Record<string, string>;
|
|
34
|
+
/**
|
|
35
|
+
* The Claude-syntax permission rules in effect for a session; deny from any
|
|
36
|
+
* scope holds. A rule from a settings file carries where its `/path` file
|
|
37
|
+
* patterns anchor. An invoked skill's `allowed-tools` and `disallowed-tools`
|
|
38
|
+
* count for the rest of its turn.
|
|
39
|
+
*/
|
|
40
|
+
permissionRules(session?: SessionHeaderLike): PermissionRuleSet;
|
|
41
|
+
/** `disableAllHooks` from the settings in effect, the most specific scope winning. */
|
|
42
|
+
hooksDisabled(session?: SessionHeaderLike): boolean;
|
|
43
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
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
|
+
import { leadsToShare } from '@kirkchinese/harness-forge-core';
|
|
11
|
+
/** The settings in effect, process-wide and per session. */
|
|
12
|
+
export class ImportedSettings {
|
|
13
|
+
turnRules;
|
|
14
|
+
entries = new Map();
|
|
15
|
+
/**
|
|
16
|
+
* @param turnRules - the allow and deny rules of a session's turn (an invoked skill's `allowed-tools`).
|
|
17
|
+
*/
|
|
18
|
+
constructor(turnRules) {
|
|
19
|
+
this.turnRules = turnRules;
|
|
20
|
+
}
|
|
21
|
+
/** Apply one settings component, process-wide or for one session. Returns the withdrawal. */
|
|
22
|
+
apply(component, sessionId) {
|
|
23
|
+
const key = sessionId === undefined ? component.id : `${component.id}@${sessionId}`;
|
|
24
|
+
const entry = { component, ...(sessionId !== undefined ? { sessionId } : {}) };
|
|
25
|
+
this.entries.set(key, entry);
|
|
26
|
+
return () => { if (this.entries.get(key) === entry)
|
|
27
|
+
this.entries.delete(key); };
|
|
28
|
+
}
|
|
29
|
+
/** The `outputStyle` the process-wide settings name, the latest winning. */
|
|
30
|
+
outputStyle() {
|
|
31
|
+
let style;
|
|
32
|
+
for (const { component, sessionId } of this.entries.values())
|
|
33
|
+
if (sessionId === undefined && component.outputStyle !== undefined)
|
|
34
|
+
style = component.outputStyle;
|
|
35
|
+
return style;
|
|
36
|
+
}
|
|
37
|
+
/** The settings in effect for a session: global ones, then the session's (project) ones. */
|
|
38
|
+
settingsFor(session) {
|
|
39
|
+
const entries = [...this.entries.values()];
|
|
40
|
+
const applies = (sessionId) => sessionId === undefined
|
|
41
|
+
|| (session !== undefined && (session.id === sessionId || session.parentSession === sessionId));
|
|
42
|
+
return [...entries.filter(entry => entry.sessionId === undefined), ...entries.filter(entry => entry.sessionId !== undefined && applies(entry.sessionId))];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Claude settings as `$.settings.read` returns them: every imported
|
|
46
|
+
* settings file in effect merged (later files win, objects merged), or one
|
|
47
|
+
* source's (`user`, `project`, `local`; `policy` is managed settings,
|
|
48
|
+
* which DSH has none of).
|
|
49
|
+
*/
|
|
50
|
+
claudeSettings(session, source) {
|
|
51
|
+
if (source === 'policy' || source === 'flag')
|
|
52
|
+
return {};
|
|
53
|
+
const merged = {};
|
|
54
|
+
for (const { component } of this.settingsFor(session)) {
|
|
55
|
+
for (const file of component.files) {
|
|
56
|
+
const kind = /settings\.local\.json$/u.test(file) ? 'local' : component.id.startsWith('claude-code:user') ? 'user' : 'project';
|
|
57
|
+
if (source !== undefined && kind !== source)
|
|
58
|
+
continue;
|
|
59
|
+
try {
|
|
60
|
+
// A file on a link to a network share it is not on is unreadable (see `leadsToShare`).
|
|
61
|
+
if (leadsToShare(file))
|
|
62
|
+
continue;
|
|
63
|
+
deepMerge(merged, JSON.parse(readFileSync(file, 'utf8')));
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
// An unreadable file adds nothing.
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return merged;
|
|
71
|
+
}
|
|
72
|
+
/** The environment imported settings declare, later (project) settings winning. */
|
|
73
|
+
env(session) {
|
|
74
|
+
const env = {};
|
|
75
|
+
for (const { component } of this.settingsFor(session))
|
|
76
|
+
Object.assign(env, component.env);
|
|
77
|
+
return env;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The Claude-syntax permission rules in effect for a session; deny from any
|
|
81
|
+
* scope holds. A rule from a settings file carries where its `/path` file
|
|
82
|
+
* patterns anchor. An invoked skill's `allowed-tools` and `disallowed-tools`
|
|
83
|
+
* count for the rest of its turn.
|
|
84
|
+
*/
|
|
85
|
+
permissionRules(session) {
|
|
86
|
+
const allow = [];
|
|
87
|
+
const deny = [];
|
|
88
|
+
const ask = [];
|
|
89
|
+
for (const { component } of this.settingsFor(session)) {
|
|
90
|
+
const { ruleRoot } = component;
|
|
91
|
+
const anchored = (rules) => ruleRoot === undefined ? [...rules] : rules.map(rule => ({ rule, root: ruleRoot }));
|
|
92
|
+
allow.push(...anchored(component.permissions.allow));
|
|
93
|
+
deny.push(...anchored(component.permissions.deny));
|
|
94
|
+
ask.push(...anchored(component.permissions.ask));
|
|
95
|
+
}
|
|
96
|
+
if (session !== undefined) {
|
|
97
|
+
const turn = this.turnRules(session.id);
|
|
98
|
+
allow.push(...turn.allow);
|
|
99
|
+
deny.push(...turn.deny);
|
|
100
|
+
}
|
|
101
|
+
return { allow, deny, ask };
|
|
102
|
+
}
|
|
103
|
+
/** `disableAllHooks` from the settings in effect, the most specific scope winning. */
|
|
104
|
+
hooksDisabled(session) {
|
|
105
|
+
let disabled = false;
|
|
106
|
+
for (const { component } of this.settingsFor(session))
|
|
107
|
+
if (component.disableAllHooks !== undefined)
|
|
108
|
+
disabled = component.disableAllHooks;
|
|
109
|
+
return disabled;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function deepMerge(into, from) {
|
|
113
|
+
for (const [key, value] of Object.entries(from)) {
|
|
114
|
+
const current = into[key];
|
|
115
|
+
if (typeof value === 'object' && value !== null && !Array.isArray(value) && typeof current === 'object' && current !== null && !Array.isArray(current)) {
|
|
116
|
+
deepMerge(current, value);
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
into[key] = value;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|