summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAddy Osmani <addy@anthropic.com>2026-10-04 02:30:41 -0700
committerAddy Osmani <addy@anthropic.com>2026-10-04 02:30:41 -0700
commitd9011aae1d144295ddfaacf1045fd5e6a5f3ef3a (patch)
treed8a61dc7096ff1d66e683957898c98cba1f4f127
parentd182ca456ca09d31d139f7d3818d1d333b103cce (diff)
Add context-lens pluginadd-context-lens
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XMBzd1x8Tvqv7hNFgmwUE4
-rw-r--r--.claude-plugin/marketplace.json12
-rw-r--r--plugins/context-lens/.claude-plugin/plugin.json17
-rw-r--r--plugins/context-lens/LICENSE201
-rw-r--r--plugins/context-lens/README.md49
-rw-r--r--plugins/context-lens/hooks/hooks.json5
-rw-r--r--plugins/context-lens/hooks/register.tsx789
-rw-r--r--plugins/context-lens/tests/context-lens.test.tsx186
-rw-r--r--plugins/context-lens/types/index.d.ts88
8 files changed, 1347 insertions, 0 deletions
diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.json
index cf0bec77..9fff910e 100644
--- a/.claude-plugin/marketplace.json
+++ b/.claude-plugin/marketplace.json
@@ -4421,6 +4421,18 @@
"sha": "10573bac239b7062a476667eaa574d400335c70e"
},
"homepage": "https://github.com/amzn/selling-partner-agentic-toolkit"
+ },
+ {
+ "name": "context-lens",
+ "description": "Browse and search exactly what is in the context window, request by request",
+ "version": "0.1.0",
+ "author": {
+ "name": "Anthropic",
+ "email": "support@anthropic.com"
+ },
+ "source": "./plugins/context-lens",
+ "category": "development",
+ "homepage": "https://github.com/anthropics/claude-plugins-official/tree/main/plugins/context-lens"
}
]
}
diff --git a/plugins/context-lens/.claude-plugin/plugin.json b/plugins/context-lens/.claude-plugin/plugin.json
new file mode 100644
index 00000000..47a0405c
--- /dev/null
+++ b/plugins/context-lens/.claude-plugin/plugin.json
@@ -0,0 +1,17 @@
+{
+ "name": "context-lens",
+ "version": "0.1.0",
+ "description": "Browse and search exactly what is in the context window, request by request",
+ "author": {
+ "name": "Anthropic",
+ "email": "support@anthropic.com"
+ },
+ "license": "Apache-2.0",
+ "keywords": [
+ "context",
+ "context-window",
+ "debugging",
+ "mod"
+ ],
+ "types": "./types/index.d.ts"
+}
diff --git a/plugins/context-lens/LICENSE b/plugins/context-lens/LICENSE
new file mode 100644
index 00000000..261eeb9e
--- /dev/null
+++ b/plugins/context-lens/LICENSE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
diff --git a/plugins/context-lens/README.md b/plugins/context-lens/README.md
new file mode 100644
index 00000000..037308c4
--- /dev/null
+++ b/plugins/context-lens/README.md
@@ -0,0 +1,49 @@
+# context-lens
+
+A Claude Code mod that shows exactly what is in the context window, request by request.
+
+Every time Claude Code is about to send a model request, context-lens snapshots what that request carries: the system prompt sections, the tools offered, and every message block (your prompts, the reminders Claude Code injects, replies, thinking, tool calls and tool results). You can browse it, search it, and step back through earlier requests.
+
+## Use
+
+- **Band above the prompt.** After each request it shows the window's fill and what the request added, for example `context 44.7k/200k · request 3 added 2 items (≈162 tok), last: result Grep parseDuration → 11 lines`. Focus the band with ctrl+x tab, then press `o` to open the lens or `h` to hide the band.
+- **`/context-lens`** opens the lens pane. `/context-lens <text>` opens it with a search already typed. `/context-lens export` writes the request on screen to `context-lens-request-<n>.md` in the project. `/context-lens band` brings a hidden band back.
+- **In the pane** (click, or focus it and use the keys):
+ - `p` / `n` / `l`: previous request, next request, latest. The pane follows the newest request until you step back.
+ - `1`–`7`: filter to All, System, Tools, Reminders, Chat, Tool I/O, or New (only what this request added compared with the one before).
+ - Search: Tab to the field and type. Matches are counted per item and highlighted.
+ - Click a row to open the reader. `b` back, `j` / `k` next or previous item, `u` / `d` page, `m` next match, `y` copy the text.
+
+## Install
+
+It needs Claude Code 2.1.287 or newer, the first release with mods (function hooks).
+
+```
+/plugin install context-lens@claude-plugins-official
+```
+
+In the terminal the pane docks beside the transcript in the fullscreen layout. It also works in the desktop app's Code tab.
+
+## Privacy
+
+context-lens only reads. It doesn't change the prompt, the tools or any tool call, and it sends nothing over the network.
+Snapshots live in the session's mod state on your machine.
+The one write is `/context-lens export`, which saves the request on screen as a Markdown file in the project, so check that file before you share it: it holds your conversation as sent to the model.
+
+## What the numbers mean
+
+- The bar and its categories are Claude Code's own estimate, the same one `/context` shows.
+- Item sizes are about four characters per token. They are for comparing items, not billing.
+- Tools show their descriptions. Input schemas are sent too, but mods can't read them, so `/context`'s System tools row is the full count.
+
+## Limits
+
+- It captures the main conversation. Subagents' requests are not shown.
+- Snapshots start when the mod loads. A resumed session's history shows up in the first new request.
+- It keeps the last 60 requests, and up to 400k characters of any one block.
+- Some sessions don't report their system prompt sections to mods (the desktop app's Code tab, in testing). The lens then shows one System prompt row with its size and says the text wasn't reported.
+
+## Develop
+
+Load a local copy with `claude --plugin-dir /path/to/context-lens`, or in the desktop app set `CLAUDE_CODE_PLUGIN_DIRS=/path/to/context-lens` in the environment it starts Claude Code with.
+Check it with `claude plugin validate .` and `claude plugin test .` (tests in `tests/`).
diff --git a/plugins/context-lens/hooks/hooks.json b/plugins/context-lens/hooks/hooks.json
new file mode 100644
index 00000000..cb11bb07
--- /dev/null
+++ b/plugins/context-lens/hooks/hooks.json
@@ -0,0 +1,5 @@
+{
+ "modules": [
+ "./register.tsx"
+ ]
+}
diff --git a/plugins/context-lens/hooks/register.tsx b/plugins/context-lens/hooks/register.tsx
new file mode 100644
index 00000000..f6fdc627
--- /dev/null
+++ b/plugins/context-lens/hooks/register.tsx
@@ -0,0 +1,789 @@
+import { atom, read, update } from 'claude-code'
+import type { Register, RenderChildren } from 'claude-code'
+
+import type { Category, Filter, Group, Item, Kind, Snap, View } from '../types'
+
+const PANE = 'context-lens'
+const MAX_SNAPS = 60
+const MAX_BLOB = 400_000
+const SNIPPET = 60
+
+const snaps = atom({ plugin: 'context-lens', key: 'snaps' } as const, [] as Snap[])
+const view = atom({ plugin: 'context-lens', key: 'view' } as const, {
+ at: null,
+ filter: 'all',
+ query: '',
+ open: null,
+ line: 0,
+ collapsed: ['system', 'tools'],
+} as View)
+const reminderTypes = atom({ plugin: 'context-lens', key: 'reminderTypes' } as const, {} as Record<string, string>)
+const bandHidden = atom({ plugin: 'context-lens', key: 'bandHidden' } as const, false)
+const BLOB = { plugin: 'context-lens', key: 'blob' } as const
+const ITEMS = { plugin: 'context-lens', key: 'items' } as const
+const MAX_REMINDER_TYPES = 500
+
+type Section = { id: string; text: string; scope: string }
+type ApiBlock = { type: string; [field: string]: unknown }
+type ApiMessage = { role: 'user' | 'assistant'; content: ApiBlock[] }
+type ToolRow = { name: string; description: string; mcp: boolean }
+
+// Blobs are content-addressed and never change, so a module cache is safe across draws.
+const blobCache = new Map<string, string>()
+const written = new Set<string>()
+const sectionsByModel = new Map<string, Section[]>()
+const deferred = new Map<string, boolean>()
+let lastSections: Section[] = []
+
+const FILTERS: { id: Filter; label: string; hotkey: string }[] = [
+ { id: 'all', label: 'All', hotkey: '1' },
+ { id: 'system', label: 'System', hotkey: '2' },
+ { id: 'tools', label: 'Tools', hotkey: '3' },
+ { id: 'reminders', label: 'Reminders', hotkey: '4' },
+ { id: 'chat', label: 'Chat', hotkey: '5' },
+ { id: 'io', label: 'Tool I/O', hotkey: '6' },
+ { id: 'new', label: 'New', hotkey: '7' },
+]
+
+const GROUP_TITLES: Record<Group, string> = {
+ system: 'System prompt',
+ tools: 'Tools',
+ messages: 'Messages',
+}
+
+const KIND_TAGS: Record<Kind, string> = {
+ section: 'section',
+ tool: 'tool',
+ prompt: 'prompt',
+ reminder: 'reminder',
+ assistant: 'reply',
+ thinking: 'thinking',
+ tool_use: 'tool call',
+ tool_result: 'result',
+ media: 'media',
+ other: 'block',
+}
+
+const CATEGORY_COLORS: Record<string, string> = {
+ 'System prompt': '#d97757',
+ 'System tools': '#9a9a9a',
+ 'MCP tools': '#56b6c2',
+ 'Custom agents': '#98c379',
+ 'Memory files': '#c678dd',
+ Skills: '#e5c07b',
+ Messages: '#61afef',
+}
+
+export function hashOf(text: string): string {
+ let h = 0x811c9dc5
+ for (let i = 0; i < text.length; i++) {
+ h ^= text.charCodeAt(i)
+ h = Math.imul(h, 0x01000193)
+ }
+ return 'b' + text.length.toString(36) + '_' + (h >>> 0).toString(36)
+}
+
+export function tokens(chars: number): number {
+ return Math.max(1, Math.round(chars / 4))
+}
+
+/** An item's size: Claude Code's own count where the text itself isn't available, else about four characters a token. */
+export function itemTokens(item: Item): number {
+ return item.estTokens ?? tokens(item.chars)
+}
+
+const SYSTEM_UNREPORTED =
+ "This session didn't hand its system prompt sections to mods, so the text can't be shown here. The size is Claude Code's own count, as /context's System prompt row reports it."
+
+export function fmtTokens(n: number): string {
+ if (n >= 100_000) return Math.round(n / 1000) + 'k'
+ if (n >= 1000) return (n / 1000).toFixed(1) + 'k'
+ return String(n)
+}
+
+function firstLine(text: string): string {
+ const line = text.split('\n').find(l => l.trim() !== '') ?? ''
+ return line.replace(/^#+\s*/, '').trim()
+}
+
+function stripReminder(text: string): string | null {
+ const m = /^\s*<system-reminder>\n?([\s\S]*?)\n?<\/system-reminder>\s*$/.exec(text)
+ return m ? (m[1] ?? '') : null
+}
+
+function describeInput(name: string, input: Record<string, unknown>): string {
+ const pick = ['file_path', 'command', 'pattern', 'url', 'query', 'description', 'prompt', 'path']
+ for (const key of pick) {
+ const v = input[key]
+ if (typeof v === 'string' && v !== '') {
+ const short = key === 'file_path' || key === 'path' ? v.split('/').slice(-2).join('/') : v
+ return name + ' ' + short.replace(/\s+/g, ' ')
+ }
+ }
+ return name
+}
+
+function resultText(content: unknown): string {
+ if (typeof content === 'string') return content
+ if (Array.isArray(content)) {
+ return content
+ .map(b => {
+ const block = b as ApiBlock
+ if (block.type === 'text') return String(block.text ?? '')
+ return '[' + block.type + ' block]'
+ })
+ .join('\n')
+ }
+ return content === undefined ? '' : JSON.stringify(content, null, 2)
+}
+
+export function buildItems(
+ sections: readonly Section[],
+ tools: readonly ToolRow[],
+ messages: readonly ApiMessage[],
+ isDeferred: (name: string) => boolean,
+ prevItems: readonly Item[] | undefined,
+ systemTokens = 0,
+): { items: Item[]; texts: Map<string, string> } {
+ const items: Item[] = []
+ const texts = new Map<string, string>()
+ const before = new Set(prevItems?.map(i => i.key) ?? [])
+ let block = 0
+ const add = (group: Group, kind: Kind, label: string, sub: string, text: string, extra: Partial<Item> = {}) => {
+ const hash = hashOf(text)
+ // Position plus content, so a repeated block (the same command run twice) still counts as new.
+ const key = group + ':' + (extra.msg ?? label) + ':' + block++ + ':' + hash
+ const kept = text.length > MAX_BLOB ? text.slice(0, MAX_BLOB) + '\n[context-lens kept the first 400k characters]' : text
+ texts.set(hash, kept)
+ items.push({
+ id: String(items.length),
+ group,
+ kind,
+ label: label.slice(0, 200) || '(empty)',
+ sub,
+ hash,
+ key,
+ chars: text.length,
+ isNew: prevItems === undefined ? true : !before.has(key),
+ ...extra,
+ })
+ }
+
+ for (const s of sections) add('system', 'section', s.id, s.scope, s.text)
+ if (sections.length === 0 && systemTokens > 0) {
+ add('system', 'section', 'system prompt (text not reported)', 'n/a', SYSTEM_UNREPORTED, { estTokens: systemTokens })
+ }
+ block = 0
+ for (const t of tools) {
+ add('tools', 'tool', t.name, isDeferred(t.name) ? 'deferred' : t.mcp ? 'mcp' : 'built-in', t.description)
+ }
+
+ const toolNames = new Map<string, string>()
+ messages.forEach((m, index) => {
+ const msg = index + 1
+ block = 0
+ for (const b of m.content) {
+ if (b.type === 'text') {
+ const text = String(b.text ?? '')
+ const inner = m.role === 'user' ? stripReminder(text) : null
+ if (inner !== null) add('messages', 'reminder', firstLine(inner), m.role, text, { msg, inner: hashOf(inner) })
+ else add('messages', m.role === 'user' ? 'prompt' : 'assistant', firstLine(text), m.role, text, { msg })
+ } else if (b.type === 'thinking') {
+ add('messages', 'thinking', firstLine(String(b.thinking ?? '')), m.role, String(b.thinking ?? ''), { msg })
+ } else if (b.type === 'redacted_thinking') {
+ add('messages', 'thinking', 'redacted thinking', m.role, '[redacted thinking block]', { msg })
+ } else if (b.type === 'tool_use') {
+ const name = String(b.name ?? 'tool')
+ const input = (b.input ?? {}) as Record<string, unknown>
+ toolNames.set(String(b.id ?? ''), describeInput(name, input))
+ add('messages', 'tool_use', describeInput(name, input), m.role, JSON.stringify(input, null, 2), { msg })
+ } else if (b.type === 'tool_result') {
+ const text = resultText(b.content)
+ const of = toolNames.get(String(b.tool_use_id ?? '')) ?? 'tool'
+ const lines = text.split('\n').length
+ const label = of + (b.is_error === true ? ' (error)' : ' → ' + lines + (lines === 1 ? ' line' : ' lines'))
+ add('messages', 'tool_result', label, m.role, text, { msg })
+ } else if (b.type === 'image' || b.type === 'document') {
+ add('messages', 'media', b.type, m.role, '[' + b.type + ' block]', { msg })
+ } else {
+ add('messages', 'other', b.type, m.role, JSON.stringify(b, null, 2), { msg })
+ }
+ }
+ })
+
+ return { items, texts }
+}
+
+async function record(
+ $: any,
+ turnId: string,
+ step: number,
+ model: string,
+ messages: readonly ApiMessage[],
+ tools: readonly ToolRow[],
+): Promise<void> {
+ const list = await read($, snaps)
+ const prev = list.at(-1)
+ const prevItems = prev === undefined ? undefined : ((await $.state.get({ ...ITEMS, id: prev.id })).value ?? [])
+ let categories: Category[] = []
+ let totalTokens = 0
+ let window = 0
+ try {
+ const usage = await $.session.usage({ breakdown: 'summary' })
+ window = usage.context.window
+ const bd = usage.context.breakdown
+ if (bd) {
+ categories = bd.categories.map((c: Category) => ({ name: c.name, tokens: c.tokens, kind: c.kind }))
+ totalTokens = bd.totalTokens
+ window = bd.rawMaxTokens || window
+ } else {
+ totalTokens = usage.context.tokens ?? 0
+ }
+ } catch {
+ totalTokens = 0
+ }
+
+ const sections = sectionsByModel.get(model) ?? lastSections
+ const systemTokens = categories.find(c => c.name === 'System prompt')?.tokens ?? 0
+ const { items, texts } = buildItems(sections, tools, messages, name => deferred.get(name) === true, prevItems, systemTokens)
+ for (const [hash, text] of texts) {
+ if (written.has(hash)) continue
+ await $.state.set({ ...BLOB, id: hash }, text)
+ written.add(hash)
+ blobCache.set(hash, text)
+ }
+ if (totalTokens === 0) totalTokens = items.reduce((n, i) => n + itemTokens(i), 0)
+
+ const fresh = items.filter(i => i.isNew)
+ const latest = fresh.filter(i => i.group === 'messages').at(-1)
+ const seq = (prev?.seq ?? 0) + 1
+ const snap: Snap = {
+ id: seq + ':' + turnId + ':' + step,
+ seq,
+ turnId,
+ turn: prev === undefined ? 1 : prev.turnId === turnId ? prev.turn : prev.turn + 1,
+ step,
+ model,
+ at: await $.clock.now(),
+ window,
+ categories,
+ totalTokens,
+ itemCount: items.length,
+ newCount: fresh.length,
+ newTokens: fresh.reduce((n, i) => n + itemTokens(i), 0),
+ latest: latest === undefined ? undefined : KIND_TAGS[latest.kind] + ' ' + latest.label,
+ }
+ await $.state.set({ ...ITEMS, id: snap.id }, items)
+ const kept = [...list, snap]
+ const keep = new Set(items.map(i => i.hash))
+ for (const old of kept.slice(0, Math.max(0, kept.length - MAX_SNAPS))) {
+ // Release what only the pruned request carried; the newest request's text stays.
+ for (const item of (await $.state.get({ ...ITEMS, id: old.id })).value ?? []) {
+ if (keep.has(item.hash) || !written.has(item.hash)) continue
+ await $.state.set({ ...BLOB, id: item.hash }, '')
+ written.delete(item.hash)
+ blobCache.delete(item.hash)
+ }
+ await $.state.set({ ...ITEMS, id: old.id }, [])
+ }
+ await update($, snaps, l => [...(l ?? []), snap].slice(-MAX_SNAPS))
+}
+
+async function blobText($: any, hash: string): Promise<string> {
+ const hit = blobCache.get(hash)
+ if (hit !== undefined) return hit
+ const { value } = await $.state.get({ ...BLOB, id: hash })
+ const text = value ?? ''
+ blobCache.set(hash, text)
+ return text
+}
+
+export function matchesFilter(item: Item, filter: Filter): boolean {
+ switch (filter) {
+ case 'all':
+ return true
+ case 'system':
+ return item.group === 'system'
+ case 'tools':
+ return item.group === 'tools'
+ case 'reminders':
+ return item.kind === 'reminder'
+ case 'chat':
+ return item.kind === 'prompt' || item.kind === 'assistant' || item.kind === 'thinking'
+ case 'io':
+ return item.kind === 'tool_use' || item.kind === 'tool_result'
+ case 'new':
+ return item.isNew
+ }
+}
+
+export function countHits(text: string, query: string): number {
+ if (query === '') return 0
+ const hay = text.toLowerCase()
+ const needle = query.toLowerCase()
+ let n = 0
+ for (let at = hay.indexOf(needle); at !== -1; at = hay.indexOf(needle, at + needle.length)) n++
+ return n
+}
+
+/** Hard-wraps text to the pane's width so one entry is one drawn row. */
+export function wrapLines(text: string, width: number): { n: number; text: string }[] {
+ const out: { n: number; text: string }[] = []
+ const w = Math.max(10, width)
+ text.split('\n').forEach((line, i) => {
+ const clean = line.replace(/\t/g, ' ')
+ if (clean.length <= w) out.push({ n: i + 1, text: clean })
+ else for (let at = 0; at < clean.length; at += w) out.push({ n: at === 0 ? i + 1 : 0, text: clean.slice(at, at + w) })
+ })
+ return out
+}
+
+/** Splits a line around case-insensitive hits of the query. */
+export function splitHits(line: string, query: string): { text: string; isHit: boolean }[] {
+ if (query === '') return [{ text: line, isHit: false }]
+ const parts: { text: string; isHit: boolean }[] = []
+ const hay = line.toLowerCase()
+ const needle = query.toLowerCase()
+ let from = 0
+ for (let at = hay.indexOf(needle); at !== -1; at = hay.indexOf(needle, from)) {
+ if (at > from) parts.push({ text: line.slice(from, at), isHit: false })
+ parts.push({ text: line.slice(at, at + needle.length), isHit: true })
+ from = at + needle.length
+ }
+ if (from < line.length) parts.push({ text: line.slice(from), isHit: false })
+ return parts
+}
+
+const TAG_COL = 10
+
+function clip(text: string, width: number): string {
+ return text.length > width ? text.slice(0, Math.max(0, width - 1)) + '…' : text
+}
+
+function snapAt(list: Snap[], v: View): { snap: Snap | undefined; index: number } {
+ if (list.length === 0) return { snap: undefined, index: -1 }
+ const pinned = v.at === null ? -1 : list.findIndex(s => s.id === v.at)
+ const index = pinned === -1 ? list.length - 1 : pinned
+ return { snap: list[index], index }
+}
+
+function exportMarkdown(snap: Snap, items: readonly Item[], texts: Map<string, string>): string {
+ const out: string[] = []
+ out.push('# Context window: request ' + snap.seq + ' (turn ' + snap.turn + ', step ' + (snap.step + 1) + ')', '')
+ out.push('Model: ' + snap.model + '. Captured by the context-lens mod as the request was about to be sent.', '')
+ let group: Group | null = null
+ for (const item of items) {
+ if (item.group !== group) {
+ group = item.group
+ out.push('## ' + GROUP_TITLES[group], '')
+ }
+ const where = item.msg !== undefined ? 'message ' + item.msg + ' (' + item.sub + ')' : item.sub
+ out.push('### ' + KIND_TAGS[item.kind] + ': ' + item.label, '', '_' + where + ', about ' + itemTokens(item) + ' tokens' + (item.isNew ? ', new in this request' : '') + '_', '')
+ out.push('~~~~', texts.get(item.hash) ?? '', '~~~~', '')
+ }
+ return out.join('\n')
+}
+
+export const register: Register = on => {
+ on('session.start', async ($, e, next) => {
+ await $.command.register({
+ name: 'context-lens',
+ description: 'Browse and search what is in the context window, request by request (args: a search, "export" or "band")',
+ })
+
+ return next(e)
+ })
+
+ on('command.run', { command: 'context-lens' }, async ($, e) => {
+ const args = e.args.trim()
+ if (args === 'export') {
+ const list = await read($, snaps)
+ const v = await read($, view)
+ const { snap } = snapAt(list, v)
+ if (snap === undefined) return { text: 'context-lens: no request captured yet. Send a prompt first.' }
+ const items = (await $.state.get({ ...ITEMS, id: snap.id })).value ?? []
+ const texts = new Map<string, string>()
+ for (const item of items) texts.set(item.hash, await blobText($, item.hash))
+ const path = (await $.session.cwd()) + '/context-lens-request-' + snap.seq + '.md'
+ await $.fs.write(path, exportMarkdown(snap, items, texts))
+
+ return { text: 'context-lens: wrote request ' + snap.seq + ' (' + items.length + ' items) to ' + path }
+ }
+
+ if (args === 'band') {
+ await update($, bandHidden, () => false)
+
+ return { text: 'context-lens: the band above the prompt is back.' }
+ }
+
+ await update($, view, v => ({ ...(v as View), query: args, open: null, line: 0 }))
+ await $.ui.open({ id: PANE, title: 'Context lens', focus: true })
+
+ return { text: args === '' ? 'Context lens opened.' : 'Context lens opened, searching for "' + args + '".' }
+ })
+
+ on('prompt.compose', async ($, e, next) => {
+ const result = await next(e)
+ // /context measures with an analysis render and a teammate composes its own; neither is this conversation's prompt
+ if (!e.traits.includes('analysis') && !e.traits.includes('teammate')) {
+ const sections = result.sections.map(s => ({ id: s.id, text: s.text, scope: s.scope }))
+ sectionsByModel.set(e.model, sections)
+ lastSections = sections
+ }
+
+ return result
+ })
+
+ on('tool.describe', async ($, e, next) => {
+ const result = await next(e)
+ deferred.set(e.tool, result.isDeferred === true || (result.isDeferred === undefined && e.isDeferred === true))
+
+ return result
+ })
+
+ on('prompt.attachment', async ($, e, next) => {
+ const result = await next(e)
+ const sent = result.text ?? null
+ if (typeof sent === 'string' && sent !== '') {
+ const key = hashOf(sent)
+ const known = await read($, reminderTypes)
+ if (known[key] !== e.type) {
+ await update($, reminderTypes, m => Object.fromEntries([...Object.entries(m ?? {}), [key, e.type]].slice(-MAX_REMINDER_TYPES)))
+ }
+ }
+
+ return result
+ })
+
+ on('turn.step', async function* ($, e, next) {
+ if (e.agentId !== undefined) return yield* next(e)
+
+ let pending: Promise<void> | null = null
+ try {
+ const [messages, tools] = await Promise.all([$.session.messages({ as: 'api' }), $.tool.list()])
+ pending = record($, e.turnId, e.index, e.model, messages as ApiMessage[], tools).catch(() => undefined)
+ } catch {
+ pending = null
+ }
+ try {
+ return yield* next(e)
+ } finally {
+ if (pending !== null) await pending
+ }
+ })
+
+ on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
+ if (e.props.hasSurvey) return next(e)
+ const list = await read($, snaps)
+ const snap = list.at(-1)
+ if (snap === undefined || (await read($, bandHidden))) return next(e)
+
+ const { Box, Text, Button } = $.ui.resolve(e)
+ const window = snap.window > 0 ? snap.window : snap.totalTokens
+ const added =
+ list.length === 1
+ ? 'first request: ' + snap.itemCount + ' items'
+ : 'request ' + snap.seq + ' added ' + snap.newCount + (snap.newCount === 1 ? ' item' : ' items') + ' (≈' + fmtTokens(snap.newTokens) + ' tok)' + (snap.latest ? ', last: ' + snap.latest : '')
+
+ return (
+ <Box flexDirection="row" gap={1} width={Math.max(40, e.props.bodyColumns - 4)}>
+ <Box flexShrink={0}>
+ <Text>
+ <Text color="#d97757">◎</Text> context {fmtTokens(snap.totalTokens)}/{fmtTokens(window)}
+ </Text>
+ </Box>
+ <Box flexGrow={1} flexShrink={1} minWidth={0}>
+ <Text dimColor wrap="truncate-end">
+ · {added}
+ </Text>
+ </Box>
+ <Box flexShrink={0} gap={1}>
+ <Button key="open" plain hotkey="o" label="open lens" onPress={() => void $.ui.open({ id: PANE, title: 'Context lens', focus: true })} />
+ <Button key="hide" plain hotkey="h" label="hide" dimColor onPress={() => update($, bandHidden, () => true)} />
+ </Box>
+ </Box>
+ )
+ })
+
+ on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
+ const table = $.ui.resolve(e)
+ const { Box, Text, Button } = table
+ // mobile draws no field yet; search is then typed with /context-lens <query>
+ const Input = 'Input' in table ? table.Input : undefined
+ const list = await read($, snaps)
+ const v = await read($, view)
+ const kindsOf = await read($, reminderTypes)
+ // two cells short of the body: the surface draws its close mark in the last column
+ const width = Math.max(40, (e.props.bodyColumns ?? 80) - 2)
+ // the rows the surface gives the pane body; the whole viewport only where a surface does not say
+ const rows = Math.max(12, e.props.scroll?.bodyRows ?? e.viewport?.rows ?? 40)
+ const { snap, index } = snapAt(list, v)
+ const setView = (fn: (old: View) => View) => update($, view, old => fn(old as View))
+
+ if (snap === undefined) {
+ return (
+ <Box flexDirection="column">
+ <Text bold>Context lens</Text>
+ <Text dimColor>No request captured yet. Each model request is snapshotted as it is sent;</Text>
+ <Text dimColor>send a prompt and this pane fills in.</Text>
+ </Box>
+ )
+ }
+
+ const items = (await $.state.get({ ...ITEMS, id: snap.id })).value ?? []
+ const labelOf = (item: Item) => (item.inner !== undefined && kindsOf[item.inner] ? kindsOf[item.inner] + ': ' : '') + item.label
+ const isLive = v.at === null || index === list.length - 1
+ const newCount = snap.newCount
+ const newTokens = snap.newTokens
+
+ const openHash = v.open === null ? undefined : items.find(i => i.id === v.open)?.hash
+ // Stepping between requests keeps the same text open when the other request carries it.
+ const goTo = async (target: number | null) => {
+ const to = target === null ? list[list.length - 1] : list[target]
+ const at = target === null || to === undefined ? null : to.id
+ const toItems = openHash === undefined || to === undefined ? [] : ((await $.state.get({ ...ITEMS, id: to.id })).value ?? [])
+ const same = toItems.find(i => i.hash === openHash)
+ await setView(o => ({ ...o, at, open: same?.id ?? null, line: same ? o.line : 0 }))
+ }
+ const nav = (
+ <Box flexDirection="row" gap={1}>
+ <Button key="prev" plain hotkey="p" label="◀ prev" dimColor={index === 0} onPress={() => goTo(Math.max(0, index - 1))} />
+ <Button key="next" plain hotkey="n" label="next ▶" dimColor={isLive} onPress={() => goTo(index + 1 >= list.length - 1 ? null : index + 1)} />
+ <Button key="live" plain hotkey="l" label="latest" dimColor={isLive} onPress={() => goTo(null)} />
+ </Box>
+ )
+
+ const header = (
+ <Box flexDirection="column">
+ <Box flexDirection="row" justifyContent="space-between" width={width}>
+ <Text bold>
+ Request {snap.seq}/{list[list.length - 1]?.seq ?? snap.seq} · turn {snap.turn} · step {snap.step + 1}
+ </Text>
+ <Text color={isLive ? '#98c379' : '#e5c07b'}>{isLive ? '● live' : '◌ ' + (list.length - 1 - index) + ' newer'}</Text>
+ </Box>
+ <Text dimColor>
+ {snap.model} · {snap.itemCount} items · {index === 0 ? 'first request captured' : '+' + newCount + ' new (≈' + fmtTokens(newTokens) + ' tok) since the last request'}
+ </Text>
+ </Box>
+ )
+
+ const used = snap.categories.filter(c => c.kind === 'used' && c.tokens > 0)
+ const barWidth = width - 2
+ const total = snap.window > 0 ? snap.window : Math.max(1, snap.totalTokens)
+ const segments = used.map(c => ({ c, cells: Math.max(1, Math.round((c.tokens / total) * barWidth)) }))
+ const usedCells = segments.reduce((n, s) => n + s.cells, 0)
+ const bar = (
+ <Box flexDirection="column">
+ <Box flexDirection="row">
+ {segments.map(s => (
+ <Text color={CATEGORY_COLORS[s.c.name] ?? '#abb2bf'}>{'█'.repeat(s.cells)}</Text>
+ ))}
+ <Text dimColor>{'░'.repeat(Math.max(0, barWidth - usedCells))}</Text>
+ </Box>
+ <Box flexDirection="row" flexWrap="wrap" columnGap={2}>
+ {used.map(c => (
+ <Text>
+ <Text color={CATEGORY_COLORS[c.name] ?? '#abb2bf'}>■</Text> {c.name} {fmtTokens(c.tokens)}
+ </Text>
+ ))}
+ <Text dimColor>
+ {fmtTokens(snap.totalTokens)} of {fmtTokens(total)} ({Math.round((snap.totalTokens / total) * 100)}%)
+ </Text>
+ </Box>
+ </Box>
+ )
+
+ const opened = v.open === null ? undefined : items.find(i => i.id === v.open)
+
+ if (opened !== undefined) {
+ const kept = await blobText($, opened.hash)
+ const text = kept === '' && opened.chars > 0 ? '[context-lens let this text go: it was only in requests older than the last ' + MAX_SNAPS + ']' : kept
+ // the terminal pages by drawn rows, so it wraps here; a desktop wraps proportional text itself
+ const lines = e.surface === 'terminal' ? wrapLines(text, width - 6) : text.split('\n').map((t, i) => ({ n: i + 1, text: t.replace(/\t/g, ' ') }))
+ const room = Math.max(6, rows - 9)
+ const hitRows = v.query === '' ? [] : lines.map((l, i) => (countHits(l.text, v.query) > 0 ? i : -1)).filter(i => i >= 0)
+ const top = Math.min(Math.max(0, v.line), Math.max(0, lines.length - room))
+ const shown = lines.slice(top, top + room)
+ const pos = items.indexOf(opened)
+ const atEnd = top + room >= lines.length
+ const nextHit = (atEnd ? undefined : hitRows.find(r => r > top + 2)) ?? hitRows[0]
+ const go = (item: Item | undefined) => item && setView(o => ({ ...o, open: item.id, line: 0 }))
+ const where = opened.msg !== undefined ? 'message ' + opened.msg + ' · ' + opened.sub : GROUP_TITLES[opened.group] + ' · ' + opened.sub
+
+ return (
+ <Box flexDirection="column">
+ {header}
+ {nav}
+ <Box flexDirection="row" gap={1} flexWrap="wrap">
+ <Button key="back" plain hotkey="b" label="back" onPress={() => setView(o => ({ ...o, open: null, line: 0 }))} />
+ <Button key="up" plain hotkey="k" label="◀ item" dimColor={pos === 0} onPress={() => go(items[pos - 1])} />
+ <Button key="down" plain hotkey="j" label="item ▶" dimColor={pos === items.length - 1} onPress={() => go(items[pos + 1])} />
+ <Button key="pgup" plain hotkey="u" label="page ▲" dimColor={top === 0} onPress={() => setView(o => ({ ...o, line: Math.max(0, top - room) }))} />
+ <Button key="pgdn" plain hotkey="d" label="page ▼" dimColor={top + room >= lines.length} onPress={() => setView(o => ({ ...o, line: top + room }))} />
+ {hitRows.length > 0 && (
+ <Button key="hit" plain hotkey="m" label={'next match (' + hitRows.length + ')'} onPress={() => setView(o => ({ ...o, line: Math.max(0, (nextHit ?? 0) - 2) }))} />
+ )}
+ <Button key="copy" plain hotkey="y" label="copy" onPress={() => void $.ui.copy({ text, surface: e.surface })} />
+ </Box>
+ <Text>
+ <Text bold color="#61afef">{KIND_TAGS[opened.kind]}</Text> <Text bold>{labelOf(opened)}</Text>
+ </Text>
+ <Text dimColor>
+ {where} · ≈{fmtTokens(itemTokens(opened))} tok · {opened.chars.toLocaleString('en-US')} chars{opened.isNew ? ' · new in this request' : ''} · lines {lines.length === 0 ? 0 : top + 1}–{Math.min(lines.length, top + room)} of {lines.length}
+ </Text>
+ {opened.kind === 'tool' && <Text dimColor>The tool's input schema is sent too; mods can't read it, so /context's System tools row is the full count.</Text>}
+ <Text dimColor>{'─'.repeat(width)}</Text>
+ {shown.map(l => (
+ <Box flexDirection="row">
+ <Box width={5} flexShrink={0} justifyContent="flex-end" paddingRight={1}>
+ <Text dimColor>{l.n === 0 ? '' : String(l.n)}</Text>
+ </Box>
+ <Box flexGrow={1} flexShrink={1} minWidth={0}>
+ <Text wrap="wrap">
+ {splitHits(l.text, v.query).map(p => (p.isHit ? <Text backgroundColor="#e5c07b" color="#1e1e1e">{p.text}</Text> : p.text))}
+ </Text>
+ </Box>
+ </Box>
+ ))}
+ </Box>
+ )
+ }
+
+ const query = v.query.trim()
+ const visible = items.filter(i => matchesFilter(i, v.filter))
+ const hits = new Map<string, { count: number; snippet: string }>()
+ if (query !== '') {
+ const texts = await Promise.all(visible.map(item => blobText($, item.hash)))
+ for (const [n, item] of visible.entries()) {
+ const text = texts[n] ?? ''
+ // a label is mostly the text's own first line; count it only when the text itself has no hit
+ const count = countHits(text, query) || countHits(labelOf(item), query)
+ if (count === 0) continue
+ const at = text.toLowerCase().indexOf(query.toLowerCase())
+ const start = Math.max(0, at - 20)
+ const snippet = at < 0 ? '' : (start > 0 ? '…' : '') + text.slice(start, start + SNIPPET).replace(/\s+/g, ' ')
+ hits.set(item.id, { count, snippet })
+ }
+ }
+ const shownItems = query === '' ? visible : visible.filter(i => hits.has(i.id))
+ const totalHits = [...hits.values()].reduce((n, h) => n + h.count, 0)
+
+ const filters = (
+ <Box flexDirection="row" flexWrap="wrap" columnGap={2}>
+ {FILTERS.map(f => (
+ <Button
+ key={'f-' + f.id}
+ plain
+ hotkey={f.hotkey}
+ label={f.label + (f.id === 'new' ? ' +' + newCount : '') + (v.filter === f.id ? ' ●' : '')}
+ dimColor={v.filter !== f.id}
+ onPress={() => setView(o => ({ ...o, filter: f.id }))}
+ />
+ ))}
+ </Box>
+ )
+
+ const search = (
+ <Box flexDirection="column">
+ {Input !== undefined && (
+ <Input
+ key="search"
+ label="Search"
+ placeholder="text anywhere in this request (Tab here, then type)"
+ value={v.query}
+ submitLabel="search"
+ onInput={value => void setView(o => ({ ...o, query: value }))}
+ onSubmit={value => void setView(o => ({ ...o, query: value }))}
+ />
+ )}
+ {query !== '' && (
+ <Text dimColor>
+ {totalHits} {totalHits === 1 ? 'match' : 'matches'} in {hits.size} of {visible.length} items
+ </Text>
+ )}
+ </Box>
+ )
+
+ const sizeCol = 7
+ const labelCol = Math.max(10, width - sizeCol - TAG_COL - 5)
+ const groups: Group[] = ['system', 'tools', 'messages']
+ const body: RenderChildren[] = []
+ for (const g of groups) {
+ const inGroup = shownItems.filter(i => i.group === g)
+ if (inGroup.length === 0) continue
+ const isCollapsed = query === '' && v.filter === 'all' && v.collapsed.includes(g)
+ const sum = inGroup.reduce((n, i) => n + itemTokens(i), 0)
+ const fresh = inGroup.filter(i => i.isNew).length
+ body.push(
+ <Button
+ key={'g-' + g}
+ plain
+ label={(isCollapsed ? '▸ ' : '▾ ') + GROUP_TITLES[g] + ' · ' + inGroup.length + ' · ≈' + fmtTokens(sum) + ' tok' + (g === 'tools' ? ' of descriptions' : '') + (fresh > 0 && index > 0 ? ' · +' + fresh + ' new' : '')}
+ onPress={() => setView(o => ({ ...o, collapsed: o.collapsed.includes(g) ? o.collapsed.filter(x => x !== g) : [...o.collapsed, g] }))}
+ />,
+ )
+ if (isCollapsed) continue
+ let lastMsg = -1
+ for (const item of inGroup) {
+ if (item.msg !== undefined && item.msg !== lastMsg) {
+ lastMsg = item.msg
+ body.push(
+ <Text dimColor>
+ {' #' + item.msg + ' ' + item.sub}
+ </Text>,
+ )
+ }
+ const mark = item.isNew && index > 0 ? '+' : ' '
+ const hit = hits.get(item.id)
+ const size = hit ? hit.count + (hit.count === 1 ? ' hit' : ' hits') : '≈' + fmtTokens(itemTokens(item))
+ // fixed-width columns in Boxes, not padded strings: a desktop draws proportional text and folds runs of spaces
+ body.push(
+ <Box flexDirection="row" width={width}>
+ <Box width={3} flexShrink={0}>
+ <Text color="#98c379">{mark}</Text>
+ </Box>
+ <Box width={TAG_COL} flexShrink={0}>
+ <Text dimColor>{KIND_TAGS[item.kind]}</Text>
+ </Box>
+ <Box flexGrow={1} flexShrink={1} minWidth={0} overflow="hidden">
+ <Button
+ key={'i-' + item.id}
+ plain
+ // only the terminal's cells are worth counting; a desktop's proportional text is cut by this Box
+ label={e.surface === 'terminal' ? clip(labelOf(item), labelCol) : clip(labelOf(item), 200)}
+ onPress={() => setView(o => ({ ...o, open: item.id, line: 0 }))}
+ />
+ </Box>
+ <Box width={sizeCol + 1} flexShrink={0} justifyContent="flex-end">
+ <Text dimColor={!hit} color={hit ? '#e5c07b' : undefined}>
+ {size}
+ </Text>
+ </Box>
+ </Box>,
+ )
+ if (hit && hit.snippet !== '') {
+ body.push(
+ <Box flexDirection="row" paddingLeft={3 + TAG_COL}>
+ <Text dimColor wrap="truncate-end">
+ {splitHits(hit.snippet, query).map(p => (p.isHit ? <Text backgroundColor="#e5c07b" color="#1e1e1e">{p.text}</Text> : p.text))}
+ </Text>
+ </Box>,
+ )
+ }
+ }
+ }
+ if (body.length === 0) body.push(<Text dimColor>Nothing here matches.</Text>)
+
+ return (
+ <Box flexDirection="column">
+ {header}
+ {nav}
+ {bar}
+ {search}
+ {filters}
+ <Text dimColor>{'─'.repeat(width)}</Text>
+ {body}
+ </Box>
+ )
+ })
+}
diff --git a/plugins/context-lens/tests/context-lens.test.tsx b/plugins/context-lens/tests/context-lens.test.tsx
new file mode 100644
index 00000000..85deeef6
--- /dev/null
+++ b/plugins/context-lens/tests/context-lens.test.tsx
@@ -0,0 +1,186 @@
+import { describe, expect, test } from 'claude-code/testing'
+
+import { buildItems, countHits, splitHits, wrapLines } from '../hooks/register'
+
+const SECTIONS = [
+ { id: 'intro', scope: 'shared', text: 'You are an interactive agent.' },
+ { id: 'memory', scope: 'session', text: 'Remember the project rules.' },
+]
+const TOOLS = [
+ { name: 'Read', description: 'Reads a file from disk.', mcp: false },
+ { name: 'Grep', description: 'Searches files.', mcp: false },
+]
+const TURN_1 = [
+ {
+ role: 'user',
+ content: [
+ { type: 'text', text: '<system-reminder>\n# Environment\nPrimary working directory: /home/jordan/trailmix\n</system-reminder>' },
+ { type: 'text', text: "Why does parseDuration('90m') return NaN?" },
+ ],
+ },
+]
+const TURN_2 = [
+ ...TURN_1,
+ {
+ role: 'assistant',
+ content: [
+ { type: 'text', text: 'Let me look at the parser.' },
+ { type: 'tool_use', id: 'toolu_1', name: 'Read', input: { file_path: '/home/jordan/trailmix/src/duration.ts' } },
+ ],
+ },
+ {
+ role: 'user',
+ content: [{ type: 'tool_result', tool_use_id: 'toolu_1', content: 'const UNITS = { s: 1000, h: 3600000 }\nreturn Number(amount) * UNITS[unit]' }],
+ },
+]
+
+describe('buildItems', () => {
+ test('labels every piece and marks what a request added', () => {
+ const first = buildItems(SECTIONS, TOOLS, TURN_1 as never, () => false, undefined)
+ expect(first.items.map(i => i.kind)).toEqual(['section', 'section', 'tool', 'tool', 'reminder', 'prompt'])
+ expect(first.items[4]?.label).toBe('Environment')
+
+ const second = buildItems(SECTIONS, TOOLS, TURN_2 as never, () => false, first.items)
+ const added = second.items.filter(i => i.isNew)
+ expect(added.map(i => i.kind)).toEqual(['assistant', 'tool_use', 'tool_result'])
+ expect(added[1]?.label).toBe('Read src/duration.ts')
+ expect(added[2]?.label).toBe('Read src/duration.ts → 2 lines')
+ expect(second.texts.get(added[2]?.hash ?? '')).toContain('UNITS[unit]')
+ })
+})
+
+describe('buildItems on repeats', () => {
+ test('a block identical to an earlier one is still new where it lands', () => {
+ const two = buildItems(SECTIONS, TOOLS, TURN_2 as never, () => false, undefined)
+ const again = [...TURN_2, TURN_2[1], TURN_2[2]]
+ const three = buildItems(SECTIONS, TOOLS, again as never, () => false, two.items)
+ expect(three.items.filter(i => i.isNew).map(i => i.kind)).toEqual(['assistant', 'tool_use', 'tool_result'])
+ })
+})
+
+describe('buildItems without sections', () => {
+ test('shows the system prompt by its size when its text was not reported', () => {
+ const none = buildItems([], TOOLS, TURN_1 as never, () => false, undefined, 3200)
+ const sys = none.items.filter(i => i.group === 'system')
+ expect(sys.length).toBe(1)
+ expect(sys[0]?.estTokens).toBe(3200)
+ })
+})
+
+describe('text helpers', () => {
+ test('counts and splits hits case-insensitively', () => {
+ expect(countHits('Units and UNITS', 'units')).toBe(2)
+ expect(splitHits('a UNITS b', 'units')).toEqual([
+ { text: 'a ', isHit: false },
+ { text: 'UNITS', isHit: true },
+ { text: ' b', isHit: false },
+ ])
+ })
+ test('wraps long lines and keeps line numbers on the first piece', () => {
+ const rows = wrapLines('x'.repeat(25) + '\nshort', 10)
+ expect(rows.map(r => r.n)).toEqual([1, 0, 0, 2])
+ })
+})
+
+function stubEngine(on: any, messages: { current: unknown[] }) {
+ on('prompt.compose', () => ({ sections: SECTIONS }))
+ on('tool.list', () => ({ value: TOOLS }))
+ on('session.messages', () => ({ value: messages.current }))
+ on('session.usage', () => ({
+ value: {
+ startedAt: 0,
+ context: {
+ window: 200000,
+ breakdown: {
+ categories: [
+ { name: 'System prompt', tokens: 6900, kind: 'used' },
+ { name: 'System tools', tokens: 12000, kind: 'used' },
+ { name: 'Messages', tokens: 900, kind: 'used' },
+ { name: 'Free space', tokens: 180200, kind: 'free' },
+ ],
+ totalTokens: 19800,
+ rawMaxTokens: 200000,
+ },
+ },
+ rateLimits: [],
+ cost: { usd: 0 },
+ },
+ }))
+ on('clock.now', () => ({ value: 1 }))
+ on('turn.step', async function* (_$: unknown, e: { turnId: string; index: number }) {
+ return { turnId: e.turnId, index: e.index, answer: '', toolUses: [], stopReason: 'end_turn', usage: null }
+ })
+}
+
+const PANE_PROPS = { title: 'Context lens', isFocused: true, bodyColumns: 90, placement: 'dock', scroll: { offset: 0, bodyRows: 40, rows: 40 }, view: {} }
+
+const BAND_PROPS = { hasSurvey: false, isWorking: false, maxRows: 6, bodyColumns: 100, scroll: { offset: 0, bodyRows: 6, rows: 6 }, view: {} }
+
+describe('band', () => {
+ for (const surface of ['terminal', 'desktop'] as const) {
+ test('sums up what the latest request added on ' + surface, async ($, on) => {
+ const messages = { current: TURN_1 as unknown[] }
+ stubEngine(on, messages)
+ on('ui.open', () => ({ value: { isPlaced: true } }))
+ on('ui.render', { component: 'AbovePrompt' }, ($: any, e: any) => {
+ const { Box } = $.ui.resolve(e)
+ return <Box />
+ })
+ const band = await $.ui.mount({ plugin: 'context-lens', surface, component: 'AbovePrompt', props: BAND_PROPS as never })
+ expect(await band.find({ key: 'open' })).toBeUndefined()
+
+ await $.prompt.compose({ model: 'm', promptModel: 'm', surfaces: [surface], tools: ['Read', 'Grep'], outputStyle: null, traits: [] })
+ for await (const _ of $.turn.step({ turnId: 't1', index: 0, model: 'm', messageCount: 1 })) void _
+ messages.current = TURN_2
+ for await (const _ of $.turn.step({ turnId: 't1', index: 1, model: 'm', messageCount: 3 })) void _
+
+ expect(await band.find({ type: 'Text', text: /request 2 added 3 items/ })).toBeDefined()
+ expect(await band.find({ type: 'Text', text: /last: result Read src\/duration.ts/ })).toBeDefined()
+ await band.press({ key: 'hide' })
+ expect(await band.find({ key: 'open' })).toBeUndefined()
+ await band.unmount()
+ })
+ }
+})
+
+describe('pane', () => {
+ for (const surface of ['terminal', 'desktop'] as const) {
+ test('browses, filters and searches a captured request on ' + surface, async ($, on) => {
+ const messages = { current: TURN_1 as unknown[] }
+ stubEngine(on, messages)
+
+ await $.prompt.compose({ model: 'm', promptModel: 'm', surfaces: [surface], tools: ['Read', 'Grep'], outputStyle: null, traits: [] })
+ for await (const _ of $.turn.step({ turnId: 't1', index: 0, model: 'm', messageCount: 1 })) void _
+ messages.current = TURN_2
+ await $.prompt.compose({ model: 'm', promptModel: 'm', surfaces: [surface], tools: ['Read', 'Grep'], outputStyle: null, traits: [] })
+ for await (const _ of $.turn.step({ turnId: 't1', index: 1, model: 'm', messageCount: 3 })) void _
+
+ const ui = await $.ui.mount({ plugin: 'context-lens', surface, component: 'Pane', requestId: 'context-lens', props: PANE_PROPS as never, viewport: { columns: 100, rows: 48 } as never })
+ expect(await ui.find({ type: 'Text', text: /Request 2\/2/ })).toBeDefined()
+ expect(await ui.find({ type: 'Button', text: /Read src\/duration.ts → 2 lines/ })).toBeDefined()
+
+ await ui.press({ key: 'f-new' })
+ const rows = async () => (await ui.findAll({ type: 'Button' })).filter(b => b.key?.startsWith('i-'))
+ expect((await rows()).map(b => b.text)).toEqual(['Let me look at the parser.', 'Read src/duration.ts', 'Read src/duration.ts → 2 lines'])
+
+ await ui.press({ key: 'f-all' })
+ await ui.input({ key: 'search', text: 'units' })
+ expect(await ui.find({ type: 'Text', text: /2 matches in 1 of/ })).toBeDefined()
+
+ const row = await ui.find({ type: 'Button', text: /Read src\/duration.ts → 2 lines/ })
+ await ui.press({ key: row?.key ?? '' })
+ expect(await ui.find({ type: 'Button', text: /next match/ })).toBeDefined()
+ expect(await ui.find({ type: 'Text', text: 'UNITS' })).toBeDefined()
+
+ await ui.press({ key: 'prev' })
+ expect(await ui.find({ type: 'Text', text: /Request 1\/2/ })).toBeDefined()
+ // request 1 has no tool result, so the reader falls back to the list
+ expect(await ui.find({ key: 'back' })).toBeUndefined()
+ await ui.input({ key: 'search', text: '' })
+ await ui.press({ key: 'f-new' })
+ // the first request is all new: two sections, two tools, the reminder and the prompt
+ expect((await ui.findAll({ type: 'Button' })).filter(b => b.key?.startsWith('i-')).length).toBe(6)
+ await ui.unmount()
+ })
+ }
+})
diff --git a/plugins/context-lens/types/index.d.ts b/plugins/context-lens/types/index.d.ts
new file mode 100644
index 00000000..7b060129
--- /dev/null
+++ b/plugins/context-lens/types/index.d.ts
@@ -0,0 +1,88 @@
+/** Where an item sits in the request: the system prompt, the tool list, or the messages. */
+export type Group = 'system' | 'tools' | 'messages'
+
+export type Kind =
+ | 'section'
+ | 'tool'
+ | 'prompt'
+ | 'reminder'
+ | 'assistant'
+ | 'thinking'
+ | 'tool_use'
+ | 'tool_result'
+ | 'media'
+ | 'other'
+
+/** One piece of one request: a system prompt section, a tool, or a message block. */
+export type Item = {
+ id: string
+ group: Group
+ kind: Kind
+ label: string
+ /** Secondary label: a section's cache scope, a tool's origin, the message's role. */
+ sub: string
+ /** Key of the item's text in the blob family. */
+ hash: string
+ /** Where the item sits plus its hash: the same key in the previous request means it is not new. */
+ key: string
+ /** Key of a reminder's inner text, to name it once its attachment type is known. */
+ inner?: string
+ chars: number
+ /** Claude Code's own token count, for an item whose text mods can't read. */
+ estTokens?: number
+ /** Not in the previous request: what this request added. */
+ isNew: boolean
+ /** 1-based message number, for message blocks. */
+ msg?: number
+}
+
+export type Category = { name: string; tokens: number; kind: string }
+
+/** One model request of the main conversation, as it was about to be sent; its items are kept apart, by id. */
+export type Snap = {
+ id: string
+ /** The request's number in this session, from 1. */
+ seq: number
+ turnId: string
+ turn: number
+ step: number
+ model: string
+ at: number
+ window: number
+ /** Tokens by category, as /context counts them (local estimate). */
+ categories: Category[]
+ totalTokens: number
+ itemCount: number
+ /** Items not in the previous request, and their size. */
+ newCount: number
+ newTokens: number
+ /** The newest message block the request added, as the band names it. */
+ latest?: string
+}
+
+export type Filter = 'all' | 'system' | 'tools' | 'reminders' | 'chat' | 'io' | 'new'
+
+export type View = {
+ /** The request shown, by snapshot id, or null to follow the newest. */
+ at: string | null
+ filter: Filter
+ query: string
+ /** The item open in the reader, by id, or null for the list. */
+ open: string | null
+ /** First wrapped line the reader shows. */
+ line: number
+ collapsed: Group[]
+}
+
+declare module 'claude-code' {
+ interface PluginState {
+ 'context-lens': {
+ snaps: Snap[]
+ view: View
+ reminderTypes: Record<string, string>
+ bandHidden: boolean
+ items: StateFamily<Item[]>
+ blob: StateFamily<string>
+ }
+ }
+}