visualproof 0.9.0 → 0.10.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/README.md +2 -1
- package/dist/mcp/visualproof-mcp.mjs +30 -5
- package/dist/server/agententry.js +13 -6
- package/dist/server/app.js +6 -2
- package/dist/server/appagent.js +1 -1
- package/dist/server/appshare.js +119 -1
- package/dist/server/apptalk.js +26 -0
- package/dist/server/children.js +6 -2
- package/dist/server/claudetokens.js +113 -0
- package/dist/server/doctor.js +7 -0
- package/dist/server/hosted.js +480 -0
- package/dist/server/live.js +20 -2
- package/dist/server/main.js +27 -0
- package/dist/server/mcptools.js +4 -2
- package/dist/server/options.js +15 -2
- package/dist/server/presence.js +2 -2
- package/dist/server/prompt.js +25 -0
- package/dist/server/tokens.js +7 -4
- package/dist/shared/collab.js +3 -0
- package/dist/web/bundle/app.js +103 -13
- package/dist/web/index.html +1 -0
- package/dist/web/style.css +3 -0
- package/docs/agent-api/mcp.md +3 -1
- package/docs/agent-api/talking.md +7 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -84,7 +84,8 @@ project's edge.
|
|
|
84
84
|
declarations, leave notes, link papers to declarations, and see what changed since an opinion was
|
|
85
85
|
given. Everyone on the page sees it live.
|
|
86
86
|
- **AI agents as reviewers**, through a JSON API and an MCP server. They read, advise and annotate
|
|
87
|
-
under their own names, and their opinions stay advice.
|
|
87
|
+
under their own names, and their opinions stay advice. Experimentally (`--claude-oauth`), the
|
|
88
|
+
server runs one itself on your Claude Code, so that it stays without being shared again.
|
|
88
89
|
- **History**: every build is kept, and a past board can be browsed.
|
|
89
90
|
- **A printed document** of what a view holds: definitions in full, proofs left out, typeset with
|
|
90
91
|
XeLaTeX.
|
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
// VISUALPROOF_WAIT_SECONDS the longest wait_for_message waits (default 1800, 30 minutes): 50 for
|
|
9
9
|
// a client that ends a tool call after a minute whatever it reports
|
|
10
10
|
// VISUALPROOF_PROGRESS_SECONDS how often a wait tells the client its progress (default 30)
|
|
11
|
+
// VISUALPROOF_FILES set by the VisualProof server for an agent it runs itself (AgentSpec
|
|
12
|
+
// decision 10): the folder `saveTo` and `path` are held to — a name is a
|
|
13
|
+
// file there, and a path that leads out of it (`..`, a symbolic link) is
|
|
14
|
+
// refused — and the client's going is no leave (the server listens for it)
|
|
11
15
|
//
|
|
12
16
|
// It offers VisualProof's agents' API as tools. The list comes from the server
|
|
13
17
|
// (GET /api/agent/mcp/tools), each tool with the request it makes — or, for a tool of several
|
|
@@ -38,8 +42,8 @@
|
|
|
38
42
|
// at once rather than after its longest wait.
|
|
39
43
|
|
|
40
44
|
import { randomUUID } from "node:crypto";
|
|
41
|
-
import { readFileSync, writeFileSync } from "node:fs";
|
|
42
|
-
import { basename, isAbsolute } from "node:path";
|
|
45
|
+
import { readFileSync, realpathSync, writeFileSync } from "node:fs";
|
|
46
|
+
import { basename, dirname, isAbsolute, join, sep } from "node:path";
|
|
43
47
|
import { createInterface } from "node:readline";
|
|
44
48
|
|
|
45
49
|
// the version of the VisualProof server it came from: the server writes its own here as it serves
|
|
@@ -60,6 +64,8 @@ const URL_ = (process.env.VISUALPROOF_URL ?? "").trim().replace(/\/+$/, "");
|
|
|
60
64
|
const TOKEN = (process.env.VISUALPROOF_TOKEN ?? "").trim();
|
|
61
65
|
const WAIT_S = seconds(process.env.VISUALPROOF_WAIT_SECONDS, 1800, 1, 7 * 86_400);
|
|
62
66
|
const PROGRESS_MS = seconds(process.env.VISUALPROOF_PROGRESS_SECONDS, 30, 0.2, 600) * 1000;
|
|
67
|
+
/** the folder an agent the VisualProof server runs keeps its files in; null: any path of this machine */
|
|
68
|
+
const FILES = (process.env.VISUALPROOF_FILES ?? "").trim() || null;
|
|
63
69
|
/** this process, as its waits name it: another one is a new session, which knows nothing yet */
|
|
64
70
|
const SESSION = randomUUID();
|
|
65
71
|
|
|
@@ -241,7 +247,9 @@ function query(args) {
|
|
|
241
247
|
* written to the new file `saveTo` names — an absolute path, never one that exists — and what was
|
|
242
248
|
* saved said; a refusal said as the server put it. */
|
|
243
249
|
async function save(path, args, cancel) {
|
|
244
|
-
const { saveTo, ...rest } = args ?? {};
|
|
250
|
+
const { saveTo: asked, ...rest } = args ?? {};
|
|
251
|
+
const saveTo = FILES && typeof asked === "string" ? confined(asked, false) : asked;
|
|
252
|
+
if (FILES && !saveTo) return text(`saveTo is the name of a new file in your folder (${FILES}).`, true);
|
|
245
253
|
if (typeof saveTo !== "string" || !isAbsolute(saveTo)) return text("saveTo is the absolute path of a new file to save it as.", true);
|
|
246
254
|
const q = query(rest);
|
|
247
255
|
const t = within(cancel, CALL_MS);
|
|
@@ -263,7 +271,9 @@ async function save(path, args, cancel) {
|
|
|
263
271
|
/** A request that uploads a file (to the library): the file `path` names on this machine read and
|
|
264
272
|
* sent as the body, its other arguments in the query (its name the file's own unless given). */
|
|
265
273
|
async function upload(path, args, cancel) {
|
|
266
|
-
const { path:
|
|
274
|
+
const { path: asked, ...rest } = args ?? {};
|
|
275
|
+
const file = FILES && typeof asked === "string" ? confined(asked, true) : asked;
|
|
276
|
+
if (FILES && !file) return text(`path is the name of a file in your folder (${FILES}).`, true);
|
|
267
277
|
if (typeof file !== "string" || !isAbsolute(file)) return text("path is the absolute path of the file to send.", true);
|
|
268
278
|
let bytes;
|
|
269
279
|
try {
|
|
@@ -286,6 +296,20 @@ async function upload(path, args, cancel) {
|
|
|
286
296
|
}
|
|
287
297
|
}
|
|
288
298
|
|
|
299
|
+
/** A path a tool names, held to FILES: a name is a file there, a path must be in it, with its
|
|
300
|
+
* symbolic links resolved (a file to send, `existing`; a new file to save, its folder); null where
|
|
301
|
+
* it leads out, or cannot be resolved. */
|
|
302
|
+
function confined(p, existing) {
|
|
303
|
+
try {
|
|
304
|
+
const root = realpathSync(FILES);
|
|
305
|
+
const abs = isAbsolute(p) ? p : join(root, p);
|
|
306
|
+
const real = existing ? realpathSync(abs) : join(realpathSync(dirname(abs)), basename(abs));
|
|
307
|
+
return real.startsWith(root + sep) && real !== root ? real : null;
|
|
308
|
+
} catch {
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
289
313
|
/** JSON as the server sent it, without the spaces a model does not need. */
|
|
290
314
|
function compact(body) {
|
|
291
315
|
try {
|
|
@@ -482,7 +506,8 @@ async function leave() {
|
|
|
482
506
|
if (leaving) return;
|
|
483
507
|
leaving = true;
|
|
484
508
|
for (const ctl of running.values()) ctl.abort();
|
|
485
|
-
|
|
509
|
+
// an agent the VisualProof server runs ends its client after each turn: the server listens for it
|
|
510
|
+
if (URL_ && TOKEN && !stopped && !FILES)
|
|
486
511
|
await fetch(`${URL_}/api/agent/view`, { method: "POST", headers: { ...auth(), "content-type": "application/json", "x-visualproof-session": SESSION }, body: '{"leave":true}', signal: AbortSignal.timeout(1500) }).catch(() => {});
|
|
487
512
|
process.exit(0);
|
|
488
513
|
}
|
|
@@ -98,15 +98,19 @@ export function endpointList(api, a) {
|
|
|
98
98
|
return fail(404, `No endpoint ${path}.`, "GET /api/agent/endpoints lists them all.");
|
|
99
99
|
return ok({ endpoints: all.map((e) => ({ method: e.method, path: e.path, about: e.about, params: e.params, returns: e.returns, ...(e.needs ? { needs: `--agents ${e.needs === "advise" ? "advise or full" : "full"}` } : {}) })) });
|
|
100
100
|
}
|
|
101
|
+
/** VisualProof's MCP server as it is served, carrying the version of the server it came from
|
|
102
|
+
* (UpdateSpec decision 11); the agents the server runs run it too (server/hosted.ts). Throws where
|
|
103
|
+
* the file is not beside the server. */
|
|
104
|
+
export function mcpServerText() {
|
|
105
|
+
return readFileSync(MCP_FILE, "utf8").replace(/^const VERSION = "[^"\n]*";$/m, `const VERSION = ${JSON.stringify(packageVersion())};`);
|
|
106
|
+
}
|
|
101
107
|
/** VisualProof's MCP server, one file (mcp/visualproof-mcp.mjs). */
|
|
102
108
|
export function mcpFile(api, a) {
|
|
103
109
|
const t = talker(api, a);
|
|
104
110
|
if (typeof t !== "string")
|
|
105
111
|
return t;
|
|
106
112
|
try {
|
|
107
|
-
|
|
108
|
-
const body = readFileSync(MCP_FILE, "utf8").replace(/^const VERSION = "[^"\n]*";$/m, `const VERSION = ${JSON.stringify(packageVersion())};`);
|
|
109
|
-
return { status: 200, body: null, text: { type: "text/javascript; charset=utf-8", body } };
|
|
113
|
+
return { status: 200, body: null, text: { type: "text/javascript; charset=utf-8", body: mcpServerText() } };
|
|
110
114
|
}
|
|
111
115
|
catch {
|
|
112
116
|
return fail(404, "The MCP server is not beside this server.", "Use the HTTP API (GET /api/agent).");
|
|
@@ -127,12 +131,15 @@ export function mcpTools(api, a) {
|
|
|
127
131
|
const t = talker(api, a);
|
|
128
132
|
if (typeof t !== "string")
|
|
129
133
|
return t;
|
|
130
|
-
const tools = TOOLS.map((d) => offered(d, (r) => mayCall(api, a.caller, r))).filter((x) => x !== null);
|
|
131
134
|
const me = a.caller;
|
|
132
|
-
|
|
135
|
+
// an agent the server runs is handed its messages as turns (AgentSpec decision 10): it has nothing
|
|
136
|
+
// to wait on
|
|
137
|
+
const hosted = me.token.runs === "server";
|
|
138
|
+
const tools = TOOLS.filter((d) => !(hosted && d.name === "wait_for_message")).map((d) => offered(d, (r) => mayCall(api, a.caller, r))).filter((x) => x !== null);
|
|
139
|
+
const you = { name: me.name, level: api.levelOf(me), by: me.token.by, expires: me.token.expires, ...(hosted ? { runs: "server" } : {}) };
|
|
133
140
|
const v = api.world.graph ? api.graph() : null;
|
|
134
141
|
const project = v instanceof View ? v.meta.project.name : null;
|
|
135
|
-
return ok({ you, instructions: mcpInstructions({ ...you, address: a.base ?? "http://127.0.0.1:4173", project }), tools, docs: DOC_TOPICS });
|
|
142
|
+
return ok({ you, instructions: mcpInstructions({ ...you, address: a.base ?? "http://127.0.0.1:4173", project, hosted }), tools, docs: DOC_TOPICS });
|
|
136
143
|
}
|
|
137
144
|
/** Whether `caller` may make a tool's request: what its endpoint needs. A request no endpoint
|
|
138
145
|
* answers is a mistake of the tools' list. */
|
package/dist/server/app.js
CHANGED
|
@@ -28,8 +28,8 @@ import { serveFilm } from "./film.js";
|
|
|
28
28
|
import { readOnlyAllows, ReadOnlyBounds } from "./readonlygate.js";
|
|
29
29
|
import { readBody, refused, sendFile, sendJson, sendRevision, sendText, TooLarge, tooLarge } from "./send.js";
|
|
30
30
|
import { endedWords, enter, enterReadOnly, identity, identityGet, refuseHost, revokedWords } from "./appaccess.js";
|
|
31
|
-
import { shareAgents, sharePeople, shareReadOnly } from "./appshare.js";
|
|
32
|
-
import { ask, chat, chatNew, pointer, presence } from "./apptalk.js";
|
|
31
|
+
import { shareAgents, shareClaude, sharePeople, shareReadOnly } from "./appshare.js";
|
|
32
|
+
import { ask, chat, chatNew, chatStop, pointer, presence } from "./apptalk.js";
|
|
33
33
|
import { history, layoutUndo, libraryRequest, reviewPost, views } from "./appsite.js";
|
|
34
34
|
import { agentRoute, exportRoute } from "./appagent.js";
|
|
35
35
|
import { git, graphParts, hover, source, status } from "./appgraph.js";
|
|
@@ -309,12 +309,16 @@ export class App {
|
|
|
309
309
|
return shareReadOnly(this, req, res, url);
|
|
310
310
|
if (path.startsWith("/api/share/agents"))
|
|
311
311
|
return shareAgents(this, req, res, url);
|
|
312
|
+
if (path === "/api/share/claude" || path.startsWith("/api/share/claude/"))
|
|
313
|
+
return shareClaude(this, req, res, url);
|
|
312
314
|
if (path === "/api/share/people" || path.startsWith("/api/share/people/"))
|
|
313
315
|
return sharePeople(this, req, res, url);
|
|
314
316
|
if (path === "/api/chat")
|
|
315
317
|
return chat(this, req, res, url);
|
|
316
318
|
if (path === "/api/chat/new")
|
|
317
319
|
return chatNew(this, req, res);
|
|
320
|
+
if (path === "/api/chat/stop")
|
|
321
|
+
return chatStop(this, req, res);
|
|
318
322
|
if (path === "/api/ask" || path === "/api/ask/answer")
|
|
319
323
|
return ask(this, req, res, url);
|
|
320
324
|
if (path === "/api/agent" || path.startsWith("/api/agent/"))
|
package/dist/server/appagent.js
CHANGED
|
@@ -70,7 +70,7 @@ export async function exportRoute(app, req, res, url, agent, readOnly = false) {
|
|
|
70
70
|
export async function agentRoute(app, req, res, url, token = null) {
|
|
71
71
|
const said = app.ids.who(req);
|
|
72
72
|
const caller = token
|
|
73
|
-
? { name: token.name, kind: "agent", token: { id: token.id, by: token.by, level: token.level, expires: token.expires }, ...(said && said !== token.name ? { ignoredName: said } : {}) }
|
|
73
|
+
? { name: token.name, kind: "agent", token: { id: token.id, by: token.by, level: token.level, expires: token.expires, ...(token.runs ? { runs: token.runs } : {}) }, ...(said && said !== token.name ? { ignoredName: said } : {}) }
|
|
74
74
|
: { name: said, kind: app.ids.kind(req) };
|
|
75
75
|
let body = null;
|
|
76
76
|
if (req.method === "POST" && url.pathname.replace(/\/+$/, "") === UPLOAD_PATH) {
|
package/dist/server/appshare.js
CHANGED
|
@@ -8,6 +8,8 @@ import { AGENT_LEVELS, lower } from "./agent.js";
|
|
|
8
8
|
import { AgentTokens, DEFAULT_DAYS, invalidAgentName, TOKEN_DAYS } from "./tokens.js";
|
|
9
9
|
import { agentPrompt } from "./prompt.js";
|
|
10
10
|
import { reachedAt, sendJson } from "./send.js";
|
|
11
|
+
import { invalidClaudeToken } from "./claudetokens.js";
|
|
12
|
+
import { PER_PERSON } from "./hosted.js";
|
|
11
13
|
/** Sharing with an AI agent (AgentSpec decision 7), for people: the tokens in force (GET), a new
|
|
12
14
|
* one with its prompt (POST), a token's prompt shown again (POST …/prompt), and revoking one
|
|
13
15
|
* (POST …/revoke). All but the list need a name: a token is made under it, and its prompt holds
|
|
@@ -25,7 +27,7 @@ export async function shareAgents(app, req, res, url) {
|
|
|
25
27
|
const me = app.ids.who(req);
|
|
26
28
|
const path = url.pathname.replace(/\/+$/, "");
|
|
27
29
|
if (path === "/api/share/agents" && req.method === "GET")
|
|
28
|
-
return sendJson(res, { level: top, levels: AGENT_LEVELS.slice(0, AGENT_LEVELS.indexOf(top) + 1), defaultLevel: lower("advise", top), days: TOKEN_DAYS, defaultDays: DEFAULT_DAYS, tokens: tokens.inForce().map(AgentTokens.info) });
|
|
30
|
+
return sendJson(res, { level: top, levels: AGENT_LEVELS.slice(0, AGENT_LEVELS.indexOf(top) + 1), defaultLevel: lower("advise", top), days: TOKEN_DAYS, defaultDays: DEFAULT_DAYS, tokens: tokens.inForce().map(AgentTokens.info), server: onServer(app, req) });
|
|
29
31
|
if (req.method !== "POST")
|
|
30
32
|
return sendJson(res, { error: `${path} answers POST.` }, 405);
|
|
31
33
|
if (!me)
|
|
@@ -47,6 +49,9 @@ export async function shareAgents(app, req, res, url) {
|
|
|
47
49
|
const t = typeof b.id === "string" ? tokens.token(b.id) : null;
|
|
48
50
|
if (!t)
|
|
49
51
|
return sendJson(res, { error: "There is no such token in force." }, 404);
|
|
52
|
+
// an agent the server runs has none: its token never leaves the server (AgentSpec decision 10)
|
|
53
|
+
if (t.record.runs === "server")
|
|
54
|
+
return sendJson(res, { error: `${t.record.name} runs on this server: it has no prompt, and its token never leaves the server.`, runs: "server" }, 400);
|
|
50
55
|
if (!madeBy(t.record, { name: me, person: app.ids.person(req)?.id ?? null }))
|
|
51
56
|
return sendJson(res, { error: `Only ${t.record.by}, who made it, is shown this token's prompt again: whoever holds a token reads its agent's private channel.`, maker: false }, 403);
|
|
52
57
|
return sendJson(res, { token: AgentTokens.info(t.record), prompt: agentPrompt({ url: address, token: t.token, record: t.record }) });
|
|
@@ -66,10 +71,123 @@ export async function shareAgents(app, req, res, url) {
|
|
|
66
71
|
const days = b.days ?? DEFAULT_DAYS;
|
|
67
72
|
if (!TOKEN_DAYS.includes(days))
|
|
68
73
|
return sendJson(res, { error: `days is one of ${TOKEN_DAYS.join(", ")}.` }, 400);
|
|
74
|
+
// an agent the server runs, on its maker's Claude Code (AgentSpec decision 10): no prompt, and its
|
|
75
|
+
// token lasts until revoked
|
|
76
|
+
if (b.runs === "server") {
|
|
77
|
+
const why = mayRunHere(app, req);
|
|
78
|
+
if (why)
|
|
79
|
+
return sendJson(res, { error: why }, 403);
|
|
80
|
+
const person = app.ids.person(req)?.id ?? null;
|
|
81
|
+
if (!app.live.claudeTokens.forMaker({ by: me, ...(person ? { makerPerson: person } : {}) }))
|
|
82
|
+
return sendJson(res, { error: app.live.claudeTokens.info({ person, name: me })?.refused ? "Claude Code refused your token: give a new one first." : "Give your Claude Code OAuth token first: the agent runs on it.", claudeToken: true }, 409);
|
|
83
|
+
const mine = tokens.inForce().filter((t) => t.runs === "server" && madeBy(t, { name: me, person }));
|
|
84
|
+
if (mine.length >= PER_PERSON)
|
|
85
|
+
return sendJson(res, { error: `You run ${PER_PERSON} agents on this server already: revoke one first.` }, 409);
|
|
86
|
+
const made = tokens.create({ name: name.trim(), by: me, person, level, days: DEFAULT_DAYS, runs: "server" });
|
|
87
|
+
app.live.hosted.start(made.record);
|
|
88
|
+
return sendJson(res, { token: AgentTokens.info(made.record), tokens: tokens.inForce().map(AgentTokens.info) });
|
|
89
|
+
}
|
|
90
|
+
if (b.runs !== undefined && b.runs !== null && b.runs !== "agent")
|
|
91
|
+
return sendJson(res, { error: 'runs is "server", or left out for a prompt.' }, 400);
|
|
69
92
|
const made = tokens.create({ name: name.trim(), by: me, person: app.ids.person(req)?.id ?? null, level, days: days });
|
|
70
93
|
const prompt = agentPrompt({ url: address, token: made.token, record: made.record });
|
|
71
94
|
return sendJson(res, { token: AgentTokens.info(made.record), prompt, tokens: tokens.inForce().map(AgentTokens.info) });
|
|
72
95
|
}
|
|
96
|
+
/** What Share's AI agent tab is told of running agents on this server (AgentSpec decision 10): null
|
|
97
|
+
* where it runs none (no --claude-oauth); else whether it can (Claude Code is here), whether the
|
|
98
|
+
* reader may (why not), and whether they gave a Claude token — when, and whether it was refused;
|
|
99
|
+
* never the token. */
|
|
100
|
+
function onServer(app, req) {
|
|
101
|
+
const hosted = app.live.hosted;
|
|
102
|
+
if (!hosted)
|
|
103
|
+
return null;
|
|
104
|
+
const me = app.ids.who(req);
|
|
105
|
+
const person = app.ids.person(req)?.id ?? null;
|
|
106
|
+
return {
|
|
107
|
+
available: hosted.available,
|
|
108
|
+
refused: mayRunHere(app, req),
|
|
109
|
+
token: me ? app.live.claudeTokens.info({ person, name: me }) : null,
|
|
110
|
+
perPerson: PER_PERSON,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/** Why the reader may not run an agent on this server, nor give a Claude token; null when they may:
|
|
114
|
+
* the server runs agents (--claude-oauth) and Claude Code is on its machine; the reader is a person
|
|
115
|
+
* whose name is theirs alone — their personal link's, the sign-in's in front of the server, or, on
|
|
116
|
+
* a server without a key reached on this machine only, whoever gives it — so that nobody runs agents
|
|
117
|
+
* on someone else's subscription by giving their name (the owner's decision of 2026-11-08). */
|
|
118
|
+
export function mayRunHere(app, req) {
|
|
119
|
+
const hosted = app.live.hosted;
|
|
120
|
+
if (!hosted)
|
|
121
|
+
return "This server runs no agents of its own: it was started without --claude-oauth.";
|
|
122
|
+
if (!hosted.available)
|
|
123
|
+
return "Claude Code (claude) is not on this server's machine: it runs no agents of its own.";
|
|
124
|
+
if (app.ids.kind(req) === "agent")
|
|
125
|
+
return "Agents cannot run agents.";
|
|
126
|
+
if (app.readOnlyOf.has(req))
|
|
127
|
+
return "This page is read-only.";
|
|
128
|
+
const here = !app.access && !app.ids.header && loopbackHost(req.headers.host) && loopbackAddress(req.socket.remoteAddress);
|
|
129
|
+
if (!app.ids.person(req) && !app.ids.header && !here)
|
|
130
|
+
return "Only someone whose name is theirs alone runs agents on this server: come by your personal link (Share, Invite a person), so that nobody can run one on your Claude Code by giving your name.";
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
/** A Claude Code OAuth token given in Share (AgentSpec decision 10), for people: POST
|
|
134
|
+
* /api/share/claude {token} gives it, in place of the one before; POST …/remove removes it. Never
|
|
135
|
+
* answered back: what Share is told of it is GET /api/share/agents's `server.token`. Only from a page
|
|
136
|
+
* over HTTPS or on this machine: a year of a subscription does not cross a network in the clear. */
|
|
137
|
+
export async function shareClaude(app, req, res, url) {
|
|
138
|
+
const path = url.pathname.replace(/\/+$/, "");
|
|
139
|
+
if (req.method !== "POST")
|
|
140
|
+
return sendJson(res, { error: `${path} answers POST.` }, 405);
|
|
141
|
+
const why = mayRunHere(app, req);
|
|
142
|
+
if (why)
|
|
143
|
+
return sendJson(res, { error: why }, app.live.hosted ? 403 : 404);
|
|
144
|
+
const me = app.ids.who(req);
|
|
145
|
+
if (!me)
|
|
146
|
+
return sendJson(res, { error: "Say who you are first: a Claude token is kept under your name.", who: true }, 401);
|
|
147
|
+
const who = { person: app.ids.person(req)?.id ?? null, name: me };
|
|
148
|
+
const claude = app.live.claudeTokens;
|
|
149
|
+
if (path === "/api/share/claude/remove") {
|
|
150
|
+
if (!claude.remove(who))
|
|
151
|
+
return sendJson(res, { error: "You gave no Claude Code token." }, 404);
|
|
152
|
+
return sendJson(res, { ok: true, token: null });
|
|
153
|
+
}
|
|
154
|
+
if (path !== "/api/share/claude")
|
|
155
|
+
return sendJson(res, { error: "not found" }, 404);
|
|
156
|
+
if (!securePage(req.headers.origin))
|
|
157
|
+
return sendJson(res, { error: "A Claude Code token is given from a page over HTTPS, or on this machine: over plain HTTP it would cross the network in the clear.", insecure: true }, 403);
|
|
158
|
+
const r = await app.json(req, 4096);
|
|
159
|
+
if (r.error !== undefined)
|
|
160
|
+
return sendJson(res, { error: `The body is ${r.error}.` }, 400);
|
|
161
|
+
const token = r.body?.token;
|
|
162
|
+
const bad = invalidClaudeToken(token);
|
|
163
|
+
if (bad)
|
|
164
|
+
return sendJson(res, { error: bad }, 400);
|
|
165
|
+
claude.give(who, token.trim());
|
|
166
|
+
return sendJson(res, { ok: true, token: claude.info(who) });
|
|
167
|
+
}
|
|
168
|
+
/** Whether a page's Origin is over HTTPS, or on this machine. */
|
|
169
|
+
function securePage(origin) {
|
|
170
|
+
if (!origin)
|
|
171
|
+
return false;
|
|
172
|
+
try {
|
|
173
|
+
const u = new URL(origin);
|
|
174
|
+
return u.protocol === "https:" || (u.protocol === "http:" && loopbackHost(u.host));
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/** Whether a Host names this machine: localhost (or a name under it), 127.x.y.z, [::1]. */
|
|
181
|
+
function loopbackHost(host) {
|
|
182
|
+
if (!host)
|
|
183
|
+
return false;
|
|
184
|
+
const name = host.replace(/:\d+$/, "").replace(/^\[|\]$/g, "").toLowerCase();
|
|
185
|
+
return name === "localhost" || name.endsWith(".localhost") || /^127\.\d+\.\d+\.\d+$/.test(name) || name === "::1";
|
|
186
|
+
}
|
|
187
|
+
/** Whether a connection comes from this machine. */
|
|
188
|
+
function loopbackAddress(a) {
|
|
189
|
+
return !!a && (/^127\./.test(a) || a === "::1" || /^::ffff:127\./.test(a));
|
|
190
|
+
}
|
|
73
191
|
/** Inviting a person (CollabSpec decision 9), for people: the personal links in force (GET), a new
|
|
74
192
|
* one for a person named in the body (POST), and revoking one (POST …/revoke). Making and
|
|
75
193
|
* revoking need a name: who made a link, and who revoked it, is said in Share. The link itself
|
package/dist/server/apptalk.js
CHANGED
|
@@ -151,6 +151,32 @@ export async function chatNew(app, req, res) {
|
|
|
151
151
|
const divider = live.talk.restart(tok, me);
|
|
152
152
|
return sendJson(res, { ok: true, divider: divider && forPages(divider) });
|
|
153
153
|
}
|
|
154
|
+
/** Stop the turn an agent the server runs is taking (AgentSpec decision 10): POST /api/chat/stop
|
|
155
|
+
* {agent}, from its maker alone, as Esc in a terminal stops an agent someone runs there. What it
|
|
156
|
+
* was answering ends with it; the agent listens again. */
|
|
157
|
+
export async function chatStop(app, req, res) {
|
|
158
|
+
const live = app.live;
|
|
159
|
+
if (req.method !== "POST")
|
|
160
|
+
return sendJson(res, { error: "/api/chat/stop answers POST." }, 405);
|
|
161
|
+
if (app.readOnlyOf.has(req))
|
|
162
|
+
return sendJson(res, { error: "This page is read-only.", readOnly: true }, 403);
|
|
163
|
+
if (app.ids.kind(req) === "agent")
|
|
164
|
+
return sendJson(res, { error: "An agent's turn is stopped by people." }, 403);
|
|
165
|
+
const me = app.ids.who(req);
|
|
166
|
+
if (!me)
|
|
167
|
+
return sendJson(res, { error: "Say who you are first.", who: true }, 401);
|
|
168
|
+
const r = await app.json(req, 1 << 12);
|
|
169
|
+
if (r.error !== undefined)
|
|
170
|
+
return sendJson(res, { error: `The body is ${r.error}.` }, 400);
|
|
171
|
+
const b = (r.body ?? {});
|
|
172
|
+
const tok = typeof b.agent === "string" ? live.presence.tokenOf(b.agent) : null;
|
|
173
|
+
const record = tok ? live.tokens.get(tok) : undefined;
|
|
174
|
+
if (!tok || !record || record.runs !== "server")
|
|
175
|
+
return sendJson(res, { error: "There is no such agent running on this server." }, 404);
|
|
176
|
+
if (!madeBy(record, { name: me, person: app.ids.person(req)?.id ?? null }))
|
|
177
|
+
return sendJson(res, { error: `Only ${record.by}, who runs ${record.name}, stops its turn.` }, 403);
|
|
178
|
+
return sendJson(res, { ok: true, stopped: live.hosted?.stopTurn(tok) ?? false });
|
|
179
|
+
}
|
|
154
180
|
/** Asking others to follow (CollabSpec decision 10, the owner's decision of 2026-10-12), for pages:
|
|
155
181
|
* POST /api/ask {token, to?} — the page whose event stream holds `token` asks everyone on the
|
|
156
182
|
* server, or the person whose page is listed as `to`, to follow its reader, who has a name and is
|
package/dist/server/children.js
CHANGED
|
@@ -47,14 +47,18 @@ export function tracked(child) {
|
|
|
47
47
|
}
|
|
48
48
|
/** The environment a program the server runs is given: the server's own, without what is the
|
|
49
49
|
* server's alone (`VISUALPROOF_*`: the supervisor's word to it, its test hooks; UpdateSpec decision
|
|
50
|
-
* 9), with `extra` over it. */
|
|
50
|
+
* 9) and without Claude's and Anthropic's credentials (CREDENTIAL), with `extra` over it. */
|
|
51
51
|
export function childEnv(extra = {}) {
|
|
52
52
|
const env = {};
|
|
53
53
|
for (const [k, v] of Object.entries(process.env))
|
|
54
|
-
if (!k.startsWith("VISUALPROOF_"))
|
|
54
|
+
if (!k.startsWith("VISUALPROOF_") && !CREDENTIAL(k))
|
|
55
55
|
env[k] = v;
|
|
56
56
|
return { ...env, ...extra };
|
|
57
57
|
}
|
|
58
|
+
/** Claude's and Anthropic's credentials, where the server's environment has them: no program the
|
|
59
|
+
* server runs is given them (Lake, Lean, git, a branch's build, which runs whatever its code says);
|
|
60
|
+
* the agents the server runs are given their maker's token alone (server/hosted.ts). */
|
|
61
|
+
const CREDENTIAL = (k) => k === "CLAUDE_CODE_OAUTH_TOKEN" || k.startsWith("ANTHROPIC_");
|
|
58
62
|
/** How many tracked programs run now. */
|
|
59
63
|
export function childCount() {
|
|
60
64
|
return running.size;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// The Claude Code OAuth tokens people give in Share (AgentSpec decision 10; the owner's decisions of
|
|
2
|
+
// 2026-11-08, experimental, behind --claude-oauth): a person's own token, made with `claude
|
|
3
|
+
// setup-token`, on which the agents they run on this server run (server/hosted.ts). One per person:
|
|
4
|
+
// their personal link's id where they came by one, else their name (as madeBy compares makers,
|
|
5
|
+
// shared/collab.ts), so that it is theirs whatever name they go by.
|
|
6
|
+
//
|
|
7
|
+
// It is worth more than anything else the site keeps: a year of the person's Claude subscription. So
|
|
8
|
+
// it is given once and never shown again — not to its holder, not to any page, agent, log or error —
|
|
9
|
+
// and kept in `<site>/claude-tokens.json`, readable by the server's user alone, as the agents' tokens
|
|
10
|
+
// and the personal links are. What the server does with it is hand it to the `claude` processes of
|
|
11
|
+
// its holder's agents, in their environment (never on a command line). Its holder replaces or removes
|
|
12
|
+
// it; a personal link revoked takes its token with it. Removing it here deletes the server's copy
|
|
13
|
+
// only: the token itself lasts until it expires or is revoked at Anthropic.
|
|
14
|
+
//
|
|
15
|
+
// No API key: the owner's decision (an `sk-ant-api…` key is refused, so that nobody gives one by
|
|
16
|
+
// mistake and is billed by the token).
|
|
17
|
+
import { existsSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
18
|
+
import { join } from "node:path";
|
|
19
|
+
import { sameName } from "../shared/collab.js";
|
|
20
|
+
const FILE = "claude-tokens.json";
|
|
21
|
+
/** what `claude setup-token` prints: `sk-ant-oat01-…` (108 characters on 2026-11-08) */
|
|
22
|
+
const OAUTH = /^sk-ant-oat\d{2}-[A-Za-z0-9_-]{20,400}$/;
|
|
23
|
+
/** Why `s` is not a Claude Code OAuth token; null when it looks like one. */
|
|
24
|
+
export function invalidClaudeToken(s) {
|
|
25
|
+
if (typeof s !== "string" || !s.trim())
|
|
26
|
+
return "Paste the token `claude setup-token` printed.";
|
|
27
|
+
const t = s.trim();
|
|
28
|
+
if (/^sk-ant-api/.test(t))
|
|
29
|
+
return "This is an API key: this server takes a Claude Code OAuth token, which `claude setup-token` prints (sk-ant-oat…).";
|
|
30
|
+
if (!OAUTH.test(t))
|
|
31
|
+
return "This is not a Claude Code OAuth token: `claude setup-token` prints one that starts with sk-ant-oat.";
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
/** Whether `k` is `who`'s: by the personal link where it was given under one, else by the name. */
|
|
35
|
+
const holds = (k, who) => (k.person ? who.person === k.person : !who.person && sameName(k.name, who.name));
|
|
36
|
+
export class ClaudeTokens {
|
|
37
|
+
file;
|
|
38
|
+
kept = [];
|
|
39
|
+
/** told when a holder's token was given, replaced, refused or removed: their agents on the server
|
|
40
|
+
* start again, or stop (server/hosted.ts) */
|
|
41
|
+
onChanged = () => { };
|
|
42
|
+
/** `siteDir` null keeps them in memory only (tests). */
|
|
43
|
+
constructor(siteDir) {
|
|
44
|
+
this.file = siteDir ? join(siteDir, FILE) : null;
|
|
45
|
+
if (this.file && existsSync(this.file)) {
|
|
46
|
+
try {
|
|
47
|
+
const data = JSON.parse(readFileSync(this.file, "utf8"));
|
|
48
|
+
if (Array.isArray(data.tokens))
|
|
49
|
+
this.kept = data.tokens.filter(isKept);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// a file that cannot be read gives no token; it is kept aside, not overwritten blindly
|
|
53
|
+
renameSync(this.file, `${this.file}.broken-${Date.now()}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** What `who` is told of their token, or null where they gave none. */
|
|
58
|
+
info(who) {
|
|
59
|
+
const k = this.kept.find((x) => holds(x, who));
|
|
60
|
+
return k ? { given: k.given, refused: k.refused } : null;
|
|
61
|
+
}
|
|
62
|
+
/** The token an agent made by `maker` runs on (its record's `by` and `makerPerson`), or null: none
|
|
63
|
+
* given, or refused since. */
|
|
64
|
+
forMaker(maker) {
|
|
65
|
+
const k = this.kept.find((x) => holds(x, { person: maker.makerPerson ?? null, name: maker.by }));
|
|
66
|
+
return k && !k.refused ? k.token : null;
|
|
67
|
+
}
|
|
68
|
+
/** Give `who`'s token, in place of the one they gave before. Checked by the caller. */
|
|
69
|
+
give(who, token, now = Date.now()) {
|
|
70
|
+
this.kept = this.kept.filter((x) => !holds(x, who));
|
|
71
|
+
this.kept.push({ ...(who.person ? { person: who.person } : {}), name: who.name, token: token.trim(), given: new Date(now).toISOString(), refused: null });
|
|
72
|
+
this.save();
|
|
73
|
+
this.onChanged(who);
|
|
74
|
+
}
|
|
75
|
+
/** Remove `who`'s token; false when they gave none. */
|
|
76
|
+
remove(who) {
|
|
77
|
+
const before = this.kept.length;
|
|
78
|
+
this.kept = this.kept.filter((x) => !holds(x, who));
|
|
79
|
+
if (this.kept.length === before)
|
|
80
|
+
return false;
|
|
81
|
+
this.save();
|
|
82
|
+
this.onChanged(who);
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
/** Claude Code refused `token` (expired, revoked at Anthropic): it is not used again until its
|
|
86
|
+
* holder gives one anew. */
|
|
87
|
+
refuse(token, now = Date.now()) {
|
|
88
|
+
const k = this.kept.find((x) => x.token === token);
|
|
89
|
+
if (!k || k.refused)
|
|
90
|
+
return;
|
|
91
|
+
k.refused = new Date(now).toISOString();
|
|
92
|
+
this.save();
|
|
93
|
+
this.onChanged({ person: k.person ?? null, name: k.name });
|
|
94
|
+
}
|
|
95
|
+
/** A personal link was revoked: its holder's token goes with it. */
|
|
96
|
+
dropPerson(person) {
|
|
97
|
+
const k = this.kept.find((x) => x.person === person);
|
|
98
|
+
if (k)
|
|
99
|
+
this.remove({ person, name: k.name });
|
|
100
|
+
}
|
|
101
|
+
save() {
|
|
102
|
+
if (!this.file)
|
|
103
|
+
return;
|
|
104
|
+
const tmp = `${this.file}.tmp`;
|
|
105
|
+
writeFileSync(tmp, JSON.stringify({ tokens: this.kept }, null, 1) + "\n", { mode: 0o600 });
|
|
106
|
+
renameSync(tmp, this.file);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function isKept(x) {
|
|
110
|
+
const k = x;
|
|
111
|
+
return !!k && typeof k === "object" && typeof k.name === "string" && typeof k.token === "string" && OAUTH.test(k.token) && typeof k.given === "string" &&
|
|
112
|
+
(k.person === undefined || typeof k.person === "string") && (k.refused === null || typeof k.refused === "string");
|
|
113
|
+
}
|
package/dist/server/doctor.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import { spawnSync } from "node:child_process";
|
|
6
6
|
import { accessSync, constants, existsSync, readFileSync, statSync } from "node:fs";
|
|
7
7
|
import { filmPin } from "./film.js";
|
|
8
|
+
import { findClaude } from "./hosted.js";
|
|
8
9
|
import { freemem, tmpdir, totalmem } from "node:os";
|
|
9
10
|
import { dirname, join, relative, resolve } from "node:path";
|
|
10
11
|
import { cacheHome, projectDirs, siteHolder } from "./dirs.js";
|
|
@@ -182,6 +183,12 @@ export function doctor(o) {
|
|
|
182
183
|
if (exec("git", ["--version"]) === null) {
|
|
183
184
|
checks.push({ level: "info", what: "git not found: builds are kept without their commit, and past sources only as the builds kept them; a branch cannot be followed (serve <git URL>)" });
|
|
184
185
|
}
|
|
186
|
+
// the agents a server runs on their makers' Claude Code (--claude-oauth, AgentSpec decision 10)
|
|
187
|
+
const claude = findClaude();
|
|
188
|
+
const claudeVersion = exec(o.run ? "claude" : (claude ?? "claude"), ["--version"]);
|
|
189
|
+
checks.push(claudeVersion
|
|
190
|
+
? { level: "ok", what: `Claude Code ${claudeVersion.trim().split(/\s/)[0]} (for --claude-oauth, agents the server runs)` }
|
|
191
|
+
: { level: "info", what: "Claude Code (claude) not found: needed only for --claude-oauth (agents the server runs)", fix: "npm install -g @anthropic-ai/claude-code" });
|
|
185
192
|
const film = filmLine(o.cache ? resolve(o.cache) : cacheHome(), o.updates ?? process.env.VISUALPROOF_UPDATES);
|
|
186
193
|
const updates = updatesLine(exec, node, o.fromPackage ?? FROM_PACKAGE, o.cache ? resolve(o.cache) : cacheHome(), o.updates ?? process.env.VISUALPROOF_UPDATES, o.path === null ? null : projectDirs(resolve(o.path), o.site, o.cache).site);
|
|
187
194
|
if (o.path === null)
|