T-1, T-2: W1.11: the TypeScript side holds a seat as the Go side does
A TypeScript holder, the forge module first, must judge a verb's caller by the runtime's proof, say its seat's events and answer its first views' read and ask-again tools (design 47, The forge module's tools come from mesh-sdk's TypeScript side; G-F1 option A). The stdio session now hands each tool the caller and call id the runtime wrote in _meta (callers.callerOf, the Go rules for Callers and VerbCallers), binds the runtime for seatPublish over mesh/seat.publish and config, and views gains the Follower and its two status tools over the state first-views. @novox/mesh-sdk 0.1.19. Builds: design 47 G §196, §200, §336 Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
This commit is contained in:
Vendored
+11
-2
@@ -1,8 +1,17 @@
|
||||
// A tools bundle written against the protocol: the module's own tool, a seat's verb, and one that fails.
|
||||
// A tools bundle written against the protocol: the module's own tool, a seat's verb, one that fails, and one that says
|
||||
// who called it as the runtime proved it and refuses a caller its verb's list does not name.
|
||||
import { serveStdio } from "../../dist/stdio/index.js";
|
||||
import { callerOf, callIdOf, refusal } from "../../dist/callers/index.js";
|
||||
|
||||
const verbs = { "node-lamp.on": ["mesh-delivery-forge-bridge"] };
|
||||
|
||||
await serveStdio("lamp", [
|
||||
{ name: "brightness", description: "set it", input: { level: { type: "number", description: "0 to 10" } }, run: async (a) => ({ level: a.level }) },
|
||||
{ name: "node-lamp.on", description: "the seat's verb", input: {}, run: async () => ({ on: true }) },
|
||||
{ name: "node-lamp.on", description: "the seat's verb", input: {}, run: async (a) => {
|
||||
const no = refusal(verbs, "node-lamp.on", callerOf(a));
|
||||
if (no) throw no;
|
||||
return { on: true };
|
||||
} },
|
||||
{ name: "fail", description: "it throws", input: {}, run: async () => { throw new Error("the bulb is gone"); } },
|
||||
{ name: "who", description: "who called", input: {}, run: async (a) => ({ caller: callerOf(a), call: callIdOf(a) }) },
|
||||
]);
|
||||
|
||||
Vendored
+4
-1
@@ -1,8 +1,11 @@
|
||||
// A launched bundle whose tool emits (novox/hq ADR 0193): the emit travels to the runtime over stdio.
|
||||
// A launched bundle whose tool emits (novox/hq ADR 0193): the emit travels to the runtime over stdio, and so does an
|
||||
// event it says on a seat (ADR 0259 §3).
|
||||
import { registerModuleTools } from "../../dist/tools/index.js";
|
||||
import { emit } from "../../dist/events/index.js";
|
||||
import { seatPublish } from "../../dist/runtime/index.js";
|
||||
import { serveRegisteredOverStdio } from "../../dist/stdio/index.js";
|
||||
registerModuleTools("chime", () => [
|
||||
{ name: "ring", description: "rings and says so", input: {}, run: async () => { await emit("rang", { times: 1 }); return { rang: true }; } },
|
||||
{ name: "peal", description: "says it on its seat", input: {}, run: async () => { await seatPublish("mesh.seat.bell.event.pealed.anchor", { times: 2 }, "p-1"); return { pealed: true }; } },
|
||||
]);
|
||||
await serveRegisteredOverStdio();
|
||||
|
||||
@@ -662,3 +662,126 @@ test("the provider loop is named for what it is, and its old name stays an alias
|
||||
// The events and tools are what the controller and the operator read on the bus: their names stay.
|
||||
assert.equal(loop.FAILING_EVENT, "provisioner.failing");
|
||||
});
|
||||
|
||||
import { allows, refusal, parseCallers, parseVerbCallers, parseConfiguredVerbCallers, moduleOf, UNPROVEN } from "../dist/callers/index.js";
|
||||
import { Follower, statusTools, keepStatus, statusKey, type Answer, type Status } from "../dist/views/index.js";
|
||||
|
||||
test("callers and verb callers judge a proved caller by the Go side's rules (ADR 0459 §2, ADR 0627 §2)", () => {
|
||||
const rows: [string, string[], string, boolean][] = [
|
||||
["a module on any machine", ["mesh-delivery-forge-bridge"], "ace.mesh-delivery-forge-bridge", true],
|
||||
["exactly a machine's module", ["ace.mesh-delivery-forge-bridge"], "g14.mesh-delivery-forge-bridge", false],
|
||||
["exactly a person", ["person.jochen"], "person.jochen", true],
|
||||
["a module entry is not a person", ["jochen"], "person.jochen", false],
|
||||
["a wildcard is no entry", ["*", ">"], "ace.anything", false],
|
||||
["an unproven caller never matches", ["unproven"], UNPROVEN, false],
|
||||
];
|
||||
for (const [what, list, caller, want] of rows) assert.equal(allows(list, caller), want, what);
|
||||
assert.deepEqual(parseCallers('{"callers":["a","person.b"]}'), ["a", "person.b"]);
|
||||
assert.throws(() => parseCallers('{"callers":["a"],"more":1}'), /unknown field "more"/);
|
||||
|
||||
const verbs = parseVerbCallers('{"pull":["*"],"merge-pull":["mesh-delivery-forge-bridge"],"comment":["operator"]}', ["pull", "merge-pull", "comment"], {});
|
||||
assert.equal(refusal(verbs, "pull", "ace.anyone"), undefined);
|
||||
assert.match(String(refusal(verbs, "pull", UNPROVEN)), /carries no caller the tool runner proved/);
|
||||
assert.equal(refusal(verbs, "merge-pull", "novox.mesh-delivery-forge-bridge"), undefined);
|
||||
assert.match(String(refusal(verbs, "merge-pull", "person.jochen")), /refused for person.jochen: not on the holder's caller list for merge-pull/);
|
||||
assert.equal(refusal(verbs, "comment", "person.jochen"), undefined);
|
||||
assert.ok(refusal(verbs, "comment", "ace.mesh-delivery-forge-bridge"));
|
||||
assert.ok(refusal(verbs, "status", "ace.anyone"), "a verb the list does not name refuses everyone");
|
||||
assert.throws(() => parseVerbCallers('{"nope":["*"]}', ["pull"], {}), /names the verb "nope", which the holder does not serve/);
|
||||
const defaults = { pull: ["*"] };
|
||||
assert.equal(parseVerbCallers(" ", ["pull"], defaults), defaults);
|
||||
assert.equal(parseConfiguredVerbCallers(null, ["pull"], defaults), defaults);
|
||||
assert.deepEqual(parseConfiguredVerbCallers('{"pull":["a"]}', ["pull"], defaults), { pull: ["a"] });
|
||||
assert.deepEqual(parseConfiguredVerbCallers({ pull: ["b"] }, ["pull"], defaults), { pull: ["b"] });
|
||||
assert.throws(() => parseConfiguredVerbCallers("{", ["pull"], defaults), /not a JSON object/);
|
||||
assert.equal(moduleOf("ace.mesh-delivery-forge-bridge"), "mesh-delivery-forge-bridge");
|
||||
});
|
||||
|
||||
test("a first view follows its owner's events, asks again on a gap and by hand, and its two tools read and steer it (ADR 0615)", async () => {
|
||||
const created = "2026-11-09T10:00:00.000Z";
|
||||
let held: Record<string, unknown> = { a: 1, b: 2, c: 3 };
|
||||
let revision = 4;
|
||||
let down = 2;
|
||||
const asked: string[] = [];
|
||||
const raised: Status[] = [];
|
||||
const cleared: Status[] = [];
|
||||
const pauses: number[] = [];
|
||||
let clock = Date.parse(created);
|
||||
// The owner answers two keys a page, and is down for its first two asks.
|
||||
const ask = async (after: string): Promise<Answer> => {
|
||||
asked.push(after);
|
||||
if (down > 0) {
|
||||
down--;
|
||||
throw new Error("no responders");
|
||||
}
|
||||
const keys = Object.keys(held).sort().filter((k) => k > after);
|
||||
const page = keys.slice(0, 2);
|
||||
return { v: 1, revision, created, values: Object.fromEntries(page.map((k) => [k, held[k]])), next: keys.length > 2 ? page[1] : "" };
|
||||
};
|
||||
const kept = new Map<string, Status>();
|
||||
const st = {
|
||||
get: async (k: string) => (kept.has(k) ? { key: k, value: kept.get(k)!, revision: 1 } : null),
|
||||
put: async (k: string, v: Status) => (kept.set(k, structuredClone(v)), 1),
|
||||
delete: async (k: string) => void kept.delete(k),
|
||||
keys: async () => [...kept.keys()].sort(),
|
||||
watch: async () => {},
|
||||
};
|
||||
const f = new Follower({
|
||||
owner: "mesh-catalog", tool: "mesh-catalog.revision", state: "catalog",
|
||||
ask, said: keepStatus(st), raise: (s) => void raised.push(s), clear: (s) => void cleared.push(s),
|
||||
now: () => new Date(clock), bound: 1000, wait: 10,
|
||||
pause: async (ms) => { pauses.push(ms); clock += 1000; },
|
||||
});
|
||||
// An event before the first view is answered is passed over: the answer holds it.
|
||||
assert.deepEqual(await f.apply({ key: "a", revision: 4, created, value: 1 }), { gap: false, why: "" });
|
||||
await f.start();
|
||||
assert.deepEqual(pauses, [10, 20], "a failed ask waits, doubling");
|
||||
assert.equal(raised.length, 1, "past its bound it raises its condition");
|
||||
assert.equal(cleared.length, 1, "the answer clears it");
|
||||
assert.deepEqual(Object.fromEntries(f.picture()), { a: 1, b: 2, c: 3 }, "every page is taken");
|
||||
assert.deepEqual(asked.slice(-2), ["", "b"]);
|
||||
let s = kept.get(statusKey("mesh-catalog", "catalog"))!;
|
||||
assert.equal(s.revision, 4);
|
||||
assert.equal(s.asks, 3);
|
||||
assert.equal(s.raised, false);
|
||||
assert.equal(s.keys, 3);
|
||||
|
||||
// The next revision is taken; one at or below the picture's is passed over.
|
||||
assert.deepEqual(await f.apply({ key: "d", revision: 5, created, value: 4 }), { gap: false, why: "" });
|
||||
assert.deepEqual(await f.apply({ key: "a", revision: 3, created, value: 0 }), { gap: false, why: "" });
|
||||
assert.equal(f.get("d"), 4);
|
||||
assert.equal(f.get("a"), 1);
|
||||
assert.equal(kept.get(statusKey("mesh-catalog", "catalog"))!.revision, 5, "the kept status follows the events");
|
||||
|
||||
// A revision past the next one is a gap: hearing it asks the first view once more.
|
||||
held = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6 };
|
||||
revision = 7;
|
||||
const before = asked.length;
|
||||
await f.hear({ key: "f", revision: 7, created, value: 6 });
|
||||
await f.ready;
|
||||
for (let i = 0; i < 10 && f.status().revision !== 7; i++) await Promise.resolve();
|
||||
assert.ok(asked.length > before, "a gap asks again");
|
||||
assert.equal(f.status().revision, 7);
|
||||
assert.equal(f.status().why?.startsWith("a gap:"), true);
|
||||
assert.equal(f.get("e"), 5);
|
||||
|
||||
// The two tools: read lists the kept status; again records the hand act first and asks by hand with the caller.
|
||||
const acts: unknown[] = [];
|
||||
const [read, again] = statusTools("gitea", "gitea_first_views", "gitea_first_view_again", [f], st, async (a) => void acts.push(a));
|
||||
const listed = (await read.run({})) as { "first-views": Status[] };
|
||||
assert.equal(listed["first-views"].length, 1);
|
||||
assert.equal(listed["first-views"][0].tool, "mesh-catalog.revision");
|
||||
await assert.rejects(again.run({ owner: "mesh-catalog", state: "catalog" }), /names the owner, the state and why/);
|
||||
await assert.rejects(again.run({ owner: "x", state: "catalog", why: "w" }), /holds no first view of x's catalog/);
|
||||
const [, refused] = statusTools("gitea", "r", "g", [f], st, undefined);
|
||||
await assert.rejects(refused.run({ owner: "mesh-catalog", state: "catalog", why: "w" }), /nothing was asked again/);
|
||||
const byHandBefore = f.status()["by-hand"];
|
||||
await again.run({ owner: "mesh-catalog", state: "catalog", why: "the picture is doubted" });
|
||||
for (let i = 0; i < 10 && f.status()["by-hand"] === byHandBefore; i++) await Promise.resolve();
|
||||
assert.equal(acts.length, 1);
|
||||
assert.equal(f.status()["by-hand"], byHandBefore + 1);
|
||||
assert.match(f.status().why ?? "", /^by hand: the picture is doubted \(unproven\)$/);
|
||||
// Another created is the state made again: a gap too.
|
||||
f.close();
|
||||
assert.equal((await f.apply({ key: "a", revision: 1, created: "2026-11-09T11:00:00.000Z", value: 9 })).gap, true);
|
||||
});
|
||||
|
||||
+24
-1
@@ -26,7 +26,7 @@ test("a bundle on stdio lists its tools and answers a call, a seat's verb among
|
||||
assert.equal((await ask(1, "initialize", { protocolVersion: "2025-03-26", capabilities: {} })).result.serverInfo.name, "lamp");
|
||||
child.stdin.write(JSON.stringify({ jsonrpc: "2.0", method: "notifications/initialized" }) + "\n");
|
||||
const listed = (await ask(2, "tools/list")).result.tools;
|
||||
assert.deepEqual(listed.map((t: any) => t.name), ["brightness", "node-lamp.on", "fail"]);
|
||||
assert.deepEqual(listed.map((t: any) => t.name), ["brightness", "node-lamp.on", "fail", "who"]);
|
||||
assert.deepEqual(listed[0].inputSchema, { type: "object", properties: { level: { type: "number", description: "0 to 10" } } });
|
||||
const called = (await ask(3, "tools/call", { name: "brightness", arguments: { level: 7 } })).result;
|
||||
assert.deepEqual(JSON.parse(called.content[0].text), { level: 7 });
|
||||
@@ -34,6 +34,21 @@ test("a bundle on stdio lists its tools and answers a call, a seat's verb among
|
||||
assert.equal(failed.isError, true);
|
||||
assert.equal(failed.content[0].text, "the bulb is gone");
|
||||
assert.equal((await ask(5, "tools/call", { name: "nothing" })).error.code, -32602);
|
||||
// The caller the runtime proved in `_meta` is handed to the tool, never one in the arguments (issue 365).
|
||||
const proved = { "mesh/caller": "ace.mesh-delivery-forge-bridge", "mesh/call": "c-17" };
|
||||
const who = (await ask(6, "tools/call", { name: "who", arguments: { caller: "person.mallory" }, _meta: proved })).result;
|
||||
assert.deepEqual(JSON.parse(who.content[0].text), { caller: "ace.mesh-delivery-forge-bridge", call: "c-17" });
|
||||
const unnamed = (await ask(7, "tools/call", { name: "who", arguments: { caller: "person.mallory" } })).result;
|
||||
assert.deepEqual(JSON.parse(unnamed.content[0].text), { caller: "unproven", call: "" });
|
||||
const malformed = (await ask(8, "tools/call", { name: "who", arguments: {}, _meta: { "mesh/caller": "a b", "mesh/call": "x y" } })).result;
|
||||
assert.deepEqual(JSON.parse(malformed.content[0].text), { caller: "unproven", call: "" });
|
||||
// A seat's verb judges its callers by the proof: the named module is answered, another and an unproven one refused.
|
||||
assert.deepEqual(JSON.parse((await ask(9, "tools/call", { name: "node-lamp.on", arguments: {}, _meta: proved })).result.content[0].text), { on: true });
|
||||
const other = (await ask(10, "tools/call", { name: "node-lamp.on", arguments: {}, _meta: { "mesh/caller": "ace.mallory" } })).result;
|
||||
assert.equal(other.isError, true);
|
||||
assert.equal(other.content[0].text, "node-lamp.on: refused for ace.mallory: not on the holder's caller list for node-lamp.on");
|
||||
const none = (await ask(11, "tools/call", { name: "node-lamp.on", arguments: {} })).result;
|
||||
assert.match(none.content[0].text, /carries no caller the tool runner proved/);
|
||||
} finally {
|
||||
child.stdin.end();
|
||||
child.kill();
|
||||
@@ -100,6 +115,14 @@ test("a launched tool's emit is asked of the runtime, and the call answers once
|
||||
send({ jsonrpc: "2.0", id: asked.id, result: {} });
|
||||
const answered = await until((m) => m.id === 2);
|
||||
assert.deepEqual(JSON.parse(answered.result.content[0].text), { rang: true });
|
||||
// A seat's event is asked of the runtime as a seat's publish, its subject whole and its id kept (ADR 0259 §3).
|
||||
send({ jsonrpc: "2.0", id: 3, method: "tools/call", params: { name: "peal", arguments: {} } });
|
||||
const said = await until((m) => m.method === "mesh/seat.publish");
|
||||
assert.ok(said, "the seat's event was not asked of the runtime");
|
||||
assert.deepEqual(said.params, { subject: "mesh.seat.bell.event.pealed.anchor", body: { times: 2 }, id: "p-1" });
|
||||
assert.equal(seen.find((m) => m.id === 3), undefined, "the call answered before its seat's event was published");
|
||||
send({ jsonrpc: "2.0", id: said.id, result: {} });
|
||||
assert.deepEqual(JSON.parse((await until((m) => m.id === 3)).result.content[0].text), { pealed: true });
|
||||
} finally {
|
||||
child.kill();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user