@parcha/agentrun-harness-acp 0.1.0-beta.46 → 0.1.0-beta.47

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/dist/state.js ADDED
@@ -0,0 +1,157 @@
1
+ // The agent's own session store, mirrored into the session's records and given back to the next agent process. ACP has
2
+ // no hook before a paid request, so the mirror is taken at the points the adapter sees: after session/new, after each
3
+ // finished tool call, and at each turn's end. Written for AgentRun.
4
+ // - Each file under the recipe's `state` paths is one record of STATE_STREAM: its path in HOME, its sha256, and its bytes
5
+ // (base64), or `deleted`. Only a file whose bytes changed since the last record is written again; a large one goes to
6
+ // the journal's blob stream on its own (NativeJournal offloads it).
7
+ // - A file whose name says it holds a credential (a token, a key, an auth or credentials file) is never mirrored, and the
8
+ // recipe's own files (written fresh at every start) and the credentials it names (whatever their names) are not either.
9
+ // - The next process's HOME gets the newest record of each path back, before the agent starts.
10
+ import { createHash } from "node:crypto";
11
+ import { lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
12
+ import { dirname, join, relative, sep } from "node:path";
13
+ /** The journal stream that holds the agent's session store. */
14
+ export const STATE_STREAM = "acp.state";
15
+ /** A file the mirror never takes: its name says it holds a credential. */
16
+ export const CREDENTIAL_FILE = /(^|[._-])(credentials?|tokens?|auth|secrets?|keyring|keychain|cookies?)([._-]|$)|\.(pem|key|p12)$/i;
17
+ /** The most bytes one file may have to be mirrored; a larger one is skipped, said in the log. */
18
+ export const MAX_STATE_FILE = 16 * 1024 * 1024;
19
+ const sha = (bytes) => createHash("sha256").update(bytes).digest("hex");
20
+ /** A file or directory the agent removed (or replaced with a file) while the walk read it: gone, never an error. */
21
+ const gone = (error) => ["ENOENT", "ENOTDIR"].includes(error?.code ?? "");
22
+ /** `read()`, or undefined when what it reads is gone. Any other error is thrown: the mirror says nothing it did not read. */
23
+ function unlessGone(read) {
24
+ try {
25
+ return read();
26
+ }
27
+ catch (error) {
28
+ if (gone(error))
29
+ return undefined;
30
+ throw error;
31
+ }
32
+ }
33
+ /** Whether `path` (in HOME, POSIX separators) is one of `skip` or inside one of them. */
34
+ const skipped = (path, skip) => skip.has(path) || [...skip].some((each) => path.startsWith(`${each}/`));
35
+ /** Every file under `paths` in `home`, by its path in HOME (POSIX separators). A link is never followed: a state path
36
+ * whose directories in HOME include one (the agent made `.agent/store` a link out of HOME) is skipped whole, and a link
37
+ * inside the walk is neither a file nor a directory. */
38
+ function filesUnder(home, paths, skip, log) {
39
+ const out = new Map();
40
+ const visit = (absolute) => {
41
+ const stat = unlessGone(() => lstatSync(absolute));
42
+ if (!stat)
43
+ return;
44
+ const path = relative(home, absolute).split(sep).join("/");
45
+ if (stat.isDirectory()) {
46
+ for (const name of (unlessGone(() => readdirSync(absolute)) ?? []).sort())
47
+ visit(join(absolute, name));
48
+ return;
49
+ }
50
+ if (!stat.isFile() || skipped(path, skip) || CREDENTIAL_FILE.test(path.split("/").at(-1) ?? ""))
51
+ return;
52
+ if (stat.size > MAX_STATE_FILE) {
53
+ log("acp: a state file too large to mirror was skipped", { path, bytes: stat.size });
54
+ return;
55
+ }
56
+ // A directory on the way swapped for a link after the check below: the file's own resolved path says so.
57
+ const real = unlessGone(() => realpathSync(absolute));
58
+ if (!real)
59
+ return;
60
+ if (!realHome || !real.startsWith(realHome + sep)) {
61
+ log("acp: a state file outside HOME was skipped", { path });
62
+ return;
63
+ }
64
+ const bytes = unlessGone(() => readFileSync(absolute));
65
+ if (bytes)
66
+ out.set(path, bytes);
67
+ };
68
+ const realHome = unlessGone(() => realpathSync(home));
69
+ /** Whether each directory between HOME and the state path's last part is a directory of HOME's own, not a link. */
70
+ const withinHome = (parts) => {
71
+ let at = home;
72
+ for (const part of parts.slice(0, -1)) {
73
+ at = join(at, part);
74
+ const stat = unlessGone(() => lstatSync(at));
75
+ if (!stat)
76
+ return true; // Not there: the walk finds nothing under it.
77
+ if (!stat.isDirectory())
78
+ return false;
79
+ }
80
+ return true;
81
+ };
82
+ for (const path of paths) {
83
+ const parts = path.split(/[\\/]/).filter(Boolean);
84
+ if (!withinHome(parts)) {
85
+ log("acp: a state path that leaves HOME through a link was skipped", { path });
86
+ continue;
87
+ }
88
+ visit(join(home, ...parts));
89
+ }
90
+ return out;
91
+ }
92
+ /** The newest record of each path, as the records hold them. */
93
+ export function foldState(records) {
94
+ const out = new Map();
95
+ for (const record of records) {
96
+ if (!record || typeof record.path !== "string")
97
+ continue;
98
+ if (record.deleted)
99
+ out.delete(record.path);
100
+ else
101
+ out.set(record.path, record);
102
+ }
103
+ return out;
104
+ }
105
+ /** Write the mirrored files back into `home`. */
106
+ export function restoreState(home, records) {
107
+ for (const [path, record] of foldState(records)) {
108
+ if (record.data === undefined || path.split("/").includes(".."))
109
+ continue;
110
+ const file = join(home, ...path.split("/"));
111
+ mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
112
+ writeFileSync(file, Buffer.from(record.data, "base64"), { mode: 0o600 });
113
+ }
114
+ }
115
+ /** The mirror of one agent process's HOME: what changed since the records' newest, appended to `journal`. */
116
+ export class StateMirror {
117
+ home;
118
+ paths;
119
+ skip;
120
+ log;
121
+ known = new Map();
122
+ constructor(home, paths,
123
+ /** Paths in HOME never mirrored, each a file or a folder: the recipe's own files (written at every start) and the
124
+ * credentials it names. */
125
+ skip, records, log = () => { }) {
126
+ this.home = home;
127
+ this.paths = paths;
128
+ this.skip = skip;
129
+ this.log = log;
130
+ for (const [path, record] of foldState(records))
131
+ if (record.sha256)
132
+ this.known.set(path, record.sha256);
133
+ }
134
+ /** Append what changed to `journal` (the caller flushes). Returns how many records it appended. */
135
+ take(journal, stream = STATE_STREAM) {
136
+ if (this.paths.length === 0)
137
+ return 0;
138
+ const now = filesUnder(this.home, this.paths, this.skip, this.log);
139
+ let appended = 0;
140
+ for (const [path, bytes] of now) {
141
+ const digest = sha(bytes);
142
+ if (this.known.get(path) === digest)
143
+ continue;
144
+ journal.append(stream, { path, sha256: digest, data: bytes.toString("base64") });
145
+ this.known.set(path, digest);
146
+ appended++;
147
+ }
148
+ for (const path of [...this.known.keys()]) {
149
+ if (now.has(path))
150
+ continue;
151
+ journal.append(stream, { path, deleted: true });
152
+ this.known.delete(path);
153
+ appended++;
154
+ }
155
+ return appended;
156
+ }
157
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcha/agentrun-harness-acp",
3
- "version": "0.1.0-beta.46",
3
+ "version": "0.1.0-beta.47",
4
4
  "description": "Any Agent Client Protocol agent as an AgentRun harness: the agent over stdio in a private HOME, the host's tools as one loopback MCP server, its permission asks through the host's gate, durable in AgentRun's own store.",
5
5
  "keywords": [
6
6
  "agentrun",
@@ -44,7 +44,7 @@
44
44
  },
45
45
  "dependencies": {
46
46
  "@agentclientprotocol/sdk": "2.7.0",
47
- "@parcha/agentrun-kernel": "0.1.0-beta.46",
47
+ "@parcha/agentrun-kernel": "0.1.0-beta.47",
48
48
  "zod": "4.6.6"
49
49
  },
50
50
  "publishConfig": {
@@ -0,0 +1,21 @@
1
+ {
2
+ "id": "cursor",
3
+ "label": "Cursor",
4
+ "args": ["--disable-auto-update", "acp"],
5
+ "keys": ["CURSOR_API_KEY"],
6
+ "files": {
7
+ ".config/cursor/cli-config.json":"{\"version\":1,\"permissions\":{\"allow\":[],\"deny\":[\"Write(**)\"]},\"approvalMode\":\"allowlist\"}\n"
8
+ },
9
+ "state": [".config/cursor/acp-sessions"],
10
+ "credentials": [".config/cursor/auth.json"],
11
+ "refusals": [
12
+ {
13
+ "says": "disabled headless Cursor CLI usage",
14
+ "code": "CURSOR_HEADLESS_DISABLED",
15
+ "words": "your Cursor team administrator has disabled headless Cursor CLI usage, so AgentRun does not run Cursor on this key; ask them to allow it in Cursor's team settings"
16
+ }
17
+ ],
18
+ "quiet": ["couldn't save your credentials", "EISDIR: illegal operation on a directory, open"],
19
+ "declares": { "load": true, "mcp": "http" },
20
+ "billing": { "kind": "plan", "maxTurns": 20, "maxToolCalls": 100 }
21
+ }