@george-veras/acta 0.0.0-stage → 0.1.1

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.
@@ -0,0 +1,62 @@
1
+ import type { Schema } from "./validate.ts";
2
+ export type Guard = (sliceValue: unknown, payload: unknown) => true | string;
3
+ export type Apply = (sliceValue: unknown, payload: unknown) => unknown;
4
+ export interface SliceDecl {
5
+ schema: Schema;
6
+ initial: unknown;
7
+ }
8
+ export interface TransitionDecl {
9
+ slice: string;
10
+ seats: string[];
11
+ input: Schema;
12
+ guard?: Guard;
13
+ apply: Apply;
14
+ }
15
+ export interface SeatDecl {
16
+ token?: string;
17
+ manifest?: {
18
+ transitions: string[];
19
+ };
20
+ }
21
+ export interface DeadlineDecl {
22
+ name: string;
23
+ dueAt: string;
24
+ awaiting: string;
25
+ matchPayload?: Record<string, unknown>;
26
+ expiryTransition: string;
27
+ seat: string;
28
+ payload?: unknown;
29
+ }
30
+ export interface EffectContext {
31
+ version: number;
32
+ record: {
33
+ seat: string;
34
+ transition: string;
35
+ slice: string;
36
+ payload: unknown;
37
+ };
38
+ read: (slice: string) => {
39
+ value: unknown;
40
+ version: number;
41
+ };
42
+ emit: (transition: string, payload: unknown) => Promise<{
43
+ ok: boolean;
44
+ version?: number;
45
+ reason?: string;
46
+ }>;
47
+ }
48
+ export interface EffectDecl {
49
+ name: string;
50
+ reads: string;
51
+ after?: string[];
52
+ seat: string;
53
+ failureTransition?: string;
54
+ run: (ctx: EffectContext) => void | Promise<void>;
55
+ }
56
+ export interface ActaConfig {
57
+ slices: Record<string, SliceDecl>;
58
+ transitions: Record<string, TransitionDecl>;
59
+ seats: Record<string, SeatDecl>;
60
+ effects?: EffectDecl[];
61
+ }
62
+ export declare function defineActa(config: ActaConfig): ActaConfig;
@@ -0,0 +1,57 @@
1
+ export function defineActa(config) {
2
+ for (const [seatName, seat] of Object.entries(config.seats)) {
3
+ for (const transition of seat.manifest?.transitions ?? []) {
4
+ const decl = config.transitions[transition];
5
+ if (!decl) {
6
+ throw new Error(`seat "${seatName}" manifest names undeclared transition "${transition}"`);
7
+ }
8
+ if (!decl.seats.includes(seatName)) {
9
+ throw new Error(`seat "${seatName}" manifest promises transition "${transition}", which does not allow that seat`);
10
+ }
11
+ }
12
+ }
13
+ for (const [name, decl] of Object.entries(config.transitions)) {
14
+ if (!(decl.slice in config.slices)) {
15
+ throw new Error(`transition "${name}" writes undeclared slice "${decl.slice}"`);
16
+ }
17
+ for (const seat of decl.seats) {
18
+ if (!(seat in config.seats)) {
19
+ throw new Error(`transition "${name}" allows undeclared seat "${seat}"`);
20
+ }
21
+ }
22
+ if (typeof decl.apply !== "function") {
23
+ throw new Error(`transition "${name}" has no apply function`);
24
+ }
25
+ }
26
+ const effectNames = new Set();
27
+ for (const effect of config.effects ?? []) {
28
+ if (effectNames.has(effect.name)) {
29
+ throw new Error(`duplicate effect name "${effect.name}"`);
30
+ }
31
+ effectNames.add(effect.name);
32
+ if (!(effect.reads in config.slices)) {
33
+ throw new Error(`effect "${effect.name}" reads undeclared slice "${effect.reads}"`);
34
+ }
35
+ if (!(effect.seat in config.seats)) {
36
+ throw new Error(`effect "${effect.name}" acts as undeclared seat "${effect.seat}"`);
37
+ }
38
+ for (const transition of effect.after ?? []) {
39
+ if (!(transition in config.transitions)) {
40
+ throw new Error(`effect "${effect.name}" subscribes to undeclared transition "${transition}"`);
41
+ }
42
+ }
43
+ if (effect.failureTransition !== undefined) {
44
+ const failure = config.transitions[effect.failureTransition];
45
+ if (!failure) {
46
+ throw new Error(`effect "${effect.name}" names undeclared failure transition "${effect.failureTransition}"`);
47
+ }
48
+ if (!failure.seats.includes(effect.seat)) {
49
+ throw new Error(`failure transition "${effect.failureTransition}" does not allow effect seat "${effect.seat}"`);
50
+ }
51
+ }
52
+ if (typeof effect.run !== "function") {
53
+ throw new Error(`effect "${effect.name}" has no run function`);
54
+ }
55
+ }
56
+ return config;
57
+ }
@@ -0,0 +1,5 @@
1
+ export { Acta, type ProposeResult, type SliceRead, type DeadlineStatus } from "./store.ts";
2
+ export { defineActa, type ActaConfig, type Guard, type Apply, type EffectDecl, type EffectContext, type DeadlineDecl, } from "./config.ts";
3
+ export { Log, writeSnapshot, readSnapshot, type LogRecord, type Snapshot } from "./log.ts";
4
+ export { validate, type Schema, type ValidationResult } from "./validate.ts";
5
+ export { startMcpServer } from "./mcp.ts";
@@ -0,0 +1,5 @@
1
+ export { Acta } from "./store.js";
2
+ export { defineActa, } from "./config.js";
3
+ export { Log, writeSnapshot, readSnapshot } from "./log.js";
4
+ export { validate } from "./validate.js";
5
+ export { startMcpServer } from "./mcp.js";
@@ -0,0 +1,22 @@
1
+ export interface LogRecord {
2
+ seq: number;
3
+ version: number;
4
+ at: string;
5
+ seat: string;
6
+ transition: string;
7
+ slice: string;
8
+ payload: unknown;
9
+ baseVersion: number;
10
+ }
11
+ export interface Snapshot {
12
+ version: number;
13
+ state: Record<string, unknown>;
14
+ }
15
+ export declare class Log {
16
+ private path;
17
+ constructor(path: string);
18
+ append(record: LogRecord): void;
19
+ readAll(): LogRecord[];
20
+ }
21
+ export declare function writeSnapshot(path: string, snapshot: Snapshot): void;
22
+ export declare function readSnapshot(path: string): Snapshot | null;
@@ -0,0 +1,26 @@
1
+ import { appendFileSync, existsSync, readFileSync, writeFileSync } from "node:fs";
2
+ export class Log {
3
+ path;
4
+ constructor(path) {
5
+ this.path = path;
6
+ }
7
+ append(record) {
8
+ appendFileSync(this.path, JSON.stringify(record) + "\n", "utf8");
9
+ }
10
+ readAll() {
11
+ if (!existsSync(this.path))
12
+ return [];
13
+ const text = readFileSync(this.path, "utf8").trim();
14
+ if (text === "")
15
+ return [];
16
+ return text.split("\n").map((line) => JSON.parse(line));
17
+ }
18
+ }
19
+ export function writeSnapshot(path, snapshot) {
20
+ writeFileSync(path, JSON.stringify(snapshot, null, 2) + "\n", "utf8");
21
+ }
22
+ export function readSnapshot(path) {
23
+ if (!existsSync(path))
24
+ return null;
25
+ return JSON.parse(readFileSync(path, "utf8"));
26
+ }
@@ -0,0 +1,3 @@
1
+ import type { ActaConfig } from "./config.ts";
2
+ import type { Acta } from "./store.ts";
3
+ export declare function startMcpServer(acta: Acta, config: ActaConfig, seat: string): void;
@@ -0,0 +1,124 @@
1
+ import { createInterface } from "node:readline";
2
+ // The MCP surface (SPEC S2), hand-rolled JSON-RPC 2.0 over stdio,
3
+ // newline-delimited, zero dependencies. Transitions are tools; slices
4
+ // are resources (acta://state/<slice>), each read stamped with the
5
+ // state version it reflects.
6
+ //
7
+ // One server process serves one seat: the seat is fixed at startup
8
+ // (--seat), and every tool call proposes its transition as that seat.
9
+ // The core refuses anything outside the seat's manifest, and a refusal
10
+ // returns as a tool error carrying the reason as data.
11
+ const SUPPORTED_VERSIONS = ["2024-11-05", "2025-03-26", "2025-06-18"];
12
+ function result(id, value) {
13
+ return JSON.stringify({ jsonrpc: "2.0", id, result: value }) + "\n";
14
+ }
15
+ function error(id, code, message) {
16
+ return JSON.stringify({ jsonrpc: "2.0", id, error: { code, message } }) + "\n";
17
+ }
18
+ export function startMcpServer(acta, config, seat) {
19
+ const rl = createInterface({ input: process.stdin, terminal: false });
20
+ rl.on("line", (line) => {
21
+ const trimmed = line.trim();
22
+ if (trimmed === "")
23
+ return;
24
+ let request;
25
+ try {
26
+ request = JSON.parse(trimmed);
27
+ }
28
+ catch {
29
+ process.stdout.write(error(null, -32700, "parse error"));
30
+ return;
31
+ }
32
+ const isNotification = request.id === undefined || request.id === null;
33
+ const id = (request.id ?? null);
34
+ const params = request.params ?? {};
35
+ switch (request.method) {
36
+ case "initialize": {
37
+ const requested = typeof params.protocolVersion === "string" ? params.protocolVersion : "";
38
+ const protocolVersion = SUPPORTED_VERSIONS.includes(requested) ? requested : "2025-03-26";
39
+ process.stdout.write(result(id, {
40
+ protocolVersion,
41
+ capabilities: { tools: {}, resources: {} },
42
+ serverInfo: { name: "acta", version: "0.0.1" },
43
+ }));
44
+ return;
45
+ }
46
+ case "ping": {
47
+ if (!isNotification)
48
+ process.stdout.write(result(id, {}));
49
+ return;
50
+ }
51
+ case "tools/list": {
52
+ const tools = Object.entries(config.transitions).map(([name, decl]) => ({
53
+ name,
54
+ description: `Acta transition on slice "${decl.slice}". Seats: ${decl.seats.join(", ")}.`,
55
+ inputSchema: decl.input,
56
+ }));
57
+ process.stdout.write(result(id, { tools }));
58
+ return;
59
+ }
60
+ case "tools/call": {
61
+ const name = params.name;
62
+ const args = (params.arguments ?? {});
63
+ void acta.propose(seat, name, args, { token: process.env.ACTA_SEAT_TOKEN }).then((outcome) => {
64
+ if (outcome.ok) {
65
+ process.stdout.write(result(id, {
66
+ content: [
67
+ { type: "text", text: JSON.stringify({ ok: true, version: outcome.version }) },
68
+ ],
69
+ isError: false,
70
+ }));
71
+ }
72
+ else {
73
+ process.stdout.write(result(id, {
74
+ content: [
75
+ { type: "text", text: JSON.stringify({ ok: false, reason: outcome.reason }) },
76
+ ],
77
+ isError: true,
78
+ }));
79
+ }
80
+ });
81
+ return;
82
+ }
83
+ case "resources/list": {
84
+ const resources = Object.keys(config.slices).map((slice) => ({
85
+ uri: `acta://state/${slice}`,
86
+ name: slice,
87
+ mimeType: "application/json",
88
+ }));
89
+ process.stdout.write(result(id, { resources }));
90
+ return;
91
+ }
92
+ case "resources/read": {
93
+ const uri = params.uri;
94
+ const prefix = "acta://state/";
95
+ if (typeof uri !== "string" || !uri.startsWith(prefix)) {
96
+ process.stdout.write(error(id, -32602, `unknown resource uri "${String(uri)}"`));
97
+ return;
98
+ }
99
+ const slice = uri.slice(prefix.length);
100
+ if (!(slice in config.slices)) {
101
+ process.stdout.write(error(id, -32602, `unknown slice "${slice}"`));
102
+ return;
103
+ }
104
+ const read = acta.read(slice);
105
+ process.stdout.write(result(id, {
106
+ contents: [
107
+ {
108
+ uri,
109
+ mimeType: "application/json",
110
+ text: JSON.stringify({ value: read.value, version: read.version }),
111
+ },
112
+ ],
113
+ }));
114
+ return;
115
+ }
116
+ default: {
117
+ if (!isNotification) {
118
+ process.stdout.write(error(id, -32601, `method not found: ${String(request.method)}`));
119
+ }
120
+ return;
121
+ }
122
+ }
123
+ });
124
+ }
@@ -0,0 +1,77 @@
1
+ import type { ActaConfig, DeadlineDecl } from "./config.ts";
2
+ import { type LogRecord, type Snapshot } from "./log.ts";
3
+ export type ProposeResult = {
4
+ ok: true;
5
+ version: number;
6
+ record: LogRecord;
7
+ } | {
8
+ ok: false;
9
+ reason: string;
10
+ };
11
+ export interface SliceRead {
12
+ value: unknown;
13
+ version: number;
14
+ }
15
+ interface ActaOptions {
16
+ logPath?: string;
17
+ effectsPath?: string;
18
+ deadlinesPath?: string;
19
+ now?: () => Date;
20
+ }
21
+ export type DeadlineStatus = "open" | "satisfied" | "expired";
22
+ interface EffectRunRecord {
23
+ status: "done" | "failed";
24
+ at: string;
25
+ error?: string;
26
+ }
27
+ export declare class Acta {
28
+ private config;
29
+ private state;
30
+ private version;
31
+ private seq;
32
+ private log;
33
+ private now;
34
+ private queue;
35
+ private fxQueue;
36
+ private fxBook;
37
+ private fxPath;
38
+ private fxCascade;
39
+ private fxPending;
40
+ private dlBook;
41
+ private dlPath;
42
+ constructor(config: ActaConfig, options?: ActaOptions);
43
+ get currentVersion(): number;
44
+ read(slice: string): SliceRead;
45
+ propose(seat: string, transition: string, payload: unknown, opts?: {
46
+ baseVersion?: number;
47
+ token?: string;
48
+ }): Promise<ProposeResult>;
49
+ settle(): Promise<void>;
50
+ private commit;
51
+ registerDeadline(decl: DeadlineDecl): {
52
+ ok: boolean;
53
+ reason?: string;
54
+ };
55
+ deadlineStatus(name: string): DeadlineStatus | undefined;
56
+ checkDeadlines(): Promise<{
57
+ name: string;
58
+ result: ProposeResult;
59
+ }[]>;
60
+ private markSatisfied;
61
+ private persistDeadlines;
62
+ private dueEffects;
63
+ private scheduleEffects;
64
+ private runEffect;
65
+ private runEffectBody;
66
+ private recordEffect;
67
+ effectOutcome(effectName: string, version: number): EffectRunRecord | undefined;
68
+ snapshot(): Snapshot;
69
+ static replay(config: ActaConfig, records: LogRecord[], opts?: {
70
+ toVersion?: number;
71
+ fromSnapshot?: Snapshot;
72
+ }): {
73
+ state: Record<string, unknown>;
74
+ version: number;
75
+ };
76
+ }
77
+ export {};