@narumitw/pi-subagents 2.1.3 → 3.0.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 +181 -1250
- package/dist/child-communication-bridge.ts +136 -0
- package/dist/child-communication-bridge.ts.map +7 -0
- package/dist/chunks/chunk-UKTWUQRM.js +667 -0
- package/dist/chunks/chunk-UKTWUQRM.js.map +7 -0
- package/dist/index.ts +1448 -1598
- package/dist/index.ts.map +4 -4
- package/docs/design-principles.md +31 -0
- package/docs/ipc-transport-idea.md +103 -0
- package/docs/tools.md +109 -0
- package/package.json +11 -19
- package/src/broker-credentials.ts +71 -0
- package/src/child-communication-bridge.ts +153 -0
- package/src/child-communication-tools.ts +147 -0
- package/src/completion-renderer.ts +60 -0
- package/src/index.ts +1 -1
- package/src/message-broker.ts +628 -0
- package/src/model-output.ts +42 -0
- package/src/process.ts +688 -0
- package/src/runtime.ts +538 -0
- package/src/subagents.ts +33 -34
- package/src/tools.ts +416 -0
- package/src/types.ts +85 -0
- package/src/widget.ts +130 -0
- package/dist/chunks/auto-transport-FUUKFDIG.ts +0 -109
- package/dist/chunks/auto-transport-FUUKFDIG.ts.map +0 -7
- package/dist/chunks/capability-grant-PR72SWWS.ts +0 -19
- package/dist/chunks/capability-grant-PR72SWWS.ts.map +0 -7
- package/dist/chunks/child-peer-bridge.ts +0 -108
- package/dist/chunks/child-peer-bridge.ts.map +0 -7
- package/dist/chunks/chunk-2LMJU25E.ts +0 -1641
- package/dist/chunks/chunk-2LMJU25E.ts.map +0 -7
- package/dist/chunks/chunk-47SK2QYC.ts +0 -56
- package/dist/chunks/chunk-47SK2QYC.ts.map +0 -7
- package/dist/chunks/chunk-4AQSF7AS.ts +0 -29
- package/dist/chunks/chunk-4AQSF7AS.ts.map +0 -7
- package/dist/chunks/chunk-5DGRMKNV.ts +0 -126
- package/dist/chunks/chunk-5DGRMKNV.ts.map +0 -7
- package/dist/chunks/chunk-6H6TBBED.ts +0 -108
- package/dist/chunks/chunk-6H6TBBED.ts.map +0 -7
- package/dist/chunks/chunk-6KJ34M6S.ts +0 -159
- package/dist/chunks/chunk-6KJ34M6S.ts.map +0 -7
- package/dist/chunks/chunk-6NSJVPXX.ts +0 -480
- package/dist/chunks/chunk-6NSJVPXX.ts.map +0 -7
- package/dist/chunks/chunk-7AAJEUSL.ts +0 -238
- package/dist/chunks/chunk-7AAJEUSL.ts.map +0 -7
- package/dist/chunks/chunk-BFK2Z4ZF.ts +0 -189
- package/dist/chunks/chunk-BFK2Z4ZF.ts.map +0 -7
- package/dist/chunks/chunk-BOAXY55Y.ts +0 -153
- package/dist/chunks/chunk-BOAXY55Y.ts.map +0 -7
- package/dist/chunks/chunk-D4CR7T73.ts +0 -353
- package/dist/chunks/chunk-D4CR7T73.ts.map +0 -7
- package/dist/chunks/chunk-FEVPWRMU.ts +0 -300
- package/dist/chunks/chunk-FEVPWRMU.ts.map +0 -7
- package/dist/chunks/chunk-G6VZTCFH.ts +0 -70
- package/dist/chunks/chunk-G6VZTCFH.ts.map +0 -7
- package/dist/chunks/chunk-H3FP6DLR.ts +0 -160
- package/dist/chunks/chunk-H3FP6DLR.ts.map +0 -7
- package/dist/chunks/chunk-HA36LPCO.ts +0 -130
- package/dist/chunks/chunk-HA36LPCO.ts.map +0 -7
- package/dist/chunks/chunk-HOP5FXDT.ts +0 -27
- package/dist/chunks/chunk-HOP5FXDT.ts.map +0 -7
- package/dist/chunks/chunk-IGAMVWFN.ts +0 -139
- package/dist/chunks/chunk-IGAMVWFN.ts.map +0 -7
- package/dist/chunks/chunk-ILEQ27AL.ts +0 -45
- package/dist/chunks/chunk-ILEQ27AL.ts.map +0 -7
- package/dist/chunks/chunk-ITVWPNU4.ts +0 -819
- package/dist/chunks/chunk-ITVWPNU4.ts.map +0 -7
- package/dist/chunks/chunk-IWC32VPY.ts +0 -172
- package/dist/chunks/chunk-IWC32VPY.ts.map +0 -7
- package/dist/chunks/chunk-JSZIP73U.ts +0 -362
- package/dist/chunks/chunk-JSZIP73U.ts.map +0 -7
- package/dist/chunks/chunk-JU6LUNLP.ts +0 -380
- package/dist/chunks/chunk-JU6LUNLP.ts.map +0 -7
- package/dist/chunks/chunk-KMGKCEO4.ts +0 -38
- package/dist/chunks/chunk-KMGKCEO4.ts.map +0 -7
- package/dist/chunks/chunk-LASD73CM.ts +0 -339
- package/dist/chunks/chunk-LASD73CM.ts.map +0 -7
- package/dist/chunks/chunk-LEOYDZI3.ts +0 -22
- package/dist/chunks/chunk-LEOYDZI3.ts.map +0 -7
- package/dist/chunks/chunk-LL4LP2T7.ts +0 -737
- package/dist/chunks/chunk-LL4LP2T7.ts.map +0 -7
- package/dist/chunks/chunk-N2T5IN4X.ts +0 -18
- package/dist/chunks/chunk-N2T5IN4X.ts.map +0 -7
- package/dist/chunks/chunk-N7BLVXKK.ts +0 -73
- package/dist/chunks/chunk-N7BLVXKK.ts.map +0 -7
- package/dist/chunks/chunk-NLT67IZS.ts +0 -322
- package/dist/chunks/chunk-NLT67IZS.ts.map +0 -7
- package/dist/chunks/chunk-NTRPLF46.ts +0 -63
- package/dist/chunks/chunk-NTRPLF46.ts.map +0 -7
- package/dist/chunks/chunk-ONDTY4EL.ts +0 -91
- package/dist/chunks/chunk-ONDTY4EL.ts.map +0 -7
- package/dist/chunks/chunk-OVPHFGKO.ts +0 -1769
- package/dist/chunks/chunk-OVPHFGKO.ts.map +0 -7
- package/dist/chunks/chunk-P7OH4XMF.ts +0 -60
- package/dist/chunks/chunk-P7OH4XMF.ts.map +0 -7
- package/dist/chunks/chunk-PBZMBTNJ.ts +0 -422
- package/dist/chunks/chunk-PBZMBTNJ.ts.map +0 -7
- package/dist/chunks/chunk-PGLSFLYW.ts +0 -52
- package/dist/chunks/chunk-PGLSFLYW.ts.map +0 -7
- package/dist/chunks/chunk-RRR66UWR.ts +0 -28
- package/dist/chunks/chunk-RRR66UWR.ts.map +0 -7
- package/dist/chunks/chunk-RSUXZD6S.ts +0 -275
- package/dist/chunks/chunk-RSUXZD6S.ts.map +0 -7
- package/dist/chunks/chunk-RTCVYIZA.ts +0 -221
- package/dist/chunks/chunk-RTCVYIZA.ts.map +0 -7
- package/dist/chunks/chunk-RY2AEGMZ.ts +0 -34
- package/dist/chunks/chunk-RY2AEGMZ.ts.map +0 -7
- package/dist/chunks/chunk-SWGQLFSD.ts +0 -60
- package/dist/chunks/chunk-SWGQLFSD.ts.map +0 -7
- package/dist/chunks/chunk-TM2R67J3.ts +0 -52
- package/dist/chunks/chunk-TM2R67J3.ts.map +0 -7
- package/dist/chunks/chunk-TMZRHIIK.ts +0 -497
- package/dist/chunks/chunk-TMZRHIIK.ts.map +0 -7
- package/dist/chunks/chunk-TZ34IQ3M.ts +0 -59
- package/dist/chunks/chunk-TZ34IQ3M.ts.map +0 -7
- package/dist/chunks/chunk-UMBPMVJW.ts +0 -22
- package/dist/chunks/chunk-UMBPMVJW.ts.map +0 -7
- package/dist/chunks/chunk-VDG7LTYE.ts +0 -80
- package/dist/chunks/chunk-VDG7LTYE.ts.map +0 -7
- package/dist/chunks/chunk-X4NMONPE.ts +0 -73
- package/dist/chunks/chunk-X4NMONPE.ts.map +0 -7
- package/dist/chunks/chunk-YPJEN6NU.ts +0 -65
- package/dist/chunks/chunk-YPJEN6NU.ts.map +0 -7
- package/dist/chunks/chunk-YU53SHA7.ts +0 -328
- package/dist/chunks/chunk-YU53SHA7.ts.map +0 -7
- package/dist/chunks/completion-delivery-RSJU6BXL.ts +0 -17
- package/dist/chunks/completion-delivery-RSJU6BXL.ts.map +0 -7
- package/dist/chunks/config-status-FKGDECZ3.ts +0 -35
- package/dist/chunks/config-status-FKGDECZ3.ts.map +0 -7
- package/dist/chunks/config-ui-ABHYNGQ7.ts +0 -1212
- package/dist/chunks/config-ui-ABHYNGQ7.ts.map +0 -7
- package/dist/chunks/consult-LJU3IQY5.ts +0 -519
- package/dist/chunks/consult-LJU3IQY5.ts.map +0 -7
- package/dist/chunks/context-QDKLVQXE.ts +0 -11
- package/dist/chunks/context-QDKLVQXE.ts.map +0 -7
- package/dist/chunks/create-stateful-transport-JWC2EFYL.ts +0 -130
- package/dist/chunks/create-stateful-transport-JWC2EFYL.ts.map +0 -7
- package/dist/chunks/cwd-policy-NB6XZ5GF.ts +0 -17
- package/dist/chunks/cwd-policy-NB6XZ5GF.ts.map +0 -7
- package/dist/chunks/delegation-contract-LA56I5DT.ts +0 -24
- package/dist/chunks/delegation-contract-LA56I5DT.ts.map +0 -7
- package/dist/chunks/discovery-MIFB2U4Y.ts +0 -12
- package/dist/chunks/discovery-MIFB2U4Y.ts.map +0 -7
- package/dist/chunks/execution-Q2JZLKJA.ts +0 -4037
- package/dist/chunks/execution-Q2JZLKJA.ts.map +0 -7
- package/dist/chunks/in-process-transport-HJ6TZXC3.ts +0 -39
- package/dist/chunks/in-process-transport-HJ6TZXC3.ts.map +0 -7
- package/dist/chunks/inspect-UH2TKH6E.ts +0 -634
- package/dist/chunks/inspect-UH2TKH6E.ts.map +0 -7
- package/dist/chunks/peer-communication-KL36Z6CO.ts +0 -294
- package/dist/chunks/peer-communication-KL36Z6CO.ts.map +0 -7
- package/dist/chunks/persistence-UY3PY6E5.ts +0 -348
- package/dist/chunks/persistence-UY3PY6E5.ts.map +0 -7
- package/dist/chunks/registry-BT54L6CY.ts +0 -1445
- package/dist/chunks/registry-BT54L6CY.ts.map +0 -7
- package/dist/chunks/retained-semantic-state-GM75FZGE.ts +0 -81
- package/dist/chunks/retained-semantic-state-GM75FZGE.ts.map +0 -7
- package/dist/chunks/rpc-transport-7R7DVCEB.ts +0 -1222
- package/dist/chunks/rpc-transport-7R7DVCEB.ts.map +0 -7
- package/dist/chunks/runtime-policy-5YELCOVC.ts +0 -15
- package/dist/chunks/runtime-policy-5YELCOVC.ts.map +0 -7
- package/dist/chunks/semantic-snapshot-WPJ3UAFW.ts +0 -19
- package/dist/chunks/semantic-snapshot-WPJ3UAFW.ts.map +0 -7
- package/dist/chunks/spawn-idempotency-BNHOSMZW.ts +0 -13
- package/dist/chunks/spawn-idempotency-BNHOSMZW.ts.map +0 -7
- package/dist/chunks/stateful-lifecycle-JAR6K55H.ts +0 -15
- package/dist/chunks/stateful-lifecycle-JAR6K55H.ts.map +0 -7
- package/dist/chunks/subprocess-transport-VHZJRBTW.ts +0 -160
- package/dist/chunks/subprocess-transport-VHZJRBTW.ts.map +0 -7
- package/dist/chunks/usage-recording-store-CW3EH2SZ.ts +0 -164
- package/dist/chunks/usage-recording-store-CW3EH2SZ.ts.map +0 -7
- package/dist/chunks/workspace-WDKLN72J.ts +0 -11
- package/dist/chunks/workspace-WDKLN72J.ts.map +0 -7
- package/docs/async-runtime-protocol.md +0 -84
- package/docs/implementation-notes/pi-subagents-capability-matrix.md +0 -62
- package/docs/implementation-notes/pi-subagents-current-direction.md +0 -99
- package/docs/implementation-notes/pi-subagents-rpc-v1.md +0 -153
- package/docs/pi-subagents-diagrams.md +0 -183
- package/src/adaptive-scheduler.ts +0 -224
- package/src/admission-benchmark.ts +0 -95
- package/src/admission-policy.ts +0 -78
- package/src/agent-projection.ts +0 -53
- package/src/agents/built-ins.ts +0 -71
- package/src/agents/catalog.ts +0 -241
- package/src/agents/discovery.ts +0 -265
- package/src/agents/types.ts +0 -100
- package/src/agents.ts +0 -51
- package/src/async-subagent-benchmark.ts +0 -532
- package/src/auto-transport.ts +0 -121
- package/src/blocking-status.ts +0 -63
- package/src/cached-module-loader.ts +0 -18
- package/src/capabilities.ts +0 -145
- package/src/capability-grant.ts +0 -115
- package/src/capability-router.ts +0 -107
- package/src/child-peer-bridge.ts +0 -124
- package/src/child-peer-tools.ts +0 -132
- package/src/completion-delivery.ts +0 -409
- package/src/completion-render.ts +0 -190
- package/src/completion-requirement.ts +0 -479
- package/src/completion-routing.ts +0 -24
- package/src/config-registration.ts +0 -114
- package/src/config-status.ts +0 -217
- package/src/config-ui.ts +0 -983
- package/src/consult-policy.ts +0 -15
- package/src/consult-registration.ts +0 -125
- package/src/consult-render.ts +0 -194
- package/src/consult-resources.ts +0 -51
- package/src/consult-tool.ts +0 -95
- package/src/consult.ts +0 -566
- package/src/context.ts +0 -126
- package/src/create-stateful-transport.ts +0 -157
- package/src/cwd-policy.ts +0 -183
- package/src/delegation-contract.ts +0 -463
- package/src/execution/budget.ts +0 -56
- package/src/execution/runtime-policy.ts +0 -19
- package/src/execution-plan.ts +0 -322
- package/src/execution-ui.ts +0 -259
- package/src/execution.ts +0 -1679
- package/src/in-process-transport.ts +0 -941
- package/src/inspect-registration.ts +0 -64
- package/src/inspect-render.ts +0 -334
- package/src/inspect-tool.ts +0 -45
- package/src/inspect.ts +0 -769
- package/src/integration-controller.ts +0 -98
- package/src/limits.ts +0 -75
- package/src/orchestration-metrics.ts +0 -116
- package/src/outcome.ts +0 -61
- package/src/panel-child-group.ts +0 -35
- package/src/panel-contract.ts +0 -343
- package/src/panel-evidence.ts +0 -59
- package/src/panel-execution.ts +0 -769
- package/src/panel-failure.ts +0 -56
- package/src/panel-planning.ts +0 -175
- package/src/panel-presets.ts +0 -3
- package/src/panel-prompts.ts +0 -132
- package/src/panel-reconciliation.ts +0 -57
- package/src/panel-render.ts +0 -103
- package/src/parallel-limit-ui.ts +0 -113
- package/src/params.ts +0 -278
- package/src/peer-communication.ts +0 -352
- package/src/peer-transport.ts +0 -49
- package/src/persistence.ts +0 -522
- package/src/pi-args.ts +0 -43
- package/src/pi-invocation.ts +0 -168
- package/src/process-control.ts +0 -43
- package/src/prompt-resources.ts +0 -38
- package/src/prompt-source-safety.ts +0 -48
- package/src/protocol.ts +0 -76
- package/src/registry-types.ts +0 -204
- package/src/registry.ts +0 -1698
- package/src/render-common.ts +0 -252
- package/src/render.ts +0 -704
- package/src/result-contract.ts +0 -431
- package/src/retained-semantic-state.ts +0 -100
- package/src/rpc-timeout-finalization.ts +0 -207
- package/src/rpc-transport-metadata.ts +0 -65
- package/src/rpc-transport.ts +0 -1035
- package/src/rpc-turn-capture.ts +0 -217
- package/src/runner-outcome.ts +0 -31
- package/src/runner-result.ts +0 -55
- package/src/runner-types.ts +0 -102
- package/src/runner-usage.ts +0 -48
- package/src/runner.ts +0 -866
- package/src/safe-text.ts +0 -67
- package/src/semantic-snapshot.ts +0 -214
- package/src/session-guidance-contract.ts +0 -399
- package/src/settings/inspection.ts +0 -302
- package/src/settings/schema.ts +0 -195
- package/src/settings-reader.ts +0 -215
- package/src/settings.ts +0 -452
- package/src/spawn-idempotency.ts +0 -68
- package/src/stateful-agent-view.ts +0 -85
- package/src/stateful-config.ts +0 -13
- package/src/stateful-guidance.ts +0 -29
- package/src/stateful-lifecycle.ts +0 -74
- package/src/stateful-limit-ui.ts +0 -249
- package/src/stateful-limits.ts +0 -96
- package/src/stateful-prompt.ts +0 -49
- package/src/stateful-registration.ts +0 -1320
- package/src/stateful-render.ts +0 -318
- package/src/stateful-safety.ts +0 -47
- package/src/stateful-tool-params.ts +0 -215
- package/src/stateful.ts +0 -12
- package/src/subagent-details.ts +0 -43
- package/src/subagents-extension.ts +0 -355
- package/src/subprocess-transport.ts +0 -145
- package/src/supervision.ts +0 -104
- package/src/task-path.ts +0 -65
- package/src/timeout-checkpoint.ts +0 -305
- package/src/timeout-finalization.ts +0 -75
- package/src/tool-schema-compatibility.ts +0 -73
- package/src/transport-types.ts +0 -74
- package/src/transport-ui.ts +0 -135
- package/src/transport.ts +0 -43
- package/src/turn-budget.ts +0 -109
- package/src/usage-format.ts +0 -42
- package/src/usage-recording-config.ts +0 -13
- package/src/usage-recording-store.ts +0 -183
- package/src/usage-recording.ts +0 -478
- package/src/verification-harness.ts +0 -516
- package/src/verification-policy.ts +0 -67
- package/src/verification-receipt.ts +0 -275
- package/src/verified-execution-benchmark.ts +0 -86
- package/src/verified-execution-contract.ts +0 -191
- package/src/verified-execution-schema.ts +0 -32
- package/src/work-item-ledger.ts +0 -1404
- package/src/work-item-persistence.ts +0 -254
- package/src/workflow-completion-controller.ts +0 -397
- package/src/workflow-planning.ts +0 -172
- package/src/workflow-tree-identity.ts +0 -289
- package/src/workflow-ui.ts +0 -69
- package/src/workflow-verification.ts +0 -296
- package/src/workspace.ts +0 -174
package/src/params.ts
DELETED
|
@@ -1,278 +0,0 @@
|
|
|
1
|
-
import { StringEnum } from "@earendil-works/pi-ai";
|
|
2
|
-
import { type Static, Type } from "typebox";
|
|
3
|
-
import { THINKING_LEVELS } from "./agents/types.js";
|
|
4
|
-
import { DelegationContractSchema } from "./delegation-contract.js";
|
|
5
|
-
import { MAX_CONFIGURABLE_PARALLEL_TASKS, MAX_SUBAGENT_TIMEOUT_MS } from "./limits.js";
|
|
6
|
-
import { PANEL_PRESETS } from "./panel-presets.js";
|
|
7
|
-
import { SUBAGENT_RESULT_FORMATS } from "./result-contract.js";
|
|
8
|
-
import { grammarSafeToolObject } from "./tool-schema-compatibility.js";
|
|
9
|
-
import { MAX_SUBAGENT_TOOL_CALLS, MAX_SUBAGENT_TURNS } from "./turn-budget.js";
|
|
10
|
-
import { VerifiedExecutionContractSchema } from "./verified-execution-schema.js";
|
|
11
|
-
|
|
12
|
-
const TimeoutMs = Type.Number({
|
|
13
|
-
description:
|
|
14
|
-
"Work deadline in milliseconds selected for the task difficulty. On expiry, Pi aborts the work and makes one separately bounded summary attempt. Defaults to PI_SUBAGENT_TIMEOUT_MS or 600000.",
|
|
15
|
-
minimum: 1,
|
|
16
|
-
maximum: MAX_SUBAGENT_TIMEOUT_MS,
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
const TurnLimitFields = {
|
|
20
|
-
idleTimeoutMs: Type.Optional(
|
|
21
|
-
Type.Integer({
|
|
22
|
-
description:
|
|
23
|
-
"Maximum milliseconds without a completed assistant turn or tool result before Pi aborts work and preserves a checkpoint.",
|
|
24
|
-
minimum: 1,
|
|
25
|
-
maximum: MAX_SUBAGENT_TIMEOUT_MS,
|
|
26
|
-
}),
|
|
27
|
-
),
|
|
28
|
-
maxTurns: Type.Optional(
|
|
29
|
-
Type.Integer({
|
|
30
|
-
description: "Maximum assistant turns before unfinished work is stopped and checkpointed.",
|
|
31
|
-
minimum: 1,
|
|
32
|
-
maximum: MAX_SUBAGENT_TURNS,
|
|
33
|
-
}),
|
|
34
|
-
),
|
|
35
|
-
maxToolCalls: Type.Optional(
|
|
36
|
-
Type.Integer({
|
|
37
|
-
description: "Maximum tool calls before additional tool work is stopped and checkpointed.",
|
|
38
|
-
minimum: 1,
|
|
39
|
-
maximum: MAX_SUBAGENT_TOOL_CALLS,
|
|
40
|
-
}),
|
|
41
|
-
),
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const ThinkingLevelSchema = StringEnum(THINKING_LEVELS, {
|
|
45
|
-
description:
|
|
46
|
-
"Pi thinking level for the subagent process: off, minimal, low, medium, high, xhigh, or max.",
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
const ResultFormatSchema = StringEnum(SUBAGENT_RESULT_FORMATS, {
|
|
50
|
-
description:
|
|
51
|
-
"Optional completion contract. Text preserves ordinary output; structured-v1 and structured-v2 request versioned JSON with bounded text fallback.",
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
const ContractFields = {
|
|
55
|
-
contract: Type.Optional(DelegationContractSchema),
|
|
56
|
-
resultFormat: Type.Optional(ResultFormatSchema),
|
|
57
|
-
retryPolicy: Type.Optional(
|
|
58
|
-
Type.Object(
|
|
59
|
-
{
|
|
60
|
-
maxAttempts: Type.Integer({ minimum: 1, maximum: 3 }),
|
|
61
|
-
backoffMs: Type.Optional(Type.Integer({ minimum: 0, maximum: 30_000 })),
|
|
62
|
-
},
|
|
63
|
-
{ additionalProperties: false },
|
|
64
|
-
),
|
|
65
|
-
),
|
|
66
|
-
hedgeAfterMs: Type.Optional(Type.Integer({ minimum: 1, maximum: 30_000 })),
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
const TaskItem = Type.Object({
|
|
70
|
-
agent: Type.String({ description: "Name of the agent to invoke" }),
|
|
71
|
-
task: Type.String({ description: "Task to delegate to the agent" }),
|
|
72
|
-
cwd: Type.Optional(Type.String({ description: "Working directory for the agent process" })),
|
|
73
|
-
timeoutMs: Type.Optional(TimeoutMs),
|
|
74
|
-
...TurnLimitFields,
|
|
75
|
-
thinkingLevel: Type.Optional(ThinkingLevelSchema),
|
|
76
|
-
...ContractFields,
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
const WorkflowTaskItem = Type.Object({
|
|
80
|
-
id: Type.String({ minLength: 1, maxLength: 256 }),
|
|
81
|
-
agent: Type.Optional(
|
|
82
|
-
Type.String({ description: "Optional explicit agent; omit to route by capability manifest" }),
|
|
83
|
-
),
|
|
84
|
-
requiredCapabilities: Type.Optional(
|
|
85
|
-
Type.Array(Type.String({ minLength: 1, maxLength: 256 }), { maxItems: 50 }),
|
|
86
|
-
),
|
|
87
|
-
requiredTools: Type.Optional(
|
|
88
|
-
Type.Array(Type.String({ minLength: 1, maxLength: 256 }), { maxItems: 50 }),
|
|
89
|
-
),
|
|
90
|
-
requiredVerificationRole: Type.Optional(Type.String({ minLength: 1, maxLength: 256 })),
|
|
91
|
-
preferredCostHint: Type.Optional(StringEnum(["low", "medium", "high"] as const)),
|
|
92
|
-
preferredLatencyHint: Type.Optional(StringEnum(["low", "medium", "high"] as const)),
|
|
93
|
-
task: Type.String({ description: "Task to delegate to the agent" }),
|
|
94
|
-
dependsOn: Type.Optional(
|
|
95
|
-
Type.Array(Type.String({ minLength: 1, maxLength: 256 }), { maxItems: 64 }),
|
|
96
|
-
),
|
|
97
|
-
inputArtifacts: Type.Optional(
|
|
98
|
-
Type.Array(Type.String({ minLength: 1, maxLength: 256 }), { maxItems: 50 }),
|
|
99
|
-
),
|
|
100
|
-
inputArtifactVersions: Type.Optional(
|
|
101
|
-
Type.Record(
|
|
102
|
-
Type.String({ minLength: 1, maxLength: 256 }),
|
|
103
|
-
Type.String({ minLength: 1, maxLength: 256 }),
|
|
104
|
-
),
|
|
105
|
-
),
|
|
106
|
-
readPaths: Type.Optional(Type.Array(Type.String({ maxLength: 4096 }), { maxItems: 50 })),
|
|
107
|
-
writePaths: Type.Optional(Type.Array(Type.String({ maxLength: 4096 }), { maxItems: 50 })),
|
|
108
|
-
ownershipKeys: Type.Optional(Type.Array(Type.String({ maxLength: 256 }), { maxItems: 50 })),
|
|
109
|
-
acceptanceCriteria: Type.Optional(Type.Array(Type.String({ maxLength: 4096 }), { maxItems: 50 })),
|
|
110
|
-
integrationOwner: Type.Optional(Type.Boolean()),
|
|
111
|
-
verifierFor: Type.Optional(
|
|
112
|
-
Type.String({
|
|
113
|
-
minLength: 1,
|
|
114
|
-
maxLength: 256,
|
|
115
|
-
description:
|
|
116
|
-
"Target task ID for one distinct direct-dependent structured-v2 verifier. The executor gates target acceptance on a current exact-tree receipt.",
|
|
117
|
-
}),
|
|
118
|
-
),
|
|
119
|
-
cwd: Type.Optional(Type.String({ description: "Working directory for the agent process" })),
|
|
120
|
-
timeoutMs: Type.Optional(TimeoutMs),
|
|
121
|
-
...TurnLimitFields,
|
|
122
|
-
thinkingLevel: Type.Optional(ThinkingLevelSchema),
|
|
123
|
-
...ContractFields,
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
const ChainItem = Type.Object({
|
|
127
|
-
agent: Type.String({ description: "Name of the agent to invoke" }),
|
|
128
|
-
task: Type.String({ description: "Task with optional {previous} placeholder for prior output" }),
|
|
129
|
-
cwd: Type.Optional(Type.String({ description: "Working directory for the agent process" })),
|
|
130
|
-
timeoutMs: Type.Optional(TimeoutMs),
|
|
131
|
-
...TurnLimitFields,
|
|
132
|
-
thinkingLevel: Type.Optional(ThinkingLevelSchema),
|
|
133
|
-
...ContractFields,
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
const AggregatorItem = Type.Object(
|
|
137
|
-
{
|
|
138
|
-
agent: Type.String({
|
|
139
|
-
description: "Name of the fan-in agent to invoke after parallel tasks complete",
|
|
140
|
-
}),
|
|
141
|
-
task: Type.String({
|
|
142
|
-
description: "Fan-in task. Use {previous} to include all parallel outputs.",
|
|
143
|
-
}),
|
|
144
|
-
cwd: Type.Optional(
|
|
145
|
-
Type.String({ description: "Working directory for the aggregator process" }),
|
|
146
|
-
),
|
|
147
|
-
timeoutMs: Type.Optional(TimeoutMs),
|
|
148
|
-
...TurnLimitFields,
|
|
149
|
-
thinkingLevel: Type.Optional(ThinkingLevelSchema),
|
|
150
|
-
...ContractFields,
|
|
151
|
-
},
|
|
152
|
-
{
|
|
153
|
-
description:
|
|
154
|
-
"Optional fan-in step for parallel mode. Omit this key entirely when no aggregation is needed; empty or whitespace-only agent/task values are treated as absent.",
|
|
155
|
-
},
|
|
156
|
-
);
|
|
157
|
-
|
|
158
|
-
const PanelReviewerItem = Type.Object(
|
|
159
|
-
{
|
|
160
|
-
id: Type.String({ minLength: 1, maxLength: 256 }),
|
|
161
|
-
agent: Type.String({ minLength: 1 }),
|
|
162
|
-
focus: Type.Optional(Type.String({ maxLength: 8 * 1024 })),
|
|
163
|
-
timeoutMs: Type.Optional(TimeoutMs),
|
|
164
|
-
...TurnLimitFields,
|
|
165
|
-
thinkingLevel: Type.Optional(ThinkingLevelSchema),
|
|
166
|
-
},
|
|
167
|
-
{ additionalProperties: false },
|
|
168
|
-
);
|
|
169
|
-
|
|
170
|
-
const PanelSynthesizerItem = Type.Object(
|
|
171
|
-
{
|
|
172
|
-
agent: Type.String({ minLength: 1 }),
|
|
173
|
-
timeoutMs: Type.Optional(TimeoutMs),
|
|
174
|
-
...TurnLimitFields,
|
|
175
|
-
thinkingLevel: Type.Optional(ThinkingLevelSchema),
|
|
176
|
-
},
|
|
177
|
-
{ additionalProperties: false },
|
|
178
|
-
);
|
|
179
|
-
|
|
180
|
-
const PanelItem = Type.Object(
|
|
181
|
-
{
|
|
182
|
-
id: Type.Optional(Type.String({ minLength: 1, maxLength: 256 })),
|
|
183
|
-
preset: Type.Optional(StringEnum(PANEL_PRESETS, { default: "custom" })),
|
|
184
|
-
task: Type.String({ minLength: 1, maxLength: 50 * 1024 }),
|
|
185
|
-
context: Type.Optional(Type.String({ maxLength: 50 * 1024 })),
|
|
186
|
-
reviewers: Type.Array(PanelReviewerItem, {
|
|
187
|
-
minItems: 2,
|
|
188
|
-
maxItems: MAX_CONFIGURABLE_PARALLEL_TASKS,
|
|
189
|
-
}),
|
|
190
|
-
synthesizer: PanelSynthesizerItem,
|
|
191
|
-
minValidReviews: Type.Optional(Type.Integer({ minimum: 2 })),
|
|
192
|
-
},
|
|
193
|
-
{
|
|
194
|
-
additionalProperties: false,
|
|
195
|
-
description:
|
|
196
|
-
"One bounded independent review round followed by one evidence-preserving synthesis when enough valid reviews remain.",
|
|
197
|
-
},
|
|
198
|
-
);
|
|
199
|
-
|
|
200
|
-
const AgentScopeSchema = StringEnum(["user", "project", "both"] as const, {
|
|
201
|
-
description:
|
|
202
|
-
'Per-invocation custom agent scope. Default: "user". Use "project" for project-local agents or "both" for user and project agents; this is a tool argument, not a pi-subagents.json setting.',
|
|
203
|
-
default: "user",
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
export const SubagentParams = grammarSafeToolObject({
|
|
207
|
-
agent: Type.Optional(
|
|
208
|
-
Type.String({ description: "Name of the agent to invoke (for single mode)" }),
|
|
209
|
-
),
|
|
210
|
-
task: Type.Optional(Type.String({ description: "Task to delegate (for single mode)" })),
|
|
211
|
-
tasks: Type.Optional(
|
|
212
|
-
Type.Array(TaskItem, {
|
|
213
|
-
description: "Array of {agent, task} for parallel execution",
|
|
214
|
-
maxItems: MAX_CONFIGURABLE_PARALLEL_TASKS,
|
|
215
|
-
}),
|
|
216
|
-
),
|
|
217
|
-
chain: Type.Optional(
|
|
218
|
-
Type.Array(ChainItem, { description: "Array of {agent, task} for sequential execution" }),
|
|
219
|
-
),
|
|
220
|
-
aggregator: Type.Optional(AggregatorItem),
|
|
221
|
-
panel: Type.Optional(PanelItem),
|
|
222
|
-
workflow: Type.Optional(
|
|
223
|
-
Type.Object(
|
|
224
|
-
{
|
|
225
|
-
id: Type.Optional(Type.String({ minLength: 1, maxLength: 256 })),
|
|
226
|
-
verifiedExecution: Type.Optional(VerifiedExecutionContractSchema),
|
|
227
|
-
honorAdmission: Type.Optional(
|
|
228
|
-
Type.Boolean({
|
|
229
|
-
description:
|
|
230
|
-
"Opt in to declining workflow tasks whose explicit audit metadata recommends parent-owned work or abstention.",
|
|
231
|
-
}),
|
|
232
|
-
),
|
|
233
|
-
tasks: Type.Array(WorkflowTaskItem, {
|
|
234
|
-
minItems: 1,
|
|
235
|
-
maxItems: MAX_CONFIGURABLE_PARALLEL_TASKS,
|
|
236
|
-
}),
|
|
237
|
-
},
|
|
238
|
-
{ additionalProperties: false },
|
|
239
|
-
),
|
|
240
|
-
),
|
|
241
|
-
agentScope: Type.Optional(AgentScopeSchema),
|
|
242
|
-
confirmProjectAgents: Type.Optional(
|
|
243
|
-
Type.Boolean({
|
|
244
|
-
description: "Prompt before running project-local agents. Default: true.",
|
|
245
|
-
default: true,
|
|
246
|
-
}),
|
|
247
|
-
),
|
|
248
|
-
cwd: Type.Optional(
|
|
249
|
-
Type.String({ description: "Working directory for the agent process (single mode)" }),
|
|
250
|
-
),
|
|
251
|
-
timeoutMs: Type.Optional(TimeoutMs),
|
|
252
|
-
totalTimeoutMs: Type.Optional(
|
|
253
|
-
Type.Number({
|
|
254
|
-
description:
|
|
255
|
-
"Overall blocking-workflow deadline in milliseconds, including queued work, workflow tasks, panel phases, chain steps, and fan-in.",
|
|
256
|
-
minimum: 1,
|
|
257
|
-
maximum: MAX_SUBAGENT_TIMEOUT_MS,
|
|
258
|
-
}),
|
|
259
|
-
),
|
|
260
|
-
...TurnLimitFields,
|
|
261
|
-
thinkingLevel: Type.Optional(ThinkingLevelSchema),
|
|
262
|
-
...ContractFields,
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
export type SubagentParams = Static<typeof SubagentParams>;
|
|
266
|
-
|
|
267
|
-
export function hasUsableAggregator(
|
|
268
|
-
aggregator: unknown,
|
|
269
|
-
): aggregator is { agent: string; task: string } {
|
|
270
|
-
if (!aggregator || typeof aggregator !== "object") return false;
|
|
271
|
-
const candidate = aggregator as { agent?: unknown; task?: unknown };
|
|
272
|
-
return (
|
|
273
|
-
typeof candidate.agent === "string" &&
|
|
274
|
-
candidate.agent.trim().length > 0 &&
|
|
275
|
-
typeof candidate.task === "string" &&
|
|
276
|
-
candidate.task.trim().length > 0
|
|
277
|
-
);
|
|
278
|
-
}
|
|
@@ -1,352 +0,0 @@
|
|
|
1
|
-
import { randomBytes, randomUUID } from "node:crypto";
|
|
2
|
-
import net, { type AddressInfo, type Server, type Socket } from "node:net";
|
|
3
|
-
import { redactPrivateText } from "./context.js";
|
|
4
|
-
import { truncateUtf8 } from "./limits.js";
|
|
5
|
-
import type { AgentMailboxMessage, AgentRegistry, ManagedAgent } from "./registry.js";
|
|
6
|
-
import { ROOT_TASK_PATH } from "./task-path.js";
|
|
7
|
-
|
|
8
|
-
const MAX_BRIDGE_FRAME_BYTES = 64 * 1024;
|
|
9
|
-
const MAX_BRIDGE_CONNECTIONS = 32;
|
|
10
|
-
const BRIDGE_HANDSHAKE_TIMEOUT_MS = 2_000;
|
|
11
|
-
const MAX_ROOT_MESSAGE_BYTES = 16 * 1024;
|
|
12
|
-
const MAX_LISTED_PEERS = 20;
|
|
13
|
-
|
|
14
|
-
export interface PeerDescriptor {
|
|
15
|
-
id: string;
|
|
16
|
-
taskName: string;
|
|
17
|
-
taskPath: string;
|
|
18
|
-
agent: string;
|
|
19
|
-
state: string;
|
|
20
|
-
self: boolean;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface PeerRootMessage {
|
|
24
|
-
message: AgentMailboxMessage;
|
|
25
|
-
senderPath: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface PeerBridgeCredentials {
|
|
29
|
-
host: string;
|
|
30
|
-
port: number;
|
|
31
|
-
token: string;
|
|
32
|
-
generation: number;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface PeerCommunicationBrokerOptions {
|
|
36
|
-
getRegistry(): AgentRegistry;
|
|
37
|
-
sendRoot(message: PeerRootMessage): void | Promise<void>;
|
|
38
|
-
dispatch?(recipient: ManagedAgent, message: AgentMailboxMessage): boolean | Promise<boolean>;
|
|
39
|
-
now?: () => number;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
interface CredentialRecord extends PeerBridgeCredentials {
|
|
43
|
-
agentId: string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** Session-owned authenticated routing and process-child JSONL bridge. */
|
|
47
|
-
export class PeerCommunicationBroker {
|
|
48
|
-
private server?: Server;
|
|
49
|
-
private starting?: Promise<void>;
|
|
50
|
-
private closed = false;
|
|
51
|
-
private readonly sockets = new Set<Socket>();
|
|
52
|
-
private readonly credentialsByToken = new Map<string, CredentialRecord>();
|
|
53
|
-
private readonly tokenByAgent = new Map<string, string>();
|
|
54
|
-
private readonly dispatchedIds = new Set<string>();
|
|
55
|
-
private readonly rootDeduplication = new Map<string, AgentMailboxMessage>();
|
|
56
|
-
|
|
57
|
-
constructor(private readonly options: PeerCommunicationBrokerOptions) {}
|
|
58
|
-
|
|
59
|
-
async send(
|
|
60
|
-
senderId: string,
|
|
61
|
-
target: string,
|
|
62
|
-
content: string,
|
|
63
|
-
deduplicationKey?: string,
|
|
64
|
-
): Promise<AgentMailboxMessage> {
|
|
65
|
-
if (this.closed) throw new Error("Subagent peer broker is closed");
|
|
66
|
-
const registry = this.options.getRegistry();
|
|
67
|
-
const sender = registry.resolveAgent(senderId);
|
|
68
|
-
if (!sender) throw new Error(`Unknown subagent: ${senderId}`);
|
|
69
|
-
if (sender.state === "closed")
|
|
70
|
-
throw new Error(`Closed agent ${sender.id} cannot send messages`);
|
|
71
|
-
if (target === ROOT_TASK_PATH) {
|
|
72
|
-
return this.sendToRoot(sender, content, deduplicationKey);
|
|
73
|
-
}
|
|
74
|
-
const message = await registry.sendMessage(target, content, sender.id, deduplicationKey);
|
|
75
|
-
if (this.closed) return message;
|
|
76
|
-
const recipient = registry.get(message.recipientId);
|
|
77
|
-
if (recipient?.state === "running" && !this.dispatchedIds.has(message.id)) {
|
|
78
|
-
this.dispatchedIds.add(message.id);
|
|
79
|
-
try {
|
|
80
|
-
await this.options.dispatch?.(recipient, redactedDeliveryCopy(message));
|
|
81
|
-
} catch {
|
|
82
|
-
// Durable mailbox delivery remains available for the recipient's next turn.
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
return message;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
list(senderId: string): PeerDescriptor[] {
|
|
89
|
-
const registry = this.options.getRegistry();
|
|
90
|
-
const sender = registry.resolveAgent(senderId);
|
|
91
|
-
if (!sender) throw new Error(`Unknown subagent: ${senderId}`);
|
|
92
|
-
const root: PeerDescriptor = {
|
|
93
|
-
id: "root",
|
|
94
|
-
taskName: "root",
|
|
95
|
-
taskPath: ROOT_TASK_PATH,
|
|
96
|
-
agent: "root",
|
|
97
|
-
state: "active",
|
|
98
|
-
self: false,
|
|
99
|
-
};
|
|
100
|
-
const retained = registry
|
|
101
|
-
.list()
|
|
102
|
-
.map((agent) => ({
|
|
103
|
-
id: truncateUtf8(agent.id, 256).text,
|
|
104
|
-
taskName: truncateUtf8(agent.taskName ?? "unknown", 128).text,
|
|
105
|
-
taskPath: truncateUtf8(agent.taskPath ?? agent.id, 2_048).text,
|
|
106
|
-
agent: truncateUtf8(agent.agent, 128).text,
|
|
107
|
-
state: agent.state,
|
|
108
|
-
self: agent.id === sender.id,
|
|
109
|
-
}))
|
|
110
|
-
.sort((left, right) => left.taskPath.localeCompare(right.taskPath));
|
|
111
|
-
const selected = retained.slice(0, MAX_LISTED_PEERS - 1);
|
|
112
|
-
if (!selected.some((peer) => peer.id === sender.id)) {
|
|
113
|
-
const senderPeer = retained.find((peer) => peer.id === sender.id);
|
|
114
|
-
if (senderPeer) selected.splice(Math.max(0, selected.length - 1), 1, senderPeer);
|
|
115
|
-
}
|
|
116
|
-
return [root, ...selected].sort((left, right) => left.taskPath.localeCompare(right.taskPath));
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async acknowledge(
|
|
120
|
-
agentId: string,
|
|
121
|
-
messageIds: readonly string[],
|
|
122
|
-
completionIds: readonly string[] = [],
|
|
123
|
-
): Promise<void> {
|
|
124
|
-
if (this.closed) return;
|
|
125
|
-
await this.options
|
|
126
|
-
.getRegistry()
|
|
127
|
-
.acknowledgeVisibleMessages(
|
|
128
|
-
agentId,
|
|
129
|
-
messageIds,
|
|
130
|
-
completionIds,
|
|
131
|
-
(this.options.now ?? Date.now)(),
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
async issueCredentials(agentId: string, generation: number): Promise<PeerBridgeCredentials> {
|
|
136
|
-
if (this.closed) throw new Error("Subagent peer broker is closed");
|
|
137
|
-
if (!Number.isSafeInteger(generation) || generation < 1) {
|
|
138
|
-
throw new Error("Subagent peer bridge generation must be a positive safe integer");
|
|
139
|
-
}
|
|
140
|
-
const agent = this.options.getRegistry().resolveAgent(agentId);
|
|
141
|
-
if (!agent || agent.state === "closed") throw new Error(`Unknown subagent: ${agentId}`);
|
|
142
|
-
await this.ensureServer();
|
|
143
|
-
this.revoke(agent.id);
|
|
144
|
-
const address = this.server?.address();
|
|
145
|
-
if (!address || typeof address === "string") {
|
|
146
|
-
throw new Error("Subagent peer bridge did not expose a loopback address");
|
|
147
|
-
}
|
|
148
|
-
const token = randomBytes(32).toString("hex");
|
|
149
|
-
const credentials: CredentialRecord = {
|
|
150
|
-
agentId: agent.id,
|
|
151
|
-
host: "127.0.0.1",
|
|
152
|
-
port: address.port,
|
|
153
|
-
token,
|
|
154
|
-
generation,
|
|
155
|
-
};
|
|
156
|
-
this.credentialsByToken.set(token, credentials);
|
|
157
|
-
this.tokenByAgent.set(agent.id, token);
|
|
158
|
-
return { host: credentials.host, port: credentials.port, token, generation };
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
revoke(agentId: string): void {
|
|
162
|
-
const token = this.tokenByAgent.get(agentId);
|
|
163
|
-
if (!token) return;
|
|
164
|
-
this.tokenByAgent.delete(agentId);
|
|
165
|
-
this.credentialsByToken.delete(token);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
async close(): Promise<void> {
|
|
169
|
-
if (this.closed) return;
|
|
170
|
-
this.closed = true;
|
|
171
|
-
this.credentialsByToken.clear();
|
|
172
|
-
this.tokenByAgent.clear();
|
|
173
|
-
this.rootDeduplication.clear();
|
|
174
|
-
this.dispatchedIds.clear();
|
|
175
|
-
for (const socket of this.sockets) socket.destroy();
|
|
176
|
-
this.sockets.clear();
|
|
177
|
-
const server = this.server;
|
|
178
|
-
this.server = undefined;
|
|
179
|
-
if (!server) return;
|
|
180
|
-
await new Promise<void>((resolve) => server.close(() => resolve()));
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
private async sendToRoot(
|
|
184
|
-
sender: ManagedAgent,
|
|
185
|
-
content: string,
|
|
186
|
-
deduplicationKey?: string,
|
|
187
|
-
): Promise<AgentMailboxMessage> {
|
|
188
|
-
if (!content.trim()) throw new Error("Subagent peer messages cannot be empty");
|
|
189
|
-
if (deduplicationKey && deduplicationKey.length > 256) {
|
|
190
|
-
throw new Error("Subagent peer deduplication keys cannot exceed 256 characters");
|
|
191
|
-
}
|
|
192
|
-
const deduplicationId = deduplicationKey ? `${sender.id}\0${deduplicationKey}` : undefined;
|
|
193
|
-
const duplicate = deduplicationId ? this.rootDeduplication.get(deduplicationId) : undefined;
|
|
194
|
-
if (duplicate) return { ...duplicate };
|
|
195
|
-
const message: AgentMailboxMessage = {
|
|
196
|
-
id: `msg_${randomUUID()}`,
|
|
197
|
-
senderId: sender.id,
|
|
198
|
-
recipientId: "root",
|
|
199
|
-
content: truncateUtf8(content, MAX_ROOT_MESSAGE_BYTES).text,
|
|
200
|
-
createdAt: (this.options.now ?? Date.now)(),
|
|
201
|
-
deduplicationKey,
|
|
202
|
-
};
|
|
203
|
-
await this.options.sendRoot({
|
|
204
|
-
message: redactedDeliveryCopy(message),
|
|
205
|
-
senderPath: sender.taskPath ?? sender.id,
|
|
206
|
-
});
|
|
207
|
-
if (deduplicationId) {
|
|
208
|
-
this.rootDeduplication.set(deduplicationId, message);
|
|
209
|
-
while (this.rootDeduplication.size > 100) {
|
|
210
|
-
const oldest = this.rootDeduplication.keys().next().value;
|
|
211
|
-
if (typeof oldest !== "string") break;
|
|
212
|
-
this.rootDeduplication.delete(oldest);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
return { ...message };
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
private async ensureServer(): Promise<void> {
|
|
219
|
-
if (this.server) return;
|
|
220
|
-
if (!this.starting) {
|
|
221
|
-
this.starting = new Promise<void>((resolve, reject) => {
|
|
222
|
-
const server = net.createServer((socket) => this.accept(socket));
|
|
223
|
-
server.maxConnections = MAX_BRIDGE_CONNECTIONS;
|
|
224
|
-
server.once("error", reject);
|
|
225
|
-
server.listen({ host: "127.0.0.1", port: 0 }, () => {
|
|
226
|
-
server.off("error", reject);
|
|
227
|
-
this.server = server;
|
|
228
|
-
resolve();
|
|
229
|
-
});
|
|
230
|
-
}).finally(() => {
|
|
231
|
-
this.starting = undefined;
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
await this.starting;
|
|
235
|
-
if (this.closed) {
|
|
236
|
-
const server = this.server as Server | undefined;
|
|
237
|
-
this.server = undefined;
|
|
238
|
-
if (server) await new Promise<void>((resolve) => server.close(() => resolve()));
|
|
239
|
-
throw new Error("Subagent peer broker closed while starting");
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
private accept(socket: Socket): void {
|
|
244
|
-
if (this.closed || this.sockets.size >= MAX_BRIDGE_CONNECTIONS) {
|
|
245
|
-
socket.destroy();
|
|
246
|
-
return;
|
|
247
|
-
}
|
|
248
|
-
this.sockets.add(socket);
|
|
249
|
-
let frame = Buffer.alloc(0);
|
|
250
|
-
let handled = false;
|
|
251
|
-
const timer = setTimeout(() => {
|
|
252
|
-
handled = true;
|
|
253
|
-
this.respond(socket, { ok: false, error: "bridge handshake timed out" });
|
|
254
|
-
}, BRIDGE_HANDSHAKE_TIMEOUT_MS);
|
|
255
|
-
timer.unref();
|
|
256
|
-
const cleanup = () => {
|
|
257
|
-
clearTimeout(timer);
|
|
258
|
-
this.sockets.delete(socket);
|
|
259
|
-
};
|
|
260
|
-
socket.on("data", (chunk: Buffer) => {
|
|
261
|
-
if (handled) return;
|
|
262
|
-
frame = Buffer.concat([frame, chunk]);
|
|
263
|
-
if (frame.byteLength > MAX_BRIDGE_FRAME_BYTES) {
|
|
264
|
-
handled = true;
|
|
265
|
-
clearTimeout(timer);
|
|
266
|
-
this.respond(socket, { ok: false, error: "bridge frame exceeds size limit" });
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
const newline = frame.indexOf(0x0a);
|
|
270
|
-
if (newline < 0) return;
|
|
271
|
-
handled = true;
|
|
272
|
-
clearTimeout(timer);
|
|
273
|
-
void this.handleFrame(frame.subarray(0, newline).toString("utf8")).then((response) =>
|
|
274
|
-
this.respond(socket, response),
|
|
275
|
-
);
|
|
276
|
-
});
|
|
277
|
-
socket.once("close", cleanup);
|
|
278
|
-
socket.once("error", cleanup);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
private async handleFrame(frame: string): Promise<Record<string, unknown>> {
|
|
282
|
-
let request: Record<string, unknown>;
|
|
283
|
-
try {
|
|
284
|
-
const parsed = JSON.parse(frame) as unknown;
|
|
285
|
-
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error();
|
|
286
|
-
request = parsed as Record<string, unknown>;
|
|
287
|
-
} catch {
|
|
288
|
-
return { ok: false, error: "malformed bridge request" };
|
|
289
|
-
}
|
|
290
|
-
const credential =
|
|
291
|
-
typeof request.token === "string" ? this.credentialsByToken.get(request.token) : undefined;
|
|
292
|
-
if (!credential) return { ok: false, error: "unauthenticated bridge request" };
|
|
293
|
-
try {
|
|
294
|
-
switch (request.action) {
|
|
295
|
-
case "send": {
|
|
296
|
-
if (typeof request.target !== "string" || typeof request.message !== "string") {
|
|
297
|
-
throw new Error("send requires target and message strings");
|
|
298
|
-
}
|
|
299
|
-
const message = await this.send(
|
|
300
|
-
credential.agentId,
|
|
301
|
-
request.target,
|
|
302
|
-
request.message,
|
|
303
|
-
typeof request.deduplicationKey === "string" ? request.deduplicationKey : undefined,
|
|
304
|
-
);
|
|
305
|
-
return { ok: true, message };
|
|
306
|
-
}
|
|
307
|
-
case "list":
|
|
308
|
-
return { ok: true, peers: this.list(credential.agentId) };
|
|
309
|
-
case "acknowledge": {
|
|
310
|
-
const messageIds = stringArray(request.messageIds);
|
|
311
|
-
const completionIds = stringArray(request.completionIds);
|
|
312
|
-
await this.acknowledge(credential.agentId, messageIds, completionIds);
|
|
313
|
-
return { ok: true };
|
|
314
|
-
}
|
|
315
|
-
default:
|
|
316
|
-
return { ok: false, error: "unsupported bridge action" };
|
|
317
|
-
}
|
|
318
|
-
} catch (error) {
|
|
319
|
-
return { ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
private respond(socket: Socket, value: Record<string, unknown>): void {
|
|
324
|
-
if (socket.destroyed) return;
|
|
325
|
-
const content = `${JSON.stringify(value)}\n`;
|
|
326
|
-
socket.end(content);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
function redactedDeliveryCopy(message: AgentMailboxMessage): AgentMailboxMessage {
|
|
331
|
-
return { ...message, content: redactPrivateText(message.content) };
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
function stringArray(value: unknown): string[] {
|
|
335
|
-
if (value === undefined) return [];
|
|
336
|
-
if (
|
|
337
|
-
!Array.isArray(value) ||
|
|
338
|
-
value.length > 100 ||
|
|
339
|
-
value.some((item) => typeof item !== "string")
|
|
340
|
-
) {
|
|
341
|
-
throw new Error("acknowledgement IDs must be a bounded string array");
|
|
342
|
-
}
|
|
343
|
-
return value;
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
export function peerBridgeAddress(credentials: PeerBridgeCredentials): string {
|
|
347
|
-
return `${credentials.host}:${credentials.port}`;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
export function isLoopbackAddress(address: AddressInfo | string | null): address is AddressInfo {
|
|
351
|
-
return Boolean(address && typeof address !== "string" && address.address === "127.0.0.1");
|
|
352
|
-
}
|
package/src/peer-transport.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { fileURLToPath } from "node:url";
|
|
2
|
-
import type { ExtensionFactory } from "@earendil-works/pi-coding-agent";
|
|
3
|
-
import { createChildPeerExtension } from "./child-peer-tools.js";
|
|
4
|
-
import type { PeerBridgeCredentials, PeerDescriptor } from "./peer-communication.js";
|
|
5
|
-
import type { AgentMailboxMessage } from "./registry.js";
|
|
6
|
-
|
|
7
|
-
export const CHILD_PEER_TOOL_NAMES = ["subagent_peer_send", "subagent_peer_list"] as const;
|
|
8
|
-
|
|
9
|
-
export interface PeerTransportRuntime {
|
|
10
|
-
send(
|
|
11
|
-
senderId: string,
|
|
12
|
-
target: string,
|
|
13
|
-
message: string,
|
|
14
|
-
deduplicationKey?: string,
|
|
15
|
-
): Promise<AgentMailboxMessage>;
|
|
16
|
-
list(senderId: string): PeerDescriptor[];
|
|
17
|
-
acknowledge(
|
|
18
|
-
agentId: string,
|
|
19
|
-
messageIds: readonly string[],
|
|
20
|
-
completionIds?: readonly string[],
|
|
21
|
-
): Promise<void>;
|
|
22
|
-
issueCredentials(agentId: string, generation: number): Promise<PeerBridgeCredentials>;
|
|
23
|
-
revoke(agentId: string): void;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function createInProcessPeerExtension(
|
|
27
|
-
runtime: PeerTransportRuntime,
|
|
28
|
-
agentId: string,
|
|
29
|
-
): ExtensionFactory {
|
|
30
|
-
return createChildPeerExtension({
|
|
31
|
-
send: (target, message, deduplicationKey) =>
|
|
32
|
-
runtime.send(agentId, target, message, deduplicationKey),
|
|
33
|
-
list: async () => runtime.list(agentId),
|
|
34
|
-
acknowledge: (messageIds, completionIds) =>
|
|
35
|
-
runtime.acknowledge(agentId, messageIds, completionIds),
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function childPeerBridgePath(): string {
|
|
40
|
-
return fileURLToPath(new URL("./child-peer-bridge.ts", import.meta.url));
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function peerBridgeEnvironment(credentials: PeerBridgeCredentials): NodeJS.ProcessEnv {
|
|
44
|
-
return {
|
|
45
|
-
PI_SUBAGENT_PEER_HOST: credentials.host,
|
|
46
|
-
PI_SUBAGENT_PEER_PORT: String(credentials.port),
|
|
47
|
-
PI_SUBAGENT_PEER_TOKEN: credentials.token,
|
|
48
|
-
};
|
|
49
|
-
}
|