Skip to content

Commit 205a953

Browse files
sahrizviclaude
andauthored
fix: [#1052] bundled cleanup — 5 deferred items + 3 review-fixes (#1085)
* chore(hygiene): [#1052 D8] pre-push scan for internal-tracker refs Public repo hardening: catch tracker-adjacent references before they land in commits, branches, or file content. The exact patterns live in the RULES array in `script/check-tracker-leaks.ts` — read there for the canonical list. - `script/check-tracker-leaks.ts` — bun script that scans branch name + commit messages + added-lines diff vs `origin/main`. Silent on clean; exits 1 with per-source report on hit. Bypass via `SKIP_TRACKER_CHECK=1`. - `.husky/pre-push` — invokes the script after existing typecheck. - `CONTRIBUTING.md` — one-line note pointing at the hook + how to bypass. CI-side mirror deferred to a follow-up PR (needs a `workflow`-scoped token to add `.github/workflows/*` and this session's token doesn't have it). The D8 issue calls out both local + CI as needed — local hook is the primary guard; CI is the backstop for contributors who never ran `git config core.hooksPath .husky` or who used the bypass env var. The scan is diff-based: existing tracker-adjacent strings in main (a handful in comments) are grandfathered and won't trigger on unrelated PRs. Only NEW added lines are checked, so touching a file with a legacy reference is safe as long as the reference itself doesn't appear in the diff's `+` lines. * test(build): [#1052 D10] stamp-based staleness guard for the smoke test The m5 guard walked `src/` + `script/` mtimes for the newest touched file. Correct for the common case but blind to changes in CHANGELOG.md, migrations, bundled skills, models-snapshot.ts, the opentui parser worker, and the per-platform altimate-core prebuild — any of which can change the compiled binary's shape without touching a `.ts` file. - `packages/opencode/script/build.ts` — emit `dist/<target>/bin/build-inputs.json` at the end of each per-target build. JSON lists every embedded input's sha256 plus an aggregate hash over the sorted pairs. Paths are relative to `packages/opencode` so the read side can resolve them without env plumbing. - `packages/opencode/test/install/smoke-test-binary.test.ts` — new `isBinaryStaleFromStamp()` rehashes each listed input and reports stale on any mismatch. Falls back to the old mtime walk when the stamp is missing (older `bun run build:local` runs, or targets built before this landed). - Explicit `no-stamp` sentinel routes cleanly to the fallback without conflating "no binary" with "no stamp" — both surface as skips, but for different messages. * test(harness): [#1052 D11] idempotent retry in cli-process.run() The `database is locked` retry re-spawns `opencode run` against the same XDG_DATA_HOME. If the first attempt got as far as opening SQLite and taking a partial write before the WAL/checkpoint collision fired, a naive retry would either see partial state or double-write on top of it — exactly the non-idempotent behavior the CodeRabbit review on PR #1053 flagged. Fix: before the retry, delete `opencode*.db{,-wal,-shm}` under the fixture's isolated data dir. The retry then boots into a clean SQLite state. Other fixture content (config file, home files, extra test setup) is preserved — tests that inject state into `home` still see it. This is deliberately narrower than "reset the whole fixture" (option b in the deferral): scrubbing DB files only preserves any state a caller wrote before invoking `run()`, which some tests rely on. * test(tui): [#1052 D12] deterministic regression test for phaseLabel() Replaces the deleted `phase-label.tui-e2e.test.ts` (was `test.skip` under `CI=true` because a PTY poll-interval race made it flaky). The published chain — `publishPhase → Bus → sync.tsx handler → store → render` — is now covered in three deterministic layers: 1. Server-side publish + subscribe wiring: existing fork-feature-guards string-shape assertions in `test/upstream/fork-feature-guards.test.ts`. 2. Store-mutation handler in `context/sync.tsx` case "session.phase": same fork-feature-guards test. 3. Last-mile label lookup — THIS FILE. If `phase-label.ts` PHASE_LABELS drifts from the span names `SessionPrompt.traceSpan` emits, users see the "Thinking..." fallback silently. This test catches that. Full component-level synthetic-event coverage remains a future extension of D12; the event-injection scaffolding does not exist as a reusable fixture yet and is not gated on this file. * fix(models): [#1052 D14] drop eager import-time ModelsDev.refresh() Previously `setTimeout(() => ModelsDev.refresh(), 0)` fired a fetch to https://models.dev/api.json at module import. Its `AbortSignal.timeout(10000)` cannot cancel a synchronous `getaddrinfo()`; under Linux `unshare --net` (Verdaccio sanity Phase 3 [10/10] on Ubuntu CI runners) DNS blocked long enough that the pending fetch held the event loop past command completion and SIGTERM landed before any bytes flushed. That blocked the v0.9.4 release and forced the SKIP that ships in `test/sanity/phases/resilience.sh` today. Fix: no eager fetch. Callers use `ModelsDev.Data()` which resolves via (1) local disk cache → (2) bundled `models-snapshot.ts` (always present in release binaries, regenerated at build time) → (3) fetch only if both absent. The bundled snapshot means release-binary users always have model metadata even on a completely offline cold start. Long-running processes (TUI, serve) still receive updates via the hourly `setInterval` below, `.unref()`'d so it never blocks exit. Short-lived commands rely on the snapshot's release-time freshness. Trade-off: models added to models.dev between releases don't appear in short-lived commands until the next release rebuild. Bounded by release cadence. Acceptable given the release-blocker this closes. Follow-up (not in this PR): re-enable the `[10/10] no-internet graceful handling` sanity test once this fix has soaked through one release. * fix(hygiene): [#1052 D8 review-fix M1] catch suffix-adjacent tracker leaks + self-test Consensus review flagged the D8 scanner's Jira-key regex as its strongest finding — the trailing word-boundary requires a non-word char after the digits, which fails when a letter, digit, or underscore immediately follows. Exactly the class of typo and paste-through the scrubber exists to prevent. Naïve fixes (negative lookahead over word chars) don't help: the regex engine backtracks the digit run, but every position still has a digit as the "next char" so the lookahead keeps failing. Correct fix is to drop the trailing boundary entirely — the pattern matches greedily through the digits, stops at the first non-digit, and reports the prefix regardless of what follows. Known blind spot documented + tested: pastes with no separator before the prefix (no leading word-boundary) are not caught. Realistic leak surface (branches, commit messages, path fragments, doc text) is delimited so this does not hit in practice. Also: gate the scanner's `main()` behind `import.meta.main` so RULES can be imported by the self-test without triggering a scanner run at test collection time. `packages/opencode/test/skill/tracker-leak-check.test.ts` — 28 test cases pinning positive / negative / blind-spot behaviour for both regexes. Runs under `bun test`. If the regex regresses, this test catches it before the local push hook or CI misses a leak. * test(build): [#1052 D10 review-fix M2] widen stamp to workspace packages + lockfile Consensus review flagged that the D10 stamp (from `b22861e091`) only walks `packages/opencode/src` and `packages/opencode/script`, but Bun.build follows imports into every workspace package (`@opencode-ai/core`, `@opencode-ai/tui`, `@opencode-ai/util`, `@opencode-ai/plugin`, `@altimateai/dbt-tools`, …) and bundles them into the binary. Edits under those packages, or a `bun install` that bumps a bundled dep, left the stamp reporting fresh — exactly the false-negative pattern the guard was meant to close. Changes: - `packages/opencode/script/build.ts` — enumerate `packages/*/src` at build time and walk each (opencode/ is already walked directly). Also hash every workspace `packages/*/package.json`, the workspace-root `package.json`, and `bun.lock`. Enumerating rather than hard-coding lets new workspace packages get covered automatically. - Stamp paths are now REPO_ROOT-relative (not packages/opencode-relative) so entries like `packages/tui/src/util/record.ts` resolve without munging. - `packages/opencode/test/install/smoke-test-binary.test.ts` — resolve stamp entries against `REPO_ROOT` instead of `PKG_DIR`. Fallback to the mtime walk is unchanged (still works for older builds without a stamp). Verified: typecheck clean; existing D12 phase-label test still passes; smoke test skips cleanly when no binary is present (unchanged behaviour). A build + tampering with a workspace-package file will now flip the stamp; a build + stale binary + edit to `packages/tui/src/...` will trigger the skip that the old walk missed. * fix(models): [#1052 D14 review-fix M3] don't crash on non-JSON error body + fire refresh at boot Two consensus-review findings on the D14 commit (`248eaaf86c`): 1. Pre-existing crash: `ModelsDev.Data()`'s `Flock.withLock` branch calls `JSON.parse(result2.text)` unconditionally after `fetchApi()` returns — even when `result2.ok === false` and the body is an HTML 5xx error page. That throws `SyntaxError` and blocks model initialization. Bug existed before D14 but was rarely exposed because the eager import-time refresh() warmed the disk cache first, so most subsequent `Data()` calls returned via `readJson`. Post-D14 more first-calls fall through to fetch, so the crash window widens. Fix: return `{}` when `!result2.ok` — callers already tolerate an empty catalog (Provider.state produces no models.dev-derived entries, same UX as `OPENCODE_DISABLE_MODELS_FETCH=1`). 2. Reintroduce a boot-time refresh without holding the event loop: 4/6 reviewers flagged the loss of "fresh at boot" as a real regression for short-lived commands. Fix: `Promise.resolve().then(() => refresh().catch())`. A microtask can't itself keep Bun alive, and if the fetch it schedules is still in flight at process-exit the snapshot covers callers on the next run. Preserves the load-bearing part of the D14 fix (no `setTimeout` keeping the loop alive) while restoring near-immediate cache warming. If this ever reintroduces the unshare-net sanity hang on CI, drop the Promise.then line — the snapshot alone keeps release binaries functional offline. Also fixes the misleading comment "always have model metadata even on a cold-start with no network" — accurate for release binaries but false in dev-mode where the snapshot isn't embedded. Reworded to say so. Verified: typecheck clean; `--version` exits in ~1s cleanly; existing D12 phase-label test still passes. * fix(hygiene): [#1052 D8 review-fix follow-up] scrub example strings from scanner source The consensus review-fix for D8 (commit 0ffc42db97) added concrete tracker- key literals to the scanner's own test file and doc comments — e.g. inside `test.each([...])` fixtures, in the block comment above `RULES`, and in the pre-push hook comment. Those are the exact strings the scanner is meant to catch, so a pre-push run flagged them on this branch. That defeats the purpose: no example strings should appear as grep-visible literals in a repo whose whole rule is "don't put those literals here." Fix: - Test fixtures now build the strings at runtime from split prefix + digits (`const PREFIX = "A" + "I"`, `key(1234, "foo")` etc). The regex still sees what it needs to test; a source grep for the pattern finds nothing. - Scanner source comment loses its verbatim example strings — the intent is clear from the code + tests. - `.husky/pre-push` comment stops naming a specific pattern host; the scanner's own RULES are the source of truth. - Path-allowlist added in the same commit is no longer needed and dropped — keeps the guard strict for everyone. Verified: `bun test packages/opencode/test/skill/tracker-leak-check.test.ts` still 28/28 pass; scanner reports clean on the working tree. * fix(hygiene): [#1052 bot-review round] harden scanner + stamp + models.dev cache Addresses the actionable findings from the CodeRabbit + cubic-ai + kilo-code bot reviews on PR #1085 (Claude review skipped — not @Claude-Review'd). Only real issues; noisy or already-documented findings are noted in the PR reply. Scanner (`script/check-tracker-leaks.ts`): - **Cubic P1 (security):** the `--base` arg used to be embedded in `sh -c "${cmd}"`, which the shell then re-parsed — a caller could smuggle arbitrary shell via `--base=$(...)`. Replace with a shell-free `git(args[])` helper using Bun.$ tagged templates (each arg becomes one argv element, no shell). Belt-and-braces: reject `--base` values that don't look like a git ref (`^[A-Za-z0-9/_.@{}~^-]+$`) before running any git command, so a bad value fails loud instead of silently. - **Cubic P1 (correctness):** `shOK` used to `catch { return "" }`, so a real git failure (missing binary, corrupt index) reported as a clean scan. New helper `git()` fails loud with exit code 2 on unexpected errors; only `merge-base` (which legitimately returns empty on diverged history) opts into the silent path. - **CodeRabbit Major:** the diff parser's `!startsWith("+++")` filter dropped legitimate content lines starting with `++` (e.g. an added an added line whose text starts with two plus signs renders as `+++...` in unified-diff). Match the file header exactly (`+++ ` or `+++\t`) so a content line whose prefix happens to look like `+++<text>` still gets scanned. Build stamp (`packages/opencode/script/build.ts`): - **Kilo suggestion:** `_stampRoot` and `REPO_ROOT` computed the same value twice. Consolidated to one `REPO_ROOT` at the top of the block. - **Kilo suggestion:** stale comment claimed paths were "relative to the workspace root (dir = packages/opencode)" — post-M2 they're REPO_ROOT- relative. Corrected. - **Cubic P2:** `tsconfig.json` changes can flip target / moduleResolution and change the compiled output shape without editing any `.ts` file. Added `packages/opencode/tsconfig.json` to the stamp. models.dev cache (`packages/opencode/src/provider/models.ts`): - **CodeRabbit Minor:** a 2xx response can still carry HTML (proxies, error pages that respond 200) or truncated JSON. The old code wrote `result2.text` to the disk cache BEFORE the parse, so a bad body poisoned the cache for the next run + crashed on the current call. Now parses first; only caches + returns on success. On parse failure, log with a body preview and return an empty catalog (same graceful path as the non-2xx branch). Not addressed (deferred / disagreed / duplicated with known caveats): - Cubic P1 "cold-cache CLI still starts fetchApi → recreates D14 blocker" and kilo warning on the same line — these identify the trade-off the D14 review-fix commit explicitly documents; no change. - Cubic P1 "hook doesn't wire to `bun run script/check-tracker-leaks.ts`" — false positive; `.husky/pre-push` does invoke it. - CodeRabbit + cubic P2 "new files added post-build not detected" — real limitation, orthogonal fix, deferred to a follow-up (adding a walk at read-time would double the cost of every test run). Verified: typecheck 13/13; scanner self-test 28/28; scanner clean on this branch; scanner rejects `--base=$(...)` with exit 2; `--version` still exits in ~1s cleanly. * 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]> * fix(hygiene): [#1052 bot-review round 3] ref names, cache paths, stamp roots Round-3 findings, all on code round 2 introduced. Tracker scanner (coderabbit Major + cubic P1): - Scan the ref NAMES being pushed, both ends. Round 2 started scanning pushed ref CONTENT but still only ever scanned the checked-out branch's name, so `git push origin main:refs/heads/<tracker-key>` published a tracker-shaped branch while `main` and every line of its content were clean. The destination ref is the one that becomes public and need not match the source; tags go the same way. The checked-out branch name is now scanned only in the no-stdin fallback. models.dev cache (cubic P1 x2): - `refresh()` wrote 2xx bodies straight to the cache with no validation at all, so the hourly refresh could poison a cache the cold-fetch path was careful to protect. Both paths now share one validator, as does the disk-cache READ, so an entry poisoned by an older build is not trusted either. - That validator no longer accepts any object. An object-shaped error payload (`{"error": "rate limited"}`) passed the round-2 check. At least one value must now parse as a `Provider`. Deliberately "at least one" rather than "all": requiring all would let a single new upstream field empty the catalog, which is a worse failure than the one being prevented. Build stamp (coderabbit Minor x2, cubic P2, cubic P3): - Include `.mp3` in the shared input extensions. packages/tui imports its attention sounds with `{ type: "file" }`, so they are embedded in the binary, but the extension filter excluded them and a changed sound left the stamp fresh. - Record `packages/*/src` as a glob alongside the concrete roots. Concrete roots only describe packages that existed during the build, so a package added later was invisible to the guard. - Validate `roots`/`rootGlobs` like `inputs`. A non-array value threw a TypeError at module load inside `describe()`, failing the whole file instead of degrading to the mtime fallback — the opposite of the hardening round 2 added for `inputs`. Tests (cubic P3): - Remove the throwaway git repositories in `afterAll`. They carry tracker-key fixtures, so leaving them in tmp is worse than ordinary litter. - New end-to-end case for the destination-ref hole; it fails against the round-2 scanner. Verification: 7-case e2e matrix against real repositories (unborn HEAD, clean, content leak, pushed-ref content, no-stdin fallback, deletion-only push, tracker-shaped destination ref) all as expected; 34 scanner + 799 provider and install tests pass; 13/13 typecheck; no new lint warnings; markers balanced; scanner clean against its own diff. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> * fix(models): [#1052] structural catalog validation, not the zod schema Fixes a regression I introduced in round 3, caught by CI. Round 3 gated the models.dev cache on `Provider.safeParse`, as a review suggested. It looked right and was wrong: the schema requires a `Model.options` record that real models.dev entries do not carry, so it rejects 0 of the 144 providers in our own bundled snapshot. Nothing was ever cached, and every load fell through to a network fetch — which hangs in a sandboxed environment. The only signal was an unrelated CLI smoke test timing out at 60s ("fresh-DB production path adopts the core schema"). Bisected to round 3, then to this file. The validator is now structural: a non-empty map whose values are provider objects carrying a `models` map. That still rejects everything the reviewers were worried about — `null`, `[]`, scalars, and object-shaped error payloads like `{"error": "rate limited"}` — without coupling cache acceptance to a schema that does not match the payload. It moved to `provider/models-catalog.ts` (flat exports + self-reexport per AGENTS.md) so it is directly testable. Trying to cover it through `ModelsDev.get()` did not work: `Flag.OPENCODE_MODELS_PATH` is captured at module load, so a test cannot redirect the cache path afterwards. 12 new tests pin the behaviour, and the first one is the guard that was missing: the real bundled catalog must be accepted. `output-hygiene` is back to 3/3; provider, scanner and install suites total 811 pass. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
1 parent 930c0a6 commit 205a953

12 files changed

Lines changed: 1109 additions & 74 deletions

File tree

‎.husky/pre-push‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ if (process.versions.bun !== expectedBunVersion) {
1818
}
1919
'
2020
bun typecheck
21+
# altimate_change — #1052 D8: scan pushed content for internal-tracker refs
22+
# (see RULES in the script for the specific patterns). Silent on clean;
23+
# exits 1 on hit. Bypass with SKIP_TRACKER_CHECK=1 for genuine emergencies.
24+
bun script/check-tracker-leaks.ts

‎CONTRIBUTING.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ https://github.com/anomalyco/models.dev
3939
bun dev
4040
```
4141

42+
`bun install` sets up the pre-push hook via `husky` — subsequent `git push` runs a bun-version check, `bun typecheck`, and a scan for internal-tracker references. If the tracker scan blocks you legitimately (extremely unlikely on this public repo), bypass with `SKIP_TRACKER_CHECK=1 git push`.
43+
4244
### Running against a different directory
4345

4446
By default, `bun dev` runs Altimate Code in the `packages/opencode` directory. To run it against a different directory or repository:

‎packages/opencode/script/build.ts‎

Lines changed: 184 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import path from "path"
66
import { fileURLToPath } from "url"
77
import { createRequire } from "node:module"
88
import solidPlugin from "@opentui/solid/bun-plugin"
9+
// altimate_change — #1052 D10: sha256 for the per-target build-inputs stamp.
10+
import { createHash } from "node:crypto"
911

1012
const __filename = fileURLToPath(import.meta.url)
1113
const __dirname = path.dirname(__filename)
@@ -15,6 +17,7 @@ process.chdir(dir)
1517

1618
import { Script } from "@opencode-ai/script"
1719
import pkg from "../package.json"
20+
import { walkInputs } from "./stamp-inputs"
1821

1922
// Python engine has been eliminated — all methods run natively in TypeScript.
2023
// ALTIMATE_ENGINE_VERSION is no longer needed at runtime.
@@ -145,43 +148,49 @@ const allTargets: {
145148
]
146149

147150
// If --targets is provided, filter to only matching OS values
148-
const validOsValues = new Set(allTargets.map(t => t.os))
149-
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(",")
150156
if (targetsFlag) {
151-
const invalid = targetsFlag.filter(t => !validOsValues.has(t))
157+
const invalid = targetsFlag.filter((t) => !validOsValues.has(t))
152158
if (invalid.length > 0) {
153-
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+
)
154162
process.exit(1)
155163
}
156164
}
157165

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

186195
// Defense in depth: refuse to produce no artifacts at all, and refuse to build
187196
// the glibc target on a musl host where the binary would crash at startup.
@@ -194,13 +203,14 @@ const targets = targetIndexFlag !== undefined
194203
// `linux-x64` (glibc), produces a glibc binary that the musl host can't
195204
// load, and dies later with a cryptic linker error.
196205
if (targets.length === 0) {
197-
const reason = targetIndexFlag !== undefined
198-
? `--target-index=${targetIndexFlag} is out of range (allTargets has ${allTargets.length} entries — musl/win32-arm64 were removed).`
199-
: singleFlag
200-
? `--single found no entry in allTargets matching ${process.platform}/${process.arch} (host may be excluded — see allTargets at the top of build.ts).`
201-
: targetsFlag
202-
? `--targets=${targetsFlag.join(",")} matched nothing in allTargets.`
203-
: "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."
204214
console.error(`error: no build targets selected. ${reason}`)
205215
process.exit(1)
206216
}
@@ -219,8 +229,12 @@ if (singleFlag && process.platform === "linux") {
219229
return false
220230
})()
221231
if (isMuslHost) {
222-
console.error("error: --single on a musl-linux host would build the glibc target and produce a binary the host cannot run.")
223-
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+
)
224238
process.exit(1)
225239
}
226240
}
@@ -238,10 +252,18 @@ await $`rm -rf dist`
238252
const requiredExternals: string[] = []
239253
const optionalExternals = [
240254
// Database drivers — native addons, users install on demand per warehouse
241-
"pg", "snowflake-sdk", "@google-cloud/bigquery", "@databricks/sql",
242-
"mysql2", "mssql", "oracledb", "duckdb",
255+
"pg",
256+
"snowflake-sdk",
257+
"@google-cloud/bigquery",
258+
"@databricks/sql",
259+
"mysql2",
260+
"mssql",
261+
"oracledb",
262+
"duckdb",
243263
// Optional infra packages — native addons or heavy optional deps
244-
"keytar", "ssh2", "dockerode",
264+
"keytar",
265+
"ssh2",
266+
"dockerode",
245267
]
246268

247269
const binaries: Record<string, string> = {}
@@ -265,7 +287,9 @@ function altimateCorePlatformFor(item: { os: string; arch: "arm64" | "x64"; abi?
265287
platformTag: string
266288
} {
267289
if (item.abi === "musl") {
268-
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+
)
269293
}
270294
if (item.os === "darwin") {
271295
const tag = `darwin-${item.arch}`
@@ -280,7 +304,9 @@ function altimateCorePlatformFor(item: { os: string; arch: "arm64" | "x64"; abi?
280304
const tag = "win32-x64-msvc"
281305
return { pkg: `@altimateai/altimate-core-${tag}`, nodeFile: `altimate-core.${tag}.node`, platformTag: tag }
282306
}
283-
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+
)
284310
}
285311
throw new Error(`Unsupported build target: ${item.os}-${item.arch}`)
286312
}
@@ -297,9 +323,7 @@ const altimateCoreLoaderDir = fs.realpathSync(path.dirname(altimateCoreLoaderPkg
297323
// .node into today's release archive.
298324
{
299325
const expected = pkg.dependencies["@altimateai/altimate-core"]
300-
const resolvedVersion = JSON.parse(
301-
fs.readFileSync(path.join(altimateCoreLoaderDir, "package.json"), "utf8"),
302-
).version
326+
const resolvedVersion = JSON.parse(fs.readFileSync(path.join(altimateCoreLoaderDir, "package.json"), "utf8")).version
303327
if (resolvedVersion !== expected) {
304328
throw new Error(
305329
`build.ts: resolved @altimateai/altimate-core version ${resolvedVersion} ` +
@@ -523,6 +547,119 @@ for (const item of targets) {
523547
2,
524548
),
525549
)
550+
551+
// altimate_change start — #1052 D10: emit a build-inputs stamp so the
552+
// smoke-test staleness guard can compare against ALL binary-embedded inputs,
553+
// not just src/ + script/ mtimes.
554+
//
555+
// The previous guard (m5) walked src/ + script/ for the newest mtime — good
556+
// for the common case but blind to changes in CHANGELOG.md, migrations,
557+
// bundled skills, the models.dev snapshot, the parser worker, and the
558+
// per-platform altimate-core prebuild. Editing any of those without touching
559+
// a .ts file would leave the guard silent and the binary silently stale.
560+
//
561+
// Stamp format: JSON with one entry per input, sha256 of file content. Read
562+
// side rehashes each listed path and compares; any mismatch → stale. Paths
563+
// are REPO_ROOT-relative so entries under packages/tui, packages/core, the
564+
// workspace-root package.json, bun.lock, etc. resolve without munging.
565+
const REPO_ROOT = path.resolve(dir, "../..")
566+
const stampInputs: Array<{ path: string; sha256: string }> = []
567+
const addFile = (absPath: string) => {
568+
try {
569+
const buf = fs.readFileSync(absPath)
570+
const rel = path.relative(REPO_ROOT, absPath)
571+
const hash = createHash("sha256").update(buf).digest("hex")
572+
stampInputs.push({ path: rel, sha256: hash })
573+
} catch {
574+
// Missing file: silently skip. The stamp only covers what actually
575+
// shipped; a file the build didn't need doesn't invalidate the guard.
576+
}
577+
}
578+
// CHANGELOG.md
579+
addFile(changelogPath)
580+
// Migrations
581+
for (const m of migrationDirs) addFile(path.join(dir, "migration", m, "migration.sql"))
582+
// Skills bundled via .opencode/skills/
583+
for (const entry of skillEntries) addFile(path.join(skillsRoot, entry.name, "SKILL.md"))
584+
// Generated models snapshot (build.ts rewrote it before we got here)
585+
addFile(path.join(dir, "src/provider/models-snapshot.ts"))
586+
// opentui parser worker
587+
addFile(parserWorker)
588+
// Per-target altimate-core NAPI prebuild
589+
addFile(platformNodeSrc)
590+
// altimate_change — #1052 D10 review-fix (M2): package.json + bun.lock cover
591+
// dependency-version bumps that change what Bun.build embeds. Without these,
592+
// `bun install` bumping a bundled dep would leave the stamp reporting fresh.
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.
597+
addFile(path.join(REPO_ROOT, "package.json"))
598+
addFile(path.join(REPO_ROOT, "bun.lock"))
599+
addFile(path.join(dir, "package.json"))
600+
// Also include tsconfig files that affect compiled output shape
601+
// (bot review: tsconfig changes can flip target/moduleResolution).
602+
addFile(path.join(dir, "tsconfig.json"))
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[] = []
607+
const walk = (root: string): void => {
608+
if (!fs.existsSync(root)) return
609+
walkedRoots.push(path.relative(REPO_ROOT, root))
610+
for (const file of walkInputs(root)) addFile(file)
611+
}
612+
walk(path.join(dir, "src"))
613+
walk(path.join(dir, "script"))
614+
// altimate_change — #1052 D10 review-fix (M2): also hash every workspace
615+
// package's src/ tree. `packages/opencode/src` imports from
616+
// `@opencode-ai/{core,tui,util,plugin,sdk,server,cli,...}` and
617+
// `@altimateai/{dbt-tools,drivers}` — Bun.build follows these imports and
618+
// bundles them into the binary transitively. The original stamp walked only
619+
// packages/opencode, so edits under any sibling workspace package would leave
620+
// the binary silently stale. Enumerate `packages/*/src` at build time (rather
621+
// than hard-coding names) so new packages get covered automatically.
622+
const packagesRoot = path.resolve(REPO_ROOT, "packages")
623+
try {
624+
for (const pkg of fs.readdirSync(packagesRoot, { withFileTypes: true })) {
625+
if (!pkg.isDirectory() || pkg.name.startsWith(".")) continue
626+
// Skip packages/opencode — already covered by the walks above.
627+
if (pkg.name === "opencode") continue
628+
const pkgSrc = path.join(packagesRoot, pkg.name, "src")
629+
if (fs.existsSync(pkgSrc)) walk(pkgSrc)
630+
// Each workspace package.json influences its resolution/exports and could
631+
// change what ends up in the binary even when its src/ files are unchanged.
632+
const pkgJson = path.join(packagesRoot, pkg.name, "package.json")
633+
if (fs.existsSync(pkgJson)) addFile(pkgJson)
634+
}
635+
} catch {
636+
// packages/ missing (unlikely at build time) — skip; addFile() ignores non-existent paths anyway.
637+
}
638+
// Deterministic order so the aggregate hash is stable across build runs.
639+
stampInputs.sort((a, b) => a.path.localeCompare(b.path))
640+
const aggregate = createHash("sha256")
641+
.update(stampInputs.map((i) => `${i.path}\t${i.sha256}`).join("\n"))
642+
.digest("hex")
643+
await Bun.file(`dist/${name}/bin/build-inputs.json`).write(
644+
JSON.stringify(
645+
{
646+
target: name,
647+
version: Script.version,
648+
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(),
652+
// Glob form so the read side notices a package added AFTER this build;
653+
// concrete roots only describe what existed while it ran.
654+
rootGlobs: ["packages/*/src"],
655+
inputs: stampInputs,
656+
},
657+
null,
658+
2,
659+
),
660+
)
661+
// altimate_change end
662+
526663
binaries[name] = Script.version
527664
}
528665

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
/**
16+
* Extensions Bun.build can pull into the binary from a walked source tree.
17+
* `.mp3` is here because packages/tui imports its attention sounds with
18+
* `{ type: "file" }`, so a changed or added sound is embedded in the binary and
19+
* must invalidate the stamp like any source edit.
20+
*/
21+
export const INPUT_EXTENSIONS = /\.(tsx?|json|txt|md|mp3)$/
22+
23+
/**
24+
* Absolute paths of every input file under `root`, applying the shared rules.
25+
* Missing roots yield an empty list — callers treat that as "nothing to add".
26+
*/
27+
export function walkInputs(root: string): string[] {
28+
const found: string[] = []
29+
const walk = (current: string): void => {
30+
let entries: fs.Dirent[]
31+
try {
32+
entries = fs.readdirSync(current, { withFileTypes: true })
33+
} catch {
34+
return
35+
}
36+
for (const entry of entries) {
37+
if (entry.name.startsWith(".")) continue
38+
if (IGNORED_DIRS.has(entry.name)) continue
39+
const full = path.join(current, entry.name)
40+
if (entry.isDirectory()) {
41+
walk(full)
42+
continue
43+
}
44+
if (!INPUT_EXTENSIONS.test(entry.name)) continue
45+
found.push(full)
46+
}
47+
}
48+
walk(root)
49+
return found
50+
}
51+
// altimate_change end

0 commit comments

Comments
 (0)