@mikeargento/bitgraph-mcp 0.5.2 → 0.7.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/dist/__tests__/carrier.test.d.ts +2 -0
- package/dist/__tests__/carrier.test.d.ts.map +1 -0
- package/dist/__tests__/carrier.test.js +243 -0
- package/dist/__tests__/carrier.test.js.map +1 -0
- package/dist/__tests__/format.test.js +3 -3
- package/dist/__tests__/format.test.js.map +1 -1
- package/dist/__tests__/proof-beside.test.js +1 -1
- package/dist/__tests__/proof-beside.test.js.map +1 -1
- package/dist/__tests__/tools.test.js +6 -5
- package/dist/__tests__/tools.test.js.map +1 -1
- package/dist/carrier-io.d.ts +52 -0
- package/dist/carrier-io.d.ts.map +1 -0
- package/dist/carrier-io.js +91 -0
- package/dist/carrier-io.js.map +1 -0
- package/dist/format.d.ts +27 -4
- package/dist/format.d.ts.map +1 -1
- package/dist/format.js +31 -11
- package/dist/format.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/instructions.d.ts +2 -0
- package/dist/instructions.d.ts.map +1 -0
- package/dist/instructions.js +9 -0
- package/dist/instructions.js.map +1 -0
- package/dist/scan.d.ts +4 -0
- package/dist/scan.d.ts.map +1 -1
- package/dist/scan.js +43 -0
- package/dist/scan.js.map +1 -1
- package/dist/server.d.ts +3 -38
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +124 -93
- package/dist/server.js.map +1 -1
- package/package.json +6 -5
- package/src/__tests__/carrier.test.ts +271 -0
- package/src/__tests__/format.test.ts +4 -4
- package/src/__tests__/proof-beside.test.ts +1 -1
- package/src/__tests__/tools.test.ts +6 -5
- package/src/format.ts +51 -16
- package/src/index.ts +1 -1
- package/src/instructions.ts +11 -0
- package/src/server.ts +129 -155
- package/src/__tests__/encoding.test.ts +0 -64
- package/src/__tests__/scan.test.ts +0 -80
- package/src/api.ts +0 -170
- package/src/encoding.ts +0 -72
- package/src/scan.ts +0 -173
- package/src/task.ts +0 -187
- package/src/types.ts +0 -96
package/src/api.ts
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @mikeargento/bitgraph-mcp: HTTP client for the hosted BitGraph API.
|
|
5
|
-
*
|
|
6
|
-
* The reads: the batch lookup, the proof detail, the number search. Making a
|
|
7
|
-
* BitGraph goes through the core package's own transport against the same
|
|
8
|
-
* site (its /api/fuse routes), which is what maintains the per-position
|
|
9
|
-
* by-digest index that makes every member of a set discoverable afterward;
|
|
10
|
-
* the one write here is a set/2's member evidence, indexed after the commit.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import { mapConcurrent } from "./encoding.js";
|
|
14
|
-
import type { BatchCheckResponse, ProofDetailResponse, SearchResponse, SetIndexResponse } from "./types.js";
|
|
15
|
-
|
|
16
|
-
export interface ApiConfig {
|
|
17
|
-
baseUrl: string;
|
|
18
|
-
apiKey?: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** Read configuration from the environment at call time (testable via env). */
|
|
22
|
-
export function configFromEnv(): ApiConfig {
|
|
23
|
-
const baseUrl = (process.env["BITGRAPH_API_URL"] ?? "https://bitgraph.ing").replace(/\/+$/, "");
|
|
24
|
-
const apiKey = process.env["BITGRAPH_API_KEY"];
|
|
25
|
-
return apiKey ? { baseUrl, apiKey } : { baseUrl };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Matches the batch endpoint's MAX_DIGESTS. */
|
|
29
|
-
export const BATCH_CHECK_LIMIT = 500;
|
|
30
|
-
/** Batch lookups in flight at once: a large folder is many pages. */
|
|
31
|
-
const BATCH_CHECK_CONCURRENCY = 4;
|
|
32
|
-
|
|
33
|
-
const CHECK_TIMEOUT_MS = 30_000;
|
|
34
|
-
const SET_INDEX_TIMEOUT_MS = 60_000;
|
|
35
|
-
|
|
36
|
-
export class ApiError extends Error {
|
|
37
|
-
readonly status: number;
|
|
38
|
-
readonly retryAfterSec: number | null;
|
|
39
|
-
|
|
40
|
-
constructor(status: number, message: string, retryAfterSec: number | null = null) {
|
|
41
|
-
super(message);
|
|
42
|
-
this.status = status;
|
|
43
|
-
this.retryAfterSec = retryAfterSec;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async function parseError(res: Response): Promise<ApiError> {
|
|
48
|
-
let message = res.statusText || `HTTP ${res.status}`;
|
|
49
|
-
try {
|
|
50
|
-
const body = (await res.json()) as { error?: string };
|
|
51
|
-
if (typeof body.error === "string" && body.error.length > 0) message = body.error;
|
|
52
|
-
} catch {
|
|
53
|
-
/* non-JSON error body */
|
|
54
|
-
}
|
|
55
|
-
const retryAfterRaw = res.headers.get("retry-after");
|
|
56
|
-
const retryAfterSec = retryAfterRaw !== null ? Number.parseInt(retryAfterRaw, 10) : null;
|
|
57
|
-
return new ApiError(res.status, message, Number.isFinite(retryAfterSec) ? retryAfterSec : null);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Redirects are refused everywhere: an Authorization header must never follow
|
|
62
|
-
* one to a different host, and the canonical endpoint serves without them.
|
|
63
|
-
*/
|
|
64
|
-
function redirectHint(err: unknown): never {
|
|
65
|
-
if (err instanceof TypeError && /redirect/i.test(err.message)) {
|
|
66
|
-
throw new Error(
|
|
67
|
-
"The BitGraph endpoint redirected. Set BITGRAPH_API_URL to the canonical https://bitgraph.ing (apex, no www)."
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
throw err;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async function getJson<T>(config: ApiConfig, path: string, timeoutMs: number): Promise<T> {
|
|
74
|
-
const res = await fetch(`${config.baseUrl}${path}`, {
|
|
75
|
-
method: "GET",
|
|
76
|
-
redirect: "error",
|
|
77
|
-
signal: AbortSignal.timeout(timeoutMs),
|
|
78
|
-
}).catch(redirectHint);
|
|
79
|
-
if (!res.ok) throw await parseError(res);
|
|
80
|
-
return (await res.json()) as T;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async function postJson<T>(
|
|
84
|
-
config: ApiConfig,
|
|
85
|
-
path: string,
|
|
86
|
-
body: unknown,
|
|
87
|
-
timeoutMs: number,
|
|
88
|
-
authenticated: boolean
|
|
89
|
-
): Promise<T> {
|
|
90
|
-
const headers: Record<string, string> = { "Content-Type": "application/json" };
|
|
91
|
-
if (authenticated && config.apiKey) headers["Authorization"] = `Bearer ${config.apiKey}`;
|
|
92
|
-
const res = await fetch(`${config.baseUrl}${path}`, {
|
|
93
|
-
method: "POST",
|
|
94
|
-
headers,
|
|
95
|
-
body: JSON.stringify(body),
|
|
96
|
-
redirect: "error",
|
|
97
|
-
signal: AbortSignal.timeout(timeoutMs),
|
|
98
|
-
}).catch(redirectHint);
|
|
99
|
-
if (!res.ok) throw await parseError(res);
|
|
100
|
-
return (await res.json()) as T;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Look up which digests are on record. Input digests must be URL-safe base64;
|
|
105
|
-
* the response is keyed by the exact strings sent. Pages of up to 500, a few
|
|
106
|
-
* in flight at once.
|
|
107
|
-
*/
|
|
108
|
-
export async function batchCheck(
|
|
109
|
-
config: ApiConfig,
|
|
110
|
-
urlSafeDigests: readonly string[]
|
|
111
|
-
): Promise<BatchCheckResponse> {
|
|
112
|
-
const chunks: string[][] = [];
|
|
113
|
-
for (let offset = 0; offset < urlSafeDigests.length; offset += BATCH_CHECK_LIMIT) {
|
|
114
|
-
chunks.push(urlSafeDigests.slice(offset, offset + BATCH_CHECK_LIMIT));
|
|
115
|
-
}
|
|
116
|
-
const pages = await mapConcurrent(chunks, BATCH_CHECK_CONCURRENCY, (chunk) =>
|
|
117
|
-
postJson<BatchCheckResponse>(config, "/api/proofs/batch", { digests: chunk }, CHECK_TIMEOUT_MS, false)
|
|
118
|
-
);
|
|
119
|
-
const merged: BatchCheckResponse = { results: {} };
|
|
120
|
-
for (const page of pages) Object.assign(merged.results, page.results);
|
|
121
|
-
return merged;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/** One request's worth of a set/2's member evidence, for the site to index. */
|
|
125
|
-
export interface SetIndexRequest {
|
|
126
|
-
/** The set proof's artifact digest, URL-safe base64. */
|
|
127
|
-
setDigest: string;
|
|
128
|
-
/** The set proof's epoch id, URL-safe base64. */
|
|
129
|
-
epoch: string;
|
|
130
|
-
/** The set proof's commit counter. */
|
|
131
|
-
counter: string;
|
|
132
|
-
members: unknown[];
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Index members of a set/2 from their evidence. The site reads the set proof
|
|
137
|
-
* from its own position, binds the root document, and checks every member's
|
|
138
|
-
* path before writing a key; a row that does not bind is rejected, never
|
|
139
|
-
* written.
|
|
140
|
-
*/
|
|
141
|
-
export async function indexSetMembers(config: ApiConfig, body: SetIndexRequest): Promise<SetIndexResponse> {
|
|
142
|
-
return postJson<SetIndexResponse>(config, "/api/fuse/set-index", body, SET_INDEX_TIMEOUT_MS, false);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/** Full detail for one digest: proof, all causal positions, anchor window. */
|
|
146
|
-
export async function getProofDetail(
|
|
147
|
-
config: ApiConfig,
|
|
148
|
-
urlSafeDigest: string,
|
|
149
|
-
counter?: string,
|
|
150
|
-
urlSafeEpoch?: string
|
|
151
|
-
): Promise<ProofDetailResponse> {
|
|
152
|
-
const params = new URLSearchParams();
|
|
153
|
-
if (counter !== undefined) params.set("counter", counter);
|
|
154
|
-
if (urlSafeEpoch !== undefined) params.set("epoch", urlSafeEpoch);
|
|
155
|
-
const query = params.size > 0 ? `?${params.toString()}` : "";
|
|
156
|
-
return getJson<ProofDetailResponse>(
|
|
157
|
-
config,
|
|
158
|
-
`/api/proofs/digest/${encodeURIComponent(urlSafeDigest)}${query}`,
|
|
159
|
-
CHECK_TIMEOUT_MS
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/** Resolve a BitGraph number (or digest string) to a digest via /api/search. */
|
|
164
|
-
export async function search(config: ApiConfig, q: string): Promise<SearchResponse> {
|
|
165
|
-
return getJson<SearchResponse>(
|
|
166
|
-
config,
|
|
167
|
-
`/api/search?q=${encodeURIComponent(q)}`,
|
|
168
|
-
CHECK_TIMEOUT_MS
|
|
169
|
-
);
|
|
170
|
-
}
|
package/src/encoding.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @mikeargento/bitgraph-mcp: digest encoding and file hashing.
|
|
5
|
-
*
|
|
6
|
-
* BitGraph uses two base64 forms for the same SHA-256 digest:
|
|
7
|
-
* standard base64 with padding inside proof objects and commit requests,
|
|
8
|
-
* URL-safe unpadded base64 in API lookups, S3 keys, and proof page URLs.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import { createHash } from "node:crypto";
|
|
12
|
-
import { createReadStream } from "node:fs";
|
|
13
|
-
import { stat } from "node:fs/promises";
|
|
14
|
-
|
|
15
|
-
/** Standard base64 → URL-safe unpadded base64. */
|
|
16
|
-
export function toUrlSafeB64(b64: string): string {
|
|
17
|
-
return b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* URL-safe base64 → standard base64 with padding.
|
|
22
|
-
* A no-op (apart from re-padding) when the input is already standard base64,
|
|
23
|
-
* so it is safe to call on either form.
|
|
24
|
-
*/
|
|
25
|
-
export function fromUrlSafeB64(urlSafe: string): string {
|
|
26
|
-
let b64 = urlSafe.replace(/-/g, "+").replace(/_/g, "/");
|
|
27
|
-
while (b64.length % 4 !== 0) b64 += "=";
|
|
28
|
-
return b64;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** True if the string plausibly encodes 32 bytes of SHA-256 in either base64 form. */
|
|
32
|
-
export function looksLikeDigest(s: string): boolean {
|
|
33
|
-
const normalized = fromUrlSafeB64(s.trim());
|
|
34
|
-
if (!/^[A-Za-z0-9+/]{43}=$/.test(normalized)) return false;
|
|
35
|
-
return Buffer.from(normalized, "base64").length === 32;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* SHA-256 of a file's bytes as standard base64.
|
|
40
|
-
* Streams from disk, so file size is not a memory concern. Only this digest
|
|
41
|
-
* ever leaves the machine; file contents are never uploaded.
|
|
42
|
-
*/
|
|
43
|
-
export async function sha256FileB64(path: string): Promise<string> {
|
|
44
|
-
const info = await stat(path);
|
|
45
|
-
if (!info.isFile()) {
|
|
46
|
-
throw new Error(`Not a regular file: ${path}`);
|
|
47
|
-
}
|
|
48
|
-
const hash = createHash("sha256");
|
|
49
|
-
for await (const chunk of createReadStream(path)) {
|
|
50
|
-
hash.update(chunk as Buffer);
|
|
51
|
-
}
|
|
52
|
-
return hash.digest("base64");
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** Run an async mapper over items with bounded concurrency, preserving order. */
|
|
56
|
-
export async function mapConcurrent<T, R>(
|
|
57
|
-
items: readonly T[],
|
|
58
|
-
concurrency: number,
|
|
59
|
-
fn: (item: T, index: number) => Promise<R>
|
|
60
|
-
): Promise<R[]> {
|
|
61
|
-
const results: R[] = new Array(items.length);
|
|
62
|
-
let next = 0;
|
|
63
|
-
async function worker(): Promise<void> {
|
|
64
|
-
while (next < items.length) {
|
|
65
|
-
const index = next++;
|
|
66
|
-
results[index] = await fn(items[index] as T, index);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
const workers = Array.from({ length: Math.min(concurrency, items.length) }, () => worker());
|
|
70
|
-
await Promise.all(workers);
|
|
71
|
-
return results;
|
|
72
|
-
}
|
package/src/scan.ts
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @mikeargento/bitgraph-mcp: the scan.
|
|
5
|
-
*
|
|
6
|
-
* One pass over each file yields its SHA-256 (the member's origin) and a
|
|
7
|
-
* hasher left open after the placement's prefix and the file's last byte, so
|
|
8
|
-
* the member's fused digest can be finished later, for whatever slot the set
|
|
9
|
-
* is made under, without reading the file again. A placement is prefix,
|
|
10
|
-
* original, suffix (its frame): trailer/1 has no prefix and container/2's is
|
|
11
|
-
* the original's tar header, which depends on the size alone; the suffix
|
|
12
|
-
* carries the commitment and is hashed at BitGraph time. Node's SHA-256 is
|
|
13
|
-
* the platform's own, and a Hash can be copied mid-stream, which is the whole
|
|
14
|
-
* trick: the copy takes the suffix, so no file is read twice or held in
|
|
15
|
-
* memory, and the original is never touched.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import { createHash, type Hash } from "node:crypto";
|
|
19
|
-
import { createReadStream } from "node:fs";
|
|
20
|
-
import { readdir, stat } from "node:fs/promises";
|
|
21
|
-
import { basename, join } from "node:path";
|
|
22
|
-
import { placementForBytes } from "@mikeargento/bitgraph";
|
|
23
|
-
import { getPlacement } from "@mikeargento/bitgraph-verify";
|
|
24
|
-
|
|
25
|
-
/** The placements the scan makes: chosen from the bytes by the core, never from the name. */
|
|
26
|
-
export type ScanPlacement = "trailer/1" | "container/2";
|
|
27
|
-
|
|
28
|
-
export interface ScannedFile {
|
|
29
|
-
path: string;
|
|
30
|
-
name: string;
|
|
31
|
-
size: number;
|
|
32
|
-
/** SHA-256 of the file, standard base64: the member's origin. */
|
|
33
|
-
digestB64: string;
|
|
34
|
-
originDigest: Uint8Array;
|
|
35
|
-
placement: ScanPlacement;
|
|
36
|
-
/**
|
|
37
|
-
* The hasher after the placement's prefix and the file's bytes, still open.
|
|
38
|
-
* Null when the file's length changed while it was read: its fused digest
|
|
39
|
-
* then needs the bytes again.
|
|
40
|
-
*/
|
|
41
|
-
state: Hash | null;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/** Bytes the placement decision reads: every magic number sits in the first 16. */
|
|
45
|
-
const SNIFF = 64;
|
|
46
|
-
|
|
47
|
-
/** Hash one file in a single pass. Throws, before any network call, when the path is not a regular file. */
|
|
48
|
-
export async function scanFile(path: string): Promise<ScannedFile> {
|
|
49
|
-
const info = await stat(path);
|
|
50
|
-
if (!info.isFile()) throw new Error(`Not a regular file: ${path}`);
|
|
51
|
-
const size = info.size;
|
|
52
|
-
const origin = createHash("sha256");
|
|
53
|
-
// Held in one object: the closures below assign to it.
|
|
54
|
-
const run: { fused: Hash | null; placement: ScanPlacement | null } = { fused: null, placement: null };
|
|
55
|
-
let head: Buffer | null = null;
|
|
56
|
-
let bytes = 0;
|
|
57
|
-
const start = (p: ScanPlacement) => {
|
|
58
|
-
run.placement = p;
|
|
59
|
-
// A second hasher runs over prefix and bytes when the prefix is not
|
|
60
|
-
// empty; with an empty prefix the origin hasher's own state is the fused state.
|
|
61
|
-
const prefix = getPlacement(p)?.scanPrefix?.(size) ?? null;
|
|
62
|
-
if (prefix !== null && prefix.length > 0) run.fused = createHash("sha256").update(prefix);
|
|
63
|
-
};
|
|
64
|
-
const feed = (chunk: Buffer) => {
|
|
65
|
-
origin.update(chunk);
|
|
66
|
-
if (run.fused !== null) run.fused.update(chunk);
|
|
67
|
-
};
|
|
68
|
-
for await (const raw of createReadStream(path)) {
|
|
69
|
-
const chunk = raw as Buffer;
|
|
70
|
-
if (run.placement === null) {
|
|
71
|
-
head = head === null ? chunk : Buffer.concat([head, chunk]);
|
|
72
|
-
if (head.length >= SNIFF) {
|
|
73
|
-
start(placementForBytes(new Uint8Array(head.subarray(0, SNIFF))));
|
|
74
|
-
feed(head);
|
|
75
|
-
head = null;
|
|
76
|
-
}
|
|
77
|
-
} else {
|
|
78
|
-
feed(chunk);
|
|
79
|
-
}
|
|
80
|
-
bytes += chunk.length;
|
|
81
|
-
}
|
|
82
|
-
if (run.placement === null) {
|
|
83
|
-
// A short file: decide from what there is, then hash it.
|
|
84
|
-
start(placementForBytes(new Uint8Array(head ?? Buffer.alloc(0))));
|
|
85
|
-
if (head !== null) feed(head);
|
|
86
|
-
}
|
|
87
|
-
const placement = run.placement as ScanPlacement;
|
|
88
|
-
const state = bytes !== size ? null : (run.fused ?? origin);
|
|
89
|
-
// When the origin hasher is the state its digest is taken from a copy, so it stays open.
|
|
90
|
-
const digest = state === origin ? origin.copy().digest() : origin.digest();
|
|
91
|
-
return {
|
|
92
|
-
path,
|
|
93
|
-
name: basename(path),
|
|
94
|
-
size,
|
|
95
|
-
digestB64: digest.toString("base64"),
|
|
96
|
-
originDigest: Uint8Array.from(digest),
|
|
97
|
-
placement,
|
|
98
|
-
state,
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* A member's fused digest for a slot: the open hasher, copied, finished with
|
|
104
|
-
* the placement's suffix for that slot's commitment. The hash of prefix,
|
|
105
|
-
* original, suffix is exactly the hash of the placement's own build, which
|
|
106
|
-
* a test pins.
|
|
107
|
-
*/
|
|
108
|
-
export function fusedDigestFor(file: ScannedFile, commitment: Uint8Array): Uint8Array {
|
|
109
|
-
if (file.state === null) throw new Error(`${file.path}: the file changed while it was read`);
|
|
110
|
-
const placement = getPlacement(file.placement);
|
|
111
|
-
if (placement?.frame === undefined) throw new Error(`placement ${file.placement} has no frame`);
|
|
112
|
-
const { suffix } = placement.frame({ originalSize: file.size, originDigest: file.originDigest, commitment });
|
|
113
|
-
return Uint8Array.from(file.state.copy().update(suffix).digest());
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export interface Expansion {
|
|
117
|
-
/** Regular files in the order given; a directory contributes its files sorted by name, depth first. */
|
|
118
|
-
files: string[];
|
|
119
|
-
/** How many of the inputs were directories. */
|
|
120
|
-
directories: number;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Paths to files: a regular file is itself; a directory is every regular
|
|
125
|
-
* file under it, recursively, with hidden entries (names starting with a
|
|
126
|
-
* dot) and symbolic links left out. The same file twice is one file. A path
|
|
127
|
-
* that is missing or neither kind, or more than `limit` files in all, throws
|
|
128
|
-
* before any network call; the message names every path that failed.
|
|
129
|
-
*/
|
|
130
|
-
export async function expandPaths(paths: readonly string[], limit: number): Promise<Expansion> {
|
|
131
|
-
const files: string[] = [];
|
|
132
|
-
const seen = new Set<string>();
|
|
133
|
-
const failures: string[] = [];
|
|
134
|
-
let directories = 0;
|
|
135
|
-
const push = (p: string) => {
|
|
136
|
-
if (seen.has(p)) return;
|
|
137
|
-
seen.add(p);
|
|
138
|
-
files.push(p);
|
|
139
|
-
if (files.length > limit) throw new Error(`more than ${limit} files; BitGraph fewer at a time`);
|
|
140
|
-
};
|
|
141
|
-
const walk = async (dir: string): Promise<void> => {
|
|
142
|
-
const entries = (await readdir(dir, { withFileTypes: true }))
|
|
143
|
-
.filter((e) => !e.name.startsWith("."))
|
|
144
|
-
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
145
|
-
for (const e of entries) {
|
|
146
|
-
const full = join(dir, e.name);
|
|
147
|
-
if (e.isSymbolicLink()) continue;
|
|
148
|
-
if (e.isDirectory()) await walk(full);
|
|
149
|
-
else if (e.isFile()) push(full);
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
for (const p of paths) {
|
|
153
|
-
try {
|
|
154
|
-
const info = await stat(p);
|
|
155
|
-
if (info.isDirectory()) {
|
|
156
|
-
directories += 1;
|
|
157
|
-
await walk(p);
|
|
158
|
-
} else if (info.isFile()) {
|
|
159
|
-
push(p);
|
|
160
|
-
} else {
|
|
161
|
-
throw new Error("not a regular file or a directory");
|
|
162
|
-
}
|
|
163
|
-
} catch (err) {
|
|
164
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
165
|
-
if (message.startsWith("more than ")) throw err;
|
|
166
|
-
failures.push(`${p}: ${message}`);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
if (failures.length > 0) {
|
|
170
|
-
throw new Error(`Could not read ${failures.length} path(s); nothing was BitGraphed.\n${failures.join("\n")}\nUse absolute paths to existing regular files or directories.`);
|
|
171
|
-
}
|
|
172
|
-
return { files, directories };
|
|
173
|
-
}
|
package/src/task.ts
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2024-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A position BEFORE the work: the task form of the stdio server.
|
|
5
|
-
*
|
|
6
|
-
* bitgraph_open with no files asks the site's own /api/fuse/allocate for a
|
|
7
|
-
* slot and returns its commitment, and nothing else. The caller puts the
|
|
8
|
-
* commitment into the task it is about to run (the prompt, the request, a
|
|
9
|
-
* seed, a line in the document) and, within the slot's life, commits the
|
|
10
|
-
* digest of those task bytes under the slot with the inline marker
|
|
11
|
-
* (bitgraph-fuse/1, title base64url): a verifier recomputes the commitment
|
|
12
|
-
* from the slot record and finds its base64url text inside the bytes.
|
|
13
|
-
* Outputs are recorded afterwards with bitgraph_record and sit later.
|
|
14
|
-
*
|
|
15
|
-
* The slot allocation and the commit go through the same two site routes
|
|
16
|
-
* the core package's fuse() uses; the recovery rule is the core's (read back
|
|
17
|
-
* by digest, refuse a proof under any other slot).
|
|
18
|
-
*/
|
|
19
|
-
import { readFile } from "node:fs/promises";
|
|
20
|
-
import { createHash } from "node:crypto";
|
|
21
|
-
import { bytesToBase64, computeSlotCommitment, findCommitment, inlineAttribution, verifyProofIntegrity, ENCODING_BASE64URL } from "@mikeargento/bitgraph-verify";
|
|
22
|
-
import type { BitGraphProof, SlotAllocation } from "@mikeargento/bitgraph-verify";
|
|
23
|
-
import { ApiError, type ApiConfig } from "./api.js";
|
|
24
|
-
import { toUrlSafeB64 } from "./encoding.js";
|
|
25
|
-
|
|
26
|
-
export const SLOT_TTL_SECONDS = 120;
|
|
27
|
-
const CHAIN = "bitgraph:main";
|
|
28
|
-
const B64_32 = /^[A-Za-z0-9+/]{43}=$/;
|
|
29
|
-
const B64_64 = /^[A-Za-z0-9+/]{86}==$/;
|
|
30
|
-
|
|
31
|
-
export interface TaskState {
|
|
32
|
-
v: 1;
|
|
33
|
-
task: true;
|
|
34
|
-
slot: SlotAllocation;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function isSlotRecord(x: unknown): x is SlotAllocation {
|
|
38
|
-
if (x === null || typeof x !== "object" || Array.isArray(x)) return false;
|
|
39
|
-
const s = x as Record<string, unknown>;
|
|
40
|
-
return s.version === "bitgraph/slot/1" && typeof s.nonceB64 === "string" && B64_32.test(s.nonceB64) && typeof s.counter === "string" && /^(0|[1-9][0-9]*)$/.test(s.counter)
|
|
41
|
-
&& typeof s.epochId === "string" && typeof s.publicKeyB64 === "string" && typeof s.signatureB64 === "string" && B64_64.test(s.signatureB64) && s.chainId === CHAIN;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function encodeTaskToken(state: TaskState): string {
|
|
45
|
-
return Buffer.from(JSON.stringify(state), "utf8").toString("base64url");
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function decodeTaskToken(token: string): TaskState | null {
|
|
49
|
-
let parsed: unknown;
|
|
50
|
-
try { parsed = JSON.parse(Buffer.from(token, "base64url").toString("utf8")); } catch { return null; }
|
|
51
|
-
if (typeof parsed !== "object" || parsed === null) return null;
|
|
52
|
-
const s = parsed as Record<string, unknown>;
|
|
53
|
-
if (s.v !== 1 || s.task !== true || !isSlotRecord(s.slot)) return null;
|
|
54
|
-
return { v: 1, task: true, slot: s.slot };
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
async function post(config: ApiConfig, path: string, body: unknown, timeoutMs: number): Promise<{ status: number; json: unknown; retryAfterSec: number | null }> {
|
|
58
|
-
const headers: Record<string, string> = { "content-type": "application/json", accept: "application/json" };
|
|
59
|
-
if (config.apiKey) headers["authorization"] = `Bearer ${config.apiKey}`;
|
|
60
|
-
const res = await fetch(`${config.baseUrl}${path}`, { method: "POST", headers, body: JSON.stringify(body), signal: AbortSignal.timeout(timeoutMs) });
|
|
61
|
-
let json: unknown = null;
|
|
62
|
-
try { json = await res.json(); } catch { /* non-JSON */ }
|
|
63
|
-
const raw = res.headers.get("retry-after");
|
|
64
|
-
const retry = raw !== null ? Number.parseInt(raw, 10) : NaN;
|
|
65
|
-
return { status: res.status, json, retryAfterSec: Number.isFinite(retry) ? retry : null };
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const messageOf = (json: unknown, fallback: string): string => {
|
|
69
|
-
const e = (json as { error?: unknown } | null)?.error;
|
|
70
|
-
return typeof e === "string" ? e : fallback;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
export interface Begun {
|
|
74
|
-
token: string;
|
|
75
|
-
/** Unpadded base64url: the string to put into the task. */
|
|
76
|
-
commitment: string;
|
|
77
|
-
commitmentB64: string;
|
|
78
|
-
slotCounter: string;
|
|
79
|
-
epoch: string;
|
|
80
|
-
floor: { block: number } | null;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** Step one of the task form: a held slot and its commitment, before any work exists. */
|
|
84
|
-
export async function beginTask(config: ApiConfig): Promise<Begun> {
|
|
85
|
-
const r = await post(config, "/api/fuse/allocate", {}, 20_000);
|
|
86
|
-
if (r.status !== 200) throw new ApiError(r.status, messageOf(r.json, `allocation refused (${r.status})`), r.retryAfterSec);
|
|
87
|
-
const slot = (r.json as { slot?: unknown } | null)?.slot;
|
|
88
|
-
if (!isSlotRecord(slot)) throw new ApiError(502, "the allocation response is not a slot record on bitgraph:main");
|
|
89
|
-
const commitmentB64 = bytesToBase64(computeSlotCommitment(slot));
|
|
90
|
-
let floor: Begun["floor"] = null;
|
|
91
|
-
try {
|
|
92
|
-
const res = await fetch(`${config.baseUrl}/api/proofs/anchors?counter=${encodeURIComponent(slot.counter)}&epoch=${encodeURIComponent(slot.epochId)}&before=1`, { headers: { accept: "application/json" }, signal: AbortSignal.timeout(10_000) });
|
|
93
|
-
const data = res.status === 200 ? ((await res.json()) as { anchors?: Array<{ commit?: { anchor?: { blockNumber?: number } } }> }) : null;
|
|
94
|
-
const b = data?.anchors?.[0]?.commit?.anchor?.blockNumber;
|
|
95
|
-
if (typeof b === "number") floor = { block: b };
|
|
96
|
-
} catch { floor = null; }
|
|
97
|
-
return { token: encodeTaskToken({ v: 1, task: true, slot }), commitment: toUrlSafeB64(commitmentB64), commitmentB64, slotCounter: slot.counter, epoch: toUrlSafeB64(slot.epochId), floor };
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export interface SealedTask {
|
|
101
|
-
proof: BitGraphProof;
|
|
102
|
-
artifactDigestB64: string;
|
|
103
|
-
/** Byte offsets of the commitment string inside the task bytes, when the bytes were given. */
|
|
104
|
-
offsets: number[] | null;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Step two: the task bytes (a path, or the bytes themselves) sealed under
|
|
109
|
-
* the held slot with the inline marker. When the bytes are in hand the
|
|
110
|
-
* commitment is looked for BEFORE the commit, so a task that does not carry
|
|
111
|
-
* it is refused without spending the slot.
|
|
112
|
-
*/
|
|
113
|
-
export async function sealTask(config: ApiConfig, state: TaskState, task: { path: string } | { bytes: Uint8Array } | { digestB64: string }): Promise<SealedTask> {
|
|
114
|
-
const { slot } = state;
|
|
115
|
-
const commitment = computeSlotCommitment(slot);
|
|
116
|
-
let bytes: Uint8Array | null = null;
|
|
117
|
-
let artifactDigestB64: string;
|
|
118
|
-
if ("digestB64" in task) {
|
|
119
|
-
if (!B64_32.test(task.digestB64)) throw new ApiError(400, "artifact digest must be a base64 SHA-256");
|
|
120
|
-
artifactDigestB64 = task.digestB64;
|
|
121
|
-
} else {
|
|
122
|
-
bytes = "path" in task ? new Uint8Array(await readFile(task.path)) : task.bytes;
|
|
123
|
-
artifactDigestB64 = createHash("sha256").update(bytes).digest("base64");
|
|
124
|
-
}
|
|
125
|
-
const offsets = bytes === null ? null : findCommitment(bytes, commitment, ENCODING_BASE64URL);
|
|
126
|
-
if (offsets !== null && offsets.length === 0) {
|
|
127
|
-
throw new ApiError(400, `the task bytes do not contain the commitment string ${toUrlSafeB64(bytesToBase64(commitment))}; put it in before sealing. Nothing was committed and the slot is still held.`);
|
|
128
|
-
}
|
|
129
|
-
const attribution = inlineAttribution();
|
|
130
|
-
const r = await post(config, "/api/fuse/commit", { digests: [{ digestB64: artifactDigestB64, hashAlg: "sha256" }], slotId: slot.nonceB64, slot, chainId: CHAIN, attribution }, 40_000);
|
|
131
|
-
let proof: BitGraphProof | null = null;
|
|
132
|
-
if (r.status === 200) proof = ((r.json as { proof?: BitGraphProof } | null)?.proof ?? null);
|
|
133
|
-
else if (r.status === 409 || r.status === 503) proof = await recover(config, artifactDigestB64, slot);
|
|
134
|
-
if (proof === null) throw new ApiError(r.status, messageOf(r.json, `commit refused (${r.status})`), r.retryAfterSec);
|
|
135
|
-
if (proof.artifact?.digestB64 !== artifactDigestB64 || proof.commit?.nonceB64 !== slot.nonceB64 || proof.slotAllocation?.nonceB64 !== slot.nonceB64) throw new ApiError(502, "the boundary returned a proof under a different slot; nothing is labelled sealed");
|
|
136
|
-
const a = proof.attribution;
|
|
137
|
-
if (a?.name !== attribution.name || a?.title !== attribution.title || a?.message !== undefined) throw new ApiError(502, "the returned proof does not carry the inline marker that was sent; nothing is labelled sealed");
|
|
138
|
-
const integrity = await verifyProofIntegrity({ proof });
|
|
139
|
-
if (!integrity.valid) throw new ApiError(502, `the returned proof does not verify: ${integrity.reason ?? "unknown reason"}`);
|
|
140
|
-
return { proof, artifactDigestB64, offsets };
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
async function recover(config: ApiConfig, artifactDigestB64: string, slot: SlotAllocation): Promise<BitGraphProof | null> {
|
|
144
|
-
for (let attempt = 0; attempt < 4; attempt++) {
|
|
145
|
-
if (attempt > 0) await new Promise((r) => setTimeout(r, 1500));
|
|
146
|
-
try {
|
|
147
|
-
const res = await fetch(`${config.baseUrl}/api/proofs/${encodeURIComponent(toUrlSafeB64(artifactDigestB64))}`, { headers: { accept: "application/json" }, signal: AbortSignal.timeout(15_000) });
|
|
148
|
-
if (res.status !== 200) continue;
|
|
149
|
-
const j = (await res.json()) as { proofs?: Array<{ proof?: BitGraphProof }> } | null;
|
|
150
|
-
for (const e of j?.proofs ?? []) {
|
|
151
|
-
const p = e.proof;
|
|
152
|
-
if (p && p.artifact?.digestB64 === artifactDigestB64 && p.commit?.nonceB64 === slot.nonceB64) return p;
|
|
153
|
-
}
|
|
154
|
-
} catch { /* try again */ }
|
|
155
|
-
}
|
|
156
|
-
return null;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
export const TASK_INSTRUCTIONS =
|
|
160
|
-
"You hold a position and its commitment, and no work exists yet. Put the commitment string INSIDE THE OUTPUT you are about to make whenever its format can hold text (SVG, HTML, code, JSON, Markdown, CSV, a document): as a comment, a field, a line that stays in the file. " +
|
|
161
|
-
`When the output exists, call bitgraph_commit with this fuse_token and the output file's path, within ${SLOT_TTL_SECONDS} seconds of opening: the output itself is sealed under the position, and the proof is written beside it. ` +
|
|
162
|
-
"If the output cannot hold text (a PNG, audio, video) or will take longer than that to make, put the commitment into a task file instead (the exact prompt or request you send), seal that file the same way, and when the output exists record it with bitgraph_record: the task is sealed before the output existed, and the output is recorded after. " +
|
|
163
|
-
"Keep the exact bytes that were sealed unchanged: a verifier recomputes the commitment from the proof and looks for the string inside them. " +
|
|
164
|
-
"Leave the proof file as written, whole, every field, environment.attestation.reportB64 included: a proof missing slotAllocation, environment, or the attestation cannot be verified.";
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Write a sealed task's proof beside the file it is about, whole, and say
|
|
168
|
-
* where. `make_task.txt` gets `make_task.proof.json`; a name already taken
|
|
169
|
-
* gets the position's counter in it rather than being overwritten.
|
|
170
|
-
*
|
|
171
|
-
* ⚠️ THE SERVER WRITES IT, NOT THE AGENT. Handed the JSON to save, an agent
|
|
172
|
-
* tidied it and dropped slotAllocation and environment, and the signature
|
|
173
|
-
* had no body left to be checked against (Grok, 2026-09-13). A file this
|
|
174
|
-
* process writes is the proof exactly as the boundary returned it.
|
|
175
|
-
*/
|
|
176
|
-
export async function writeProofBeside(sealedPath: string, proof: BitGraphProof): Promise<string> {
|
|
177
|
-
const { writeFile, access } = await import("node:fs/promises");
|
|
178
|
-
const { basename, dirname, extname, join } = await import("node:path");
|
|
179
|
-
const dir = dirname(sealedPath);
|
|
180
|
-
const base = basename(sealedPath);
|
|
181
|
-
const stem = extname(base) === "" ? base : base.slice(0, -extname(base).length);
|
|
182
|
-
const counter = proof.commit?.counter ?? "0";
|
|
183
|
-
let target = join(dir, `${stem}.proof.json`);
|
|
184
|
-
if (await access(target).then(() => true).catch(() => false)) target = join(dir, `${stem}.proof.${counter}.json`);
|
|
185
|
-
await writeFile(target, JSON.stringify(proof, null, 2) + "\n", { flag: "wx" });
|
|
186
|
-
return target;
|
|
187
|
-
}
|