Repository navigation
Chats: context window meter and compaction, after T3 Code - #60
Conversation
Published from the private repository by oss-sync. - Chats: context window meter and compaction, after T3 Code 44 files changed, 2229 insertions(+), 78 deletions(-)
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bd8130da81
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| const ctx = latestContext(messages, input.provider) | ||
| if (!ctx || ctx.used < COMPACTION_MIN_TOKENS || !ctx.at) return null | ||
| return input.now - ctx.at >= (ctx.cacheTtlMs ?? CACHE_TTL_FALLBACK_MS) ? ctx.used : null |
There was a problem hiding this comment.
Honor the recorded cache expiry when offering compaction
When Claude reports a cache lifetime, cacheExpiresAt is calculated from the request start, but this check instead adds that lifetime to ctx.at, which is recorded when the usage event arrives. For a long-running response—especially one exceeding a 5-minute cache TTL—the cache can already be cold while the Compact chip remains unavailable for another full TTL. Compare now with cacheExpiresAt when present, retaining the 70-minute fallback only when expiry is unknown. The byte-identical mobile copy has the same issue.
AGENTS.md reference: AGENTS.md:L105-L105
Useful? React with 👍 / 👎.
| if (file && fromEnd) { | ||
| // From the start of the line being written, if the end falls mid-line. | ||
| const size = (await stat(file).catch(() => null))?.size || 0 | ||
| const end = size ? await readSlice(file, Math.max(0, size - 65536), Math.min(size, 65536)) : null | ||
| const cut = end ? end.lastIndexOf(10) : -1 | ||
| offset = cut >= 0 ? size - end.length + cut + 1 : size |
There was a problem hiding this comment.
Preserve current Codex records when attaching the tailer
On a resumed Codex chat, the watcher is created only after thread.started, then asynchronously finds the rollout and sets offset to its current end. If the CLI writes this turn’s token_count or compaction records before that lookup/stat completes—as can happen for fast turns—those new records are treated as old and permanently skipped, leaving the context meter or compaction divider stale. The initial boundary needs to be captured before the resumed turn can append, or records should be read and filtered by their existing startedAt timestamps.
AGENTS.md reference: AGENTS.md:L105-L105
Useful? React with 👍 / 👎.
| const t = await threads.get(threadId).catch(() => null) | ||
| if (!windowFor(t) || !(await enabled(t.provider))) return | ||
| const last = t.messages[t.messages.length - 1] | ||
| log(`idle compaction: compacting ${threadId} before its prompt cache expires`) | ||
| await startTurn({ threadId, text: '/compact', provider: t.provider, model: last.model, effort: last.effort, modelSettings: t.modelSettings }, { idle: 'beforeCacheExpires' }) |
There was a problem hiding this comment.
Reserve the chat before starting automatic compaction
If the owner sends a message just as this timer fires, both calls can pass startChatTurn’s initial chatRuns.active check before either registers a run. The thread-store mutations are serialized, so both turns are appended, but only the first subsequent chatRuns.start succeeds; the loser leaves an orphaned streaming assistant turn, and when the user request loses, the busy fallback also queues the same message again. Reserve the run before appending either turn, or make the automatic compaction start conditional under the same per-thread lock.
AGENTS.md reference: AGENTS.md:L105-L105
Useful? React with 👍 / 👎.
| // Settings → Chat › Auto-compact after, as T3 passes it (CLAUDE_CODE_AUTO_COMPACT_WINDOW). | ||
| const ownWindow = normalizeAutoCompactWindow(chatSettings.autoCompactWindow) | ||
| return spawnJsonStream(account.cli, args, { | ||
| input, label, signal, timeoutMs, holds: browser?.holds, env: { ...instanceEnvironment(account), ...browserCli.env }, | ||
| input, label, signal, timeoutMs, holds: browser?.holds, env: { ...instanceEnvironment(account), ...browserCli.env, ...(ownWindow ? { CLAUDE_CODE_AUTO_COMPACT_WINDOW: ownWindow } : {}) }, |
There was a problem hiding this comment.
Let the environment override the auto-compact setting
When both CLAUDE_CODE_AUTO_COMPACT_WINDOW and the dashboard’s Auto-compact after value are set, this final spread replaces the deployment environment with the stored dashboard value. The meter resolver likewise prioritizes setting before env, so the UI consistently reports the wrong effective threshold. Only inject the dashboard value when the environment has not supplied one, as required by the repository’s configuration precedence.
AGENTS.md reference: AGENTS.md:L138-L139
Useful? React with 👍 / 👎.
Published from the private repository by oss-sync.
44 files changed, 2229 insertions(+), 78 deletions(-)