Skip to content

Unbreak main: compare the autoload guard's conditions, not its exact text - #10654

Merged
danielhanchen merged 8 commits into
mainfrom
fix-autoload-guard-anchors
Sep 10, 2026
Merged

danielhanchen merged 8 commits into
mainfrom
fix-autoload-guard-anchors

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Two assertions in tests/studio/test_model_picker_contracts.py fail on a clean checkout of main:

FAILED test_chat_autoload_records_every_validation_failure
FAILED test_a_failed_quant_is_marked_tried_so_the_repo_continues
2 failed, 177 deselected

What happened

Both slice studio/frontend/src/features/chat/api/chat-adapter.ts on the exact one-line text of an autoload guard:

assert "if (autoLoadCancelled || loadAttempts >= MAX_AUTO_LOAD_ATTEMPTS)" in autoload
assert "while (!autoLoadCancelled && loadAttempts < MAX_AUTO_LOAD_ATTEMPTS)" in cascade

#10508 added a validateFailures bound to both guards. That pushed each past the print width, so prettier wrapped them one condition per line:

if (
  autoLoadCancelled ||
  loadAttempts >= MAX_AUTO_LOAD_ATTEMPTS ||
  validateFailures >= MAX_AUTO_VALIDATE_FAILURES
) {

The guards are correct and the behaviour is intact. Only the text the assertions match on moved, and neither assertion was updated, so main has been red on these two since that merge. Nothing has touched the test file since: git log <base>..main -- tests/studio/test_model_picker_contracts.py is empty.

The change

_guard_conjuncts() collapses whitespace and splits the guard on || / &&, and the two assertions now check the conditions they care about are present. That survives a rewrap and survives a later PR adding a fourth condition, which is what broke this one.

It stays strict about the thing the tests exist for. Checked both directions:

  • removing autoLoadCancelled from the if: test_chat_autoload_records_every_validation_failure fails
  • removing loadAttempts < MAX_AUTO_LOAD_ATTEMPTS from the while: test_a_failed_quant_is_marked_tried_so_the_repo_continues fails

Checks

tests/studio/test_model_picker_contracts.py goes from 2 failed to 179 passed. python3 -Werror -m py_compile is clean, and scripts/run_ruff_format.py leaves the file alone, so it stays a fixed point of the hook.

…text

Two assertions in test_model_picker_contracts.py slice chat-adapter.ts on the
exact one-line text of the autoload guards. #10508 added a validateFailures
bound to both, which pushed each past the print width, so prettier wrapped them
and both assertions stopped matching. Neither test was updated, so main fails
them today.

Comparing the SET of conditions instead survives a rewrap and survives a later
PR adding another condition, while still failing when a condition is dropped.
Checked both ways: removing the cancellation check from the if, and removing
the attempts bound from the while, each fail the corresponding test.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-09T22:22:32.648483Z 6bf8a23 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
Three problems with the first version, all reproduced before fixing:

Splitting on both || and && discarded the operator, so flipping the if-guard
from OR to AND still passed. Stopping at the first ) truncated the body when a
condition held a call, and taking the first if in the slice parsed an unrelated
earlier guard, both failing on a legitimate edit.

Balanced parentheses and a required operator fix those, but scanning for the
first guard that matches introduced a fourth: this slice carries the guard
twice, so a condition dropped from one copy was masked by the other. Requiring
every guard that mentions a condition to hold them all is wrong the other way,
since a nearby guard legitimately tests a subset. Counting the complete ones
catches a drop in either copy and leaves the neighbours alone.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

Scanning the raw source counted a commented-out copy of the old guard as live,
so dropping a condition from the real one kept the count at two and the test
passed. Reproduced before fixing. It now reads through _code_only, which this
file already used for the same reason.

The depth scan also treated a parenthesis inside a string as syntax, so a
condition like reason.includes(")") truncated the body and failed the test on
a legitimate edit. Both the balance scan and the operator split now track quote
state, including escapes.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

Two more holes, both reproduced first. A guard-shaped string, such as a
diagnostic message quoting the guard, was counted as a live guard, so a real
condition dropped from the actual guard kept the count at two and the test
passed. And a live guard holding a URL string lost its tail, because the
comment strip removed everything after the // inside the quotes.

Handling comments and quotes separately cannot fix either: each pass needs the
other's answer. One scan now blanks comment bodies and literal contents while
preserving length, so delimiters and offsets still line up. Regex literals are
left alone and the docstring says so; distinguishing them from division needs
real parsing, and a guard holding one would be read with its slashes intact,
which is visible rather than silent.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 04c9039d7f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +91 to +93
Regex literals are left alone. Telling `/` as division from `/` as a regex needs real
parsing, and no guard this file inspects contains one; a guard that did would be read
with its slashes intact, which is visible rather than silent.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Blank regex literals before enumerating guards

When the sliced TypeScript contains a regex such as /if (autoLoadCancelled || loadAttempts >= MAX_AUTO_LOAD_ATTEMPTS)/, _parenthesised_bodies counts its contents as a live guard, so weakening one of the two real guards can still leave expected = 2 and let the regression pass; similarly, a regex containing ) inside a real guard truncates the parsed condition and causes a false failure. Fresh evidence after the earlier review is that this final implementation explicitly leaves regex literals unblanked, so the quote/comment fixes do not cover this case; blank regex bodies too or use a JavaScript parser.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Both slices this helper is pointed at contain no regex literal at all, and no / of any kind: after _blank_literals_and_comments runs, the autoload slice (chat-adapter.ts:3078 to EOF, 4,976 lines) and the cascade slice (3553-3656) each hold zero / characters. Every one of the file's 45 regex literals sits between lines 593 and 2477, ahead of where either slice starts.

Tested rather than argued. Adding realistic regexes inside the slice (.replace(/\.gguf$/i, ""), .split(/[,\s]+/), /^\d+$/.test(id) || autoLoadCancelled, and the two shapes this comment names: a regex holding ) inside a live guard, and /\)/) leaves both assertions green. The only silent wrong verdict I could produce needs a regex whose body is the guard's own source text, /if (autoLoadCancelled || loadAttempts >= MAX_AUTO_LOAD_ATTEMPTS)/; with that plus a condition dropped from a real guard the count returns to 2 and the test passes, but nothing resembling it would be written here. The only realistic red is a regex carrying a backtick, which reddens loudly with the guard printed. That is the trade the docstring already picks: slashes left intact are visible rather than silent.

out.append(" ")
i += 2
continue
if char in "\"'`":

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Track nested templates while blanking literals

When a template interpolation contains a nested template literal, this loop treats the nested backtick as the end of the outer literal and leaves the nested contents visible to the guard scanner. For example, a diagnostic shaped like `outer ${`if (autoLoadCancelled || loadAttempts >= MAX_AUTO_LOAD_ATTEMPTS)`}` supplies a second matching guard after one real guard is weakened, so expected = 2 still passes; a quoted backtick inside an interpolation can instead make the scanner blank the following live guards and fail harmless code. Fresh evidence beyond the earlier template-literal fix is that the new single-pass implementation has no interpolation/nesting state, so nested template contents remain exposed.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Neither slice contains a nested template literal, and the desync needs an odd number of backticks in a region, which nesting cannot produce: an interpolation holding ${a ? \x` : `y`} contributes backticks in pairs, so the scanner mis-labels the interior and re-syncs at the outer close. Measured on the real files: the cascade slice's 6 backticks are all delimiters (6 raw, 6 surviving), and adding a realistic nested template to the autoload slice (`sweep ${v ? `q:${v}` : `plain`}``) leaves both assertions green.

The false pass needs the nested template to contain the guard's own source text, which is the same unrealistic construction as the regex item. The quoted-backtick variant does redden, but only loudly and only for code that has no reason to exist here (id.replace("\", "")`); nothing in the two slices carries a backtick inside a quoted string outside a comment.

def _parenthesised_bodies(source: str, keyword: str):
"""Each `keyword (...)` body in `source`, whitespace collapsed, parentheses balanced."""
source = _blank_literals_and_comments(source)
for match in re.finditer(rf"\b{re.escape(keyword)}\s*\(", source):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Exclude member calls from guard enumeration

Because the pattern only requires a word boundary before the keyword, valid calls such as policy.if(autoLoadCancelled || loadAttempts >= MAX_AUTO_LOAD_ATTEMPTS) and loop.while(!autoLoadCancelled && loadAttempts < MAX_AUTO_LOAD_ATTEMPTS) are enumerated as control-flow guards. In either contract, adding such a call while weakening a real guard can preserve the expected match count and let the regression pass, so the matcher should ensure if or while is actually a standalone statement keyword rather than a property or $-prefixed identifier.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

.if( and .while( do not occur anywhere in the frontend: zero matches for [.$]\s*(if|while)\s*\( across all 1,371 .ts/.tsx files under studio/frontend/src, and zero across the whole studio tree (grep verified against a positive control so the pattern is known to match policy.if( and $if(). if and while are reserved words; no lint-clean TypeScript here uses either as a method name, and neither object in this comment's example exists in these scopes.

The mechanism does work if you write it. Dropping autoLoadCancelled from the first if guard and adding policy.if(autoLoadCancelled || loadAttempts >= MAX_AUTO_LOAD_ATTEMPTS) restores the count to 2 and the test passes, and the same holds for the while contract with loop.while(...). But that is not an edit anyone would make, so the count keeps its verdict on every mutation of this source I could construct: dropping a required condition or flipping the operator reddens each contract, a reformat stays green.

test_desktop_startup_waits_for_auth_without_intermediate_handoff asserted the
exact line 'const showApp = status === "running" && desktopAuthReady;'. That
binding has since been renamed to canMountApp and showApp now also requires
appShellReady, so the assertion fails on a clean checkout of main even though
the contract it protects is intact.

Match the condition and require the resulting binding to condition the mount in
the markup. Removing desktopAuthReady, removing the status check, or taking the
gate out of the mount all still fail; renaming the binding no longer does.
chatgpt-codex-connector[bot]

This comment was marked as resolved.

memory-unified-apu.test.ts pinned the panel's non-Apple unified-memory branch
with two source anchors and a 160 character budget between them. #10627
(ab28823) rewrote that branch to return a record rather than a number, so the
second anchor no longer exists anywhere in the file and the test has failed on
main ever since. The rule it guards survived the rewrite and got stricter: the
old Math.max fell back to the carved GPU window whenever the host RAM reading
was missing, which is the substitution the assertion message forbids, and the
new code reports known: false instead.

Move the rule into gpu-vram.ts as resolveFreeGpuCapacityGb, beside the
aggregates it is built from and reachable from the test runner, and have the
panel call it. The test now runs the rule: a non-Apple unified pool answers from
the host view rather than the window, an unread host figure is reported unread
rather than replaced by the window, and Apple and discrete hosts still answer
from their own free VRAM. One source assertion remains, for the one thing that
is still not reachable from here, which flags the panel hands over, and it no
longer depends on the distance between them.
…x three holes

Both contract files grew their own copy of the same scan, and the copies had
drifted. One module now, beside the other _-prefixed helpers in this directory.

Three defects went with the move, each reproduced first:

- A ternary was accepted without looking at its arms, so {gate ? null : <Shell />}
  passed while mounting the shell exactly when the gate is false. The true arm is
  now checked.
- The gate declaration was matched as exact text, so rewrapping it across lines
  turned a behaviour-preserving edit red. That is the failure this branch exists
  to fix, reintroduced two tests over. Operands are compared instead.
- Splitting stopped at depth zero, so (a || b) || c reported no complete guard.
  Same-operator groups are flattened and redundant operand parens stripped.

binding_joining locates declarations in the blanked source but reads operands from
the original at the same offsets: blanking empties string literals, and
status === "running" is a required operand.

Matrix, both call sites: drop a condition FAIL, flip the operator FAIL, gate out of
the mount FAIL, inverted ternary FAIL; rename, rewrap, redundant grouping and an
added prerequisite all PASS.
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 6bf8a23228

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 92141b3 into main Sep 10, 2026
39 of 42 checks passed
@danielhanchen
danielhanchen deleted the fix-autoload-guard-anchors branch September 10, 2026 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants