Skip to content

Feed measured first-pass rate into cost model (#28) - #34

Merged
datacas merged 2 commits into
mainfrom
issue-28-measured-routing-cost
Sep 23, 2026
Merged

datacas merged 2 commits into
mainfrom
issue-28-measured-routing-cost

Conversation

@datacas

@datacas datacas commented Sep 23, 2026

Copy link
Copy Markdown
Owner

Summary

  • In measured mode, snapshot the repository first-pass rate once when the cycle starts and pass it into the routing cost estimate.
  • Keep unknown rates as unknown below the sample threshold, use the conservative default for estimation, and include Rate.explain() in the decision.
  • Record the estimate breakdown and structured rate provenance with routing telemetry. fixed never queries the rate, and neither strategy changes target selection.
  • Add regression coverage and update the README and instrumentation docs.

Verification

  • rtk python3 -m unittest discover -s tests -v — 477 tests passed.
  • rtk python3 scripts/validate-package.py — passed.
  • rtk git diff --check — passed.

Closes #28

@datacas

datacas commented Sep 23, 2026

Copy link
Copy Markdown
Owner Author

Initial review — PR #34 · Feed measured first-pass rate into cost model (#28)

Verdict: APPROVED — reviewed head b2bc66e9321d47c93fe1f6c17839229865bb69fb. No open finding blocks approval.

Summary and scope

I reviewed the full diff from merge-base 3360d6b to b2bc66e, a single commit touching 6 files: scripts/router.py, scripts/cycle.py, scripts/telemetry.py, tests/test_cycle.py, README.md and docs/instrumentation.md. I checked it against every acceptance criterion in #28:

Criterion Result
measured with ≥ MINIMUM_SAMPLE uses the measured rate and records Rate.explain() Met. The test at exactly 10 observations passes, which also covers the boundary
measured below the minimum uses DEFAULT_FIRST_PASS_RATE and records "unknown" without inventing a value Met. rate_value is None and rate_source=conservative_default
Under fixed, first_pass_rate() is never called Met. A spy test asserts assert_not_called
fixed and measured select the same target Met. The cost is computed but never read by the selection loop
router imports nothing from telemetry and opens no DB Met. CycleRecorder takes the rate snapshot once in __init__ and passes plain values in
Switching back to fixed loses no telemetry Met. A test checks the earlier rows are kept

Two points are design decisions, not defects. Rate.explain() lives on the RoutingDecision and in explain(), but is not stored in the telemetry payload, which is consistent with the store's no-prose promise; the provenance is stored as structured fields (routing_rate_source/value/used/observations/minimum/known). Also, every stage records the estimate for the whole cycle, not for its own role.

Repository conventions came from CONTRIBUTING.md. There is no AGENTS.md or CLAUDE.md.

Findings

[REV-001] · low · open · blocks:no — A failure in the informational cost estimate now aborts target selection, under fixed too

scripts/router.py:411

route() calls estimate_cost() before the selection loop and does not catch errors from it. Its result never influences the target, but if it raises, the stage cannot be routed at all:

  • under measured, a rate outside [0, 1] raises RouterError: first_pass_rate must be between 0 and 1. I observed this with route(..., strategy=MEASURED, first_pass_rate=1.5, rate_known=True);
  • under both strategies, a profile name returned by profile_for that is missing from PROFILE_COST would raise KeyError.

Neither case can happen today: Rate.value is always in [0, 1], and every profile profile_for returns has a price. But fixed routing now depends on a number that #28 describes as explanatory only, and a future profile added without a price would break every route. Consider computing the estimate after selection, or turning an estimate failure into a recorded "estimate unavailable" instead of an exception.

[REV-002] · low · open · blocks:no — The decision-to-payload mapping of the cost is duplicated

scripts/cycle.py:307, scripts/telemetry.py:566

CycleRecorder._cost_record() (used on the blocked path) and Telemetry.record_dispatch() (dispatched path) each map the same ten routing_cost_*/routing_rate_* fields by hand, with slightly different fallbacks: decision.rate_source versus getattr(decision, "rate_source", "default"). docs/instrumentation.md already warns that "two validators agree until one of them is edited". Adding a field to one copy and not the other would make blocked rows and dispatched rows diverge without any error. A single helper used by both paths would prevent that.

[REV-003] · low · open · blocks:no — CHANGELOG.md not updated

CHANGELOG.md

This PR adds behaviour to measured and ten new telemetry payload fields, but has no [Unreleased] entry. #33 (routing strategy) also merged without one, so code_cycle.routing.strategy doesn't appear in the changelog either.

Security triage

This is not a sensitive change, so I skipped the security audit after triage. The diff reads an aggregate rate from the local telemetry store and writes numbers and closed tokens to it, and FIELD_SPECS validates every new field (routing_rate_source is a closed enum; the rest are amount, count or flag). No free text is persisted. It doesn't touch authentication, authorization, secrets, external input, dependencies, CI, or the permission and workspace-policy rules. The PR has no labels.

Commands and observed results

Command / flow Result
python3 -m unittest discover -s tests (checkout at b2bc66e) 477 tests, OK
python3 -m unittest tests.test_cycle.MeasuredCostTests 5/5 OK
python3 scripts/validate-package.py VALID: 12 skills (6 cycle, 6 supporting), shared sections in sync, cross-agent manifests aligned
route('implement', …, strategy=MEASURED, first_pass_rate=1.5, rate_known=True) RouterError: first_pass_rate must be between 0 and 1: 1.5 (REV-001, runtime evidence)
route('implement', …, strategy=MEASURED, first_pass_rate=0.5) rate_source=measured, rate_used=0.5, rate_known=True
route('security', security_sensitive=True).explain() Adds cycle cost estimate: implementation 1.0 + 7.0 expected resolution work + review 6.0 = 14.0

Note: running the suite from a linked git worktree gives 1 failure, test_local_only_rejects_the_live_repository. That test assumes ROOT is the primary checkout. The failure is environmental and unrelated to this PR: the same suite passes 477/477 in the primary checkout at the same SHA and in CI.

Unverified items

  • I didn't run an end-to-end run_cycle.py --config with strategy: measured against real executors. Coverage comes from the unit tests with scripted adapters.

CI

Check State
Validate package / Skills, manifests, and Bash installer ✅ success (×2, push + pull_request)
Validate package / PowerShell installer ✅ success (×2)

4 passed, 0 failed, 0 pending, 0 skipped. Every gate the repository defines ran. The PR is not a draft.

Residual risks

  • Once the selection-policy work starts reading decision.cost, REV-001 will matter more: the estimate will stop being purely informational.
  • The rate is a snapshot taken at the start of the cycle, as designed. Stages in the same run don't see observations recorded during that run.

Verdict: APPROVED. All checks passed on the reviewed head. REV-001 to REV-003 are open but don't block approval.

@datacas

datacas commented Sep 23, 2026

Copy link
Copy Markdown
Owner Author

Resolution update — PR #34

Functional status: RESOLVED. All three low findings are addressed in commit 41a7893391b6de75264a26cf7ef1011f7536e131. No finding blocks approval.

[REV-001] · low · resolved · blocks:no — Informational cost estimate failures no longer stop routing

route() catches cost-estimation failures, records routing_cost_status=unavailable, and keeps target selection independent of the estimate. Regression tests cover fixed routing when the estimator raises, an invalid measured rate, and successful dispatch with an unavailable estimate. Verified by python3 -m unittest discover -s tests (480 tests, OK).

[REV-002] · low · resolved · blocks:no — Routing telemetry fields use one shared mapping

Added routing_decision_fields() in scripts/telemetry.py and use it for both blocked rows in scripts/cycle.py and dispatched rows in scripts/telemetry.py. Tests compare the persisted fields to the shared mapping on both paths. Verified by the full unit suite (480 tests, OK).

[REV-003] · low · resolved · blocks:no — Changelog records issues 27 and 28

Added an [Unreleased] entry for explicit fixed/measured strategy and measured first-pass cost estimates, including best-effort behavior.

Verification

  • python3 -m unittest discover -s tests — 480 tests, passed.
  • python3 scripts/validate-package.py — passed (12 skills, shared sections and manifests aligned).
  • Independent cc-code-review pass — no findings.
  • GitHub checks — 4 passed, 0 failed, 0 pending.

@datacas

datacas commented Sep 23, 2026

Copy link
Copy Markdown
Owner Author

Rereview — PR #34 · Feed measured first-pass rate into cost model (#28)

Verdict: APPROVED — reviewed head 41a7893391b6de75264a26cf7ef1011f7536e131 (previously reviewed head: b2bc66e9321d47c93fe1f6c17839229865bb69fb). No open finding blocks approval.

What changed since the previous review

One commit, 41a7893 Resolve routing cost review findings, touching 6 files:

  • scripts/router.py: route() now wraps estimate_cost() in try/except Exception and adds RoutingDecision.cost_status (available|unavailable).
  • scripts/telemetry.py: new routing_decision_fields(decision), used by record_dispatch, and a new routing_cost_status token in FIELD_SPECS.
  • scripts/cycle.py: the blocked path uses routing_decision_fields, and _cost_record is removed.
  • CHANGELOG.md: [Unreleased] entry for Declare the routing strategy explicitly #27 and Feed the measured first-pass rate into the cost model #28.
  • tests/test_router.py, tests/test_cycle.py: 3 new tests plus field-parity assertions on the dispatched and blocked paths.

I also re-read the full accumulated diff from merge-base 3360d6b to 41a7893. Every acceptance criterion in #28 is still met, and target selection is still identical under fixed and measured.

Status of previous findings

[REV-001] · low · resolved · blocks:no — A failure in the informational cost estimate now aborts target selection, under fixed too

Fixed, and I verified it by running the original reproduction. route('implement', …, strategy=MEASURED, first_pass_rate=1.5, rate_known=True) now returns a target (codex:openai/gpt-5.6-luna high) with cost_status='unavailable' instead of raising RouterError. Removing reviewer from PROFILE_COST (a missing price) also routes normally, with cost_status='unavailable'. The new tests test_an_unavailable_cost_estimate_does_not_prevent_fixed_routing, test_an_invalid_measured_rate_only_disables_the_estimate and test_cost_estimate_failure_is_recorded_without_stopping_dispatch pass.

[REV-002] · low · resolved · blocks:no — The decision-to-payload mapping of the cost is duplicated

Fixed and verified. grep finds no routing_cost_* or routing_rate_* mapping left in scripts/cycle.py; both paths go through telemetry.routing_decision_fields. The tests now compare every persisted field against that mapping, on both the dispatched row (test_the_routing_decision_is_on_every_dispatch_row) and the blocked row (BlockedRoutingTests), and they pass.

[REV-003] · low · resolved · blocks:no — CHANGELOG.md not updated

Fixed. CHANGELOG.md has an [Unreleased] entry covering both strategies, the measured rate and the best-effort behaviour of the estimate (#27, #28). I checked this by reading the file; there is nothing to execute.

New findings

[REV-004] · low · open · blocks:no — An estimate marked unavailable records no cause and keeps the rejected rate as measured

scripts/router.py:414, scripts/telemetry.py:439

The except Exception catches any error, including programming errors such as TypeError or NameError, and keeps no trace of the cause. The row only says routing_cost_status=unavailable. In addition, when the failure comes from an invalid rate, the row still stores that rate as a valid measurement. Observed at runtime, with a rate of 1.5:

{'routing_rate_source': 'measured', 'routing_rate_value': 1.5, 'routing_rate_known': True, 'routing_cost_status': 'unavailable'}
row ok 1.5

record_stage accepts it because amount does not check the range, so the store holds a "known measured rate" of 1.5 that the estimator rejected. This can't happen today, because Rate.value is always in [0, 1], which is why the severity is low. But the row contradicts itself, and a bug inside estimate_cost would show up as an ordinary unavailable instead of failing a test. Possible fixes:

  • catch only RouterError and KeyError;
  • record a closed token for the cause (for example routing_cost_unavailable_reason: invalid_rate|missing_price);
  • clear rate_value and rate_known when the rate is out of range.

Security triage

This is not a sensitive change, so I skipped the security audit after triage. The incremental commit only restructures how local telemetry is recorded and adds one closed token (routing_cost_status). It doesn't touch authentication, authorization, secrets, external input, dependencies, CI, or the permission and workspace-policy rules. The PR has no labels.

Verification (at 41a7893)

Command / flow Result
python3 -m unittest discover -s tests 480 tests, OK
python3 -m unittest tests.test_router.AvailabilityGateTests tests.test_cycle.FullCycleTests tests.test_cycle.BlockedRoutingTests tests.test_cycle.MeasuredCostTests 20 tests, OK
python3 scripts/validate-package.py VALID: 12 skills (6 cycle, 6 supporting), shared sections in sync, cross-agent manifests aligned
git diff --check 3360d6b 41a7893 clean
Reproduction of REV-001 (rate 1.5 and missing price) Routes; cost_status=unavailable
routing_decision_fields + record_stage with rate 1.5 Row written with routing_rate_value=1.5, routing_rate_known=True (REV-004)

Unverified: I didn't run an end-to-end run_cycle.py with strategy: measured against real executors. Coverage comes from unit tests with scripted adapters.

CI (check runs for 41a7893)

Check State
Validate package / Skills, manifests, and Bash installer ✅ success (×2, push + pull_request)
Validate package / PowerShell installer ✅ success (×2)

4 passed, 0 failed, 0 pending, 0 skipped. Every gate the repository defines ran. The PR is not a draft.

Residual risks

  • REV-004 matters once selection policy starts reading decision.cost or the persisted rates: an unexplained unavailable, or a stored rate outside [0, 1], would be hard to diagnose.
  • The rate is a snapshot taken at the start of the cycle, as designed.

Verdict: APPROVED. All checks passed on the reviewed head. REV-001, REV-002 and REV-003 are resolved; REV-004 is open but doesn't block approval.

@datacas
datacas merged commit 63f6c86 into main Sep 23, 2026
4 checks passed
@datacas
datacas deleted the issue-28-measured-routing-cost branch September 24, 2026 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feed the measured first-pass rate into the cost model

1 participant