feature: Backtest recorded sessions to answer what-if questions on time, cost and quality

未关闭
#1,835 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

这个 Issue 还没有评估数据。

描述

Summary

A Cortex user wants to answer "what if" questions about a recorded session. For example:

  • What if this run had used another model?
  • What if reasoning had been off?
  • What if a skill had worked differently?

The answer must cover three things: time, cost and work quality. A backtest must not change any state outside the machine. It must not push, open PRs, post comments or call live services.

This issue records what we explored and measured, and a proposed design. The design is not final: the open decisions are at the end.

What we did, and what it can and cannot answer

We analyzed 14 /sdd-fast runs from Cortex recordings, Claude Code transcripts and the workflow journals. Then we built a re-timing replay (a script outside Cortex):

  • It takes each subagent's measured start and end, and applies the workflow's rules: dependencies, one merge at a time per PR, the PR review after the merges.
  • It recomputes the run's wall-clock time when one input changes. For example: "this merge takes 20 s", "this task does not wait for that one", "this fix round does not happen".
  • It matches the real wall-clock time of all 14 runs to within 0.4 minutes.

We used it to choose and backtest changes to the skill. It gave real results. For example, skipping the gate after a clean cherry-pick saved 38 of 688 minutes over 11 runs, and a critical-path sum had said 52.

Its limit: it runs nothing again. So it cannot answer "what if another model" or "what if no reasoning". With another model, the code, the review findings and the fix rounds would all change. A re-timing assumes that they stay the same.

Three levels of backtest

Level What it does What it answers Outside effects
1. Re-time Recompute the schedule from the recorded step times, with changed inputs Time, if the work stays the same None
2. Re-ask Send each recorded model request again, with another model or setting, and compare the answer Per-call time, tokens, cost, and agreement with the original answer Model calls only
3. Re-run Run the whole task again in a sandbox, with all outside effects blocked Time, cost and quality of a different result Model calls only, if the sandbox holds

All three are needed. Time and cost come from levels 1 and 2. Quality needs level 3.

How to measure quality

Proposed, and agreed in the discussion: the main measure is a hidden exam made from the real result.

  1. The main measure: the hidden exam. The original run's merged PR is the reference. Its tests run against the code that the re-run wrote. Score: the share of tests that pass, plus the full gate.
    • The re-run must not see the merged PR's new test files before the exam.
    • A run whose PR has not merged has no reference yet.
  2. Blocking review findings per task, by kind. In the 14 runs, the 29 blocking findings were:
    • 9 real bugs;
    • 8 missing tests;
    • 7 false alarms (a gate setup problem or a flaky test);
    • 2 tests that did not test what they claimed;
    • 2 plan or spec items left out;
    • 1 process problem.
  3. What happened after real runs: CI, human requests for changes, follow-up fixes, reverts. This checks that measures 1 and 2 predict real outcomes.
  4. Per-call agreement, for level 2: the same tool call, the same edit, the same review findings. It is cheap, but it says only "different", not "worse".
  5. A model judge: only to break a tie.

What Cortex has today, measured

  • Every model request and response, in full, including tool output, which reaches the model in the next request. The archive keeps exact bytes (?raw=1, from #1494 and #1669). Search over bodies works: one search found the flaky test in 105 events in seconds.
  • Per-call ttfbMs, durationMs, tokens, cost, host, status and error kind. These found things that transcripts do not show:
    • Waiting for the first token was 52–54% of subagent model time.
    • Hidden report-check calls cost 10–14% of each run (three runs measured), and they are not on the critical path.
    • Cache writes were 56% of the subagent cost in one run (#1816).
  • The timeline links each workflow subagent to its parent. It does not show which step started a subagent, or which subagent waited for which. For example, 20 of 20 subagents had a parent and 0 had spawnedAt in one run. We took the task graph from the workflow's own journal.
  • Gaps that matter for a backtest:
    • Most non-model traffic is an opaque tunnel. For example, GitHub traffic is CONNECT api.github.com:443, so Cortex records the host and the bytes, but no method, path or body.
    • git push goes over SSH ([email protected]:…), so it never passes through Cortex.
    • Files read from disk that never reach a model are not recorded. A workflow started with scriptPath is an example: 0 events contain its code, while 45 events contain the prompts that it sends.
    • The egress allow list judges only host:port, not the HTTP method. It also applies only to the forward proxy.
    • The inference router keeps the model that the agent asked for, unless the server refuses that model. Script plugins can rewrite a request body, so a script can already set another model or effort.
    • Cortex has a VM sandbox channel (MOCA SBX). The microVMs have no network card, and all egress goes through Cortex's forward proxy over a per-VM socket.

The outside world in the recorded runs

Every run had all of these:

  • a git fetch of upstream main in the plan step, which moves between runs;
  • GitHub reads (87 commands over the 14 runs);
  • GitHub writes (21 gh pr create and similar commands) and git push (17 commands).

Some runs also had:

  • registry and crates.io lookups;
  • reads of the agent's setup under the home folder, for example the skills folder;
  • skill files that changed during the study. sdd-fast.js changed 4 times in two days, so a re-run that starts from the first prompt alone uses today's skill, not the recorded one.

These are from sdd-fast runs, but the design below does not depend on GitHub or on sdd-fast. It must work for any task.

Proposed design for level 3

1. Scope: define the inside, and treat everything else as outside.

No tool can discover everything that any task touched. So the inside is a defined set, and everything else is outside.

  • The inside is copied into the sandbox. At record time, Cortex keeps a snapshot or hashes of it:
    • the working folder at its exact state (the commit plus uncommitted changes);
    • the agent program and its setup files (settings, instruction files, skills, scripts);
    • toolchain versions;
    • local copies of the git remotes that the task used.
  • The outside is reached only through Cortex:
    • The model: a live call. The what-if setting applies here (model, effort, reasoning).
    • Any other host: it gets the response that was recorded the first time. That needs the TLS bridge on for all hosts while recording, because a tunnel has no content to replay.
    • A request with no recorded response: a divergence. The backtest counts it and reports it.

2. No side effects: make them impossible, do not try to detect them.

No tool can classify every command of any task as a read or a write. So the guarantee must come from the structure:

  • No network route except Cortex. That is a MOCA-style microVM with no network card.
  • Cortex in backtest mode refuses everything except the model and recorded responses.
  • No secrets in the sandbox: no SSH keys and no tokens. A route that was missed cannot log in.
  • No shared disk: the sandbox writes to its own copy.
  • No host services: localhost in the sandbox is the sandbox, not the user's live Cortex.

One outside change remains: the model calls themselves. They cost money and they add to budget counters. Those calls get a "backtest" label and a cost limit.

3. Repeats. Models are not deterministic. A backtest runs several repeats and reports the spread of each score.

What is needed

  • Record the inside at session start: the commit and diff, the agent setup files, the toolchain versions, and the git remotes.
  • A backtest mode for recording: the TLS bridge on for all hosts, so non-model responses can be replayed.
  • A sandbox runner: the user starts it, with a microVM and a fresh home folder with the recorded setup.
  • A backtest profile: model, effort and reasoning for every request of a sandbox session, recorded on each event.
  • Egress in backtest mode: deny by default, the model gateway, and recorded responses. Every refusal and divergence is recorded.
  • An exam runner: apply the merged PR's tests to the re-run's tree, run them, and record the pass rate and the review findings.
  • Re-timing inside Cortex: a way for a workflow to declare its task graph (or for Cortex to infer it), so level 1 needs no workflow-specific journal.
  • A side-by-side report: original against backtest, for time, cost and quality, with the spread over repeats.

Open decisions

  1. Which sessions can be backtested? Only sessions recorded with the capture above turned on, or also sessions recorded as they are today? Today's sessions miss the inside snapshot and the content of tunneled hosts.
  2. A request with no recorded response: refuse it, or allow a live read and mark the result?
  3. Where the report lives: an agentop pane, a command, or both.

Related

  • #1494 (closed): lossless capture, so that past sessions can be replayed. #1669 shipped the exact request and response bodies.
  • #1021: shadow a request against a second model. That is level 2, live.
  • #1810: export session events for post-processing.
主要语言
Go
星标
31
派生
49
平均合并
9 小时 43 分钟
30 天内合并 PR
367

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rossoctl/cortex 的其他 Issue

查看 rossoctl/cortex 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。