Commit f7f5932
fix(runtime): emit consistent token_invalidated body on upstream-401 path (#497)
* fix(runtime): emit consistent token_invalidated body on upstream-401 path
The upstream-401 invalidation path forwarded the raw OpenAI body, so
clients keying off error.code saw a stable "token_invalidated" code on
the refresh-failure path but not here (Greptile P3 on #496).
- Add buildTokenInvalidationBody(): wraps both paths in the same
{ error: { message, code: "token_invalidated" } } shape, preserving
the upstream message when present and falling back to a stable message
for non-JSON bodies (no markup echoed to clients).
- Force content-type: application/json and drop content-length/-encoding
via responseHeadersForClient since the body is rewritten.
- Document that applyMonotonicAuthCooldown intentionally uses Date.now()
(not the injected now()) to match the markAccountCoolingDown/nowMs()
write domain, guarding against a regression that would defeat the
monotonic cooldown race protection.
- Strengthen existing upstream-401 and html-body tests to assert the
client contract (code + message).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* refactor(runtime): route refresh-failure 401 through shared invalidation builder + unit-test branches
Addresses two Greptile P2 comments on this PR:
- The refresh-failure invalidation exit still hardcoded the body literals
the new constants were meant to centralise, reopening the exact drift
risk this PR closes. Route it through buildTokenInvalidationBody(""),
which yields the same { error: { message: <fallback>, code:
"token_invalidated" } } so both paths can no longer diverge.
- Export buildTokenInvalidationBody and add 8 unit tests covering all
message-extraction branches: empty input, top-level message, nested
error.message, top-level-wins priority, blank-top-level -> nested
fallback, non-JSON body, and no-usable-message fallback.
Full suite: 4062 tests pass (4054 + 8). typecheck/lint/build clean.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>1 parent 4569197 commit f7f5932
2 files changed
Lines changed: 121 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
666 | 666 | | |
667 | 667 | | |
668 | 668 | | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
669 | 674 | | |
670 | 675 | | |
671 | 676 | | |
| |||
843 | 848 | | |
844 | 849 | | |
845 | 850 | | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
846 | 886 | | |
847 | 887 | | |
848 | 888 | | |
| |||
1516 | 1556 | | |
1517 | 1557 | | |
1518 | 1558 | | |
1519 | | - | |
1520 | | - | |
1521 | | - | |
1522 | | - | |
1523 | | - | |
1524 | | - | |
1525 | | - | |
1526 | | - | |
| 1559 | + | |
| 1560 | + | |
| 1561 | + | |
| 1562 | + | |
1527 | 1563 | | |
1528 | 1564 | | |
1529 | 1565 | | |
| |||
1742 | 1778 | | |
1743 | 1779 | | |
1744 | 1780 | | |
1745 | | - | |
1746 | | - | |
| 1781 | + | |
| 1782 | + | |
| 1783 | + | |
| 1784 | + | |
| 1785 | + | |
| 1786 | + | |
| 1787 | + | |
1747 | 1788 | | |
1748 | 1789 | | |
1749 | 1790 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
1857 | 1858 | | |
1858 | 1859 | | |
1859 | 1860 | | |
| 1861 | + | |
| 1862 | + | |
| 1863 | + | |
| 1864 | + | |
| 1865 | + | |
| 1866 | + | |
| 1867 | + | |
1860 | 1868 | | |
1861 | 1869 | | |
1862 | 1870 | | |
| |||
2027 | 2035 | | |
2028 | 2036 | | |
2029 | 2037 | | |
| 2038 | + | |
| 2039 | + | |
| 2040 | + | |
| 2041 | + | |
| 2042 | + | |
2030 | 2043 | | |
2031 | 2044 | | |
2032 | 2045 | | |
2033 | 2046 | | |
| 2047 | + | |
| 2048 | + | |
| 2049 | + | |
| 2050 | + | |
| 2051 | + | |
| 2052 | + | |
| 2053 | + | |
| 2054 | + | |
| 2055 | + | |
| 2056 | + | |
| 2057 | + | |
| 2058 | + | |
| 2059 | + | |
| 2060 | + | |
| 2061 | + | |
| 2062 | + | |
| 2063 | + | |
| 2064 | + | |
| 2065 | + | |
| 2066 | + | |
| 2067 | + | |
| 2068 | + | |
| 2069 | + | |
| 2070 | + | |
| 2071 | + | |
| 2072 | + | |
| 2073 | + | |
| 2074 | + | |
| 2075 | + | |
| 2076 | + | |
| 2077 | + | |
| 2078 | + | |
| 2079 | + | |
| 2080 | + | |
| 2081 | + | |
| 2082 | + | |
| 2083 | + | |
| 2084 | + | |
| 2085 | + | |
| 2086 | + | |
| 2087 | + | |
| 2088 | + | |
| 2089 | + | |
| 2090 | + | |
| 2091 | + | |
| 2092 | + | |
| 2093 | + | |
| 2094 | + | |
| 2095 | + | |
| 2096 | + | |
| 2097 | + | |
| 2098 | + | |
| 2099 | + | |
| 2100 | + | |
| 2101 | + | |
| 2102 | + | |
| 2103 | + | |
0 commit comments