pi-crew 0.1.30 → 0.1.32
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/CHANGELOG.md +9 -0
- package/README.md +80 -27
- package/agents/analyst.md +11 -11
- package/agents/critic.md +11 -11
- package/agents/executor.md +11 -11
- package/agents/explorer.md +11 -11
- package/agents/planner.md +11 -11
- package/agents/reviewer.md +11 -11
- package/agents/security-reviewer.md +11 -11
- package/agents/test-engineer.md +11 -11
- package/agents/verifier.md +11 -11
- package/agents/writer.md +11 -11
- package/docs/architecture.md +173 -164
- package/docs/refactor-tasks-phase3.md +394 -394
- package/docs/refactor-tasks-phase4.md +564 -564
- package/docs/refactor-tasks-phase5.md +402 -402
- package/docs/refactor-tasks-phase6.md +662 -662
- package/docs/research-extension-examples.md +297 -0
- package/docs/research-extension-system.md +324 -0
- package/docs/research-optimization-plan.md +548 -0
- package/docs/research-pi-coding-agent.md +357 -0
- package/docs/resource-formats.md +4 -3
- package/docs/runtime-flow.md +148 -148
- package/docs/source-runtime-refactor-map.md +83 -83
- package/docs/usage.md +3 -3
- package/index.ts +6 -6
- package/package.json +1 -1
- package/schema.json +53 -1
- package/skills/git-master/SKILL.md +24 -19
- package/skills/read-only-explorer/SKILL.md +26 -21
- package/skills/safe-bash/SKILL.md +21 -16
- package/skills/task-packet/SKILL.md +28 -23
- package/skills/verify-evidence/SKILL.md +27 -22
- package/src/agents/agent-serializer.ts +34 -34
- package/src/agents/discover-agents.ts +102 -102
- package/src/config/config.ts +14 -1
- package/src/config/defaults.ts +3 -2
- package/src/extension/cross-extension-rpc.ts +82 -82
- package/src/extension/import-index.ts +4 -3
- package/src/extension/management.ts +2 -2
- package/src/extension/project-init.ts +9 -7
- package/src/extension/register.ts +63 -0
- package/src/extension/registration/artifact-cleanup.ts +15 -14
- package/src/extension/registration/commands.ts +208 -208
- package/src/extension/registration/compaction-guard.ts +125 -0
- package/src/extension/registration/subagent-tools.ts +27 -8
- package/src/extension/registration/team-tool.ts +61 -44
- package/src/extension/result-watcher.ts +98 -98
- package/src/extension/run-import.ts +4 -4
- package/src/extension/run-index.ts +14 -14
- package/src/extension/run-maintenance.ts +24 -24
- package/src/extension/team-tool/api.ts +3 -0
- package/src/extension/team-tool/cancel.ts +31 -31
- package/src/extension/team-tool/doctor.ts +179 -178
- package/src/extension/team-tool/inspect.ts +41 -41
- package/src/extension/team-tool/lifecycle-actions.ts +79 -79
- package/src/extension/team-tool/plan.ts +19 -19
- package/src/extension/team-tool/status.ts +73 -73
- package/src/prompt/prompt-runtime.ts +68 -68
- package/src/runtime/agent-control.ts +64 -64
- package/src/runtime/agent-memory.ts +72 -72
- package/src/runtime/agent-observability.ts +113 -113
- package/src/runtime/async-marker.ts +26 -26
- package/src/runtime/background-runner.ts +53 -53
- package/src/runtime/crew-agent-runtime.ts +58 -58
- package/src/runtime/direct-run.ts +35 -35
- package/src/runtime/foreground-control.ts +82 -82
- package/src/runtime/green-contract.ts +46 -46
- package/src/runtime/group-join.ts +88 -88
- package/src/runtime/live-agent-control.ts +78 -78
- package/src/runtime/live-agent-manager.ts +85 -85
- package/src/runtime/live-control-realtime.ts +36 -36
- package/src/runtime/live-session-runtime.ts +299 -299
- package/src/runtime/manifest-cache.ts +212 -214
- package/src/runtime/model-fallback.ts +261 -261
- package/src/runtime/parallel-research.ts +44 -44
- package/src/runtime/parallel-utils.ts +99 -99
- package/src/runtime/pi-json-output.ts +111 -111
- package/src/runtime/pi-spawn.ts +96 -96
- package/src/runtime/policy-engine.ts +78 -78
- package/src/runtime/post-exit-stdio-guard.ts +86 -86
- package/src/runtime/process-status.ts +56 -56
- package/src/runtime/progress-event-coalescer.ts +43 -43
- package/src/runtime/recovery-recipes.ts +74 -74
- package/src/runtime/role-permission.ts +39 -39
- package/src/runtime/session-usage.ts +79 -79
- package/src/runtime/sidechain-output.ts +28 -28
- package/src/runtime/subagent-manager.ts +29 -2
- package/src/runtime/task-display.ts +38 -38
- package/src/runtime/task-output-context.ts +106 -106
- package/src/runtime/task-packet.ts +84 -84
- package/src/runtime/task-runner/live-executor.ts +98 -98
- package/src/runtime/task-runner/progress.ts +111 -111
- package/src/runtime/task-runner/prompt-builder.ts +72 -72
- package/src/runtime/task-runner/result-utils.ts +14 -14
- package/src/runtime/task-runner/state-helpers.ts +22 -22
- package/src/runtime/worker-heartbeat.ts +21 -21
- package/src/runtime/worker-startup.ts +57 -57
- package/src/schema/config-schema.ts +12 -0
- package/src/schema/team-tool-schema.ts +100 -100
- package/src/state/artifact-store.ts +108 -108
- package/src/state/contracts.ts +105 -105
- package/src/state/jsonl-writer.ts +77 -77
- package/src/state/state-store.ts +8 -9
- package/src/state/task-claims.ts +42 -42
- package/src/state/types.ts +180 -180
- package/src/state/usage.ts +29 -29
- package/src/subagents/async-entry.ts +1 -1
- package/src/subagents/index.ts +3 -3
- package/src/subagents/live/control.ts +1 -1
- package/src/subagents/live/manager.ts +1 -1
- package/src/subagents/live/realtime.ts +1 -1
- package/src/subagents/live/session-runtime.ts +1 -1
- package/src/subagents/manager.ts +1 -1
- package/src/subagents/spawn.ts +1 -1
- package/src/teams/discover-teams.ts +2 -2
- package/src/teams/team-serializer.ts +36 -36
- package/src/types/diff.d.ts +18 -0
- package/src/ui/crew-footer.ts +101 -101
- package/src/ui/crew-select-list.ts +111 -111
- package/src/ui/crew-widget.ts +285 -285
- package/src/ui/dynamic-border.ts +25 -25
- package/src/ui/layout-primitives.ts +106 -106
- package/src/ui/loaders.ts +158 -158
- package/src/ui/mascot.ts +441 -441
- package/src/ui/powerbar-publisher.ts +94 -94
- package/src/ui/render-diff.ts +119 -119
- package/src/ui/run-dashboard.ts +372 -372
- package/src/ui/status-colors.ts +54 -54
- package/src/ui/syntax-highlight.ts +116 -116
- package/src/ui/transcript-viewer.ts +302 -302
- package/src/utils/completion-dedupe.ts +63 -63
- package/src/utils/file-coalescer.ts +84 -84
- package/src/utils/frontmatter.ts +36 -36
- package/src/utils/fs-watch.ts +31 -31
- package/src/utils/git.ts +262 -262
- package/src/utils/ids.ts +12 -12
- package/src/utils/names.ts +26 -26
- package/src/utils/paths.ts +34 -7
- package/src/utils/sleep.ts +32 -32
- package/src/utils/timings.ts +31 -31
- package/src/utils/visual.ts +159 -159
- package/src/workflows/discover-workflows.ts +2 -2
- package/src/workflows/validate-workflow.ts +40 -40
- package/src/worktree/branch-freshness.ts +45 -45
- package/src/worktree/cleanup.ts +71 -69
- package/src/worktree/worktree-manager.ts +3 -1
- package/teams/default.team.md +12 -12
- package/teams/fast-fix.team.md +11 -11
- package/teams/implementation.team.md +18 -18
- package/teams/parallel-research.team.md +14 -14
- package/teams/research.team.md +11 -11
- package/teams/review.team.md +12 -12
- package/workflows/default.workflow.md +29 -29
- package/workflows/fast-fix.workflow.md +22 -22
- package/workflows/implementation.workflow.md +38 -38
- package/workflows/parallel-research.workflow.md +46 -46
- package/workflows/research.workflow.md +22 -22
- package/workflows/review.workflow.md +30 -30
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
import type { CrewLimitsConfig } from "../config/config.ts";
|
|
2
|
-
import type { PolicyDecision, PolicyDecisionAction, PolicyDecisionReason, TeamRunManifest, TeamTaskState } from "../state/types.ts";
|
|
3
|
-
import { evaluateGreenContract } from "./green-contract.ts";
|
|
4
|
-
import { isWorkerHeartbeatStale } from "./worker-heartbeat.ts";
|
|
5
|
-
|
|
6
|
-
export interface PolicyEngineInput {
|
|
7
|
-
manifest: TeamRunManifest;
|
|
8
|
-
tasks: TeamTaskState[];
|
|
9
|
-
limits?: CrewLimitsConfig;
|
|
10
|
-
now?: Date;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function decision(action: PolicyDecisionAction, reason: PolicyDecisionReason, message: string, taskId?: string): PolicyDecision {
|
|
14
|
-
return {
|
|
15
|
-
action,
|
|
16
|
-
reason,
|
|
17
|
-
message,
|
|
18
|
-
taskId,
|
|
19
|
-
createdAt: new Date().toISOString(),
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function taskDepth(task: TeamTaskState, tasksById: Map<string, TeamTaskState>): number {
|
|
24
|
-
let depth = 0;
|
|
25
|
-
let current = task.graph?.parentId;
|
|
26
|
-
const seen = new Set<string>();
|
|
27
|
-
while (current && !seen.has(current)) {
|
|
28
|
-
seen.add(current);
|
|
29
|
-
depth += 1;
|
|
30
|
-
current = tasksById.get(current)?.graph?.parentId;
|
|
31
|
-
}
|
|
32
|
-
return depth;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function evaluateCrewPolicy(input: PolicyEngineInput): PolicyDecision[] {
|
|
36
|
-
const decisions: PolicyDecision[] = [];
|
|
37
|
-
const maxTasksPerRun = input.limits?.maxTasksPerRun;
|
|
38
|
-
if (maxTasksPerRun !== undefined && input.tasks.length > maxTasksPerRun) {
|
|
39
|
-
decisions.push(decision("block", "limit_exceeded", `Run has ${input.tasks.length} tasks, exceeding maxTasksPerRun=${maxTasksPerRun}.`));
|
|
40
|
-
}
|
|
41
|
-
const runningCount = input.tasks.filter((task) => task.status === "running").length;
|
|
42
|
-
if (input.limits?.maxConcurrentWorkers !== undefined && runningCount > input.limits.maxConcurrentWorkers) {
|
|
43
|
-
decisions.push(decision("block", "limit_exceeded", `Run has ${runningCount} running workers, exceeding maxConcurrentWorkers=${input.limits.maxConcurrentWorkers}.`));
|
|
44
|
-
}
|
|
45
|
-
const tasksById = new Map(input.tasks.map((task) => [task.id, task]));
|
|
46
|
-
|
|
47
|
-
for (const task of input.tasks) {
|
|
48
|
-
if (input.limits?.maxChildrenPerTask !== undefined && (task.graph?.children.length ?? 0) > input.limits.maxChildrenPerTask) {
|
|
49
|
-
decisions.push(decision("block", "limit_exceeded", `Task has ${task.graph?.children.length ?? 0} children, exceeding maxChildrenPerTask=${input.limits.maxChildrenPerTask}.`, task.id));
|
|
50
|
-
}
|
|
51
|
-
if (input.limits?.maxTaskDepth !== undefined && taskDepth(task, tasksById) > input.limits.maxTaskDepth) {
|
|
52
|
-
decisions.push(decision("block", "limit_exceeded", `Task graph depth exceeds maxTaskDepth=${input.limits.maxTaskDepth}.`, task.id));
|
|
53
|
-
}
|
|
54
|
-
if (task.status === "failed") {
|
|
55
|
-
const retryCount = task.policy?.retryCount ?? 0;
|
|
56
|
-
const maxRetries = input.limits?.maxRetriesPerTask ?? 0;
|
|
57
|
-
decisions.push(decision(retryCount < maxRetries ? "retry" : "escalate", "task_failed", task.error ? `Task failed: ${task.error}` : "Task failed.", task.id));
|
|
58
|
-
}
|
|
59
|
-
if ((task.status === "running" || task.status === "queued") && task.heartbeat && task.heartbeat.alive !== false && isWorkerHeartbeatStale(task.heartbeat, input.limits?.heartbeatStaleMs ?? 60_000, input.now)) {
|
|
60
|
-
decisions.push(decision("escalate", "worker_stale", "Worker heartbeat is stale.", task.id));
|
|
61
|
-
}
|
|
62
|
-
if (task.taskPacket?.verification) {
|
|
63
|
-
const outcome = evaluateGreenContract(task.taskPacket.verification, task.verification);
|
|
64
|
-
if (!outcome.satisfied && task.status === "completed") {
|
|
65
|
-
decisions.push(decision("block", "green_unsatisfied", `Green contract unsatisfied: required=${outcome.requiredGreenLevel}, observed=${outcome.observedGreenLevel}.`, task.id));
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (decisions.length === 0 && input.tasks.length > 0 && input.tasks.every((task) => task.status === "completed")) {
|
|
71
|
-
decisions.push(decision("closeout", "run_complete", "All tasks completed and no policy blockers were found."));
|
|
72
|
-
}
|
|
73
|
-
return decisions;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function summarizePolicyDecisions(decisions: PolicyDecision[]): string[] {
|
|
77
|
-
return decisions.map((item) => `- ${item.action} (${item.reason})${item.taskId ? ` ${item.taskId}` : ""}: ${item.message}`);
|
|
78
|
-
}
|
|
1
|
+
import type { CrewLimitsConfig } from "../config/config.ts";
|
|
2
|
+
import type { PolicyDecision, PolicyDecisionAction, PolicyDecisionReason, TeamRunManifest, TeamTaskState } from "../state/types.ts";
|
|
3
|
+
import { evaluateGreenContract } from "./green-contract.ts";
|
|
4
|
+
import { isWorkerHeartbeatStale } from "./worker-heartbeat.ts";
|
|
5
|
+
|
|
6
|
+
export interface PolicyEngineInput {
|
|
7
|
+
manifest: TeamRunManifest;
|
|
8
|
+
tasks: TeamTaskState[];
|
|
9
|
+
limits?: CrewLimitsConfig;
|
|
10
|
+
now?: Date;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function decision(action: PolicyDecisionAction, reason: PolicyDecisionReason, message: string, taskId?: string): PolicyDecision {
|
|
14
|
+
return {
|
|
15
|
+
action,
|
|
16
|
+
reason,
|
|
17
|
+
message,
|
|
18
|
+
taskId,
|
|
19
|
+
createdAt: new Date().toISOString(),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function taskDepth(task: TeamTaskState, tasksById: Map<string, TeamTaskState>): number {
|
|
24
|
+
let depth = 0;
|
|
25
|
+
let current = task.graph?.parentId;
|
|
26
|
+
const seen = new Set<string>();
|
|
27
|
+
while (current && !seen.has(current)) {
|
|
28
|
+
seen.add(current);
|
|
29
|
+
depth += 1;
|
|
30
|
+
current = tasksById.get(current)?.graph?.parentId;
|
|
31
|
+
}
|
|
32
|
+
return depth;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function evaluateCrewPolicy(input: PolicyEngineInput): PolicyDecision[] {
|
|
36
|
+
const decisions: PolicyDecision[] = [];
|
|
37
|
+
const maxTasksPerRun = input.limits?.maxTasksPerRun;
|
|
38
|
+
if (maxTasksPerRun !== undefined && input.tasks.length > maxTasksPerRun) {
|
|
39
|
+
decisions.push(decision("block", "limit_exceeded", `Run has ${input.tasks.length} tasks, exceeding maxTasksPerRun=${maxTasksPerRun}.`));
|
|
40
|
+
}
|
|
41
|
+
const runningCount = input.tasks.filter((task) => task.status === "running").length;
|
|
42
|
+
if (input.limits?.maxConcurrentWorkers !== undefined && runningCount > input.limits.maxConcurrentWorkers) {
|
|
43
|
+
decisions.push(decision("block", "limit_exceeded", `Run has ${runningCount} running workers, exceeding maxConcurrentWorkers=${input.limits.maxConcurrentWorkers}.`));
|
|
44
|
+
}
|
|
45
|
+
const tasksById = new Map(input.tasks.map((task) => [task.id, task]));
|
|
46
|
+
|
|
47
|
+
for (const task of input.tasks) {
|
|
48
|
+
if (input.limits?.maxChildrenPerTask !== undefined && (task.graph?.children.length ?? 0) > input.limits.maxChildrenPerTask) {
|
|
49
|
+
decisions.push(decision("block", "limit_exceeded", `Task has ${task.graph?.children.length ?? 0} children, exceeding maxChildrenPerTask=${input.limits.maxChildrenPerTask}.`, task.id));
|
|
50
|
+
}
|
|
51
|
+
if (input.limits?.maxTaskDepth !== undefined && taskDepth(task, tasksById) > input.limits.maxTaskDepth) {
|
|
52
|
+
decisions.push(decision("block", "limit_exceeded", `Task graph depth exceeds maxTaskDepth=${input.limits.maxTaskDepth}.`, task.id));
|
|
53
|
+
}
|
|
54
|
+
if (task.status === "failed") {
|
|
55
|
+
const retryCount = task.policy?.retryCount ?? 0;
|
|
56
|
+
const maxRetries = input.limits?.maxRetriesPerTask ?? 0;
|
|
57
|
+
decisions.push(decision(retryCount < maxRetries ? "retry" : "escalate", "task_failed", task.error ? `Task failed: ${task.error}` : "Task failed.", task.id));
|
|
58
|
+
}
|
|
59
|
+
if ((task.status === "running" || task.status === "queued") && task.heartbeat && task.heartbeat.alive !== false && isWorkerHeartbeatStale(task.heartbeat, input.limits?.heartbeatStaleMs ?? 60_000, input.now)) {
|
|
60
|
+
decisions.push(decision("escalate", "worker_stale", "Worker heartbeat is stale.", task.id));
|
|
61
|
+
}
|
|
62
|
+
if (task.taskPacket?.verification) {
|
|
63
|
+
const outcome = evaluateGreenContract(task.taskPacket.verification, task.verification);
|
|
64
|
+
if (!outcome.satisfied && task.status === "completed") {
|
|
65
|
+
decisions.push(decision("block", "green_unsatisfied", `Green contract unsatisfied: required=${outcome.requiredGreenLevel}, observed=${outcome.observedGreenLevel}.`, task.id));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (decisions.length === 0 && input.tasks.length > 0 && input.tasks.every((task) => task.status === "completed")) {
|
|
71
|
+
decisions.push(decision("closeout", "run_complete", "All tasks completed and no policy blockers were found."));
|
|
72
|
+
}
|
|
73
|
+
return decisions;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function summarizePolicyDecisions(decisions: PolicyDecision[]): string[] {
|
|
77
|
+
return decisions.map((item) => `- ${item.action} (${item.reason})${item.taskId ? ` ${item.taskId}` : ""}: ${item.message}`);
|
|
78
|
+
}
|
|
@@ -1,86 +1,86 @@
|
|
|
1
|
-
import type { ChildProcess } from "node:child_process";
|
|
2
|
-
|
|
3
|
-
interface PostExitStdioGuardOptions {
|
|
4
|
-
idleMs: number;
|
|
5
|
-
hardMs: number;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface ChildWithPipedStdio {
|
|
9
|
-
stdout: ChildProcess["stdout"];
|
|
10
|
-
stderr: ChildProcess["stderr"];
|
|
11
|
-
on: ChildProcess["on"];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface ChildWithKill {
|
|
15
|
-
kill(signal?: NodeJS.Signals | number): boolean;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function trySignalChild(child: ChildWithKill, signal: NodeJS.Signals): boolean {
|
|
19
|
-
try {
|
|
20
|
-
return child.kill(signal);
|
|
21
|
-
} catch {
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function attachPostExitStdioGuard(child: ChildWithPipedStdio, options: PostExitStdioGuardOptions): () => void {
|
|
27
|
-
const { idleMs, hardMs } = options;
|
|
28
|
-
let exited = false;
|
|
29
|
-
let stdoutEnded = false;
|
|
30
|
-
let stderrEnded = false;
|
|
31
|
-
let idleTimer: ReturnType<typeof setTimeout> | undefined;
|
|
32
|
-
let hardTimer: ReturnType<typeof setTimeout> | undefined;
|
|
33
|
-
|
|
34
|
-
const destroyUnendedStdio = (): void => {
|
|
35
|
-
if (!stdoutEnded) {
|
|
36
|
-
try {
|
|
37
|
-
child.stdout?.destroy();
|
|
38
|
-
} catch {}
|
|
39
|
-
}
|
|
40
|
-
if (!stderrEnded) {
|
|
41
|
-
try {
|
|
42
|
-
child.stderr?.destroy();
|
|
43
|
-
} catch {}
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const clearTimers = (): void => {
|
|
48
|
-
if (idleTimer) {
|
|
49
|
-
clearTimeout(idleTimer);
|
|
50
|
-
idleTimer = undefined;
|
|
51
|
-
}
|
|
52
|
-
if (hardTimer) {
|
|
53
|
-
clearTimeout(hardTimer);
|
|
54
|
-
hardTimer = undefined;
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const armIdleTimer = () => {
|
|
59
|
-
if (!exited) return;
|
|
60
|
-
if (idleTimer) clearTimeout(idleTimer);
|
|
61
|
-
idleTimer = setTimeout(destroyUnendedStdio, idleMs);
|
|
62
|
-
idleTimer.unref?.();
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
child.stdout?.on("data", armIdleTimer);
|
|
66
|
-
child.stderr?.on("data", armIdleTimer);
|
|
67
|
-
child.stdout?.on("end", () => {
|
|
68
|
-
stdoutEnded = true;
|
|
69
|
-
if (stdoutEnded && stderrEnded) clearTimers();
|
|
70
|
-
});
|
|
71
|
-
child.stderr?.on("end", () => {
|
|
72
|
-
stderrEnded = true;
|
|
73
|
-
if (stdoutEnded && stderrEnded) clearTimers();
|
|
74
|
-
});
|
|
75
|
-
child.on("exit", () => {
|
|
76
|
-
exited = true;
|
|
77
|
-
armIdleTimer();
|
|
78
|
-
if (hardTimer) return;
|
|
79
|
-
hardTimer = setTimeout(destroyUnendedStdio, hardMs);
|
|
80
|
-
hardTimer.unref?.();
|
|
81
|
-
});
|
|
82
|
-
child.on("close", clearTimers);
|
|
83
|
-
child.on("error", clearTimers);
|
|
84
|
-
|
|
85
|
-
return clearTimers;
|
|
86
|
-
}
|
|
1
|
+
import type { ChildProcess } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
interface PostExitStdioGuardOptions {
|
|
4
|
+
idleMs: number;
|
|
5
|
+
hardMs: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface ChildWithPipedStdio {
|
|
9
|
+
stdout: ChildProcess["stdout"];
|
|
10
|
+
stderr: ChildProcess["stderr"];
|
|
11
|
+
on: ChildProcess["on"];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface ChildWithKill {
|
|
15
|
+
kill(signal?: NodeJS.Signals | number): boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function trySignalChild(child: ChildWithKill, signal: NodeJS.Signals): boolean {
|
|
19
|
+
try {
|
|
20
|
+
return child.kill(signal);
|
|
21
|
+
} catch {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function attachPostExitStdioGuard(child: ChildWithPipedStdio, options: PostExitStdioGuardOptions): () => void {
|
|
27
|
+
const { idleMs, hardMs } = options;
|
|
28
|
+
let exited = false;
|
|
29
|
+
let stdoutEnded = false;
|
|
30
|
+
let stderrEnded = false;
|
|
31
|
+
let idleTimer: ReturnType<typeof setTimeout> | undefined;
|
|
32
|
+
let hardTimer: ReturnType<typeof setTimeout> | undefined;
|
|
33
|
+
|
|
34
|
+
const destroyUnendedStdio = (): void => {
|
|
35
|
+
if (!stdoutEnded) {
|
|
36
|
+
try {
|
|
37
|
+
child.stdout?.destroy();
|
|
38
|
+
} catch {}
|
|
39
|
+
}
|
|
40
|
+
if (!stderrEnded) {
|
|
41
|
+
try {
|
|
42
|
+
child.stderr?.destroy();
|
|
43
|
+
} catch {}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const clearTimers = (): void => {
|
|
48
|
+
if (idleTimer) {
|
|
49
|
+
clearTimeout(idleTimer);
|
|
50
|
+
idleTimer = undefined;
|
|
51
|
+
}
|
|
52
|
+
if (hardTimer) {
|
|
53
|
+
clearTimeout(hardTimer);
|
|
54
|
+
hardTimer = undefined;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const armIdleTimer = () => {
|
|
59
|
+
if (!exited) return;
|
|
60
|
+
if (idleTimer) clearTimeout(idleTimer);
|
|
61
|
+
idleTimer = setTimeout(destroyUnendedStdio, idleMs);
|
|
62
|
+
idleTimer.unref?.();
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
child.stdout?.on("data", armIdleTimer);
|
|
66
|
+
child.stderr?.on("data", armIdleTimer);
|
|
67
|
+
child.stdout?.on("end", () => {
|
|
68
|
+
stdoutEnded = true;
|
|
69
|
+
if (stdoutEnded && stderrEnded) clearTimers();
|
|
70
|
+
});
|
|
71
|
+
child.stderr?.on("end", () => {
|
|
72
|
+
stderrEnded = true;
|
|
73
|
+
if (stdoutEnded && stderrEnded) clearTimers();
|
|
74
|
+
});
|
|
75
|
+
child.on("exit", () => {
|
|
76
|
+
exited = true;
|
|
77
|
+
armIdleTimer();
|
|
78
|
+
if (hardTimer) return;
|
|
79
|
+
hardTimer = setTimeout(destroyUnendedStdio, hardMs);
|
|
80
|
+
hardTimer.unref?.();
|
|
81
|
+
});
|
|
82
|
+
child.on("close", clearTimers);
|
|
83
|
+
child.on("error", clearTimers);
|
|
84
|
+
|
|
85
|
+
return clearTimers;
|
|
86
|
+
}
|
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
import type { CrewAgentRecord } from "./crew-agent-runtime.ts";
|
|
2
|
-
import type { TeamRunManifest } from "../state/types.ts";
|
|
3
|
-
export { hasAsyncStartMarker } from "./async-marker.ts";
|
|
4
|
-
|
|
5
|
-
export interface ProcessLiveness {
|
|
6
|
-
pid?: number;
|
|
7
|
-
alive: boolean;
|
|
8
|
-
detail: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const ORPHANED_ACTIVE_RUN_MS = 10 * 60 * 1000;
|
|
12
|
-
|
|
13
|
-
export function checkProcessLiveness(pid: number | undefined): ProcessLiveness {
|
|
14
|
-
if (pid === undefined || !Number.isInteger(pid) || pid <= 0) {
|
|
15
|
-
return { pid, alive: false, detail: "no pid recorded" };
|
|
16
|
-
}
|
|
17
|
-
try {
|
|
18
|
-
process.kill(pid, 0);
|
|
19
|
-
return { pid, alive: true, detail: "process is alive" };
|
|
20
|
-
} catch (error) {
|
|
21
|
-
const nodeError = error as NodeJS.ErrnoException;
|
|
22
|
-
if (nodeError.code === "EPERM") return { pid, alive: true, detail: "process exists but permission is denied" };
|
|
23
|
-
if (nodeError.code === "ESRCH") return { pid, alive: false, detail: "process does not exist" };
|
|
24
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
25
|
-
return { pid, alive: false, detail: message };
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function isActiveRunStatus(status: string): boolean {
|
|
30
|
-
return status === "queued" || status === "planning" || status === "running";
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function isLikelyOrphanedActiveRun(run: TeamRunManifest, agents: CrewAgentRecord[] = [], now = Date.now(), staleMs = ORPHANED_ACTIVE_RUN_MS): boolean {
|
|
34
|
-
if (!isActiveRunStatus(run.status)) return false;
|
|
35
|
-
if (run.async?.pid !== undefined) return false;
|
|
36
|
-
const updatedAt = new Date(run.updatedAt).getTime();
|
|
37
|
-
if (!Number.isFinite(updatedAt) || now - updatedAt < staleMs) return false;
|
|
38
|
-
if (agents.length === 0) return run.summary === "Creating workflow prompts and placeholder results.";
|
|
39
|
-
return agents.every((agent) => agent.status === "queued" && !agent.completedAt && !agent.progress);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function hasDurableActiveAgentEvidence(agent: CrewAgentRecord): boolean {
|
|
43
|
-
if (agent.status !== "running" && agent.status !== "queued") return false;
|
|
44
|
-
return Boolean(agent.statusPath || agent.eventsPath || agent.outputPath || agent.progress || agent.toolUses || agent.jsonEvents);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function hasStaleAsyncProcess(run: TeamRunManifest): boolean {
|
|
48
|
-
if (!isActiveRunStatus(run.status) || !run.async) return false;
|
|
49
|
-
return !checkProcessLiveness(run.async.pid).alive;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export function isDisplayActiveRun(run: TeamRunManifest, agents: CrewAgentRecord[] = [], now = Date.now()): boolean {
|
|
53
|
-
if (!isActiveRunStatus(run.status) || hasStaleAsyncProcess(run) || isLikelyOrphanedActiveRun(run, agents, now)) return false;
|
|
54
|
-
if (agents.length === 0) return true;
|
|
55
|
-
return agents.some(hasDurableActiveAgentEvidence);
|
|
56
|
-
}
|
|
1
|
+
import type { CrewAgentRecord } from "./crew-agent-runtime.ts";
|
|
2
|
+
import type { TeamRunManifest } from "../state/types.ts";
|
|
3
|
+
export { hasAsyncStartMarker } from "./async-marker.ts";
|
|
4
|
+
|
|
5
|
+
export interface ProcessLiveness {
|
|
6
|
+
pid?: number;
|
|
7
|
+
alive: boolean;
|
|
8
|
+
detail: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const ORPHANED_ACTIVE_RUN_MS = 10 * 60 * 1000;
|
|
12
|
+
|
|
13
|
+
export function checkProcessLiveness(pid: number | undefined): ProcessLiveness {
|
|
14
|
+
if (pid === undefined || !Number.isInteger(pid) || pid <= 0) {
|
|
15
|
+
return { pid, alive: false, detail: "no pid recorded" };
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
process.kill(pid, 0);
|
|
19
|
+
return { pid, alive: true, detail: "process is alive" };
|
|
20
|
+
} catch (error) {
|
|
21
|
+
const nodeError = error as NodeJS.ErrnoException;
|
|
22
|
+
if (nodeError.code === "EPERM") return { pid, alive: true, detail: "process exists but permission is denied" };
|
|
23
|
+
if (nodeError.code === "ESRCH") return { pid, alive: false, detail: "process does not exist" };
|
|
24
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
25
|
+
return { pid, alive: false, detail: message };
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function isActiveRunStatus(status: string): boolean {
|
|
30
|
+
return status === "queued" || status === "planning" || status === "running";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function isLikelyOrphanedActiveRun(run: TeamRunManifest, agents: CrewAgentRecord[] = [], now = Date.now(), staleMs = ORPHANED_ACTIVE_RUN_MS): boolean {
|
|
34
|
+
if (!isActiveRunStatus(run.status)) return false;
|
|
35
|
+
if (run.async?.pid !== undefined) return false;
|
|
36
|
+
const updatedAt = new Date(run.updatedAt).getTime();
|
|
37
|
+
if (!Number.isFinite(updatedAt) || now - updatedAt < staleMs) return false;
|
|
38
|
+
if (agents.length === 0) return run.summary === "Creating workflow prompts and placeholder results.";
|
|
39
|
+
return agents.every((agent) => agent.status === "queued" && !agent.completedAt && !agent.progress);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function hasDurableActiveAgentEvidence(agent: CrewAgentRecord): boolean {
|
|
43
|
+
if (agent.status !== "running" && agent.status !== "queued") return false;
|
|
44
|
+
return Boolean(agent.statusPath || agent.eventsPath || agent.outputPath || agent.progress || agent.toolUses || agent.jsonEvents);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function hasStaleAsyncProcess(run: TeamRunManifest): boolean {
|
|
48
|
+
if (!isActiveRunStatus(run.status) || !run.async) return false;
|
|
49
|
+
return !checkProcessLiveness(run.async.pid).alive;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function isDisplayActiveRun(run: TeamRunManifest, agents: CrewAgentRecord[] = [], now = Date.now()): boolean {
|
|
53
|
+
if (!isActiveRunStatus(run.status) || hasStaleAsyncProcess(run) || isLikelyOrphanedActiveRun(run, agents, now)) return false;
|
|
54
|
+
if (agents.length === 0) return true;
|
|
55
|
+
return agents.some(hasDurableActiveAgentEvidence);
|
|
56
|
+
}
|
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
export interface ProgressEventSummary {
|
|
2
|
-
eventType: string;
|
|
3
|
-
currentTool?: string;
|
|
4
|
-
toolCount?: number;
|
|
5
|
-
tokens?: number;
|
|
6
|
-
turns?: number;
|
|
7
|
-
activityState?: string;
|
|
8
|
-
lastActivityAt?: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface ProgressEventCoalesceDecision {
|
|
12
|
-
shouldAppend: boolean;
|
|
13
|
-
reason: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface ProgressEventCoalesceInput {
|
|
17
|
-
previous?: ProgressEventSummary;
|
|
18
|
-
next: ProgressEventSummary;
|
|
19
|
-
nowMs: number;
|
|
20
|
-
lastAppendMs?: number;
|
|
21
|
-
minIntervalMs: number;
|
|
22
|
-
force?: boolean;
|
|
23
|
-
tokenThreshold?: number;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const DEFAULT_TOKEN_THRESHOLD = 256;
|
|
27
|
-
|
|
28
|
-
function numericIncrease(previous: number | undefined, next: number | undefined): number {
|
|
29
|
-
return next !== undefined && previous !== undefined ? next - previous : next !== undefined ? next : 0;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function shouldAppendProgressEventUpdate(input: ProgressEventCoalesceInput): ProgressEventCoalesceDecision {
|
|
33
|
-
if (input.force) return { shouldAppend: true, reason: "force" };
|
|
34
|
-
if (!input.previous) return { shouldAppend: true, reason: "first" };
|
|
35
|
-
if (input.previous.activityState !== input.next.activityState) return { shouldAppend: true, reason: "activity_changed" };
|
|
36
|
-
if (input.previous.currentTool !== input.next.currentTool) return { shouldAppend: true, reason: "tool_changed" };
|
|
37
|
-
if (numericIncrease(input.previous.toolCount, input.next.toolCount) > 0) return { shouldAppend: true, reason: "tool_count_increased" };
|
|
38
|
-
if (numericIncrease(input.previous.turns, input.next.turns) > 0) return { shouldAppend: true, reason: "turns_increased" };
|
|
39
|
-
const tokenIncrease = numericIncrease(input.previous.tokens, input.next.tokens);
|
|
40
|
-
if (tokenIncrease >= (input.tokenThreshold ?? DEFAULT_TOKEN_THRESHOLD)) return { shouldAppend: true, reason: "tokens_increased" };
|
|
41
|
-
if (input.lastAppendMs === undefined || input.nowMs - input.lastAppendMs >= input.minIntervalMs) return { shouldAppend: true, reason: "interval" };
|
|
42
|
-
return { shouldAppend: false, reason: "coalesced" };
|
|
43
|
-
}
|
|
1
|
+
export interface ProgressEventSummary {
|
|
2
|
+
eventType: string;
|
|
3
|
+
currentTool?: string;
|
|
4
|
+
toolCount?: number;
|
|
5
|
+
tokens?: number;
|
|
6
|
+
turns?: number;
|
|
7
|
+
activityState?: string;
|
|
8
|
+
lastActivityAt?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ProgressEventCoalesceDecision {
|
|
12
|
+
shouldAppend: boolean;
|
|
13
|
+
reason: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ProgressEventCoalesceInput {
|
|
17
|
+
previous?: ProgressEventSummary;
|
|
18
|
+
next: ProgressEventSummary;
|
|
19
|
+
nowMs: number;
|
|
20
|
+
lastAppendMs?: number;
|
|
21
|
+
minIntervalMs: number;
|
|
22
|
+
force?: boolean;
|
|
23
|
+
tokenThreshold?: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const DEFAULT_TOKEN_THRESHOLD = 256;
|
|
27
|
+
|
|
28
|
+
function numericIncrease(previous: number | undefined, next: number | undefined): number {
|
|
29
|
+
return next !== undefined && previous !== undefined ? next - previous : next !== undefined ? next : 0;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function shouldAppendProgressEventUpdate(input: ProgressEventCoalesceInput): ProgressEventCoalesceDecision {
|
|
33
|
+
if (input.force) return { shouldAppend: true, reason: "force" };
|
|
34
|
+
if (!input.previous) return { shouldAppend: true, reason: "first" };
|
|
35
|
+
if (input.previous.activityState !== input.next.activityState) return { shouldAppend: true, reason: "activity_changed" };
|
|
36
|
+
if (input.previous.currentTool !== input.next.currentTool) return { shouldAppend: true, reason: "tool_changed" };
|
|
37
|
+
if (numericIncrease(input.previous.toolCount, input.next.toolCount) > 0) return { shouldAppend: true, reason: "tool_count_increased" };
|
|
38
|
+
if (numericIncrease(input.previous.turns, input.next.turns) > 0) return { shouldAppend: true, reason: "turns_increased" };
|
|
39
|
+
const tokenIncrease = numericIncrease(input.previous.tokens, input.next.tokens);
|
|
40
|
+
if (tokenIncrease >= (input.tokenThreshold ?? DEFAULT_TOKEN_THRESHOLD)) return { shouldAppend: true, reason: "tokens_increased" };
|
|
41
|
+
if (input.lastAppendMs === undefined || input.nowMs - input.lastAppendMs >= input.minIntervalMs) return { shouldAppend: true, reason: "interval" };
|
|
42
|
+
return { shouldAppend: false, reason: "coalesced" };
|
|
43
|
+
}
|