pi-cowboy 0.3.4 → 0.3.7
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/logger.js +6 -8
- package/dist/spawn/crockford-base32.js +252 -0
- package/dist/spawn/spawn-id.js +1 -1
- package/package.json +2 -4
package/dist/logger.js
CHANGED
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
import { appendFileSync, mkdirSync } from "node:fs";
|
|
6
6
|
import { join } from "node:path";
|
|
7
7
|
import { CONFIG_DIR_NAME } from "@earendil-works/pi-coding-agent";
|
|
8
|
-
import { getDetailedString } from "caller-id";
|
|
9
8
|
const LEVELS = ["debug", "info", "warn", "error"];
|
|
10
9
|
let minimumLevel = parseLevel(process.env.PI_COWBOY_LOG_LEVEL);
|
|
11
10
|
const logDir = join(process.cwd(), CONFIG_DIR_NAME);
|
|
@@ -15,16 +14,16 @@ export function setLogLevel(level) {
|
|
|
15
14
|
}
|
|
16
15
|
export function createLogger(module) {
|
|
17
16
|
function debug(msg, data) {
|
|
18
|
-
write("debug", module, msg, data
|
|
17
|
+
write("debug", module, msg, data);
|
|
19
18
|
}
|
|
20
19
|
function info(msg, data) {
|
|
21
|
-
write("info", module, msg, data
|
|
20
|
+
write("info", module, msg, data);
|
|
22
21
|
}
|
|
23
22
|
function warn(msg, data) {
|
|
24
|
-
write("warn", module, msg, data
|
|
23
|
+
write("warn", module, msg, data);
|
|
25
24
|
}
|
|
26
25
|
function error(msg, data) {
|
|
27
|
-
write("error", module, msg, data
|
|
26
|
+
write("error", module, msg, data);
|
|
28
27
|
}
|
|
29
28
|
return { debug, info, warn, error };
|
|
30
29
|
}
|
|
@@ -34,12 +33,11 @@ function parseLevel(value) {
|
|
|
34
33
|
? normalized
|
|
35
34
|
: "info";
|
|
36
35
|
}
|
|
37
|
-
function write(level, module, msg, data
|
|
36
|
+
function write(level, module, msg, data) {
|
|
38
37
|
if (LEVELS.indexOf(level) < LEVELS.indexOf(minimumLevel))
|
|
39
38
|
return;
|
|
40
|
-
const caller = getDetailedString(loggerMethod);
|
|
41
39
|
const suffix = data === undefined ? "" : serializeData(data);
|
|
42
|
-
const line = `${new Date().toISOString()} ${level.toUpperCase().padEnd(5)} [${module}] ${
|
|
40
|
+
const line = `${new Date().toISOString()} ${level.toUpperCase().padEnd(5)} [${module}] ${msg}${suffix}\n`;
|
|
43
41
|
try {
|
|
44
42
|
mkdirSync(logDir, { recursive: true });
|
|
45
43
|
appendFileSync(logFile, line, "utf8");
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { Buffer } from "buffer";
|
|
2
|
+
const characters = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
3
|
+
const checksumCharacters = "*~$=U";
|
|
4
|
+
export class InvalidChecksumCharacterError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "InvalidChecksumCharacterError";
|
|
8
|
+
// oxlint-disable-next-line typescript/no-unnecessary-condition -- captureStackTrace is a V8 extension, absent on other engines
|
|
9
|
+
if (Error.captureStackTrace) {
|
|
10
|
+
Error.captureStackTrace(this, this.constructor);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export class InvalidChecksumError extends Error {
|
|
15
|
+
constructor(message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = "InvalidChecksumError";
|
|
18
|
+
// oxlint-disable-next-line typescript/no-unnecessary-condition -- captureStackTrace is a V8 extension, absent on other engines
|
|
19
|
+
if (Error.captureStackTrace) {
|
|
20
|
+
Error.captureStackTrace(this, this.constructor);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* An implementation of the Crockford Base32 algorithm.
|
|
26
|
+
*
|
|
27
|
+
* Spec: https://www.crockford.com/base32.html
|
|
28
|
+
*/
|
|
29
|
+
export class CrockfordBase32 {
|
|
30
|
+
static encode(input, options) {
|
|
31
|
+
if (Buffer.isBuffer(input)) {
|
|
32
|
+
input = Buffer.from(input);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
input = this.createBuffer(input);
|
|
36
|
+
}
|
|
37
|
+
const variant = options?.variant ?? "crockford";
|
|
38
|
+
const checksum = options?.checksum ?? false;
|
|
39
|
+
if (variant === "ulid") {
|
|
40
|
+
if (checksum) {
|
|
41
|
+
throw new Error("Checksums are not supported with the ulid variant");
|
|
42
|
+
}
|
|
43
|
+
return this.encodeUlid(input);
|
|
44
|
+
}
|
|
45
|
+
const output = [];
|
|
46
|
+
let bitsRead = 0;
|
|
47
|
+
let buffer = 0;
|
|
48
|
+
for (const byte of input) {
|
|
49
|
+
// Add current byte to start of buffer
|
|
50
|
+
buffer = (buffer << 8) | byte;
|
|
51
|
+
bitsRead += 8;
|
|
52
|
+
while (bitsRead >= 5) {
|
|
53
|
+
output.push((buffer >>> (bitsRead - 5)) & 0x1f);
|
|
54
|
+
bitsRead -= 5;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (bitsRead > 0) {
|
|
58
|
+
output.push((buffer << (5 - bitsRead)) & 0x1f);
|
|
59
|
+
}
|
|
60
|
+
let result = output.map((byte) => characters.charAt(byte)).join("");
|
|
61
|
+
if (checksum) {
|
|
62
|
+
result += this.computeChecksum(input);
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
static computeChecksum(bytes) {
|
|
67
|
+
// Crockford's check symbol is value mod 37, where value is the number the
|
|
68
|
+
// symbols represent. Fold byte-by-byte to avoid materializing a bigint;
|
|
69
|
+
// acc stays < 37 so (acc * 256 + byte) fits comfortably in a JS number.
|
|
70
|
+
let acc = 0;
|
|
71
|
+
for (const byte of bytes) {
|
|
72
|
+
acc = (acc * 256 + byte) % 37;
|
|
73
|
+
}
|
|
74
|
+
return (characters + checksumCharacters).charAt(acc);
|
|
75
|
+
}
|
|
76
|
+
static decode(input, options) {
|
|
77
|
+
// 1. Translate input to all uppercase
|
|
78
|
+
// 2. Translate I, L, and O to valid base 32 characters
|
|
79
|
+
// 3. Remove all hyphens
|
|
80
|
+
input = input
|
|
81
|
+
.toUpperCase()
|
|
82
|
+
.replace(/O/g, "0")
|
|
83
|
+
.replace(/[IL]/g, "1")
|
|
84
|
+
.replace(/-+/g, "");
|
|
85
|
+
const variant = options?.variant ?? "crockford";
|
|
86
|
+
const checksum = options?.checksum ?? false;
|
|
87
|
+
if (variant === "ulid") {
|
|
88
|
+
if (checksum) {
|
|
89
|
+
throw new Error("Checksums are not supported with the ulid variant");
|
|
90
|
+
}
|
|
91
|
+
return this.decodeUlid(input, options);
|
|
92
|
+
}
|
|
93
|
+
let providedCheck = null;
|
|
94
|
+
if (checksum) {
|
|
95
|
+
if (input.length === 0) {
|
|
96
|
+
throw new InvalidChecksumCharacterError("Cannot validate checksum: input is empty");
|
|
97
|
+
}
|
|
98
|
+
const last = input.charAt(input.length - 1);
|
|
99
|
+
if ((characters + checksumCharacters).indexOf(last) === -1) {
|
|
100
|
+
throw new InvalidChecksumCharacterError(`Invalid checksum character: ${last}`);
|
|
101
|
+
}
|
|
102
|
+
providedCheck = last;
|
|
103
|
+
input = input.slice(0, -1);
|
|
104
|
+
}
|
|
105
|
+
const output = [];
|
|
106
|
+
let bitsRead = 0;
|
|
107
|
+
let buffer = 0;
|
|
108
|
+
for (const character of input) {
|
|
109
|
+
const byte = characters.indexOf(character);
|
|
110
|
+
if (byte === -1) {
|
|
111
|
+
throw new Error(`Invalid base 32 character found in string: ${character}`);
|
|
112
|
+
}
|
|
113
|
+
bitsRead += 5;
|
|
114
|
+
if (bitsRead >= 8) {
|
|
115
|
+
bitsRead -= 8;
|
|
116
|
+
output.push(buffer | (byte >> bitsRead));
|
|
117
|
+
buffer = (byte << (8 - bitsRead)) & 0xff;
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
buffer |= byte << (8 - bitsRead);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// Flush a trailing partial byte when it carries data: either bits are
|
|
124
|
+
// non-zero, or a whole character (>= 5 bits) went unpaired and so cannot
|
|
125
|
+
// be canonical zero padding.
|
|
126
|
+
if (buffer > 0 || bitsRead >= 5) {
|
|
127
|
+
output.push(buffer);
|
|
128
|
+
}
|
|
129
|
+
if (providedCheck !== null) {
|
|
130
|
+
const computedCheck = this.computeChecksum(output);
|
|
131
|
+
if (providedCheck !== computedCheck) {
|
|
132
|
+
throw new InvalidChecksumError(`Checksum mismatch: expected '${computedCheck}' but found '${providedCheck}'`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (options?.asNumber === true) {
|
|
136
|
+
return this.asNumber(output);
|
|
137
|
+
}
|
|
138
|
+
return this.asBuffer(output);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Validates whether `input` is a Crockford Base32 string with a correct
|
|
142
|
+
* trailing check symbol. Returns false for any malformed input (invalid
|
|
143
|
+
* characters, mismatched checksum, empty string), never throws.
|
|
144
|
+
*
|
|
145
|
+
* Use {@link decode} with `{ checksum: true }` if you need typed errors.
|
|
146
|
+
*/
|
|
147
|
+
static verify(input) {
|
|
148
|
+
try {
|
|
149
|
+
this.decode(input, { checksum: true });
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
static encodeUlid(input) {
|
|
157
|
+
// Keep empty binary input empty so Buffer round-trips do not become 0x00.
|
|
158
|
+
if (input.length === 0) {
|
|
159
|
+
return "";
|
|
160
|
+
}
|
|
161
|
+
// ULID variant uses modulo-based encoding (right-to-left, padding left)
|
|
162
|
+
// Convert buffer to bigint
|
|
163
|
+
let value = 0n;
|
|
164
|
+
for (const byte of input) {
|
|
165
|
+
value = (value << 8n) | BigInt(byte);
|
|
166
|
+
}
|
|
167
|
+
// Use repeated division by 32 to extract base32 digits
|
|
168
|
+
const output = [];
|
|
169
|
+
while (value > 0n) {
|
|
170
|
+
const remainder = Number(value % 32n);
|
|
171
|
+
output.unshift(characters.charAt(remainder));
|
|
172
|
+
value /= 32n;
|
|
173
|
+
}
|
|
174
|
+
// Pad to the expected length based on input size
|
|
175
|
+
// Each base32 character encodes 5 bits, so n bytes = n*8 bits
|
|
176
|
+
// require ceil(n*8/5) characters
|
|
177
|
+
const expectedLength = Math.ceil((input.length * 8) / 5);
|
|
178
|
+
return output.join("").padStart(expectedLength, "0");
|
|
179
|
+
}
|
|
180
|
+
static decodeUlid(input, options) {
|
|
181
|
+
// Empty encoded data represents no bytes; asNumber has no empty numeric
|
|
182
|
+
// value, so return the same numeric zero used by other empty decodes.
|
|
183
|
+
if (input.length === 0) {
|
|
184
|
+
return options?.asNumber === true ? 0n : Buffer.from([]);
|
|
185
|
+
}
|
|
186
|
+
// ULID variant uses modulo-based decoding (reverse of encoding)
|
|
187
|
+
// Treat the base32 string as a number in base 32
|
|
188
|
+
let value = 0n;
|
|
189
|
+
for (const character of input) {
|
|
190
|
+
const digit = characters.indexOf(character);
|
|
191
|
+
if (digit === -1) {
|
|
192
|
+
throw new Error(`Invalid base 32 character found in string: ${character}`);
|
|
193
|
+
}
|
|
194
|
+
value = value * 32n + BigInt(digit);
|
|
195
|
+
}
|
|
196
|
+
// Calculate expected output length based on input string length
|
|
197
|
+
// Each base32 character represents 5 bits, so n characters = n*5 bits
|
|
198
|
+
// We use floor because the encoder may have added padding characters
|
|
199
|
+
// to reach the expected length, creating extra bits we should ignore
|
|
200
|
+
const expectedBytes = Math.floor((input.length * 5) / 8);
|
|
201
|
+
// Convert bigint to byte array
|
|
202
|
+
const output = [];
|
|
203
|
+
let temp = value;
|
|
204
|
+
while (temp > 0n) {
|
|
205
|
+
output.unshift(Number(temp & 0xffn));
|
|
206
|
+
temp >>= 8n;
|
|
207
|
+
}
|
|
208
|
+
// For all-zero input the bigint loop emits no bytes, so seed one zero
|
|
209
|
+
// byte. The padding loop below then extends to expectedBytes when needed
|
|
210
|
+
// (e.g. "0000" -> two bytes); single-character "0" decodes to one byte
|
|
211
|
+
// even though expectedBytes is 0.
|
|
212
|
+
if (value === 0n) {
|
|
213
|
+
output.push(0);
|
|
214
|
+
}
|
|
215
|
+
// Pad with leading zeros to match expected length
|
|
216
|
+
while (output.length < expectedBytes) {
|
|
217
|
+
output.unshift(0);
|
|
218
|
+
}
|
|
219
|
+
if (options?.asNumber === true) {
|
|
220
|
+
return this.asNumber(output);
|
|
221
|
+
}
|
|
222
|
+
return this.asBuffer(output);
|
|
223
|
+
}
|
|
224
|
+
static createBuffer(input) {
|
|
225
|
+
if (typeof input === "number") {
|
|
226
|
+
if (!Number.isSafeInteger(input)) {
|
|
227
|
+
throw new Error("Input must be a safe integer");
|
|
228
|
+
}
|
|
229
|
+
input = BigInt(input);
|
|
230
|
+
}
|
|
231
|
+
if (input < 0n) {
|
|
232
|
+
throw new Error("Input cannot be a negative number");
|
|
233
|
+
}
|
|
234
|
+
const bytes = [];
|
|
235
|
+
while (input > 0n) {
|
|
236
|
+
bytes.unshift(Number(input & 0xffn));
|
|
237
|
+
input >>= 8n;
|
|
238
|
+
}
|
|
239
|
+
return Buffer.from(bytes);
|
|
240
|
+
}
|
|
241
|
+
static asNumber(output) {
|
|
242
|
+
let outputNumber = 0n;
|
|
243
|
+
output.forEach((byte) => {
|
|
244
|
+
outputNumber <<= 8n;
|
|
245
|
+
outputNumber |= BigInt(byte);
|
|
246
|
+
});
|
|
247
|
+
return outputNumber;
|
|
248
|
+
}
|
|
249
|
+
static asBuffer(output) {
|
|
250
|
+
return Buffer.from(output.map((byte) => byte.toString(16).padStart(2, "0")).join(""), "hex");
|
|
251
|
+
}
|
|
252
|
+
}
|
package/dist/spawn/spawn-id.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* Uniqueness is the caller's business — see generateUniqueSpawnId.
|
|
6
6
|
*/
|
|
7
7
|
import { randomBytes } from "node:crypto";
|
|
8
|
-
import { CrockfordBase32 } from "crockford-base32";
|
|
8
|
+
import { CrockfordBase32 } from "./crockford-base32.js";
|
|
9
9
|
/** Spawn-id alphabet (lowercase Crockford base32); src/predicates.ts derives its pattern from this. */
|
|
10
10
|
export const SPAWN_ID_ALPHABET = "0123456789abcdefghjkmnpqrstvwxyz";
|
|
11
11
|
/** Exact length of every spawn id (40 bits ⇒ 8 base32 chars). */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-cowboy",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"packageManager": "pnpm@12.9.1",
|
|
6
6
|
"description": "Agent orchestrator for pi: each subagent runs as its own pi process in a background Herdr pane, isolated by a copy-on-write git worktree.",
|
|
@@ -96,7 +96,7 @@
|
|
|
96
96
|
"eslint": "^10.12.0",
|
|
97
97
|
"eslint-plugin-import-x": "^4.17.1",
|
|
98
98
|
"husky": "^9.1.7",
|
|
99
|
-
"oxlint": "1.
|
|
99
|
+
"oxlint": "1.87.0",
|
|
100
100
|
"oxlint-tsgolint": "7.0.2003",
|
|
101
101
|
"prettier": "^3.9.9",
|
|
102
102
|
"semantic-release": "^25.0.9",
|
|
@@ -104,8 +104,6 @@
|
|
|
104
104
|
"vitest": "^5.0.3"
|
|
105
105
|
},
|
|
106
106
|
"dependencies": {
|
|
107
|
-
"caller-id": "^0.1.0",
|
|
108
|
-
"crockford-base32": "^2.1.0",
|
|
109
107
|
"mustache": "^4.2.0",
|
|
110
108
|
"smol-toml": "^1.9.0",
|
|
111
109
|
"zod": "^4.6.5"
|