@narumitw/pi-subagents 0.49.3 → 0.52.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 +362 -53
- package/package.json +10 -7
- package/src/adaptive-scheduler.ts +224 -0
- package/src/admission-benchmark.ts +95 -0
- package/src/admission-policy.ts +78 -0
- package/src/agent-projection.ts +53 -0
- package/src/agents.ts +58 -1
- package/src/auto-transport.ts +114 -0
- package/src/blocking-status.ts +63 -0
- package/src/capabilities.ts +145 -0
- package/src/capability-grant.ts +115 -0
- package/src/capability-router.ts +107 -0
- package/src/completion-delivery.ts +257 -0
- package/src/config-status.ts +221 -0
- package/src/config-ui.ts +215 -236
- package/src/consult-resources.ts +4 -27
- package/src/consult.ts +9 -1
- package/src/create-stateful-transport.ts +55 -0
- package/src/delegation-contract.ts +417 -0
- package/src/execution-plan.ts +322 -0
- package/src/execution-profiles.ts +95 -0
- package/src/execution-ui.ts +320 -0
- package/src/execution.ts +1098 -158
- package/src/in-process-transport.ts +269 -25
- package/src/inspect-render.ts +101 -1
- package/src/inspect.ts +321 -3
- package/src/integration-controller.ts +98 -0
- package/src/limits.ts +3 -0
- package/src/orchestration-metrics.ts +109 -0
- package/src/outcome.ts +61 -0
- package/src/panel-child-group.ts +35 -0
- package/src/panel-contract.ts +343 -0
- package/src/panel-evidence.ts +59 -0
- package/src/panel-execution.ts +770 -0
- package/src/panel-failure.ts +56 -0
- package/src/panel-planning.ts +175 -0
- package/src/panel-prompts.ts +132 -0
- package/src/panel-reconciliation.ts +57 -0
- package/src/panel-render.ts +103 -0
- package/src/parallel-limit-ui.ts +112 -0
- package/src/params.ts +179 -3
- package/src/persistence.ts +182 -32
- package/src/prompt-resources.ts +38 -0
- package/src/registry-types.ts +175 -0
- package/src/registry.ts +466 -143
- package/src/render.ts +72 -6
- package/src/result-contract.ts +416 -0
- package/src/retained-semantic-state.ts +100 -0
- package/src/rpc-timeout-finalization.ts +207 -0
- package/src/rpc-transport-metadata.ts +65 -0
- package/src/rpc-transport.ts +990 -0
- package/src/rpc-turn-capture.ts +142 -0
- package/src/runner-result.ts +55 -0
- package/src/runner-usage.ts +48 -0
- package/src/runner.ts +325 -73
- package/src/semantic-snapshot.ts +214 -0
- package/src/settings.ts +254 -35
- package/src/spawn-idempotency.ts +61 -0
- package/src/stateful-config.ts +13 -0
- package/src/stateful-guidance.ts +1 -0
- package/src/stateful-lifecycle.ts +45 -2
- package/src/stateful-limit-ui.ts +246 -0
- package/src/stateful-limits.ts +96 -0
- package/src/stateful-prompt.ts +11 -2
- package/src/stateful-render.ts +48 -3
- package/src/stateful.ts +467 -357
- package/src/subagents.ts +114 -46
- package/src/subprocess-transport.ts +64 -5
- package/src/supervision.ts +103 -0
- package/src/timeout-checkpoint.ts +305 -0
- package/src/timeout-finalization.ts +75 -0
- package/src/transport-types.ts +68 -0
- package/src/transport-ui.ts +169 -0
- package/src/transport.ts +16 -4
- package/src/turn-budget.ts +109 -0
- package/src/verification-policy.ts +67 -0
- package/src/work-item-ledger.ts +931 -0
- package/src/work-item-persistence.ts +223 -0
- package/src/workflow-planning.ts +162 -0
- package/src/workflow-tree-identity.ts +289 -0
- package/src/workflow-ui.ts +61 -0
- package/src/workflow-verification.ts +296 -0
- package/src/workspace.ts +69 -12
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { getAgentDir, withFileMutationQueue } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import { redactPrivateText } from "./context.js";
|
|
6
|
+
import { WorkItemLedger, type WorkItemLedgerSnapshot } from "./work-item-ledger.js";
|
|
7
|
+
|
|
8
|
+
const WORKFLOW_STATE_DIRECTORY = "pi-subagents-workflows";
|
|
9
|
+
const DEFAULT_MAX_STORED_WORKFLOWS = 64;
|
|
10
|
+
const DEFAULT_RETENTION_MS = 30 * 24 * 60 * 60 * 1000;
|
|
11
|
+
const MAX_WORKFLOW_STATE_BYTES = 1024 * 1024;
|
|
12
|
+
|
|
13
|
+
export interface SessionWorkflowPersistenceOptions {
|
|
14
|
+
stateDir?: string;
|
|
15
|
+
maxStoredWorkflows?: number;
|
|
16
|
+
retentionMs?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface SessionWorkflowInspection {
|
|
20
|
+
workflows: WorkItemLedgerSnapshot[];
|
|
21
|
+
invalid: number;
|
|
22
|
+
omitted: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class WorkItemPersistence {
|
|
26
|
+
constructor(
|
|
27
|
+
readonly filePath: string,
|
|
28
|
+
private readonly afterSave?: () => Promise<void>,
|
|
29
|
+
) {}
|
|
30
|
+
|
|
31
|
+
async save(snapshot: WorkItemLedgerSnapshot): Promise<void> {
|
|
32
|
+
const filePath = path.resolve(this.filePath);
|
|
33
|
+
const sanitized = sanitizeWorkflowSnapshot(snapshot);
|
|
34
|
+
const content = `${JSON.stringify(sanitized)}\n`;
|
|
35
|
+
if (Buffer.byteLength(content, "utf8") > MAX_WORKFLOW_STATE_BYTES) {
|
|
36
|
+
throw new Error("WorkItem workflow state exceeds the persistence size limit");
|
|
37
|
+
}
|
|
38
|
+
await withFileMutationQueue(filePath, async () => {
|
|
39
|
+
await fs.promises.mkdir(path.dirname(filePath), { recursive: true, mode: 0o700 });
|
|
40
|
+
const temporary = `${filePath}.${process.pid}.${Date.now()}.tmp`;
|
|
41
|
+
try {
|
|
42
|
+
await fs.promises.writeFile(temporary, content, { mode: 0o600 });
|
|
43
|
+
await fs.promises.rename(temporary, filePath);
|
|
44
|
+
} finally {
|
|
45
|
+
await fs.promises.rm(temporary, { force: true });
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
await this.afterSave?.();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
load(): WorkItemLedger | undefined {
|
|
52
|
+
const filePath = path.resolve(this.filePath);
|
|
53
|
+
let source: string;
|
|
54
|
+
try {
|
|
55
|
+
const stat = fs.statSync(filePath);
|
|
56
|
+
if (stat.size > MAX_WORKFLOW_STATE_BYTES)
|
|
57
|
+
throw new Error("workflow state exceeds size limit");
|
|
58
|
+
source = fs.readFileSync(filePath, "utf8");
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
return WorkItemLedger.restore(JSON.parse(source) as WorkItemLedgerSnapshot);
|
|
65
|
+
} catch {
|
|
66
|
+
const quarantine = `${filePath}.invalid-${Date.now()}`;
|
|
67
|
+
try {
|
|
68
|
+
fs.renameSync(filePath, quarantine);
|
|
69
|
+
} catch {
|
|
70
|
+
// A concurrent owner may already have handled the invalid file.
|
|
71
|
+
}
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function createSessionWorkItemPersistence(
|
|
78
|
+
owner: string,
|
|
79
|
+
workflowId: string,
|
|
80
|
+
options: SessionWorkflowPersistenceOptions = {},
|
|
81
|
+
): WorkItemPersistence {
|
|
82
|
+
const maxStoredWorkflows = options.maxStoredWorkflows ?? DEFAULT_MAX_STORED_WORKFLOWS;
|
|
83
|
+
const retentionMs = options.retentionMs ?? DEFAULT_RETENTION_MS;
|
|
84
|
+
if (!Number.isSafeInteger(maxStoredWorkflows) || maxStoredWorkflows < 1) {
|
|
85
|
+
throw new Error("maxStoredWorkflows must be a positive safe integer");
|
|
86
|
+
}
|
|
87
|
+
if (!Number.isFinite(retentionMs) || retentionMs <= 0) {
|
|
88
|
+
throw new Error("workflow retentionMs must be a positive finite number");
|
|
89
|
+
}
|
|
90
|
+
const stateDir = resolveStateDirectory(options.stateDir);
|
|
91
|
+
const prefix = sessionPrefix(owner);
|
|
92
|
+
const filePath = path.join(stateDir, `${prefix}-${stableId(workflowId)}.json`);
|
|
93
|
+
return new WorkItemPersistence(filePath, () =>
|
|
94
|
+
pruneSessionWorkflows(stateDir, prefix, maxStoredWorkflows, retentionMs),
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function inspectSessionWorkflows(
|
|
99
|
+
owner: string,
|
|
100
|
+
options: SessionWorkflowPersistenceOptions = {},
|
|
101
|
+
): SessionWorkflowInspection {
|
|
102
|
+
const stateDir = resolveStateDirectory(options.stateDir);
|
|
103
|
+
const prefix = `${sessionPrefix(owner)}-`;
|
|
104
|
+
const limit = options.maxStoredWorkflows ?? DEFAULT_MAX_STORED_WORKFLOWS;
|
|
105
|
+
if (!Number.isSafeInteger(limit) || limit < 1) {
|
|
106
|
+
throw new Error("maxStoredWorkflows must be a positive safe integer");
|
|
107
|
+
}
|
|
108
|
+
let entries: fs.Dirent[];
|
|
109
|
+
try {
|
|
110
|
+
entries = fs.readdirSync(stateDir, { withFileTypes: true });
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
|
113
|
+
return { workflows: [], invalid: 0, omitted: 0 };
|
|
114
|
+
}
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
const candidates = entries
|
|
118
|
+
.filter(
|
|
119
|
+
(entry) => entry.isFile() && entry.name.startsWith(prefix) && entry.name.endsWith(".json"),
|
|
120
|
+
)
|
|
121
|
+
.map((entry) => {
|
|
122
|
+
const filePath = path.join(stateDir, entry.name);
|
|
123
|
+
return { filePath, modifiedAt: safeModifiedAt(filePath) };
|
|
124
|
+
})
|
|
125
|
+
.sort((left, right) => right.modifiedAt - left.modifiedAt);
|
|
126
|
+
const workflows: WorkItemLedgerSnapshot[] = [];
|
|
127
|
+
let invalid = 0;
|
|
128
|
+
for (const candidate of candidates.slice(0, limit)) {
|
|
129
|
+
try {
|
|
130
|
+
const stat = fs.statSync(candidate.filePath);
|
|
131
|
+
if (stat.size > MAX_WORKFLOW_STATE_BYTES)
|
|
132
|
+
throw new Error("workflow state exceeds size limit");
|
|
133
|
+
const source = fs.readFileSync(candidate.filePath, "utf8");
|
|
134
|
+
workflows.push(
|
|
135
|
+
WorkItemLedger.restore(JSON.parse(source) as WorkItemLedgerSnapshot).snapshot(),
|
|
136
|
+
);
|
|
137
|
+
} catch {
|
|
138
|
+
invalid++;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return { workflows, invalid, omitted: Math.max(0, candidates.length - limit) };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function safeModifiedAt(filePath: string): number {
|
|
145
|
+
try {
|
|
146
|
+
return fs.statSync(filePath).mtimeMs;
|
|
147
|
+
} catch {
|
|
148
|
+
return 0;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function resolveStateDirectory(stateDir: string | undefined): string {
|
|
153
|
+
return path.resolve(stateDir ?? path.join(getAgentDir(), WORKFLOW_STATE_DIRECTORY));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function sessionPrefix(owner: string): string {
|
|
157
|
+
return stableId(`session:${owner}`);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function stableId(value: string): string {
|
|
161
|
+
return createHash("sha256").update(value).digest("hex").slice(0, 24);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function pruneSessionWorkflows(
|
|
165
|
+
stateDir: string,
|
|
166
|
+
prefix: string,
|
|
167
|
+
maxStoredWorkflows: number,
|
|
168
|
+
retentionMs: number,
|
|
169
|
+
): Promise<void> {
|
|
170
|
+
await withFileMutationQueue(path.join(stateDir, `${prefix}.prune`), async () => {
|
|
171
|
+
const cutoff = Date.now() - retentionMs;
|
|
172
|
+
const entries = (await fs.promises.readdir(stateDir, { withFileTypes: true }))
|
|
173
|
+
.filter(
|
|
174
|
+
(entry) =>
|
|
175
|
+
entry.isFile() && entry.name.startsWith(`${prefix}-`) && entry.name.endsWith(".json"),
|
|
176
|
+
)
|
|
177
|
+
.map((entry) => path.join(stateDir, entry.name));
|
|
178
|
+
const records = await Promise.all(
|
|
179
|
+
entries.map(async (filePath) => ({
|
|
180
|
+
filePath,
|
|
181
|
+
modifiedAt: (await fs.promises.stat(filePath)).mtimeMs,
|
|
182
|
+
})),
|
|
183
|
+
);
|
|
184
|
+
records.sort((left, right) => right.modifiedAt - left.modifiedAt);
|
|
185
|
+
await Promise.all(
|
|
186
|
+
records
|
|
187
|
+
.filter((record, index) => index >= maxStoredWorkflows || record.modifiedAt < cutoff)
|
|
188
|
+
.map((record) => fs.promises.rm(record.filePath, { force: true })),
|
|
189
|
+
);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function sanitizeWorkflowSnapshot(snapshot: WorkItemLedgerSnapshot): WorkItemLedgerSnapshot {
|
|
194
|
+
const sanitized = structuredClone(snapshot);
|
|
195
|
+
for (const item of sanitized.items) {
|
|
196
|
+
item.objective = redact(item.objective);
|
|
197
|
+
item.selectedAgentName = item.selectedAgentName ? redact(item.selectedAgentName) : undefined;
|
|
198
|
+
item.requiredCapabilities = item.requiredCapabilities.map(redact);
|
|
199
|
+
item.requiredTools = item.requiredTools.map(redact);
|
|
200
|
+
item.readPaths = item.readPaths.map(redact);
|
|
201
|
+
item.writePaths = item.writePaths.map(redact);
|
|
202
|
+
item.ownershipKeys = item.ownershipKeys.map(redact);
|
|
203
|
+
item.acceptanceCriteria = item.acceptanceCriteria.map(redact);
|
|
204
|
+
item.invalidationReasons = item.invalidationReasons.map(redact);
|
|
205
|
+
item.outcomeReason = item.outcomeReason ? redact(item.outcomeReason) : undefined;
|
|
206
|
+
if (item.verificationReceipt) {
|
|
207
|
+
item.verificationReceipt.summary = redact(item.verificationReceipt.summary);
|
|
208
|
+
item.verificationReceipt.evidence = item.verificationReceipt.evidence.map(redact);
|
|
209
|
+
item.verificationReceipt.limitations = item.verificationReceipt.limitations.map(redact);
|
|
210
|
+
}
|
|
211
|
+
for (const artifact of [...item.artifacts, ...item.artifactHistory]) {
|
|
212
|
+
artifact.kind = redact(artifact.kind);
|
|
213
|
+
artifact.version = redact(artifact.version);
|
|
214
|
+
artifact.digest = artifact.digest ? redact(artifact.digest) : undefined;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
WorkItemLedger.restore(sanitized);
|
|
218
|
+
return sanitized;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function redact(value: string): string {
|
|
222
|
+
return redactPrivateText(value).trim();
|
|
223
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type { AgentConfig } from "./agents.js";
|
|
2
|
+
import { routeByCapability } from "./capability-router.js";
|
|
3
|
+
import { normalizeDelegationContract } from "./delegation-contract.js";
|
|
4
|
+
import type { SubagentParams } from "./params.js";
|
|
5
|
+
import { type WorkItemDefinition, WorkItemLedger } from "./work-item-ledger.js";
|
|
6
|
+
|
|
7
|
+
export type WorkflowTask = NonNullable<SubagentParams["workflow"]>["tasks"][number];
|
|
8
|
+
export type ResolvedWorkflowTask = WorkflowTask & { agent: string };
|
|
9
|
+
type Aggregator = NonNullable<SubagentParams["aggregator"]>;
|
|
10
|
+
|
|
11
|
+
type WorkRequest = {
|
|
12
|
+
contract?: unknown;
|
|
13
|
+
inputArtifacts?: string[];
|
|
14
|
+
inputArtifactVersions?: Record<string, string>;
|
|
15
|
+
requiredCapabilities?: string[];
|
|
16
|
+
requiredTools?: string[];
|
|
17
|
+
agent?: string;
|
|
18
|
+
sideEffectPolicy?: "read-only" | "idempotent" | "mutating";
|
|
19
|
+
readPaths?: string[];
|
|
20
|
+
writePaths?: string[];
|
|
21
|
+
ownershipKeys?: string[];
|
|
22
|
+
acceptanceCriteria?: string[];
|
|
23
|
+
integrationOwner?: boolean;
|
|
24
|
+
verifierFor?: string;
|
|
25
|
+
dependencyPolicy?: "completed" | "settled";
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export function resolveWorkflowTasks(
|
|
29
|
+
params: SubagentParams,
|
|
30
|
+
agents: readonly AgentConfig[],
|
|
31
|
+
): ResolvedWorkflowTask[] {
|
|
32
|
+
return (params.workflow?.tasks ?? []).map((task) => {
|
|
33
|
+
const contract = normalizeDelegationContract(task.contract);
|
|
34
|
+
const route = routeByCapability(agents, {
|
|
35
|
+
agent: task.agent,
|
|
36
|
+
requiredCapabilities: [
|
|
37
|
+
...(task.requiredCapabilities ?? []),
|
|
38
|
+
...(contract?.requestedAuthority?.capabilities ?? []),
|
|
39
|
+
],
|
|
40
|
+
requiredTools: [
|
|
41
|
+
...(task.requiredTools ?? []),
|
|
42
|
+
...(contract?.requestedAuthority?.tools ?? []),
|
|
43
|
+
],
|
|
44
|
+
requiredVerificationRole: task.requiredVerificationRole,
|
|
45
|
+
requiredSideEffectClass: contract?.sideEffectPolicy === "read-only" ? "read-only" : undefined,
|
|
46
|
+
preferredCostHint: task.preferredCostHint,
|
|
47
|
+
preferredLatencyHint: task.preferredLatencyHint,
|
|
48
|
+
});
|
|
49
|
+
return { ...task, agent: route.agent.name };
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function createBlockingWorkLedger(
|
|
54
|
+
params: SubagentParams,
|
|
55
|
+
resolvedWorkflowTasks: ResolvedWorkflowTask[],
|
|
56
|
+
aggregator: Aggregator | undefined,
|
|
57
|
+
): WorkItemLedger | undefined {
|
|
58
|
+
if (params.agent && params.task) {
|
|
59
|
+
return WorkItemLedger.create({
|
|
60
|
+
workflowId: "blocking-single",
|
|
61
|
+
items: [definition("task-1", params.task, [], params)],
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
if (params.chain?.length) {
|
|
65
|
+
return WorkItemLedger.create({
|
|
66
|
+
workflowId: "blocking-chain",
|
|
67
|
+
items: params.chain.map((step, index) =>
|
|
68
|
+
definition(`step-${index + 1}`, step.task, index === 0 ? [] : [`step-${index}`], step),
|
|
69
|
+
),
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
if (params.tasks?.length) {
|
|
73
|
+
const items = params.tasks.map((task, index) =>
|
|
74
|
+
definition(`task-${index + 1}`, task.task, [], task),
|
|
75
|
+
);
|
|
76
|
+
if (aggregator) {
|
|
77
|
+
items.push(
|
|
78
|
+
definition(
|
|
79
|
+
"aggregator",
|
|
80
|
+
aggregator.task,
|
|
81
|
+
params.tasks.map((_task, index) => `task-${index + 1}`),
|
|
82
|
+
{ ...aggregator, dependencyPolicy: "settled" },
|
|
83
|
+
),
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
return WorkItemLedger.create({ workflowId: "blocking-parallel", items });
|
|
87
|
+
}
|
|
88
|
+
if (params.workflow && resolvedWorkflowTasks.length > 0) {
|
|
89
|
+
const hasExplicitIntegrationOwner = resolvedWorkflowTasks.some(
|
|
90
|
+
(task) => task.integrationOwner === true,
|
|
91
|
+
);
|
|
92
|
+
let defaultIntegrationOwnerIndex = -1;
|
|
93
|
+
if (!hasExplicitIntegrationOwner) {
|
|
94
|
+
for (let index = resolvedWorkflowTasks.length - 1; index >= 0; index--) {
|
|
95
|
+
if (resolvedWorkflowTasks[index]?.verifierFor === undefined) {
|
|
96
|
+
defaultIntegrationOwnerIndex = index;
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!hasExplicitIntegrationOwner && defaultIntegrationOwnerIndex < 0) {
|
|
102
|
+
throw new Error("Workflow has no non-verifier integration owner candidate");
|
|
103
|
+
}
|
|
104
|
+
return WorkItemLedger.create({
|
|
105
|
+
workflowId: params.workflow.id ?? "blocking-workflow",
|
|
106
|
+
items: resolvedWorkflowTasks.map((task, index) =>
|
|
107
|
+
definition(task.id, task.task, task.dependsOn ?? [], {
|
|
108
|
+
...task,
|
|
109
|
+
integrationOwner:
|
|
110
|
+
task.integrationOwner ??
|
|
111
|
+
(!hasExplicitIntegrationOwner && index === defaultIntegrationOwnerIndex),
|
|
112
|
+
}),
|
|
113
|
+
),
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function definition(
|
|
120
|
+
id: string,
|
|
121
|
+
task: string,
|
|
122
|
+
dependencies: string[],
|
|
123
|
+
request: WorkRequest,
|
|
124
|
+
): WorkItemDefinition {
|
|
125
|
+
const contract = normalizeDelegationContract(request.contract);
|
|
126
|
+
return {
|
|
127
|
+
id,
|
|
128
|
+
objective: contract?.objective ?? task,
|
|
129
|
+
dependencies,
|
|
130
|
+
inputArtifacts: [
|
|
131
|
+
...(request.inputArtifacts ?? contract?.requiredInputs ?? []),
|
|
132
|
+
...(contract?.dependencies
|
|
133
|
+
.filter((dependency) => dependency.artifactId)
|
|
134
|
+
.map((dependency) => dependency.artifactId as string) ?? []),
|
|
135
|
+
],
|
|
136
|
+
inputArtifactVersions: {
|
|
137
|
+
...Object.fromEntries(
|
|
138
|
+
(contract?.dependencies ?? [])
|
|
139
|
+
.filter((dependency) => dependency.artifactId && dependency.version)
|
|
140
|
+
.map((dependency) => [dependency.artifactId as string, dependency.version as string]),
|
|
141
|
+
),
|
|
142
|
+
...(request.inputArtifactVersions ?? {}),
|
|
143
|
+
},
|
|
144
|
+
requiredCapabilities: [
|
|
145
|
+
...(request.requiredCapabilities ?? []),
|
|
146
|
+
...(contract?.requestedAuthority?.capabilities ?? []),
|
|
147
|
+
],
|
|
148
|
+
requiredTools: [
|
|
149
|
+
...(request.requiredTools ?? []),
|
|
150
|
+
...(contract?.requestedAuthority?.tools ?? []),
|
|
151
|
+
],
|
|
152
|
+
selectedAgentName: request.agent,
|
|
153
|
+
sideEffectPolicy: contract?.sideEffectPolicy ?? request.sideEffectPolicy ?? "mutating",
|
|
154
|
+
readPaths: request.readPaths ?? contract?.requestedAuthority?.readPaths ?? [],
|
|
155
|
+
writePaths: request.writePaths ?? contract?.requestedAuthority?.writePaths ?? [],
|
|
156
|
+
ownershipKeys: request.ownershipKeys ?? [],
|
|
157
|
+
acceptanceCriteria: request.acceptanceCriteria ?? contract?.acceptanceCriteria ?? [],
|
|
158
|
+
integrationOwner: request.integrationOwner,
|
|
159
|
+
verifierFor: request.verifierFor,
|
|
160
|
+
dependencyPolicy: request.dependencyPolicy,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { createHash, type Hash } from "node:crypto";
|
|
3
|
+
import * as fs from "node:fs";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
|
|
6
|
+
export const WORKFLOW_TREE_IDENTITY_VERSION = "pi-subagents:workflow-tree:v1" as const;
|
|
7
|
+
export const DEFAULT_WORKFLOW_TREE_MAX_BYTES = 1024 * 1024;
|
|
8
|
+
const MAX_UNTRACKED_FILES = 256;
|
|
9
|
+
|
|
10
|
+
export interface WorkflowTreeIdentity {
|
|
11
|
+
version: typeof WORKFLOW_TREE_IDENTITY_VERSION;
|
|
12
|
+
kind: "git-commit" | "git-dirty";
|
|
13
|
+
digest: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface CaptureWorkflowTreeIdentityOptions {
|
|
17
|
+
maxBytes?: number;
|
|
18
|
+
signal?: AbortSignal;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function captureWorkflowTreeIdentity(
|
|
22
|
+
cwd: string,
|
|
23
|
+
options: CaptureWorkflowTreeIdentityOptions = {},
|
|
24
|
+
): Promise<WorkflowTreeIdentity> {
|
|
25
|
+
const maxBytes = options.maxBytes ?? DEFAULT_WORKFLOW_TREE_MAX_BYTES;
|
|
26
|
+
if (!Number.isSafeInteger(maxBytes) || maxBytes < 1) {
|
|
27
|
+
throw new Error("Workflow tree identity maxBytes must be a positive safe integer");
|
|
28
|
+
}
|
|
29
|
+
throwIfAborted(options.signal);
|
|
30
|
+
let canonicalCwd: string;
|
|
31
|
+
try {
|
|
32
|
+
canonicalCwd = await fs.promises.realpath(path.resolve(cwd));
|
|
33
|
+
} catch {
|
|
34
|
+
throw new Error("Workflow verification requires a readable Git repository directory");
|
|
35
|
+
}
|
|
36
|
+
const rootOutput = await git(
|
|
37
|
+
canonicalCwd,
|
|
38
|
+
["rev-parse", "--show-toplevel"],
|
|
39
|
+
64 * 1024,
|
|
40
|
+
options.signal,
|
|
41
|
+
).catch((error) => {
|
|
42
|
+
throw normalizedGitError(error, "Workflow verification requires a Git repository");
|
|
43
|
+
});
|
|
44
|
+
let repositoryRoot: string;
|
|
45
|
+
try {
|
|
46
|
+
repositoryRoot = await fs.promises.realpath(rootOutput.toString("utf8").trim());
|
|
47
|
+
} catch {
|
|
48
|
+
throw new Error("Workflow verification requires a readable Git repository root");
|
|
49
|
+
}
|
|
50
|
+
const relativeCwd = path.relative(repositoryRoot, canonicalCwd);
|
|
51
|
+
if (relativeCwd.startsWith("..") || path.isAbsolute(relativeCwd)) {
|
|
52
|
+
throw new Error("Workflow verification cwd is outside its Git repository");
|
|
53
|
+
}
|
|
54
|
+
const submodules = await git(
|
|
55
|
+
repositoryRoot,
|
|
56
|
+
["submodule", "status", "--recursive"],
|
|
57
|
+
64 * 1024,
|
|
58
|
+
options.signal,
|
|
59
|
+
).catch((error) => {
|
|
60
|
+
throw normalizedGitError(error, "Workflow tree identity could not inspect submodules");
|
|
61
|
+
});
|
|
62
|
+
if (submodules.toString("utf8").trim()) {
|
|
63
|
+
throw new Error("Workflow tree identity does not support repositories with submodules");
|
|
64
|
+
}
|
|
65
|
+
const head = (
|
|
66
|
+
await git(repositoryRoot, ["rev-parse", "HEAD"], 64 * 1024, options.signal).catch((error) => {
|
|
67
|
+
throw normalizedGitError(error, "Workflow verification requires a stable Git HEAD");
|
|
68
|
+
})
|
|
69
|
+
)
|
|
70
|
+
.toString("utf8")
|
|
71
|
+
.trim();
|
|
72
|
+
if (!/^[a-f0-9]{40,64}$/u.test(head)) {
|
|
73
|
+
throw new Error("Workflow verification requires a stable Git HEAD");
|
|
74
|
+
}
|
|
75
|
+
const commandLimit = maxBytes + 1;
|
|
76
|
+
const indexDiff = await git(
|
|
77
|
+
repositoryRoot,
|
|
78
|
+
["diff", "--binary", "--no-ext-diff", "--cached", "HEAD", "--"],
|
|
79
|
+
commandLimit,
|
|
80
|
+
options.signal,
|
|
81
|
+
).catch((error) => {
|
|
82
|
+
throw normalizedGitError(error, "Workflow tree identity exceeded its size limit");
|
|
83
|
+
});
|
|
84
|
+
const worktreeDiff = await git(
|
|
85
|
+
repositoryRoot,
|
|
86
|
+
["diff", "--binary", "--no-ext-diff", "--"],
|
|
87
|
+
commandLimit,
|
|
88
|
+
options.signal,
|
|
89
|
+
).catch((error) => {
|
|
90
|
+
throw normalizedGitError(error, "Workflow tree identity exceeded its size limit");
|
|
91
|
+
});
|
|
92
|
+
if (indexDiff.length > maxBytes || worktreeDiff.length > maxBytes) {
|
|
93
|
+
throw new Error("Workflow tree identity exceeded its size limit");
|
|
94
|
+
}
|
|
95
|
+
const untrackedOutput = await git(
|
|
96
|
+
repositoryRoot,
|
|
97
|
+
["ls-files", "--others", "--exclude-standard", "-z"],
|
|
98
|
+
commandLimit,
|
|
99
|
+
options.signal,
|
|
100
|
+
).catch((error) => {
|
|
101
|
+
throw normalizedGitError(error, "Workflow tree identity exceeded its size limit");
|
|
102
|
+
});
|
|
103
|
+
const untrackedEntries = splitNul(untrackedOutput).sort(Buffer.compare);
|
|
104
|
+
if (untrackedEntries.length > MAX_UNTRACKED_FILES) {
|
|
105
|
+
throw new Error("Workflow tree identity exceeded its untracked-file limit");
|
|
106
|
+
}
|
|
107
|
+
if (indexDiff.length === 0 && worktreeDiff.length === 0 && untrackedEntries.length === 0) {
|
|
108
|
+
return identity("git-commit", hashParts([Buffer.from("commit\0"), Buffer.from(head)]));
|
|
109
|
+
}
|
|
110
|
+
let consumed = indexDiff.length + worktreeDiff.length + untrackedOutput.length;
|
|
111
|
+
if (consumed > maxBytes) throw new Error("Workflow tree identity exceeded its size limit");
|
|
112
|
+
const hasher = createHash("sha256");
|
|
113
|
+
hasher.update("pi-subagents:workflow-tree:v1\0");
|
|
114
|
+
updateHashFrame(hasher, "head", head);
|
|
115
|
+
updateHashFrame(hasher, "index-diff", indexDiff);
|
|
116
|
+
updateHashFrame(hasher, "worktree-diff", worktreeDiff);
|
|
117
|
+
for (const rawRelativePath of untrackedEntries) {
|
|
118
|
+
throwIfAborted(options.signal);
|
|
119
|
+
const relativePath = decodeGitPath(rawRelativePath);
|
|
120
|
+
const candidate = path.resolve(repositoryRoot, relativePath);
|
|
121
|
+
const relative = path.relative(repositoryRoot, candidate);
|
|
122
|
+
if (!relative || relative.startsWith("..") || path.isAbsolute(relative)) {
|
|
123
|
+
throw new Error("Workflow tree identity encountered an unsafe untracked path");
|
|
124
|
+
}
|
|
125
|
+
let bytes: Buffer;
|
|
126
|
+
let kind: string;
|
|
127
|
+
try {
|
|
128
|
+
const stat = await fs.promises.lstat(candidate);
|
|
129
|
+
if (stat.isSymbolicLink()) {
|
|
130
|
+
kind = "symlink";
|
|
131
|
+
bytes = Buffer.from(await fs.promises.readlink(candidate), "utf8");
|
|
132
|
+
} else if (stat.isFile()) {
|
|
133
|
+
kind = "file";
|
|
134
|
+
if (stat.size > maxBytes - consumed) {
|
|
135
|
+
throw new Error("Workflow tree identity exceeded its size limit");
|
|
136
|
+
}
|
|
137
|
+
bytes = await readRegularFileNoFollow(candidate, stat, options.signal);
|
|
138
|
+
} else {
|
|
139
|
+
throw new Error("Workflow tree identity encountered an unsupported untracked file type");
|
|
140
|
+
}
|
|
141
|
+
} catch (error) {
|
|
142
|
+
if (error instanceof Error && error.name === "AbortError") throw error;
|
|
143
|
+
if (error instanceof Error && error.message.startsWith("Workflow tree identity")) {
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
throw new Error("Workflow tree identity could not read an untracked entry");
|
|
147
|
+
}
|
|
148
|
+
consumed += rawRelativePath.length + bytes.length;
|
|
149
|
+
if (consumed > maxBytes) throw new Error("Workflow tree identity exceeded its size limit");
|
|
150
|
+
updateHashFrame(hasher, "untracked-kind", kind);
|
|
151
|
+
updateHashFrame(hasher, "untracked-path", rawRelativePath);
|
|
152
|
+
updateHashFrame(hasher, "untracked-content", bytes);
|
|
153
|
+
}
|
|
154
|
+
return identity("git-dirty", hasher.digest("hex"));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function sameWorkflowTreeIdentity(
|
|
158
|
+
left: WorkflowTreeIdentity,
|
|
159
|
+
right: WorkflowTreeIdentity,
|
|
160
|
+
): boolean {
|
|
161
|
+
return (
|
|
162
|
+
isWorkflowTreeIdentity(left) &&
|
|
163
|
+
isWorkflowTreeIdentity(right) &&
|
|
164
|
+
left.kind === right.kind &&
|
|
165
|
+
left.digest === right.digest
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function isWorkflowTreeIdentity(value: unknown): value is WorkflowTreeIdentity {
|
|
170
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
171
|
+
const candidate = value as Partial<WorkflowTreeIdentity>;
|
|
172
|
+
if (
|
|
173
|
+
Object.keys(value as Record<string, unknown>).some(
|
|
174
|
+
(key) => !["version", "kind", "digest"].includes(key),
|
|
175
|
+
)
|
|
176
|
+
) {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
return (
|
|
180
|
+
candidate.version === WORKFLOW_TREE_IDENTITY_VERSION &&
|
|
181
|
+
(candidate.kind === "git-commit" || candidate.kind === "git-dirty") &&
|
|
182
|
+
typeof candidate.digest === "string" &&
|
|
183
|
+
/^[a-f0-9]{64}$/u.test(candidate.digest)
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function identity(kind: WorkflowTreeIdentity["kind"], digest: string): WorkflowTreeIdentity {
|
|
188
|
+
return { version: WORKFLOW_TREE_IDENTITY_VERSION, kind, digest };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function hashParts(parts: Buffer[]): string {
|
|
192
|
+
const hash = createHash("sha256");
|
|
193
|
+
for (const part of parts) hash.update(part);
|
|
194
|
+
return hash.digest("hex");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function updateHashFrame(hash: Hash, label: string, value: string | Buffer): void {
|
|
198
|
+
const labelBytes = Buffer.from(label, "utf8");
|
|
199
|
+
const valueBytes = typeof value === "string" ? Buffer.from(value, "utf8") : value;
|
|
200
|
+
const header = Buffer.allocUnsafe(8);
|
|
201
|
+
header.writeUInt32BE(labelBytes.length, 0);
|
|
202
|
+
header.writeUInt32BE(valueBytes.length, 4);
|
|
203
|
+
hash.update(header);
|
|
204
|
+
hash.update(labelBytes);
|
|
205
|
+
hash.update(valueBytes);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function splitNul(value: Buffer): Buffer[] {
|
|
209
|
+
const entries: Buffer[] = [];
|
|
210
|
+
let start = 0;
|
|
211
|
+
for (let index = 0; index < value.length; index++) {
|
|
212
|
+
if (value[index] !== 0) continue;
|
|
213
|
+
if (index > start) entries.push(value.subarray(start, index));
|
|
214
|
+
start = index + 1;
|
|
215
|
+
}
|
|
216
|
+
if (start < value.length) entries.push(value.subarray(start));
|
|
217
|
+
return entries;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function decodeGitPath(value: Buffer): string {
|
|
221
|
+
const decoded = value.toString("utf8");
|
|
222
|
+
if (!decoded || decoded.includes("\0") || !Buffer.from(decoded, "utf8").equals(value)) {
|
|
223
|
+
throw new Error("Workflow tree identity encountered an unsupported Git path encoding");
|
|
224
|
+
}
|
|
225
|
+
return decoded;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async function readRegularFileNoFollow(
|
|
229
|
+
filePath: string,
|
|
230
|
+
expected: fs.Stats,
|
|
231
|
+
signal: AbortSignal | undefined,
|
|
232
|
+
): Promise<Buffer> {
|
|
233
|
+
throwIfAborted(signal);
|
|
234
|
+
const handle = await fs.promises.open(filePath, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW);
|
|
235
|
+
try {
|
|
236
|
+
const before = await handle.stat();
|
|
237
|
+
if (!before.isFile() || before.dev !== expected.dev || before.ino !== expected.ino) {
|
|
238
|
+
throw new Error("Workflow tree identity file changed during capture");
|
|
239
|
+
}
|
|
240
|
+
const content = await handle.readFile({ signal });
|
|
241
|
+
const after = await handle.stat();
|
|
242
|
+
if (
|
|
243
|
+
after.dev !== before.dev ||
|
|
244
|
+
after.ino !== before.ino ||
|
|
245
|
+
after.size !== before.size ||
|
|
246
|
+
after.mtimeMs !== before.mtimeMs
|
|
247
|
+
) {
|
|
248
|
+
throw new Error("Workflow tree identity file changed during capture");
|
|
249
|
+
}
|
|
250
|
+
return content;
|
|
251
|
+
} finally {
|
|
252
|
+
await handle.close();
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function git(
|
|
257
|
+
cwd: string,
|
|
258
|
+
args: string[],
|
|
259
|
+
maxBuffer: number,
|
|
260
|
+
signal?: AbortSignal,
|
|
261
|
+
): Promise<Buffer> {
|
|
262
|
+
throwIfAborted(signal);
|
|
263
|
+
return new Promise((resolve, reject) => {
|
|
264
|
+
execFile(
|
|
265
|
+
"git",
|
|
266
|
+
["-C", cwd, ...args],
|
|
267
|
+
{ encoding: "buffer", maxBuffer, signal },
|
|
268
|
+
(error, stdout) => {
|
|
269
|
+
if (error) reject(error);
|
|
270
|
+
else resolve(stdout);
|
|
271
|
+
},
|
|
272
|
+
);
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function throwIfAborted(signal: AbortSignal | undefined): void {
|
|
277
|
+
if (!signal?.aborted) return;
|
|
278
|
+
const error = new Error("Workflow tree identity capture was cancelled");
|
|
279
|
+
error.name = "AbortError";
|
|
280
|
+
throw error;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function normalizedGitError(error: unknown, fallback: string): Error {
|
|
284
|
+
if (error instanceof Error && error.name === "AbortError") return error;
|
|
285
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
286
|
+
return new Error(
|
|
287
|
+
/maxBuffer|stdout.*large|SIGTERM/iu.test(message) ? `${fallback}: size limit` : fallback,
|
|
288
|
+
);
|
|
289
|
+
}
|