Skip to content

Commit 02f7f8d

Browse files
authored
fix: cap compaction and completion max_tokens to provider limits (#12)
## Related Issue No issue filed — problem explained directly. ## Problem Context compaction fails with a provider 400 whenever the model alias has no `maxOutputSize` configured: ``` Compaction cancelled Error: [compaction.failed] APIStatusError: 400 Error from provider (Console Go): Upstream request failed: [invalid_request_error] Invalid max_tokens value, the valid range of max_tokens is [1, 393216] ``` The compaction request resolved its completion budget without `maxOutputSize`, so the cap fell back to the model's full context window and was sent as `max_tokens`. Providers that enforce `input + max_tokens <= context window` (Moonshot/Console gateways, DeepSeek, Groq, and also Anthropic/OpenAI/Google under the same conditions) reject the request outright. The class of bug is provider-agnostic; compaction just hits it first because its prompt is the largest. ## What changed Port of the two upstream fixes for this (kimi-code `794db5553` and `76c643bcb`), adapted to pythinker: - Compaction now resolves its budget with `maxOutputSize ?? min(contextWindow, 128k)` instead of falling through to the context window (`packages/agent-core/src/agent/compaction/full.ts`). - `withMaxCompletionTokens` gained optional context: OpenAI-legacy and Pythinker chat-completions providers clamp the cap to the remaining context window (`maxContextTokens - usedContextTokens`), and OpenAI-legacy additionally to a 128k ceiling (`packages/kosong`). - Normal turns pass the live context token count through `KosongLLM` so per-request caps shrink as the context fills. Also includes a second commit: restart Homebrew-managed installs after `/update` (helper + activation step, doctor/preflight updates, docs). Both changes carry their own changeset; happy to split into a separate PR if preferred. ## Checklist - [x] I have read the [CONTRIBUTING](https://github.com/Pythoughts-labs/pythinker-code/blob/main/CONTRIBUTING.md) document. - [x] I have linked a related issue, or explained the problem above. - [x] I have added tests that prove my feature works. - [x] Ran `gen-changesets` skill, or this PR needs no changeset. - [x] Ran `gen-docs` skill, or this PR needs no doc update. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Automatic updates can download and verify in the background, then install on the next launch. * Added clearer update status, failure details, pending versions, and installer logs to diagnostics. * **Bug Fixes** * Improved context compaction to prevent excessive completion sizes and provider overflows. * Workflow progress now advances smoothly, displays a Finalizing state, and aligns correctly on narrow screens. * **Documentation** * Updated upgrade, configuration, environment-variable, and data-location guidance for the revised update behavior. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 3c9cffe commit 02f7f8d

44 files changed

Lines changed: 2645 additions & 109 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pythoughts/pythinker-code": patch
3+
---
4+
5+
Fix context compaction failing with provider "Invalid max_tokens" errors by capping requested completion tokens to the remaining context window and a safe output ceiling instead of the full context window size.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pythoughts/pythinker-code": patch
3+
---
4+
5+
Fix Dynamic Workflow progress sticking at 90% during long streaming, show a Finalizing state once all delegated agents finish, and fix member row alignment at narrow widths.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@pythoughts/pythinker-code": minor
3+
---
4+
5+
Prepare verified Homebrew updates in the background and install them automatically on the next interactive launch.

‎apps/pythinker-code/src/cli/sub/doctor.ts‎

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ import { z } from 'zod';
1515

1616
import { getTuiConfigPath, parseTuiConfig } from '#/tui/config';
1717
import { readUpdateCache } from '#/cli/update/cache';
18-
import { isAutoUpdateDisabledByEnv, shouldAutoInstallUpdates } from '#/cli/update/preflight';
18+
import { readUpdateInstallState } from '#/cli/update/install-state';
19+
import {
20+
automaticUpdateModeFor,
21+
isAutoUpdateDisabledByEnv,
22+
shouldAutoInstallUpdates,
23+
type AutomaticUpdateMode,
24+
} from '#/cli/update/preflight';
1925
import { detectInstallSource } from '#/cli/update/source';
26+
import type { UpdateInstallFailure } from '#/cli/update/types';
2027
import { getHostPackageRoot, getVersion } from '#/cli/version';
28+
import { getUpdateInstallLogFile } from '#/utils/paths';
2129

2230
interface WritableLike {
2331
write(chunk: string): boolean;
@@ -50,6 +58,12 @@ export interface DoctorRuntimeInfo {
5058
readonly latest: string | null;
5159
readonly checkedAt: string | null;
5260
readonly autoUpdate?: 'on' | 'off' | 'env-disabled';
61+
readonly mode?: AutomaticUpdateMode;
62+
readonly pendingVersion?: string;
63+
readonly pendingRequestedBy?: 'automatic' | 'manual';
64+
readonly activeOperation?: string;
65+
readonly lastFailure?: string;
66+
readonly logPath?: string;
5367
};
5468
}
5569

@@ -166,11 +180,12 @@ function resolveDeps(deps: Partial<DoctorDeps> | DoctorDeps | undefined): Resolv
166180
runtimeInfo:
167181
deps?.runtimeInfo ??
168182
(async () => {
169-
const [installSource, installations, ripgrep, update, autoInstall] = await Promise.all([
183+
const [installSource, installations, ripgrep, update, installState, autoInstall] = await Promise.all([
170184
detectInstallSource(),
171185
findPythinkerExecutables(),
172186
findExistingRg(resolvePythinkerHome()),
173187
readUpdateCache(),
188+
readUpdateInstallState(),
174189
shouldAutoInstallUpdates(),
175190
]);
176191
return {
@@ -184,12 +199,31 @@ function resolveDeps(deps: Partial<DoctorDeps> | DoctorDeps | undefined): Resolv
184199
latest: update.latest,
185200
checkedAt: update.checkedAt,
186201
autoUpdate: isAutoUpdateDisabledByEnv() ? 'env-disabled' : autoInstall ? 'on' : 'off',
202+
mode: automaticUpdateModeFor(installSource, process.platform),
203+
pendingVersion: installState.pending?.version,
204+
pendingRequestedBy: installState.pending?.requestedBy,
205+
activeOperation:
206+
installState.active === null
207+
? undefined
208+
: `${installState.active.operation ?? 'install'} ${installState.active.version}`,
209+
lastFailure:
210+
installState.lastFailure === null
211+
? undefined
212+
: formatUpdateFailure(installState.lastFailure),
213+
logPath: getUpdateInstallLogFile(),
187214
},
188215
};
189216
}),
190217
};
191218
}
192219

220+
function formatUpdateFailure(failure: UpdateInstallFailure): string {
221+
const summary = `${failure.operation ?? 'install'} ${failure.version} ` +
222+
`(attempt ${String(failure.attempts)})`;
223+
const message = failure.message?.replaceAll(/\s+/gu, ' ').trim();
224+
return message === undefined || message === '' ? summary : `${summary}: ${message}`;
225+
}
226+
193227
export async function findPythinkerExecutables(
194228
pathValue = process.env['PATH'],
195229
platform: NodeJS.Platform = process.platform,
@@ -368,25 +402,64 @@ function formatRuntimeInfo(info: DoctorRuntimeInfo | undefined): string[] {
368402
? []
369403
: [
370404
' Update channel: CDN staged rollout',
371-
...(info.update.autoUpdate === undefined
372-
? []
373-
: [
374-
info.update.autoUpdate === 'env-disabled'
375-
? ' Auto-update: disabled by PYTHINKER_CODE_NO_AUTO_UPDATE'
376-
: ` Auto-update: ${info.update.autoUpdate} (tui.toml [upgrade].auto_install)`,
377-
]),
405+
...formatAutomaticUpdate(info),
378406
...(info.update.latest === null
379407
? [' Latest cached version: unavailable']
380408
: [
381409
` Latest cached version: ${info.update.latest}${
382410
info.update.checkedAt === null ? '' : ` (checked ${info.update.checkedAt})`
383411
}`,
384412
]),
413+
...formatPreparedUpdate(info.update),
414+
...(info.update.activeOperation === undefined
415+
? []
416+
: [` Update operation: ${info.update.activeOperation}`]),
417+
...(info.update.lastFailure === undefined
418+
? []
419+
: [` Last update failure: ${info.update.lastFailure}`]),
420+
...(info.update.logPath === undefined ? [] : [` Update log: ${info.update.logPath}`]),
385421
]),
386422
'',
387423
];
388424
}
389425

426+
function formatPreparedUpdate(
427+
update: NonNullable<DoctorRuntimeInfo['update']>,
428+
): string[] {
429+
if (update.pendingVersion === undefined) return [];
430+
if (update.pendingRequestedBy === 'automatic' && update.autoUpdate !== 'on') {
431+
return [
432+
` Prepared update: ${update.pendingVersion} ` +
433+
'(automatic activation paused until auto-update is enabled)',
434+
];
435+
}
436+
return [` Prepared update: ${update.pendingVersion} (installs on next launch)`];
437+
}
438+
439+
function formatAutomaticUpdate(info: DoctorRuntimeInfo): string[] {
440+
const update = info.update;
441+
if (update?.autoUpdate === undefined) return [];
442+
if (update.autoUpdate === 'env-disabled') {
443+
return [
444+
' Auto-update: disabled by PYTHINKER_CODE_NO_AUTO_UPDATE or ' +
445+
'PYTHINKER_CLI_NO_AUTO_UPDATE',
446+
];
447+
}
448+
if (update.autoUpdate === 'off') {
449+
return [' Auto-update: off (tui.toml [upgrade].auto_install)'];
450+
}
451+
switch (update.mode) {
452+
case 'restart-install':
453+
return [' Auto-update: on (prepare in background; install on next launch)'];
454+
case 'background-install':
455+
return [' Auto-update: on (installs in background)'];
456+
case 'manual':
457+
return [` Auto-update: unavailable for ${info.installSource}`];
458+
case undefined:
459+
return [' Auto-update: on (tui.toml [upgrade].auto_install)'];
460+
}
461+
}
462+
390463
function formatResults(results: readonly CheckResult[]): string[] {
391464
const lines: string[] = [];
392465
for (const result of results) {
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import { gte, valid } from 'semver';
2+
3+
import { getUpdateInstallLogFile } from '#/utils/paths';
4+
5+
import {
6+
activateHomebrewUpdate,
7+
PreparedHomebrewUpdateInvalidError,
8+
} from './homebrew';
9+
import { formatErrorMessage } from './format-error';
10+
import { tryAcquireUpdateInstallLock, type UpdateInstallLockHandle } from './install-lock';
11+
import { readUpdateInstallState, writeUpdateInstallState } from './install-state';
12+
import { detectInstallSource } from './source';
13+
import type { InstallSource, UpdateInstallState, UpdatePreparedHomebrew } from './types';
14+
15+
const ACTIVATION_FAILURE_LIMIT = 2;
16+
17+
export interface ActivatePendingUpdateDeps {
18+
readonly readState: () => Promise<UpdateInstallState>;
19+
readonly writeState: (state: UpdateInstallState) => Promise<void>;
20+
readonly acquireLock: (
21+
request: { readonly version: string },
22+
) => Promise<UpdateInstallLockHandle | null>;
23+
readonly activateHomebrew: (
24+
prepared: UpdatePreparedHomebrew,
25+
) => Promise<{ readonly version: string; readonly executable: string }>;
26+
readonly detectSource: () => Promise<InstallSource>;
27+
readonly now: () => Date;
28+
readonly pid: number;
29+
}
30+
31+
export interface ActivatePendingUpdateOptions {
32+
readonly enabled: boolean;
33+
readonly automaticEnabled: boolean;
34+
readonly deps?: Partial<ActivatePendingUpdateDeps>;
35+
}
36+
37+
function resolveDeps(overrides: Partial<ActivatePendingUpdateDeps> = {}): ActivatePendingUpdateDeps {
38+
return {
39+
readState: overrides.readState ?? (() => readUpdateInstallState()),
40+
writeState: overrides.writeState ?? ((state) => writeUpdateInstallState(state)),
41+
acquireLock: overrides.acquireLock ?? ((request) => tryAcquireUpdateInstallLock(request)),
42+
activateHomebrew:
43+
overrides.activateHomebrew ??
44+
((prepared) => activateHomebrewUpdate(prepared, { logFile: getUpdateInstallLogFile() })),
45+
detectSource: overrides.detectSource ?? (() => detectInstallSource()),
46+
now: overrides.now ?? (() => new Date()),
47+
pid: overrides.pid ?? process.pid,
48+
};
49+
}
50+
51+
function activationAttempts(state: UpdateInstallState, version: string): number {
52+
const failure = state.lastFailure;
53+
return failure?.version === version && failure.operation === 'activate' ? failure.attempts : 0;
54+
}
55+
56+
function isRunningPreparedVersion(currentVersion: string, preparedVersion: string): boolean {
57+
return (
58+
valid(currentVersion) !== null &&
59+
valid(preparedVersion) !== null &&
60+
gte(currentVersion, preparedVersion)
61+
);
62+
}
63+
64+
export async function activatePendingUpdate(
65+
currentVersion: string,
66+
options: ActivatePendingUpdateOptions,
67+
) {
68+
if (!options.enabled) return { status: 'none' as const };
69+
const deps = resolveDeps(options.deps);
70+
let state = await deps.readState();
71+
const pending = state.pending;
72+
if (pending === null) return { status: 'none' as const };
73+
if (pending.requestedBy === 'automatic' && !options.automaticEnabled) {
74+
return { status: 'none' as const };
75+
}
76+
77+
if (await deps.detectSource() !== pending.source) {
78+
await deps.writeState({ ...state, active: null, pending: null });
79+
return { status: 'invalidated' as const, version: pending.version };
80+
}
81+
82+
if (isRunningPreparedVersion(currentVersion, pending.version)) {
83+
const installedAt = deps.now().toISOString();
84+
await deps.writeState({
85+
active: null,
86+
pending: null,
87+
lastFailure: null,
88+
lastSuccess: {
89+
version: currentVersion,
90+
installedAt,
91+
notifiedAt: null,
92+
},
93+
});
94+
return { status: 'finalized' as const, version: currentVersion };
95+
}
96+
97+
if (activationAttempts(state, pending.version) >= ACTIVATION_FAILURE_LIMIT) {
98+
// Terminal: drop the pending record (keeping lastFailure for preflight)
99+
// so later launches stop retrying and reporting an in-progress update.
100+
await deps.writeState({ ...state, pending: null });
101+
return {
102+
status: 'failed' as const,
103+
version: pending.version,
104+
message: `Automatic activation failed ${String(ACTIVATION_FAILURE_LIMIT)} times`,
105+
};
106+
}
107+
108+
const lock = await deps.acquireLock({ version: pending.version });
109+
if (lock === null) return { status: 'in-progress' as const, version: pending.version };
110+
111+
try {
112+
state = await deps.readState();
113+
if (state.pending?.jobId !== pending.jobId) return { status: 'none' as const };
114+
const startedAt = deps.now().toISOString();
115+
const activatingState: UpdateInstallState = {
116+
...state,
117+
active: {
118+
version: pending.version,
119+
source: pending.source,
120+
operation: 'activate',
121+
jobId: pending.jobId,
122+
startedAt,
123+
pid: deps.pid,
124+
},
125+
};
126+
await deps.writeState(activatingState);
127+
128+
try {
129+
const activated = await deps.activateHomebrew(pending);
130+
await deps.writeState({
131+
...activatingState,
132+
active: null,
133+
lastFailure: null,
134+
});
135+
return {
136+
status: 'activated' as const,
137+
version: activated.version,
138+
executable: activated.executable,
139+
};
140+
} catch (error) {
141+
const message = formatErrorMessage(error);
142+
if (error instanceof PreparedHomebrewUpdateInvalidError) {
143+
// Carry the cumulative prepare-failure count so repeated invalid
144+
// artifacts can reach the auto-install failure threshold.
145+
const priorFailure = activatingState.lastFailure;
146+
const prepareAttempts =
147+
priorFailure?.version === pending.version && priorFailure.operation === 'prepare'
148+
? priorFailure.attempts + 1
149+
: 1;
150+
await deps.writeState({
151+
...activatingState,
152+
active: null,
153+
pending: null,
154+
lastFailure: {
155+
version: pending.version,
156+
failedAt: deps.now().toISOString(),
157+
attempts: prepareAttempts,
158+
operation: 'prepare',
159+
message,
160+
},
161+
});
162+
return { status: 'invalidated' as const, version: pending.version };
163+
}
164+
const attempts = activationAttempts(activatingState, pending.version) + 1;
165+
await deps.writeState({
166+
...activatingState,
167+
active: null,
168+
lastFailure: {
169+
version: pending.version,
170+
failedAt: deps.now().toISOString(),
171+
attempts,
172+
operation: 'activate',
173+
message,
174+
},
175+
});
176+
return { status: 'failed' as const, version: pending.version, message };
177+
}
178+
} finally {
179+
await lock.release().catch(() => {});
180+
}
181+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** Shared failure-message formatter for update install/prepare/activate state. */
2+
export function formatErrorMessage(error: unknown): string {
3+
return error instanceof Error ? error.message : String(error);
4+
}

0 commit comments

Comments
 (0)