Commit Graph
4 Commits
Author SHA1 Message Date
yetone e13f151302 gateway tests: TestConcurrentRedactedCountTokens' children run the stand-in CLIs their parent already ran, and stop at the parent's deadline rather than a fixed 20s
The test re-executes its own binary once per mode so the redaction key
cache is cold. Each child went through TestMain again, which wrote all
eight stand-in CLIs (security, claude, codex, cursor-agent, devin, ...)
into a new temp home. On macOS a newly written program waits for the
system's first-run check before it runs. The child's fixture,
provider.Save, calls Accounts(), which runs those stand-ins (cursor and
devin identity asks, each waited for up to 3s, and `security` for the
Claude credential). So the child spent its budget on account discovery
it doesn't test. The masked count_tokens work under test takes ~6ms.

Evidence (stacks from the child's own timeout panic): every timed-out
child sat in provider.Save -> Accounts -> cliIdentity.get /
askDevinStatus / askCursorStatus / readClaudeCredential, waiting on a
stand-in's exec. None was in redact or count_tokens. Timing provider.Save
in the child under scratchpad load.sh (CPU burners plus new-script exec
loops), 6 interleaved runs each: 5.7-9.4s before, 3.9-5.1s after (one
8.1s). Idle: 4-6s before, 1.7-2.2s after. Under `taskpolicy -c
background` the old children timed out at 20s in 2 of 4 runs and in 2 of
6 earlier ones.

What remains after that is CPU starvation. Under background QoS at load
average ~150, children still went past 20s. Their stacks were runnable in
json encoding, or in `security` exec'd from a stand-in that had already
run, with nothing blocked. So the child's -test.timeout is now 90% of
the parent's remaining deadline instead of a fixed 20s. A child that
really hangs still prints its stacks before the parent's run ends.

Children now take the parent's stand-in folder from
MAGPIE_GATEWAY_TEST_STAND_INS. A child still gets its own temp HOME and
XDG folders.

Verified: -race -count=10 of the test; go test -race ./internal/gateway
(ok, 739s on a loaded machine; TestLargeRequestEndpoints alone took 272s
under -race, so the default 10m -timeout is too short there); go vet
./...; full go test -tags nogui ./... has gateway ok (298s).
2026-10-07 06:10:06 +08:00
yetone 4216d2b338 redact: a gateway's key in "key": "oc_sk_…" is masked, and Settings → Privacy takes the user's own masking rules (#195) — an OpenCode Go key in providers.json went to the vendor as it was: the API key rules wanted sk- with a hyphen, and a field called key alone was none of the secret field's names. Now a key of a gateway or relay, two to four letters and _sk_ (oc_sk_, or_sk_) with digits in it, is an API_KEY wherever it is, and a field named key and nothing more (not sort_key, monkey or obj.key) is a SECRET when its value is 16 or more letters and digits both, so a map's "key": "value" and key = "someIdentifier" are left alone; \"key\": \"…\" in JSON inside a string is found too. The secret field's names and its markers come from one list, so they can't drift apart, and of two rules finding the same value the first keeps it, so its placeholder no longer changes between an API_KEY and a SECRET from one request to the next. Masking rules, next to 脱敏词: a name for the placeholders (upper case letters, digits and _, 24 at most) and a prefix (then 8 or more key characters) or an RE2 regular expression, group 1 the value if it has one, masked with the secrets while 脱敏密钥 is on and put back as the others are, the user's name winning where one of magpie's finds the same value. They are set on their own: a pattern that doesn't compile, matches nothing at all, or is too long (300), a prefix under 3 or over 64, both at once, or more than 32 rules, is refused with the reason under the add row and what was typed kept. Tested: every row of the issue's table and the false positives, the rules' tidying, errors, stable placeholders, restore, overlaps, and kinds with digits in a stream; the settings file and the gateway's count_tokens with a rule; the page in a stub magpie web (add, a bad regex, remove, zh dark) 2026-09-29 15:26:01 +08:00
Zhizhen HeandClaude Code 41de0d87eb gateway: count_tokens counts the masked prompt, and a count that cannot be had is told apart — the count Claude Code asks for before every turn went to the vendor as the conversation was written, so Mask secrets was defeated on every one; it now goes masked, the same prompt the turn it counts sends. An endpoint that doesn't implement counting (404/405/501, or a 400 that says so, in English or Chinese) falls back to the local estimate as before; a limited key or a broken connection yields to the next checked key and rests the one that failed, as a generation would, while a client's own cancellation rests nothing; and a real error — a wrong key, a prompt past the model's context, an image it won't take — reaches the agent. The gateway tests are isolated from the machine now (a temp HOME, inert stubs for the sign-in CLIs and the keychain) so a cache that outlives a test can't reach a real login, and the redaction tests' cold starts run in parallel. (#103)
Co-authored-by: Claude Code <noreply@anthropic.com>
2026-09-27 18:58:23 +08:00
yetone 67c539d5da gateway: secrets can stay on the machine — with Settings › Privacy › Mask secrets on, API keys, private keys, tokens, JWTs and the passwords of connection strings and assignments in what an agent sends go to the vendor as placeholders like {{API_KEY_k3v9x2mq}}, and what the vendor answers has them back, streams included (a placeholder split between two events is put back whole, a tool call's arguments get the value escaped); personal data (emails, phone numbers, ID and bank card numbers, checksum-checked) and the user's own words can be masked too. A placeholder is the same for the same value every time, so a conversation's history and what the vendor cached of it stay the same turn after turn, and the request log shows what the vendor saw. Suggested on X as maskit's idea; written from scratch 2026-09-26 14:21:14 +08:00