Repository navigation
perf(cli): avoid hashing invalid inline image cache hits #10626
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
Changes from 1 commit
390f4e9
141e2fb
2fb460e
9803b78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import crypto from 'node:crypto'; | ||
| import fs from 'node:fs/promises'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
|
|
@@ -133,52 +134,78 @@ describe('terminalImageRenderer', () => { | |
| } | ||
| }); | ||
|
|
||
| it('caches repeated invalid inline payloads and evicts old entries', () => { | ||
| it('caches invalid inline payloads with bounded LRU eviction', () => { | ||
| const invalidPayloads = Array.from( | ||
| { length: INLINE_DECODE_NEGATIVE_CACHE_LIMIT + 1 }, | ||
| (_, index) => Buffer.from(`not a png ${index}`).toString('base64'), | ||
| (_, index) => | ||
| Buffer.from(`invalid LRU payload ${index}`).toString('base64'), | ||
| ); | ||
| const bufferFrom = vi.spyOn(Buffer, 'from'); | ||
|
|
||
| try { | ||
| for (const data of invalidPayloads) { | ||
| prepareInlineTerminalImage({ | ||
| data, | ||
| mimeType: 'image/png', | ||
| contentWidth: 24, | ||
| env: { TERM: 'xterm-kitty' }, | ||
| stdoutIsTTY: true, | ||
| }); | ||
| } | ||
|
|
||
| const createHash = vi.spyOn(crypto, 'createHash'); | ||
| const prepare = (data: string) => | ||
| prepareInlineTerminalImage({ | ||
| data: invalidPayloads[0], | ||
| data, | ||
| mimeType: 'image/png', | ||
| contentWidth: 24, | ||
| env: { TERM: 'xterm-kitty' }, | ||
| stdoutIsTTY: true, | ||
| }); | ||
|
|
||
| try { | ||
| for (const data of invalidPayloads.slice( | ||
| 0, | ||
| INLINE_DECODE_NEGATIVE_CACHE_LIMIT, | ||
| )) { | ||
| prepare(data); | ||
| } | ||
| expect(bufferFrom).toHaveBeenCalledTimes( | ||
| INLINE_DECODE_NEGATIVE_CACHE_LIMIT, | ||
| ); | ||
|
|
||
| prepare(invalidPayloads[0]); | ||
| expect(bufferFrom).toHaveBeenCalledTimes( | ||
| INLINE_DECODE_NEGATIVE_CACHE_LIMIT, | ||
| ); | ||
|
|
||
| prepare(invalidPayloads[INLINE_DECODE_NEGATIVE_CACHE_LIMIT]); | ||
| expect(bufferFrom).toHaveBeenCalledTimes( | ||
| INLINE_DECODE_NEGATIVE_CACHE_LIMIT + 1, | ||
| ); | ||
|
|
||
| prepare(invalidPayloads[0]); | ||
| expect(bufferFrom).toHaveBeenCalledTimes( | ||
| INLINE_DECODE_NEGATIVE_CACHE_LIMIT + 1, | ||
| ); | ||
|
|
||
| prepare(invalidPayloads[1]); | ||
| expect(bufferFrom).toHaveBeenCalledTimes( | ||
| INLINE_DECODE_NEGATIVE_CACHE_LIMIT + 2, | ||
| ); | ||
| expect(createHash).not.toHaveBeenCalled(); | ||
| } finally { | ||
| bufferFrom.mockRestore(); | ||
| createHash.mockRestore(); | ||
| } | ||
| }); | ||
|
|
||
| it('rejects inline payloads above the shared image limit before decoding', () => { | ||
| const oversizedBase64 = 'A'.repeat(MAX_INLINE_IMAGE_ENCODED_LENGTH + 1); | ||
| const bufferFrom = vi.spyOn(Buffer, 'from'); | ||
|
|
||
| expect( | ||
| prepareInlineTerminalImage({ | ||
| data: oversizedBase64, | ||
| mimeType: 'image/png', | ||
| contentWidth: 24, | ||
| env: { TERM: 'xterm-kitty' }, | ||
| stdoutIsTTY: true, | ||
| }), | ||
| ).toEqual({ fallbackText: '[image: png]', result: null }); | ||
| try { | ||
| expect( | ||
| prepareInlineTerminalImage({ | ||
| data: oversizedBase64, | ||
| mimeType: 'image/png', | ||
| contentWidth: 24, | ||
| env: { TERM: 'xterm-kitty' }, | ||
| stdoutIsTTY: true, | ||
| }), | ||
| ).toEqual({ fallbackText: '[image: png]', result: null }); | ||
| expect(bufferFrom).not.toHaveBeenCalled(); | ||
| } finally { | ||
|
Comment on lines
+246
to
+248
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in The round-2 cache-pollution witness is covered too: after filling all 64 resident entries, the test submits an oversized payload and confirms that the original oldest entry is still resident. Moving the length guard back into the decoder makes that assertion fail through an extra decode. Payloads in the count/LRU test share a 64-character prefix, which also makes the former N5 prefix-key mutation fail. The focused file passes 32/32 tests, and an independent verifier killed all five relevant mutations: missing hit refresh, guard-down/cache pollution, missing byte bound, prefix key, and restored SHA key. 中文说明已在 第 2 轮指出的缓存污染也已有承重见证:测试填满全部 64 个驻留项后提交超限 payload,并确认原本最旧的条目仍然驻留。把长度 guard 移回 decoder 会导致额外一次解码,从而让该断言失败。count/LRU 测试中的 payload 共享 64 字符前缀,因此此前的 N5 前缀 key 变异也会失败。 目标测试文件 32/32 通过;独立 verifier 杀死了五类相关变异:删除命中刷新、guard 下移并污染缓存、删除字节上限、前缀 key,以及恢复 SHA key。 |
||
| bufferFrom.mockRestore(); | ||
| } | ||
| }); | ||
|
|
||
| it('rejects inline PNG dimensions above the shared image limit', () => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in
2fb460ef0957e2c09e6f631a7fb173af924055fe. The oversized-path test now spies oncrypto.createHashand asserts zero calls, as suggested.I also strengthened the resident-cache witness because the standalone SHA assertion alone does not detect every cache-pollution form once the cache uses raw keys: the LRU test fills all 64 resident entries, submits an oversized payload, and then revisits the original oldest entry. Moving the guard back into the decoder now evicts that resident and makes the test fail with an extra decode. The same test uses payloads sharing a 64-character prefix, so a truncated-key implementation fails as well.
The focused file passes 32/32 tests. Independent mutation checks killed the guard/cache-pollution regression, restored SHA key, 64-character prefix key, missing LRU refresh, and missing byte-limit branch.
中文说明
已在
2fb460ef0957e2c09e6f631a7fb173af924055fe中处理。超限路径测试现在按建议对crypto.createHash打桩并断言调用次数为零。同时加固了驻留缓存见证,因为负缓存改用 raw key 后,单独的零 SHA 断言并不能识别所有缓存污染形式:LRU 测试先填满 64 个驻留项,再提交一个超限 payload,最后复访原本最旧的条目。把 guard 移回 decoder 会挤掉该驻留项并产生额外一次解码,因此测试会失败。该测试的 payload 还共享 64 字符前缀,所以截断 key 的实现同样会失败。
目标测试文件 32/32 通过。独立变异验证已经杀死 guard/缓存污染、恢复 SHA key、64 字符前缀 key、删除 LRU refresh 和删除 byte-limit 分支五类回归。