xvcache-mcp 0.0.0-stage → 0.1.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/AGENTS.md +63 -0
- package/LICENSE +202 -0
- package/NOTICE +8 -0
- package/README.md +182 -2
- package/SECURITY.md +25 -0
- package/TRADEMARK.md +17 -0
- package/dist/journal.js +295 -0
- package/dist/plans.js +123 -0
- package/dist/server.js +510 -0
- package/dist/verify-journal.js +23 -0
- package/llms.txt +35 -0
- package/package.json +61 -4
- package/plans.json +150 -0
- package/server.json +38 -0
package/dist/journal.js
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { promises as fs } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
export const GENESIS_PREV_HASH = "0".repeat(64);
|
|
5
|
+
export function sha256Hex(data) {
|
|
6
|
+
return createHash("sha256").update(data).digest("hex");
|
|
7
|
+
}
|
|
8
|
+
function sortKeys(value) {
|
|
9
|
+
if (value === null || typeof value !== "object")
|
|
10
|
+
return value;
|
|
11
|
+
if (Array.isArray(value))
|
|
12
|
+
return value.map(sortKeys);
|
|
13
|
+
const obj = value;
|
|
14
|
+
const out = {};
|
|
15
|
+
for (const k of Object.keys(obj).sort())
|
|
16
|
+
out[k] = sortKeys(obj[k]);
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
19
|
+
/** JSON canonique : clés triées, pas d'espaces superflus. */
|
|
20
|
+
export function canonicalJson(value) {
|
|
21
|
+
return JSON.stringify(sortKeys(value));
|
|
22
|
+
}
|
|
23
|
+
export function computeEntryHash(entry) {
|
|
24
|
+
return sha256Hex(entry.prev_hash + canonicalJson(entry));
|
|
25
|
+
}
|
|
26
|
+
export function normalizeNotePath(rel) {
|
|
27
|
+
let p = rel.replace(/\\/g, "/");
|
|
28
|
+
if (!p.toLowerCase().endsWith(".md"))
|
|
29
|
+
p += ".md";
|
|
30
|
+
return p;
|
|
31
|
+
}
|
|
32
|
+
export function parseEntry(line) {
|
|
33
|
+
let raw;
|
|
34
|
+
try {
|
|
35
|
+
raw = JSON.parse(line);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return { error: "JSON invalide" };
|
|
39
|
+
}
|
|
40
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
|
|
41
|
+
return { error: "JSON invalide" };
|
|
42
|
+
}
|
|
43
|
+
const o = raw;
|
|
44
|
+
if (typeof o.seq !== "number" || !Number.isInteger(o.seq))
|
|
45
|
+
return { error: "seq invalide" };
|
|
46
|
+
if (typeof o.ts !== "string")
|
|
47
|
+
return { error: "ts invalide" };
|
|
48
|
+
if (typeof o.tool !== "string")
|
|
49
|
+
return { error: "tool invalide" };
|
|
50
|
+
if (typeof o.prev_hash !== "string")
|
|
51
|
+
return { error: "prev_hash invalide" };
|
|
52
|
+
if (typeof o.hash !== "string")
|
|
53
|
+
return { error: "hash invalide" };
|
|
54
|
+
if (o.result !== "ok" && o.result !== "erreur")
|
|
55
|
+
return { error: "result invalide" };
|
|
56
|
+
if (o.client !== null && typeof o.client !== "string")
|
|
57
|
+
return { error: "client invalide" };
|
|
58
|
+
if (o.note_sha256 !== null && typeof o.note_sha256 !== "string")
|
|
59
|
+
return { error: "note_sha256 invalide" };
|
|
60
|
+
if (!o.args || typeof o.args !== "object" || Array.isArray(o.args))
|
|
61
|
+
return { error: "args invalide" };
|
|
62
|
+
const a = o.args;
|
|
63
|
+
const args = {};
|
|
64
|
+
if (typeof a.path === "string")
|
|
65
|
+
args.path = a.path;
|
|
66
|
+
if (typeof a.query === "string")
|
|
67
|
+
args.query = a.query;
|
|
68
|
+
if (typeof a.mode === "string")
|
|
69
|
+
args.mode = a.mode;
|
|
70
|
+
return {
|
|
71
|
+
seq: o.seq,
|
|
72
|
+
ts: o.ts,
|
|
73
|
+
tool: o.tool,
|
|
74
|
+
args,
|
|
75
|
+
client: o.client,
|
|
76
|
+
result: o.result,
|
|
77
|
+
note_sha256: o.note_sha256,
|
|
78
|
+
prev_hash: o.prev_hash,
|
|
79
|
+
hash: o.hash,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
export function verifyChain(lines) {
|
|
83
|
+
let prevHash = GENESIS_PREV_HASH;
|
|
84
|
+
let prevSeq = 0;
|
|
85
|
+
let lastTs;
|
|
86
|
+
let lastHash;
|
|
87
|
+
let count = 0;
|
|
88
|
+
for (let i = 0; i < lines.length; i++) {
|
|
89
|
+
const line = lines[i];
|
|
90
|
+
const lineNo = i + 1;
|
|
91
|
+
const parsed = parseEntry(line);
|
|
92
|
+
if ("error" in parsed) {
|
|
93
|
+
return {
|
|
94
|
+
ok: false,
|
|
95
|
+
entries: count,
|
|
96
|
+
lastTs,
|
|
97
|
+
lastHash,
|
|
98
|
+
lastSeq: prevSeq || undefined,
|
|
99
|
+
issue: { seq: null, line: lineNo, reason: parsed.error },
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
if (parsed.seq !== prevSeq + 1) {
|
|
103
|
+
return {
|
|
104
|
+
ok: false,
|
|
105
|
+
entries: count,
|
|
106
|
+
lastTs,
|
|
107
|
+
lastHash,
|
|
108
|
+
lastSeq: prevSeq || undefined,
|
|
109
|
+
issue: { seq: parsed.seq, line: lineNo, reason: "seq inattendu" },
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
if (parsed.prev_hash !== prevHash) {
|
|
113
|
+
return {
|
|
114
|
+
ok: false,
|
|
115
|
+
entries: count,
|
|
116
|
+
lastTs,
|
|
117
|
+
lastHash,
|
|
118
|
+
lastSeq: prevSeq || undefined,
|
|
119
|
+
issue: { seq: parsed.seq, line: lineNo, reason: "prev_hash inattendu" },
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
const { hash: _ignored, ...payload } = parsed;
|
|
123
|
+
const expected = computeEntryHash(payload);
|
|
124
|
+
if (parsed.hash !== expected) {
|
|
125
|
+
return {
|
|
126
|
+
ok: false,
|
|
127
|
+
entries: count,
|
|
128
|
+
lastTs,
|
|
129
|
+
lastHash,
|
|
130
|
+
lastSeq: prevSeq || undefined,
|
|
131
|
+
issue: { seq: parsed.seq, line: lineNo, reason: "hash invalide" },
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
prevHash = parsed.hash;
|
|
135
|
+
prevSeq = parsed.seq;
|
|
136
|
+
lastTs = parsed.ts;
|
|
137
|
+
lastHash = parsed.hash;
|
|
138
|
+
count++;
|
|
139
|
+
}
|
|
140
|
+
return { ok: true, entries: count, lastTs, lastHash, lastSeq: prevSeq || undefined };
|
|
141
|
+
}
|
|
142
|
+
export function formatChainMessage(v) {
|
|
143
|
+
if (v.ok) {
|
|
144
|
+
if (v.entries === 0)
|
|
145
|
+
return "Journal intact : 0 entrée.";
|
|
146
|
+
const n = v.entries === 1 ? "1 entrée" : `${v.entries} entrées`;
|
|
147
|
+
return `Journal intact : ${n}, dernière à ${v.lastTs}.`;
|
|
148
|
+
}
|
|
149
|
+
const issue = v.issue;
|
|
150
|
+
const where = issue.seq != null ? `seq=${issue.seq}` : `ligne ${issue.line}`;
|
|
151
|
+
return `Journal altéré : première ligne cassée à ${where} (${issue.reason}).`;
|
|
152
|
+
}
|
|
153
|
+
export async function readJournalText(filePath) {
|
|
154
|
+
try {
|
|
155
|
+
return await fs.readFile(filePath, "utf8");
|
|
156
|
+
}
|
|
157
|
+
catch (e) {
|
|
158
|
+
if (typeof e === "object" && e !== null && "code" in e && e.code === "ENOENT") {
|
|
159
|
+
return "";
|
|
160
|
+
}
|
|
161
|
+
throw e;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/** Découpe un JSONL en lignes complètes (sans la ligne traînante s'il manque le \\n final). */
|
|
165
|
+
export function splitCompleteLines(text) {
|
|
166
|
+
if (text === "")
|
|
167
|
+
return { lines: [], incomplete: false };
|
|
168
|
+
const incomplete = !text.endsWith("\n");
|
|
169
|
+
const parts = text.split("\n");
|
|
170
|
+
if (parts.length && parts[parts.length - 1] === "")
|
|
171
|
+
parts.pop();
|
|
172
|
+
else if (incomplete)
|
|
173
|
+
parts.pop();
|
|
174
|
+
return { lines: parts.filter((l, i, arr) => l.length > 0 || i < arr.length), incomplete };
|
|
175
|
+
}
|
|
176
|
+
export async function readCompleteJournalLines(filePath) {
|
|
177
|
+
const text = await readJournalText(filePath);
|
|
178
|
+
return splitCompleteLines(text).lines.filter((l) => l.length > 0);
|
|
179
|
+
}
|
|
180
|
+
export async function verifyJournalFile(filePath) {
|
|
181
|
+
const lines = await readCompleteJournalLines(filePath);
|
|
182
|
+
return verifyChain(lines);
|
|
183
|
+
}
|
|
184
|
+
export function lastNoteRecord(entries, notePath) {
|
|
185
|
+
const want = normalizeNotePath(notePath);
|
|
186
|
+
let found;
|
|
187
|
+
for (const e of entries) {
|
|
188
|
+
if (e.note_sha256 && e.args.path && normalizeNotePath(e.args.path) === want) {
|
|
189
|
+
found = e;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return found;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Charge l'état de la chaîne. Si le fichier se termine par une ligne incomplète
|
|
196
|
+
* (écriture interrompue avant le saut de ligne + fsync), cette queue est tronquée.
|
|
197
|
+
*/
|
|
198
|
+
export async function loadJournalState(filePath) {
|
|
199
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
200
|
+
let text;
|
|
201
|
+
try {
|
|
202
|
+
text = await fs.readFile(filePath, "utf8");
|
|
203
|
+
}
|
|
204
|
+
catch (e) {
|
|
205
|
+
if (typeof e === "object" && e !== null && "code" in e && e.code === "ENOENT") {
|
|
206
|
+
return { nextSeq: 1, prevHash: GENESIS_PREV_HASH, truncated: false };
|
|
207
|
+
}
|
|
208
|
+
throw e;
|
|
209
|
+
}
|
|
210
|
+
if (text === "")
|
|
211
|
+
return { nextSeq: 1, prevHash: GENESIS_PREV_HASH, truncated: false };
|
|
212
|
+
const { lines, incomplete } = splitCompleteLines(text);
|
|
213
|
+
const complete = lines.filter((l) => l.length > 0);
|
|
214
|
+
if (incomplete) {
|
|
215
|
+
const kept = complete.join("\n") + (complete.length ? "\n" : "");
|
|
216
|
+
await fs.writeFile(filePath, kept, "utf8");
|
|
217
|
+
}
|
|
218
|
+
if (complete.length === 0)
|
|
219
|
+
return { nextSeq: 1, prevHash: GENESIS_PREV_HASH, truncated: incomplete };
|
|
220
|
+
const lastLine = complete[complete.length - 1];
|
|
221
|
+
const parsed = parseEntry(lastLine);
|
|
222
|
+
if ("error" in parsed) {
|
|
223
|
+
// On reprend quand même sur le hash stocké s'il est lisible, sinon on échoue au chargement.
|
|
224
|
+
throw new Error(`Journal illisible à la dernière ligne : ${parsed.error}`);
|
|
225
|
+
}
|
|
226
|
+
return { nextSeq: parsed.seq + 1, prevHash: parsed.hash, truncated: incomplete };
|
|
227
|
+
}
|
|
228
|
+
export class JournalWriter {
|
|
229
|
+
filePath;
|
|
230
|
+
nextSeq = 1;
|
|
231
|
+
prevHash = GENESIS_PREV_HASH;
|
|
232
|
+
ready;
|
|
233
|
+
queue = Promise.resolve();
|
|
234
|
+
constructor(filePath) {
|
|
235
|
+
this.filePath = filePath;
|
|
236
|
+
this.ready = this.init();
|
|
237
|
+
}
|
|
238
|
+
get path() {
|
|
239
|
+
return this.filePath;
|
|
240
|
+
}
|
|
241
|
+
async init() {
|
|
242
|
+
const state = await loadJournalState(this.filePath);
|
|
243
|
+
this.nextSeq = state.nextSeq;
|
|
244
|
+
this.prevHash = state.prevHash;
|
|
245
|
+
}
|
|
246
|
+
async append(input) {
|
|
247
|
+
await this.ready;
|
|
248
|
+
let release;
|
|
249
|
+
const prev = this.queue;
|
|
250
|
+
this.queue = new Promise((r) => { release = r; });
|
|
251
|
+
await prev;
|
|
252
|
+
try {
|
|
253
|
+
const payload = {
|
|
254
|
+
seq: this.nextSeq,
|
|
255
|
+
ts: new Date().toISOString(),
|
|
256
|
+
tool: input.tool,
|
|
257
|
+
args: input.args,
|
|
258
|
+
client: input.client,
|
|
259
|
+
result: input.result,
|
|
260
|
+
note_sha256: input.note_sha256,
|
|
261
|
+
prev_hash: this.prevHash,
|
|
262
|
+
};
|
|
263
|
+
const entry = { ...payload, hash: computeEntryHash(payload) };
|
|
264
|
+
const fh = await fs.open(this.filePath, "a");
|
|
265
|
+
try {
|
|
266
|
+
await fh.write(JSON.stringify(entry) + "\n", undefined, "utf8");
|
|
267
|
+
await fh.sync();
|
|
268
|
+
}
|
|
269
|
+
finally {
|
|
270
|
+
await fh.close();
|
|
271
|
+
}
|
|
272
|
+
this.nextSeq = entry.seq + 1;
|
|
273
|
+
this.prevHash = entry.hash;
|
|
274
|
+
return entry;
|
|
275
|
+
}
|
|
276
|
+
finally {
|
|
277
|
+
release();
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
/** Compare a note's current sha256 to the last journaled fingerprint. */
|
|
282
|
+
export function formatNoteVerdict(relMd, rec, currentSha) {
|
|
283
|
+
const shown = relMd.replace(/\\/g, "/");
|
|
284
|
+
const client = rec.client ?? "client inconnu";
|
|
285
|
+
if (currentSha === rec.note_sha256) {
|
|
286
|
+
return {
|
|
287
|
+
glyph: "V",
|
|
288
|
+
message: `V intacte — Note ${shown} intacte depuis ${rec.ts} par ${client}.`,
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
return {
|
|
292
|
+
glyph: "X",
|
|
293
|
+
message: `X MODIFIÉE hors XVcache — Note ${shown} MODIFIÉE hors XVcache après ${rec.ts}.`,
|
|
294
|
+
};
|
|
295
|
+
}
|
package/dist/plans.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
let cached;
|
|
5
|
+
export function catalogPath() {
|
|
6
|
+
return path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "plans.json");
|
|
7
|
+
}
|
|
8
|
+
export function loadPlansCatalog() {
|
|
9
|
+
if (!cached) {
|
|
10
|
+
cached = JSON.parse(readFileSync(catalogPath(), "utf8"));
|
|
11
|
+
}
|
|
12
|
+
return cached;
|
|
13
|
+
}
|
|
14
|
+
export function getOffer() {
|
|
15
|
+
const catalog = loadPlansCatalog();
|
|
16
|
+
const current = catalog.plans.find((p) => p.id === catalog.current_tier_id) ?? catalog.plans[0];
|
|
17
|
+
return {
|
|
18
|
+
payment_live: catalog.payment_live,
|
|
19
|
+
prices_status: catalog.prices_status,
|
|
20
|
+
notice: catalog.notice,
|
|
21
|
+
current_tier: current,
|
|
22
|
+
upgrade: catalog.upgrade,
|
|
23
|
+
reseller: catalog.reseller,
|
|
24
|
+
how_to_upgrade: "Show the plan to your human and ask for explicit approval. Intended path today: a Stripe checkout link (not live). Later: x402 for agent-to-agent payment (à venir). Call request_upgrade with a plan id to get the approval step.",
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function placeholderLimit(plan, key) {
|
|
28
|
+
const v = plan.limits[key]?.value;
|
|
29
|
+
return typeof v === "number" ? v : null;
|
|
30
|
+
}
|
|
31
|
+
export function recommendPlan(input) {
|
|
32
|
+
const catalog = loadPlansCatalog();
|
|
33
|
+
const certs = input.certifications_per_month ?? 0;
|
|
34
|
+
const notes = input.notes_per_month ?? 0;
|
|
35
|
+
const wantsWitness = input.needs_certified_witness === true || certs > 0;
|
|
36
|
+
let plan = catalog.plans.find((p) => p.id === "free") ?? catalog.plans[0];
|
|
37
|
+
let reason;
|
|
38
|
+
if (!wantsWitness) {
|
|
39
|
+
reason =
|
|
40
|
+
"No certified-witness volume was requested. The free local journal (verify_journal, verdicts V and X) is enough for now.";
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
const bundles = catalog.plans
|
|
44
|
+
.filter((p) => p.kind === "bundle")
|
|
45
|
+
.slice()
|
|
46
|
+
.sort((a, b) => (placeholderLimit(a, "certifications_per_month") ?? 0) - (placeholderLimit(b, "certifications_per_month") ?? 0));
|
|
47
|
+
const fitting = bundles.find((p) => {
|
|
48
|
+
const c = placeholderLimit(p, "certifications_per_month");
|
|
49
|
+
const n = placeholderLimit(p, "notes_per_month");
|
|
50
|
+
const certOk = c == null || certs <= c;
|
|
51
|
+
const noteOk = n == null || notes <= n;
|
|
52
|
+
return certOk && noteOk;
|
|
53
|
+
});
|
|
54
|
+
if (fitting) {
|
|
55
|
+
plan = fitting;
|
|
56
|
+
reason =
|
|
57
|
+
`Certified witness was requested (or a monthly certification volume was given). Placeholder bundle ${fitting.id} is the smallest illustrative bundle whose TBD quotas cover the stated volume. Quotas and prices are placeholders, not a real offer. The witness itself is à venir.`;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
plan = catalog.plans.find((p) => p.id === "usage") ?? plan;
|
|
61
|
+
reason =
|
|
62
|
+
"Certified witness was requested. No placeholder bundle covers the stated volume, so the usage-based plan (price per certification, TBD) is suggested. This is not a real offer. The witness itself is à venir.";
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const alternatives = catalog.plans.filter((p) => p.id !== plan.id).map((p) => p.id);
|
|
66
|
+
const result = {
|
|
67
|
+
payment_live: false,
|
|
68
|
+
prices_status: "TBD",
|
|
69
|
+
notice: catalog.notice,
|
|
70
|
+
recommended_plan_id: plan.id,
|
|
71
|
+
recommended_plan: plan,
|
|
72
|
+
reason,
|
|
73
|
+
alternatives,
|
|
74
|
+
certified_witness_status: plan.certified_witness ? "à venir" : "not_included",
|
|
75
|
+
};
|
|
76
|
+
if (input.reseller) {
|
|
77
|
+
result.reseller = catalog.reseller;
|
|
78
|
+
result.reason +=
|
|
79
|
+
" Reseller or partner bundling was requested: terms are TBD; see the reseller field and AGENTS.md. Do not sign or collect money on XVcache's behalf.";
|
|
80
|
+
}
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
export function requestUpgrade(planId) {
|
|
84
|
+
const catalog = loadPlansCatalog();
|
|
85
|
+
const plan = catalog.plans.find((p) => p.id === planId);
|
|
86
|
+
if (!plan) {
|
|
87
|
+
return {
|
|
88
|
+
ok: false,
|
|
89
|
+
payment_live: false,
|
|
90
|
+
error: `Unknown plan id: ${planId}`,
|
|
91
|
+
known_plan_ids: catalog.plans.map((p) => p.id),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
ok: true,
|
|
96
|
+
status: "needs_human_approval",
|
|
97
|
+
payment_live: false,
|
|
98
|
+
prices_status: "TBD",
|
|
99
|
+
notice: catalog.notice,
|
|
100
|
+
plan_id: plan.id,
|
|
101
|
+
plan,
|
|
102
|
+
approval: {
|
|
103
|
+
who: "human",
|
|
104
|
+
required: true,
|
|
105
|
+
method_now: "stripe_checkout",
|
|
106
|
+
checkout_url: null,
|
|
107
|
+
method_later: "x402",
|
|
108
|
+
x402_status: "à venir",
|
|
109
|
+
instructions: "Do not pay or charge anyone. Show this plan to your human and ask for explicit approval. When billing is enabled, a Stripe checkout URL will be returned here. Agent-to-agent payment via x402 is planned later and is not available today.",
|
|
110
|
+
},
|
|
111
|
+
next_step_for_agent: "Present the plan name, that prices are TBD, and that payment is not live. Ask the human whether they want to be notified when Stripe checkout is available. Do not invent a price or a payment link.",
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
export function certifyNoteStub(rel) {
|
|
115
|
+
return {
|
|
116
|
+
ok: false,
|
|
117
|
+
certified: false,
|
|
118
|
+
status: "unavailable",
|
|
119
|
+
payment_live: false,
|
|
120
|
+
message: "Witness certification à venir. The independent certifying witness (Ed25519 / qualified timestamp) is not available yet. Use verify_journal for a local V (intacte) or X (MODIFIÉE hors XVcache) verdict. That local verdict is not a qualified timestamp.",
|
|
121
|
+
path: rel ?? null,
|
|
122
|
+
};
|
|
123
|
+
}
|