pi-crew 0.9.68 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (217) hide show
  1. package/CHANGELOG.md +124 -0
  2. package/agents/analyst.md +1 -1
  3. package/agents/cold-verifier.md +3 -1
  4. package/agents/critic.md +1 -1
  5. package/agents/executor.md +1 -1
  6. package/agents/explorer.md +1 -1
  7. package/agents/planner.md +1 -1
  8. package/agents/reviewer.md +1 -1
  9. package/agents/security-reviewer.md +1 -1
  10. package/agents/test-engineer.md +1 -1
  11. package/agents/verifier.md +1 -1
  12. package/agents/writer.md +1 -1
  13. package/dist/index.mjs +65838 -60331
  14. package/docs/README.md +2 -0
  15. package/docs/actions-reference.md +31 -0
  16. package/docs/commands-reference.md +17 -6
  17. package/docs/resource-formats.md +13 -0
  18. package/package.json +4 -2
  19. package/schema.json +15 -1
  20. package/scripts/resource-sampler.mjs +36 -2
  21. package/skills/requirements-to-task-packet/SKILL.md +26 -0
  22. package/skills/widget-rendering/SKILL.md +7 -7
  23. package/src/agents/agent-config.ts +2 -1
  24. package/src/agents/discover-agents.ts +23 -14
  25. package/src/config/config-merge.ts +183 -0
  26. package/src/config/config-validation.ts +681 -0
  27. package/src/config/config.ts +22 -864
  28. package/src/config/defaults.ts +35 -1
  29. package/src/config/drift-detector.ts +1 -1
  30. package/src/config/env-vars.ts +691 -0
  31. package/src/config/role-tools.ts +8 -8
  32. package/src/config/sanitize-project-config.ts +172 -0
  33. package/src/config/types.ts +30 -0
  34. package/src/extension/async-notifier.ts +25 -2
  35. package/src/extension/crew-vibes/config.ts +2 -1
  36. package/src/extension/plan-orchestrate.ts +132 -0
  37. package/src/extension/registration/commands/dashboard.ts +158 -0
  38. package/src/extension/registration/commands/index.ts +35 -0
  39. package/src/extension/registration/commands/manage.ts +303 -0
  40. package/src/extension/registration/commands/run.ts +214 -0
  41. package/src/extension/registration/commands/shared.ts +631 -0
  42. package/src/extension/registration/commands/status.ts +60 -0
  43. package/src/extension/registration/commands.ts +13 -1224
  44. package/src/extension/registration/lifecycle-handlers.ts +126 -15
  45. package/src/extension/registration/subagent-tools.ts +218 -9
  46. package/src/extension/rpc-hmac.ts +5 -3
  47. package/src/extension/team-tool/api/heartbeat.ts +47 -10
  48. package/src/extension/team-tool/api/plan-approval.ts +9 -0
  49. package/src/extension/team-tool/api/task-claims.ts +109 -40
  50. package/src/extension/team-tool/cancel.ts +84 -50
  51. package/src/extension/team-tool/dispatch/index.ts +1 -0
  52. package/src/extension/team-tool/dispatch/run.ts +4 -1
  53. package/src/extension/team-tool/doctor.ts +103 -1
  54. package/src/extension/team-tool/orchestrate.ts +66 -1
  55. package/src/extension/team-tool/plans.ts +192 -0
  56. package/src/extension/team-tool/respond.ts +197 -65
  57. package/src/extension/team-tool/run-deadline.ts +16 -1
  58. package/src/extension/team-tool/run-intent.ts +63 -0
  59. package/src/extension/team-tool/run.ts +48 -18
  60. package/src/extension/team-tool/status.ts +84 -26
  61. package/src/extension/team-tool.ts +11 -2
  62. package/src/hooks/registry.ts +1 -6
  63. package/src/i18n.ts +9 -0
  64. package/src/prompt/prompt-runtime.ts +521 -2
  65. package/src/prompt/worker-events-channel.ts +173 -0
  66. package/src/runtime/README.md +8 -8
  67. package/src/runtime/async-runner.ts +7 -3
  68. package/src/runtime/background-runner.ts +42 -14
  69. package/src/runtime/broker/broker-issuer.ts +9 -2
  70. package/src/runtime/broker/crew-broker-tokens.ts +43 -6
  71. package/src/runtime/broker/crew-broker.ts +838 -10
  72. package/src/runtime/broker/wait-status-cache.ts +157 -0
  73. package/src/runtime/budget-enforcement.ts +281 -0
  74. package/src/runtime/child-pi/child-pi-spawn.ts +60 -14
  75. package/src/runtime/child-pi/child-pi-timers.ts +324 -0
  76. package/src/runtime/child-pi/child-pi.ts +97 -201
  77. package/src/runtime/child-pi/mock-fixtures.ts +16 -2
  78. package/src/runtime/crew-agent-records.ts +259 -14
  79. package/src/runtime/delegate-spawn.ts +148 -0
  80. package/src/runtime/deterministic-ast.ts +2 -1
  81. package/src/runtime/dispatch-batch.ts +945 -0
  82. package/src/runtime/finalize-run.ts +557 -0
  83. package/src/runtime/goal-workflow/adaptive-plan.ts +87 -13
  84. package/src/runtime/goal-workflow/dynamic-workflow-runner.ts +3 -2
  85. package/src/runtime/goal-workflow/goal-state-store.ts +1 -1
  86. package/src/runtime/group-join.ts +11 -125
  87. package/src/runtime/live-session/live-session-runtime.ts +26 -1
  88. package/src/runtime/merge-gate.ts +7 -1
  89. package/src/runtime/merge-loop.ts +130 -0
  90. package/src/runtime/model/model-budget-summary.ts +53 -0
  91. package/src/runtime/model/model-fallback.ts +32 -2
  92. package/src/runtime/model/pi-args.ts +10 -0
  93. package/src/runtime/model/provider-extensions.ts +10 -0
  94. package/src/runtime/orphan-worker-registry.ts +1 -1
  95. package/src/runtime/output/output-validator.ts +45 -0
  96. package/src/runtime/parent-guard.ts +3 -1
  97. package/src/runtime/peer-dep.ts +2 -1
  98. package/src/runtime/per-write-validator.ts +0 -5
  99. package/src/runtime/pi-spawn.ts +61 -15
  100. package/src/runtime/plan-approval.ts +125 -0
  101. package/src/runtime/plan-replan.ts +151 -0
  102. package/src/runtime/recovery/checkpoint.ts +0 -18
  103. package/src/runtime/recovery/crash-recovery.ts +95 -46
  104. package/src/runtime/scheduler-context.ts +98 -0
  105. package/src/runtime/scheduling/coalesce-tasks.ts +5 -0
  106. package/src/runtime/scheduling/global-worker-cap.ts +2 -1
  107. package/src/runtime/scheduling/nested-slots.ts +70 -0
  108. package/src/runtime/scheduling/run-coalesced-task-group.ts +64 -13
  109. package/src/runtime/scheduling/task-graph-scheduler.ts +0 -10
  110. package/src/runtime/settings-store.ts +219 -0
  111. package/src/runtime/spawn-policy.ts +217 -0
  112. package/src/runtime/stale-reconciler.ts +87 -6
  113. package/src/runtime/subagent-manager.ts +25 -1
  114. package/src/runtime/task-output-context.ts +230 -9
  115. package/src/runtime/task-packet.ts +23 -1
  116. package/src/runtime/task-runner/child-executor.ts +106 -7
  117. package/src/runtime/task-runner/post-execution.ts +125 -1
  118. package/src/runtime/task-runner/pre-execution.ts +39 -1
  119. package/src/runtime/task-runner/prompt-builder.ts +51 -1
  120. package/src/runtime/task-runner/retrieval-orchestrator.ts +72 -18
  121. package/src/runtime/task-runner/spec-evidence.ts +403 -0
  122. package/src/runtime/task-runner/state-helpers.ts +26 -24
  123. package/src/runtime/task-runner.ts +11 -0
  124. package/src/runtime/team-runner.ts +129 -1671
  125. package/src/runtime/verification/spec-sandbox.ts +255 -0
  126. package/src/runtime/verification/verification-gates.ts +3 -2
  127. package/src/runtime/verification/verification-worktree.ts +2 -1
  128. package/src/runtime/workflow-phase-advance.ts +100 -0
  129. package/src/runtime/workspace-tree.ts +9 -0
  130. package/src/schema/config-schema.ts +65 -25
  131. package/src/schema/sensitive-config-paths.ts +64 -0
  132. package/src/schema/team-tool-schema.ts +13 -3
  133. package/src/state/README.md +4 -10
  134. package/src/state/atomic-write.ts +20 -3
  135. package/src/state/contracts.ts +38 -0
  136. package/src/state/coordination/mailbox.ts +12 -2
  137. package/src/state/event-log/cursor.ts +223 -0
  138. package/src/state/event-log/event-log-rotation.ts +12 -4
  139. package/src/state/event-log/event-log.ts +152 -369
  140. package/src/state/event-log/sequence-cache.ts +373 -0
  141. package/src/state/event-log/worker-atomic-writer.ts +2 -1
  142. package/src/state/stores/active-run-registry.ts +3 -2
  143. package/src/state/stores/manifest-io.ts +237 -0
  144. package/src/state/stores/ownership-map.ts +162 -0
  145. package/src/state/stores/plan-store.ts +241 -0
  146. package/src/state/stores/run-cache.ts +0 -90
  147. package/src/state/stores/spec-store.ts +189 -0
  148. package/src/state/stores/state-store.ts +66 -229
  149. package/src/state/types.ts +197 -0
  150. package/src/ui/dashboard-panes/plan-pane.ts +136 -0
  151. package/src/ui/dashboard-panes/progress-pane.ts +6 -0
  152. package/src/ui/dashboard-panes/transcript-pane.ts +31 -0
  153. package/src/ui/heartbeat-aggregator.ts +9 -1
  154. package/src/ui/keybinding-map.ts +54 -13
  155. package/src/ui/powerbar-publisher.ts +52 -1
  156. package/src/ui/run-dashboard.ts +31 -5
  157. package/src/ui/run-snapshot-cache.ts +57 -30
  158. package/src/ui/snapshot-types.ts +6 -1
  159. package/src/ui/widget/widget-renderer.ts +9 -1
  160. package/src/utils/file-coalescer.ts +0 -4
  161. package/src/utils/fs-errno.ts +66 -0
  162. package/src/utils/fs-watch.ts +1 -1
  163. package/src/utils/internal-error.ts +3 -1
  164. package/src/utils/paths.ts +11 -3
  165. package/src/utils/task-name-generator.ts +1 -8
  166. package/src/workflows/discover-workflows.ts +19 -2
  167. package/src/workflows/validate-workflow.ts +7 -1
  168. package/src/workflows/workflow-config.ts +10 -0
  169. package/src/workflows/workflow-serializer.ts +3 -0
  170. package/src/worktree/worktree-manager.ts +22 -0
  171. package/src/agents/agent-search.ts +0 -98
  172. package/src/benchmark/benchmark-runner.ts +0 -313
  173. package/src/benchmark/feedback-loop.ts +0 -73
  174. package/src/config/resilient-parser.ts +0 -117
  175. package/src/extension/crew-vibes/cat-frames.ts +0 -18
  176. package/src/extension/result-watcher.ts +0 -139
  177. package/src/observability/exporters/prometheus-exporter.ts +0 -54
  178. package/src/observability/metric-retention.ts +0 -64
  179. package/src/runtime/compaction/compaction-summary.ts +0 -278
  180. package/src/runtime/errors/crew-errors.ts +0 -162
  181. package/src/runtime/live-session/intercom-bridge.ts +0 -187
  182. package/src/runtime/loop-gates.ts +0 -128
  183. package/src/runtime/metric-parser.ts +0 -36
  184. package/src/runtime/output/stream-preview.ts +0 -184
  185. package/src/runtime/output/tool-progress.ts +0 -278
  186. package/src/runtime/phase-tracker.ts +0 -385
  187. package/src/runtime/pipeline-runner.ts +0 -523
  188. package/src/runtime/process/process-lifecycle.ts +0 -491
  189. package/src/runtime/recovery/retry-runner.ts +0 -330
  190. package/src/runtime/run-drift.ts +0 -219
  191. package/src/runtime/task-quality.ts +0 -199
  192. package/src/runtime/task-runner/run-projection.ts +0 -128
  193. package/src/runtime/verification/post-checks.ts +0 -142
  194. package/src/state/coordination/schedule.ts +0 -166
  195. package/src/state/event-log/jsonl-writer.ts +0 -115
  196. package/src/state/hook-instinct-bridge.ts +0 -94
  197. package/src/state/hook-integrations.ts +0 -51
  198. package/src/state/session-state-map.ts +0 -51
  199. package/src/state/stores/blob-store.ts +0 -308
  200. package/src/state/stores/instinct-store.ts +0 -275
  201. package/src/state/stores/observation-store.ts +0 -176
  202. package/src/state/tiered-eval.ts +0 -480
  203. package/src/state/types-eval.ts +0 -58
  204. package/src/tools/safe-bash-extension.ts +0 -54
  205. package/src/tools/safe-bash.ts +0 -505
  206. package/src/ui/agent-management-overlay.ts +0 -160
  207. package/src/ui/crew-footer.ts +0 -102
  208. package/src/ui/crew-select-list.ts +0 -114
  209. package/src/ui/dashboard-panes/capability-pane.ts +0 -77
  210. package/src/ui/transcript-entries.ts +0 -256
  211. package/src/utils/conflict-detect.ts +0 -721
  212. package/src/utils/fingerprint.ts +0 -180
  213. package/src/utils/gh-protocol.ts +0 -556
  214. package/src/utils/project-detector.ts +0 -160
  215. package/src/utils/sse-parser.ts +0 -131
  216. package/src/workflows/cost-estimator.ts +0 -34
  217. package/src/workflows/intermediate-store.ts +0 -166
@@ -1,117 +0,0 @@
1
- /**
2
- * Per-field resilient config parsing — continues on error, collects all field-level issues.
3
- */
4
-
5
- import type { TSchema } from "@sinclair/typebox";
6
- import { Value } from "@sinclair/typebox/value";
7
- import { PiTeamsConfigSchema } from "../schema/config-schema.ts";
8
- import { type PiTeamsConfig, parseConfig } from "./config.ts";
9
- import { suggestConfigKey } from "./suggestions.ts";
10
-
11
- // ---------------------------------------------------------------------------
12
- // Types
13
- // ---------------------------------------------------------------------------
14
-
15
- export interface FieldError {
16
- field: string;
17
- message: string;
18
- value: unknown;
19
- suggestion?: string;
20
- }
21
-
22
- export interface ResilientParseResult {
23
- config: Record<string, unknown>;
24
- errors: FieldError[];
25
- warnings: string[];
26
- valid: boolean;
27
- }
28
-
29
- // ---------------------------------------------------------------------------
30
- // Helpers
31
- // ---------------------------------------------------------------------------
32
-
33
- const KNOWN_TOP_LEVEL_KEYS: readonly string[] = Object.keys(PiTeamsConfigSchema.properties ?? {});
34
-
35
- function isObject(value: unknown): value is Record<string, unknown> {
36
- return value !== null && typeof value === "object" && !Array.isArray(value);
37
- }
38
-
39
- // ---------------------------------------------------------------------------
40
- // Implementation
41
- // ---------------------------------------------------------------------------
42
-
43
- /**
44
- * Parse a raw config value field-by-field.
45
- *
46
- * - Each top-level key is validated independently via TypeBox sub-schemas.
47
- * - Fields that fail are recorded as errors but do not block others.
48
- * - Unknown keys get a fuzzy suggestion via Levenshtein distance.
49
- * - The returned `config` contains only successfully-parsed fields (via `parseConfig`).
50
- */
51
- export function parseConfigResilient(raw: unknown): ResilientParseResult {
52
- const errors: FieldError[] = [];
53
- const warnings: string[] = [];
54
-
55
- if (!isObject(raw)) {
56
- return {
57
- config: {},
58
- errors: [
59
- {
60
- field: "config",
61
- message: "config must be an object",
62
- value: raw,
63
- },
64
- ],
65
- warnings,
66
- valid: false,
67
- };
68
- }
69
-
70
- // Collect unknown top-level keys with suggestions
71
- for (const key of Object.keys(raw)) {
72
- if (!KNOWN_TOP_LEVEL_KEYS.includes(key)) {
73
- const suggestion = suggestConfigKey(key, KNOWN_TOP_LEVEL_KEYS);
74
- errors.push({
75
- field: key,
76
- message: `unknown config key '${key}'`,
77
- value: raw[key],
78
- suggestion: suggestion ?? undefined,
79
- });
80
- }
81
- }
82
-
83
- // Use TypeBox per-field validation for known keys
84
- for (const key of KNOWN_TOP_LEVEL_KEYS) {
85
- if (!(key in raw)) continue;
86
- const value = raw[key];
87
- const subSchema = (PiTeamsConfigSchema.properties as Record<string, unknown>)[key];
88
- if (subSchema && !Value.Check(subSchema as TSchema, value)) {
89
- // Collect ALL errors for this field
90
- const fieldErrors = [...Value.Errors(subSchema as TSchema, value)];
91
- for (const err of fieldErrors) {
92
- const errObj = err as unknown as Record<string, unknown>;
93
- const msg = typeof errObj.message === "string" ? errObj.message : "invalid value";
94
- errors.push({
95
- field: key,
96
- message: msg,
97
- value,
98
- });
99
- }
100
- }
101
- }
102
-
103
- // Build the valid subset using existing parseConfig (which silently drops invalid fields)
104
- const parsed: PiTeamsConfig = parseConfig(raw);
105
- const config: Record<string, unknown> = {};
106
- for (const key of Object.keys(parsed)) {
107
- const val = (parsed as Record<string, unknown>)[key];
108
- if (val !== undefined) config[key] = val;
109
- }
110
-
111
- return {
112
- config,
113
- errors,
114
- warnings,
115
- valid: errors.length === 0,
116
- };
117
- }
@@ -1,18 +0,0 @@
1
- /**
2
- * ANSI art cat animation frames extracted from runner-spritesheet.png.
3
- * Used when PUA font glyphs are not available (web terminals like gotty).
4
- * Each frame is an array of strings (one per line).
5
- */
6
- export const CAT_FRAMES: readonly string[][] = [
7
- // Frame 0 — standing
8
- ["▄▄ ███▄ ", "▀▀█████▄", " ██▀██ ", " ▀▀ ▀▀"],
9
- // Frame 1 — step 1
10
- [" ████ ", " █████▄", "▄██████▀", " ██ ▀█▄"],
11
- // Frame 2 — step 2
12
- [" ▄██████", " █████▀ ", "███████ ", "▀██████ "],
13
- // Frame 3 — step 3
14
- [" ████ ", "▄█████▄ ", "▀██████▄", "▄█▀ ██ "],
15
- ] as const;
16
-
17
- /** Number of animation frames. */
18
- export const CAT_FRAME_COUNT = CAT_FRAMES.length;
@@ -1,139 +0,0 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import { buildCompletionKey, getGlobalSeenMap, markSeenWithTtl } from "../utils/completion-dedupe.ts";
4
- import { createFileCoalescer } from "../utils/file-coalescer.ts";
5
- import { closeWatcher, watchWithErrorHandler } from "../utils/fs-watch.ts";
6
- import { logInternalError } from "../utils/internal-error.ts";
7
-
8
- export interface ResultWatcherEvents {
9
- emit(event: string, data: unknown): void;
10
- }
11
-
12
- export interface ResultWatcherHandle {
13
- start(): void;
14
- prime(): void;
15
- stop(): void;
16
- }
17
-
18
- interface ResultWatcherDependencies {
19
- watch?: typeof watchWithErrorHandler;
20
- }
21
-
22
- export interface ResultWatcherOptions extends ResultWatcherDependencies {
23
- eventName?: string;
24
- completionTtlMs?: number;
25
- isCurrent?: () => boolean;
26
- }
27
-
28
- const RESULT_WATCHER_RESTART_MS = 3000;
29
- const RESULT_WATCHER_POLL_MS = 1000;
30
-
31
- function shouldFallBackToPolling(error: unknown): boolean {
32
- const code = error && typeof error === "object" ? (error as { code?: unknown }).code : undefined;
33
- return code === "EMFILE" || code === "ENOSPC" || code === "EPERM";
34
- }
35
-
36
- function readJson(filePath: string): unknown | undefined {
37
- try {
38
- return JSON.parse(fs.readFileSync(filePath, "utf-8")) as unknown;
39
- } catch (error) {
40
- logInternalError("result-watcher.parse", error, `filePath=${filePath}`);
41
- return undefined;
42
- }
43
- }
44
-
45
- export function createResultWatcher(
46
- events: ResultWatcherEvents,
47
- resultsDir: string,
48
- eventNameOrOptions: string | ResultWatcherOptions = "pi-crew:run-result",
49
- ): ResultWatcherHandle {
50
- const options: ResultWatcherOptions = typeof eventNameOrOptions === "string" ? { eventName: eventNameOrOptions } : eventNameOrOptions;
51
- const eventName = options.eventName ?? "pi-crew:run-result";
52
- const completionTtlMs = options.completionTtlMs ?? 5 * 60_000;
53
- const watch = options.watch ?? watchWithErrorHandler;
54
- const isCurrent = options.isCurrent ?? (() => true);
55
- const seen = getGlobalSeenMap("pi-crew.result-watcher");
56
- let watcher: fs.FSWatcher | null | undefined;
57
- let restartTimer: ReturnType<typeof setTimeout> | undefined;
58
- let pollTimer: ReturnType<typeof setInterval> | undefined;
59
- const coalescer = createFileCoalescer((file) => {
60
- if (!isCurrent()) return;
61
- const filePath = path.join(resultsDir, file);
62
- if (!file.endsWith(".json") || !fs.existsSync(filePath)) return;
63
- const payload = readJson(filePath);
64
- if (payload === undefined) {
65
- coalescer.schedule(file, RESULT_WATCHER_POLL_MS);
66
- return;
67
- }
68
- const key = buildCompletionKey(
69
- payload && typeof payload === "object" && !Array.isArray(payload) ? (payload as Record<string, unknown>) : {},
70
- `file:${file}`,
71
- );
72
- if (!markSeenWithTtl(seen, key, Date.now(), completionTtlMs)) {
73
- events.emit(eventName, payload);
74
- }
75
- try {
76
- fs.unlinkSync(filePath);
77
- } catch (error) {
78
- logInternalError("result-watcher.unlink", error, `filePath=${filePath}`);
79
- }
80
- }, 50);
81
- const poll = () => {
82
- if (!isCurrent() || !fs.existsSync(resultsDir)) return;
83
- for (const file of fs.readdirSync(resultsDir).filter((entry) => entry.endsWith(".json"))) coalescer.schedule(file, 0);
84
- };
85
- const startPolling = () => {
86
- if (pollTimer) return;
87
- pollTimer = setInterval(poll, RESULT_WATCHER_POLL_MS);
88
- pollTimer.unref();
89
- poll();
90
- };
91
- const stopPolling = () => {
92
- if (pollTimer) clearInterval(pollTimer);
93
- pollTimer = undefined;
94
- };
95
- const scheduleRestart = (error?: unknown) => {
96
- if (shouldFallBackToPolling(error)) startPolling();
97
- if (restartTimer) clearTimeout(restartTimer);
98
- restartTimer = setTimeout(() => {
99
- restartTimer = undefined;
100
- try {
101
- if (!isCurrent()) return;
102
- fs.mkdirSync(resultsDir, { recursive: true });
103
- handle.start();
104
- } catch (error) {
105
- logInternalError("result-watcher.restart", error, `resultsDir=${resultsDir}`);
106
- }
107
- }, RESULT_WATCHER_RESTART_MS);
108
- restartTimer.unref();
109
- };
110
- const handle: ResultWatcherHandle = {
111
- start() {
112
- if (!isCurrent()) return;
113
- fs.mkdirSync(resultsDir, { recursive: true });
114
- if (watcher) closeWatcher(watcher);
115
- watcher = watch(
116
- resultsDir,
117
- (event, fileName) => {
118
- if (event !== "rename" || !fileName) return;
119
- coalescer.schedule(fileName.toString());
120
- },
121
- scheduleRestart,
122
- );
123
- if (watcher) stopPolling();
124
- watcher?.unref();
125
- },
126
- prime() {
127
- poll();
128
- },
129
- stop() {
130
- if (restartTimer) clearTimeout(restartTimer);
131
- restartTimer = undefined;
132
- closeWatcher(watcher);
133
- watcher = undefined;
134
- stopPolling();
135
- coalescer.clear();
136
- },
137
- };
138
- return handle;
139
- }
@@ -1,54 +0,0 @@
1
- import type { HistogramPoint, MetricLabels, MetricPoint, MetricSnapshot } from "../metrics-primitives.ts";
2
-
3
- function prometheusName(name: string): string {
4
- return name.replace(/[^a-zA-Z0-9_:]/g, "_").replace(/^[0-9]/, "_$&");
5
- }
6
-
7
- function escapeLabel(value: string): string {
8
- return value.replace(/\\/g, "\\\\").replace(/\n/g, "\\n").replace(/"/g, '\\"');
9
- }
10
-
11
- function labelsText(labels: MetricLabels): string {
12
- const entries = Object.entries(labels);
13
- if (!entries.length) return "";
14
- return `{${entries.map(([key, value]) => `${key}="${escapeLabel(String(value))}"`).join(",")}}`;
15
- }
16
-
17
- function metricType(type: MetricSnapshot["type"]): string {
18
- return type === "histogram" ? "histogram" : type === "gauge" ? "gauge" : "counter";
19
- }
20
-
21
- function isHistogramPoint(value: MetricPoint | HistogramPoint): value is HistogramPoint {
22
- return "buckets" in value && "counts" in value;
23
- }
24
-
25
- function formatPrometheusValue(num: number): string {
26
- if (Number.isNaN(num)) return "Nan";
27
- if (num === Number.POSITIVE_INFINITY) return "+Inf";
28
- if (num === Number.NEGATIVE_INFINITY) return "-Inf";
29
- return String(num);
30
- }
31
-
32
- export function formatPrometheus(snapshots: MetricSnapshot[]): string {
33
- const lines: string[] = [];
34
- for (const snapshot of snapshots) {
35
- const name = prometheusName(snapshot.name);
36
- lines.push(`# HELP ${name} ${snapshot.description}`);
37
- lines.push(`# TYPE ${name} ${metricType(snapshot.type)}`);
38
- for (const value of snapshot.values) {
39
- if (isHistogramPoint(value)) {
40
- let cumulative = 0;
41
- for (let index = 0; index < value.buckets.length; index += 1) {
42
- cumulative += value.counts[index] ?? 0;
43
- const le = Number.isFinite(value.buckets[index]) ? String(value.buckets[index]) : "+Inf";
44
- lines.push(`${name}_bucket${labelsText({ ...value.labels, le })} ${cumulative}`);
45
- }
46
- lines.push(`${name}_sum${labelsText(value.labels)} ${value.sum}`);
47
- lines.push(`${name}_count${labelsText(value.labels)} ${value.count}`);
48
- } else {
49
- lines.push(`${name}${labelsText(value.labels)} ${formatPrometheusValue(value.value)}`);
50
- }
51
- }
52
- }
53
- return `${lines.join("\n")}\n`;
54
- }
@@ -1,64 +0,0 @@
1
- import { labelKey, type MetricLabels } from "./metrics-primitives.ts";
2
-
3
- interface WindowEvent {
4
- timestamp: number;
5
- labels: MetricLabels;
6
- delta: number;
7
- }
8
-
9
- export class TimeWindowedCounter {
10
- private events: WindowEvent[] = [];
11
- private readonly windowMs: number;
12
- private readonly now: () => number;
13
- private static readonly MAX_EVENTS = 100_000;
14
-
15
- constructor(windowMs = 3_600_000, now: () => number = () => Date.now()) {
16
- this.windowMs = windowMs;
17
- this.now = now;
18
- }
19
-
20
- inc(labels: MetricLabels = {}, delta = 1): void {
21
- if (!Number.isFinite(delta)) return;
22
- // Cap the event array to prevent unbounded memory growth.
23
- if (this.events.length >= TimeWindowedCounter.MAX_EVENTS) this.prune();
24
- this.events.push({
25
- timestamp: this.now(),
26
- labels: { ...labels },
27
- delta,
28
- });
29
- this.prune();
30
- }
31
-
32
- // PERF (OBS-5): This method is O(N) per call because it linearly scans
33
- // all events and calls labelKey() (which uses JSON.stringify) on each.
34
- // A more efficient aggregation (e.g., pre-aggregated label buckets or a
35
- // sliding-window histogram) is planned to replace this hot path.
36
- count(labels: MetricLabels = {}, durationMs = this.windowMs): number {
37
- const now = this.now();
38
- this.pruneAt(now);
39
- const key = labelKey(labels);
40
- const cutoff = now - durationMs;
41
- return this.events
42
- .filter((event) => event.timestamp >= cutoff && labelKey(event.labels) === key)
43
- .reduce((sum, event) => sum + event.delta, 0);
44
- }
45
-
46
- rate(labels: MetricLabels = {}, durationMs = this.windowMs): number {
47
- if (durationMs <= 0) return 0;
48
- return this.count(labels, durationMs) / (durationMs / 1000);
49
- }
50
-
51
- size(): number {
52
- this.prune();
53
- return this.events.length;
54
- }
55
-
56
- private prune(): void {
57
- this.pruneAt(this.now());
58
- }
59
-
60
- private pruneAt(now: number): void {
61
- const cutoff = now - this.windowMs;
62
- this.events = this.events.filter((event) => event.timestamp >= cutoff);
63
- }
64
- }
@@ -1,278 +0,0 @@
1
- /**
2
- * Deterministic compaction summary — builds a markdown summary of a pi-crew run
3
- * from manifest.json, tasks.json, and the tail of events.jsonl.
4
- *
5
- * Distilled from pi-autoresearch's compaction-summary pattern.
6
- */
7
-
8
- class GiantLineFallbackError extends Error {
9
- constructor() {
10
- super("GIANT_LINE_FALLBACK");
11
- this.name = "GiantLineFallbackError";
12
- }
13
- }
14
-
15
- import * as fs from "node:fs";
16
- import * as path from "node:path";
17
- import { readJsonFile } from "../../state/atomic-write.ts";
18
- import type { TeamEvent } from "../../state/event-log/event-log.ts";
19
- import type { TeamRunManifest, TeamTaskState } from "../../state/types.ts";
20
-
21
- /** Maximum number of events to read from the tail of events.jsonl. */
22
- const MAX_TAIL_EVENTS = 100;
23
-
24
- /** Maximum number of completed tasks to include in the "Recent Results" section. */
25
- const MAX_RECENT_RESULTS = 10;
26
-
27
- /** Paths relevant to building a compaction summary for a run. */
28
- export interface SummaryPaths {
29
- manifestPath: string;
30
- tasksPath: string;
31
- eventsPath: string;
32
- stateRoot: string;
33
- }
34
-
35
- /**
36
- * Derive the standard summary-relevant paths from a state root directory.
37
- * Mirrors pi-autoresearch's `autoresearchSummaryPathsFor()`.
38
- */
39
- export function summaryPathsFor(stateRoot: string): SummaryPaths {
40
- return {
41
- stateRoot,
42
- manifestPath: path.join(stateRoot, "manifest.json"),
43
- tasksPath: path.join(stateRoot, "tasks.json"),
44
- eventsPath: path.join(stateRoot, "events.jsonl"),
45
- };
46
- }
47
-
48
- /**
49
- * Read the last N lines from a text file.
50
- * Uses reverse buffer reading to avoid loading the entire file into memory.
51
- * For files larger than TAIL_MAX_READ bytes, only the last chunk is read.
52
- */
53
- const TAIL_MAX_READ = 256 * 1024; // 256KB — enough for ~1000 lines of JSONL
54
- const MAX_FALLBACK_READ = 2 * 1024 * 1024; // 2MB — safety limit for giant-line fallback
55
- function readTailLines(filePath: string, maxLines: number): string[] {
56
- if (!fs.existsSync(filePath)) return [];
57
- try {
58
- const stat = fs.statSync(filePath);
59
- const fileSize = stat.size;
60
- if (fileSize === 0) return [];
61
-
62
- // For small files, just read everything
63
- if (fileSize <= TAIL_MAX_READ) {
64
- const content = fs.readFileSync(filePath, "utf-8");
65
- return content
66
- .split("\n")
67
- .filter((line) => line.trim().length > 0)
68
- .slice(-maxLines);
69
- }
70
-
71
- // For large files, read only the last chunk.
72
- // Search backwards from the read boundary for a newline to avoid
73
- // splitting mid-line or mid-multibyte UTF-8 character.
74
- const fd = fs.openSync(filePath, "r");
75
- try {
76
- const readSize = Math.min(fileSize, TAIL_MAX_READ);
77
- const readOffset = fileSize - readSize;
78
- const buf = Buffer.alloc(readSize);
79
- fs.readSync(fd, buf, 0, readSize, readOffset);
80
-
81
- // Find the first newline in the buffer to avoid partial lines.
82
- // This also handles multibyte UTF-8 safety — we start after a
83
- // newline boundary which is always a clean character boundary.
84
- let start = 0;
85
- if (readOffset > 0) {
86
- const firstNewline = buf.indexOf("\n");
87
- if (firstNewline >= 0) {
88
- start = firstNewline + 1;
89
- } else {
90
- // No newline found in the entire tail chunk — single giant line.
91
- // Fall back to reading the full file to avoid data loss.
92
- // Note: fd will be closed by the outer finally block.
93
- throw new GiantLineFallbackError();
94
- }
95
- }
96
-
97
- const content = buf.toString("utf-8", start, readSize);
98
- const lines = content.split("\n").filter((line) => line.trim().length > 0);
99
- return lines.slice(-maxLines);
100
- } finally {
101
- fs.closeSync(fd);
102
- }
103
- } catch (err) {
104
- // Giant-line fallback: fd already closed by finally above.
105
- if (err instanceof GiantLineFallbackError) {
106
- const stat = fs.statSync(filePath);
107
- if (stat.size > MAX_FALLBACK_READ) return [];
108
- const content = fs.readFileSync(filePath, "utf-8");
109
- return content
110
- .split("\n")
111
- .filter((line) => line.trim().length > 0)
112
- .slice(-maxLines);
113
- }
114
- return [];
115
- }
116
- }
117
-
118
- /**
119
- * Parse JSONL lines into TeamEvent objects, skipping malformed lines.
120
- */
121
- function parseEvents(lines: string[]): TeamEvent[] {
122
- const events: TeamEvent[] = [];
123
- for (const line of lines) {
124
- try {
125
- const parsed = JSON.parse(line.trim());
126
- if (typeof parsed === "object" && parsed !== null && typeof parsed.type === "string" && typeof parsed.runId === "string") {
127
- events.push(parsed as TeamEvent);
128
- }
129
- } catch {
130
- // Skip malformed lines
131
- }
132
- }
133
- return events;
134
- }
135
-
136
- /**
137
- * Compute a human-readable duration between two ISO timestamp strings.
138
- */
139
- function formatDuration(startIso?: string, endIso?: string): string {
140
- if (!startIso) return "—";
141
- const start = new Date(startIso).getTime();
142
- if (Number.isNaN(start)) return "—";
143
- const end = endIso ? new Date(endIso).getTime() : Date.now();
144
- if (Number.isNaN(end)) return "—";
145
- const diffMs = end - start;
146
- if (diffMs < 0) return "—";
147
- const seconds = Math.floor(diffMs / 1000);
148
- if (seconds < 60) return `${seconds}s`;
149
- const minutes = Math.floor(seconds / 60);
150
- const remainingSeconds = seconds % 60;
151
- if (minutes < 60) return `${minutes}m ${remainingSeconds}s`;
152
- const hours = Math.floor(minutes / 60);
153
- const remainingMinutes = minutes % 60;
154
- return `${hours}h ${remainingMinutes}m`;
155
- }
156
-
157
- /**
158
- * Build a deterministic compaction summary for a pi-crew run.
159
- *
160
- * Reads manifest.json, tasks.json, and the tail of events.jsonl to produce
161
- * a self-contained markdown summary suitable for context injection.
162
- *
163
- * @param stateRoot - Path to the run's state root directory
164
- * @returns Markdown-formatted compaction summary
165
- */
166
- export function buildCompactionSummary(stateRoot: string): string {
167
- const paths = summaryPathsFor(stateRoot);
168
-
169
- // Read manifest
170
- const manifest = readJsonFile<TeamRunManifest>(paths.manifestPath);
171
-
172
- // Read tasks
173
- const tasks = readJsonFile<TeamTaskState[]>(paths.tasksPath) ?? [];
174
-
175
- // Read tail events
176
- const tailLines = readTailLines(paths.eventsPath, MAX_TAIL_EVENTS);
177
- const tailEvents = parseEvents(tailLines);
178
-
179
- const sections: string[] = [];
180
-
181
- // Section: Run Metadata
182
- sections.push("# Run Summary");
183
- if (manifest) {
184
- sections.push("");
185
- sections.push("## Run Metadata");
186
- sections.push(`- **Run ID**: ${manifest.runId}`);
187
- sections.push(`- **Team**: ${manifest.team}`);
188
- if (manifest.workflow) {
189
- sections.push(`- **Workflow**: ${manifest.workflow}`);
190
- }
191
- if (manifest.goal) {
192
- sections.push(`- **Goal**: ${manifest.goal.replace(/\n/g, " ").slice(0, 500)}`);
193
- }
194
- sections.push(`- **Status**: ${manifest.status}`);
195
- sections.push(`- **Created**: ${manifest.createdAt}`);
196
- sections.push(`- **Updated**: ${manifest.updatedAt}`);
197
- if (manifest.workspaceMode) {
198
- sections.push(`- **Workspace Mode**: ${manifest.workspaceMode}`);
199
- }
200
- } else {
201
- sections.push("");
202
- sections.push("## Run Metadata");
203
- sections.push("- **Status**: manifest unavailable");
204
- }
205
-
206
- // Section: Task Progress Table
207
- sections.push("");
208
- sections.push("## Task Progress");
209
- if (tasks.length > 0) {
210
- sections.push("");
211
- sections.push("| ID | Role | Status | Duration |");
212
- sections.push("|---|---|---|---|");
213
- for (const task of tasks) {
214
- const taskId = task.id;
215
- const role = task.role || "—";
216
- const status = task.status || "—";
217
- const duration = formatDuration(task.startedAt, task.finishedAt);
218
- sections.push(`| ${taskId} | ${role} | ${status} | ${duration} |`);
219
- }
220
- } else {
221
- sections.push("");
222
- sections.push("No tasks recorded.");
223
- }
224
-
225
- // Section: Recent Task Results
226
- const completedTasks = tasks.filter((t) => t.status === "completed" || t.status === "failed").slice(-MAX_RECENT_RESULTS);
227
-
228
- if (completedTasks.length > 0) {
229
- sections.push("");
230
- sections.push("## Recent Task Results");
231
- for (const task of completedTasks) {
232
- sections.push("");
233
- sections.push(`### ${task.id} (${task.status})`);
234
- if (task.error) {
235
- sections.push(`- **Error**: ${task.error}`);
236
- }
237
- if (task.diagnostics && Object.keys(task.diagnostics).length > 0) {
238
- sections.push("- **Diagnostics**:");
239
- for (const [key, value] of Object.entries(task.diagnostics)) {
240
- sections.push(` - ${key}: ${JSON.stringify(value)}`);
241
- }
242
- }
243
- if (task.metrics && Object.keys(task.metrics).length > 0) {
244
- sections.push("- **Metrics**:");
245
- for (const [key, value] of Object.entries(task.metrics)) {
246
- sections.push(` - ${key}: ${value}`);
247
- }
248
- }
249
- }
250
- }
251
-
252
- // Section: Next Steps (pending/queued tasks)
253
- const pendingStatuses = new Set(["queued", "waiting", "running"]);
254
- const pendingTasks = tasks.filter((t) => pendingStatuses.has(t.status));
255
- if (pendingTasks.length > 0) {
256
- sections.push("");
257
- sections.push("## Next Steps");
258
- sections.push("");
259
- for (const task of pendingTasks) {
260
- const title = task.title || task.role || "Untitled";
261
- sections.push(`- [${task.status}] ${task.id}: ${title}`);
262
- }
263
- }
264
-
265
- // Section: Tail Events Summary
266
- if (tailEvents.length > 0) {
267
- sections.push("");
268
- sections.push(`## Recent Events (last ${tailEvents.length})`);
269
- sections.push("");
270
- for (const event of tailEvents.slice(-10)) {
271
- const taskPart = event.taskId ? ` task=${event.taskId}` : "";
272
- const msgPart = event.message ? ` — ${event.message}` : "";
273
- sections.push(`- [${event.time}] ${event.type}${taskPart}${msgPart}`);
274
- }
275
- }
276
-
277
- return sections.join("\n");
278
- }