@mistriwork/acp 0.0.1 → 1.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 +10 -0
- package/dist/adapter.d.ts +32 -0
- package/dist/adapter.js +102 -0
- package/dist/cliAdapter.d.ts +54 -0
- package/dist/cliAdapter.js +197 -0
- package/dist/client.d.ts +6 -0
- package/dist/client.js +680 -0
- package/dist/env.d.ts +29 -0
- package/dist/env.js +114 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +12 -0
- package/dist/presets.d.ts +29 -0
- package/dist/presets.js +60 -0
- package/dist/stdioMcp.d.ts +37 -0
- package/dist/stdioMcp.js +61 -0
- package/dist/types.d.ts +237 -0
- package/dist/types.js +1 -0
- package/dist/worktree.d.ts +52 -0
- package/dist/worktree.js +145 -0
- package/package.json +38 -2
package/dist/worktree.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reusable workspace & Git-worktree `SandboxAdapter` implementations for
|
|
3
|
+
* coding-agent control planes built on `@mistriwork/mistri/agents` and `@mistriwork/acp`.
|
|
4
|
+
*
|
|
5
|
+
* - `createWorkspaceSandbox`: root-contained filesystem + command execution sandbox
|
|
6
|
+
* (blocks path traversal and symlink escapes outside `rootDir`).
|
|
7
|
+
* - `createWorktreeSandbox`: creates or attaches an isolated `git worktree` branch
|
|
8
|
+
* for one agent task/workspace and exposes `diff()`, `status()`, and `cleanup()`
|
|
9
|
+
* helpers beside the standard `SandboxAdapter` methods.
|
|
10
|
+
*/
|
|
11
|
+
import { execFile } from 'node:child_process';
|
|
12
|
+
import { mkdir, readFile, realpath, rm, writeFile } from 'node:fs/promises';
|
|
13
|
+
import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
14
|
+
function runGit(args, cwd, timeoutMs = 30_000) {
|
|
15
|
+
return new Promise((done, fail) => {
|
|
16
|
+
execFile('git', [...args], { cwd, timeout: timeoutMs, maxBuffer: 4 * 1024 * 1024 }, (error, stdout, stderr) => {
|
|
17
|
+
if (error) {
|
|
18
|
+
fail(new Error(`git ${args.join(' ')} failed: ${(stderr || error.message).trim()}`));
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
done({ stdout: String(stdout ?? ''), stderr: String(stderr ?? '') });
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function safeBranchSlug(branch) {
|
|
26
|
+
return branch.replace(/[^A-Za-z0-9._-]+/g, '-').replace(/^-+|-+$/g, '') || 'worktree';
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Create a root-contained `SandboxAdapter` backed by local filesystem I/O and
|
|
30
|
+
* `/bin/sh -c` command execution, blocking path traversal and symlink escapes.
|
|
31
|
+
*/
|
|
32
|
+
export function createWorkspaceSandbox(options) {
|
|
33
|
+
const root = resolve(options.rootDir);
|
|
34
|
+
const defaultTimeoutMs = options.defaultTimeoutMs ?? 30_000;
|
|
35
|
+
const maxBuffer = options.maxBufferBytes ?? 2 * 1024 * 1024;
|
|
36
|
+
const containPath = async (rawPath, forWrite) => {
|
|
37
|
+
if (!rawPath || rawPath.includes('\0'))
|
|
38
|
+
throw new Error('Invalid sandbox path.');
|
|
39
|
+
const resolvedRoot = await realpath(root).catch(() => root);
|
|
40
|
+
const candidate = isAbsolute(rawPath) ? resolve(rawPath) : resolve(resolvedRoot, rawPath);
|
|
41
|
+
const rel = relative(resolvedRoot, candidate);
|
|
42
|
+
if (rel.startsWith('..') || isAbsolute(rel)) {
|
|
43
|
+
throw new Error(`Path "${rawPath}" escapes workspace root "${resolvedRoot}".`);
|
|
44
|
+
}
|
|
45
|
+
if (forWrite) {
|
|
46
|
+
const parent = dirname(candidate);
|
|
47
|
+
await mkdir(parent, { recursive: true });
|
|
48
|
+
const resolvedParent = await realpath(parent);
|
|
49
|
+
const parentRel = relative(resolvedRoot, resolvedParent);
|
|
50
|
+
if (parentRel.startsWith('..') || isAbsolute(parentRel)) {
|
|
51
|
+
throw new Error(`Resolved parent for "${rawPath}" escapes workspace root.`);
|
|
52
|
+
}
|
|
53
|
+
return resolve(resolvedParent, candidate.slice(parent.length + 1));
|
|
54
|
+
}
|
|
55
|
+
const real = await realpath(candidate);
|
|
56
|
+
const realRel = relative(resolvedRoot, real);
|
|
57
|
+
if (realRel.startsWith('..') || isAbsolute(realRel)) {
|
|
58
|
+
throw new Error(`Resolved path for "${rawPath}" escapes workspace root.`);
|
|
59
|
+
}
|
|
60
|
+
return real;
|
|
61
|
+
};
|
|
62
|
+
return Object.freeze({
|
|
63
|
+
rootDir: root,
|
|
64
|
+
async readFile(filePath) {
|
|
65
|
+
const target = await containPath(filePath, false);
|
|
66
|
+
return readFile(target, 'utf8');
|
|
67
|
+
},
|
|
68
|
+
async writeFile(filePath, content) {
|
|
69
|
+
const target = await containPath(filePath, true);
|
|
70
|
+
await writeFile(target, content, 'utf8');
|
|
71
|
+
},
|
|
72
|
+
async exec(command, execOptions) {
|
|
73
|
+
const cwd = execOptions?.cwd ? await containPath(execOptions.cwd, false) : root;
|
|
74
|
+
const timeout = execOptions?.timeoutMs ?? defaultTimeoutMs;
|
|
75
|
+
return new Promise((done) => {
|
|
76
|
+
execFile('/bin/sh', ['-c', command], { cwd, timeout, maxBuffer, signal: execOptions?.signal }, (error, stdout, stderr) => {
|
|
77
|
+
const exitCode = error && typeof error.code === 'number'
|
|
78
|
+
? Number(error.code)
|
|
79
|
+
: error
|
|
80
|
+
? 1
|
|
81
|
+
: 0;
|
|
82
|
+
done({
|
|
83
|
+
stdout: String(stdout ?? ''),
|
|
84
|
+
stderr: String(stderr ?? ''),
|
|
85
|
+
exitCode,
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Create an isolated Git worktree for a coding-agent session and return a
|
|
94
|
+
* root-contained `WorktreeSandbox` (`SandboxAdapter` + `diff`, `status`, `cleanup`).
|
|
95
|
+
*/
|
|
96
|
+
export async function createWorktreeSandbox(options) {
|
|
97
|
+
const repoDir = resolve(options.repoDir);
|
|
98
|
+
const branch = options.branch.trim();
|
|
99
|
+
if (!branch)
|
|
100
|
+
throw new Error('createWorktreeSandbox requires a non-empty branch name.');
|
|
101
|
+
const worktreeDir = resolve(options.worktreeDir ?? join(repoDir, '.mistri-worktrees', safeBranchSlug(branch)));
|
|
102
|
+
await mkdir(dirname(worktreeDir), { recursive: true });
|
|
103
|
+
const branchExists = await runGit(['rev-parse', '--verify', `refs/heads/${branch}`], repoDir)
|
|
104
|
+
.then(() => true)
|
|
105
|
+
.catch(() => false);
|
|
106
|
+
if (branchExists) {
|
|
107
|
+
await runGit(['worktree', 'add', worktreeDir, branch], repoDir);
|
|
108
|
+
}
|
|
109
|
+
else if (options.createBranch !== false) {
|
|
110
|
+
await runGit(['worktree', 'add', '-b', branch, worktreeDir, options.baseRef ?? 'HEAD'], repoDir);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
throw new Error(`Branch "${branch}" does not exist and createBranch is false.`);
|
|
114
|
+
}
|
|
115
|
+
const base = createWorkspaceSandbox({ rootDir: worktreeDir, defaultTimeoutMs: options.defaultTimeoutMs });
|
|
116
|
+
return Object.freeze({
|
|
117
|
+
rootDir: worktreeDir,
|
|
118
|
+
repoDir,
|
|
119
|
+
branch,
|
|
120
|
+
readFile: base.readFile,
|
|
121
|
+
writeFile: base.writeFile,
|
|
122
|
+
exec: base.exec,
|
|
123
|
+
async diff(baseRef) {
|
|
124
|
+
const args = baseRef ? ['diff', baseRef] : ['diff'];
|
|
125
|
+
return (await runGit(args, worktreeDir)).stdout;
|
|
126
|
+
},
|
|
127
|
+
async status() {
|
|
128
|
+
const { stdout } = await runGit(['status', '--porcelain'], worktreeDir);
|
|
129
|
+
return stdout
|
|
130
|
+
.split('\n')
|
|
131
|
+
.map((line) => line.trimEnd())
|
|
132
|
+
.filter(Boolean);
|
|
133
|
+
},
|
|
134
|
+
async cleanup(cleanupOpts) {
|
|
135
|
+
const forceFlag = cleanupOpts?.force === false ? [] : ['--force'];
|
|
136
|
+
await runGit(['worktree', 'remove', ...forceFlag, worktreeDir], repoDir).catch(async () => {
|
|
137
|
+
await rm(worktreeDir, { recursive: true, force: true });
|
|
138
|
+
await runGit(['worktree', 'prune'], repoDir).catch(() => undefined);
|
|
139
|
+
});
|
|
140
|
+
if (cleanupOpts?.deleteBranch) {
|
|
141
|
+
await runGit(['branch', '-D', branch], repoDir).catch(() => undefined);
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mistriwork/acp",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.4.0",
|
|
4
|
+
"description": "Agent Client Protocol (ACP) host runtime, stdio MCP client, and coding-agent presets (Claude Code, Codex, Gemini) for mistri Apps, wired to SandboxAdapter, PermissionBroker, and scoped MCP bridges with zero extra runtime dependencies.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"mistri",
|
|
7
|
+
"acp",
|
|
8
|
+
"agent-client-protocol",
|
|
9
|
+
"mcp",
|
|
10
|
+
"coding-agents",
|
|
11
|
+
"claude-code",
|
|
12
|
+
"codex",
|
|
13
|
+
"gemini"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://mistriwork.dev",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/rbaljinder/mistri/issues"
|
|
18
|
+
},
|
|
5
19
|
"license": "MIT",
|
|
20
|
+
"author": "Baljinder Randhawa",
|
|
6
21
|
"repository": {
|
|
7
22
|
"type": "git",
|
|
8
23
|
"url": "git+https://github.com/rbaljinder/mistri.git",
|
|
9
24
|
"directory": "packages/acp"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"type": "module",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"default": "./dist/index.js"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@mistriwork/engine": "1.4.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@mistriwork/mistri": "1.4.0"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=24"
|
|
10
46
|
}
|
|
11
47
|
}
|