From ac762543f8dff4e5c2dcea0e56088817115c68ab Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Sun, 15 Nov 2026 17:32:54 +0000 Subject: [PATCH] fix(core): take a leading-dash Apify reference as the positional value (#3689) About one in 4,096 random base64url Apify consent references and run handles starts with --, and brain apify read it as an unknown flag. That intermittently failed apify-front-doors on main (incident #4026). The apify command now moves a 43-character dashed reference behind a bare -- before the shared parser runs. Shape, entropy, stored values and the general parser are unchanged. Closes #3689 Refs #4026 Co-Authored-By: Claude Opus 5.5 --- .changeset/3689-dashed-apify-references.md | 5 +++ packages/core/src/cli/commands/apify.ts | 24 +++++++++++++- .../ui-server/tests/apify-front-doors.test.ts | 31 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 .changeset/3689-dashed-apify-references.md diff --git a/.changeset/3689-dashed-apify-references.md b/.changeset/3689-dashed-apify-references.md new file mode 100644 index 0000000000..d9634be3b4 --- /dev/null +++ b/.changeset/3689-dashed-apify-references.md @@ -0,0 +1,5 @@ +--- +"@schlessera/brain": patch +--- + +`brain apify inspect`, `prepare`, `run`, `status`, `results` and `abort` now accept an Apify reference or run handle that starts with `--` in the documented positional form (#3689). These are random base64url values, and about one in 4,096 began with `--`. Until now the CLI refused those as `Unknown flag`. References keep their shape and entropy, stored values need no migration, and the `--` separator form still works. diff --git a/packages/core/src/cli/commands/apify.ts b/packages/core/src/cli/commands/apify.ts index b1699e8a00..a9aed491cd 100644 --- a/packages/core/src/cli/commands/apify.ts +++ b/packages/core/src/cli/commands/apify.ts @@ -33,6 +33,28 @@ export const apifyCommandDeps: { create: (root: string) => StandaloneApify } = { create: root => createStandaloneApify({ root }), }; +/** + * A consent reference or run handle that starts with `--`. Both are 32 random + * bytes in base64url, 43 characters (`reference`, + * packages/common/src/apify/consent.ts:412; the run handle, + * packages/common/src/apify/ledger.ts:390), and about one in 4,096 begins with + * `--`. No flag has this shape, so it is the positional value (#3689). + */ +const DASHED_REFERENCE = /^--[A-Za-z0-9_-]{41}$/u; + +/** + * The arguments with each dashed reference before a bare `--` moved after one, + * where the shared parser keeps it positional verbatim. Everything else keeps + * its order, and values already after a separator are untouched. + */ +function referencesAsPositional(args: string[]): string[] { + const separator = args.indexOf("--"); + const options = separator === -1 ? args : args.slice(0, separator); + const dashed = options.filter(arg => DASHED_REFERENCE.test(arg)); + if (!dashed.length) return args; + return [...options.filter(arg => !DASHED_REFERENCE.test(arg)), "--", ...dashed, ...(separator === -1 ? [] : args.slice(separator + 1))]; +} + function number(flag: string, value: string | boolean | undefined): number | undefined { if (value === undefined) return undefined; if (typeof value !== "string" || !/^-?\d+(\.\d+)?$/u.test(value.trim())) throw new UsageError(`--${flag} needs a number.`); @@ -94,7 +116,7 @@ export const apifyCommand: CoreCommand = { summary: "Discover, inspect and run Apify Actors under the governance policy", helpBlock: USAGE, async run(args, cli) { - const { args: positional, flags } = parseArgs(args); + const { args: positional, flags } = parseArgs(referencesAsPositional(args)); const [word, ...rest] = positional; if (!word) throw new UsageError(`Usage: ${USAGE.split("\n")[0]}`); const common = ["json", "human", "session"]; diff --git a/packages/ui-server/tests/apify-front-doors.test.ts b/packages/ui-server/tests/apify-front-doors.test.ts index b9eed813fb..0b83b8d388 100644 --- a/packages/ui-server/tests/apify-front-doors.test.ts +++ b/packages/ui-server/tests/apify-front-doors.test.ts @@ -10,6 +10,7 @@ * account, prices and origins are fictional. */ import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"; +import * as crypto from "node:crypto"; import { mkdtempSync, rmSync, mkdirSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; @@ -221,6 +222,36 @@ describe("one implementation behind every front door", () => { expect(fake.seen.every(entry => !entry.path.includes(APIFY_SENTINEL))).toBe(true); }); + test("references and run handles that start with -- work in the direct positional form (#3689)", async () => { + // Every 32-byte draw encodes to a distinct base64url value with a leading `--`, as one in 4,096 + // real references and handles does: the consent reference and the ledger's run handle alike. + const real = crypto.randomBytes; + let draws = 0; + const forced = spyOn(crypto, "randomBytes").mockImplementation(((size: number, ...rest: unknown[]) => size === 32 + ? Buffer.from(`--odysseus${(++draws).toString(36)}`.padEnd(43, "A"), "base64url") + : (real as (...args: unknown[]) => Buffer)(size, ...rest)) as typeof crypto.randomBytes); + try { + apifyPolicy(brain, { enabled: true, discovery: "allow", actors: "allow", actor_grants: [apifyGrant()], limits: APIFY_LIMITS }); + const approvals = openApifyLedger(ledgerPath); + approvals.setActorApproval({ brain_id: (await import("@schlessera/brain-common/internal/apify")).apifyBrainId(brain), principal_id: "local", account_id: "IthacaHousehold01" }, + { actor_id: "SeaCharts01", actor: "odysseus/sea-charts", approved_at: Date.now() }, true); + approvals.close(); + const candidate = data>((await cli(["approvals"])).envelope)[0]!.candidate; + expect(candidate, "APIFY_FORCED_DASHED_REFERENCE").toMatch(/^--[A-Za-z0-9_-]{41}$/u); + expect((await cli(["inspect", candidate])).envelope, "APIFY_DASHED_INSPECT").toMatchObject({ ok: true, operation: "inspect" }); + // An existing stored value still resolves through the separator form, options before it. + expect((await cli(["inspect", "--", candidate])).envelope, "APIFY_DASHED_SEPARATOR").toMatchObject({ ok: true, operation: "inspect" }); + const prepared = data<{ proposal: string }>((await cli(["prepare", candidate, "--input", JSON.stringify(APIFY_INPUT), "--max-charge-usd", "2"])).envelope); + expect(prepared.proposal, "APIFY_FORCED_DASHED_PROPOSAL").toMatch(/^--/u); + const started = data<{ run: string; state: string }>((await cli(["run", prepared.proposal])).envelope); + expect(started.run, "APIFY_FORCED_DASHED_HANDLE").toMatch(/^--[A-Za-z0-9_-]{41}$/u); + expect(code((await cli(["status", started.run])).envelope), "APIFY_DASHED_STATUS").toBe("ok"); + expect(data<{ items: unknown[] }>((await cli(["results", started.run, "--limit", "2"])).envelope).items, "APIFY_DASHED_RESULTS").toHaveLength(2); + expect(data<{ outcome: string }>((await cli(["abort", started.run])).envelope).outcome, "APIFY_DASHED_ABORT").toBe("not_needed"); + expect(fake.starts()).toBe(1); + } finally { forced.mockRestore(); } + }); + test("the MCP wrappers keep a fixed roster and return discovered schemas as data", async () => { apifyPolicy(brain, { enabled: true, discovery: "allow", actors: "allow", actor_grants: [apifyGrant()], limits: APIFY_LIMITS }); const door = await mcp();