Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): cap the manuscript floor to the raw window so a tiny/heavy-overhead window can't overflow
[._.]/ Adam Eivyb82a49341692
diff --git a/server/lib/contextBudget.js b/server/lib/contextBudget.jsindex cffdc133b5..0e852044f9 100644--- a/server/lib/contextBudget.js+++ b/server/lib/contextBudget.js@@ -80,6 +80,14 @@ export function usableInputTokens({* philosophy — never starve the content block to empty — while respecting the* provider window: a small window trims the content to fit (the caller appends its* own `[… truncated …]` marker), a large window scales up exactly as before.+ *+ * The floor is itself capped by what the raw window can hold after fixed overhead+ * (the output reserve is generated by the model, not part of the input prompt, so+ * it doesn't bound what we send). On a window big enough to seat the floor this cap+ * is a no-op; on a genuinely tiny window — or one whose overhead alone is large —+ * the manuscript is trimmed *below* the floor (down to 0 only when overhead already+ * meets/exceeds the window) rather than overflowing the real context. So the floor+ * is reserved *within* the provider window, never beyond it.*/export function manuscriptContentBudgetChars({contextWindow,@@ -88,10 +96,18 @@ export function manuscriptContentBudgetChars({safetyMargin = DEFAULT_SAFETY_MARGIN,floorTokens = MANUSCRIPT_FLOOR_TOKENS,} = {}) {+ // Preferred size: the usable input budget (which holds back the output reserve),+ // floored at the manuscript minimum (the explicit 0 guards a negative floor), and+ // free to grow to fill a large window.const budgetTokens = usableInputTokens({ contextWindow, overheadTokens, outputReserveTokens, safetyMargin });- // budgetTokens is already ≥ 0 (usableInputTokens floors it); the explicit 0- // guards against a caller passing a negative floorTokens.- return Math.max(0, floorTokens, budgetTokens) * CHARS_PER_TOKEN;+ const desiredTokens = Math.max(0, floorTokens, budgetTokens);+ // Hard ceiling: the raw window after overhead, ignoring the (generated) output+ // reserve. The floor may exceed `budgetTokens`, but must never exceed this — else+ // the prompt itself overflows the provider's context on a tiny/heavy-overhead+ // window (#1488 codex review). usableInputTokens floors at 0, so this is 0 when+ // overhead ≥ window.+ const windowCapTokens = usableInputTokens({ contextWindow, overheadTokens, outputReserveTokens: 0, safetyMargin });+ return Math.min(desiredTokens, windowCapTokens) * CHARS_PER_TOKEN;}/**diff --git a/server/lib/contextBudget.test.js b/server/lib/contextBudget.test.jsindex c60d159671..b021cdb96d 100644--- a/server/lib/contextBudget.test.js+++ b/server/lib/contextBudget.test.js@@ -258,6 +258,31 @@ describe('contextBudget', () => {});expect(cap).toBe(500 * CHARS_PER_TOKEN);});+ it('caps the floor to the raw window when even the floor cannot fit (#1488 codex review)', () => {+ // 2000-token window, 1500 overhead, 0 reserve/margin -> only 500 tokens of raw+ // room after overhead, less than the 1500-token floor. The floor must NOT+ // overflow the window: trim to the 500 the window can actually hold.+ const cap = manuscriptContentBudgetChars({+ contextWindow: 2_000,+ overheadTokens: 1_500,+ outputReserveTokens: 0,+ safetyMargin: 0,+ floorTokens: 1_500,+ });+ expect(cap).toBe(500 * CHARS_PER_TOKEN);+ expect(cap).toBeLessThan(MANUSCRIPT_FLOOR_TOKENS * CHARS_PER_TOKEN);+ });+ it('yields zero when fixed overhead alone meets or exceeds the window', () => {+ // Overhead (1500) ≥ usable window (1000 * 0.9 = 900) -> nothing fits; send no+ // manuscript rather than overflow the context.+ const cap = manuscriptContentBudgetChars({+ contextWindow: 1_000,+ overheadTokens: 1_500,+ outputReserveTokens: 0,+ safetyMargin: 0.1,+ });+ expect(cap).toBe(0);+ });});describe('capContextOverhead', () => {