Skip to content

Commit 334adfe

Browse files
anandgupta42claude
andcommitted
fix: Windows grep outage and test-run telemetry pollution
Two production defects found in Azure telemetry over 2026-07-22 → 2026-08-05. **Windows `grep` broken for 99 machines** `core_failure` showed 328 events across 99 distinct Windows machines (of 617 total) carrying `? is not recognized as an internal or external command, operable program or batch file.` and its German, French, Spanish and Portuguese translations. Present on released 0.9.2, 0.9.3 and 0.9.4. Root cause: ripgrep's Windows release is a zip, and `RipgrepBinary` extracted it via `powershell.exe -Command Expand-Archive`, falling back to the literal string `"powershell.exe"` when neither `powershell.exe` nor `pwsh.exe` resolved. `cross-spawn`'s `parseNonShell()` sets `needsShell = true` when `resolveCommand()` returns undefined and re-spawns through `cmd.exe /d /s /c`, so cmd.exe produced that message. `throw new Error(result.stderr.trim())` made it the error verbatim, and since `RipgrepBinary.filepath` is `Effect.cached`, one failed extraction broke grep for the whole session. Upstream carries the same fragility: anomalyco/opencode#24291 is open, reporting `Expand-Archive` unusable when spawned from the Bun-compiled binary, affecting `grep`, `glob` and `skill`. Their #23457 fix only corrected how paths were passed to PowerShell (the `$args` → inlined-and-escaped form we already carry); it did not remove the dependency on PowerShell being resolvable. Extract the zip in-process with `@zip.js/zip.js`, converging on the approach the `packages/opencode/src/file/ripgrep.ts` shim already uses in production. `unzipExecutable` is exported so archive handling is tested directly, and decodes with `checkSignature: true` — zip.js defaults it off, and a CRC-corrupt download would otherwise be written to the cache and trusted by every later session. Install the binary atomically (stage to `rg.exe.tmp`, then rename). `filepath` trusts the cached binary on existence alone, so an interrupted write previously left a truncated `rg.exe` that every later session reused — the same permanent breakage `checkSignature` guards against, which CRC cannot catch because it is verified before the write. The tar path installs the same way. Attribute resolution failures. Child stderr was reported verbatim, so a shell-level failure was indistinguishable from a tool bug; and because a resolve failure is memoized, it is re-reported on every later grep in the session. The tar branch now names ripgrep, and any typed filesystem or HTTP failure is wrapped as `ripgrep binary resolve failed: …`. Note the blast radius is wider than the `grep` tool: `@opencode-ai/core/ripgrep` also backs the HTTP-API file handlers and `cli/cmd/debug/ripgrep.ts`. **Test runs shipped telemetry to the production resource** 1,020 of 3,135 machine ids in the same window emitted `provider_id="test"` / `cli_version="local"` — test processes that regenerate their machine id every run, inflating install and active-machine counts by roughly a third. `doInit()` gated only on `ALTIMATE_TELEMETRY_DISABLED`. Refuse the baked-in connection string when `NODE_ENV=test`, `BUN_TEST`, `VITEST` or `JEST_WORKER_ID` is set. Keyed on test runners, deliberately not on CI: `altimate-code-actions` wraps this CLI, so every run of that shipped product sets `CI`/`GITHUB_ACTIONS`, and gating on those would blind a real product surface. `bun test` sets `NODE_ENV=test`, which covers CI and developer machines alike. An explicit `APPLICATIONINSIGHTS_CONNECTION_STRING` is always honoured, so suites with their own sink are unaffected; `ALTIMATE_TELEMETRY_FORCE=true` overrides the default-sink refusal, and the existing opt-outs still win over both. Test plan: 8 ripgrep tests — including a layer-level test that drives `filepath` through the Windows zip path with a spawner that fails if invoked, asserting the staged-then-renamed install, and a CRC case that fails without `checkSignature` — plus 15 telemetry-gate tests covering CI-alone-still-reports and one that relies on the real runner's `NODE_ENV`. `turbo typecheck` green, marker guard green, `packages/core` and `packages/opencode` suites show no failures beyond the pre-existing set on `main`. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent 03b9459 commit 334adfe

10 files changed

Lines changed: 770 additions & 32 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,39 @@ jobs:
379379
$config.Output.Verbosity = "Detailed"
380380
Invoke-Pester -Configuration $config
381381
382+
# altimate_change start — Windows ripgrep E2E (issue #1072)
383+
# ---------------------------------------------------------------------------
384+
# Real Windows check for ripgrep binary resolution. Downloads and extracts the
385+
# actual archive with PowerShell stripped from PATH, then executes the binary.
386+
# This is the condition that broke grep for 99 Windows machines; no amount of
387+
# unit testing on Linux/macOS covers it.
388+
# ---------------------------------------------------------------------------
389+
windows-ripgrep-e2e:
390+
name: Windows ripgrep E2E
391+
needs: changes
392+
if: needs.changes.outputs.typescript == 'true' || github.event_name == 'push'
393+
runs-on: windows-latest
394+
timeout-minutes: 15
395+
steps:
396+
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
397+
398+
- uses: oven-sh/setup-bun@ecf28ddc73e819eb6fa29df6b34ef8921c743461 # v2
399+
with:
400+
bun-version: "1.3.14"
401+
402+
# --ignore-scripts: `tree-sitter-powershell` has no Windows prebuild and its fallback
403+
# compile needs Visual Studio Build Tools, which this runner does not have (see
404+
# anomalyco/opencode#25563). This check only needs the pure-JS dependency graph
405+
# (effect, @zip.js/zip.js, which, xdg-basedir), so skipping lifecycle scripts is enough.
406+
- name: Install dependencies
407+
run: bun install --ignore-scripts
408+
409+
# Run from packages/core so `effect` and the other deps resolve — they are not root deps.
410+
- name: Resolve ripgrep with PowerShell unavailable
411+
working-directory: packages/core
412+
run: bun run script/windows-ripgrep-e2e.ts
413+
# altimate_change end
414+
382415
# ---------------------------------------------------------------------------
383416
# dbt-tools E2E — slow (~3 min), only on push to main.
384417
# Tests dbt CLI fallbacks against real dbt versions (1.8, 1.10, 1.11) and

‎bun.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎docs/docs/reference/telemetry.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,24 @@ export ALTIMATE_TELEMETRY_DISABLED=true
126126

127127
When telemetry is disabled, no events are sent and no network requests are made to the telemetry endpoint.
128128

129+
### Test runs are excluded
130+
131+
Test runners never reach the default telemetry endpoint. Telemetry is suppressed when `NODE_ENV=test`,
132+
`BUN_TEST`, `VITEST`, or `JEST_WORKER_ID` is present. This exists because test processes regenerate
133+
their machine ID on every run, so without the exclusion they dominate install and active-machine counts.
134+
135+
Running in CI is **not** excluded — that is ordinary product usage (for example
136+
[altimate-code-actions](https://github.com/AltimateAI/altimate-code-actions) wraps this CLI), so
137+
`CI` and `GITHUB_ACTIONS` on their own do not suppress anything.
138+
139+
Two escape hatches exist for reporting from a test run deliberately:
140+
141+
- Set `APPLICATIONINSIGHTS_CONNECTION_STRING` to your own endpoint — an explicitly-configured sink
142+
is always honoured, which is how the project's own telemetry tests work.
143+
- Set `ALTIMATE_TELEMETRY_FORCE=true` to use the default endpoint anyway.
144+
145+
`ALTIMATE_TELEMETRY_DISABLED` and the config opt-out take precedence over both.
146+
129147
## Privacy
130148

131149
We take your privacy seriously. Altimate Code telemetry **never** collects:

‎packages/core/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
"@opentelemetry/sdk-trace-base": "2.6.1",
9898
"@parcel/watcher": "2.5.1",
9999
"@silvia-odwyer/photon-node": "0.3.4",
100+
"@zip.js/zip.js": "2.7.62",
100101
"@openrouter/ai-sdk-provider": "2.9.0",
101102
"ai-gateway-provider": "3.1.2",
102103
"bun-pty": "0.4.8",
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* Windows end-to-end check for ripgrep binary resolution.
3+
*
4+
* Reproduces the condition behind the outage in
5+
* https://github.com/AltimateAI/altimate-code/issues/1072: a cold cache on a Windows machine
6+
* where PowerShell cannot be resolved from PATH. The old implementation extracted ripgrep's zip
7+
* by shelling out to `powershell.exe -Command Expand-Archive`, so cross-spawn fell back to
8+
* `cmd.exe /d /s /c` and the extraction died with "is not recognized as an internal or external
9+
* command" — which `Effect.cached` then replayed for the rest of the session.
10+
*
11+
* This performs a real download and a real extraction, then executes the resulting binary. It is
12+
* deliberately not a unit test: the point is to exercise the actual filesystem, the actual archive
13+
* and the actual process launch on a real Windows host.
14+
*
15+
* Run: bun run script/windows-ripgrep-e2e.ts (from packages/core — `effect` resolves there)
16+
*/
17+
import { execFileSync } from "node:child_process"
18+
import { mkdtempSync, existsSync, readdirSync, statSync, rmSync } from "node:fs"
19+
import { tmpdir } from "node:os"
20+
import path from "node:path"
21+
22+
function fail(message: string): never {
23+
console.error(`FAIL: ${message}`)
24+
process.exit(1)
25+
}
26+
27+
function ok(message: string) {
28+
console.log(`ok ${message}`)
29+
}
30+
31+
if (process.platform !== "win32") fail(`this check only means anything on Windows (got ${process.platform})`)
32+
33+
// Cold cache: point the XDG cache root at a throwaway directory *before* importing anything that
34+
// reads it, since Global computes its paths at module load.
35+
const cacheRoot = mkdtempSync(path.join(tmpdir(), "rg-e2e-"))
36+
process.env.XDG_CACHE_HOME = cacheRoot
37+
process.env.LOCALAPPDATA = cacheRoot
38+
39+
// Remove every PATH entry that could provide PowerShell. System32 itself is kept so cmd.exe and
40+
// the rest of Windows still work — this simulates the locked-down/sanitized-PATH machines in the
41+
// telemetry, not a broken OS.
42+
const originalPath = process.env.PATH ?? process.env.Path ?? ""
43+
const stripped = originalPath
44+
.split(path.delimiter)
45+
.filter((entry) => entry && !/powershell/i.test(entry))
46+
.join(path.delimiter)
47+
process.env.PATH = stripped
48+
process.env.Path = stripped
49+
50+
const { which } = await import("../src/util/which")
51+
52+
// Guard against a vacuous run: if PowerShell is still resolvable, this proves nothing.
53+
const ps = which("powershell.exe")
54+
const pwsh = which("pwsh.exe")
55+
if (ps || pwsh) fail(`PowerShell is still resolvable (${ps ?? pwsh}) — the scenario was not reproduced`)
56+
ok("PowerShell is not resolvable on PATH (failure condition reproduced)")
57+
58+
// Also assert the binary really is absent, so we exercise download + extract rather than a cache hit.
59+
const { Global } = await import("../src/global")
60+
const target = path.join(Global.Path.bin, "rg.exe")
61+
if (existsSync(target)) fail(`expected a cold cache but ${target} already exists`)
62+
ok(`cold cache at ${Global.Path.bin}`)
63+
64+
const { Effect } = await import("effect")
65+
const { RipgrepBinary } = await import("../src/ripgrep/binary")
66+
67+
/** Resolve through the real layer: real HTTP, real filesystem, real process launch. */
68+
async function resolveBinary(): Promise<string> {
69+
const program = Effect.gen(function* () {
70+
const binary = yield* RipgrepBinary.Service
71+
return yield* binary.filepath
72+
}).pipe(Effect.provide(RipgrepBinary.defaultLayer))
73+
return (await Effect.runPromise(program as never)) as string
74+
}
75+
76+
let resolved: string
77+
try {
78+
resolved = await resolveBinary()
79+
} catch (err: unknown) {
80+
fail(`binary.filepath failed: ${err instanceof Error ? err.message : String(err)}`)
81+
}
82+
83+
ok(`resolved ${resolved}`)
84+
85+
if (!existsSync(resolved)) fail(`resolved path does not exist: ${resolved}`)
86+
const size = statSync(resolved).size
87+
if (size < 100_000) fail(`resolved binary is implausibly small (${size} bytes) — likely a partial write`)
88+
ok(`binary present, ${size} bytes`)
89+
90+
// No staging files should survive a successful install.
91+
const leftovers = readdirSync(Global.Path.bin).filter((f) => f.endsWith(".tmp"))
92+
if (leftovers.length > 0) fail(`staging files left behind: ${leftovers.join(", ")}`)
93+
ok("no staging files left behind")
94+
95+
// The real proof: the extracted binary actually executes.
96+
const version = execFileSync(resolved, ["--version"], { encoding: "utf8" })
97+
if (!/ripgrep\s+\d/.test(version)) fail(`unexpected --version output: ${version.trim()}`)
98+
ok(`executes: ${version.split("\n")[0]!.trim()}`)
99+
100+
// And it can actually search.
101+
const hit = execFileSync(resolved, ["--no-config", "NEEDLE_MARKER", "--", import.meta.filename], {
102+
encoding: "utf8",
103+
})
104+
if (!hit.includes("NEEDLE_MARKER")) fail("ripgrep did not return the expected match")
105+
ok("search returns matches") // NEEDLE_MARKER
106+
107+
// A second resolve must hit the cache and stay valid.
108+
const again = await resolveBinary()
109+
if (again !== resolved) fail(`second resolve returned a different path: ${again}`)
110+
ok("second resolve hits the cache")
111+
112+
// ---------------------------------------------------------------------------
113+
// Negative control. Everything above passing only proves the fix works if the OLD approach would
114+
// have failed here — otherwise this environment is simply too forgiving and the check is vacuous.
115+
// So reproduce what the previous implementation did (spawn PowerShell through cross-spawn, exactly
116+
// as `CrossSpawnSpawner` does) and require it to fail.
117+
// ---------------------------------------------------------------------------
118+
const launch = (await import("cross-spawn")).default
119+
const old = launch.sync("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", "exit 0"], {
120+
encoding: "utf8",
121+
})
122+
123+
if (old.status === 0) {
124+
fail("the old PowerShell approach still succeeds here — this environment does not reproduce the bug, so the run above proves nothing")
125+
}
126+
127+
const oldOutput = `${old.stderr ?? ""}${old.stdout ?? ""}${old.error?.message ?? ""}`.trim()
128+
ok(`old PowerShell path fails as expected: ${oldOutput.split("\n")[0]?.slice(0, 120) || `status=${old.status}`}`)
129+
130+
rmSync(cacheRoot, { recursive: true, force: true })
131+
console.log("\nPASS — ripgrep resolves, installs and runs on Windows with PowerShell unavailable,")
132+
console.log(" under conditions where the previous PowerShell-based extraction fails.")

‎packages/core/src/ripgrep.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ export const layer = Layer.effect(
140140
return yield* new InvalidPatternError({ pattern: input.pattern, message: stderr.trim() })
141141
}
142142
if (code !== 0 && code !== 1 && code !== 2) {
143-
return yield* failure(stderr.trim() || `ripgrep failed with code ${code}`)
143+
// altimate_change start — upstream_fix: keep child stderr attributable to ripgrep.
144+
// Reporting stderr verbatim made shell-level failures (e.g. a Windows "not recognized"
145+
// message) look like they came from the tool itself, with no hint of the real source.
146+
return yield* failure(`ripgrep failed with code ${code}: ${stderr.trim() || "no output"}`)
147+
// altimate_change end
144148
}
145149
return { items: code === 1 ? [] : rows, truncated: false, partial: code === 2 }
146150
}),

0 commit comments

Comments
 (0)