@agentic-sdlc-kit/cli 0.0.0-stage → 0.9.1
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 +32 -2
- package/dist/application/agent/execution-store.d.ts +55 -0
- package/dist/application/agent/execution-store.d.ts.map +1 -0
- package/dist/application/agent/execution-store.js +91 -0
- package/dist/application/agent/execution-store.js.map +1 -0
- package/dist/application/agent/gateway.d.ts +74 -0
- package/dist/application/agent/gateway.d.ts.map +1 -0
- package/dist/application/agent/gateway.js +580 -0
- package/dist/application/agent/gateway.js.map +1 -0
- package/dist/application/agent/index.d.ts +6 -0
- package/dist/application/agent/index.d.ts.map +1 -0
- package/dist/application/agent/index.js +18 -0
- package/dist/application/agent/index.js.map +1 -0
- package/dist/application/agent/orchestrator.d.ts +76 -0
- package/dist/application/agent/orchestrator.d.ts.map +1 -0
- package/dist/application/agent/orchestrator.js +422 -0
- package/dist/application/agent/orchestrator.js.map +1 -0
- package/dist/application/agent/providers/claude-code/claude-argv.d.ts +15 -0
- package/dist/application/agent/providers/claude-code/claude-argv.d.ts.map +1 -0
- package/dist/application/agent/providers/claude-code/claude-argv.js +98 -0
- package/dist/application/agent/providers/claude-code/claude-argv.js.map +1 -0
- package/dist/application/agent/providers/claude-code/index.d.ts +7 -0
- package/dist/application/agent/providers/claude-code/index.d.ts.map +1 -0
- package/dist/application/agent/providers/claude-code/index.js +13 -0
- package/dist/application/agent/providers/claude-code/index.js.map +1 -0
- package/dist/application/agent/providers/claude-code/mod-parity.d.ts +15 -0
- package/dist/application/agent/providers/claude-code/mod-parity.d.ts.map +1 -0
- package/dist/application/agent/providers/claude-code/mod-parity.js +41 -0
- package/dist/application/agent/providers/claude-code/mod-parity.js.map +1 -0
- package/dist/application/agent/providers/claude-code/permission-config.d.ts +59 -0
- package/dist/application/agent/providers/claude-code/permission-config.d.ts.map +1 -0
- package/dist/application/agent/providers/claude-code/permission-config.js +189 -0
- package/dist/application/agent/providers/claude-code/permission-config.js.map +1 -0
- package/dist/application/agent/providers/claude-code/provider.d.ts +38 -0
- package/dist/application/agent/providers/claude-code/provider.d.ts.map +1 -0
- package/dist/application/agent/providers/claude-code/provider.js +577 -0
- package/dist/application/agent/providers/claude-code/provider.js.map +1 -0
- package/dist/application/agent/providers/claude-code/resume-store.d.ts +25 -0
- package/dist/application/agent/providers/claude-code/resume-store.d.ts.map +1 -0
- package/dist/application/agent/providers/claude-code/resume-store.js +120 -0
- package/dist/application/agent/providers/claude-code/resume-store.js.map +1 -0
- package/dist/application/agent/providers/claude-code/stream-parser.d.ts +50 -0
- package/dist/application/agent/providers/claude-code/stream-parser.d.ts.map +1 -0
- package/dist/application/agent/providers/claude-code/stream-parser.js +129 -0
- package/dist/application/agent/providers/claude-code/stream-parser.js.map +1 -0
- package/dist/application/agent/test-fixture.d.ts +15 -0
- package/dist/application/agent/test-fixture.d.ts.map +1 -0
- package/dist/application/agent/test-fixture.js +93 -0
- package/dist/application/agent/test-fixture.js.map +1 -0
- package/dist/application/agent/workspace.d.ts +24 -0
- package/dist/application/agent/workspace.d.ts.map +1 -0
- package/dist/application/agent/workspace.js +139 -0
- package/dist/application/agent/workspace.js.map +1 -0
- package/dist/application/dispatcher.d.ts +13 -0
- package/dist/application/dispatcher.d.ts.map +1 -0
- package/dist/application/dispatcher.js +116 -0
- package/dist/application/dispatcher.js.map +1 -0
- package/dist/application/index.d.ts +5 -0
- package/dist/application/index.d.ts.map +1 -0
- package/dist/application/index.js +6 -0
- package/dist/application/index.js.map +1 -0
- package/dist/application/registry.d.ts +8 -0
- package/dist/application/registry.d.ts.map +1 -0
- package/dist/application/registry.js +17 -0
- package/dist/application/registry.js.map +1 -0
- package/dist/application/version.d.ts +2 -0
- package/dist/application/version.d.ts.map +1 -0
- package/dist/application/version.js +7 -0
- package/dist/application/version.js.map +1 -0
- package/dist/commands/attach-exclude.d.ts +8 -0
- package/dist/commands/attach-exclude.d.ts.map +1 -0
- package/dist/commands/attach-exclude.js +48 -0
- package/dist/commands/attach-exclude.js.map +1 -0
- package/dist/commands/attach-git.d.ts +11 -0
- package/dist/commands/attach-git.d.ts.map +1 -0
- package/dist/commands/attach-git.js +40 -0
- package/dist/commands/attach-git.js.map +1 -0
- package/dist/commands/attach-store.d.ts +10 -0
- package/dist/commands/attach-store.d.ts.map +1 -0
- package/dist/commands/attach-store.js +107 -0
- package/dist/commands/attach-store.js.map +1 -0
- package/dist/commands/attach.d.ts +8 -0
- package/dist/commands/attach.d.ts.map +1 -0
- package/dist/commands/attach.js +124 -0
- package/dist/commands/attach.js.map +1 -0
- package/dist/commands/doctor.d.ts +3 -0
- package/dist/commands/doctor.d.ts.map +1 -0
- package/dist/commands/doctor.js +68 -0
- package/dist/commands/doctor.js.map +1 -0
- package/dist/commands/elicit.d.ts +3 -0
- package/dist/commands/elicit.d.ts.map +1 -0
- package/dist/commands/elicit.js +163 -0
- package/dist/commands/elicit.js.map +1 -0
- package/dist/commands/evolve.d.ts +3 -0
- package/dist/commands/evolve.d.ts.map +1 -0
- package/dist/commands/evolve.js +165 -0
- package/dist/commands/evolve.js.map +1 -0
- package/dist/commands/feature.d.ts +13 -0
- package/dist/commands/feature.d.ts.map +1 -0
- package/dist/commands/feature.js +326 -0
- package/dist/commands/feature.js.map +1 -0
- package/dist/commands/fix.d.ts +4 -0
- package/dist/commands/fix.d.ts.map +1 -0
- package/dist/commands/fix.js +53 -0
- package/dist/commands/fix.js.map +1 -0
- package/dist/commands/help.d.ts +4 -0
- package/dist/commands/help.d.ts.map +1 -0
- package/dist/commands/help.js +32 -0
- package/dist/commands/help.js.map +1 -0
- package/dist/commands/host-apply.d.ts +12 -0
- package/dist/commands/host-apply.d.ts.map +1 -0
- package/dist/commands/host-apply.js +43 -0
- package/dist/commands/host-apply.js.map +1 -0
- package/dist/commands/host.d.ts +3 -0
- package/dist/commands/host.d.ts.map +1 -0
- package/dist/commands/host.js +107 -0
- package/dist/commands/host.js.map +1 -0
- package/dist/commands/index.d.ts +25 -0
- package/dist/commands/index.d.ts.map +1 -0
- package/dist/commands/index.js +26 -0
- package/dist/commands/index.js.map +1 -0
- package/dist/commands/init.d.ts +3 -0
- package/dist/commands/init.d.ts.map +1 -0
- package/dist/commands/init.js +92 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/inspect.d.ts +3 -0
- package/dist/commands/inspect.d.ts.map +1 -0
- package/dist/commands/inspect.js +64 -0
- package/dist/commands/inspect.js.map +1 -0
- package/dist/commands/install.d.ts +3 -0
- package/dist/commands/install.d.ts.map +1 -0
- package/dist/commands/install.js +337 -0
- package/dist/commands/install.js.map +1 -0
- package/dist/commands/journal.d.ts +3 -0
- package/dist/commands/journal.d.ts.map +1 -0
- package/dist/commands/journal.js +107 -0
- package/dist/commands/journal.js.map +1 -0
- package/dist/commands/memory.d.ts +3 -0
- package/dist/commands/memory.d.ts.map +1 -0
- package/dist/commands/memory.js +85 -0
- package/dist/commands/memory.js.map +1 -0
- package/dist/commands/new.d.ts +3 -0
- package/dist/commands/new.d.ts.map +1 -0
- package/dist/commands/new.js +68 -0
- package/dist/commands/new.js.map +1 -0
- package/dist/commands/next.d.ts +3 -0
- package/dist/commands/next.d.ts.map +1 -0
- package/dist/commands/next.js +107 -0
- package/dist/commands/next.js.map +1 -0
- package/dist/commands/optimize.d.ts +3 -0
- package/dist/commands/optimize.d.ts.map +1 -0
- package/dist/commands/optimize.js +28 -0
- package/dist/commands/optimize.js.map +1 -0
- package/dist/commands/plugin-linker.d.ts +43 -0
- package/dist/commands/plugin-linker.d.ts.map +1 -0
- package/dist/commands/plugin-linker.js +216 -0
- package/dist/commands/plugin-linker.js.map +1 -0
- package/dist/commands/refactor.d.ts +3 -0
- package/dist/commands/refactor.d.ts.map +1 -0
- package/dist/commands/refactor.js +32 -0
- package/dist/commands/refactor.js.map +1 -0
- package/dist/commands/skills-install-commit.d.ts +34 -0
- package/dist/commands/skills-install-commit.d.ts.map +1 -0
- package/dist/commands/skills-install-commit.js +113 -0
- package/dist/commands/skills-install-commit.js.map +1 -0
- package/dist/commands/skills-install-manifest.d.ts +35 -0
- package/dist/commands/skills-install-manifest.d.ts.map +1 -0
- package/dist/commands/skills-install-manifest.js +89 -0
- package/dist/commands/skills-install-manifest.js.map +1 -0
- package/dist/commands/skills-install-report.d.ts +12 -0
- package/dist/commands/skills-install-report.d.ts.map +1 -0
- package/dist/commands/skills-install-report.js +44 -0
- package/dist/commands/skills-install-report.js.map +1 -0
- package/dist/commands/skills-install-secure.d.ts +14 -0
- package/dist/commands/skills-install-secure.d.ts.map +1 -0
- package/dist/commands/skills-install-secure.js +96 -0
- package/dist/commands/skills-install-secure.js.map +1 -0
- package/dist/commands/skills-install.d.ts +15 -0
- package/dist/commands/skills-install.d.ts.map +1 -0
- package/dist/commands/skills-install.js +182 -0
- package/dist/commands/skills-install.js.map +1 -0
- package/dist/commands/skills.d.ts +3 -0
- package/dist/commands/skills.d.ts.map +1 -0
- package/dist/commands/skills.js +217 -0
- package/dist/commands/skills.js.map +1 -0
- package/dist/commands/types.d.ts +19 -0
- package/dist/commands/types.d.ts.map +1 -0
- package/dist/commands/types.js +7 -0
- package/dist/commands/types.js.map +1 -0
- package/dist/commands/uninstall.d.ts +3 -0
- package/dist/commands/uninstall.d.ts.map +1 -0
- package/dist/commands/uninstall.js +113 -0
- package/dist/commands/uninstall.js.map +1 -0
- package/dist/commands/validate-plan.d.ts +3 -0
- package/dist/commands/validate-plan.d.ts.map +1 -0
- package/dist/commands/validate-plan.js +62 -0
- package/dist/commands/validate-plan.js.map +1 -0
- package/dist/commands/validate-spec.d.ts +3 -0
- package/dist/commands/validate-spec.d.ts.map +1 -0
- package/dist/commands/validate-spec.js +62 -0
- package/dist/commands/validate-spec.js.map +1 -0
- package/dist/commands/verify.d.ts +3 -0
- package/dist/commands/verify.d.ts.map +1 -0
- package/dist/commands/verify.js +103 -0
- package/dist/commands/verify.js.map +1 -0
- package/dist/commands/version.d.ts +3 -0
- package/dist/commands/version.d.ts.map +1 -0
- package/dist/commands/version.js +12 -0
- package/dist/commands/version.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/output/formatter.d.ts +2 -0
- package/dist/output/formatter.d.ts.map +1 -0
- package/dist/output/formatter.js +10 -0
- package/dist/output/formatter.js.map +1 -0
- package/dist/output/index.d.ts +2 -0
- package/dist/output/index.d.ts.map +1 -0
- package/dist/output/index.js +3 -0
- package/dist/output/index.js.map +1 -0
- package/package.json +37 -3
- package/src/application/agent/execution-store.ts +163 -0
- package/src/application/agent/gateway.test.ts +529 -0
- package/src/application/agent/gateway.ts +744 -0
- package/src/application/agent/index.ts +18 -0
- package/src/application/agent/orchestrator.test.ts +341 -0
- package/src/application/agent/orchestrator.ts +629 -0
- package/src/application/agent/providers/claude-code/claude-argv.ts +119 -0
- package/src/application/agent/providers/claude-code/claude-code-smoke.test.ts +139 -0
- package/src/application/agent/providers/claude-code/fixtures/fake-claude.mjs +125 -0
- package/src/application/agent/providers/claude-code/index.ts +28 -0
- package/src/application/agent/providers/claude-code/mod-parity.ts +50 -0
- package/src/application/agent/providers/claude-code/permission-config.test.ts +221 -0
- package/src/application/agent/providers/claude-code/permission-config.ts +242 -0
- package/src/application/agent/providers/claude-code/provider.test.ts +378 -0
- package/src/application/agent/providers/claude-code/provider.ts +717 -0
- package/src/application/agent/providers/claude-code/resume-store.test.ts +161 -0
- package/src/application/agent/providers/claude-code/resume-store.ts +157 -0
- package/src/application/agent/providers/claude-code/stream-parser.ts +166 -0
- package/src/application/agent/test-fixture.ts +131 -0
- package/src/application/agent/workspace.test.ts +104 -0
- package/src/application/agent/workspace.ts +156 -0
- package/src/application/dispatcher.test.ts +108 -0
- package/src/application/dispatcher.ts +106 -0
- package/src/application/index.ts +5 -0
- package/src/application/registry.ts +24 -0
- package/src/application/version.ts +7 -0
- package/src/commands/attach-exclude.ts +50 -0
- package/src/commands/attach-git.ts +50 -0
- package/src/commands/attach-store.ts +107 -0
- package/src/commands/attach.test.ts +192 -0
- package/src/commands/attach.ts +143 -0
- package/src/commands/clse.test.ts +181 -0
- package/src/commands/doctor.test.ts +29 -0
- package/src/commands/doctor.ts +78 -0
- package/src/commands/elicit.test.ts +111 -0
- package/src/commands/elicit.ts +188 -0
- package/src/commands/evolve.ts +209 -0
- package/src/commands/feature.test.ts +323 -0
- package/src/commands/feature.ts +405 -0
- package/src/commands/fix.test.ts +118 -0
- package/src/commands/fix.ts +58 -0
- package/src/commands/help.test.ts +33 -0
- package/src/commands/help.ts +35 -0
- package/src/commands/host-apply.ts +61 -0
- package/src/commands/host.test.ts +120 -0
- package/src/commands/host.ts +125 -0
- package/src/commands/index.ts +25 -0
- package/src/commands/init.test.ts +87 -0
- package/src/commands/init.ts +110 -0
- package/src/commands/inspect.test.ts +57 -0
- package/src/commands/inspect.ts +73 -0
- package/src/commands/install.test.ts +92 -0
- package/src/commands/install.ts +389 -0
- package/src/commands/journal.ts +118 -0
- package/src/commands/memory.ts +96 -0
- package/src/commands/new.test.ts +44 -0
- package/src/commands/new.ts +77 -0
- package/src/commands/next.test.ts +241 -0
- package/src/commands/next.ts +124 -0
- package/src/commands/optimize.ts +30 -0
- package/src/commands/plugin-linker.test.ts +219 -0
- package/src/commands/plugin-linker.ts +244 -0
- package/src/commands/refactor.ts +34 -0
- package/src/commands/skills-install-commit.ts +149 -0
- package/src/commands/skills-install-manifest.ts +124 -0
- package/src/commands/skills-install-report.ts +60 -0
- package/src/commands/skills-install-secure.ts +113 -0
- package/src/commands/skills-install.ts +245 -0
- package/src/commands/skills.test.ts +449 -0
- package/src/commands/skills.ts +247 -0
- package/src/commands/stack-commands.test.ts +67 -0
- package/src/commands/tier-c-conformance.test.ts +194 -0
- package/src/commands/types.ts +26 -0
- package/src/commands/uninstall.test.ts +122 -0
- package/src/commands/uninstall.ts +116 -0
- package/src/commands/validate-plan.test.ts +107 -0
- package/src/commands/validate-plan.ts +67 -0
- package/src/commands/validate-spec.test.ts +59 -0
- package/src/commands/validate-spec.ts +68 -0
- package/src/commands/verify.test.ts +117 -0
- package/src/commands/verify.ts +112 -0
- package/src/commands/version.test.ts +11 -0
- package/src/commands/version.ts +14 -0
- package/src/index.ts +30 -0
- package/src/output/formatter.ts +10 -0
- package/src/output/index.ts +2 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// packages/cli/src/application/agent/workspace.test.ts
|
|
2
|
+
//
|
|
3
|
+
// Worktree workspace manager tests (ADR-0019 v2 revision B, ADR-0020 §7):
|
|
4
|
+
// a dedicated git worktree per run, with the branch + baseCommitSha
|
|
5
|
+
// identity the contract requires, and honest failure modes.
|
|
6
|
+
|
|
7
|
+
import { execFile } from 'node:child_process';
|
|
8
|
+
import { promises as fs } from 'node:fs';
|
|
9
|
+
import os from 'node:os';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { promisify } from 'node:util';
|
|
12
|
+
import { describe, expect, it } from 'vitest';
|
|
13
|
+
import {
|
|
14
|
+
createWorktreeWorkspace,
|
|
15
|
+
removeWorktreeWorkspace,
|
|
16
|
+
worktreeBranch,
|
|
17
|
+
worktreeDir,
|
|
18
|
+
workspaceExists,
|
|
19
|
+
} from './workspace.js';
|
|
20
|
+
|
|
21
|
+
const exec = promisify(execFile);
|
|
22
|
+
|
|
23
|
+
async function gitRepo(): Promise<string> {
|
|
24
|
+
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'kit-workspace-'));
|
|
25
|
+
await exec('git', ['init', '-b', 'main'], { cwd: root });
|
|
26
|
+
await fs.writeFile(path.join(root, 'file.txt'), 'baseline\n');
|
|
27
|
+
await exec(
|
|
28
|
+
'git',
|
|
29
|
+
['-c', 'user.email=k@e.com', '-c', 'user.name=k', 'add', '.'],
|
|
30
|
+
{ cwd: root },
|
|
31
|
+
);
|
|
32
|
+
await exec(
|
|
33
|
+
'git',
|
|
34
|
+
['-c', 'user.email=k@e.com', '-c', 'user.name=k', 'commit', '-m', 'init'],
|
|
35
|
+
{ cwd: root },
|
|
36
|
+
);
|
|
37
|
+
return root;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe('createWorktreeWorkspace', () => {
|
|
41
|
+
it('creates an isolated git worktree with full identity (kind/branch/baseCommitSha)', async () => {
|
|
42
|
+
const root = await gitRepo();
|
|
43
|
+
const res = await createWorktreeWorkspace(root, 'RUN-001');
|
|
44
|
+
expect(res.ok).toBe(true);
|
|
45
|
+
if (!res.ok) return;
|
|
46
|
+
expect(res.value.kind).toBe('git-worktree');
|
|
47
|
+
expect(res.value.branch).toBe('kit/RUN-001');
|
|
48
|
+
expect(res.value.rootPath).toBe(worktreeDir(root, 'RUN-001'));
|
|
49
|
+
expect(res.value.baseCommitSha).toMatch(/^[0-9a-f]{7,40}$/);
|
|
50
|
+
// A real worktree: .git is a FILE pointing at the main repo metadata.
|
|
51
|
+
const stat = await fs.stat(path.join(res.value.rootPath, '.git'));
|
|
52
|
+
expect(stat.isFile()).toBe(true);
|
|
53
|
+
// The committed baseline is present in the worktree.
|
|
54
|
+
expect(await fs.readFile(path.join(res.value.rootPath, 'file.txt'), 'utf8')).toBe('baseline\n');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('refuses to overwrite an existing workspace directory', async () => {
|
|
58
|
+
const root = await gitRepo();
|
|
59
|
+
const first = await createWorktreeWorkspace(root, 'RUN-001');
|
|
60
|
+
expect(first.ok).toBe(true);
|
|
61
|
+
const second = await createWorktreeWorkspace(root, 'RUN-001');
|
|
62
|
+
expect(second.ok).toBe(false);
|
|
63
|
+
if (!second.ok) expect(second.error.message).toContain('already exists');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('fails honestly in a directory that is not a git repository', async () => {
|
|
67
|
+
const notGit = await fs.mkdtemp(path.join(os.tmpdir(), 'kit-notgit-'));
|
|
68
|
+
const res = await createWorktreeWorkspace(notGit, 'RUN-001');
|
|
69
|
+
expect(res.ok).toBe(false);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe('workspaceExists / removeWorktreeWorkspace', () => {
|
|
74
|
+
it('probes existence and removes worktree plus branch', async () => {
|
|
75
|
+
const root = await gitRepo();
|
|
76
|
+
const res = await createWorktreeWorkspace(root, 'RUN-001');
|
|
77
|
+
expect(res.ok).toBe(true);
|
|
78
|
+
if (!res.ok) return;
|
|
79
|
+
expect(await workspaceExists(res.value)).toBe(true);
|
|
80
|
+
|
|
81
|
+
const rm = await removeWorktreeWorkspace(root, res.value);
|
|
82
|
+
expect(rm.ok).toBe(true);
|
|
83
|
+
expect(await workspaceExists(res.value)).toBe(false);
|
|
84
|
+
// The branch is gone too.
|
|
85
|
+
const branches = await exec('git', ['branch', '--list', worktreeBranch('RUN-001')], {
|
|
86
|
+
cwd: root,
|
|
87
|
+
});
|
|
88
|
+
expect(branches.stdout.trim()).toBe('');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('treats a missing workspace as absent (not an error)', async () => {
|
|
92
|
+
const root = await gitRepo();
|
|
93
|
+
expect(
|
|
94
|
+
await workspaceExists({
|
|
95
|
+
id: 'ws-gone',
|
|
96
|
+
kind: 'git-worktree',
|
|
97
|
+
rootPath: path.join(root, '.ai-kit', 'worktrees', 'RUN-404'),
|
|
98
|
+
branch: 'kit/RUN-404',
|
|
99
|
+
baseCommitSha: '0000000',
|
|
100
|
+
createdAt: new Date().toISOString(),
|
|
101
|
+
}),
|
|
102
|
+
).toBe(false);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// packages/cli/src/application/agent/workspace.ts
|
|
2
|
+
//
|
|
3
|
+
// Execution workspace manager (ADR-0019 v2 revision B, ADR-0020 §7).
|
|
4
|
+
//
|
|
5
|
+
// Every Mode 1 run executes in a DEDICATED GIT WORKTREE per workflow run,
|
|
6
|
+
// never in the developer's active working tree. This isolates repository
|
|
7
|
+
// changes and gives the orchestrator a clean diff for evidence (branch +
|
|
8
|
+
// baseCommitSha are recorded in the ExecutionWorkspace, which is exactly
|
|
9
|
+
// what the git-worktree schema variant requires).
|
|
10
|
+
//
|
|
11
|
+
// SECURITY QUALIFICATION (ADR-0020 §7, honest scope): a git worktree
|
|
12
|
+
// isolates REPOSITORY changes; it is NOT an OS sandbox. Provider
|
|
13
|
+
// processes must additionally run with restricted host permissions or in
|
|
14
|
+
// a container (PR4's concern). Nothing here claims otherwise.
|
|
15
|
+
//
|
|
16
|
+
// All git invocations are argument arrays via execFile — never a shell
|
|
17
|
+
// string, never shell:true (the kit's non-negotiable process rule).
|
|
18
|
+
|
|
19
|
+
import { execFile } from 'node:child_process';
|
|
20
|
+
import { promises as fs } from 'node:fs';
|
|
21
|
+
import path from 'node:path';
|
|
22
|
+
import { Errors, type ExecutionWorkspace, type Result } from '@agentic-sdlc-kit/contracts';
|
|
23
|
+
|
|
24
|
+
const GIT_TIMEOUT_MS = 30_000;
|
|
25
|
+
|
|
26
|
+
function execGit(args: readonly string[], cwd: string): Promise<{ stdout: string; stderr: string }> {
|
|
27
|
+
return new Promise((resolve, reject) => {
|
|
28
|
+
execFile(
|
|
29
|
+
'git',
|
|
30
|
+
[...args],
|
|
31
|
+
{ cwd, timeout: GIT_TIMEOUT_MS, windowsHide: true },
|
|
32
|
+
(err, stdout, stderr) => {
|
|
33
|
+
if (err) {
|
|
34
|
+
reject(new Error(`git ${args.join(' ')} failed: ${String(stderr || err.message)}`));
|
|
35
|
+
} else {
|
|
36
|
+
resolve({ stdout: String(stdout), stderr: String(stderr) });
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function worktreeDir(projectRoot: string, runId: string): string {
|
|
44
|
+
return path.resolve(projectRoot, '.ai-kit', 'worktrees', runId);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function worktreeBranch(runId: string): string {
|
|
48
|
+
return `kit/${runId}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function pathExists(p: string): Promise<boolean> {
|
|
52
|
+
try {
|
|
53
|
+
await fs.stat(p);
|
|
54
|
+
return true;
|
|
55
|
+
} catch {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Create the isolated execution workspace for a run: a git worktree at
|
|
62
|
+
* .ai-kit/worktrees/<runId> on branch kit/<runId>, based at the current
|
|
63
|
+
* HEAD of the project. The workspace identity (branch + baseCommitSha) is
|
|
64
|
+
* frozen in the returned ExecutionWorkspace and persisted by the
|
|
65
|
+
* orchestrator, so later phases and audits can derive clean diffs.
|
|
66
|
+
*/
|
|
67
|
+
export async function createWorktreeWorkspace(
|
|
68
|
+
projectRoot: string,
|
|
69
|
+
runId: string,
|
|
70
|
+
now: () => Date = () => new Date(),
|
|
71
|
+
): Promise<Result<ExecutionWorkspace>> {
|
|
72
|
+
const dir = worktreeDir(projectRoot, runId);
|
|
73
|
+
try {
|
|
74
|
+
if (await pathExists(dir)) {
|
|
75
|
+
return {
|
|
76
|
+
ok: false,
|
|
77
|
+
error: Errors.contractInvalid(
|
|
78
|
+
`Workspace directory ${dir} already exists — the orchestration state and the ` +
|
|
79
|
+
`filesystem have diverged; refusing to overwrite`,
|
|
80
|
+
),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
const shaRes = await execGit(['rev-parse', 'HEAD'], projectRoot);
|
|
84
|
+
const baseCommitSha = shaRes.stdout.trim();
|
|
85
|
+
if (!/^[0-9a-f]{7,40}$/.test(baseCommitSha)) {
|
|
86
|
+
return {
|
|
87
|
+
ok: false,
|
|
88
|
+
error: Errors.contractInvalid(
|
|
89
|
+
`Could not resolve a commit HEAD for ${projectRoot} — the project must have ` +
|
|
90
|
+
`at least one commit before Mode 1 execution (spec, plan, and profile must be committed)`,
|
|
91
|
+
),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
await execGit(['worktree', 'add', '-b', worktreeBranch(runId), dir, baseCommitSha], projectRoot);
|
|
95
|
+
return {
|
|
96
|
+
ok: true,
|
|
97
|
+
value: {
|
|
98
|
+
id: `ws-${runId}`,
|
|
99
|
+
kind: 'git-worktree',
|
|
100
|
+
rootPath: dir,
|
|
101
|
+
branch: worktreeBranch(runId),
|
|
102
|
+
baseCommitSha,
|
|
103
|
+
createdAt: now().toISOString(),
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
} catch (e) {
|
|
107
|
+
return {
|
|
108
|
+
ok: false,
|
|
109
|
+
error: Errors.internal(`Failed to create execution workspace: ${String(e)}`),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Probe that a persisted workspace still exists on disk. A git worktree
|
|
116
|
+
* is marked by a `.git` FILE (not a directory) pointing at the main
|
|
117
|
+
* repo's worktree metadata — that is the check.
|
|
118
|
+
*/
|
|
119
|
+
export async function workspaceExists(workspace: ExecutionWorkspace): Promise<boolean> {
|
|
120
|
+
try {
|
|
121
|
+
const gitLink = path.join(workspace.rootPath, '.git');
|
|
122
|
+
const stat = await fs.stat(gitLink);
|
|
123
|
+
return stat.isFile();
|
|
124
|
+
} catch {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Remove a worktree and its branch. Used by tests and by explicit
|
|
131
|
+
* cleanup; the orchestrator does NOT auto-remove on completion — the
|
|
132
|
+
* worktree is the evidence (the diff) and stays for review/merge.
|
|
133
|
+
*/
|
|
134
|
+
export async function removeWorktreeWorkspace(
|
|
135
|
+
projectRoot: string,
|
|
136
|
+
workspace: ExecutionWorkspace,
|
|
137
|
+
): Promise<Result<true>> {
|
|
138
|
+
try {
|
|
139
|
+
await execGit(['worktree', 'remove', '--force', workspace.rootPath], projectRoot);
|
|
140
|
+
} catch {
|
|
141
|
+
// The worktree may already be gone; prune and continue.
|
|
142
|
+
try {
|
|
143
|
+
await execGit(['worktree', 'prune'], projectRoot);
|
|
144
|
+
} catch {
|
|
145
|
+
// fall through: branch cleanup below is best-effort too
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
try {
|
|
149
|
+
if (workspace.branch) {
|
|
150
|
+
await execGit(['branch', '-D', workspace.branch], projectRoot);
|
|
151
|
+
}
|
|
152
|
+
} catch {
|
|
153
|
+
// Branch may not exist (already merged/deleted) — not an error.
|
|
154
|
+
}
|
|
155
|
+
return { ok: true, value: true };
|
|
156
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// packages/cli/src/application/dispatcher.test.ts
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { describe, expect, it } from 'vitest';
|
|
6
|
+
import { createDispatcher } from './dispatcher.js';
|
|
7
|
+
import type { CommandContext } from '../commands/types.js';
|
|
8
|
+
|
|
9
|
+
const makeCtx = (
|
|
10
|
+
argv: ReadonlyArray<string>,
|
|
11
|
+
): CommandContext & { out: string[]; err: string[] } => {
|
|
12
|
+
const out: string[] = [];
|
|
13
|
+
const err: string[] = [];
|
|
14
|
+
return {
|
|
15
|
+
argv,
|
|
16
|
+
cwd: '/tmp',
|
|
17
|
+
stdout: (s) => void out.push(s),
|
|
18
|
+
stderr: (s) => void err.push(s),
|
|
19
|
+
out,
|
|
20
|
+
err,
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
describe('dispatcher', () => {
|
|
25
|
+
it('handles --version', async () => {
|
|
26
|
+
const { dispatch } = createDispatcher();
|
|
27
|
+
const ctx = makeCtx(['--version']);
|
|
28
|
+
const r = await dispatch(ctx);
|
|
29
|
+
expect(r.exitCode).toBe(0);
|
|
30
|
+
expect(r.message).toBe('0.9.1');
|
|
31
|
+
expect(r.command).toBe('version');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('handles -v', async () => {
|
|
35
|
+
const { dispatch } = createDispatcher();
|
|
36
|
+
const ctx = makeCtx(['-v']);
|
|
37
|
+
const r = await dispatch(ctx);
|
|
38
|
+
expect(r.exitCode).toBe(0);
|
|
39
|
+
expect(r.message).toBe('0.9.1');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('handles --help', async () => {
|
|
43
|
+
const { dispatch } = createDispatcher();
|
|
44
|
+
const ctx = makeCtx(['--help']);
|
|
45
|
+
const r = await dispatch(ctx);
|
|
46
|
+
expect(r.exitCode).toBe(0);
|
|
47
|
+
expect(r.command).toBe('help');
|
|
48
|
+
// help text mentions the registered commands
|
|
49
|
+
expect(ctx.out[0]).toContain('kit — Agentic SDLC Kit');
|
|
50
|
+
expect(ctx.out[0]).toContain('version');
|
|
51
|
+
expect(ctx.out[0]).toContain('doctor');
|
|
52
|
+
expect(ctx.out[0]).toContain('inspect');
|
|
53
|
+
expect(ctx.out[0]).toContain('init');
|
|
54
|
+
expect(ctx.out[0]).toContain('validate-spec');
|
|
55
|
+
expect(ctx.out[0]).toContain('validate-plan');
|
|
56
|
+
expect(ctx.out[0]).toContain('verify');
|
|
57
|
+
expect(ctx.out[0]).toContain('feature');
|
|
58
|
+
expect(ctx.out[0]).toContain('new');
|
|
59
|
+
expect(ctx.out[0]).toContain('fix');
|
|
60
|
+
expect(ctx.out[0]).toContain('install');
|
|
61
|
+
expect(ctx.out[0]).toContain('memory');
|
|
62
|
+
expect(ctx.out[0]).toContain('evolve');
|
|
63
|
+
expect(ctx.out[0]).toContain('help');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('handles no args as help', async () => {
|
|
67
|
+
const { dispatch } = createDispatcher();
|
|
68
|
+
const ctx = makeCtx([]);
|
|
69
|
+
const r = await dispatch(ctx);
|
|
70
|
+
expect(r.exitCode).toBe(0);
|
|
71
|
+
expect(r.command).toBe('help');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('dispatches the doctor command', async () => {
|
|
75
|
+
const { dispatch } = createDispatcher();
|
|
76
|
+
const ctx = makeCtx(['doctor']);
|
|
77
|
+
const r = await dispatch(ctx);
|
|
78
|
+
expect(r.exitCode).toBe(0);
|
|
79
|
+
expect(r.command).toBe('doctor');
|
|
80
|
+
expect(ctx.out[0]).toContain('kit 0.9.1');
|
|
81
|
+
expect(ctx.out[0]).toContain('node');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('rejects unknown commands with exit code 1', async () => {
|
|
85
|
+
const { dispatch } = createDispatcher();
|
|
86
|
+
const ctx = makeCtx(['bogus']);
|
|
87
|
+
const r = await dispatch(ctx);
|
|
88
|
+
expect(r.exitCode).toBe(1);
|
|
89
|
+
expect(r.command).toBe('bogus');
|
|
90
|
+
expect(ctx.err[0]).toContain("unknown command 'bogus'");
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// The docs quote the command count in prose; it drifted (16 vs 17 vs the real
|
|
95
|
+
// registry size) because nothing tied them together. This does.
|
|
96
|
+
describe('documented command count', () => {
|
|
97
|
+
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..', '..', '..');
|
|
98
|
+
it.each([
|
|
99
|
+
['README.md', /\((\d+) commands\)/],
|
|
100
|
+
['README.md', /provides (\d+) commands/],
|
|
101
|
+
['AGENTS.md', /\((\d+) commands\)/],
|
|
102
|
+
])('%s states the registry size (%s)', (file, re) => {
|
|
103
|
+
const size = createDispatcher().registry.list().length;
|
|
104
|
+
const match = re.exec(readFileSync(path.join(root, file), 'utf8'));
|
|
105
|
+
expect(match, `${file} must state the command count`).not.toBeNull();
|
|
106
|
+
expect(Number(match![1])).toBe(size);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// packages/cli/src/application/dispatcher.ts
|
|
2
|
+
//
|
|
3
|
+
// Resolves the first non-flag argument to a registered command and runs it.
|
|
4
|
+
// Handles --help, --version, and unknown commands.
|
|
5
|
+
|
|
6
|
+
import { makeHelpCommand } from '../commands/help.js';
|
|
7
|
+
import { versionCommand } from '../commands/version.js';
|
|
8
|
+
import { doctorCommand } from '../commands/doctor.js';
|
|
9
|
+
import { inspectCommand } from '../commands/inspect.js';
|
|
10
|
+
import { initCommand } from '../commands/init.js';
|
|
11
|
+
import { validateSpecCommand } from '../commands/validate-spec.js';
|
|
12
|
+
import { elicitCommand } from '../commands/elicit.js';
|
|
13
|
+
import { validatePlanCommand } from '../commands/validate-plan.js';
|
|
14
|
+
import { verifyCommand } from '../commands/verify.js';
|
|
15
|
+
import { featureCommand } from '../commands/feature.js';
|
|
16
|
+
import { newCommand } from '../commands/new.js';
|
|
17
|
+
import { fixCommand } from '../commands/fix.js';
|
|
18
|
+
import { installCommand } from '../commands/install.js';
|
|
19
|
+
import { uninstallCommand } from '../commands/uninstall.js';
|
|
20
|
+
import { memoryCommand } from '../commands/memory.js';
|
|
21
|
+
import { evolveCommand } from '../commands/evolve.js';
|
|
22
|
+
import { refactorCommand } from '../commands/refactor.js';
|
|
23
|
+
import { optimizeCommand } from '../commands/optimize.js';
|
|
24
|
+
import { nextCommand } from '../commands/next.js';
|
|
25
|
+
import { journalCommand } from '../commands/journal.js';
|
|
26
|
+
import { skillsCommand } from '../commands/skills.js';
|
|
27
|
+
import { attachCommand } from '../commands/attach.js';
|
|
28
|
+
import { hostCommand } from '../commands/host.js';
|
|
29
|
+
import type { Command, CommandContext, CommandResult } from '../commands/types.js';
|
|
30
|
+
import { createCommandRegistry, type CommandRegistry } from './registry.js';
|
|
31
|
+
import { VERSION } from './version.js';
|
|
32
|
+
|
|
33
|
+
export interface DispatchResult extends CommandResult {
|
|
34
|
+
command?: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface DispatcherOptions {
|
|
38
|
+
registry?: CommandRegistry;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createDispatcher(opts: DispatcherOptions = {}): {
|
|
42
|
+
dispatch: (ctx: CommandContext) => Promise<DispatchResult>;
|
|
43
|
+
registry: CommandRegistry;
|
|
44
|
+
} {
|
|
45
|
+
const registry = opts.registry ?? createCommandRegistry();
|
|
46
|
+
// Register default commands. Help needs the registry so it is built lazily.
|
|
47
|
+
if (!registry.get('version')) registry.register(versionCommand);
|
|
48
|
+
if (!registry.get('doctor')) registry.register(doctorCommand);
|
|
49
|
+
if (!registry.get('inspect')) registry.register(inspectCommand);
|
|
50
|
+
if (!registry.get('init')) registry.register(initCommand);
|
|
51
|
+
if (!registry.get('validate-spec')) registry.register(validateSpecCommand);
|
|
52
|
+
if (!registry.get('elicit')) registry.register(elicitCommand);
|
|
53
|
+
if (!registry.get('validate-plan')) registry.register(validatePlanCommand);
|
|
54
|
+
if (!registry.get('verify')) registry.register(verifyCommand);
|
|
55
|
+
if (!registry.get('feature')) registry.register(featureCommand);
|
|
56
|
+
if (!registry.get('new')) registry.register(newCommand);
|
|
57
|
+
if (!registry.get('fix')) registry.register(fixCommand);
|
|
58
|
+
if (!registry.get('install')) registry.register(installCommand);
|
|
59
|
+
if (!registry.get('uninstall')) registry.register(uninstallCommand);
|
|
60
|
+
if (!registry.get('memory')) registry.register(memoryCommand);
|
|
61
|
+
if (!registry.get('evolve')) registry.register(evolveCommand);
|
|
62
|
+
if (!registry.get('refactor')) registry.register(refactorCommand);
|
|
63
|
+
if (!registry.get('optimize')) registry.register(optimizeCommand);
|
|
64
|
+
if (!registry.get('next')) registry.register(nextCommand);
|
|
65
|
+
if (!registry.get('journal')) registry.register(journalCommand);
|
|
66
|
+
if (!registry.get('skills')) registry.register(skillsCommand);
|
|
67
|
+
if (!registry.get('attach')) registry.register(attachCommand);
|
|
68
|
+
if (!registry.get('host')) registry.register(hostCommand);
|
|
69
|
+
if (!registry.get('help')) registry.register(makeHelpCommand(registry));
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
registry,
|
|
73
|
+
async dispatch(ctx: CommandContext): Promise<DispatchResult> {
|
|
74
|
+
const argv = ctx.argv;
|
|
75
|
+
// --version / -v
|
|
76
|
+
if (argv.includes('--version') || argv.includes('-v')) {
|
|
77
|
+
return { exitCode: 0, message: VERSION, command: 'version' };
|
|
78
|
+
}
|
|
79
|
+
// --help / -h with no further command, OR no args at all
|
|
80
|
+
if (argv.length === 0 || argv.includes('--help') || argv.includes('-h')) {
|
|
81
|
+
const help = registry.get('help');
|
|
82
|
+
if (!help) return { exitCode: 1, command: 'help', message: 'help command missing' };
|
|
83
|
+
const r = await help.run(ctx);
|
|
84
|
+
return { ...r, command: 'help' };
|
|
85
|
+
}
|
|
86
|
+
// First non-flag argument is the command name.
|
|
87
|
+
const first = argv.find((a) => !a.startsWith('-'));
|
|
88
|
+
if (!first) {
|
|
89
|
+
const help = registry.get('help');
|
|
90
|
+
if (help) {
|
|
91
|
+
const r = await help.run(ctx);
|
|
92
|
+
return { ...r, command: 'help' };
|
|
93
|
+
}
|
|
94
|
+
return { exitCode: 1, message: 'no command' };
|
|
95
|
+
}
|
|
96
|
+
const cmd: Command | undefined = registry.get(first);
|
|
97
|
+
if (!cmd) {
|
|
98
|
+
ctx.stderr(`kit: unknown command '${first}'\nRun 'kit --help' for usage.\n`);
|
|
99
|
+
return { exitCode: 1, command: first, message: 'unknown command' };
|
|
100
|
+
}
|
|
101
|
+
const rest = argv.filter((a) => a !== first);
|
|
102
|
+
const r = await cmd.run({ ...ctx, argv: rest });
|
|
103
|
+
return { ...r, command: first };
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// packages/cli/src/application/registry.ts
|
|
2
|
+
//
|
|
3
|
+
// Command registry. The dispatcher uses this to find a command by name.
|
|
4
|
+
// Adding a command = adding one entry here.
|
|
5
|
+
|
|
6
|
+
import type { Command } from '../commands/types.js';
|
|
7
|
+
|
|
8
|
+
export interface CommandRegistry {
|
|
9
|
+
list(): ReadonlyArray<Command>;
|
|
10
|
+
get(name: string): Command | undefined;
|
|
11
|
+
register(command: Command): void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function createCommandRegistry(initial: ReadonlyArray<Command> = []): CommandRegistry {
|
|
15
|
+
const commands = new Map<string, Command>();
|
|
16
|
+
for (const c of initial) commands.set(c.name, c);
|
|
17
|
+
return {
|
|
18
|
+
list: () => Array.from(commands.values()),
|
|
19
|
+
get: (name: string) => commands.get(name),
|
|
20
|
+
register: (command: Command) => {
|
|
21
|
+
commands.set(command.name, command);
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// packages/cli/src/application/version.ts
|
|
2
|
+
//
|
|
3
|
+
// Single source of truth for the kit version. Sourced from package.json at
|
|
4
|
+
// build time via a build-time constant. In M1 we hard-code to keep the
|
|
5
|
+
// build simple; M16 wires this to a generated file.
|
|
6
|
+
|
|
7
|
+
export const VERSION = '0.9.1';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// packages/cli/src/commands/attach-exclude.ts
|
|
2
|
+
//
|
|
3
|
+
// Managed block inside `.git/info/exclude`. Patterns carry NO trailing slash:
|
|
4
|
+
// a slash-suffixed gitignore pattern matches directories only and git treats
|
|
5
|
+
// a symlink as a file, so the symlinked (global) mode would leak.
|
|
6
|
+
|
|
7
|
+
import { promises as fs } from 'node:fs';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
|
|
10
|
+
export const BLOCK_BEGIN = '# >>> kit-attach managed block >>>';
|
|
11
|
+
export const BLOCK_END = '# <<< kit-attach managed block <<<';
|
|
12
|
+
|
|
13
|
+
export function stripBlock(text: string): string {
|
|
14
|
+
const out: string[] = [];
|
|
15
|
+
let skip = false;
|
|
16
|
+
for (const line of text.split('\n')) {
|
|
17
|
+
if (line === BLOCK_BEGIN) skip = true;
|
|
18
|
+
if (!skip) out.push(line);
|
|
19
|
+
if (line === BLOCK_END) skip = false;
|
|
20
|
+
}
|
|
21
|
+
return out.join('\n');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function withBlock(text: string, paths: readonly string[]): string {
|
|
25
|
+
const base = stripBlock(text).replace(/\n*$/, '');
|
|
26
|
+
const block = [BLOCK_BEGIN, ...paths, BLOCK_END].join('\n');
|
|
27
|
+
return (base ? base + '\n' : '') + block + '\n';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function readOrEmpty(file: string): Promise<string> {
|
|
31
|
+
try {
|
|
32
|
+
return await fs.readFile(file, 'utf8');
|
|
33
|
+
} catch {
|
|
34
|
+
return '';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function writeBlock(file: string, paths: readonly string[]): Promise<void> {
|
|
39
|
+
await fs.mkdir(path.dirname(file), { recursive: true });
|
|
40
|
+
await fs.writeFile(file, withBlock(await readOrEmpty(file), paths));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function removeBlock(file: string): Promise<void> {
|
|
44
|
+
const text = await readOrEmpty(file);
|
|
45
|
+
if (text.includes(BLOCK_BEGIN)) await fs.writeFile(file, stripBlock(text));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function hasBlock(file: string): Promise<boolean> {
|
|
49
|
+
return (await readOrEmpty(file)).includes(BLOCK_BEGIN);
|
|
50
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// packages/cli/src/commands/attach-git.ts
|
|
2
|
+
//
|
|
3
|
+
// Thin git helpers for `kit attach`. All calls use argument arrays (never a
|
|
4
|
+
// shell string) so paths with spaces or metacharacters cannot be injected.
|
|
5
|
+
|
|
6
|
+
import { execFileSync } from 'node:child_process';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
|
|
9
|
+
export interface RepoInfo {
|
|
10
|
+
/** Working-tree root of the repository (or linked worktree). */
|
|
11
|
+
root: string;
|
|
12
|
+
/** Shared git dir: where `info/exclude` must live so linked worktrees agree. */
|
|
13
|
+
commonDir: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function git(cwd: string, args: readonly string[]): string {
|
|
17
|
+
return execFileSync('git', [...args], {
|
|
18
|
+
cwd,
|
|
19
|
+
encoding: 'utf8',
|
|
20
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function resolveRepo(cwd: string): RepoInfo | null {
|
|
25
|
+
try {
|
|
26
|
+
const root = git(cwd, ['rev-parse', '--show-toplevel']).trim();
|
|
27
|
+
const common = git(root, ['rev-parse', '--git-common-dir']).trim();
|
|
28
|
+
return { root, commonDir: path.resolve(root, common) };
|
|
29
|
+
} catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function hasTrackedFiles(root: string, rel: string): boolean {
|
|
35
|
+
return git(root, ['ls-files', '--', rel]).trim().length > 0;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function isIgnored(root: string, rel: string): boolean {
|
|
39
|
+
try {
|
|
40
|
+
git(root, ['check-ignore', '-q', '--', rel]);
|
|
41
|
+
return true;
|
|
42
|
+
} catch {
|
|
43
|
+
return false; // exit 1 = not ignored
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function statusLeaks(root: string, rels: readonly string[]): string[] {
|
|
48
|
+
const out = git(root, ['status', '--porcelain', '--', ...rels]);
|
|
49
|
+
return out.split('\n').filter((l) => l.length > 0);
|
|
50
|
+
}
|