pi-crew 0.1.37 → 0.1.39
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/AGENTS.md +1 -1
- package/CHANGELOG.md +27 -0
- package/README.md +5 -0
- 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/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 -297
- package/docs/research-extension-system.md +324 -324
- package/docs/research-optimization-plan.md +548 -548
- package/docs/research-pi-coding-agent.md +357 -357
- package/docs/research-source-pi-crew-reference.md +174 -174
- package/docs/resource-formats.md +10 -8
- package/docs/runtime-flow.md +148 -148
- package/docs/source-runtime-refactor-map.md +83 -83
- package/docs/usage.md +6 -0
- package/index.ts +6 -6
- package/package.json +3 -3
- package/schema.json +2 -2
- package/src/agents/agent-serializer.ts +34 -34
- package/src/config/config.ts +8 -4
- package/src/extension/cross-extension-rpc.ts +82 -82
- package/src/extension/import-index.ts +18 -2
- package/src/extension/register.ts +11 -1
- package/src/extension/registration/compaction-guard.ts +125 -125
- package/src/extension/registration/subagent-helpers.ts +30 -6
- package/src/extension/registration/subagent-tools.ts +8 -3
- package/src/extension/result-watcher.ts +98 -98
- package/src/extension/run-import.ts +12 -2
- package/src/extension/run-index.ts +12 -2
- package/src/extension/run-maintenance.ts +24 -24
- package/src/extension/team-tool/api.ts +54 -14
- package/src/extension/team-tool/cancel.ts +31 -31
- package/src/extension/team-tool/doctor.ts +179 -179
- 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/observability/correlation.ts +35 -35
- package/src/observability/event-to-metric.ts +54 -54
- package/src/observability/exporters/adapter.ts +24 -24
- package/src/observability/exporters/otlp-exporter.ts +65 -65
- package/src/observability/exporters/prometheus-exporter.ts +47 -47
- package/src/observability/metric-registry.ts +72 -72
- package/src/observability/metric-retention.ts +46 -46
- package/src/observability/metric-sink.ts +51 -51
- package/src/observability/metrics-primitives.ts +166 -166
- 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 +114 -113
- package/src/runtime/async-marker.ts +26 -26
- package/src/runtime/background-runner.ts +53 -53
- package/src/runtime/crash-recovery.ts +56 -56
- package/src/runtime/crew-agent-records.ts +54 -9
- package/src/runtime/crew-agent-runtime.ts +58 -58
- package/src/runtime/deadletter.ts +36 -36
- 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/heartbeat-gradient.ts +28 -28
- package/src/runtime/heartbeat-watcher.ts +80 -80
- package/src/runtime/live-agent-control.ts +87 -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 +248 -212
- 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/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/retry-executor.ts +59 -59
- 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 +80 -12
- package/src/runtime/task-display.ts +38 -38
- package/src/runtime/task-output-context.ts +127 -106
- package/src/runtime/task-runner/live-executor.ts +98 -98
- package/src/runtime/task-runner/progress.ts +111 -111
- package/src/runtime/task-runner/result-utils.ts +14 -14
- package/src/runtime/task-runner/state-helpers.ts +22 -22
- package/src/runtime/team-runner.ts +1 -1
- package/src/runtime/worker-heartbeat.ts +21 -21
- package/src/runtime/worker-startup.ts +57 -57
- package/src/schema/config-schema.ts +21 -21
- package/src/schema/team-tool-schema.ts +100 -100
- package/src/state/artifact-store.ts +122 -108
- package/src/state/contracts.ts +105 -105
- package/src/state/jsonl-writer.ts +77 -77
- package/src/state/mailbox.ts +67 -22
- package/src/state/state-store.ts +36 -5
- package/src/state/task-claims.ts +42 -42
- 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 +27 -5
- package/src/teams/team-serializer.ts +38 -36
- package/src/types/diff.d.ts +18 -18
- package/src/ui/crew-footer.ts +101 -101
- package/src/ui/crew-select-list.ts +111 -111
- package/src/ui/dashboard-panes/metrics-pane.ts +34 -34
- 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/render-diff.ts +119 -119
- package/src/ui/run-dashboard.ts +5 -2
- package/src/ui/run-snapshot-cache.ts +19 -8
- package/src/ui/spinner.ts +17 -17
- package/src/ui/status-colors.ts +54 -54
- package/src/ui/syntax-highlight.ts +116 -116
- package/src/ui/transcript-viewer.ts +15 -1
- 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 +3 -2
- package/src/utils/safe-paths.ts +34 -0
- 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 +30 -3
- package/src/workflows/validate-workflow.ts +40 -40
- package/src/worktree/branch-freshness.ts +45 -45
- 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,166 +1,166 @@
|
|
|
1
|
-
export type MetricLabelValue = string | number;
|
|
2
|
-
export type MetricLabels = Record<string, MetricLabelValue>;
|
|
3
|
-
|
|
4
|
-
export interface MetricPoint {
|
|
5
|
-
labels: MetricLabels;
|
|
6
|
-
value: number;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface HistogramPoint {
|
|
10
|
-
labels: MetricLabels;
|
|
11
|
-
buckets: number[];
|
|
12
|
-
counts: number[];
|
|
13
|
-
sum: number;
|
|
14
|
-
count: number;
|
|
15
|
-
quantiles: Record<string, number>;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface MetricSnapshot {
|
|
19
|
-
type: "counter" | "gauge" | "histogram";
|
|
20
|
-
name: string;
|
|
21
|
-
description: string;
|
|
22
|
-
values: MetricPoint[] | HistogramPoint[];
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
interface StoredValue {
|
|
26
|
-
labels: MetricLabels;
|
|
27
|
-
value: number;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
interface StoredHistogram {
|
|
31
|
-
labels: MetricLabels;
|
|
32
|
-
counts: number[];
|
|
33
|
-
sum: number;
|
|
34
|
-
count: number;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export const DEFAULT_HISTOGRAM_BUCKETS = [1, 2, 5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000] as const;
|
|
38
|
-
|
|
39
|
-
function normalizeLabels(labels: MetricLabels = {}): MetricLabels {
|
|
40
|
-
const normalized: MetricLabels = {};
|
|
41
|
-
for (const [key, value] of Object.entries(labels).sort(([left], [right]) => left.localeCompare(right))) normalized[key] = value;
|
|
42
|
-
return normalized;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function labelKey(labels: MetricLabels = {}): string {
|
|
46
|
-
return JSON.stringify(normalizeLabels(labels));
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function cloneLabels(labels: MetricLabels): MetricLabels {
|
|
50
|
-
return { ...labels };
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export abstract class Metric {
|
|
54
|
-
readonly name: string;
|
|
55
|
-
readonly description: string;
|
|
56
|
-
|
|
57
|
-
constructor(name: string, description: string) {
|
|
58
|
-
this.name = name;
|
|
59
|
-
this.description = description;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
abstract snapshot(): MetricSnapshot;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export class Counter extends Metric {
|
|
66
|
-
private values = new Map<string, StoredValue>();
|
|
67
|
-
|
|
68
|
-
inc(labels: MetricLabels = {}, delta = 1): void {
|
|
69
|
-
if (!Number.isFinite(delta)) return;
|
|
70
|
-
const key = labelKey(labels);
|
|
71
|
-
const current = this.values.get(key) ?? { labels: normalizeLabels(labels), value: 0 };
|
|
72
|
-
this.values.set(key, { labels: current.labels, value: current.value + delta });
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
value(labels: MetricLabels = {}): number {
|
|
76
|
-
return this.values.get(labelKey(labels))?.value ?? 0;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
snapshot(): MetricSnapshot {
|
|
80
|
-
return { type: "counter", name: this.name, description: this.description, values: [...this.values.values()].map((entry) => ({ labels: cloneLabels(entry.labels), value: entry.value })) };
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export class Gauge extends Metric {
|
|
85
|
-
private values = new Map<string, StoredValue>();
|
|
86
|
-
|
|
87
|
-
set(labels: MetricLabels = {}, value: number): void {
|
|
88
|
-
if (!Number.isFinite(value)) return;
|
|
89
|
-
this.values.set(labelKey(labels), { labels: normalizeLabels(labels), value });
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
add(labels: MetricLabels = {}, delta: number): void {
|
|
93
|
-
if (!Number.isFinite(delta)) return;
|
|
94
|
-
this.set(labels, this.value(labels) + delta);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
value(labels: MetricLabels = {}): number {
|
|
98
|
-
return this.values.get(labelKey(labels))?.value ?? 0;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
snapshot(): MetricSnapshot {
|
|
102
|
-
return { type: "gauge", name: this.name, description: this.description, values: [...this.values.values()].map((entry) => ({ labels: cloneLabels(entry.labels), value: entry.value })) };
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export class Histogram extends Metric {
|
|
107
|
-
private readonly buckets: number[];
|
|
108
|
-
private observations = new Map<string, StoredHistogram>();
|
|
109
|
-
|
|
110
|
-
constructor(name: string, description: string, buckets?: number[]) {
|
|
111
|
-
super(name, description);
|
|
112
|
-
const source = buckets?.length ? buckets : [...DEFAULT_HISTOGRAM_BUCKETS];
|
|
113
|
-
this.buckets = [...new Set(source.filter((bucket) => Number.isFinite(bucket)).sort((left, right) => left - right))];
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
observe(labels: MetricLabels = {}, value: number): void {
|
|
117
|
-
if (!Number.isFinite(value)) return;
|
|
118
|
-
const key = labelKey(labels);
|
|
119
|
-
const current = this.observations.get(key) ?? { labels: normalizeLabels(labels), counts: new Array(this.buckets.length + 1).fill(0) as number[], sum: 0, count: 0 };
|
|
120
|
-
const bucketIndex = this.buckets.findIndex((bucket) => value <= bucket);
|
|
121
|
-
current.counts[bucketIndex === -1 ? this.buckets.length : bucketIndex] = (current.counts[bucketIndex === -1 ? this.buckets.length : bucketIndex] ?? 0) + 1;
|
|
122
|
-
current.sum += value;
|
|
123
|
-
current.count += 1;
|
|
124
|
-
this.observations.set(key, current);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
quantile(labels: MetricLabels = {}, q: number): number {
|
|
128
|
-
const obs = this.observations.get(labelKey(labels));
|
|
129
|
-
if (!obs || obs.count === 0 || !Number.isFinite(q)) return Number.NaN;
|
|
130
|
-
const bounded = Math.min(1, Math.max(0, q));
|
|
131
|
-
const target = Math.max(1, bounded * obs.count);
|
|
132
|
-
let cumulative = 0;
|
|
133
|
-
for (let index = 0; index < obs.counts.length; index += 1) {
|
|
134
|
-
const count = obs.counts[index] ?? 0;
|
|
135
|
-
cumulative += count;
|
|
136
|
-
if (cumulative >= target) {
|
|
137
|
-
const previous = cumulative - count;
|
|
138
|
-
const lower = index === 0 ? 0 : this.buckets[index - 1] ?? this.buckets.at(-1) ?? 0;
|
|
139
|
-
const upper = index < this.buckets.length ? this.buckets[index] ?? lower : Math.max(lower, obs.sum / Math.max(1, obs.count));
|
|
140
|
-
const fraction = count === 0 ? 0 : (target - previous) / Math.max(1, count);
|
|
141
|
-
return lower + fraction * (upper - lower);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
return this.buckets.at(-1) ?? Number.NaN;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
count(labels: MetricLabels = {}): number {
|
|
148
|
-
return this.observations.get(labelKey(labels))?.count ?? 0;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
snapshot(): MetricSnapshot {
|
|
152
|
-
return {
|
|
153
|
-
type: "histogram",
|
|
154
|
-
name: this.name,
|
|
155
|
-
description: this.description,
|
|
156
|
-
values: [...this.observations.values()].map((entry) => ({
|
|
157
|
-
labels: cloneLabels(entry.labels),
|
|
158
|
-
buckets: [...this.buckets, Number.POSITIVE_INFINITY],
|
|
159
|
-
counts: [...entry.counts],
|
|
160
|
-
sum: entry.sum,
|
|
161
|
-
count: entry.count,
|
|
162
|
-
quantiles: { p50: this.quantile(entry.labels, 0.5), p95: this.quantile(entry.labels, 0.95), p99: this.quantile(entry.labels, 0.99) },
|
|
163
|
-
})),
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
}
|
|
1
|
+
export type MetricLabelValue = string | number;
|
|
2
|
+
export type MetricLabels = Record<string, MetricLabelValue>;
|
|
3
|
+
|
|
4
|
+
export interface MetricPoint {
|
|
5
|
+
labels: MetricLabels;
|
|
6
|
+
value: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface HistogramPoint {
|
|
10
|
+
labels: MetricLabels;
|
|
11
|
+
buckets: number[];
|
|
12
|
+
counts: number[];
|
|
13
|
+
sum: number;
|
|
14
|
+
count: number;
|
|
15
|
+
quantiles: Record<string, number>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface MetricSnapshot {
|
|
19
|
+
type: "counter" | "gauge" | "histogram";
|
|
20
|
+
name: string;
|
|
21
|
+
description: string;
|
|
22
|
+
values: MetricPoint[] | HistogramPoint[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface StoredValue {
|
|
26
|
+
labels: MetricLabels;
|
|
27
|
+
value: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface StoredHistogram {
|
|
31
|
+
labels: MetricLabels;
|
|
32
|
+
counts: number[];
|
|
33
|
+
sum: number;
|
|
34
|
+
count: number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const DEFAULT_HISTOGRAM_BUCKETS = [1, 2, 5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000] as const;
|
|
38
|
+
|
|
39
|
+
function normalizeLabels(labels: MetricLabels = {}): MetricLabels {
|
|
40
|
+
const normalized: MetricLabels = {};
|
|
41
|
+
for (const [key, value] of Object.entries(labels).sort(([left], [right]) => left.localeCompare(right))) normalized[key] = value;
|
|
42
|
+
return normalized;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function labelKey(labels: MetricLabels = {}): string {
|
|
46
|
+
return JSON.stringify(normalizeLabels(labels));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function cloneLabels(labels: MetricLabels): MetricLabels {
|
|
50
|
+
return { ...labels };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export abstract class Metric {
|
|
54
|
+
readonly name: string;
|
|
55
|
+
readonly description: string;
|
|
56
|
+
|
|
57
|
+
constructor(name: string, description: string) {
|
|
58
|
+
this.name = name;
|
|
59
|
+
this.description = description;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
abstract snapshot(): MetricSnapshot;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class Counter extends Metric {
|
|
66
|
+
private values = new Map<string, StoredValue>();
|
|
67
|
+
|
|
68
|
+
inc(labels: MetricLabels = {}, delta = 1): void {
|
|
69
|
+
if (!Number.isFinite(delta)) return;
|
|
70
|
+
const key = labelKey(labels);
|
|
71
|
+
const current = this.values.get(key) ?? { labels: normalizeLabels(labels), value: 0 };
|
|
72
|
+
this.values.set(key, { labels: current.labels, value: current.value + delta });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
value(labels: MetricLabels = {}): number {
|
|
76
|
+
return this.values.get(labelKey(labels))?.value ?? 0;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
snapshot(): MetricSnapshot {
|
|
80
|
+
return { type: "counter", name: this.name, description: this.description, values: [...this.values.values()].map((entry) => ({ labels: cloneLabels(entry.labels), value: entry.value })) };
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export class Gauge extends Metric {
|
|
85
|
+
private values = new Map<string, StoredValue>();
|
|
86
|
+
|
|
87
|
+
set(labels: MetricLabels = {}, value: number): void {
|
|
88
|
+
if (!Number.isFinite(value)) return;
|
|
89
|
+
this.values.set(labelKey(labels), { labels: normalizeLabels(labels), value });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
add(labels: MetricLabels = {}, delta: number): void {
|
|
93
|
+
if (!Number.isFinite(delta)) return;
|
|
94
|
+
this.set(labels, this.value(labels) + delta);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
value(labels: MetricLabels = {}): number {
|
|
98
|
+
return this.values.get(labelKey(labels))?.value ?? 0;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
snapshot(): MetricSnapshot {
|
|
102
|
+
return { type: "gauge", name: this.name, description: this.description, values: [...this.values.values()].map((entry) => ({ labels: cloneLabels(entry.labels), value: entry.value })) };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export class Histogram extends Metric {
|
|
107
|
+
private readonly buckets: number[];
|
|
108
|
+
private observations = new Map<string, StoredHistogram>();
|
|
109
|
+
|
|
110
|
+
constructor(name: string, description: string, buckets?: number[]) {
|
|
111
|
+
super(name, description);
|
|
112
|
+
const source = buckets?.length ? buckets : [...DEFAULT_HISTOGRAM_BUCKETS];
|
|
113
|
+
this.buckets = [...new Set(source.filter((bucket) => Number.isFinite(bucket)).sort((left, right) => left - right))];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
observe(labels: MetricLabels = {}, value: number): void {
|
|
117
|
+
if (!Number.isFinite(value)) return;
|
|
118
|
+
const key = labelKey(labels);
|
|
119
|
+
const current = this.observations.get(key) ?? { labels: normalizeLabels(labels), counts: new Array(this.buckets.length + 1).fill(0) as number[], sum: 0, count: 0 };
|
|
120
|
+
const bucketIndex = this.buckets.findIndex((bucket) => value <= bucket);
|
|
121
|
+
current.counts[bucketIndex === -1 ? this.buckets.length : bucketIndex] = (current.counts[bucketIndex === -1 ? this.buckets.length : bucketIndex] ?? 0) + 1;
|
|
122
|
+
current.sum += value;
|
|
123
|
+
current.count += 1;
|
|
124
|
+
this.observations.set(key, current);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
quantile(labels: MetricLabels = {}, q: number): number {
|
|
128
|
+
const obs = this.observations.get(labelKey(labels));
|
|
129
|
+
if (!obs || obs.count === 0 || !Number.isFinite(q)) return Number.NaN;
|
|
130
|
+
const bounded = Math.min(1, Math.max(0, q));
|
|
131
|
+
const target = Math.max(1, bounded * obs.count);
|
|
132
|
+
let cumulative = 0;
|
|
133
|
+
for (let index = 0; index < obs.counts.length; index += 1) {
|
|
134
|
+
const count = obs.counts[index] ?? 0;
|
|
135
|
+
cumulative += count;
|
|
136
|
+
if (cumulative >= target) {
|
|
137
|
+
const previous = cumulative - count;
|
|
138
|
+
const lower = index === 0 ? 0 : this.buckets[index - 1] ?? this.buckets.at(-1) ?? 0;
|
|
139
|
+
const upper = index < this.buckets.length ? this.buckets[index] ?? lower : Math.max(lower, obs.sum / Math.max(1, obs.count));
|
|
140
|
+
const fraction = count === 0 ? 0 : (target - previous) / Math.max(1, count);
|
|
141
|
+
return lower + fraction * (upper - lower);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return this.buckets.at(-1) ?? Number.NaN;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
count(labels: MetricLabels = {}): number {
|
|
148
|
+
return this.observations.get(labelKey(labels))?.count ?? 0;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
snapshot(): MetricSnapshot {
|
|
152
|
+
return {
|
|
153
|
+
type: "histogram",
|
|
154
|
+
name: this.name,
|
|
155
|
+
description: this.description,
|
|
156
|
+
values: [...this.observations.values()].map((entry) => ({
|
|
157
|
+
labels: cloneLabels(entry.labels),
|
|
158
|
+
buckets: [...this.buckets, Number.POSITIVE_INFINITY],
|
|
159
|
+
counts: [...entry.counts],
|
|
160
|
+
sum: entry.sum,
|
|
161
|
+
count: entry.count,
|
|
162
|
+
quantiles: { p50: this.quantile(entry.labels, 0.5), p95: this.quantile(entry.labels, 0.95), p99: this.quantile(entry.labels, 0.99) },
|
|
163
|
+
})),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
2
|
-
|
|
3
|
-
export const PI_TEAMS_INHERIT_PROJECT_CONTEXT_ENV = "PI_TEAMS_INHERIT_PROJECT_CONTEXT";
|
|
4
|
-
export const PI_TEAMS_INHERIT_SKILLS_ENV = "PI_TEAMS_INHERIT_SKILLS";
|
|
5
|
-
export const PI_CREW_INHERIT_PROJECT_CONTEXT_ENV = "PI_CREW_INHERIT_PROJECT_CONTEXT";
|
|
6
|
-
export const PI_CREW_INHERIT_SKILLS_ENV = "PI_CREW_INHERIT_SKILLS";
|
|
7
|
-
|
|
8
|
-
const PROJECT_CONTEXT_HEADER = "\n\n# Project Context\n\nProject-specific instructions and guidelines:\n\n";
|
|
9
|
-
const SKILLS_HEADER = "\n\nThe following skills provide specialized instructions for specific tasks.";
|
|
10
|
-
const DATE_HEADER = "\nCurrent date:";
|
|
11
|
-
|
|
12
|
-
function readBooleanEnv(name: string): boolean | undefined {
|
|
13
|
-
const value = process.env[name];
|
|
14
|
-
if (value === undefined) return undefined;
|
|
15
|
-
return value !== "0";
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function readBooleanEnvAny(...names: string[]): boolean | undefined {
|
|
19
|
-
for (const name of names) {
|
|
20
|
-
const value = readBooleanEnv(name);
|
|
21
|
-
if (value !== undefined) return value;
|
|
22
|
-
}
|
|
23
|
-
return undefined;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function findSectionEnd(prompt: string, startIndex: number, nextHeaders: string[]): number {
|
|
27
|
-
let endIndex = prompt.length;
|
|
28
|
-
for (const header of nextHeaders) {
|
|
29
|
-
const index = prompt.indexOf(header, startIndex);
|
|
30
|
-
if (index !== -1 && index < endIndex) endIndex = index;
|
|
31
|
-
}
|
|
32
|
-
return endIndex;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function stripProjectContext(prompt: string): string {
|
|
36
|
-
const startIndex = prompt.indexOf(PROJECT_CONTEXT_HEADER);
|
|
37
|
-
if (startIndex === -1) return prompt;
|
|
38
|
-
const endIndex = findSectionEnd(prompt, startIndex + PROJECT_CONTEXT_HEADER.length, [SKILLS_HEADER, DATE_HEADER]);
|
|
39
|
-
return `${prompt.slice(0, startIndex)}${prompt.slice(endIndex)}`;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function stripInheritedSkills(prompt: string): string {
|
|
43
|
-
const startIndex = prompt.indexOf(SKILLS_HEADER);
|
|
44
|
-
if (startIndex === -1) return prompt;
|
|
45
|
-
const endIndex = findSectionEnd(prompt, startIndex + SKILLS_HEADER.length, [DATE_HEADER]);
|
|
46
|
-
return `${prompt.slice(0, startIndex)}${prompt.slice(endIndex)}`;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function rewriteTeamWorkerPrompt(prompt: string, options: { inheritProjectContext: boolean; inheritSkills: boolean }): string {
|
|
50
|
-
let rewritten = prompt;
|
|
51
|
-
if (!options.inheritProjectContext) rewritten = stripProjectContext(rewritten);
|
|
52
|
-
if (!options.inheritSkills) rewritten = stripInheritedSkills(rewritten);
|
|
53
|
-
return rewritten;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export default function registerPiTeamsPromptRuntime(pi: ExtensionAPI): void {
|
|
57
|
-
pi.on("before_agent_start", (event) => {
|
|
58
|
-
const inheritProjectContext = readBooleanEnvAny(PI_CREW_INHERIT_PROJECT_CONTEXT_ENV, PI_TEAMS_INHERIT_PROJECT_CONTEXT_ENV);
|
|
59
|
-
const inheritSkills = readBooleanEnvAny(PI_CREW_INHERIT_SKILLS_ENV, PI_TEAMS_INHERIT_SKILLS_ENV);
|
|
60
|
-
if (inheritProjectContext === undefined && inheritSkills === undefined) return;
|
|
61
|
-
const rewritten = rewriteTeamWorkerPrompt(event.systemPrompt, {
|
|
62
|
-
inheritProjectContext: inheritProjectContext ?? true,
|
|
63
|
-
inheritSkills: inheritSkills ?? true,
|
|
64
|
-
});
|
|
65
|
-
if (rewritten === event.systemPrompt) return;
|
|
66
|
-
return { systemPrompt: rewritten };
|
|
67
|
-
});
|
|
68
|
-
}
|
|
1
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
export const PI_TEAMS_INHERIT_PROJECT_CONTEXT_ENV = "PI_TEAMS_INHERIT_PROJECT_CONTEXT";
|
|
4
|
+
export const PI_TEAMS_INHERIT_SKILLS_ENV = "PI_TEAMS_INHERIT_SKILLS";
|
|
5
|
+
export const PI_CREW_INHERIT_PROJECT_CONTEXT_ENV = "PI_CREW_INHERIT_PROJECT_CONTEXT";
|
|
6
|
+
export const PI_CREW_INHERIT_SKILLS_ENV = "PI_CREW_INHERIT_SKILLS";
|
|
7
|
+
|
|
8
|
+
const PROJECT_CONTEXT_HEADER = "\n\n# Project Context\n\nProject-specific instructions and guidelines:\n\n";
|
|
9
|
+
const SKILLS_HEADER = "\n\nThe following skills provide specialized instructions for specific tasks.";
|
|
10
|
+
const DATE_HEADER = "\nCurrent date:";
|
|
11
|
+
|
|
12
|
+
function readBooleanEnv(name: string): boolean | undefined {
|
|
13
|
+
const value = process.env[name];
|
|
14
|
+
if (value === undefined) return undefined;
|
|
15
|
+
return value !== "0";
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function readBooleanEnvAny(...names: string[]): boolean | undefined {
|
|
19
|
+
for (const name of names) {
|
|
20
|
+
const value = readBooleanEnv(name);
|
|
21
|
+
if (value !== undefined) return value;
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function findSectionEnd(prompt: string, startIndex: number, nextHeaders: string[]): number {
|
|
27
|
+
let endIndex = prompt.length;
|
|
28
|
+
for (const header of nextHeaders) {
|
|
29
|
+
const index = prompt.indexOf(header, startIndex);
|
|
30
|
+
if (index !== -1 && index < endIndex) endIndex = index;
|
|
31
|
+
}
|
|
32
|
+
return endIndex;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function stripProjectContext(prompt: string): string {
|
|
36
|
+
const startIndex = prompt.indexOf(PROJECT_CONTEXT_HEADER);
|
|
37
|
+
if (startIndex === -1) return prompt;
|
|
38
|
+
const endIndex = findSectionEnd(prompt, startIndex + PROJECT_CONTEXT_HEADER.length, [SKILLS_HEADER, DATE_HEADER]);
|
|
39
|
+
return `${prompt.slice(0, startIndex)}${prompt.slice(endIndex)}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function stripInheritedSkills(prompt: string): string {
|
|
43
|
+
const startIndex = prompt.indexOf(SKILLS_HEADER);
|
|
44
|
+
if (startIndex === -1) return prompt;
|
|
45
|
+
const endIndex = findSectionEnd(prompt, startIndex + SKILLS_HEADER.length, [DATE_HEADER]);
|
|
46
|
+
return `${prompt.slice(0, startIndex)}${prompt.slice(endIndex)}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function rewriteTeamWorkerPrompt(prompt: string, options: { inheritProjectContext: boolean; inheritSkills: boolean }): string {
|
|
50
|
+
let rewritten = prompt;
|
|
51
|
+
if (!options.inheritProjectContext) rewritten = stripProjectContext(rewritten);
|
|
52
|
+
if (!options.inheritSkills) rewritten = stripInheritedSkills(rewritten);
|
|
53
|
+
return rewritten;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export default function registerPiTeamsPromptRuntime(pi: ExtensionAPI): void {
|
|
57
|
+
pi.on("before_agent_start", (event) => {
|
|
58
|
+
const inheritProjectContext = readBooleanEnvAny(PI_CREW_INHERIT_PROJECT_CONTEXT_ENV, PI_TEAMS_INHERIT_PROJECT_CONTEXT_ENV);
|
|
59
|
+
const inheritSkills = readBooleanEnvAny(PI_CREW_INHERIT_SKILLS_ENV, PI_TEAMS_INHERIT_SKILLS_ENV);
|
|
60
|
+
if (inheritProjectContext === undefined && inheritSkills === undefined) return;
|
|
61
|
+
const rewritten = rewriteTeamWorkerPrompt(event.systemPrompt, {
|
|
62
|
+
inheritProjectContext: inheritProjectContext ?? true,
|
|
63
|
+
inheritSkills: inheritSkills ?? true,
|
|
64
|
+
});
|
|
65
|
+
if (rewritten === event.systemPrompt) return;
|
|
66
|
+
return { systemPrompt: rewritten };
|
|
67
|
+
});
|
|
68
|
+
}
|
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
import type { PiTeamsConfig } from "../config/config.ts";
|
|
2
|
-
import type { TeamRunManifest } from "../state/types.ts";
|
|
3
|
-
import { appendEvent } from "../state/event-log.ts";
|
|
4
|
-
import type { CrewAgentRecord } from "./crew-agent-runtime.ts";
|
|
5
|
-
import { upsertCrewAgent } from "./crew-agent-records.ts";
|
|
6
|
-
|
|
7
|
-
export interface CrewControlConfig {
|
|
8
|
-
enabled: boolean;
|
|
9
|
-
needsAttentionAfterMs: number;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const DEFAULT_NEEDS_ATTENTION_MS = 60_000;
|
|
13
|
-
|
|
14
|
-
function positiveInt(value: unknown): number | undefined {
|
|
15
|
-
return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : undefined;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function resolveCrewControlConfig(config: PiTeamsConfig | undefined): CrewControlConfig {
|
|
19
|
-
const raw = config as PiTeamsConfig & { control?: { enabled?: unknown; needsAttentionAfterMs?: unknown } } | undefined;
|
|
20
|
-
return {
|
|
21
|
-
enabled: raw?.control?.enabled === false ? false : true,
|
|
22
|
-
needsAttentionAfterMs: positiveInt(raw?.control?.needsAttentionAfterMs) ?? DEFAULT_NEEDS_ATTENTION_MS,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function activityAgeMs(agent: CrewAgentRecord, now = Date.now()): number | undefined {
|
|
27
|
-
const timestamp = agent.progress?.lastActivityAt ?? agent.startedAt;
|
|
28
|
-
if (!timestamp) return undefined;
|
|
29
|
-
const ms = now - new Date(timestamp).getTime();
|
|
30
|
-
return Number.isFinite(ms) ? Math.max(0, ms) : undefined;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function formatActivityAge(agent: CrewAgentRecord, now = Date.now()): string | undefined {
|
|
34
|
-
const age = activityAgeMs(agent, now);
|
|
35
|
-
if (age === undefined) return undefined;
|
|
36
|
-
if (age < 1000) return "active now";
|
|
37
|
-
const seconds = Math.floor(age / 1000);
|
|
38
|
-
if (seconds < 60) return agent.progress?.activityState === "needs_attention" ? `no activity for ${seconds}s` : `active ${seconds}s ago`;
|
|
39
|
-
const minutes = Math.floor(seconds / 60);
|
|
40
|
-
return agent.progress?.activityState === "needs_attention" ? `no activity for ${minutes}m` : `active ${minutes}m ago`;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function applyAttentionState(manifest: TeamRunManifest, agent: CrewAgentRecord, config: CrewControlConfig, now = Date.now()): CrewAgentRecord {
|
|
44
|
-
if (!config.enabled || agent.status !== "running") return agent;
|
|
45
|
-
const age = activityAgeMs(agent, now);
|
|
46
|
-
if (age === undefined || age <= config.needsAttentionAfterMs) return agent;
|
|
47
|
-
if (agent.progress?.activityState === "needs_attention") return agent;
|
|
48
|
-
const updated: CrewAgentRecord = {
|
|
49
|
-
...agent,
|
|
50
|
-
progress: {
|
|
51
|
-
...(agent.progress ?? { recentTools: [], recentOutput: [], toolCount: agent.toolUses ?? 0 }),
|
|
52
|
-
activityState: "needs_attention",
|
|
53
|
-
},
|
|
54
|
-
};
|
|
55
|
-
upsertCrewAgent(manifest, updated);
|
|
56
|
-
appendEvent(manifest.eventsPath, {
|
|
57
|
-
type: "agent.needs_attention",
|
|
58
|
-
runId: manifest.runId,
|
|
59
|
-
taskId: agent.taskId,
|
|
60
|
-
message: `${agent.agent} needs attention (no observed activity for ${Math.floor(age / 1000)}s).`,
|
|
61
|
-
data: { agentId: agent.id, ageMs: age, needsAttentionAfterMs: config.needsAttentionAfterMs },
|
|
62
|
-
});
|
|
63
|
-
return updated;
|
|
64
|
-
}
|
|
1
|
+
import type { PiTeamsConfig } from "../config/config.ts";
|
|
2
|
+
import type { TeamRunManifest } from "../state/types.ts";
|
|
3
|
+
import { appendEvent } from "../state/event-log.ts";
|
|
4
|
+
import type { CrewAgentRecord } from "./crew-agent-runtime.ts";
|
|
5
|
+
import { upsertCrewAgent } from "./crew-agent-records.ts";
|
|
6
|
+
|
|
7
|
+
export interface CrewControlConfig {
|
|
8
|
+
enabled: boolean;
|
|
9
|
+
needsAttentionAfterMs: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const DEFAULT_NEEDS_ATTENTION_MS = 60_000;
|
|
13
|
+
|
|
14
|
+
function positiveInt(value: unknown): number | undefined {
|
|
15
|
+
return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function resolveCrewControlConfig(config: PiTeamsConfig | undefined): CrewControlConfig {
|
|
19
|
+
const raw = config as PiTeamsConfig & { control?: { enabled?: unknown; needsAttentionAfterMs?: unknown } } | undefined;
|
|
20
|
+
return {
|
|
21
|
+
enabled: raw?.control?.enabled === false ? false : true,
|
|
22
|
+
needsAttentionAfterMs: positiveInt(raw?.control?.needsAttentionAfterMs) ?? DEFAULT_NEEDS_ATTENTION_MS,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function activityAgeMs(agent: CrewAgentRecord, now = Date.now()): number | undefined {
|
|
27
|
+
const timestamp = agent.progress?.lastActivityAt ?? agent.startedAt;
|
|
28
|
+
if (!timestamp) return undefined;
|
|
29
|
+
const ms = now - new Date(timestamp).getTime();
|
|
30
|
+
return Number.isFinite(ms) ? Math.max(0, ms) : undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function formatActivityAge(agent: CrewAgentRecord, now = Date.now()): string | undefined {
|
|
34
|
+
const age = activityAgeMs(agent, now);
|
|
35
|
+
if (age === undefined) return undefined;
|
|
36
|
+
if (age < 1000) return "active now";
|
|
37
|
+
const seconds = Math.floor(age / 1000);
|
|
38
|
+
if (seconds < 60) return agent.progress?.activityState === "needs_attention" ? `no activity for ${seconds}s` : `active ${seconds}s ago`;
|
|
39
|
+
const minutes = Math.floor(seconds / 60);
|
|
40
|
+
return agent.progress?.activityState === "needs_attention" ? `no activity for ${minutes}m` : `active ${minutes}m ago`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function applyAttentionState(manifest: TeamRunManifest, agent: CrewAgentRecord, config: CrewControlConfig, now = Date.now()): CrewAgentRecord {
|
|
44
|
+
if (!config.enabled || agent.status !== "running") return agent;
|
|
45
|
+
const age = activityAgeMs(agent, now);
|
|
46
|
+
if (age === undefined || age <= config.needsAttentionAfterMs) return agent;
|
|
47
|
+
if (agent.progress?.activityState === "needs_attention") return agent;
|
|
48
|
+
const updated: CrewAgentRecord = {
|
|
49
|
+
...agent,
|
|
50
|
+
progress: {
|
|
51
|
+
...(agent.progress ?? { recentTools: [], recentOutput: [], toolCount: agent.toolUses ?? 0 }),
|
|
52
|
+
activityState: "needs_attention",
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
upsertCrewAgent(manifest, updated);
|
|
56
|
+
appendEvent(manifest.eventsPath, {
|
|
57
|
+
type: "agent.needs_attention",
|
|
58
|
+
runId: manifest.runId,
|
|
59
|
+
taskId: agent.taskId,
|
|
60
|
+
message: `${agent.agent} needs attention (no observed activity for ${Math.floor(age / 1000)}s).`,
|
|
61
|
+
data: { agentId: agent.id, ageMs: age, needsAttentionAfterMs: config.needsAttentionAfterMs },
|
|
62
|
+
});
|
|
63
|
+
return updated;
|
|
64
|
+
}
|