@qlairoslabs/acp-client 0.7.4 → 0.8.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/CHANGELOG-audit.md +58 -0
- package/CONSUMER_GUIDE.md +452 -19
- package/package.json +6 -1
- package/src/agent-adapter-registry.ts +66 -0
- package/src/agent-conversation-continuation.ts +121 -0
- package/src/agent-conversation-contracts.ts +279 -0
- package/src/agent-conversation-errors.ts +31 -0
- package/src/agent-conversation-owner.ts +55 -0
- package/src/agent-conversation-presentation.ts +217 -0
- package/src/agent-conversation-queue.ts +44 -0
- package/src/agent-conversation.ts +41 -0
- package/src/agent-ids.ts +50 -0
- package/src/agent-session.ts +973 -673
- package/src/agent.ts +1175 -0
- package/src/audit/config.ts +186 -0
- package/src/audit/contracts.ts +59 -0
- package/src/audit/index.ts +50 -0
- package/src/audit/projection.ts +125 -0
- package/src/audit/reader.ts +371 -0
- package/src/audit/runtime.ts +1060 -0
- package/src/checkpointing/config.ts +91 -0
- package/src/checkpointing/contracts.ts +45 -0
- package/src/checkpointing/coordinator.ts +100 -0
- package/src/checkpointing/ids.ts +5 -0
- package/src/checkpointing/index.ts +6 -0
- package/src/checkpointing/persistence/sqlite-repository.ts +159 -0
- package/src/checkpointing/recovery.ts +114 -0
- package/src/checkpointing/strategy.ts +139 -0
- package/src/identity.ts +62 -0
- package/src/index.ts +117 -0
- package/src/internal-inference/config.ts +68 -0
- package/src/internal-inference/contracts.ts +218 -0
- package/src/internal-inference/coordination/handles.ts +133 -0
- package/src/internal-inference/coordination/manager.ts +487 -0
- package/src/internal-inference/ids.ts +10 -0
- package/src/internal-inference/index.ts +10 -0
- package/src/internal-inference/persistence/records.ts +132 -0
- package/src/internal-inference/persistence/repository.ts +35 -0
- package/src/internal-inference/persistence/sqlite-repository.ts +551 -0
- package/src/internal-inference/runtime/harness-runtime.ts +78 -0
- package/src/internal-inference/strategy/registry.ts +42 -0
- package/src/metering/engine.ts +47 -0
- package/src/metering/record.ts +4 -1
- package/src/observation/contracts.ts +10 -0
- package/src/observation/index.ts +8 -0
- package/src/observation/subscription-hub.ts +43 -0
- package/src/persistence/activity-projection.ts +26 -12
- package/src/persistence/backend.ts +774 -0
- package/src/persistence/index.ts +99 -0
- package/src/persistence/sqlite-backend.ts +1927 -0
- package/src/persistence/sqlite-conversation-records.ts +425 -0
- package/src/persistence/sqlite-owner-lock.ts +106 -0
- package/src/persistence/sqlite-schema.ts +561 -0
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import { open, readdir, realpath } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import type { FileHandle } from "node:fs/promises";
|
|
4
|
+
import { canonicalizeJson, sha256CanonicalJson } from "../persistence/integrity.ts";
|
|
5
|
+
import { resolveAgentAuditLogConfig } from "./config.ts";
|
|
6
|
+
import {
|
|
7
|
+
AGENT_AUDIT_BATCH_SIZE,
|
|
8
|
+
type AgentAuditEntryV2,
|
|
9
|
+
type AgentAuditSegmentHeaderV2,
|
|
10
|
+
} from "./runtime.ts";
|
|
11
|
+
|
|
12
|
+
const MAX_CURSOR_BYTES = 4_096;
|
|
13
|
+
const MAX_LINE_BYTES = 1_048_576;
|
|
14
|
+
|
|
15
|
+
export type AgentAuditReadMode = "logical" | "physical";
|
|
16
|
+
|
|
17
|
+
export interface AgentAuditLogReaderConfig {
|
|
18
|
+
readonly directory: string;
|
|
19
|
+
readonly filePrefix?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface AgentAuditReadPageOptions {
|
|
23
|
+
readonly mode?: AgentAuditReadMode;
|
|
24
|
+
readonly limit?: number;
|
|
25
|
+
readonly cursor?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface AgentAuditReadPage {
|
|
29
|
+
readonly entries: readonly AgentAuditEntryV2[];
|
|
30
|
+
readonly nextCursor?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface ReaderTuple {
|
|
34
|
+
readonly sequence: number;
|
|
35
|
+
readonly outboxId: string;
|
|
36
|
+
readonly payloadSha256: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface ReaderCursor {
|
|
40
|
+
readonly v: 1;
|
|
41
|
+
readonly auditLogId: string;
|
|
42
|
+
readonly agentSessionId: string;
|
|
43
|
+
readonly mode: AgentAuditReadMode;
|
|
44
|
+
readonly snapshotGeneration: number;
|
|
45
|
+
readonly snapshotOffset: number;
|
|
46
|
+
readonly generation: number;
|
|
47
|
+
readonly byteOffset: number;
|
|
48
|
+
readonly logicalSequence: number;
|
|
49
|
+
readonly recent: readonly ReaderTuple[];
|
|
50
|
+
readonly replayLength: number;
|
|
51
|
+
readonly replayLastSequence: number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class AgentAuditLogReaderError extends Error {
|
|
55
|
+
constructor(message: string, options?: ErrorOptions) {
|
|
56
|
+
super(message, options);
|
|
57
|
+
this.name = "AgentAuditLogReaderError";
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class AgentAuditLogReader {
|
|
62
|
+
private constructor(
|
|
63
|
+
readonly directory: string,
|
|
64
|
+
readonly filePrefix: string,
|
|
65
|
+
) {}
|
|
66
|
+
|
|
67
|
+
static async open(config: AgentAuditLogReaderConfig): Promise<AgentAuditLogReader> {
|
|
68
|
+
const resolved = resolveAgentAuditLogConfig({
|
|
69
|
+
directory: config.directory,
|
|
70
|
+
...(config.filePrefix ? { filePrefix: config.filePrefix } : {}),
|
|
71
|
+
});
|
|
72
|
+
let directory: string;
|
|
73
|
+
try {
|
|
74
|
+
directory = await realpath(resolved.directory);
|
|
75
|
+
} catch (error) {
|
|
76
|
+
throw new AgentAuditLogReaderError(`audit directory is unavailable: ${resolved.directory}`, { cause: error });
|
|
77
|
+
}
|
|
78
|
+
return new AgentAuditLogReader(directory, resolved.filePrefix);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async readPage(options: AgentAuditReadPageOptions = {}): Promise<AgentAuditReadPage> {
|
|
82
|
+
const mode = options.mode ?? "logical";
|
|
83
|
+
if (mode !== "logical" && mode !== "physical") throw new AgentAuditLogReaderError(`invalid reader mode: ${mode}`);
|
|
84
|
+
const limit = boundedLimit(options.limit ?? 100);
|
|
85
|
+
const segments = await listSegments(this.directory, this.filePrefix);
|
|
86
|
+
if (segments.length === 0) throw new AgentAuditLogReaderError("audit stream contains no segments");
|
|
87
|
+
|
|
88
|
+
let cursor: ReaderCursor;
|
|
89
|
+
if (options.cursor) {
|
|
90
|
+
cursor = decodeCursor(options.cursor);
|
|
91
|
+
if (cursor.mode !== mode) throw new AgentAuditLogReaderError("audit cursor mode does not match request");
|
|
92
|
+
if (segments.length < cursor.snapshotGeneration) {
|
|
93
|
+
throw new AgentAuditLogReaderError("audit snapshot segments are missing");
|
|
94
|
+
}
|
|
95
|
+
const finalSize = await fileSize(join(this.directory, segmentName(this.filePrefix, cursor.snapshotGeneration)));
|
|
96
|
+
if (finalSize < cursor.snapshotOffset) throw new AgentAuditLogReaderError("audit snapshot boundary was truncated");
|
|
97
|
+
} else {
|
|
98
|
+
const snapshotGeneration = segments.length;
|
|
99
|
+
const snapshotOffset = await fileSize(join(this.directory, segments.at(-1)!.name));
|
|
100
|
+
const firstHeader = await readHeader(
|
|
101
|
+
join(this.directory, segments[0]!.name),
|
|
102
|
+
segments[0]!.generation,
|
|
103
|
+
);
|
|
104
|
+
cursor = {
|
|
105
|
+
v: 1,
|
|
106
|
+
auditLogId: firstHeader.header.auditLogId,
|
|
107
|
+
agentSessionId: firstHeader.header.agentSessionId,
|
|
108
|
+
mode,
|
|
109
|
+
snapshotGeneration,
|
|
110
|
+
snapshotOffset,
|
|
111
|
+
generation: 1,
|
|
112
|
+
byteOffset: firstHeader.nextOffset,
|
|
113
|
+
logicalSequence: 0,
|
|
114
|
+
recent: [],
|
|
115
|
+
replayLength: 0,
|
|
116
|
+
replayLastSequence: 0,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const entries: AgentAuditEntryV2[] = [];
|
|
121
|
+
let position = { ...cursor };
|
|
122
|
+
while (entries.length < limit && position.generation <= position.snapshotGeneration) {
|
|
123
|
+
const path = join(this.directory, segmentName(this.filePrefix, position.generation));
|
|
124
|
+
const boundary = position.generation === position.snapshotGeneration
|
|
125
|
+
? position.snapshotOffset
|
|
126
|
+
: await fileSize(path);
|
|
127
|
+
const segmentHeader = await readHeader(path, position.generation);
|
|
128
|
+
if (position.byteOffset === 0) {
|
|
129
|
+
const header = segmentHeader;
|
|
130
|
+
assertHeaderIdentity(header.header, position);
|
|
131
|
+
position = { ...position, byteOffset: header.nextOffset };
|
|
132
|
+
} else {
|
|
133
|
+
assertHeaderIdentity(segmentHeader.header, position);
|
|
134
|
+
}
|
|
135
|
+
if (position.byteOffset > boundary) throw new AgentAuditLogReaderError(`audit segment was truncated: ${path}`);
|
|
136
|
+
if (position.byteOffset === boundary) {
|
|
137
|
+
position = { ...position, generation: position.generation + 1, byteOffset: 0 };
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const handle = await open(path, "r");
|
|
142
|
+
try {
|
|
143
|
+
while (entries.length < limit && position.byteOffset < boundary) {
|
|
144
|
+
const physical = await readLine(handle, position.byteOffset, boundary, path);
|
|
145
|
+
const entry = parseEntry(physical.line, path, position.auditLogId, position.agentSessionId);
|
|
146
|
+
if (
|
|
147
|
+
position.byteOffset === segmentHeader.nextOffset
|
|
148
|
+
&& entry.auditSequence !== segmentHeader.header.firstAuditSequence
|
|
149
|
+
) throw new AgentAuditLogReaderError(`audit segment first sequence is incorrect: ${path}`);
|
|
150
|
+
const update = advanceLogicalState(position, entry);
|
|
151
|
+
const { duplicate, ...logicalState } = update;
|
|
152
|
+
position = { ...position, ...logicalState, byteOffset: physical.nextOffset };
|
|
153
|
+
if (mode === "physical" || !duplicate) entries.push(entry);
|
|
154
|
+
}
|
|
155
|
+
} finally {
|
|
156
|
+
await handle.close();
|
|
157
|
+
}
|
|
158
|
+
if (position.byteOffset === boundary && position.generation < position.snapshotGeneration) {
|
|
159
|
+
position = { ...position, generation: position.generation + 1, byteOffset: 0 };
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const more = position.generation <= position.snapshotGeneration && (
|
|
164
|
+
position.generation < position.snapshotGeneration || position.byteOffset < position.snapshotOffset
|
|
165
|
+
);
|
|
166
|
+
return Object.freeze({
|
|
167
|
+
entries: Object.freeze(entries),
|
|
168
|
+
...(more ? { nextCursor: encodeCursor(position) } : {}),
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async function listSegments(directory: string, prefix: string) {
|
|
174
|
+
const matcher = new RegExp(`^${escapeRegExp(prefix)}\\.([0-9]{6,})\\.jsonl$`);
|
|
175
|
+
const segments = (await readdir(directory))
|
|
176
|
+
.map((name) => ({ name, match: matcher.exec(name) }))
|
|
177
|
+
.filter((item): item is { name: string; match: RegExpExecArray } => item.match !== null)
|
|
178
|
+
.map(({ name, match }) => ({ name, generation: Number(match[1]) }))
|
|
179
|
+
.sort((left, right) => left.generation - right.generation);
|
|
180
|
+
for (let index = 0; index < segments.length; index++) {
|
|
181
|
+
if (segments[index]!.generation !== index + 1) {
|
|
182
|
+
throw new AgentAuditLogReaderError("audit segment generations are not contiguous from one");
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return segments;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async function readHeader(path: string, generation: number) {
|
|
189
|
+
const handle = await open(path, "r");
|
|
190
|
+
try {
|
|
191
|
+
const physical = await readLine(handle, 0, (await handle.stat()).size, path);
|
|
192
|
+
let value: unknown;
|
|
193
|
+
try {
|
|
194
|
+
value = JSON.parse(physical.line);
|
|
195
|
+
} catch (error) {
|
|
196
|
+
throw new AgentAuditLogReaderError(`malformed audit header: ${path}`, { cause: error });
|
|
197
|
+
}
|
|
198
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
199
|
+
throw new AgentAuditLogReaderError(`invalid audit header: ${path}`);
|
|
200
|
+
}
|
|
201
|
+
const header = value as unknown as AgentAuditSegmentHeaderV2;
|
|
202
|
+
if (
|
|
203
|
+
header.v !== 2 || header.t !== "header" || header.generation !== generation
|
|
204
|
+
|| typeof header.auditLogId !== "string" || typeof header.agentSessionId !== "string"
|
|
205
|
+
|| !Number.isSafeInteger(header.firstAuditSequence) || header.firstAuditSequence < 1
|
|
206
|
+
|| !Number.isFinite(Date.parse(header.createdAt))
|
|
207
|
+
) throw new AgentAuditLogReaderError(`invalid audit header: ${path}`);
|
|
208
|
+
return { header, nextOffset: physical.nextOffset };
|
|
209
|
+
} finally {
|
|
210
|
+
await handle.close();
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function assertHeaderIdentity(header: AgentAuditSegmentHeaderV2, cursor: ReaderCursor): void {
|
|
215
|
+
if (header.auditLogId !== cursor.auditLogId || header.agentSessionId !== cursor.agentSessionId) {
|
|
216
|
+
throw new AgentAuditLogReaderError("audit segment identity changed within the stream");
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async function readLine(
|
|
221
|
+
handle: FileHandle,
|
|
222
|
+
offset: number,
|
|
223
|
+
boundary: number,
|
|
224
|
+
path: string,
|
|
225
|
+
): Promise<{ line: string; nextOffset: number }> {
|
|
226
|
+
const chunks: Buffer[] = [];
|
|
227
|
+
let cursor = offset;
|
|
228
|
+
let total = 0;
|
|
229
|
+
while (cursor < boundary) {
|
|
230
|
+
const size = Math.min(64 * 1024, boundary - cursor, MAX_LINE_BYTES + 1 - total);
|
|
231
|
+
if (size <= 0) throw new AgentAuditLogReaderError(`audit line exceeds ${MAX_LINE_BYTES} bytes: ${path}`);
|
|
232
|
+
const buffer = Buffer.allocUnsafe(size);
|
|
233
|
+
const { bytesRead } = await handle.read(buffer, 0, size, cursor);
|
|
234
|
+
if (bytesRead === 0) break;
|
|
235
|
+
const slice = buffer.subarray(0, bytesRead);
|
|
236
|
+
const newline = slice.indexOf(0x0a);
|
|
237
|
+
if (newline >= 0) {
|
|
238
|
+
chunks.push(slice.subarray(0, newline));
|
|
239
|
+
return { line: Buffer.concat(chunks).toString("utf8"), nextOffset: cursor + newline + 1 };
|
|
240
|
+
}
|
|
241
|
+
chunks.push(slice);
|
|
242
|
+
cursor += bytesRead;
|
|
243
|
+
total += bytesRead;
|
|
244
|
+
}
|
|
245
|
+
throw new AgentAuditLogReaderError(`torn audit tail at byte ${offset}: ${path}`);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function parseEntry(line: string, path: string, auditLogId: string, agentSessionId: string): AgentAuditEntryV2 {
|
|
249
|
+
let value: unknown;
|
|
250
|
+
try {
|
|
251
|
+
value = JSON.parse(line);
|
|
252
|
+
} catch (error) {
|
|
253
|
+
throw new AgentAuditLogReaderError(`malformed audit entry: ${path}`, { cause: error });
|
|
254
|
+
}
|
|
255
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
256
|
+
throw new AgentAuditLogReaderError(`invalid audit entry: ${path}`);
|
|
257
|
+
}
|
|
258
|
+
const entry = value as unknown as AgentAuditEntryV2;
|
|
259
|
+
if (
|
|
260
|
+
entry.v !== 2 || entry.t !== "audit" || entry.auditLogId !== auditLogId
|
|
261
|
+
|| entry.agentSessionId !== agentSessionId || typeof entry.outboxId !== "string" || !entry.outboxId
|
|
262
|
+
|| !Number.isSafeInteger(entry.auditSequence) || entry.auditSequence < 1
|
|
263
|
+
|| typeof entry.payloadSha256 !== "string" || !/^[0-9a-f]{64}$/.test(entry.payloadSha256)
|
|
264
|
+
|| sha256CanonicalJson(entry.payload) !== entry.payloadSha256
|
|
265
|
+
) throw new AgentAuditLogReaderError(`audit entry failed validation: ${path}`);
|
|
266
|
+
return Object.freeze(entry);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function advanceLogicalState(cursor: ReaderCursor, entry: AgentAuditEntryV2) {
|
|
270
|
+
const tuple: ReaderTuple = {
|
|
271
|
+
sequence: entry.auditSequence,
|
|
272
|
+
outboxId: entry.outboxId,
|
|
273
|
+
payloadSha256: entry.payloadSha256,
|
|
274
|
+
};
|
|
275
|
+
if (entry.auditSequence === cursor.logicalSequence + 1) {
|
|
276
|
+
return {
|
|
277
|
+
logicalSequence: entry.auditSequence,
|
|
278
|
+
recent: Object.freeze([...cursor.recent, tuple].slice(-AGENT_AUDIT_BATCH_SIZE)),
|
|
279
|
+
replayLength: 0,
|
|
280
|
+
replayLastSequence: 0,
|
|
281
|
+
duplicate: false,
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
const original = cursor.recent.find((candidate) => candidate.sequence === entry.auditSequence);
|
|
285
|
+
if (
|
|
286
|
+
!original || original.outboxId !== entry.outboxId || original.payloadSha256 !== entry.payloadSha256
|
|
287
|
+
|| (cursor.replayLength > 0 && entry.auditSequence !== cursor.replayLastSequence + 1)
|
|
288
|
+
|| cursor.replayLength + 1 > AGENT_AUDIT_BATCH_SIZE
|
|
289
|
+
) throw new AgentAuditLogReaderError(`conflicting or out-of-window audit sequence ${entry.auditSequence}`);
|
|
290
|
+
return {
|
|
291
|
+
logicalSequence: cursor.logicalSequence,
|
|
292
|
+
recent: cursor.recent,
|
|
293
|
+
replayLength: cursor.replayLength + 1,
|
|
294
|
+
replayLastSequence: entry.auditSequence,
|
|
295
|
+
duplicate: true,
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function encodeCursor(cursor: ReaderCursor): string {
|
|
300
|
+
const encoded = Buffer.from(canonicalizeJson(cursor), "utf8").toString("base64url");
|
|
301
|
+
if (Buffer.byteLength(encoded, "utf8") > MAX_CURSOR_BYTES) {
|
|
302
|
+
throw new AgentAuditLogReaderError("audit cursor exceeds its safety limit");
|
|
303
|
+
}
|
|
304
|
+
return encoded;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function decodeCursor(encoded: string): ReaderCursor {
|
|
308
|
+
if (Buffer.byteLength(encoded, "utf8") > MAX_CURSOR_BYTES) {
|
|
309
|
+
throw new AgentAuditLogReaderError("audit cursor exceeds its safety limit");
|
|
310
|
+
}
|
|
311
|
+
let value: unknown;
|
|
312
|
+
try {
|
|
313
|
+
value = JSON.parse(Buffer.from(encoded, "base64url").toString("utf8"));
|
|
314
|
+
} catch (error) {
|
|
315
|
+
throw new AgentAuditLogReaderError("audit cursor is malformed", { cause: error });
|
|
316
|
+
}
|
|
317
|
+
if (!isReaderCursor(value)) throw new AgentAuditLogReaderError("audit cursor is invalid");
|
|
318
|
+
return value;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function isReaderCursor(value: unknown): value is ReaderCursor {
|
|
322
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
323
|
+
const cursor = value as Record<string, unknown>;
|
|
324
|
+
return cursor.v === 1 && typeof cursor.auditLogId === "string" && typeof cursor.agentSessionId === "string"
|
|
325
|
+
&& (cursor.mode === "logical" || cursor.mode === "physical")
|
|
326
|
+
&& safePositive(cursor.snapshotGeneration) && safeNonNegative(cursor.snapshotOffset)
|
|
327
|
+
&& safePositive(cursor.generation) && safeNonNegative(cursor.byteOffset)
|
|
328
|
+
&& safeNonNegative(cursor.logicalSequence) && Array.isArray(cursor.recent)
|
|
329
|
+
&& cursor.recent.length <= AGENT_AUDIT_BATCH_SIZE
|
|
330
|
+
&& cursor.recent.every(isReaderTuple) && safeNonNegative(cursor.replayLength)
|
|
331
|
+
&& cursor.replayLength <= AGENT_AUDIT_BATCH_SIZE && safeNonNegative(cursor.replayLastSequence);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function isReaderTuple(value: unknown): value is ReaderTuple {
|
|
335
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
336
|
+
const tuple = value as Record<string, unknown>;
|
|
337
|
+
return safePositive(tuple.sequence) && typeof tuple.outboxId === "string"
|
|
338
|
+
&& typeof tuple.payloadSha256 === "string" && /^[0-9a-f]{64}$/.test(tuple.payloadSha256);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function boundedLimit(value: number): number {
|
|
342
|
+
if (!Number.isSafeInteger(value) || value < 1 || value > 1_000) {
|
|
343
|
+
throw new AgentAuditLogReaderError("audit page limit must be an integer between 1 and 1000");
|
|
344
|
+
}
|
|
345
|
+
return value;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
async function fileSize(path: string): Promise<number> {
|
|
349
|
+
const handle = await open(path, "r");
|
|
350
|
+
try {
|
|
351
|
+
return (await handle.stat()).size;
|
|
352
|
+
} finally {
|
|
353
|
+
await handle.close();
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function safePositive(value: unknown): value is number {
|
|
358
|
+
return Number.isSafeInteger(value) && Number(value) > 0;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function safeNonNegative(value: unknown): value is number {
|
|
362
|
+
return Number.isSafeInteger(value) && Number(value) >= 0;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function segmentName(prefix: string, generation: number): string {
|
|
366
|
+
return `${prefix}.${String(generation).padStart(6, "0")}.jsonl`;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function escapeRegExp(value: string): string {
|
|
370
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
371
|
+
}
|