-
Notifications
You must be signed in to change notification settings - Fork 5
fix: cap compaction and completion max_tokens to provider limits #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
63ee849
fix: keep Dynamic Workflow progress moving instead of pinning at 90%
elkaix 41a660d
fix: cap compaction and completion max_tokens to provider limits
elkaix 2bb9e9d
feat: restart Homebrew-managed installs after update and refine updat…
elkaix 1ea5543
fix: address code review findings on update lifecycle and token caps
elkaix 74f9c09
test: bound the compaction remaining-window cap against the history e…
elkaix 47254a0
fix: mention legacy env alias in doctor and refine /update in-progres…
elkaix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@pythoughts/pythinker-code": patch | ||
| --- | ||
|
|
||
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@pythoughts/pythinker-code": patch | ||
| --- | ||
|
|
||
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@pythoughts/pythinker-code": minor | ||
| --- | ||
|
|
||
| Prepare verified Homebrew updates in the background and install them automatically on the next interactive launch. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import { gte, valid } from 'semver'; | ||
|
|
||
| import { getUpdateInstallLogFile } from '#/utils/paths'; | ||
|
|
||
| import { | ||
| activateHomebrewUpdate, | ||
| PreparedHomebrewUpdateInvalidError, | ||
| } from './homebrew'; | ||
| import { formatErrorMessage } from './format-error'; | ||
| import { tryAcquireUpdateInstallLock, type UpdateInstallLockHandle } from './install-lock'; | ||
| import { readUpdateInstallState, writeUpdateInstallState } from './install-state'; | ||
| import { detectInstallSource } from './source'; | ||
| import type { InstallSource, UpdateInstallState, UpdatePreparedHomebrew } from './types'; | ||
|
|
||
| const ACTIVATION_FAILURE_LIMIT = 2; | ||
|
|
||
| export interface ActivatePendingUpdateDeps { | ||
| readonly readState: () => Promise<UpdateInstallState>; | ||
| readonly writeState: (state: UpdateInstallState) => Promise<void>; | ||
| readonly acquireLock: ( | ||
| request: { readonly version: string }, | ||
| ) => Promise<UpdateInstallLockHandle | null>; | ||
| readonly activateHomebrew: ( | ||
| prepared: UpdatePreparedHomebrew, | ||
| ) => Promise<{ readonly version: string; readonly executable: string }>; | ||
| readonly detectSource: () => Promise<InstallSource>; | ||
| readonly now: () => Date; | ||
| readonly pid: number; | ||
| } | ||
|
|
||
| export interface ActivatePendingUpdateOptions { | ||
| readonly enabled: boolean; | ||
| readonly automaticEnabled: boolean; | ||
| readonly deps?: Partial<ActivatePendingUpdateDeps>; | ||
| } | ||
|
|
||
| function resolveDeps(overrides: Partial<ActivatePendingUpdateDeps> = {}): ActivatePendingUpdateDeps { | ||
| return { | ||
| readState: overrides.readState ?? (() => readUpdateInstallState()), | ||
| writeState: overrides.writeState ?? ((state) => writeUpdateInstallState(state)), | ||
| acquireLock: overrides.acquireLock ?? ((request) => tryAcquireUpdateInstallLock(request)), | ||
| activateHomebrew: | ||
| overrides.activateHomebrew ?? | ||
| ((prepared) => activateHomebrewUpdate(prepared, { logFile: getUpdateInstallLogFile() })), | ||
| detectSource: overrides.detectSource ?? (() => detectInstallSource()), | ||
| now: overrides.now ?? (() => new Date()), | ||
| pid: overrides.pid ?? process.pid, | ||
| }; | ||
| } | ||
|
|
||
| function activationAttempts(state: UpdateInstallState, version: string): number { | ||
| const failure = state.lastFailure; | ||
| return failure?.version === version && failure.operation === 'activate' ? failure.attempts : 0; | ||
| } | ||
|
|
||
| function isRunningPreparedVersion(currentVersion: string, preparedVersion: string): boolean { | ||
| return ( | ||
| valid(currentVersion) !== null && | ||
| valid(preparedVersion) !== null && | ||
| gte(currentVersion, preparedVersion) | ||
| ); | ||
| } | ||
|
|
||
| export async function activatePendingUpdate( | ||
| currentVersion: string, | ||
| options: ActivatePendingUpdateOptions, | ||
| ) { | ||
| if (!options.enabled) return { status: 'none' as const }; | ||
| const deps = resolveDeps(options.deps); | ||
| let state = await deps.readState(); | ||
| const pending = state.pending; | ||
| if (pending === null) return { status: 'none' as const }; | ||
| if (pending.requestedBy === 'automatic' && !options.automaticEnabled) { | ||
| return { status: 'none' as const }; | ||
| } | ||
|
|
||
| if (await deps.detectSource() !== pending.source) { | ||
| await deps.writeState({ ...state, active: null, pending: null }); | ||
| return { status: 'invalidated' as const, version: pending.version }; | ||
| } | ||
|
|
||
| if (isRunningPreparedVersion(currentVersion, pending.version)) { | ||
| const installedAt = deps.now().toISOString(); | ||
| await deps.writeState({ | ||
| active: null, | ||
| pending: null, | ||
| lastFailure: null, | ||
| lastSuccess: { | ||
| version: currentVersion, | ||
| installedAt, | ||
| notifiedAt: null, | ||
| }, | ||
| }); | ||
| return { status: 'finalized' as const, version: currentVersion }; | ||
| } | ||
|
|
||
| if (activationAttempts(state, pending.version) >= ACTIVATION_FAILURE_LIMIT) { | ||
| // Terminal: drop the pending record (keeping lastFailure for preflight) | ||
| // so later launches stop retrying and reporting an in-progress update. | ||
| await deps.writeState({ ...state, pending: null }); | ||
| return { | ||
| status: 'failed' as const, | ||
| version: pending.version, | ||
| message: `Automatic activation failed ${String(ACTIVATION_FAILURE_LIMIT)} times`, | ||
| }; | ||
| } | ||
|
|
||
| const lock = await deps.acquireLock({ version: pending.version }); | ||
| if (lock === null) return { status: 'in-progress' as const, version: pending.version }; | ||
|
|
||
| try { | ||
| state = await deps.readState(); | ||
| if (state.pending?.jobId !== pending.jobId) return { status: 'none' as const }; | ||
| const startedAt = deps.now().toISOString(); | ||
| const activatingState: UpdateInstallState = { | ||
| ...state, | ||
| active: { | ||
| version: pending.version, | ||
| source: pending.source, | ||
| operation: 'activate', | ||
| jobId: pending.jobId, | ||
| startedAt, | ||
| pid: deps.pid, | ||
| }, | ||
| }; | ||
| await deps.writeState(activatingState); | ||
|
|
||
| try { | ||
| const activated = await deps.activateHomebrew(pending); | ||
| await deps.writeState({ | ||
| ...activatingState, | ||
| active: null, | ||
| lastFailure: null, | ||
| }); | ||
| return { | ||
| status: 'activated' as const, | ||
| version: activated.version, | ||
| executable: activated.executable, | ||
| }; | ||
| } catch (error) { | ||
| const message = formatErrorMessage(error); | ||
| if (error instanceof PreparedHomebrewUpdateInvalidError) { | ||
| // Carry the cumulative prepare-failure count so repeated invalid | ||
| // artifacts can reach the auto-install failure threshold. | ||
| const priorFailure = activatingState.lastFailure; | ||
| const prepareAttempts = | ||
| priorFailure?.version === pending.version && priorFailure.operation === 'prepare' | ||
| ? priorFailure.attempts + 1 | ||
| : 1; | ||
| await deps.writeState({ | ||
| ...activatingState, | ||
| active: null, | ||
| pending: null, | ||
| lastFailure: { | ||
| version: pending.version, | ||
| failedAt: deps.now().toISOString(), | ||
| attempts: prepareAttempts, | ||
| operation: 'prepare', | ||
| message, | ||
| }, | ||
| }); | ||
| return { status: 'invalidated' as const, version: pending.version }; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| const attempts = activationAttempts(activatingState, pending.version) + 1; | ||
| await deps.writeState({ | ||
| ...activatingState, | ||
| active: null, | ||
| lastFailure: { | ||
| version: pending.version, | ||
| failedAt: deps.now().toISOString(), | ||
| attempts, | ||
| operation: 'activate', | ||
| message, | ||
| }, | ||
| }); | ||
| return { status: 'failed' as const, version: pending.version, message }; | ||
| } | ||
| } finally { | ||
| await lock.release().catch(() => {}); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| /** Shared failure-message formatter for update install/prepare/activate state. */ | ||
| export function formatErrorMessage(error: unknown): string { | ||
| return error instanceof Error ? error.message : String(error); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.