-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
585 lines (473 loc) · 20.5 KB
/
Copy pathmodels.py
File metadata and controls
585 lines (473 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
"""Pydantic models: experiment definitions and result objects."""
from __future__ import annotations
import re
from typing import Literal
from pydantic import BaseModel, ConfigDict, Field, model_validator
from ._util import slugify
ReasoningEffort = Literal["none", "low", "medium", "high", "xhigh", "max"]
Mode = Literal["interactive", "plan", "autopilot"]
ProviderType = Literal["openai", "azure", "anthropic"]
WireApi = Literal["completions", "responses"]
# Outcome of a single trial, distinguishing *harness/infra* failures from the
# experiment's own (verify) result:
# * ``ok`` -- Copilot ran to completion (verify pass/fail is separate).
# * ``copilot_failed`` -- Copilot was invoked but errored out / produced no session
# log (e.g. authentication failure, bad working dir).
# * ``harness_error`` -- the harness pipeline itself raised (provisioning, diffing).
TrialStatus = Literal["ok", "copilot_failed", "harness_error"]
# Roll-up of a run: every trial ``ok`` -> ``completed``; some but not all failed ->
# ``partial``; nothing ran successfully -> ``failed``.
RunStatus = Literal["completed", "partial", "failed"]
# Environment variable names whose *value* should be masked in stored artifacts.
# A safety net: BYOK secrets belong in ``ProviderConfig`` (already redacted), but a
# token set via the free-form ``Variant.env`` escape hatch must never be persisted.
_SECRET_ENV_HINT = re.compile(
r"key|token|secret|password|passwd|bearer|credential|authorization", re.IGNORECASE
)
def _redact_env(env: dict[str, str]) -> dict[str, str]:
"""Mask values of environment variables whose name hints at a secret."""
return {k: ("***redacted***" if _SECRET_ENV_HINT.search(k) else v) for k, v in env.items()}
# --------------------------------------------------------------------------- #
# Experiment definition
# --------------------------------------------------------------------------- #
class ProviderConfig(BaseModel):
"""Bring-Your-Own-Key custom model provider.
Translated to ``COPILOT_PROVIDER_*`` environment variables when a variant
using this provider is executed. Works with any OpenAI-compatible endpoint
(Ollama, vLLM, Foundry Local), Azure OpenAI, or Anthropic.
"""
model_config = ConfigDict(extra="forbid")
base_url: str
type: ProviderType = "openai"
api_key: str | None = None
bearer_token: str | None = None
wire_api: WireApi | None = None
model_id: str | None = None
wire_model: str | None = None
azure_api_version: str | None = None
max_prompt_tokens: int | None = None
max_output_tokens: int | None = None
def to_env(self) -> dict[str, str]:
"""Render the provider config as Copilot CLI environment variables."""
env: dict[str, str] = {
"COPILOT_PROVIDER_BASE_URL": self.base_url,
"COPILOT_PROVIDER_TYPE": self.type,
}
if self.api_key:
env["COPILOT_PROVIDER_API_KEY"] = self.api_key
if self.bearer_token:
env["COPILOT_PROVIDER_BEARER_TOKEN"] = self.bearer_token
if self.wire_api:
env["COPILOT_PROVIDER_WIRE_API"] = self.wire_api
if self.model_id:
env["COPILOT_PROVIDER_MODEL_ID"] = self.model_id
if self.wire_model:
env["COPILOT_PROVIDER_WIRE_MODEL"] = self.wire_model
if self.azure_api_version:
env["COPILOT_PROVIDER_AZURE_API_VERSION"] = self.azure_api_version
if self.max_prompt_tokens is not None:
env["COPILOT_PROVIDER_MAX_PROMPT_TOKENS"] = str(self.max_prompt_tokens)
if self.max_output_tokens is not None:
env["COPILOT_PROVIDER_MAX_OUTPUT_TOKENS"] = str(self.max_output_tokens)
return env
def redacted(self) -> dict:
"""Serializable representation with secrets masked, for stored artifacts."""
data = self.model_dump(exclude_none=True)
for secret in ("api_key", "bearer_token"):
if data.get(secret):
data[secret] = "***redacted***"
return data
class Task(BaseModel):
"""What Copilot is asked to do, and how to provision/verify the workspace."""
model_config = ConfigDict(extra="forbid")
name: str | None = None
"""Human-readable task name. When set, it seeds the task's directory slug;
otherwise a positional ``task-NNN`` slug is assigned by the experiment."""
prompt: str
"""The prompt handed to ``copilot -p``."""
fixture: str | None = None
"""Path (relative to the experiment repo) to a directory copied as the
starting workspace for every trial."""
repo: str | None = None
"""Git URL to clone as the starting workspace (alternative to ``fixture``)."""
ref: str | None = None
"""Branch, tag, or commit to check out when ``repo`` is used."""
setup: list[str] = Field(default_factory=list)
"""Shell commands run in the workspace after provisioning, before Copilot."""
verify: str | None = None
"""Shell command run in the workspace after Copilot finishes. Exit code 0
means the trial succeeded. ``None`` means effectiveness is not measured."""
class Variant(BaseModel):
"""A single parameterization of an experiment (one cell of the matrix)."""
model_config = ConfigDict(extra="forbid")
name: str
model: str | None = None
reasoning_effort: ReasoningEffort | None = None
agent: str | None = None
mode: Mode | None = None
allow_tools: list[str] = Field(default_factory=list)
deny_tools: list[str] = Field(default_factory=list)
allow_all_tools: bool = True
provider: ProviderConfig | None = None
env: dict[str, str] = Field(default_factory=dict)
extra_args: list[str] = Field(default_factory=list)
trials: int = 1
@property
def slug(self) -> str:
return slugify(self.name)
def stored(self) -> dict:
"""Serializable representation with provider and env secrets redacted."""
data = self.model_dump(exclude_none=True)
if self.provider is not None:
data["provider"] = self.provider.redacted()
if self.env:
data["env"] = _redact_env(self.env)
return data
class Experiment(BaseModel):
"""A named task suite plus the matrix of variants to run it under.
The comparison matrix is ``Tasks × Variants × Trials``. Provide either a
single ``task`` (sugar for a one-task suite) or an explicit list of
``tasks`` -- exactly one of the two. See ADR-0012.
"""
model_config = ConfigDict(extra="forbid")
name: str
description: str = ""
task: Task | None = None
tasks: list[Task] = Field(default_factory=list)
variants: list[Variant]
@model_validator(mode="after")
def _check_task_suite(self) -> Experiment:
if self.task is not None and self.tasks:
raise ValueError("Provide either 'task' or 'tasks', not both.")
if self.task is None and not self.tasks:
raise ValueError("An experiment must define a 'task' or a non-empty 'tasks' list.")
return self
@property
def slug(self) -> str:
return slugify(self.name)
def iter_tasks(self) -> list[tuple[str, Task]]:
"""Return the task suite as an ordered list of ``(task_slug, Task)``.
Slugs come from ``Task.name`` when set, else a positional ``task-NNN``.
Collisions are disambiguated with a numeric suffix so slugs are unique
and stable for directory names and the index.
"""
tasks = self.tasks if self.tasks else ([self.task] if self.task else [])
result: list[tuple[str, Task]] = []
seen: dict[str, int] = {}
for idx, task in enumerate(tasks, start=1):
base = slugify(task.name) if task.name else f"task-{idx:03d}"
if base in seen:
seen[base] += 1
slug = f"{base}-{seen[base]}"
else:
seen[base] = 1
slug = base
result.append((slug, task))
return result
# --------------------------------------------------------------------------- #
# Result objects
# --------------------------------------------------------------------------- #
class ModelMetric(BaseModel):
"""Per-model usage from ``session.shutdown.modelMetrics`` (multi-model sessions)."""
model: str
requests: int = 0
input_tokens: int = 0
output_tokens: int = 0
cache_read_tokens: int = 0
cache_write_tokens: int = 0
reasoning_tokens: int = 0
aiu: float | None = None
class TokenEconomics(BaseModel):
"""Session-level token accounting and AIU cost.
Parsed from ``session.shutdown`` (authoritative totals) plus ``session.compaction_*`` and
``session.truncation`` events. Every field is best-effort: a session that never emitted a
``session.shutdown`` (e.g. aborted) leaves the totals ``None``. Cost is expressed in **AIU**
(GitHub's billing unit; ``totalNanoAiu / 1e9``). Premium requests are intentionally ignored
(GitHub stopped using them on 2026-06-01).
"""
# Token-type decomposition (the paper's taxonomy).
input_tokens_noncached: int | None = None
cache_read_tokens: int | None = None
cache_write_tokens: int | None = None
output_tokens: int | None = None
reasoning_tokens: int | None = None
input_tokens_total: int | None = None
total_tokens: int | None = None
# Cost (AIU).
aiu: float | None = None
aiu_by_type: dict[str, float] = Field(default_factory=dict)
# Throughput.
api_duration_ms: int | None = None
n_requests: int | None = None
# Context-window composition at end of session.
system_tokens: int | None = None
tool_definitions_tokens: int | None = None
conversation_tokens: int | None = None
context_tokens: int | None = None
peak_context_tokens: int | None = None
# Context-management dynamics.
n_compactions: int = 0
n_truncations: int = 0
compaction_aiu: float | None = None
tokens_removed_truncation: int | None = None
# Productivity / effectiveness.
files_modified: int | None = None
lines_added: int | None = None
lines_removed: int | None = None
# Per-model split.
model_metrics: list[ModelMetric] = Field(default_factory=list)
class Metrics(BaseModel):
"""Metrics parsed from a single trial's session ``events.jsonl``.
Flat scalars for aggregation and the SQLite index. The richer, nested view lives in
:class:`TokenEconomics` on :class:`SessionAnalysis`; both are derived from the same events.
"""
n_turns: int = 0
n_assistant_messages: int = 0
n_tool_calls: int = 0
n_tool_failures: int = 0
n_warnings: int = 0
models: list[str] = Field(default_factory=list)
duration_s: float | None = None
input_tokens: int | None = None
output_tokens: int | None = None
total_tokens: int | None = None
# Token-type decomposition and AIU cost (from session.shutdown; may be null).
input_tokens_noncached: int | None = None
cache_read_tokens: int | None = None
cache_write_tokens: int | None = None
reasoning_tokens: int | None = None
aiu: float | None = None
aiu_by_type: dict[str, float] = Field(default_factory=dict)
api_duration_ms: int | None = None
n_requests: int | None = None
# Context composition and dynamics.
system_tokens: int | None = None
tool_definitions_tokens: int | None = None
conversation_tokens: int | None = None
context_tokens: int | None = None
peak_context_tokens: int | None = None
n_compactions: int = 0
n_truncations: int = 0
compaction_aiu: float | None = None
# Productivity (from session.shutdown.codeChanges).
files_modified: int | None = None
lines_added: int | None = None
lines_removed: int | None = None
class ToolStat(BaseModel):
"""How often a single tool was invoked in a session, and how often it failed.
``total_duration_ms`` and ``total_result_chars`` aggregate ``toolTelemetry.metrics``
(per-tool latency and the size of the result fed back to the model -- a proxy for the
input-token cost each tool injects into subsequent requests).
"""
name: str
calls: int = 0
failures: int = 0
total_duration_ms: int = 0
total_result_chars: int = 0
class LlmCallSummary(BaseModel):
"""One LLM request reconstructed from Copilot OTel ``chat <model>`` spans."""
turn_id: str | None = None
started_at: str | None = None
ended_at: str | None = None
duration_s: float | None = None
request_model: str | None = None
response_model: str | None = None
response_id: str | None = None
finish_reasons: list[str] = Field(default_factory=list)
input_tokens: int | None = None
cache_creation_input_tokens: int | None = None
output_tokens: int | None = None
total_tokens: int | None = None
aiu: float | None = None
server_duration_ms: int | None = None
current_tokens: int | None = None
token_limit: int | None = None
interaction_id: str | None = None
service_request_id: str | None = None
class TurnSummary(BaseModel):
"""One assistant turn (``assistant.turn_start`` .. ``assistant.turn_end``)."""
turn_no: int
turn_id: str | None = None
started_at: str | None = None
ended_at: str | None = None
duration_s: float | None = None
assistant_messages: int = 0
text_preview: str | None = None
tools: list[str] = Field(default_factory=list)
output_tokens: int | None = None
input_tokens: int | None = None
cache_creation_input_tokens: int | None = None
aiu: float | None = None
api_duration_ms: int | None = None
class PhaseStat(BaseModel):
"""Aggregated activity for one temporal phase of a session.
The session's turns are split into five contiguous, near-equal groups
(early -> later), echoing the phase-level analysis in Bai et al. (the paper's
Finding #6: context construction dominates early phases, generation later
ones). Only native per-turn signals are aggregated: output tokens, tool
activity, and duration. Per-phase *input*/cache/cost are intentionally
omitted; OTel can provide per-call economics, but phase-level attribution is
kept separate from native event analysis -- see ``docs/analysis.md``.
"""
name: str
turn_from: int
turn_to: int
n_turns: int = 0
n_tool_calls: int = 0
output_tokens: int = 0
duration_s: float | None = None
output_share: float | None = None
class SessionAnalysis(BaseModel):
"""A structured, human-friendly overview of a single Copilot session log.
Derived purely from a session's ``events.jsonl``. Kept as plain data (no
rendering) so it can be serialized to ``analysis.json``, rendered in the CLI
with Rich, or consumed by a future web explorer.
"""
# Session header / context.
session_id: str | None = None
copilot_version: str | None = None
producer: str | None = None
models: list[str] = Field(default_factory=list)
reasoning_effort: str | None = None
repository: str | None = None
branch: str | None = None
cwd: str | None = None
started_at: str | None = None
finished_at: str | None = None
duration_s: float | None = None
# Totals.
n_events: int = 0
n_turns: int = 0
n_user_messages: int = 0
n_assistant_messages: int = 0
n_tool_calls: int = 0
n_tool_failures: int = 0
n_warnings: int = 0
n_hooks: int = 0
input_tokens: int | None = None
output_tokens: int | None = None
total_tokens: int | None = None
# Token-type decomposition, AIU cost, context composition/dynamics, and productivity.
economics: TokenEconomics = Field(default_factory=TokenEconomics)
# Breakdowns.
tools: list[ToolStat] = Field(default_factory=list)
llm_calls: list[LlmCallSummary] = Field(default_factory=list)
turns: list[TurnSummary] = Field(default_factory=list)
phases: list[PhaseStat] = Field(default_factory=list)
warnings: list[str] = Field(default_factory=list)
event_type_counts: dict[str, int] = Field(default_factory=dict)
class TrialResult(BaseModel):
trial_no: int
session_id: str
exit_code: int
duration_s: float
success: bool | None = None
metrics: Metrics = Field(default_factory=Metrics)
# Harness/infra outcome (orthogonal to ``success``, which is the experiment's
# verify result). ``error`` is a short human-readable message; ``error_artifact``
# names the file inside the trial directory to inspect for the full story.
status: TrialStatus = "ok"
error: str | None = None
error_artifact: str | None = None
@property
def failed(self) -> bool:
"""True when the trial did not run cleanly (harness or copilot failure)."""
return self.status != "ok"
class TaskResult(BaseModel):
"""All trials of one task within a variant (one cell of the suite × matrix)."""
task_slug: str
task_name: str | None = None
prompt: str | None = None
trials: list[TrialResult] = Field(default_factory=list)
@property
def success_rate(self) -> float | None:
"""Mean trial success for this task (the variability-aware measure)."""
graded = [t.success for t in self.trials if t.success is not None]
if not graded:
return None
return sum(1 for s in graded if s) / len(graded)
@property
def n_failed(self) -> int:
"""Number of trials that did not run cleanly (harness/copilot failures)."""
return sum(1 for t in self.trials if t.failed)
@property
def resolved(self) -> bool | None:
"""Resolved@k: did *any* trial of this task pass (best-of-k)?"""
graded = [t.success for t in self.trials if t.success is not None]
if not graded:
return None
return any(graded)
class VariantResult(BaseModel):
variant: Variant
tasks: list[TaskResult] = Field(default_factory=list)
@property
def all_trials(self) -> list[TrialResult]:
"""Every trial across every task, flattened (for cost/token aggregates)."""
return [t for tr in self.tasks for t in tr.trials]
@property
def success_rate(self) -> float | None:
"""Mean trial success across all tasks and trials of this variant."""
graded = [t.success for t in self.all_trials if t.success is not None]
if not graded:
return None
return sum(1 for s in graded if s) / len(graded)
@property
def mean_resolved_rate(self) -> float | None:
"""Mean over tasks of each task's mean trial success."""
rates = [tr.success_rate for tr in self.tasks if tr.success_rate is not None]
if not rates:
return None
return sum(rates) / len(rates)
@property
def resolved_at_k_rate(self) -> float | None:
"""Fraction of tasks resolved on at least one trial (best-of-k)."""
graded = [tr.resolved for tr in self.tasks if tr.resolved is not None]
if not graded:
return None
return sum(1 for r in graded if r) / len(graded)
class ExperimentRun(BaseModel):
run_id: str
experiment_slug: str
experiment_name: str
experiment_description: str = ""
started_at: str
finished_at: str | None = None
git_base: str | None = None
status: str = "running"
variants: list[VariantResult] = Field(default_factory=list)
@property
def all_trials(self) -> list[TrialResult]:
return [t for vr in self.variants for t in vr.all_trials]
@property
def n_failed_trials(self) -> int:
return sum(1 for t in self.all_trials if t.failed)
def rollup_status(self) -> RunStatus:
"""Derive the run status from its trials' harness/copilot outcomes."""
trials = self.all_trials
if not trials:
return "failed"
failed = self.n_failed_trials
if failed == 0:
return "completed"
if failed == len(trials):
return "failed"
return "partial"
# --------------------------------------------------------------------------- #
# Dry-run (ephemeral plumbing check)
# --------------------------------------------------------------------------- #
class DryRunCheck(BaseModel):
"""One validated stage of the run pipeline during a ``--dry-run``."""
name: str
ok: bool
detail: str = ""
class DryRunReport(BaseModel):
"""Result of an ephemeral dry-run: did each pipeline stage do its job?
A dry-run runs the whole pipeline (with the mock invoker) inside a throwaway
directory, records these checks, then deletes everything. Nothing is
persisted; only this report survives.
"""
experiment: str
checks: list[DryRunCheck] = Field(default_factory=list)
@property
def ok(self) -> bool:
return all(c.ok for c in self.checks)