Skip to content

fix(sandbox): correlate mediation by test case - #327

Merged
Jake Present (jakepresent) merged 10 commits into
mainfrom
jake/fix-sandbox-case-correlation
Aug 28, 2026
Merged

fix(sandbox): correlate mediation by test case#327
Jake Present (jakepresent) merged 10 commits into
mainfrom
jake/fix-sandbox-case-correlation

Conversation

@jakepresent

@jakepresent Jake Present (jakepresent) commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

Problem

ASSERT carried a case_id, but the sandbox path did not propagate and enforce one consistent identity end to end. That made case-bound mocks incomplete, allowed ambiguous rule selection, and left evidence unable to prove which case rule fired.

Fix

  • propagate the effective ASSERT case ID through SandboxedEndpointSession, the stock container environment, and the optional JSON request sent to an already-running sandbox endpoint;
  • preserve the ordinary target.endpoint request shape when no case ID is configured;
  • use one case-ID resolver for mock selection and evidence, and reject conflicting legacy/session IDs before any pass-through tool executes;
  • define rule precedence as exact case ID, matching case glob, generic fallback, argument specificity, then file order;
  • partition built-in scenario state by case while preserving the legacy current_state(scenario) extension contract;
  • serialize matching and state transition so parallel calls cannot consume the same scenario step;
  • include mock_source, replay detail, and matched_case_id in judge-visible evidence; and
  • keep the packaged and example stock servers aligned.

Verification

  • Focused case, endpoint, sandbox, and runtime suites: 124 passed.
  • Full exact-head Python suite: 1461 passed, 23 skipped, 840 subtests passed.
  • Regression coverage includes endpoint payload propagation, exact/glob/generic precedence, conflicting identities, legacy backend transitions, parallel scenario resolution, and judge-visible matched-rule evidence.
  • Branch includes current main, including the merged host-owned egress ledger correction.

Risk

The endpoint case_id field is optional. Existing ordinary endpoints and mock rules without a case binding retain their previous behavior. Legacy custom scenario backends remain usable, but cannot gain per-case state isolation until they adopt the new case-aware contract.

A clean local stock-image rebuild remains blocked by the local Docker builder's external PyPI TLS failure. The final combined correction tree was separately exercised with the exact branch source overlaid onto the existing stock image; clean-image CI is owned by #328.

sandbox-action-mediation resolve built its MockCall with no case_id and
had no way to supply one, so once case-bound mocks existed the tool
reported the uncorrelated-run branch for every call. That is worse than
missing: it confidently prints a mock the run will not use.

  - add --case-id and thread it into both the find() and resolve() calls
  - echo the case being resolved as
  - name the matched rule's case binding when one applies
  - when no --case-id is given but the setup declares case-bound rules,
    say so instead of passing the default branch off as the answer

Also document the case_id precedence rule in the example README. It is a
tier, not another matcher: a case-bound rule outranks every unbound rule
regardless of argument specificity, which is deliberate but surprising if
you assume it just adds a condition.

@changliu2 Chang Liu (changliu2) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two blockers on exact head 46d583a:

  1. Endpoint sandbox targets do not receive the per-test-case ID. The endpoint session still posts only message and history, so case-bound mocks cannot vary correctly across an already-running endpoint.
  2. ScenarioBackend.current_state() now accepts an optional case ID, but callers always pass it; legacy subclasses that override the prior one-argument method raise TypeError.

Please also align wildcard/no-case matching and evidence case-ID precedence so mock selection and recorded evidence cannot disagree.

@changliu2 Chang Liu (changliu2) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The endpoint propagation, legacy ScenarioBackend.current_state() compatibility, conflict handling, concurrency isolation, and evidence case-proof fixes now look resolved. One blocker remains: a mock rule with case_id: "*" still matches a call with no case ID because the missing ID is matched as an empty string and * succeeds. This contradicts the CLI/README guarantee that case-bound rules cannot match uncorrelated calls and can select the wrong mock. Please explicitly skip every case-bound rule when call.case_id is absent, and add library and CLI coverage for case_id: "*".

@jakepresent

Copy link
Copy Markdown
Collaborator Author

Fixed the remaining wildcard case-binding issue in 5781f1e: every case-bound rule, including case_id: "*", now requires a non-empty correlated case ID. I added library and CLI regressions for missing and empty IDs, and all checks are green. Chang Liu (@changliu2) ready for re-review.

@changliu2 Chang Liu (changliu2) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed exact head 5781f1e. The wildcard case-binding blocker is resolved: every case-bound rule, including case_id: "*", is now rejected when the call has no case ID, and the library/CLI coverage exercises both uncorrelated and correlated wildcard paths. The earlier endpoint propagation, legacy backend compatibility, conflict handling, concurrency isolation, and evidence case-proof fixes remain intact. Approved.

@jakepresent
Jake Present (jakepresent) merged commit c20da90 into main Aug 28, 2026
15 checks passed
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.

2 participants