Skip to content

Commit d2c2cd6

Browse files
sahrizviclaude
andcommitted
fix(hygiene): [#1052 bot-review round 2] address open bot findings
Round 1 fixed 10 of the 22 bot comments; this covers the rest, plus two the round-1 fixes introduced. models.dev (coderabbit Major + kilo + cubic P1 — all three flagged it): - Remove the import-time `Promise.resolve().then(() => ModelsDev.refresh())`. The comment defending it argued a microtask cannot keep Bun alive. The microtask cannot, but the `fetch()` it starts can — and on a cold cache `refresh()` reaches `fetchApi()`, whose `AbortSignal.timeout` still cannot cancel a blocking `getaddrinfo()`. That is the D14 release-blocker shape, so the eager refresh is gone rather than rescheduled. Snapshot + on-demand load + the unref'd hourly interval remain. - Reject JSON that parses but is not a catalog object (`null`, `[]`, scalars — what a misconfigured proxy returns with a JSON content-type) before writing the disk cache, so the "don't poison the cache" intent holds for structured-but-wrong bodies too. Build stamp (cubic P2 x2, coderabbit Major, coderabbit Minor): - Stamp `packages/opencode/package.json`. The workspace walk skips `opencode`, so its own manifest was unstamped while a comment claimed otherwise. - Extract the input walk to `script/stamp-inputs.ts` so the smoke-test guard can re-enumerate with identical rules. Rehashing recorded inputs only catches CHANGED and DELETED files; a file ADDED after the build left every hash matching while the binary was stale. The stamp now records the walked roots and the guard treats an unrecorded file under them as stale. - Validate the stamp shape at runtime. `as BuildStamp` is a compile-time claim: `{"inputs":"x"}` passed the old length check and then threw in the loop. Tracker scanner (cubic P1 x2, cubic P2, cubic P3, coderabbit Major): - Scan the refs actually being pushed, read from the pre-push stdin, instead of always scanning HEAD. `git push origin dirty:other` previously approved a ref nobody had looked at. - A deletion-only push now scans nothing rather than falling back to HEAD. Found by the new end-to-end tests, not by a reviewer. - An unborn HEAD (`git init`, zero commits) no longer exits 2. That regression came from round 1's `failOnError: true` default and contradicted the documented brand-new-repo path. - A missing base ref says so on stderr instead of exiting silently, so a guard that has disabled itself is visible. Test harness (coderabbit Major): - `cli-process` SQLite scrub rethrows anything that is not ENOENT, so a permission or removal failure no longer lets the retry start on half-written state — the exact condition the scrub exists to prevent. Verification: 5 end-to-end tests drive the real script against throwaway repositories, including two real `git push` runs through an installed pre-push hook (clean branch pushes; a branch carrying a tracker ref is blocked). Three of them fail against the pre-fix scanner. Suites: 33 scanner, 580 provider, 218 install, 13/13 typecheck; lint warnings down from 15 to 13; markers verified "All blocks properly closed" repo-wide. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 5b30a30 commit d2c2cd6

7 files changed

Lines changed: 357 additions & 119 deletions

File tree

‎packages/opencode/script/build.ts‎

Lines changed: 84 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ process.chdir(dir)
1717

1818
import { Script } from "@opencode-ai/script"
1919
import pkg from "../package.json"
20+
import { walkInputs } from "./stamp-inputs"
2021

2122
// Python engine has been eliminated — all methods run natively in TypeScript.
2223
// ALTIMATE_ENGINE_VERSION is no longer needed at runtime.
@@ -147,43 +148,49 @@ const allTargets: {
147148
]
148149

149150
// If --targets is provided, filter to only matching OS values
150-
const validOsValues = new Set(allTargets.map(t => t.os))
151-
const targetsFlag = process.argv.find(a => a.startsWith('--targets='))?.split('=')[1]?.split(',')
151+
const validOsValues = new Set(allTargets.map((t) => t.os))
152+
const targetsFlag = process.argv
153+
.find((a) => a.startsWith("--targets="))
154+
?.split("=")[1]
155+
?.split(",")
152156
if (targetsFlag) {
153-
const invalid = targetsFlag.filter(t => !validOsValues.has(t))
157+
const invalid = targetsFlag.filter((t) => !validOsValues.has(t))
154158
if (invalid.length > 0) {
155-
console.error(`error: invalid --targets value(s): ${invalid.join(', ')}. Valid values: ${[...validOsValues].join(', ')}`)
159+
console.error(
160+
`error: invalid --targets value(s): ${invalid.join(", ")}. Valid values: ${[...validOsValues].join(", ")}`,
161+
)
156162
process.exit(1)
157163
}
158164
}
159165

160166
// --target-index=N builds a single target by index (for parallel CI matrix)
161-
const targetIndexFlag = process.argv.find(a => a.startsWith('--target-index='))?.split('=')[1]
162-
163-
const targets = targetIndexFlag !== undefined
164-
? [allTargets[parseInt(targetIndexFlag, 10)]].filter(Boolean)
165-
: singleFlag
166-
? allTargets.filter((item) => {
167-
if (item.os !== process.platform || item.arch !== process.arch) {
168-
return false
169-
}
170-
171-
// When building for the current platform, prefer a single native binary by default.
172-
// Baseline binaries require additional Bun artifacts and can be flaky to download.
173-
if (item.avx2 === false) {
174-
return baselineFlag
175-
}
176-
177-
// also skip abi-specific builds for the same reason
178-
if (item.abi !== undefined) {
179-
return false
180-
}
181-
182-
return true
183-
})
184-
: targetsFlag
185-
? allTargets.filter(t => targetsFlag.includes(t.os))
186-
: allTargets
167+
const targetIndexFlag = process.argv.find((a) => a.startsWith("--target-index="))?.split("=")[1]
168+
169+
const targets =
170+
targetIndexFlag !== undefined
171+
? [allTargets[parseInt(targetIndexFlag, 10)]].filter(Boolean)
172+
: singleFlag
173+
? allTargets.filter((item) => {
174+
if (item.os !== process.platform || item.arch !== process.arch) {
175+
return false
176+
}
177+
178+
// When building for the current platform, prefer a single native binary by default.
179+
// Baseline binaries require additional Bun artifacts and can be flaky to download.
180+
if (item.avx2 === false) {
181+
return baselineFlag
182+
}
183+
184+
// also skip abi-specific builds for the same reason
185+
if (item.abi !== undefined) {
186+
return false
187+
}
188+
189+
return true
190+
})
191+
: targetsFlag
192+
? allTargets.filter((t) => targetsFlag.includes(t.os))
193+
: allTargets
187194

188195
// Defense in depth: refuse to produce no artifacts at all, and refuse to build
189196
// the glibc target on a musl host where the binary would crash at startup.
@@ -196,13 +203,14 @@ const targets = targetIndexFlag !== undefined
196203
// `linux-x64` (glibc), produces a glibc binary that the musl host can't
197204
// load, and dies later with a cryptic linker error.
198205
if (targets.length === 0) {
199-
const reason = targetIndexFlag !== undefined
200-
? `--target-index=${targetIndexFlag} is out of range (allTargets has ${allTargets.length} entries — musl/win32-arm64 were removed).`
201-
: singleFlag
202-
? `--single found no entry in allTargets matching ${process.platform}/${process.arch} (host may be excluded — see allTargets at the top of build.ts).`
203-
: targetsFlag
204-
? `--targets=${targetsFlag.join(",")} matched nothing in allTargets.`
205-
: "allTargets is empty."
206+
const reason =
207+
targetIndexFlag !== undefined
208+
? `--target-index=${targetIndexFlag} is out of range (allTargets has ${allTargets.length} entries — musl/win32-arm64 were removed).`
209+
: singleFlag
210+
? `--single found no entry in allTargets matching ${process.platform}/${process.arch} (host may be excluded — see allTargets at the top of build.ts).`
211+
: targetsFlag
212+
? `--targets=${targetsFlag.join(",")} matched nothing in allTargets.`
213+
: "allTargets is empty."
206214
console.error(`error: no build targets selected. ${reason}`)
207215
process.exit(1)
208216
}
@@ -221,8 +229,12 @@ if (singleFlag && process.platform === "linux") {
221229
return false
222230
})()
223231
if (isMuslHost) {
224-
console.error("error: --single on a musl-linux host would build the glibc target and produce a binary the host cannot run.")
225-
console.error(" altimate-core has no NAPI prebuild for musl yet. Build on a glibc host, or install via `apk add gcompat` + the npm wrapper.")
232+
console.error(
233+
"error: --single on a musl-linux host would build the glibc target and produce a binary the host cannot run.",
234+
)
235+
console.error(
236+
" altimate-core has no NAPI prebuild for musl yet. Build on a glibc host, or install via `apk add gcompat` + the npm wrapper.",
237+
)
226238
process.exit(1)
227239
}
228240
}
@@ -240,10 +252,18 @@ await $`rm -rf dist`
240252
const requiredExternals: string[] = []
241253
const optionalExternals = [
242254
// Database drivers — native addons, users install on demand per warehouse
243-
"pg", "snowflake-sdk", "@google-cloud/bigquery", "@databricks/sql",
244-
"mysql2", "mssql", "oracledb", "duckdb",
255+
"pg",
256+
"snowflake-sdk",
257+
"@google-cloud/bigquery",
258+
"@databricks/sql",
259+
"mysql2",
260+
"mssql",
261+
"oracledb",
262+
"duckdb",
245263
// Optional infra packages — native addons or heavy optional deps
246-
"keytar", "ssh2", "dockerode",
264+
"keytar",
265+
"ssh2",
266+
"dockerode",
247267
]
248268

249269
const binaries: Record<string, string> = {}
@@ -267,7 +287,9 @@ function altimateCorePlatformFor(item: { os: string; arch: "arm64" | "x64"; abi?
267287
platformTag: string
268288
} {
269289
if (item.abi === "musl") {
270-
throw new Error(`No @altimateai/altimate-core prebuild for linux-${item.arch}-musl; this target should not be in allTargets.`)
290+
throw new Error(
291+
`No @altimateai/altimate-core prebuild for linux-${item.arch}-musl; this target should not be in allTargets.`,
292+
)
271293
}
272294
if (item.os === "darwin") {
273295
const tag = `darwin-${item.arch}`
@@ -282,7 +304,9 @@ function altimateCorePlatformFor(item: { os: string; arch: "arm64" | "x64"; abi?
282304
const tag = "win32-x64-msvc"
283305
return { pkg: `@altimateai/altimate-core-${tag}`, nodeFile: `altimate-core.${tag}.node`, platformTag: tag }
284306
}
285-
throw new Error(`No @altimateai/altimate-core prebuild for win32-${item.arch}; this target should not be in allTargets.`)
307+
throw new Error(
308+
`No @altimateai/altimate-core prebuild for win32-${item.arch}; this target should not be in allTargets.`,
309+
)
286310
}
287311
throw new Error(`Unsupported build target: ${item.os}-${item.arch}`)
288312
}
@@ -299,9 +323,7 @@ const altimateCoreLoaderDir = fs.realpathSync(path.dirname(altimateCoreLoaderPkg
299323
// .node into today's release archive.
300324
{
301325
const expected = pkg.dependencies["@altimateai/altimate-core"]
302-
const resolvedVersion = JSON.parse(
303-
fs.readFileSync(path.join(altimateCoreLoaderDir, "package.json"), "utf8"),
304-
).version
326+
const resolvedVersion = JSON.parse(fs.readFileSync(path.join(altimateCoreLoaderDir, "package.json"), "utf8")).version
305327
if (resolvedVersion !== expected) {
306328
throw new Error(
307329
`build.ts: resolved @altimateai/altimate-core version ${resolvedVersion} ` +
@@ -568,33 +590,24 @@ for (const item of targets) {
568590
// altimate_change — #1052 D10 review-fix (M2): package.json + bun.lock cover
569591
// dependency-version bumps that change what Bun.build embeds. Without these,
570592
// `bun install` bumping a bundled dep would leave the stamp reporting fresh.
571-
// Include per-package package.json in the workspace walk below.
593+
// Sibling workspace manifests are added in the packages walk below; this
594+
// package's own manifest is added here, because that walk skips `opencode`
595+
// (its src/ and script/ trees are already covered) and would otherwise leave
596+
// `imports`, `exports` and other bundler-relevant fields unstamped.
572597
addFile(path.join(REPO_ROOT, "package.json"))
573598
addFile(path.join(REPO_ROOT, "bun.lock"))
599+
addFile(path.join(dir, "package.json"))
574600
// Also include tsconfig files that affect compiled output shape
575601
// (bot review: tsconfig changes can flip target/moduleResolution).
576602
addFile(path.join(dir, "tsconfig.json"))
577-
// src/ + script/ TypeScript tree — hash every file the compiler actually saw
578-
// (same extension filter build.ts globs for embedding).
579-
const IGNORED = new Set(["node_modules", ".turbo", ".cache", "dist", "target"])
603+
// src/ + script/ TypeScript tree — hash every file the compiler actually saw.
604+
// The walk rules live in ./build-inputs so the smoke-test guard can
605+
// re-enumerate with identical rules and notice files ADDED after the build.
606+
const walkedRoots: string[] = []
580607
const walk = (root: string): void => {
581-
let entries: fs.Dirent[]
582-
try {
583-
entries = fs.readdirSync(root, { withFileTypes: true })
584-
} catch {
585-
return
586-
}
587-
for (const entry of entries) {
588-
if (entry.name.startsWith(".")) continue
589-
if (IGNORED.has(entry.name)) continue
590-
const full = path.join(root, entry.name)
591-
if (entry.isDirectory()) {
592-
walk(full)
593-
continue
594-
}
595-
if (!/\.(tsx?|json|txt|md)$/.test(entry.name)) continue
596-
addFile(full)
597-
}
608+
if (!fs.existsSync(root)) return
609+
walkedRoots.push(path.relative(REPO_ROOT, root))
610+
for (const file of walkInputs(root)) addFile(file)
598611
}
599612
walk(path.join(dir, "src"))
600613
walk(path.join(dir, "script"))
@@ -633,6 +646,9 @@ for (const item of targets) {
633646
target: name,
634647
version: Script.version,
635648
aggregate,
649+
// Roots the walk covered, so the read side can detect files added after
650+
// the build rather than only rehashing what was present at build time.
651+
roots: [...new Set(walkedRoots)].sort(),
636652
inputs: stampInputs,
637653
},
638654
null,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// altimate_change start — #1052 D10 bot-review fix: shared build-input walk.
2+
//
3+
// The build stamp and the smoke-test staleness guard have to agree on exactly
4+
// which files count as a build input. When the walk lived only inside build.ts,
5+
// the guard could rehash the paths the stamp listed but had no way to notice a
6+
// file ADDED after the build — a new module under any walked root left the
7+
// binary stale while the guard stayed silent. Sharing the walk lets the guard
8+
// re-enumerate with identical rules and compare sets, not just hashes.
9+
import fs from "fs"
10+
import path from "path"
11+
12+
/** Directories never part of a build input set. */
13+
export const IGNORED_DIRS = new Set(["node_modules", ".turbo", ".cache", "dist", "target"])
14+
15+
/** Extensions Bun.build can pull into the binary from a walked source tree. */
16+
export const INPUT_EXTENSIONS = /\.(tsx?|json|txt|md)$/
17+
18+
/**
19+
* Absolute paths of every input file under `root`, applying the shared rules.
20+
* Missing roots yield an empty list — callers treat that as "nothing to add".
21+
*/
22+
export function walkInputs(root: string): string[] {
23+
const found: string[] = []
24+
const walk = (current: string): void => {
25+
let entries: fs.Dirent[]
26+
try {
27+
entries = fs.readdirSync(current, { withFileTypes: true })
28+
} catch {
29+
return
30+
}
31+
for (const entry of entries) {
32+
if (entry.name.startsWith(".")) continue
33+
if (IGNORED_DIRS.has(entry.name)) continue
34+
const full = path.join(current, entry.name)
35+
if (entry.isDirectory()) {
36+
walk(full)
37+
continue
38+
}
39+
if (!INPUT_EXTENSIONS.test(entry.name)) continue
40+
found.push(full)
41+
}
42+
}
43+
walk(root)
44+
return found
45+
}
46+
// altimate_change end

‎packages/opencode/src/provider/models.ts‎

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export namespace ModelsDev {
136136
// parse failure, log and return empty — same graceful-degradation path
137137
// as the non-2xx branch, and we don't poison the disk cache with junk.
138138
if (!result2.ok) return {}
139-
let parsed: Record<string, unknown>
139+
let parsed: unknown
140140
try {
141141
parsed = JSON.parse(result2.text)
142142
} catch (e) {
@@ -146,6 +146,17 @@ export namespace ModelsDev {
146146
})
147147
return {}
148148
}
149+
// Bot-review follow-up: syntactically valid JSON is not necessarily the
150+
// catalog. `null`, `[]` and scalars all survive JSON.parse — a shape a
151+
// misconfigured proxy returns with a JSON content-type. Caching one of
152+
// those poisons the disk cache for the whole TTL, and `get()` casts it to
153+
// `Record<string, Provider>` for `fromModelsDevProvider` to iterate.
154+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
155+
log.error("models.dev returned JSON that is not a catalog object; not caching", {
156+
firstBytes: result2.text.slice(0, 120),
157+
})
158+
return {}
159+
}
149160
await Filesystem.write(filepath, result2.text).catch((e) => {
150161
log.error("Failed to write models cache", { error: e })
151162
})
@@ -195,18 +206,22 @@ if (!Flag.OPENCODE_DISABLE_MODELS_FETCH && !process.argv.includes("--get-yargs-c
195206
// via the hourly `setInterval` below (`.unref()`'d so it never blocks exit).
196207
//
197208
// Trade-off: without an eager fetch, models added to models.dev between
198-
// releases would not appear until the hourly interval below fires. The
199-
// fire-and-forget refresh() below narrows that window without holding the
200-
// event loop — a microtask can't itself keep Bun alive, and if the fetch it
201-
// schedules is still in flight at process-exit, the snapshot covers callers
202-
// on the next run. The load-bearing part of the D14 fix (removing the
203-
// setTimeout(...,0)-wrapped fetch that kept the loop alive) is preserved.
209+
// releases do not appear until a long-running process's hourly interval
210+
// fires, or until the disk cache expires and a caller loads on demand. That
211+
// is accepted deliberately.
204212
//
205-
// If this reintroduces the unshare-net hang on CI, drop the Promise.then
206-
// line — the snapshot alone still keeps release binaries functional offline.
207-
Promise.resolve().then(() => ModelsDev.refresh().catch(() => {}))
208-
setInterval(async () => {
209-
await ModelsDev.refresh()
210-
}, 60 * 60 * 1000).unref()
213+
// An earlier revision tried to narrow that window with
214+
// `Promise.resolve().then(() => ModelsDev.refresh())`, reasoning that a
215+
// microtask cannot keep Bun alive. The microtask cannot — but the `fetch()`
216+
// it starts can, and that is the whole failure. On a cold cache `refresh()`
217+
// reaches `fetchApi()`, whose `AbortSignal.timeout(10000)` still cannot
218+
// cancel a blocking `getaddrinfo()`. That is the D14 shape exactly, so the
219+
// eager refresh is gone rather than rescheduled.
220+
setInterval(
221+
async () => {
222+
await ModelsDev.refresh()
223+
},
224+
60 * 60 * 1000,
225+
).unref()
211226
// altimate_change end
212227
}

0 commit comments

Comments
 (0)