|
| 1 | +# wp3 — Devin cloud-direct hardening |
| 2 | + |
| 3 | +Branch: codex/260912-devin-cloud-direct-hardening (base codex/260912-devin-cli-token-transition) |
| 4 | + |
| 5 | +## 1. Usage is decoded from the display field, not the usage field |
| 6 | + |
| 7 | +This is the defect the user can see, and it is confirmed against the reference proto. |
| 8 | + |
| 9 | +decodeUsageBlock in src/adapters/devin/cloud-direct/chat.ts treats GetChatMessageResponse |
| 10 | +field 28 as a usage block keyed by metric-id strings. In the Cognition schema carried by |
| 11 | +can1357/oh-my-pi: |
| 12 | + |
| 13 | + GetChatMessageResponse.usage = 7 (ModelUsageStats) |
| 14 | + GetChatMessageResponse.response_dimension_groups = 28 (repeated ResponseDimensionGroup) |
| 15 | + |
| 16 | + ModelUsageStats.input_tokens = 2 uint64 varint |
| 17 | + ModelUsageStats.output_tokens = 3 uint64 varint |
| 18 | + ModelUsageStats.cache_write_tokens = 4 uint64 varint |
| 19 | + ModelUsageStats.cache_read_tokens = 5 uint64 varint |
| 20 | + |
| 21 | +Field 28 is not an older usage shape. It is the current display message: |
| 22 | +ResponseDimensionGroup is {title, dimensions}, and ResponseDimension.uid is field 5 — which is |
| 23 | +exactly the sub-field today's decoder reads as metric_id. So the existing decoder works by |
| 24 | +reading presentation rows whose uid happens to spell the metric, and it yields cache numbers |
| 25 | +only when the server chose to render cache rows. Field 7 carries them unconditionally. |
| 26 | + |
| 27 | +Three consequences the first draft of this plan got wrong, corrected after audit: |
| 28 | + |
| 29 | +- Field 7 is uint64 varints. The existing entry walker only descends length-delimited |
| 30 | + sub-messages and reads a fixed32 float, so it cannot read field 7 at all. Field 7 needs its |
| 31 | + own decoder. |
| 32 | +- "Decode both, field 7 wins" is not what decoding both produces. Both fields arrive in the |
| 33 | + same response and src/adapters/devin.ts replaces usage on every usage event, so a naive |
| 34 | + addition lets field 28 land last and win. Within one message, field 7 must suppress |
| 35 | + field 28 outright; field 28 stays only as the fallback for a message that carries no field 7. |
| 36 | +- The adapter must merge usage fields across events rather than replacing the object, so a |
| 37 | + later partial frame cannot zero an earlier input count. |
| 38 | + |
| 39 | +## 2. Whether input_tokens already includes cache is not known, so do not assume it |
| 40 | + |
| 41 | +This repository's convention is inclusive: inputTokens covers the whole prompt, cachedInputTokens |
| 42 | +is the read subset, and totalTokens is input + output with no cache added on top. Adapters split |
| 43 | +on what the wire gives them — anthropic.ts and kiro-events.ts fold cache into input because their |
| 44 | +wire format is exclusive, while openai-responses.ts passes input_tokens through because it is |
| 45 | +already inclusive. |
| 46 | + |
| 47 | +oh-my-pi summing input + output + cacheRead + cacheWrite is evidence that Devin might be |
| 48 | +exclusive. It is not proof, and guessing wrong in the inclusive direction silently inflates |
| 49 | +input and bills cache at the uncached rate, because normalizeCostTokens only rejects |
| 50 | +read + write > input. |
| 51 | + |
| 52 | +So the mapping is derived from the frame rather than assumed: |
| 53 | + |
| 54 | + if (input >= cacheRead + cacheWrite) inputTokens = input // already inclusive |
| 55 | + else inputTokens = input + cacheRead + cacheWrite |
| 56 | + |
| 57 | +Both branches converge on the right answer for the case that prompted this work — a 58k prompt |
| 58 | +that is 57k cache read and 1k fresh reads as 58k total with a 57k cached subset whichever |
| 59 | +convention the wire uses — and neither branch can produce read + write > input. The heuristic |
| 60 | +is written down in the code with that reasoning, and replaced with a fixed mapping the moment a |
| 61 | +live ModelUsageStats frame settles the question. |
| 62 | + |
| 63 | +## 3. An HTTP status never reaches the classifier |
| 64 | + |
| 65 | +CloudChatError is thrown as "GetChatMessage failed (HTTP <status>)" with no status field, so a |
| 66 | +401 on a revoked import is a generic adapter failure rather than an authentication error, and |
| 67 | +inferHttpStatusFromAdapterMessage turns an HTTP 429 into a 502 — which means core's failover |
| 68 | +never rotates or backs off. Fix: carry status on the error and map 401, 403, 429 and 5xx. |
| 69 | + |
| 70 | +## 4. A client abort is reported as an upstream failure |
| 71 | + |
| 72 | +The adapter emits "Devin turn was aborted." with no status, and isClientClosedMessage does not |
| 73 | +recognise that wording, so a cancelled turn infers 502. Fix: emit the phrase the classifier |
| 74 | +already knows, with status 499. |
| 75 | + |
| 76 | +## Verification |
| 77 | + |
| 78 | +bun test tests/providers/devin-adapter.test.ts tests/providers/devin-hardening.test.ts |
0 commit comments