Repository navigation
fix(aggregator): confirm surfaced charges after delivery #3167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #!/usr/bin/env bun | ||
| // Confirm only the charges in the exact feed successfully installed remotely. | ||
| // The push script holds the state-specific flock across collection and confirmation. | ||
| import { readFile, rename, unlink, writeFile } from "node:fs/promises"; | ||
| import { homedir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { unmatchedKey } from "../src/charges-collect.ts"; | ||
|
|
||
| const feedPath = process.argv[2]; | ||
| if (!feedPath) throw new Error("usage: confirm-charges-surfaced.ts <delivered-feed>"); | ||
| const statePath = process.env.CHARGES_SURFACED_STATE_PATH ?? join(homedir(), "workspace/goals/keep-the-four-repos-healthy/hidden_files/charges-surfaced-state.json"); | ||
| const feed = JSON.parse(await readFile(feedPath, "utf8")); | ||
| if (feed.chargesAttention !== undefined && !Array.isArray(feed.chargesAttention)) throw new Error("invalid chargesAttention"); | ||
| const attention = feed.chargesAttention ?? []; | ||
| if (attention.length) { | ||
| let state: { surfaced?: unknown } = {}; | ||
| try { | ||
| state = JSON.parse(await readFile(statePath, "utf8")); | ||
| if (!state || !Array.isArray(state.surfaced) || !state.surfaced.every((key) => typeof key === "string")) throw new Error("invalid surfaced state"); | ||
| } catch (err) { | ||
| if ((err as NodeJS.ErrnoException).code !== "ENOENT") throw err; | ||
| } | ||
| const keys = new Set<string>((state.surfaced as string[] | undefined) ?? []); | ||
| for (const charge of attention) keys.add(unmatchedKey(charge)); | ||
| const temp = `${statePath}.${process.pid}.${crypto.randomUUID()}.tmp`; | ||
| try { | ||
| await writeFile(temp, JSON.stringify({ version: 1, surfaced: [...keys], updatedAt: new Date().toISOString() }, null, 2), { flag: "wx" }); | ||
| await rename(temp, statePath); | ||
| } finally { | ||
| await unlink(temp).catch(() => {}); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { test, expect } from "bun:test"; | ||
| import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
|
|
||
| // Explicit timeout: five subprocess push invocations can exceed the bun default 5s per-test timeout on slower hosts (Codex P2 on PR #3167). | ||
| test("only a successfully installed feed confirms its charges", async () => { | ||
| const dir = await mkdtemp(join(tmpdir(), "charges-push-")); | ||
| const bin = join(dir, "bin"); | ||
| const state = join(dir, "state.json"); | ||
| const known = join(dir, "known-hosts"); | ||
| const realBun = process.execPath; | ||
| // Resolve the real interpreter to an absolute path BEFORE the wrapper | ||
| // directory leads the child PATH; a bare "python3" wrapper would exec | ||
| // itself in a loop (Codex P2 on PR #3167). | ||
| const pythonOverride = process.env.TEST_PYTHON3; | ||
| const python = pythonOverride?.startsWith("/") | ||
| ? pythonOverride | ||
| : (Bun.which(pythonOverride ?? "python3") ?? pythonOverride ?? "python3"); | ||
| const charge = { normalized: "example", amountUsd: 12, at: "2026-10-01" }; | ||
| const script = join(import.meta.dir, "../scripts/push-fleet-feed.sh"); | ||
| try { | ||
| await mkdir(bin); | ||
| await writeFile(known, "pinned\n"); | ||
| // The fake collector selects from confirmed state, like the real collector. | ||
| await writeFile(join(bin, "bun"), `#!/usr/bin/env bash\nif [[ "$2" == scripts/collect-fleet-feed.ts ]]; then\n exec '${realBun}' '${join(dir, "collect.ts")}' "$4"\nfi\nexec '${realBun}' "$@"\n`, { mode: 0o755 }); | ||
| await writeFile(join(dir, "collect.ts"), `import {readFile,writeFile,appendFile} from 'node:fs/promises';\nlet keys=[]; try { keys=JSON.parse(await readFile(process.env.CHARGES_SURFACED_STATE_PATH,'utf8')).surfaced } catch {}\nconst fresh=!keys.includes('example|12.00|2026-10-01');\nawait appendFile(process.env.COLLECTION_LOG, fresh?'fresh\\n':'seen\\n');\nawait writeFile(process.argv[2],JSON.stringify({chargesAttention:fresh?[${JSON.stringify(charge)}]:[]}));\n`); | ||
| await writeFile(join(bin, "python3"), `#!/usr/bin/env bash\nexec '${python}' "$@"\n`, { mode: 0o755 }); | ||
| await writeFile(join(bin, "scp"), `#!/usr/bin/env bash\n[[ "$FAIL_STEP" != scp ]]\n`, { mode: 0o755 }); | ||
| await writeFile(join(bin, "ssh"), `#!/usr/bin/env bash\n[[ "$FAIL_STEP" != ssh ]]\n`, { mode: 0o755 }); | ||
| const run = async (fail: string) => { | ||
| const proc = Bun.spawn(["bash", script], { env: { ...process.env, PATH: `${bin}:${process.env.PATH ?? "/usr/bin:/bin"}`, HOME: dir, HTTPS_PROXY: "http://user:pass@localhost:3128", CHARGES_SURFACED_STATE_PATH: state, COLLECTION_LOG: join(dir, "collections"), PUSH_FLEET_FEED_KNOWN_HOSTS: known, FAIL_STEP: fail }, stdout: "pipe", stderr: "pipe" }); | ||
| const output = await new Response(proc.stderr).text(); | ||
| return { code: await proc.exited, output }; | ||
| }; | ||
| for (const step of ["scp", "ssh"]) { | ||
| expect((await run(step)).code).not.toBe(0); | ||
| expect(await Bun.file(state).exists()).toBe(false); | ||
| } | ||
| await writeFile(state, JSON.stringify({ version: 1, surfaced: ["previous"] })); | ||
| const successes = await Promise.all([run(""), run("")]); | ||
| expect(successes).toEqual([{ code: 0, output: "" }, { code: 0, output: "" }]); | ||
| expect((await readFile(join(dir, "collections"), "utf8")).trim().split("\n")).toEqual(["fresh", "fresh", "fresh", "seen"]); | ||
| expect(JSON.parse(await readFile(state, "utf8")).surfaced).toEqual(["previous", "example|12.00|2026-10-01"]); | ||
| expect((await run("")).code).toBe(0); | ||
| expect(JSON.parse(await readFile(state, "utf8")).surfaced).toEqual(["previous", "example|12.00|2026-10-01"]); | ||
| } finally { | ||
| await rm(dir, { recursive: true, force: true }); | ||
| } | ||
| }, 30000); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.