spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

mods/diff: an asked diff is cut to the room the prompt's context has left, and dropped with a status when none is leftBrowse files
poteat authored on Sep 10, 2026 parent d920522 · commit 1c97c29b184b
6 changed files+79 -8compared with d920522
Amods/diff/hooks/ask/ask-cut-note.ts
Amods/diff/hooks/ask/fitted-ask-text-of.ts
Mmods/diff/hooks/ask/index.ts
Mmods/diff/hooks/limits/sizes/index.ts
Amods/diff/hooks/limits/sizes/prompt-context-max-chars.ts
Mmods/diff/hooks/register.ts
@@ -1,0 +1,5 @@
1+/**
2+ * The line an ask ends with when its diff was cut to fit the prompt.
3+ */
4+export const ASK_CUT_NOTE =
5+ '(The rest of this diff was cut: it did not fit in the prompt.)'
@@ -1,0 +1,33 @@
1+import { ASK_CUT_NOTE } from './ask-cut-note.js'
2+
3+/**
4+ * An armed ask's text as it fits in the room a prompt's context has left:
5+ * whole when it fits, else its first lines and ASK_CUT_NOTE.
6+ *
7+ * @param text the armed ask's text
8+ * @param room the characters the context has left
9+ * @returns the text to attach, or undefined when not even its first line fits
10+ */
11+export function fittedAskTextOf(text: string, room: number): string | undefined {
12+ if (text.length <= room) {
13+ return text
14+ }
15+
16+ const kept: string[] = []
17+ let used = ASK_CUT_NOTE.length
18+
19+ for (const line of text.split('\n')) {
20+ const cost = line.length + 1
21+
22+ if (used + cost > room) {
23+ break
24+ }
25+
26+ kept.push(line)
27+ used += cost
28+ }
29+
30+ const hasBody = kept.length > 1
31+
32+ return hasBody ? `${kept.join('\n')}\n${ASK_CUT_NOTE}` : undefined
33+}
@@ -1,4 +1,6 @@
11 export * from './armed-ask'
22 export * from './armed-ask-of.js'
3+export * from './ask-cut-note.js'
4+export * from './fitted-ask-text-of.js'
35
46 export * as default from '.'
@@ -6,5 +6,6 @@
66 export * from './max-listed-directories.js'
77 export * from './max-untracked-probes.js'
88 export * from './pane-top-pad-rows.js'
9+export * from './prompt-context-max-chars.js'
910
1011 export * as default from '.'
@@ -1,0 +1,5 @@
1+/**
2+ * The most characters a prompt's context carries, its entries together; the
3+ * engine refuses a context past it, so an ask is fitted to what is left.
4+ */
5+export const PROMPT_CONTEXT_MAX_CHARS = 32_000
@@ -40,6 +40,7 @@
4040 let hasShownOnce = false
4141 let wasDrawnSinceProbe = false
4242 let armed: Ask.ArmedAsk | null = null
43+ let carrying: Ask.ArmedAsk | null = null
4344 let isRefreshing = false
4445 let isRefreshQueued = false
4546 let generation = 0
@@ -592,21 +593,45 @@
592593 on('prompt.submit', async ($, e, next) => {
593594 const asked = armed
594595
595- if (!host || !asked) {
596+ if (!host || !asked || carrying === asked) {
596597 return next(e)
597598 }
598599
599- const result = await next({
600- ...e,
601- context: [...(e.context ?? []), asked.text],
602- })
600+ const context = e.context ?? []
601+ const used = context.reduce((sum, entry) => sum + entry.length, 0)
602+ const text = Ask.fittedAskTextOf(
603+ asked.text,
604+ Limits.PROMPT_CONTEXT_MAX_CHARS - used,
605+ )
603606
604- if (result.drop === undefined && armed === asked) {
607+ if (text === undefined) {
605608 disarm(host)
609+ host.status(
610+ `${Views.sanitizeName(asked.path)}'s diff did not fit in the prompt ` +
611+ `and was dropped`,
612+ )
606613 redraw(host)
607- Record.recorderOf(host).asked(asked.lines)
614+
615+ return next(e)
608616 }
609617
610- return result
618+ carrying = asked
619+
620+ try {
621+ const result = await next({ ...e, context: [...context, text] })
622+
623+ if (result.drop === undefined) {
624+ Record.recorderOf(host).asked(asked.lines)
625+
626+ if (armed === asked) {
627+ disarm(host)
628+ redraw(host)
629+ }
630+ }
631+
632+ return result
633+ } finally {
634+ carrying = null
635+ }
611636 })
612637 }