cpa-agents 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/LICENSE +21 -0
- package/README.md +146 -0
- package/dist/adapters/openclaw.d.ts +49 -0
- package/dist/adapters/openclaw.d.ts.map +1 -0
- package/dist/adapters/openclaw.js +186 -0
- package/dist/adapters/openclaw.js.map +1 -0
- package/dist/adapters/pi-harness.d.ts +38 -0
- package/dist/adapters/pi-harness.d.ts.map +1 -0
- package/dist/adapters/pi-harness.js +77 -0
- package/dist/adapters/pi-harness.js.map +1 -0
- package/dist/agent.d.ts +72 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +148 -0
- package/dist/agent.js.map +1 -0
- package/dist/channel.d.ts +57 -0
- package/dist/channel.d.ts.map +1 -0
- package/dist/channel.js +113 -0
- package/dist/channel.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -0
- package/dist/process.d.ts +143 -0
- package/dist/process.d.ts.map +1 -0
- package/dist/process.js +273 -0
- package/dist/process.js.map +1 -0
- package/dist/scheduler.d.ts +50 -0
- package/dist/scheduler.d.ts.map +1 -0
- package/dist/scheduler.js +109 -0
- package/dist/scheduler.js.map +1 -0
- package/package.json +51 -0
package/dist/agent.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentProcess: bridges the process algebra to actual LLM agent calls.
|
|
3
|
+
*
|
|
4
|
+
* An AgentProcess wraps an LLM invocation (tool use loop, ReAct step,
|
|
5
|
+
* or sub-agent spawn) as a Process<T> that can be composed with
|
|
6
|
+
* all the algebra combinators.
|
|
7
|
+
*/
|
|
8
|
+
import { Channel } from "./channel.js";
|
|
9
|
+
import { branchFix, par } from "./process.js";
|
|
10
|
+
/**
|
|
11
|
+
* Lift an agent call into a Process.
|
|
12
|
+
*/
|
|
13
|
+
export function agentProcess(agent, input) {
|
|
14
|
+
return async (ctx) => {
|
|
15
|
+
ctx.trace.emit({
|
|
16
|
+
type: "spawn",
|
|
17
|
+
runId: ctx.runId,
|
|
18
|
+
parentId: ctx.parentId,
|
|
19
|
+
name: agent.name,
|
|
20
|
+
ts: Date.now(),
|
|
21
|
+
});
|
|
22
|
+
const result = await agent.invoke(input, ctx.signal);
|
|
23
|
+
ctx.trace.emit({
|
|
24
|
+
type: "done",
|
|
25
|
+
runId: ctx.runId,
|
|
26
|
+
ts: Date.now(),
|
|
27
|
+
});
|
|
28
|
+
return result;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
// ─── Common agent workflow patterns ─────────────────────────────
|
|
32
|
+
/**
|
|
33
|
+
* Code-then-fix: the tree pattern from Pi Harness.
|
|
34
|
+
*
|
|
35
|
+
* 1. Agent writes code
|
|
36
|
+
* 2. Lint/test/typecheck runs
|
|
37
|
+
* 3. If errors found → branch to fix agent → re-check → continue
|
|
38
|
+
* 4. If clean → proceed to next task
|
|
39
|
+
*/
|
|
40
|
+
export function codeThenFix(opts) {
|
|
41
|
+
return branchFix({
|
|
42
|
+
name: `code_fix_${opts.task.slice(0, 20)}`,
|
|
43
|
+
maxFixes: opts.maxFixes,
|
|
44
|
+
main: (requestFix) => async (ctx) => {
|
|
45
|
+
// Step 1: generate code
|
|
46
|
+
let code = await opts.coder.invoke(opts.task, ctx.signal);
|
|
47
|
+
// Step 2: check it
|
|
48
|
+
let checkResult = await opts.checker.invoke(code, ctx.signal);
|
|
49
|
+
// Step 3: loop fix if needed
|
|
50
|
+
while (!checkResult.pass) {
|
|
51
|
+
await requestFix(checkResult.errors.join("; "));
|
|
52
|
+
code = await opts.fixer.invoke({ code, errors: checkResult.errors }, ctx.signal);
|
|
53
|
+
checkResult = await opts.checker.invoke(code, ctx.signal);
|
|
54
|
+
}
|
|
55
|
+
return code;
|
|
56
|
+
},
|
|
57
|
+
fix: (reason) => async (ctx) => {
|
|
58
|
+
ctx.trace.emit({
|
|
59
|
+
type: "fix_start",
|
|
60
|
+
runId: ctx.runId,
|
|
61
|
+
reason,
|
|
62
|
+
ts: Date.now(),
|
|
63
|
+
});
|
|
64
|
+
// The actual fix work happens in the main loop above.
|
|
65
|
+
// This hook is for side effects: logging, notifying, etc.
|
|
66
|
+
ctx.trace.emit({
|
|
67
|
+
type: "fix_end",
|
|
68
|
+
runId: ctx.runId,
|
|
69
|
+
success: true,
|
|
70
|
+
ts: Date.now(),
|
|
71
|
+
});
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Fan-out pattern: send the same task to N agents in parallel,
|
|
77
|
+
* collect results, then merge.
|
|
78
|
+
*
|
|
79
|
+
* Useful for: parallel research, multi-model consensus, ensemble.
|
|
80
|
+
*/
|
|
81
|
+
export function fanOut(opts) {
|
|
82
|
+
const processes = opts.agents.map((agent) => agentProcess(agent, opts.input));
|
|
83
|
+
return async (ctx) => {
|
|
84
|
+
const results = await par(...processes)(ctx);
|
|
85
|
+
return opts.merge(results);
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
export function pipeline(a, inputA) {
|
|
89
|
+
return new PipelineBuilder(a, inputA);
|
|
90
|
+
}
|
|
91
|
+
export class PipelineBuilder {
|
|
92
|
+
steps = [];
|
|
93
|
+
firstAgent;
|
|
94
|
+
firstInput;
|
|
95
|
+
constructor(agent, input) {
|
|
96
|
+
this.firstAgent = agent;
|
|
97
|
+
this.firstInput = input;
|
|
98
|
+
}
|
|
99
|
+
then(agent, transform) {
|
|
100
|
+
this.steps.push({ agent, inputFn: transform });
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
build() {
|
|
104
|
+
const { firstAgent, firstInput, steps } = this;
|
|
105
|
+
return async (ctx) => {
|
|
106
|
+
let result = await firstAgent.invoke(firstInput, ctx.signal);
|
|
107
|
+
for (const step of steps) {
|
|
108
|
+
const input = step.inputFn ? step.inputFn(result) : result;
|
|
109
|
+
result = await step.agent.invoke(input, ctx.signal);
|
|
110
|
+
}
|
|
111
|
+
return result;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Handoff: one agent works, then hands off to another via a channel.
|
|
117
|
+
* Models the Pi Harness pattern of sub-agent spawning.
|
|
118
|
+
*/
|
|
119
|
+
export function handoff(opts) {
|
|
120
|
+
return async (ctx) => {
|
|
121
|
+
await Channel.restrict("handoff", async (ch) => {
|
|
122
|
+
await par(
|
|
123
|
+
// Producer: runs `from`, sends result on channel
|
|
124
|
+
async (pCtx) => {
|
|
125
|
+
const result = await opts.from.invoke(undefined, pCtx.signal);
|
|
126
|
+
pCtx.trace.emit({
|
|
127
|
+
type: "send",
|
|
128
|
+
runId: pCtx.runId,
|
|
129
|
+
channel: ch.name,
|
|
130
|
+
ts: Date.now(),
|
|
131
|
+
});
|
|
132
|
+
await ch.send(result);
|
|
133
|
+
},
|
|
134
|
+
// Consumer: receives from channel, runs `to`
|
|
135
|
+
async (cCtx) => {
|
|
136
|
+
const value = await ch.receive();
|
|
137
|
+
cCtx.trace.emit({
|
|
138
|
+
type: "receive",
|
|
139
|
+
runId: cCtx.runId,
|
|
140
|
+
channel: ch.name,
|
|
141
|
+
ts: Date.now(),
|
|
142
|
+
});
|
|
143
|
+
await opts.to.invoke(value, cCtx.signal);
|
|
144
|
+
})(ctx);
|
|
145
|
+
});
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAqC,SAAS,EAAE,GAAG,EAAO,MAAM,cAAc,CAAC;AAWtF;;GAEG;AACH,MAAM,UAAU,YAAY,CAC1B,KAAiC,EACjC,KAAa;IAEb,OAAO,KAAK,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M,OAAO,CAAC,QAAQ,CAAiB,SAAS,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE;YAC7D,MAAM,GAAG;YACP,iDAAiD;YACjD,KAAK,EAAE,IAAI,EAAE,EAAE;gBACb,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;gBAC9D,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;oBACd,IAAI,EAAE,MAAM;oBACZ,KAAK,EAAE,IAAI,CAAC,KAAK;oBACjB,OAAO,EAAE,EAAE,CAAC,IAAI;oBAChB,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE;iBACf,CAAC,CAAC;gBACH,MAAM,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YACxB,CAAC;YACD,6CAA6C;YAC7C,KAAK,EAAE,IAAI,EAAE,EAAE;gBACb,MAAM,KAAK,GAAG,MAAM,EAAE,CAAC,OAAO,EAAE,CAAC;gBACjC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;oBACd,IAAI,EAAE,SAAS;oBACf,KAAK,EAAE,IAAI,CAAC,KAAK;oBACjB,OAAO,EAAE,EAAE,CAAC,IAAI;oBAChB,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE;iBACf,CAAC,CAAC;gBACH,MAAM,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;YAC3C,CAAC,CACF,CAAC,GAAG,CAAC,CAAC;QACT,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cpa-agents — Concurrent Process Algebra for AI Agents
|
|
3
|
+
*
|
|
4
|
+
* Channel<T>: typed communication channel with π-calculus semantics.
|
|
5
|
+
* Supports synchronous rendezvous (sender blocks until receiver is ready).
|
|
6
|
+
*/
|
|
7
|
+
export type ChannelId = string & {
|
|
8
|
+
__brand: "ChannelId";
|
|
9
|
+
};
|
|
10
|
+
export declare function freshId(prefix?: string): ChannelId;
|
|
11
|
+
/**
|
|
12
|
+
* A typed, named channel. Corresponds to π-calculus names.
|
|
13
|
+
* Channels can be sent over other channels (mobility).
|
|
14
|
+
*/
|
|
15
|
+
export declare class Channel<T = unknown> {
|
|
16
|
+
readonly id: ChannelId;
|
|
17
|
+
readonly name: string;
|
|
18
|
+
private sendQueue;
|
|
19
|
+
private recvQueue;
|
|
20
|
+
private _closed;
|
|
21
|
+
constructor(name?: string);
|
|
22
|
+
get closed(): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Send a value. Blocks until a receiver picks it up (rendezvous).
|
|
25
|
+
* In π-calculus: ā⟨v⟩.P — output v on channel a, then continue as P.
|
|
26
|
+
*/
|
|
27
|
+
send(value: T): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Receive a value. Blocks until a sender provides one.
|
|
30
|
+
* In π-calculus: a(x).P — input x from channel a, then continue as P.
|
|
31
|
+
*/
|
|
32
|
+
receive(): Promise<T>;
|
|
33
|
+
/**
|
|
34
|
+
* Non-blocking try-receive. Returns undefined if nothing is waiting.
|
|
35
|
+
*/
|
|
36
|
+
tryReceive(): T | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Close the channel. Rejects all pending waiters.
|
|
39
|
+
*/
|
|
40
|
+
close(): void;
|
|
41
|
+
/**
|
|
42
|
+
* π-calculus restriction: ν(x). Creates a fresh channel
|
|
43
|
+
* scoped to the provided function. The channel is closed
|
|
44
|
+
* when the scope exits.
|
|
45
|
+
*/
|
|
46
|
+
static restrict<T, R>(name: string, body: (ch: Channel<T>) => Promise<R>): Promise<R>;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Select: π-calculus external choice (P + Q).
|
|
50
|
+
* Waits on multiple channels, returns the first that fires.
|
|
51
|
+
*/
|
|
52
|
+
export interface SelectCase<T> {
|
|
53
|
+
channel: Channel<T>;
|
|
54
|
+
handler: (value: T) => Promise<void> | void;
|
|
55
|
+
}
|
|
56
|
+
export declare function select(...cases: SelectCase<any>[]): Promise<void>;
|
|
57
|
+
//# sourceMappingURL=channel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG;IAAE,OAAO,EAAE,WAAW,CAAA;CAAE,CAAC;AAG1D,wBAAgB,OAAO,CAAC,MAAM,SAAO,GAAG,SAAS,CAEhD;AAOD;;;GAGG;AACH,qBAAa,OAAO,CAAC,CAAC,GAAG,OAAO;IAC9B,QAAQ,CAAC,EAAE,EAAE,SAAS,CAAC;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,SAAS,CAA+C;IAChE,OAAO,CAAC,SAAS,CAAwB;IACzC,OAAO,CAAC,OAAO,CAAS;gBAEZ,IAAI,CAAC,EAAE,MAAM;IAKzB,IAAI,MAAM,IAAI,OAAO,CAEpB;IAED;;;OAGG;IACH,IAAI,CAAC,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAkB7B;;;OAGG;IACH,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC;IAkBrB;;OAEG;IACH,UAAU,IAAI,CAAC,GAAG,SAAS;IAS3B;;OAEG;IACH,KAAK,IAAI,IAAI;IASb;;;;OAIG;WACU,QAAQ,CAAC,CAAC,EAAE,CAAC,EACxB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GACnC,OAAO,CAAC,CAAC,CAAC;CAQd;AAED;;;GAGG;AACH,MAAM,WAAW,UAAU,CAAC,CAAC;IAC3B,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IACpB,OAAO,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;CAC7C;AAED,wBAAsB,MAAM,CAAC,GAAG,KAAK,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAUvE"}
|
package/dist/channel.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cpa-agents — Concurrent Process Algebra for AI Agents
|
|
3
|
+
*
|
|
4
|
+
* Channel<T>: typed communication channel with π-calculus semantics.
|
|
5
|
+
* Supports synchronous rendezvous (sender blocks until receiver is ready).
|
|
6
|
+
*/
|
|
7
|
+
let _nextId = 0;
|
|
8
|
+
export function freshId(prefix = "ch") {
|
|
9
|
+
return `${prefix}_${_nextId++}`;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A typed, named channel. Corresponds to π-calculus names.
|
|
13
|
+
* Channels can be sent over other channels (mobility).
|
|
14
|
+
*/
|
|
15
|
+
export class Channel {
|
|
16
|
+
id;
|
|
17
|
+
name;
|
|
18
|
+
sendQueue = [];
|
|
19
|
+
recvQueue = [];
|
|
20
|
+
_closed = false;
|
|
21
|
+
constructor(name) {
|
|
22
|
+
this.id = freshId(name ?? "ch");
|
|
23
|
+
this.name = name ?? this.id;
|
|
24
|
+
}
|
|
25
|
+
get closed() {
|
|
26
|
+
return this._closed;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Send a value. Blocks until a receiver picks it up (rendezvous).
|
|
30
|
+
* In π-calculus: ā⟨v⟩.P — output v on channel a, then continue as P.
|
|
31
|
+
*/
|
|
32
|
+
send(value) {
|
|
33
|
+
if (this._closed) {
|
|
34
|
+
return Promise.reject(new Error(`Channel ${this.name} is closed`));
|
|
35
|
+
}
|
|
36
|
+
// If a receiver is already waiting, hand off directly
|
|
37
|
+
const receiver = this.recvQueue.shift();
|
|
38
|
+
if (receiver) {
|
|
39
|
+
receiver.resolve(value);
|
|
40
|
+
return Promise.resolve();
|
|
41
|
+
}
|
|
42
|
+
// Otherwise, block until a receiver appears
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
this.sendQueue.push({ value, done: { resolve, reject } });
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Receive a value. Blocks until a sender provides one.
|
|
49
|
+
* In π-calculus: a(x).P — input x from channel a, then continue as P.
|
|
50
|
+
*/
|
|
51
|
+
receive() {
|
|
52
|
+
if (this._closed && this.sendQueue.length === 0) {
|
|
53
|
+
return Promise.reject(new Error(`Channel ${this.name} is closed`));
|
|
54
|
+
}
|
|
55
|
+
// If a sender is already waiting, hand off directly
|
|
56
|
+
const sender = this.sendQueue.shift();
|
|
57
|
+
if (sender) {
|
|
58
|
+
sender.done.resolve();
|
|
59
|
+
return Promise.resolve(sender.value);
|
|
60
|
+
}
|
|
61
|
+
// Otherwise, block until a sender appears
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
this.recvQueue.push({ resolve, reject });
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Non-blocking try-receive. Returns undefined if nothing is waiting.
|
|
68
|
+
*/
|
|
69
|
+
tryReceive() {
|
|
70
|
+
const sender = this.sendQueue.shift();
|
|
71
|
+
if (sender) {
|
|
72
|
+
sender.done.resolve();
|
|
73
|
+
return sender.value;
|
|
74
|
+
}
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Close the channel. Rejects all pending waiters.
|
|
79
|
+
*/
|
|
80
|
+
close() {
|
|
81
|
+
this._closed = true;
|
|
82
|
+
const err = new Error(`Channel ${this.name} closed`);
|
|
83
|
+
for (const w of this.sendQueue)
|
|
84
|
+
w.done.reject(err);
|
|
85
|
+
for (const w of this.recvQueue)
|
|
86
|
+
w.reject(err);
|
|
87
|
+
this.sendQueue = [];
|
|
88
|
+
this.recvQueue = [];
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* π-calculus restriction: ν(x). Creates a fresh channel
|
|
92
|
+
* scoped to the provided function. The channel is closed
|
|
93
|
+
* when the scope exits.
|
|
94
|
+
*/
|
|
95
|
+
static async restrict(name, body) {
|
|
96
|
+
const ch = new Channel(name);
|
|
97
|
+
try {
|
|
98
|
+
return await body(ch);
|
|
99
|
+
}
|
|
100
|
+
finally {
|
|
101
|
+
ch.close();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
export async function select(...cases) {
|
|
106
|
+
// Race all receives
|
|
107
|
+
const result = await Promise.race(cases.map(async (c, i) => {
|
|
108
|
+
const value = await c.channel.receive();
|
|
109
|
+
return { index: i, value };
|
|
110
|
+
}));
|
|
111
|
+
await cases[result.index].handler(result.value);
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=channel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"channel.js","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,IAAI,OAAO,GAAG,CAAC,CAAC;AAChB,MAAM,UAAU,OAAO,CAAC,MAAM,GAAG,IAAI;IACnC,OAAO,GAAG,MAAM,IAAI,OAAO,EAAE,EAAe,CAAC;AAC/C,CAAC;AAOD;;;GAGG;AACH,MAAM,OAAO,OAAO;IACT,EAAE,CAAY;IACd,IAAI,CAAS;IACd,SAAS,GAA4C,EAAE,CAAC;IACxD,SAAS,GAAqB,EAAE,CAAC;IACjC,OAAO,GAAG,KAAK,CAAC;IAExB,YAAY,IAAa;QACvB,IAAI,CAAC,EAAE,GAAG,OAAO,CAAC,IAAI,IAAI,IAAI,CAAC,CAAC;QAChC,IAAI,CAAC,IAAI,GAAG,IAAI,IAAI,IAAI,CAAC,EAAE,CAAC;IAC9B,CAAC;IAED,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED;;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AAC,EAAE,EAAE;QACvB,MAAM,KAAK,GAAG,MAAM,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;QACxC,OAAO,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC;IAC7B,CAAC,CAAC,CACH,CAAC;IAEF,MAAM,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAClD,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cpa-agents — Concurrent Process Algebra for AI Agents
|
|
3
|
+
*
|
|
4
|
+
* A TypeScript library that applies π-calculus process algebra
|
|
5
|
+
* to AI agent orchestration. Designed for Pi Harness and OpenClaw,
|
|
6
|
+
* but usable standalone.
|
|
7
|
+
*
|
|
8
|
+
* Core concepts:
|
|
9
|
+
* - Channel<T>: typed communication (π-calculus names)
|
|
10
|
+
* - Process<T>: unit of computation
|
|
11
|
+
* - par(P, Q): parallel composition (P | Q)
|
|
12
|
+
* - seq(P, Q): sequential composition (P ; Q)
|
|
13
|
+
* - choice([...branches]): external choice (P + Q)
|
|
14
|
+
* - branchFix({ main, fix }): branch-to-fix-then-continue
|
|
15
|
+
* - restrict(name, body): scoped channel creation (ν(x).P)
|
|
16
|
+
* - replicate(trigger, handler): server pattern (!P)
|
|
17
|
+
* - supervisor({ process, maxRetries }): error recovery
|
|
18
|
+
*
|
|
19
|
+
* Agent patterns:
|
|
20
|
+
* - agentProcess(agent, input): lift an LLM call into a Process
|
|
21
|
+
* - codeThenFix({ coder, checker, fixer }): code → check → fix loop
|
|
22
|
+
* - fanOut({ agents, input, merge }): parallel multi-agent
|
|
23
|
+
* - pipeline(a, input).then(b).build(): sequential chain
|
|
24
|
+
* - handoff({ from, to }): channel-mediated agent transfer
|
|
25
|
+
*/
|
|
26
|
+
export { Channel, freshId, select, type ChannelId, type SelectCase } from "./channel.js";
|
|
27
|
+
export { type Process, type ProcessContext, type TraceEvent, type SessionNode, TraceCollector, par, seq, choice, branchFix, restrict, replicate, supervisor, } from "./process.js";
|
|
28
|
+
export { type AgentCall, type CheckResult, agentProcess, codeThenFix, fanOut, pipeline, PipelineBuilder, handoff, } from "./agent.js";
|
|
29
|
+
export { Scheduler, type SchedulerOpts, type SchedulerResult } from "./scheduler.js";
|
|
30
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAGH,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,SAAS,EAAE,KAAK,UAAU,EAAE,MAAM,cAAc,CAAC;AAGzF,OAAO,EACL,KAAK,OAAO,EACZ,KAAK,cAAc,EACnB,KAAK,UAAU,EACf,KAAK,WAAW,EAChB,cAAc,EACd,GAAG,EACH,GAAG,EACH,MAAM,EACN,SAAS,EACT,QAAQ,EACR,SAAS,EACT,UAAU,GACX,MAAM,cAAc,CAAC;AAGtB,OAAO,EACL,KAAK,SAAS,EACd,KAAK,WAAW,EAChB,YAAY,EACZ,WAAW,EACX,MAAM,EACN,QAAQ,EACR,eAAe,EACf,OAAO,GACR,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,SAAS,EAAE,KAAK,aAAa,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cpa-agents — Concurrent Process Algebra for AI Agents
|
|
3
|
+
*
|
|
4
|
+
* A TypeScript library that applies π-calculus process algebra
|
|
5
|
+
* to AI agent orchestration. Designed for Pi Harness and OpenClaw,
|
|
6
|
+
* but usable standalone.
|
|
7
|
+
*
|
|
8
|
+
* Core concepts:
|
|
9
|
+
* - Channel<T>: typed communication (π-calculus names)
|
|
10
|
+
* - Process<T>: unit of computation
|
|
11
|
+
* - par(P, Q): parallel composition (P | Q)
|
|
12
|
+
* - seq(P, Q): sequential composition (P ; Q)
|
|
13
|
+
* - choice([...branches]): external choice (P + Q)
|
|
14
|
+
* - branchFix({ main, fix }): branch-to-fix-then-continue
|
|
15
|
+
* - restrict(name, body): scoped channel creation (ν(x).P)
|
|
16
|
+
* - replicate(trigger, handler): server pattern (!P)
|
|
17
|
+
* - supervisor({ process, maxRetries }): error recovery
|
|
18
|
+
*
|
|
19
|
+
* Agent patterns:
|
|
20
|
+
* - agentProcess(agent, input): lift an LLM call into a Process
|
|
21
|
+
* - codeThenFix({ coder, checker, fixer }): code → check → fix loop
|
|
22
|
+
* - fanOut({ agents, input, merge }): parallel multi-agent
|
|
23
|
+
* - pipeline(a, input).then(b).build(): sequential chain
|
|
24
|
+
* - handoff({ from, to }): channel-mediated agent transfer
|
|
25
|
+
*/
|
|
26
|
+
// Core primitives
|
|
27
|
+
export { Channel, freshId, select } from "./channel.js";
|
|
28
|
+
// Process algebra
|
|
29
|
+
export { TraceCollector, par, seq, choice, branchFix, restrict, replicate, supervisor, } from "./process.js";
|
|
30
|
+
// Agent wrappers
|
|
31
|
+
export { agentProcess, codeThenFix, fanOut, pipeline, PipelineBuilder, handoff, } from "./agent.js";
|
|
32
|
+
// Scheduler
|
|
33
|
+
export { Scheduler } from "./scheduler.js";
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,kBAAkB;AAClB,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAmC,MAAM,cAAc,CAAC;AAEzF,kBAAkB;AAClB,OAAO,EAKL,cAAc,EACd,GAAG,EACH,GAAG,EACH,MAAM,EACN,SAAS,EACT,QAAQ,EACR,SAAS,EACT,UAAU,GACX,MAAM,cAAc,CAAC;AAEtB,iBAAiB;AACjB,OAAO,EAGL,YAAY,EACZ,WAAW,EACX,MAAM,EACN,QAAQ,EACR,eAAe,EACf,OAAO,GACR,MAAM,YAAY,CAAC;AAEpB,YAAY;AACZ,OAAO,EAAE,SAAS,EAA4C,MAAM,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Process: the core unit of computation in the algebra.
|
|
3
|
+
*
|
|
4
|
+
* A Process is a function that takes a context (channels, signals)
|
|
5
|
+
* and returns a result. Processes compose via parallel, sequential,
|
|
6
|
+
* choice, and branch-fix-continue combinators.
|
|
7
|
+
*/
|
|
8
|
+
import { Channel } from "./channel.js";
|
|
9
|
+
export interface ProcessContext {
|
|
10
|
+
/** Unique run ID for this process instance */
|
|
11
|
+
readonly runId: string;
|
|
12
|
+
/** Parent process ID, if spawned as a child */
|
|
13
|
+
readonly parentId?: string;
|
|
14
|
+
/** Abort signal — checked by cooperative processes */
|
|
15
|
+
readonly signal: AbortSignal;
|
|
16
|
+
/** Trace collector for session tree */
|
|
17
|
+
readonly trace: TraceCollector;
|
|
18
|
+
/** Named channels available in scope */
|
|
19
|
+
readonly channels: Map<string, Channel<any>>;
|
|
20
|
+
}
|
|
21
|
+
export type Process<T = void> = (ctx: ProcessContext) => Promise<T>;
|
|
22
|
+
export type TraceEvent = {
|
|
23
|
+
type: "spawn";
|
|
24
|
+
runId: string;
|
|
25
|
+
parentId?: string;
|
|
26
|
+
name: string;
|
|
27
|
+
ts: number;
|
|
28
|
+
} | {
|
|
29
|
+
type: "send";
|
|
30
|
+
runId: string;
|
|
31
|
+
channel: string;
|
|
32
|
+
ts: number;
|
|
33
|
+
} | {
|
|
34
|
+
type: "receive";
|
|
35
|
+
runId: string;
|
|
36
|
+
channel: string;
|
|
37
|
+
ts: number;
|
|
38
|
+
} | {
|
|
39
|
+
type: "branch";
|
|
40
|
+
runId: string;
|
|
41
|
+
chosen: string;
|
|
42
|
+
alternatives: string[];
|
|
43
|
+
ts: number;
|
|
44
|
+
} | {
|
|
45
|
+
type: "fix_start";
|
|
46
|
+
runId: string;
|
|
47
|
+
reason: string;
|
|
48
|
+
ts: number;
|
|
49
|
+
} | {
|
|
50
|
+
type: "fix_end";
|
|
51
|
+
runId: string;
|
|
52
|
+
success: boolean;
|
|
53
|
+
ts: number;
|
|
54
|
+
} | {
|
|
55
|
+
type: "done";
|
|
56
|
+
runId: string;
|
|
57
|
+
ts: number;
|
|
58
|
+
} | {
|
|
59
|
+
type: "error";
|
|
60
|
+
runId: string;
|
|
61
|
+
error: string;
|
|
62
|
+
ts: number;
|
|
63
|
+
};
|
|
64
|
+
export declare class TraceCollector {
|
|
65
|
+
readonly events: TraceEvent[];
|
|
66
|
+
emit(event: TraceEvent): void;
|
|
67
|
+
/** Get a serialisable session tree from the flat event log */
|
|
68
|
+
toTree(): SessionNode[];
|
|
69
|
+
}
|
|
70
|
+
export interface SessionNode {
|
|
71
|
+
runId: string;
|
|
72
|
+
name: string;
|
|
73
|
+
parentId?: string;
|
|
74
|
+
events: TraceEvent[];
|
|
75
|
+
children: SessionNode[];
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Parallel composition: P | Q
|
|
79
|
+
* Runs all processes concurrently and waits for all to complete.
|
|
80
|
+
*/
|
|
81
|
+
export declare function par<T extends any[]>(...processes: {
|
|
82
|
+
[K in keyof T]: Process<T[K]>;
|
|
83
|
+
}): Process<T>;
|
|
84
|
+
/**
|
|
85
|
+
* Sequential composition: P ; Q
|
|
86
|
+
* Runs processes one after another, threading context.
|
|
87
|
+
*/
|
|
88
|
+
export declare function seq<T>(...processes: Process<any>[]): Process<T>;
|
|
89
|
+
/**
|
|
90
|
+
* Choice: P + Q (external choice)
|
|
91
|
+
*
|
|
92
|
+
* Waits for a message on any of the guard channels.
|
|
93
|
+
* The first channel that fires determines which branch runs.
|
|
94
|
+
* All other branches are discarded (committed choice).
|
|
95
|
+
*/
|
|
96
|
+
export declare function choice<T>(branches: Array<{
|
|
97
|
+
name: string;
|
|
98
|
+
guard: Channel<any>;
|
|
99
|
+
process: Process<T>;
|
|
100
|
+
}>): Process<T>;
|
|
101
|
+
/**
|
|
102
|
+
* Branch-Fix-Continue: the tree pattern you described.
|
|
103
|
+
*
|
|
104
|
+
* 1. Run the main process.
|
|
105
|
+
* 2. If it signals a fix is needed (via the fix channel), pause main,
|
|
106
|
+
* run the fix process, then resume main from where it left off.
|
|
107
|
+
* 3. If the main completes without needing a fix, continue normally.
|
|
108
|
+
*
|
|
109
|
+
* This is the key pattern for AI agents: you're coding along,
|
|
110
|
+
* discover a lint error, branch to fix it, then rejoin the main flow.
|
|
111
|
+
*/
|
|
112
|
+
export declare function branchFix<T>(opts: {
|
|
113
|
+
name: string;
|
|
114
|
+
/** The main process. Receives a `requestFix` function it can call. */
|
|
115
|
+
main: (requestFix: (reason: string) => Promise<void>) => Process<T>;
|
|
116
|
+
/** Given the reason, produce a fix process. */
|
|
117
|
+
fix: (reason: string) => Process<void>;
|
|
118
|
+
/** Max number of fix cycles before giving up */
|
|
119
|
+
maxFixes?: number;
|
|
120
|
+
}): Process<T>;
|
|
121
|
+
/**
|
|
122
|
+
* Restriction: ν(name). Creates a fresh scoped channel.
|
|
123
|
+
* The channel is only visible to processes inside the scope.
|
|
124
|
+
*/
|
|
125
|
+
export declare function restrict<T, R>(name: string, body: (ch: Channel<T>) => Process<R>): Process<R>;
|
|
126
|
+
/**
|
|
127
|
+
* Replication: !P — spawn a new copy of P each time the trigger fires.
|
|
128
|
+
* Useful for server-like patterns where each incoming request
|
|
129
|
+
* gets its own agent process.
|
|
130
|
+
*/
|
|
131
|
+
export declare function replicate<T>(trigger: Channel<T>, handler: (value: T) => Process<void>): Process<void>;
|
|
132
|
+
/**
|
|
133
|
+
* Supervisor: wraps a process with retry + fallback semantics.
|
|
134
|
+
* On failure, can either restart the process or run a recovery process.
|
|
135
|
+
*/
|
|
136
|
+
export declare function supervisor<T>(opts: {
|
|
137
|
+
name: string;
|
|
138
|
+
process: Process<T>;
|
|
139
|
+
maxRetries?: number;
|
|
140
|
+
onError?: (err: Error, attempt: number) => Process<void>;
|
|
141
|
+
fallback?: Process<T>;
|
|
142
|
+
}): Process<T>;
|
|
143
|
+
//# sourceMappingURL=process.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process.d.ts","sourceRoot":"","sources":["../src/process.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,OAAO,EAA+C,MAAM,cAAc,CAAC;AAIpF,MAAM,WAAW,cAAc;IAC7B,8CAA8C;IAC9C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,+CAA+C;IAC/C,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,sDAAsD;IACtD,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,uCAAuC;IACvC,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC;IAC/B,wCAAwC;IACxC,QAAQ,CAAC,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;CAC9C;AAED,MAAM,MAAM,OAAO,CAAC,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,EAAE,cAAc,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;AAIpE,MAAM,MAAM,UAAU,GAClB;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GAC7E;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,GAC5D;IAAE,IAAI,EAAE,SAAS,CAAC;I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