Commit d8dfa42
committed
perf(models-config): thread validated models.json outcome through short-circuit cache (review feedback)
Round-10 follow-up on Clawsweeper P2 (outstanding since 2026-04-30T22:05,
missed in rounds 6-9): "Cache only the validated models.json snapshot."
Pre-fix, `readExistingProviderMatchesConfig` would safe-read +
structurally validate the on-disk `models.json` once via
`safeReadFileOutcome`, then return a bare `boolean`. The caller at
models-config.ts:1397-1401 would then issue a SECOND read via
`readModelsJsonContentOutcome(targetPath)` and store THAT outcome's
hash in the provider-scoped readyCache. Between the two reads, an
attacker (or any external editor) replacing `models.json` could land
hash(swappedBytes) in the scoped cache as "the validated snapshot." A
later targeted call hitting the cache would compare current disk
against hash(swappedBytes) and accept it as a match, blessing
attacker-controlled provider transport (api / baseUrl / headers
consumed per-call by `pi-embedded-runner/model.ts`). CWE-345.
This commit collapses the two reads into one by threading the
validated `ContentHashOutcome` straight back from the structural
check. Concretely:
- `readExistingProviderMatchesConfig` now returns a discriminated
union: `{ matches: true; validatedModelsJsonOutcome }` on success,
`{ matches: false }` on every failure path. The validated outcome
is derived directly from the `safeReadFileOutcome` the function
already runs (line 838) — same bytes, same hash, no second disk
trip.
- The caller uses `matchOutcome.validatedModelsJsonOutcome` for the
scoped-cache `set` and drops the redundant
`readModelsJsonContentOutcome(targetPath)` call.
- The `kind !== "uncacheable"` guard is preserved as
belt-and-suspenders even though, on the success path, the validated
outcome is provably `hashed` (uncacheable cases early-return
`{ matches: false }` before we get here).
Three new regression tests pin the contract from independent angles:
- `cache-stores-validated-outcome` — happy path: cold + stable disk +
reset-cache + targeted call must take the disk-based short-circuit
exactly once and a follow-up targeted call must hit the warm scoped
cache without re-running discovery.
- `toctou-swap-cannot-bless-unvalidated-content` — security: after a
cold short-circuit populates the scoped cache, swap on-disk
`models.json` to a config-disagreeing baseUrl; the next targeted
call MUST detect drift, refuse the cache, fail the structural
check, and fall through to a full plan (resolveImplicit fires).
- `no-cache-on-uncacheable-validated-outcome` — defense-in-depth:
oversize `models.json` forces a full plan and the post-plan disk
size returns under cap, confirming the uncacheable branch is
reachable end-to-end without the redundant second read.
Adds a real-runtime proof script
`scripts/proof-73261-validated-outcome-cache.ts` that drives the
production `ensureOpenClawModelsJson` against a real on-disk
`models.json` in a temp dir, instruments `fs.promises.lstat` to count
reads per scenario, and self-checks that the round-10 contract holds:
[1/3] cache-stores-validated-outcome
lstat(models.json) during short-circuit: 1
lstat(models.json) during scoped-cache hit + drift-check: 1
ok
[2/3] toctou-swap-cannot-bless-unvalidated-content
disk baseUrl after cold plan: https://api.openai.com/v1
swap was not silently blessed by stale scoped cache
ok
[3/3] no-cache-on-uncacheable-validated-outcome
bloated models.json size: 2102064 bytes (> 1 MiB cap)
post-plan models.json size: 9105 bytes
ok
All runtime assertions passed.
Validation gate (full output captured for the PR comment):
pnpm tsgo:core ok
pnpm tsgo:core:test ok
pnpm vitest run src/agents/models-config.target-provider-... 66/66
pnpm vitest run src/agents/models-config.fingerprint-cache,
src/agents/models-config.write-serialization 32/32
pnpm config:schema:check ok
pnpm plugin-sdk:api:check ok
pnpm oxlint <touched files> ok
git diff --check clean
pnpm tsx scripts/proof-73261-validated-outcome-cache.ts ok
Beads: openclaw-8zp1 parent 721a524 commit d8dfa42
4 files changed
Lines changed: 485 additions & 35 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
0 commit comments