Skip to content

Commit cb9e4d7

Browse files
anandgupta42claude
andcommitted
fix: Codex re-review round 8 (cycleOrder staleness, ready-pending test coverage)
1. cycleOrder goes stale after an external recents change (local.tsx): the round-7 fix (cycleOrder, re-captured only when the current model fell out of the snapshot) still went stale the moment a PICKER selection reordered `recent` without also knocking the current model out of the old snapshot. Codex's repro: [A, B, C], cycle once, then pick D and A via the picker -> recents [A, D, C, B]; A stays present in the stale [A, B, C] snapshot the whole time, so the round-7 check never re-fires, and D stays permanently unreachable by cycling. Fixed with a `recentsVersion` counter (bumped by a new `setRecent` wrapper — the only path that writes to `modelStore.recent` now) and a `cycleOrderVersion` on the cycle() closure: cycle() re-captures whenever the live version has moved past what it last saw or resynced to, which happens for ANY external write, picker included — while resyncing after its own write so repeated presses with nothing else interleaved keep reusing the same stable snapshot. Regression test: extended cycle-stability.test.tsx's existing "stable order" scenario with Codex's exact repro (pick D and A via the picker after cycling once, assert D becomes reachable and the re-captured order stays stable afterward). All three tests in that file now share one mount via beforeAll/afterAll rather than each mounting independently — a second or third independent heavy provider-tree mount in this file was found to reproducibly hang during its own bootstrap (a test-infrastructure resource-contention issue, worse the more work a prior mount did, unrelated to anything under test) — chaining the scenarios onto one shared, stateful mount sidesteps it while still exercising the real cycle() end to end. Confirmed the new assertion fails (D never reachable, times out) against the round-7-only invalidation check, and passes on HEAD. 2. ready-pending.test.tsx exercised a hand-rolled reimplementation of the defer/retry shape instead of production code, so reverting the real fix in component/prompt/index.tsx left it passing regardless. Extracted `createDeferredRetry` (packages/tui/src/util/signal.ts) — a small, generic, reactive defer-then-retry primitive — and made both prompt/index.tsx's submit gate and the test's harness call the SAME function. Confirmed the test fails (2 of 3 cases, exactly the ones relying on automatic retry) when `createDeferredRetry`'s retry is temporarily broken, and passes on HEAD once restored. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent ce0d3af commit cb9e4d7

5 files changed

Lines changed: 206 additions & 75 deletions

File tree

‎packages/tui/src/component/prompt/index.tsx‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import { DialogModelWelcome, markFirstRunActive, useReady, useReadyPending } fro
5252
import { DialogAlert } from "../../ui/dialog-alert"
5353
import { useToast } from "../../ui/toast"
5454
import { useKV } from "../../context/kv"
55-
import { createFadeIn } from "../../util/signal"
55+
import { createDeferredRetry, createFadeIn } from "../../util/signal"
5656
import { DialogSkill } from "../dialog-skill"
5757
import { DialogWorkspaceUnavailable } from "../dialog-workspace-unavailable"
5858
import { useArgs } from "../../context/args"
@@ -1035,20 +1035,20 @@ export function Prompt(props: PromptProps) {
10351035
let submitting = false
10361036
// altimate_change start — Codex HOLD finding 1: a submit attempted while `readyPending()` is
10371037
// true (kv still hydrating, see its declaration above) defers instead of discarding — see
1038-
// `submitInner`'s `readyPending()` branch below, which sets this flag rather than clearing the
1039-
// prompt. This effect is the retry: once `readyPending()` flips false (kv resolved either way),
1040-
// re-attempt the exact same `submit()` call automatically, so a submission made during that
1041-
// window is neither lost nor stuck waiting on the user to press Enter again. Re-running
1042-
// `submit()` (not some cached decision) means it re-evaluates `ready()` fresh against whatever
1043-
// `store.prompt.input` currently holds — if the user kept typing while deferred, that's what
1044-
// goes out; if they cleared it, `submitInner`'s own `if (!store.prompt.input) return false`
1045-
// early-exit makes this a no-op.
1046-
let deferredSubmit = false
1047-
createEffect(() => {
1048-
if (readyPending() || !deferredSubmit) return
1049-
deferredSubmit = false
1050-
void submit()
1051-
})
1038+
// `submitInner`'s `readyPending()` branch below, which calls `deferredSubmit.defer()` rather
1039+
// than clearing the prompt. `createDeferredRetry` (util/signal.ts) is the retry: once
1040+
// `readyPending()` flips false (kv resolved either way), it re-attempts the exact same
1041+
// `submit()` call automatically, so a submission made during that window is neither lost nor
1042+
// stuck waiting on the user to press Enter again. Re-running `submit()` (not some cached
1043+
// decision) means it re-evaluates `ready()` fresh against whatever `store.prompt.input`
1044+
// currently holds — if the user kept typing while deferred, that's what goes out; if they
1045+
// cleared it, `submitInner`'s own `if (!store.prompt.input) return false` early-exit makes
1046+
// this a no-op. Codex re-review round 8: extracted into a standalone, shared primitive (rather
1047+
// than the flag + `createEffect` inlined here) specifically so
1048+
// test/context/ready-pending.test.tsx exercises the SAME production code this component runs,
1049+
// not a hand-rolled reimplementation that could drift from — or stop reflecting — a change
1050+
// made only here.
1051+
const deferredSubmit = createDeferredRetry(readyPending, () => void submit())
10521052
// altimate_change end
10531053
async function submit() {
10541054
// Prevent overlapping invocations (e.g. a double-pressed Enter, or the
@@ -1100,7 +1100,7 @@ export function Prompt(props: PromptProps) {
11001100
// Defer instead — keep the prompt exactly as-is, do not open anything — and let the retry
11011101
// effect above resubmit once `readyPending()` settles.
11021102
if (readyPending()) {
1103-
deferredSubmit = true
1103+
deferredSubmit.defer()
11041104
return false
11051105
}
11061106
// altimate_change — cubic review (3986532221): this is the prompt-gate's own equivalent of

‎packages/tui/src/context/local.tsx‎

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,21 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
432432
// altimate_change end
433433
})
434434

435+
// altimate_change start — Codex re-review round 8: `cycle()`'s stable-order snapshot
436+
// (`cycleOrder`, declared near its own definition below) needs to know when `recent` has
437+
// changed for a reason OTHER than cycle()'s own pick, so it can re-capture and pick up
438+
// entries a picker selection just added — otherwise a `/model` pick that reorders `recent`
439+
// out from under a stale `cycleOrder` permanently excludes the newly-recent-ed model from
440+
// the cycle. `recentsVersion` increments on every write to `modelStore.recent`, routed
441+
// through `setRecent` (never call `setModelStore("recent", ...)` directly) so it can never
442+
// drift out of sync with reality.
443+
let recentsVersion = 0
444+
function setRecent(value: { providerID: string; modelID: string }[]) {
445+
recentsVersion++
446+
setModelStore("recent", value)
447+
}
448+
// altimate_change end
449+
435450
const filePath = path.join(paths.state, "model.json")
436451
const state = {
437452
pending: false,
@@ -476,7 +491,7 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
476491
if (!x || typeof x !== "object") return
477492
const value = x as Record<string, unknown>
478493
// altimate_change start — discard malformed persisted model references before default migration
479-
if (Array.isArray(value.recent)) setModelStore("recent", value.recent.filter(isModelRef))
494+
if (Array.isArray(value.recent)) setRecent(value.recent.filter(isModelRef))
480495
// altimate_change end
481496
if (Array.isArray(value.favorite)) setModelStore("favorite", value.favorite)
482497
if (typeof value.variant === "object" && value.variant !== null)
@@ -634,7 +649,7 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
634649
const a = agent.current()
635650
if (!a) return
636651
setModelStore("model", a.name, model)
637-
if (options?.recent) setModelStore("recent", recentModels(model, modelStore.recent))
652+
if (options?.recent) setRecent(recentModels(model, modelStore.recent))
638653
// A picker-driven selection, as opposed to session restore or programmatic migration —
639654
// see `hasExplicitModel` above for why this needs its own persisted marker.
640655
if (options?.explicit) setModelStore("explicitDefault", { providerID: model.providerID, modelID: model.modelID })
@@ -757,6 +772,11 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
757772
// `cycle()`'s own comment below for why a LIVE read of `modelStore.recent` (which `cycle()`
758773
// itself reorders via `selectModel(val, { recent: true })`) breaks repeated presses.
759774
let cycleOrder: readonly { providerID: string; modelID: string }[] | undefined
775+
// altimate_change — Codex re-review round 8: the version `cycleOrder` was captured at (or
776+
// last resynced to, after cycle()'s own write) — see `recentsVersion`'s declaration above.
777+
// A mismatch against the LIVE `recentsVersion` means something OTHER than `cycle()` wrote
778+
// to `recent` since, and `cycleOrder` must be re-captured to see it.
779+
let cycleOrderVersion = -1
760780
// altimate_change end
761781

762782
return {
@@ -804,16 +824,30 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
804824
// the rest of the cycling sequence — `cycle()`'s own index math walks THIS frozen list,
805825
// never the live, self-reordering `modelStore.recent`. `selectModel(..., { recent: true })`
806826
// still updates the real persisted `recent` on every pick, satisfying requirement 1; it
807-
// just no longer feeds back into what `cycle()` itself reads for requirement 2. If the
808-
// current model isn't found in the captured order (the user picked something else via
809-
// `/model` since cycling last started, or this is the very first cycle this session),
810-
// (re-)capture fresh from the CURRENT `modelStore.recent` and start the sequence there.
827+
// just no longer feeds back into what `cycle()` itself reads for requirement 2.
828+
//
829+
// altimate_change — Codex re-review round 8: "held fixed for the rest of the cycling
830+
// sequence" must not mean "held fixed forever." Only invalidating on "the current model
831+
// fell out of `cycleOrder`" (the original round-7 check) went stale the moment a PICKER
832+
// selection reordered `recent` without also knocking the current model out of the old
833+
// snapshot: e.g. `recent = [A, B, C]`, cycle once (B is now current, `recent = [B, A,
834+
// C]`), then the user picks D and A via `/model` (`recent` ends up `[A, D, C, B]`) — A is
835+
// still present in the STALE `cycleOrder` (`[A, B, C]`), so the old check never
836+
// re-captured, and D stayed permanently unreachable by cycling. `cycleOrderVersion` (see
837+
// its declaration above) closes this: it also re-captures whenever `recentsVersion` has
838+
// moved since `cycleOrder` was last captured OR resynced — which happens for ANY write
839+
// to `recent`, picker or otherwise — while still recognizing cycle()'s OWN write (via the
840+
// resync at the end of this function) so repeated presses with nothing else interleaved
841+
// keep reusing the same stable snapshot, unaffected.
811842
cycle(direction: 1 | -1) {
812843
const current = currentModel()
813844
if (!current) return
814845
const findCurrent = (order: readonly { providerID: string; modelID: string }[]) =>
815846
order.findIndex((x) => x.providerID === current.providerID && x.modelID === current.modelID)
816-
if (!cycleOrder || findCurrent(cycleOrder) === -1) cycleOrder = modelStore.recent.slice()
847+
if (!cycleOrder || cycleOrderVersion !== recentsVersion || findCurrent(cycleOrder) === -1) {
848+
cycleOrder = modelStore.recent.slice()
849+
cycleOrderVersion = recentsVersion
850+
}
817851
const index = findCurrent(cycleOrder)
818852
if (index === -1) return
819853
let next = index + direction
@@ -822,6 +856,9 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
822856
const val = cycleOrder[next]
823857
if (!val) return
824858
selectModel(val, { explicit: true, recent: true })
859+
// Absorb our OWN write (selectModel above bumped `recentsVersion` via `setRecent`) so
860+
// it does not look like an external change the NEXT time `cycle()` runs.
861+
cycleOrderVersion = recentsVersion
825862
},
826863
// altimate_change end
827864
cycleFavorite(direction: 1 | -1) {
@@ -937,7 +974,7 @@ export const { use: useLocal, provider: LocalProvider } = createSimpleContext({
937974
if (a && shouldMoveAgentModelDuringMigration(currentModel(), previous))
938975
setModelStore("model", a.name, { ...ALTIMATE_BASE_MODEL })
939976
// altimate_change end
940-
setModelStore("recent", migrateLegacyRecentModels(modelStore.recent, previous))
977+
setRecent(migrateLegacyRecentModels(modelStore.recent, previous))
941978
// altimate_change — fixes #1301 (Codex review round 2, P2): an explicit accept via
942979
// migration clears any earlier decline the same way `selectModel` does for every
943980
// other explicit Base selection (`/connect`, favorite-cycling) — both flags together.

‎packages/tui/src/util/signal.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
import { createEffect, createSignal, on, onCleanup, type Accessor } from "solid-js"
22

3+
// Codex re-review round 8 (cycle-stability/ready-pending test coverage): a reactive
4+
// defer-then-retry primitive — call `.defer()` when a caller can't act yet (e.g. a readiness
5+
// signal is still pending), and the wrapped `retry` callback fires automatically, exactly once,
6+
// the NEXT time `pending()` reads false. Extracted as a standalone, importable function so the
7+
// SAME production code path is exercised by both a real consumer (component/prompt/index.tsx's
8+
// submit gate — see `readyPending`'s declaration there) and its test
9+
// (test/context/ready-pending.test.tsx) — the test was previously a hand-rolled reimplementation
10+
// of this exact shape, which meant reverting the real fix in prompt/index.tsx left the test
11+
// passing regardless, since it never touched production code at all.
12+
export function createDeferredRetry(pending: Accessor<boolean>, retry: () => void) {
13+
let deferred = false
14+
createEffect(() => {
15+
if (pending() || !deferred) return
16+
deferred = false
17+
retry()
18+
})
19+
return {
20+
defer() {
21+
deferred = true
22+
},
23+
}
24+
}
25+
326
export function createDebouncedSignal<T>(value: T, ms: number): [Accessor<T>, (value: T) => void] {
427
const [get, set] = createSignal(value)
528
let timer: ReturnType<typeof setTimeout> | undefined

0 commit comments

Comments
 (0)