Skip to content

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.

TypeScript

mods/diff: an asked diff is cut to the room the prompt's context has left, and dropped with a status when none is left

poteat1c97c29b184b
diff --git a/mods/diff/hooks/ask/ask-cut-note.ts b/mods/diff/hooks/ask/ask-cut-note.ts
new file mode 100644
index 00000000..f4b1edbd
--- /dev/null
+++ b/mods/diff/hooks/ask/ask-cut-note.ts
@@ -0,0 +1,5 @@
+/**
+ * The line an ask ends with when its diff was cut to fit the prompt.
+ */
+export const ASK_CUT_NOTE =
+ '(The rest of this diff was cut: it did not fit in the prompt.)'
diff --git a/mods/diff/hooks/ask/fitted-ask-text-of.ts b/mods/diff/hooks/ask/fitted-ask-text-of.ts
new file mode 100644
index 00000000..caddbf48
--- /dev/null
+++ b/mods/diff/hooks/ask/fitted-ask-text-of.ts
@@ -0,0 +1,33 @@
+import { ASK_CUT_NOTE } from './ask-cut-note.js'
+
+/**
+ * An armed ask's text as it fits in the room a prompt's context has left:
+ * whole when it fits, else its first lines and ASK_CUT_NOTE.
+ *
+ * @param text the armed ask's text
+ * @param room the characters the context has left
+ * @returns the text to attach, or undefined when not even its first line fits
+ */
+export function fittedAskTextOf(text: string, room: number): string | undefined {
+ if (text.length <= room) {
+ return text
+ }
+
+ const kept: string[] = []
+ let used = ASK_CUT_NOTE.length
+
+ for (const line of text.split('\n')) {
+ const cost = line.length + 1
+
+ if (used + cost > room) {
+ break
+ }
+
+ kept.push(line)
+ used += cost
+ }
+
+ const hasBody = kept.length > 1
+
+ return hasBody ? `${kept.join('\n')}\n${ASK_CUT_NOTE}` : undefined
+}
diff --git a/mods/diff/hooks/ask/index.ts b/mods/diff/hooks/ask/index.ts
index f167fcca..efac7f9a 100644
--- a/mods/diff/hooks/ask/index.ts
+++ b/mods/diff/hooks/ask/index.ts
@@ -1,4 +1,6 @@
export * from './armed-ask'
export * from './armed-ask-of.js'
+export * from './ask-cut-note.js'
+export * from './fitted-ask-text-of.js'
export * as default from '.'
diff --git a/mods/diff/hooks/limits/sizes/index.ts b/mods/diff/hooks/limits/sizes/index.ts
index 3fe50cfb..c802a776 100644
--- a/mods/diff/hooks/limits/sizes/index.ts
+++ b/mods/diff/hooks/limits/sizes/index.ts
@@ -6,5 +6,6 @@ export * from './max-lines-per-file.js'
export * from './max-listed-directories.js'
export * from './max-untracked-probes.js'
export * from './pane-top-pad-rows.js'
+export * from './prompt-context-max-chars.js'
export * as default from '.'
diff --git a/mods/diff/hooks/limits/sizes/prompt-context-max-chars.ts b/mods/diff/hooks/limits/sizes/prompt-context-max-chars.ts
new file mode 100644
index 00000000..81520f10
--- /dev/null
+++ b/mods/diff/hooks/limits/sizes/prompt-context-max-chars.ts
@@ -0,0 +1,5 @@
+/**
+ * The most characters a prompt's context carries, its entries together; the
+ * engine refuses a context past it, so an ask is fitted to what is left.
+ */
+export const PROMPT_CONTEXT_MAX_CHARS = 32_000
diff --git a/mods/diff/hooks/register.ts b/mods/diff/hooks/register.ts
index 19258b53..be528660 100644
--- a/mods/diff/hooks/register.ts
+++ b/mods/diff/hooks/register.ts
@@ -40,6 +40,7 @@ export function register(on: On) {
let hasShownOnce = false
let wasDrawnSinceProbe = false
let armed: Ask.ArmedAsk | null = null
+ let carrying: Ask.ArmedAsk | null = null
let isRefreshing = false
let isRefreshQueued = false
let generation = 0
@@ -592,21 +593,45 @@ export function register(on: On) {
on('prompt.submit', async ($, e, next) => {
const asked = armed
- if (!host || !asked) {
+ if (!host || !asked || carrying === asked) {
return next(e)
}
- const result = await next({
- ...e,
- context: [...(e.context ?? []), asked.text],
- })
+ const context = e.context ?? []
+ const used = context.reduce((sum, entry) => sum + entry.length, 0)
+ const text = Ask.fittedAskTextOf(
+ asked.text,
+ Limits.PROMPT_CONTEXT_MAX_CHARS - used,
+ )
- if (result.drop === undefined && armed === asked) {
+ if (text === undefined) {
disarm(host)
+ host.status(
+ `${Views.sanitizeName(asked.path)}'s diff did not fit in the prompt ` +
+ `and was dropped`,
+ )
redraw(host)
- Record.recorderOf(host).asked(asked.lines)
+
+ return next(e)
}
- return result
+ carrying = asked
+
+ try {
+ const result = await next({ ...e, context: [...context, text] })
+
+ if (result.drop === undefined) {
+ Record.recorderOf(host).asked(asked.lines)
+
+ if (armed === asked) {
+ disarm(host)
+ redraw(host)
+ }
+ }
+
+ return result
+ } finally {
+ carrying = null
+ }
})
}