@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,45 @@
|
|
|
1
|
+
import type { MountKind } from './contract.js';
|
|
2
|
+
/** One mounted action that is live. */
|
|
3
|
+
export interface LiveMount {
|
|
4
|
+
readonly componentId: string;
|
|
5
|
+
readonly kind: MountKind;
|
|
6
|
+
readonly dispose: () => Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
/** What a release did: the mounts withdrawn, and those whose withdrawal failed (they stay live). */
|
|
9
|
+
export interface Release {
|
|
10
|
+
readonly released: LiveMount[];
|
|
11
|
+
readonly failed: Array<{
|
|
12
|
+
readonly actionId: string;
|
|
13
|
+
readonly mount: LiveMount;
|
|
14
|
+
readonly reason: string;
|
|
15
|
+
}>;
|
|
16
|
+
}
|
|
17
|
+
/** A plugin's live mounts, by plan action id. */
|
|
18
|
+
export declare class LiveMounts {
|
|
19
|
+
private readonly fault?;
|
|
20
|
+
private readonly mounts;
|
|
21
|
+
/** Withdrawals running now, by action id: an overlapping release waits for the same one. */
|
|
22
|
+
private readonly withdrawing;
|
|
23
|
+
/** Why the last withdrawal of a mount still live failed, by action id. */
|
|
24
|
+
private readonly failures;
|
|
25
|
+
/** @param fault - where a withdrawal that failed is recorded. */
|
|
26
|
+
constructor(fault?: ((error: unknown) => void) | undefined);
|
|
27
|
+
/** Whether the action has a live registration (one being withdrawn, or whose withdrawal failed, still counts). */
|
|
28
|
+
has(actionId: string): boolean;
|
|
29
|
+
/** Why the last withdrawal of a live action failed, if it did. */
|
|
30
|
+
failure(actionId: string): string | undefined;
|
|
31
|
+
/** The mounts still live because their last withdrawal failed, with why. */
|
|
32
|
+
stuck(): Array<{
|
|
33
|
+
readonly componentId: string;
|
|
34
|
+
readonly reason: string;
|
|
35
|
+
}>;
|
|
36
|
+
/** Register a mount. An action still live is never replaced: withdraw it first. */
|
|
37
|
+
add(actionId: string, mount: LiveMount): void;
|
|
38
|
+
get size(): number;
|
|
39
|
+
/**
|
|
40
|
+
* Withdraw the mounts that match, each once. A mount leaves the set only
|
|
41
|
+
* when its disposer succeeds; one that fails is recorded as a fault and
|
|
42
|
+
* stays, so a later release retries it. A failing disposer does not stop the others.
|
|
43
|
+
*/
|
|
44
|
+
release(match?: (mount: LiveMount, actionId: string) => boolean): Promise<Release>;
|
|
45
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The live registrations of one installed plugin: each mounted action's
|
|
3
|
+
* withdrawal, kept with the component and kind it mounted. Withdrawing reads
|
|
4
|
+
* this snapshot only, so a source that moved, changed or no longer scans is
|
|
5
|
+
* still torn down.
|
|
6
|
+
* @module @kirkchinese/harness-forge
|
|
7
|
+
*/
|
|
8
|
+
import { messageOf } from '../shared/util.js';
|
|
9
|
+
/** A plugin's live mounts, by plan action id. */
|
|
10
|
+
export class LiveMounts {
|
|
11
|
+
fault;
|
|
12
|
+
mounts = new Map();
|
|
13
|
+
/** Withdrawals running now, by action id: an overlapping release waits for the same one. */
|
|
14
|
+
withdrawing = new Map();
|
|
15
|
+
/** Why the last withdrawal of a mount still live failed, by action id. */
|
|
16
|
+
failures = new Map();
|
|
17
|
+
/** @param fault - where a withdrawal that failed is recorded. */
|
|
18
|
+
constructor(fault) {
|
|
19
|
+
this.fault = fault;
|
|
20
|
+
}
|
|
21
|
+
/** Whether the action has a live registration (one being withdrawn, or whose withdrawal failed, still counts). */
|
|
22
|
+
has(actionId) {
|
|
23
|
+
return this.mounts.has(actionId);
|
|
24
|
+
}
|
|
25
|
+
/** Why the last withdrawal of a live action failed, if it did. */
|
|
26
|
+
failure(actionId) {
|
|
27
|
+
return this.failures.get(actionId);
|
|
28
|
+
}
|
|
29
|
+
/** The mounts still live because their last withdrawal failed, with why. */
|
|
30
|
+
stuck() {
|
|
31
|
+
return [...this.failures].flatMap(([actionId, reason]) => {
|
|
32
|
+
const mount = this.mounts.get(actionId);
|
|
33
|
+
return mount === undefined ? [] : [{ componentId: mount.componentId, reason }];
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
/** Register a mount. An action still live is never replaced: withdraw it first. */
|
|
37
|
+
add(actionId, mount) {
|
|
38
|
+
if (this.mounts.has(actionId))
|
|
39
|
+
throw new Error(`${actionId} is still live: withdraw it first`);
|
|
40
|
+
this.mounts.set(actionId, mount);
|
|
41
|
+
}
|
|
42
|
+
get size() {
|
|
43
|
+
return this.mounts.size;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Withdraw the mounts that match, each once. A mount leaves the set only
|
|
47
|
+
* when its disposer succeeds; one that fails is recorded as a fault and
|
|
48
|
+
* stays, so a later release retries it. A failing disposer does not stop the others.
|
|
49
|
+
*/
|
|
50
|
+
async release(match = () => true) {
|
|
51
|
+
const result = { released: [], failed: [] };
|
|
52
|
+
for (const [actionId, mount] of [...this.mounts]) {
|
|
53
|
+
if (!match(mount, actionId))
|
|
54
|
+
continue;
|
|
55
|
+
let withdrawal = this.withdrawing.get(actionId);
|
|
56
|
+
if (withdrawal === undefined) {
|
|
57
|
+
withdrawal = mount.dispose().then(() => {
|
|
58
|
+
if (this.mounts.get(actionId) === mount)
|
|
59
|
+
this.mounts.delete(actionId);
|
|
60
|
+
this.failures.delete(actionId);
|
|
61
|
+
return undefined;
|
|
62
|
+
}, (error) => {
|
|
63
|
+
this.fault?.(error);
|
|
64
|
+
const reason = messageOf(error);
|
|
65
|
+
this.failures.set(actionId, reason);
|
|
66
|
+
return reason;
|
|
67
|
+
}).finally(() => { this.withdrawing.delete(actionId); });
|
|
68
|
+
this.withdrawing.set(actionId, withdrawal);
|
|
69
|
+
}
|
|
70
|
+
const reason = await withdrawal;
|
|
71
|
+
if (reason === undefined)
|
|
72
|
+
result.released.push(mount);
|
|
73
|
+
else
|
|
74
|
+
result.failed.push({ actionId, mount, reason });
|
|
75
|
+
}
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Git marketplace source fetcher. It is deliberately narrow: clone a URL,
|
|
3
|
+
* optionally pin a ref/SHA, cache by content key, and return a local plugin
|
|
4
|
+
* root. No arbitrary command sources and no lifecycle scripts are executed.
|
|
5
|
+
* @module @kirkchinese/harness-forge
|
|
6
|
+
*/
|
|
7
|
+
import { createHash } from 'node:crypto';
|
|
8
|
+
import { execFile } from 'node:child_process';
|
|
9
|
+
import { existsSync, mkdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
10
|
+
import { join, resolve } from 'node:path';
|
|
11
|
+
import { promisify } from 'node:util';
|
|
12
|
+
import { isInside, HarnessError, } from '@kirkchinese/harness-forge-core';
|
|
13
|
+
import { messageOf } from '../shared/util.js';
|
|
14
|
+
const run = promisify(execFile);
|
|
15
|
+
/** Fetch one git marketplace source and return the plugin root inside it. */
|
|
16
|
+
export async function fetchGitSource(source, cacheRoot) {
|
|
17
|
+
if (source.kind !== 'git' || source.url === undefined) {
|
|
18
|
+
throw new HarnessError('marketplace-failed', 'not a git marketplace source');
|
|
19
|
+
}
|
|
20
|
+
const url = source.url;
|
|
21
|
+
if (!/^(?:https:\/\/|file:\/\/|ssh:\/\/|git@)/u.test(url)) {
|
|
22
|
+
throw new HarnessError('marketplace-failed', `unsupported git URL: ${url}`);
|
|
23
|
+
}
|
|
24
|
+
const key = createHash('sha256')
|
|
25
|
+
.update(url)
|
|
26
|
+
.update('\0')
|
|
27
|
+
.update(source.ref ?? '')
|
|
28
|
+
.update('\0')
|
|
29
|
+
.update(source.sha ?? '')
|
|
30
|
+
.digest('hex')
|
|
31
|
+
.slice(0, 24);
|
|
32
|
+
const target = join(cacheRoot, key);
|
|
33
|
+
if (!existsSync(target)) {
|
|
34
|
+
mkdirSync(cacheRoot, { recursive: true });
|
|
35
|
+
const temporary = `${target}.tmp-${String(process.pid)}`;
|
|
36
|
+
rmSync(temporary, { recursive: true, force: true });
|
|
37
|
+
try {
|
|
38
|
+
const args = ['clone', '--depth', '1'];
|
|
39
|
+
if (source.ref !== undefined && source.sha === undefined)
|
|
40
|
+
args.push('--branch', source.ref);
|
|
41
|
+
args.push(url, temporary);
|
|
42
|
+
await run('git', args, { timeout: 120_000, windowsHide: true, maxBuffer: 4 * 1024 * 1024 });
|
|
43
|
+
if (source.sha !== undefined) {
|
|
44
|
+
await run('git', ['-C', temporary, 'fetch', '--depth', '1', 'origin', source.sha], { timeout: 120_000, windowsHide: true });
|
|
45
|
+
await run('git', ['-C', temporary, 'checkout', '--detach', 'FETCH_HEAD'], { timeout: 60_000, windowsHide: true });
|
|
46
|
+
}
|
|
47
|
+
writeFileSync(join(temporary, '.harness-forge-source.json'), `${JSON.stringify({ url, ref: source.ref, sha: source.sha, fetchedAt: new Date().toISOString() }, null, 2)}\n`, 'utf8');
|
|
48
|
+
rmSync(target, { recursive: true, force: true });
|
|
49
|
+
renameSync(temporary, target);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
rmSync(temporary, { recursive: true, force: true });
|
|
53
|
+
throw new HarnessError('marketplace-failed', `git clone failed: ${messageOf(error)}`, {
|
|
54
|
+
cause: error,
|
|
55
|
+
suggestion: 'Check the repository URL, ref/SHA, and network access.',
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const pluginRoot = source.subpath === undefined ? target : resolve(target, source.subpath);
|
|
60
|
+
if (!isInside(target, pluginRoot)) {
|
|
61
|
+
throw new HarnessError('path-escape', `marketplace subpath escapes the repository: ${source.subpath ?? ''}`);
|
|
62
|
+
}
|
|
63
|
+
return pluginRoot;
|
|
64
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** What to spawn, without a shell: a program and its arguments. */
|
|
2
|
+
export interface Launch {
|
|
3
|
+
readonly command: string;
|
|
4
|
+
readonly args: readonly string[];
|
|
5
|
+
}
|
|
6
|
+
/** What {@link npmLaunch} reads from the machine. */
|
|
7
|
+
export interface NpmEnvironment {
|
|
8
|
+
readonly platform: NodeJS.Platform;
|
|
9
|
+
/** The running Node's executable. */
|
|
10
|
+
readonly execPath: string;
|
|
11
|
+
/** `npm_execpath` when the process was started by npm. */
|
|
12
|
+
readonly npmExecPath?: string;
|
|
13
|
+
readonly exists: (path: string) => boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* How to run `npm <args>` without a shell. Windows ships npm as `npm.cmd`, which Node refuses to spawn without
|
|
17
|
+
* one (since the 2024 security fix), so npm's own JavaScript CLI runs under the running Node: from `npm_execpath`
|
|
18
|
+
* when npm started this process, else from `node_modules/npm` beside `node.exe` (the Node installer's layout).
|
|
19
|
+
* Elsewhere `npm` is an executable script and runs as it is.
|
|
20
|
+
*/
|
|
21
|
+
export declare function npmLaunch(args: readonly string[], env?: NpmEnvironment): Launch;
|
|
22
|
+
/** The arguments of `npm install` for `specs`: no install scripts, no audit or funding chatter. */
|
|
23
|
+
export declare function npmInstallArgs(specs: readonly string[]): string[];
|
|
24
|
+
/**
|
|
25
|
+
* `npm install <specs>` into `prefix`. The prefix gets a `package.json` of its
|
|
26
|
+
* own first: without one npm walks up to the nearest `package.json`, which
|
|
27
|
+
* under a DSH profile is the profile's, and installs there.
|
|
28
|
+
*/
|
|
29
|
+
export declare function npmInstall(specs: readonly string[], prefix: string, timeoutMs?: number): Promise<void>;
|
|
30
|
+
/** Run a program to its end; rejects with the tail of its output when it fails. */
|
|
31
|
+
export declare function runProcess(command: string, args: readonly string[], cwd: string, timeoutMs: number): Promise<void>;
|
package/lib/forge/npm.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* npm installs into a directory of Harness Forge's own, outside the profile's
|
|
3
|
+
* `node_modules`: what Harness Forge fetches on first use (Bun, the mods
|
|
4
|
+
* bundler) never goes through the profile's pnpm.
|
|
5
|
+
* @module @kirkchinese/harness-forge
|
|
6
|
+
*/
|
|
7
|
+
import { spawn } from 'node:child_process';
|
|
8
|
+
import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
|
|
9
|
+
import { join, win32 } from 'node:path';
|
|
10
|
+
/**
|
|
11
|
+
* How to run `npm <args>` without a shell. Windows ships npm as `npm.cmd`, which Node refuses to spawn without
|
|
12
|
+
* one (since the 2024 security fix), so npm's own JavaScript CLI runs under the running Node: from `npm_execpath`
|
|
13
|
+
* when npm started this process, else from `node_modules/npm` beside `node.exe` (the Node installer's layout).
|
|
14
|
+
* Elsewhere `npm` is an executable script and runs as it is.
|
|
15
|
+
*/
|
|
16
|
+
export function npmLaunch(args, env = { platform: process.platform, execPath: process.execPath, ...(process.env['npm_execpath'] !== undefined ? { npmExecPath: process.env['npm_execpath'] } : {}), exists: existsSync }) {
|
|
17
|
+
if (env.platform !== 'win32')
|
|
18
|
+
return { command: 'npm', args };
|
|
19
|
+
const beside = win32.join(win32.dirname(env.execPath), 'node_modules', 'npm', 'bin', 'npm-cli.js');
|
|
20
|
+
const cli = [...(env.npmExecPath !== undefined && /\.c?js$/iu.test(env.npmExecPath) && /npm-cli/iu.test(env.npmExecPath) ? [env.npmExecPath] : []), beside].find(candidate => env.exists(candidate));
|
|
21
|
+
if (cli === undefined)
|
|
22
|
+
throw new Error(`npm's command line (npm-cli.js) was not found beside ${env.execPath}`);
|
|
23
|
+
return { command: env.execPath, args: [cli, ...args] };
|
|
24
|
+
}
|
|
25
|
+
/** The arguments of `npm install` for `specs`: no install scripts, no audit or funding chatter. */
|
|
26
|
+
export function npmInstallArgs(specs) {
|
|
27
|
+
return ['install', ...specs, '--ignore-scripts', '--no-audit', '--no-fund'];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* `npm install <specs>` into `prefix`. The prefix gets a `package.json` of its
|
|
31
|
+
* own first: without one npm walks up to the nearest `package.json`, which
|
|
32
|
+
* under a DSH profile is the profile's, and installs there.
|
|
33
|
+
*/
|
|
34
|
+
export async function npmInstall(specs, prefix, timeoutMs = 600_000) {
|
|
35
|
+
// cwd must exist: a missing cwd makes spawn report ENOENT for npm.
|
|
36
|
+
mkdirSync(prefix, { recursive: true });
|
|
37
|
+
const manifest = join(prefix, 'package.json');
|
|
38
|
+
if (!existsSync(manifest))
|
|
39
|
+
writeFileSync(manifest, `${JSON.stringify({ name: 'harness-forge-tools', private: true }, null, 2)}\n`);
|
|
40
|
+
const npm = npmLaunch(npmInstallArgs(specs));
|
|
41
|
+
await runProcess(npm.command, npm.args, prefix, timeoutMs);
|
|
42
|
+
}
|
|
43
|
+
/** Run a program to its end; rejects with the tail of its output when it fails. */
|
|
44
|
+
export function runProcess(command, args, cwd, timeoutMs) {
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
46
|
+
const child = spawn(command, args, { cwd, windowsHide: true });
|
|
47
|
+
let output = '';
|
|
48
|
+
child.stdout?.on('data', (chunk) => { output = (output + chunk.toString()).slice(-4000); });
|
|
49
|
+
child.stderr?.on('data', (chunk) => { output = (output + chunk.toString()).slice(-4000); });
|
|
50
|
+
const timer = setTimeout(() => { child.kill(); }, timeoutMs);
|
|
51
|
+
child.on('error', (error) => { clearTimeout(timer); reject(error); });
|
|
52
|
+
child.on('close', (code) => {
|
|
53
|
+
clearTimeout(timer);
|
|
54
|
+
if (code === 0)
|
|
55
|
+
resolve();
|
|
56
|
+
else
|
|
57
|
+
reject(new Error(`${command} ${args.join(' ')} exited with ${String(code)}: ${output.trim().split(/\r?\n/u).slice(-4).join(' | ')}`));
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { type ComponentOutcome, type ForeignComponent, type ForeignPlugin } from '@kirkchinese/harness-forge-core';
|
|
2
|
+
import { type Ledger } from './ledger.js';
|
|
3
|
+
import { type WorkspaceTrust, type WorkspaceTrustState } from './trust.js';
|
|
4
|
+
interface AgentLike {
|
|
5
|
+
readonly ctx?: unknown;
|
|
6
|
+
readonly session: {
|
|
7
|
+
readonly header: {
|
|
8
|
+
readonly id: string;
|
|
9
|
+
readonly cwd?: string;
|
|
10
|
+
readonly delegationDepth?: number;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
/** The project assets one session runs with. */
|
|
15
|
+
export interface ProjectSession {
|
|
16
|
+
readonly sessionId: string;
|
|
17
|
+
readonly root: string;
|
|
18
|
+
readonly plugin: ForeignPlugin;
|
|
19
|
+
readonly outcomes: readonly ComponentOutcome[];
|
|
20
|
+
}
|
|
21
|
+
/** A project part that waits for the workspace to be trusted (or its MCP server approved). */
|
|
22
|
+
export interface WaitingPart {
|
|
23
|
+
readonly pluginId: string;
|
|
24
|
+
readonly componentId: string;
|
|
25
|
+
readonly kind: ForeignComponent['kind'];
|
|
26
|
+
readonly name: string;
|
|
27
|
+
/** What it would do: run hook commands, start an MCP server, … */
|
|
28
|
+
readonly does: string;
|
|
29
|
+
readonly message: string;
|
|
30
|
+
}
|
|
31
|
+
export type { ProjectRecord } from '../dsh/session-records.js';
|
|
32
|
+
/** The parts of a directory's project assets that wait for trust (or approval) now. */
|
|
33
|
+
export declare function waitingParts(cwd: string, trust: WorkspaceTrustState): WaitingPart[];
|
|
34
|
+
/**
|
|
35
|
+
* Per-session project assets, mounted through the same mounters as installed
|
|
36
|
+
* plugins, in the session's scope. Parts that run code or reach the network
|
|
37
|
+
* wait until the workspace is trusted; trusting it here mounts them in the
|
|
38
|
+
* sessions already open in it.
|
|
39
|
+
*/
|
|
40
|
+
export declare class ProjectAssets {
|
|
41
|
+
private readonly ledger;
|
|
42
|
+
private readonly dataRoot;
|
|
43
|
+
readonly trust: WorkspaceTrust;
|
|
44
|
+
private readonly fault?;
|
|
45
|
+
private readonly mounts;
|
|
46
|
+
/** Each session's mounts, once its first mount finished. */
|
|
47
|
+
private readonly settled;
|
|
48
|
+
/** Mounts of ended sessions whose withdrawal failed: kept, and withdrawn again on the next release or at dispose. */
|
|
49
|
+
private readonly retained;
|
|
50
|
+
/** Mounting, reconciling and releasing run one at a time, in the order they were asked for. */
|
|
51
|
+
private chain;
|
|
52
|
+
/** Bumped on every trust change: a mount that was running across one is stale. */
|
|
53
|
+
private generation;
|
|
54
|
+
private readonly stopTrust;
|
|
55
|
+
constructor(ledger: Ledger, dataRoot: string, trust: WorkspaceTrust, fault?: ((error: unknown) => void) | undefined);
|
|
56
|
+
/** A directory's trust now, with the generation it belongs to: read together, with no await between. */
|
|
57
|
+
private snapshot;
|
|
58
|
+
/** Run `task` after everything asked for before it. */
|
|
59
|
+
private serial;
|
|
60
|
+
/**
|
|
61
|
+
* Trust (or stop trusting) a workspace in Harness Forge, and return once its
|
|
62
|
+
* open sessions have mounted or withdrawn what changed, with the parts in it
|
|
63
|
+
* whose withdrawal failed: they are still live, and setting trust again retries.
|
|
64
|
+
*/
|
|
65
|
+
setTrust(cwd: string, trusted: boolean): Promise<{
|
|
66
|
+
decision: WorkspaceTrustState;
|
|
67
|
+
stillLive: Array<{
|
|
68
|
+
readonly componentId: string;
|
|
69
|
+
readonly reason: string;
|
|
70
|
+
}>;
|
|
71
|
+
}>;
|
|
72
|
+
/**
|
|
73
|
+
* Mount the project assets of a new top-level session, once: every caller
|
|
74
|
+
* (the `harness-forge` row, the hooks row before SessionStart) waits for
|
|
75
|
+
* the same mount.
|
|
76
|
+
*/
|
|
77
|
+
prepare(agent: AgentLike, cwd: string): Promise<void>;
|
|
78
|
+
/** The workspaces of the open sessions, with their trust and what waits for it. */
|
|
79
|
+
workspaces(): Promise<Array<WorkspaceTrustState & {
|
|
80
|
+
readonly cwd: string;
|
|
81
|
+
readonly waiting: readonly WaitingPart[];
|
|
82
|
+
}>>;
|
|
83
|
+
private mountAll;
|
|
84
|
+
/**
|
|
85
|
+
* Trust changed for a workspace: mount what now may run in its open
|
|
86
|
+
* sessions, and withdraw what no longer may (trust revoked).
|
|
87
|
+
*/
|
|
88
|
+
private reconcile;
|
|
89
|
+
/** Mount the given parts of a session's project assets that may run; the others wait. */
|
|
90
|
+
private mountParts;
|
|
91
|
+
/** Withdraw a session's project assets. */
|
|
92
|
+
release(sessionId: string): Promise<void>;
|
|
93
|
+
dispose(): Promise<void>;
|
|
94
|
+
}
|