benignswarm 0.12.115 → 0.12.116
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/config.js +2 -2
- package/dist/directories.js +7 -178
- package/dist/harness.js +33 -5
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/config.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { CliError } from "./errors.js";
|
|
4
|
-
import { findIdentityFile, readIdentityFile } from "./harness.js";
|
|
4
|
+
import { findIdentityFile, readIdentityFile, identityWalkStoppedAt } from "./harness.js";
|
|
5
5
|
import { resolveModelDetailed } from "./model.js";
|
|
6
6
|
import { readState, writeState } from "./state.js";
|
|
7
7
|
import { detectVendor } from "./vendor.js";
|
|
@@ -164,7 +164,7 @@ export function resolveAgentName(asFlag, env = process.env, cwd = process.cwd())
|
|
|
164
164
|
}
|
|
165
165
|
if (!name) {
|
|
166
166
|
const saved = listSaved("agents", env);
|
|
167
|
-
throw new CliError("no_identity",
|
|
167
|
+
throw new CliError("no_identity", `BSCHAT_AGENT is not set and --as was not given; b9s cannot tell which agent you are${identityWalkStoppedAt() ? ` (it looked for this checkout's identity file up to ${identityWalkStoppedAt()}, where it is not permitted to look)` : ""}`, saved.length
|
|
168
168
|
? `Saved identities on this machine: ${saved.join(", ")}. Ask the user which one is yours, then: export BSCHAT_AGENT=<project>/<name> (or run: b9s setup --as <project>/<name> to record it for this repo)`
|
|
169
169
|
: "No identities are saved here. Ask the account owner for an invite (an enrollment token) and run: npx -y benignswarm join --as <name> --token <bs_enroll_...>");
|
|
170
170
|
}
|
package/dist/directories.js
CHANGED
|
@@ -1,186 +1,15 @@
|
|
|
1
|
-
import { closeSync, existsSync, lstatSync, openSync, readFileSync, readdirSync, readlinkSync, readSync, realpathSync, renameSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
2
1
|
import { homedir } from "node:os";
|
|
3
2
|
import { join } from "node:path";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
catch (error) {
|
|
10
|
-
const code = error.code;
|
|
11
|
-
if (code === "ENOENT" || code === "ENOTDIR")
|
|
12
|
-
return null;
|
|
13
|
-
throw error;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
function directory(path) { return maybeLstat(path)?.isDirectory() ?? false; }
|
|
17
|
-
function present(path) { return maybeLstat(path) !== null; }
|
|
18
|
-
function treeEntries(root) {
|
|
19
|
-
const entries = new Map();
|
|
20
|
-
const visit = (path, relative) => {
|
|
21
|
-
const st = lstatSync(path);
|
|
22
|
-
if (st.isDirectory()) {
|
|
23
|
-
for (const name of readdirSync(path).sort())
|
|
24
|
-
visit(join(path, name), relative ? relative + "/" + name : name);
|
|
25
|
-
}
|
|
26
|
-
else if (st.isFile())
|
|
27
|
-
entries.set(relative, { kind: "file", path, size: st.size });
|
|
28
|
-
else if (st.isSymbolicLink())
|
|
29
|
-
entries.set(relative, { kind: "symlink", target: readlinkSync(path) });
|
|
30
|
-
else
|
|
31
|
-
entries.set(relative, { kind: "other", mode: st.mode });
|
|
32
|
-
};
|
|
33
|
-
visit(root, "");
|
|
34
|
-
return entries;
|
|
35
|
-
}
|
|
36
|
-
function sameFileBytes(left, right, size) {
|
|
37
|
-
const a = openSync(left, "r");
|
|
38
|
-
let b;
|
|
39
|
-
try {
|
|
40
|
-
b = openSync(right, "r");
|
|
41
|
-
const leftChunk = Buffer.allocUnsafe(64 * 1024);
|
|
42
|
-
const rightChunk = Buffer.allocUnsafe(64 * 1024);
|
|
43
|
-
let position = 0;
|
|
44
|
-
while (position < size) {
|
|
45
|
-
const expected = Math.min(leftChunk.length, size - position);
|
|
46
|
-
const nLeft = readSync(a, leftChunk, 0, expected, position);
|
|
47
|
-
const nRight = readSync(b, rightChunk, 0, expected, position);
|
|
48
|
-
if (nLeft !== nRight || nLeft === 0 || !leftChunk.subarray(0, nLeft).equals(rightChunk.subarray(0, nRight)))
|
|
49
|
-
return false;
|
|
50
|
-
position += nLeft;
|
|
51
|
-
}
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
finally {
|
|
55
|
-
closeSync(a);
|
|
56
|
-
if (b !== undefined)
|
|
57
|
-
closeSync(b);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
function differingFiles(left, right) {
|
|
61
|
-
const a = treeEntries(left);
|
|
62
|
-
const b = treeEntries(right);
|
|
63
|
-
return [...new Set([...a.keys(), ...b.keys()])].sort().filter((name) => {
|
|
64
|
-
const x = a.get(name);
|
|
65
|
-
const y = b.get(name);
|
|
66
|
-
if (!x || !y || x.kind !== y.kind)
|
|
67
|
-
return true;
|
|
68
|
-
if (x.kind === "file" && y.kind === "file")
|
|
69
|
-
return x.size !== y.size || !sameFileBytes(x.path, y.path, x.size);
|
|
70
|
-
if (x.kind === "symlink" && y.kind === "symlink")
|
|
71
|
-
return x.target !== y.target;
|
|
72
|
-
return x.kind === "other" && y.kind === "other" && x.mode !== y.mode;
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
function shellQuote(value) { return "'" + value.replace(/'/g, "'\\''") + "'"; }
|
|
76
|
-
function conflictHint(current, old, currentName) {
|
|
77
|
-
let backup = old + ".conflict";
|
|
78
|
-
for (let n = 2; existsSync(backup); n++)
|
|
79
|
-
backup = old + ".conflict-" + n;
|
|
80
|
-
return "After reviewing the differences, keep " + current + " as the active copy and preserve the other at " + backup + ": mv -- " + shellQuote(old) + " " + shellQuote(backup) + " && ln -s -- " + shellQuote(currentName) + " " + shellQuote(old);
|
|
81
|
-
}
|
|
82
|
-
function conflictFiles(current, old, names, currentName) {
|
|
83
|
-
const details = names.map((name) => {
|
|
84
|
-
const a = join(current, name);
|
|
85
|
-
const b = join(old, name);
|
|
86
|
-
return (present(a) ? a : a + " (missing)") + " <> " + (present(b) ? b : b + " (missing)");
|
|
87
|
-
});
|
|
88
|
-
return new CliError("directory_conflict", "both " + current + " and " + old + " are real directories with different files: " + details.join("; "), conflictHint(current, old, currentName));
|
|
89
|
-
}
|
|
90
|
-
let quarantineSequence = 0;
|
|
91
|
-
function quarantinePath(old) {
|
|
92
|
-
let path;
|
|
93
|
-
do {
|
|
94
|
-
path = old + ".b9s-migrating-" + process.pid + "-" + quarantineSequence++;
|
|
95
|
-
} while (existsSync(path));
|
|
96
|
-
return path;
|
|
97
|
-
}
|
|
98
|
-
/** Resolve a renamed state directory, preserving old paths through a symlink. */
|
|
99
|
-
export function renamedDirectory(parent, currentName, oldName, report = true) {
|
|
100
|
-
const current = join(parent, currentName);
|
|
101
|
-
const old = join(parent, oldName);
|
|
102
|
-
const currentDir = directory(current);
|
|
103
|
-
const oldDir = directory(old);
|
|
104
|
-
const currentExists = present(current);
|
|
105
|
-
const oldExists = present(old);
|
|
106
|
-
if (currentDir && oldDir) {
|
|
107
|
-
const different = differingFiles(current, old);
|
|
108
|
-
if (different.length)
|
|
109
|
-
throw conflictFiles(current, old, different, currentName);
|
|
110
|
-
const quarantine = quarantinePath(old);
|
|
111
|
-
renameSync(old, quarantine);
|
|
112
|
-
const changedDuringMove = differingFiles(current, quarantine);
|
|
113
|
-
if (changedDuringMove.length) {
|
|
114
|
-
if (!present(old))
|
|
115
|
-
renameSync(quarantine, old);
|
|
116
|
-
throw conflictFiles(current, present(old) ? old : quarantine, changedDuringMove, currentName);
|
|
117
|
-
}
|
|
118
|
-
try {
|
|
119
|
-
symlinkSync(currentName, old, "dir");
|
|
120
|
-
}
|
|
121
|
-
catch (error) {
|
|
122
|
-
if (!present(old))
|
|
123
|
-
renameSync(quarantine, old);
|
|
124
|
-
throw error;
|
|
125
|
-
}
|
|
126
|
-
rmSync(quarantine, { recursive: true });
|
|
127
|
-
if (report)
|
|
128
|
-
process.stderr.write("[b9s] kept identical " + current + "; removed duplicate " + old + "; " + oldName + " now links to " + currentName + "\n");
|
|
129
|
-
}
|
|
130
|
-
else if (!currentExists && oldDir) {
|
|
131
|
-
renameSync(old, current);
|
|
132
|
-
symlinkSync(currentName, old, "dir");
|
|
133
|
-
if (report)
|
|
134
|
-
process.stderr.write(`[b9s] moved ${old} to ${current}; ${oldName} now links to ${currentName}\n`);
|
|
135
|
-
}
|
|
136
|
-
else if (currentExists && oldDir) {
|
|
137
|
-
throw new CliError("directory_conflict", `${current} and ${old} both exist, but ${current} is not a directory; refusing to move or replace either path`);
|
|
138
|
-
}
|
|
139
|
-
else if (currentExists && !currentDir) {
|
|
140
|
-
throw new CliError("directory_conflict", `${current} exists but is not a directory; refusing to use it as b9s state`);
|
|
141
|
-
}
|
|
142
|
-
else if (oldExists && !currentDir) {
|
|
143
|
-
throw new CliError("directory_conflict", `${old} exists but is not a directory; refusing to replace it with a ${oldName} compatibility link`);
|
|
144
|
-
}
|
|
145
|
-
else if (oldExists && currentDir) {
|
|
146
|
-
try {
|
|
147
|
-
const st = lstatSync(old);
|
|
148
|
-
if (!st.isSymbolicLink())
|
|
149
|
-
throw new Error("not a symbolic link");
|
|
150
|
-
const resolved = realpathSync(old);
|
|
151
|
-
if (resolved !== realpathSync(current))
|
|
152
|
-
throw new Error("points elsewhere");
|
|
153
|
-
}
|
|
154
|
-
catch {
|
|
155
|
-
throw new CliError("directory_conflict", `both ${current} and ${old} exist and ${old} is not a symlink to ${current}; refusing to choose`);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
return current;
|
|
159
|
-
}
|
|
3
|
+
/**
|
|
4
|
+
* Where b9s keeps its state. The names are fixed: ~/.b9s for the machine and <checkout>/.b9s for a
|
|
5
|
+
* checkout. Nothing is discovered, inspected or migrated here, so a session sandbox that grants
|
|
6
|
+
* these two folders grants everything b9s reads to find them.
|
|
7
|
+
*/
|
|
160
8
|
export function configDirectory(env = process.env) {
|
|
161
9
|
if (env.BSCHAT_HOME !== undefined || env.B9S_HOME !== undefined)
|
|
162
10
|
return env.BSCHAT_HOME ?? env.B9S_HOME;
|
|
163
|
-
|
|
164
|
-
return renamedDirectory(home, ".b9s", ".bschat");
|
|
11
|
+
return join(env.HOME ?? homedir(), ".b9s");
|
|
165
12
|
}
|
|
166
13
|
export function repoDirectory(root) {
|
|
167
|
-
|
|
168
|
-
const oldPath = join(root, ".bschat");
|
|
169
|
-
const currentBefore = maybeLstat(currentPath);
|
|
170
|
-
const oldBefore = maybeLstat(oldPath);
|
|
171
|
-
const migration = oldBefore?.isDirectory() === true && currentBefore === null;
|
|
172
|
-
const identicalCopies = oldBefore?.isDirectory() === true && currentBefore?.isDirectory() === true;
|
|
173
|
-
const compatibilityLink = oldBefore?.isSymbolicLink() === true && currentBefore?.isDirectory() === true;
|
|
174
|
-
const current = renamedDirectory(root, ".b9s", ".bschat");
|
|
175
|
-
if ((migration || identicalCopies || compatibilityLink) && existsSync(join(root, ".git"))) {
|
|
176
|
-
const ignore = join(root, ".gitignore");
|
|
177
|
-
const contents = existsSync(ignore) ? readFileSync(ignore, "utf8") : "";
|
|
178
|
-
const lines = contents.split("\n");
|
|
179
|
-
const missing = [".b9s/", ".bschat"].filter((entry) => !lines.includes(entry));
|
|
180
|
-
if (missing.length) {
|
|
181
|
-
const prefix = contents && !contents.endsWith("\n") ? `${contents}\n` : contents;
|
|
182
|
-
writeFileSync(ignore, `${prefix}${prefix ? "\n" : ""}# b9s (per-checkout agent state)\n${missing.join("\n")}\n`);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
return current;
|
|
14
|
+
return join(root, ".b9s");
|
|
186
15
|
}
|
package/dist/harness.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { shellPath } from "./install.js";
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
4
|
import { basename, dirname, join } from "node:path";
|
|
@@ -17,13 +17,41 @@ export function normalizeVendor(v) {
|
|
|
17
17
|
return VENDOR_ALIASES[v.toLowerCase()] ?? v.toLowerCase();
|
|
18
18
|
}
|
|
19
19
|
export const IDENTITY_FILE = join(".b9s", "identity.json");
|
|
20
|
-
/**
|
|
20
|
+
/** Where the last walk had to stop because the process may not look in a folder; null when it reached the top. */
|
|
21
|
+
let identityWalkDenied = null;
|
|
22
|
+
export function identityWalkStoppedAt() { return identityWalkDenied; }
|
|
23
|
+
/** Whether `dir` holds an identity file. "denied": this process may not look there. */
|
|
24
|
+
function identityFileIn(dir) {
|
|
25
|
+
try {
|
|
26
|
+
return statSync(join(dir, IDENTITY_FILE)).isFile();
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
const code = error.code;
|
|
30
|
+
if (code === "EPERM" || code === "EACCES")
|
|
31
|
+
return "denied";
|
|
32
|
+
if (code === "ENOENT" || code === "ENOTDIR")
|
|
33
|
+
return false;
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Walks up from cwd to find the repo-local identity file (.b9s/identity.json). The walk only
|
|
39
|
+
* looks; it resolves and migrates nothing. It used to resolve a state directory in every ancestor,
|
|
40
|
+
* which inspected the home folder from inside a session sandbox and failed there. A folder the
|
|
41
|
+
* process may not look in ends the walk: an identity file it cannot read is not one it can use,
|
|
42
|
+
* and the place is kept for the error a caller raises when it needed a name and found none.
|
|
43
|
+
*/
|
|
21
44
|
export function findIdentityFile(start = process.cwd()) {
|
|
45
|
+
identityWalkDenied = null;
|
|
22
46
|
let dir = start;
|
|
23
47
|
for (;;) {
|
|
24
|
-
const
|
|
25
|
-
if (
|
|
26
|
-
return
|
|
48
|
+
const here = identityFileIn(dir);
|
|
49
|
+
if (here === true)
|
|
50
|
+
return join(dir, IDENTITY_FILE);
|
|
51
|
+
if (here === "denied") {
|
|
52
|
+
identityWalkDenied = dir;
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
27
55
|
const parent = dirname(dir);
|
|
28
56
|
if (parent === dir)
|
|
29
57
|
return null;
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const CLI_VERSION = "0.12.
|
|
1
|
+
export const CLI_VERSION = "0.12.116";
|