mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-11-30 17:07:50 +00:00
* OpenFleet reference implementation: @logicsrc/openfleet 0.1.0 and logicsrc fleet Ship what docs/openfleet.md describes. The new workspace package holds the record (write once, never overwrite, 0600), the ledger (append-only JSON Lines, merged across ledger*.jsonl by at), the ceiling rules (whole fleet ceiling, narrowed swarm keys, a merge that never widens, refusals by key), claiming and deriving exactly as the spec's "Claiming and deriving" and rule 13, and fold(), which turns any $OPENFLEET_HOME plus the engine rosters into the tree the landing page shows. logicsrc fleet open|cap|tree|stop|log are the sysop's verbs, every one with --json. open and cap exit 4 when OPENFLEET_MEMBER is set; stop exits 4 outside the caller's subtree, ends nested swarms first, goes through each member's own engine (claude stop, moshcode herd kill, tmux kill-pane, a signal for claude-p) and writes one swarm.end per swarm. tree reads claude agents --json --all and ~/.moshcode/herd/sessions.json when it can, draws recordless sessions as roster roots of the implicit fleet, and writes member.end lost for a recorded member its engine no longer lists. Claude Code takes part through hooks: logicsrc fleet hooks install merges SessionStart, UserPromptSubmit, PreToolUse, Stop and SessionEnd into ~/.claude/settings.json without clobbering it, and logicsrc fleet hook <Event> runs each one. SessionStart claims, derives or writes a root record and hands the member its variables through CLAUDE_ENV_FILE; UserPromptSubmit checks the ceiling with the permission mode the engine reports and writes member.start, or refuses the first prompt with exit 2 and ceiling.refuse; PreToolUse denies an edit outside piece.owns; Stop and SessionEnd write member.end. A hand-started root takes the engine's reported approvals before member.start, since the command line only guesses them. Hooks never fail the engine: everything is caught and logged to hooks.log. The spec and the landing page now say what ships, keep Status 0.1, and record the two verified Claude Code limits: a background job dispatched from claude agents gets no launcher environment, and OPENFLEET_* exported at SessionStart reach the member's tools but not later hooks, so hooks key on session_id through $OPENFLEET_HOME/sessions/<session_id>.json. PRD 0008 covers the work. CLI 0.2.1 -> 0.3.0; build and build:cli chains build the package before the CLI; README and docs/cli.md list the group. Tests: 95 in the package (record, ledger merge, every narrower case, the worked example's claim and derive, the folded tree, hook install idempotence, each hook handler including the exit-2 refusal and the PreToolUse deny, every verb with fake deps) and 4 in the CLI. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RZV4zJ2pDZLNN3kE5jFCmV * OpenFleet fix round: rebuild the ceiling from the ledger, once-markers, rule 6 in tree, lost only for what a roster can hold The review of the reference implementation against moshcode found the two readers disagreeing on the same files. This round applies the shared rulings so both sides read a ledger the same way. Ceiling (R-A, R-B, R-C, R1, R6, R10, R15, R17): memberCeiling rebuilds the effective ceiling from the ledger on every read. The latest fleet-target fleet.cap (else fleet.open, else the implicit fleet's) replaces the copy in a record, so a sysop's widening cap reaches running members; then each swarm.spawn narrowing down the path, then swarm caps last. In the implicit fleet a parentless record's own approvals enters at the root; a ceiling a writer left without the key is never read as native, and startMember fills it with the engine's word while the record is unclaimed. A fleet.open or cap with no hosts means the host it was written on (R23). Once-markers (R-G, R28): member.start, member.end and swarm.end each take an exclusive create under fleets/<fleet>/marks/<event>.<id> before the append; a lost end takes <id>.lost so a real end can still supersede it. The hooks let a real end follow a lost line (R9). tree (R-F, R20): run by the sysop it enforces rule 6, stopping a member past its effective until with state timeout and the members of a swarm or fleet at its budget with state budget, then writes swarm.end for each swarm touched once it is complete. An agent's tree stops nothing. lost is written only for a member its engine's roster can hold: a claude-code background job (8-hex member or session) or a moshcode pane, never an interactive session claude agents does not list (R-E, R3, R14). A nested swarm is drawn under the member that spawned it and its row shows the effective ceiling (R25). stop and cap (R-D, R-H, R22, R27): swarm.end is written only once every member and every nested swarm has an end line that counts; an engine that will not end a member leaves it without an end line and the verb exits non-zero. claude stop takes the job id: the member of a background job, else the first eight characters of a session UUID; an interactive session with no job id cannot be stopped and the tool says so. cap on a swarm refuses a key that would widen. A derived claude-code job is named by its job id and carries no pid. Also: R-I (endMember ends only the engine-minted swarm of one), R35 (a derived record's guessed approvals corrected at UserPromptSubmit), R32 (the UserPromptSubmit hook passes only exit 2 through), R31 (package README), R36 (rule 13 says the launcher test is unimplemented in 0.1), docs and PRD 0008 updated for lost, rule 6 and the markers. 113 openfleet tests, 93 CLI tests, contract green. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RZV4zJ2pDZLNN3kE5jFCmV * openfleet hooks: no member.end for a member that never started A first prompt refused by the ceiling still lets the session wind down through Stop and SessionEnd; those handlers now write nothing when the ledger holds no member.start for the member, so a refused member is never drawn as done. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RZV4zJ2pDZLNN3kE5jFCmV * logicsrc-mcp test: the next free PRD id is 0009 now that PRD 0008 exists Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RZV4zJ2pDZLNN3kE5jFCmV --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
167 lines
13 KiB
TypeScript
167 lines
13 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
import { flattenMembers, flattenSwarms, fold, renderTree } from "./fold.js";
|
|
import { append, writeRecord } from "./store.js";
|
|
import { DEV, FLEET, PLANNER, ROOT, cleanup, seedWorkedExampleToEnd, tempHome } from "./test-helpers.js";
|
|
import type { RosterRow } from "./types.js";
|
|
|
|
describe("fold: the worked example", () => {
|
|
let home: string;
|
|
beforeEach(() => {
|
|
home = tempHome();
|
|
seedWorkedExampleToEnd(home);
|
|
});
|
|
afterEach(() => cleanup(home));
|
|
|
|
it("builds the implicit fleet, the root, the planner's swarm of one and the two-piece swarm", async () => {
|
|
const tree = await fold(home, {}, { implicit: DEV, host: "dev" });
|
|
expect(tree.fleets.length).toBe(1);
|
|
const fleet = tree.fleets[0];
|
|
expect(fleet).toMatchObject({ fleet: FLEET, sysop: FLEET, implicit: true, ceiling: { depth: 1, hosts: ["dev"] }, swarms: [] });
|
|
expect(fleet.roots.length).toBe(1);
|
|
const root = fleet.roots[0];
|
|
expect(root).toMatchObject({ member: "460a4502", engine: "claude-code", depth: 0, state: "working", approvals: "bypass", host: "dev" });
|
|
expect(root.alive).toBeUndefined();
|
|
expect(root.swarms.map((swarm) => swarm.swarm)).toEqual(["460a4502-1", "create-two-0541"]);
|
|
|
|
const planner = root.swarms[0];
|
|
expect(planner).toMatchObject({ by: "460a4502", state: "done", ceiling: {} });
|
|
expect(planner.members.map((member) => [member.member, member.session, member.engine, member.state])).toEqual([[PLANNER, "4242", "claude-p", "done"]]);
|
|
|
|
const swarm = root.swarms[1];
|
|
expect(swarm).toMatchObject({ task: "create two ...", by: "460a4502", ceiling: { fan_out: 4, until: "2026-09-13T06:11:01Z" } });
|
|
expect(swarm.state).toBeUndefined();
|
|
expect(swarm.members.map((member) => ({ member: member.member, session: member.session, title: member.title, engine: member.engine, state: member.state, approvals: member.approvals, owns: member.owns, depth: member.depth }))).toEqual([
|
|
{ member: "create-two-0541-1", session: "172ffd83", title: "create hello.sh bash", engine: "claude-code", state: "done", approvals: "bypass", owns: ["hello.sh"], depth: 1 },
|
|
{ member: "create-two-0541-2", session: "create-two-0541-2", title: "create bye.sh bash", engine: "moshcode/claude", state: "working", approvals: "bypass", owns: ["bye.sh"], depth: 1 },
|
|
]);
|
|
expect(swarm.members[0].summary).toBe("Created hello.sh, mode -rwxrwxr-x, prints hello.");
|
|
expect(flattenMembers(tree).map((entry) => entry.member.member)).toEqual(["460a4502", PLANNER, "create-two-0541-1", "create-two-0541-2"]);
|
|
expect(flattenSwarms(tree).map((entry) => entry.swarm.swarm)).toEqual(["460a4502-1", "create-two-0541"]);
|
|
});
|
|
|
|
it("renders the landing page's tree", async () => {
|
|
const tree = await fold(home, {}, { implicit: DEV, host: "dev" });
|
|
const text = renderTree(tree, { host: "dev" });
|
|
const lines = text.split("\n");
|
|
expect(lines[0]).toBe("anthony@dev (implicit fleet, sysop anthony@dev, depth 1, hosts dev)");
|
|
expect(lines[1]).toMatch(/^└─ 460a4502 +claude-code +working {2}\[bypass\]$/);
|
|
expect(lines[2]).toMatch(/^ {3}├─ swarm 460a4502-1 +claude -p "Split the task below into at most 4 \.\.\." +1 member +done$/);
|
|
expect(lines[3]).toMatch(/^ {3}│ {2}└─ 9f1c2d3e-0000-4000-8000-000000000001 \(4242\) +claude-p +done {2}\[bypass\]$/);
|
|
expect(lines[4]).toMatch(/^ {3}└─ swarm create-two-0541 +"create two \.\.\." +2\/4 members +until 06:11$/);
|
|
expect(lines[5]).toMatch(/^ {6}├─ create-two-0541-1 \(172ffd83\) +create hello.sh bash +claude-code +done {2}\[bypass\] {2}owns hello.sh$/);
|
|
expect(lines[6]).toMatch(/^ {6}└─ create-two-0541-2 +create bye.sh bash +moshcode\/claude +working {2}\[bypass\] {2}owns bye.sh$/);
|
|
expect(lines.length).toBe(7);
|
|
expect(renderTree({ fleets: [] })).toBe("(no fleets)");
|
|
});
|
|
|
|
it("joins the rosters: liveness for recorded members, a name for a root, and recordless sessions as roster roots", async () => {
|
|
const claude = async (): Promise<RosterRow[]> => [
|
|
{ engine: "claude-code", id: "460a4502", sessionId: "460a4502-aaaa-bbbb-cccc-dddddddddddd", name: "ultracode mode documentation", state: "working", approvals: "bypass" },
|
|
{ engine: "claude-code", id: "b9fc0f52", sessionId: "b9fc0f52-322b-452e-8523-77945ab1d777", name: "meta card screenshots stream", state: "working", approvals: "native" },
|
|
];
|
|
const moshcode = async (): Promise<RosterRow[]> => [];
|
|
const tree = await fold(home, { claude, moshcode }, { implicit: DEV, host: "dev" });
|
|
const fleet = tree.fleets[0];
|
|
const root = fleet.roots.find((node) => node.member === "460a4502")!;
|
|
expect(root.alive).toBe(true);
|
|
expect(root.title).toBe("ultracode mode documentation");
|
|
const rosterOnly = fleet.roots.find((node) => node.member === "b9fc0f52")!;
|
|
expect(rosterOnly).toMatchObject({ roster: true, alive: true, state: "working", approvals: "native", title: "meta card screenshots stream", depth: 0, engine: "claude-code" });
|
|
// moshcode's roster was readable and lists nothing: the pane is gone.
|
|
const pane = root.swarms[1].members.find((node) => node.member === "create-two-0541-2")!;
|
|
expect(pane.alive).toBe(false);
|
|
// 172ffd83 claimed its record and is not in the roster: gone too. The planner ended, so liveness is moot but reported.
|
|
expect(root.swarms[1].members[0].alive).toBe(false);
|
|
const text = renderTree(tree, { host: "dev" });
|
|
expect(text).toMatch(/^├─ 460a4502 +ultracode mode documentation +claude-code +working {2}\[bypass\]$/m);
|
|
expect(text).toMatch(/^└─ b9fc0f52 \(b9fc0f52-322b-452e-8523-77945ab1d777\) +meta card screenshots stream +claude-code +working {2}\[roster\]$/m);
|
|
expect(text).toMatch(/create-two-0541-2 +create bye.sh bash +moshcode\/claude +working {2}\[bypass\] {2}\[gone\]/);
|
|
});
|
|
|
|
it("ignores a roster that cannot be read, and reads only one fleet when asked", async () => {
|
|
const tree = await fold(home, { claude: async () => null, moshcode: async () => { throw new Error("no manifest"); } }, { implicit: DEV, host: "dev", fleet: FLEET });
|
|
expect(tree.fleets[0].roots[0].alive).toBeUndefined();
|
|
const none = await fold(home, {}, { implicit: DEV, host: "dev", fleet: "other" });
|
|
expect(none.fleets).toEqual([{ fleet: "other", sysop: FLEET, implicit: true, ceiling: { depth: 1, hosts: ["dev"] }, roots: [], swarms: [], spend: {} }]);
|
|
});
|
|
});
|
|
|
|
describe("fold: opened fleets, nesting, spend and orphans", () => {
|
|
let home: string;
|
|
beforeEach(() => {
|
|
home = tempHome();
|
|
});
|
|
afterEach(() => cleanup(home));
|
|
|
|
it("puts a sysop-started swarm at the fleet level, nests by parent_swarm, sums spend against budget, marks orphans", async () => {
|
|
const fleet = "team-20260913";
|
|
append(home, fleet, { at: "2026-09-13T05:00:00Z", event: "fleet.open", by: "sysop", fleet, sysop: "https://anthony.example/profile.md", ceiling: { approvals: "bypass", depth: 3, hosts: ["dev"], budget: "20 USD" } }, { host: "dev" });
|
|
append(home, fleet, { at: "2026-09-13T05:01:00Z", event: "swarm.spawn", by: "sysop", swarm: "hand-0501", task: "by hand", ceiling: { fan_out: 2 }, pieces: [{ member: "hand-0501-1", title: "one" }] }, { host: "dev" });
|
|
append(home, fleet, { at: "2026-09-13T05:02:00Z", event: "member.start", by: "hand-0501-1", member: "hand-0501-1", session: "h1", swarm: "hand-0501", depth: 0, engine: "moshcode/codex", approvals: "native" }, { host: "dev" });
|
|
append(home, fleet, { at: "2026-09-13T05:03:00Z", event: "swarm.spawn", by: "hand-0501-1", swarm: "inner-0503", parent_swarm: "hand-0501", task: "nested", ceiling: {}, pieces: [{ member: "inner-0503-1" }] }, { host: "dev" });
|
|
append(home, fleet, { at: "2026-09-13T05:04:00Z", event: "member.start", by: "inner-0503-1", member: "inner-0503-1", session: "i1", swarm: "inner-0503", parent: "hand-0501-1", depth: 1, engine: "moshcode/kimi", approvals: "native" }, { host: "dev" });
|
|
append(home, fleet, { at: "2026-09-13T05:05:00Z", event: "member.spend", by: "inner-0503-1", member: "inner-0503-1", amount: "3 USD", total: "3 USD" }, { host: "dev" });
|
|
append(home, fleet, { at: "2026-09-13T05:06:00Z", event: "member.spend", by: "inner-0503-1", member: "inner-0503-1", amount: "2 USD", total: "5 USD" }, { host: "dev" });
|
|
append(home, fleet, { at: "2026-09-13T05:06:00Z", event: "member.spend", by: "hand-0501-1", member: "hand-0501-1", amount: "1000 tokens", total: "1000 tokens" }, { host: "dev" });
|
|
writeRecord(home, { ...ROOT, fleet, sysop: "https://anthony.example/profile.md", member: "stray", orphan: true, approvals: "native", ceiling: { approvals: "native", depth: 1, hosts: ["dev"] } });
|
|
|
|
const tree = await fold(home, {}, { implicit: DEV, host: "dev" });
|
|
const node = tree.fleets[0];
|
|
expect(node).toMatchObject({ fleet, sysop: "https://anthony.example/profile.md", implicit: false, ceiling: { approvals: "bypass", depth: 3, hosts: ["dev"], budget: "20 USD" } });
|
|
expect(node.roots.map((root) => [root.member, root.state, root.orphan])).toEqual([["stray", "unclaimed", true]]);
|
|
expect(node.swarms.length).toBe(1);
|
|
const hand = node.swarms[0];
|
|
expect(hand).toMatchObject({ swarm: "hand-0501", by: "sysop", spend: { USD: 5, tokens: 1000 } });
|
|
expect(hand.members.length).toBe(1);
|
|
// The nested swarm sits under the member that spawned it, the row that says who, not under the parent swarm.
|
|
expect(hand.swarms).toEqual([]);
|
|
const spawner = hand.members[0];
|
|
expect(spawner.swarms.map((swarm) => swarm.swarm)).toEqual(["inner-0503"]);
|
|
expect(spawner.swarms[0].members[0]).toMatchObject({ member: "inner-0503-1", spend: "5 USD", depth: 1, parent: "hand-0501-1" });
|
|
// A swarm carries what it narrowed and what it runs under: the fleet's budget reaches the row.
|
|
expect(hand.ceiling).toEqual({ fan_out: 2 });
|
|
expect(hand.effective).toEqual({ approvals: "bypass", depth: 3, hosts: ["dev"], budget: "20 USD", fan_out: 2 });
|
|
expect(spawner.swarms[0].effective).toEqual({ approvals: "bypass", depth: 3, hosts: ["dev"], budget: "20 USD", fan_out: 2 });
|
|
expect(node.spend).toEqual({ USD: 5, tokens: 1000 });
|
|
expect(flattenSwarms(tree).map((entry) => [entry.swarm.swarm, entry.parentMember])).toEqual([["hand-0501", null], ["inner-0503", "hand-0501-1"]]);
|
|
|
|
const text = renderTree(tree, { host: "dev" });
|
|
expect(text.split("\n")[0]).toBe("team-20260913 (fleet, sysop https://anthony.example/profile.md, approvals bypass, depth 3, hosts dev, budget 20 USD, spent 5/20 USD)");
|
|
expect(text).toMatch(/└─ swarm hand-0501 +"by hand" +1\/2 members +5\/20 USD/);
|
|
expect(text).toMatch(/├─ stray +claude-code +unclaimed {2}\[orphan\]/);
|
|
// The nested swarm inherits fan_out 2 and the fleet's budget.
|
|
expect(text).toMatch(/└─ swarm inner-0503 +"nested" +1\/2 members +5\/20 USD/);
|
|
expect(text).toMatch(/└─ inner-0503-1 \(i1\) +moshcode\/kimi +working {2}5 USD/);
|
|
});
|
|
});
|
|
|
|
describe("fold: which members a roster can hold", () => {
|
|
let home: string;
|
|
beforeEach(() => {
|
|
home = tempHome();
|
|
});
|
|
afterEach(() => cleanup(home));
|
|
|
|
it("never reads an interactive claude session as gone, and reads a background job or a pane as gone when its roster is readable and silent", async () => {
|
|
const interactive = "68aca9c1-1111-4222-8333-444455556666";
|
|
// An interactive root: the member is the session UUID, there is no job id, and `claude agents` never lists it.
|
|
append(home, FLEET, { at: "2026-09-13T05:00:00Z", event: "member.start", by: interactive, member: interactive, session: interactive, depth: 0, engine: "claude-code", approvals: "native" }, { host: "dev" });
|
|
// A background job: an 8-hex member the roster can hold.
|
|
append(home, FLEET, { at: "2026-09-13T05:01:00Z", event: "member.start", by: "b9fc0f52", member: "b9fc0f52", session: "b9fc0f52", depth: 0, engine: "claude-code", approvals: "native" }, { host: "dev" });
|
|
// A claimed piece whose session is a job id.
|
|
append(home, FLEET, { at: "2026-09-13T05:02:00Z", event: "member.start", by: "piece-0502-1", member: "piece-0502-1", session: "172ffd83", depth: 1, engine: "claude-code", approvals: "native" }, { host: "dev" });
|
|
// A tmux pane moshcode started: in the herd manifest under its member id.
|
|
append(home, FLEET, { at: "2026-09-13T05:03:00Z", event: "member.start", by: "piece-0502-2", member: "piece-0502-2", session: "%7", depth: 1, engine: "tmux", approvals: "native" }, { host: "dev" });
|
|
// A claude -p: no roster holds it.
|
|
append(home, FLEET, { at: "2026-09-13T05:04:00Z", event: "member.start", by: "p1", member: "p1", session: "31337", depth: 1, engine: "claude-p", approvals: "native" }, { host: "dev" });
|
|
const tree = await fold(home, { claude: async () => [], moshcode: async () => [] }, { implicit: DEV, host: "dev" });
|
|
const alive = Object.fromEntries(tree.fleets[0].roots.map((node) => [node.member, node.alive]));
|
|
expect(alive).toEqual({ [interactive]: undefined, b9fc0f52: false, "piece-0502-1": false, "piece-0502-2": false, p1: undefined });
|
|
const text = renderTree(tree, { host: "dev" });
|
|
expect(text).not.toMatch(new RegExp(`${interactive}.*\\[gone\\]`));
|
|
expect(text).toMatch(/b9fc0f52 +claude-code +working {2}\[gone\]/);
|
|
// With the rosters unreadable nothing is gone.
|
|
const blind = await fold(home, { claude: async () => null, moshcode: async () => null }, { implicit: DEV, host: "dev" });
|
|
expect(blind.fleets[0].roots.every((node) => node.alive === undefined)).toBe(true);
|
|
});
|
|
});
|