Skip to content

feat(compaction): add modelFallbacks for compaction model resolution#52012

Open
scottgl9 wants to merge 1 commit intoopenclaw:mainfrom
scottgl9:feat/compaction-model-fallbacks
Open

feat(compaction): add modelFallbacks for compaction model resolution#52012
scottgl9 wants to merge 1 commit intoopenclaw:mainfrom
scottgl9:feat/compaction-model-fallbacks

Conversation

@scottgl9
Copy link
Copy Markdown
Contributor

Closes #52011

Summary

When the primary compaction model override is unreachable (e.g. a local vLLM server is down), compaction currently fails immediately. This adds a modelFallbacks array to AgentCompactionConfig that allows specifying fallback models tried in order before giving up.

Changes

  • src/config/types.agent-defaults.ts: Add modelFallbacks?: string[] to AgentCompactionConfig
  • src/agents/pi-embedded-runner/compact.ts: When primary resolveModelAsync fails, iterate through configured fallbacks. Auth profile is reset when fallback provider differs from primary.
  • docs/concepts/compaction.md: Document the new config option with examples

Config Example

{
  "agents": {
    "defaults": {
      "compaction": {
        "model": "vllm/qwen3-coder-next",
        "modelFallbacks": ["anthropic/claude-haiku-4-5"]
      }
    }
  }
}

Testing

  • When modelFallbacks is unset: no behavior change (backward compatible)
  • When primary model resolves: fallbacks are never tried
  • When primary model fails + fallbacks configured: tries each in order, uses first that resolves
  • When all fail: returns the original error (same as current behavior)

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation agents Agent runtime and tooling size: XS labels Mar 22, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Mar 22, 2026

Greptile Summary

This PR adds modelFallbacks?: string[] to AgentCompactionConfig and updates compactEmbeddedPiSessionDirect to iterate through the fallback list when the primary compaction model fails to resolve — a useful resilience improvement for setups that use local/self-hosted compaction models. The type definition and documentation are clean additions with no issues.

There is one correctness bug in the fallback auth-profile logic worth addressing before merge:

  • Auth profile not restored on same-provider fallback (compact.ts lines 483–486): The check if (fbProvider !== params.provider) { authProfileId = undefined; } only clears the profile — it never restores it. When the primary compaction override uses a different provider (clearing authProfileId to undefined at lines 424–427) and a fallback then resolves to the original params.provider, authProfileId stays undefined. This causes getApiKeyForModel to skip the user's configured auth profile and silently fall back to unscoped key resolution, which may fail or pick up the wrong credentials.
  • Error message omits fallback attempts (P2): When all models fail the error message only reports the primary model, making debugging harder.

Confidence Score: 3/5

  • Safe to merge after fixing the auth-profile restoration bug in the fallback path; all other changes are clean.
  • The feature is well-structured and backward compatible, but the auth-profile reset logic in the fallback loop has a concrete correctness gap: when a fallback provider matches the original params.provider, the previously-cleared authProfileId is never restored to params.authProfileId. This can cause silent credential-resolution failures in a realistic config scenario (primary override on a different provider + fallback on the original provider). One targeted fix resolves the issue.
  • src/agents/pi-embedded-runner/compact.ts lines 483–486 — the auth-profile restoration condition in the fallback loop.
Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-runner/compact.ts
Line: 483-486

Comment:
**Auth profile not restored when fallback matches original provider**

The condition only clears `authProfileId` when `fbProvider` differs from `params.provider`, but it never restores it when they match. This means if the primary compaction override uses a different provider (which already cleared `authProfileId` to `undefined` at lines 424–427), and a fallback then resolves to the original `params.provider`, `authProfileId` remains `undefined` even though `params.authProfileId` would be the correct credential to use for that provider.

This causes silent auth failures — `getApiKeyForModel` receives `profileId: undefined` and falls back to unscoped key resolution instead of the user's configured profile.

```suggestion
          if (fbProvider !== (params.provider ?? "").trim()) {
            authProfileId = undefined;
          } else {
            authProfileId = params.authProfileId;
          }
```

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/agents/pi-embedded-runner/compact.ts
Line: 495-497

Comment:
**Error message omits exhausted fallbacks**

When all fallbacks also fail, the error returned is either the primary model's `error` string or `Unknown model: {primaryProvider}/{primaryModelId}`. This gives the user no indication that fallback attempts were also made and failed, which can make debugging confusing.

Consider accumulating failure reasons and surfacing them in the final message, e.g.:

```ts
// After the fallback loop, if no model was resolved:
const triedModels = [`${provider}/${modelId}`, ...(fallbacks ?? [])].join(", ");
const reason = error ?? `All compaction models failed to resolve (tried: ${triedModels})`;
return fail(reason);
```

How can I resolve this? If you propose a fix, please make it concise.

Last reviewed commit: "feat(compaction): ad..."

Comment thread src/agents/pi-embedded-runner/compact.ts Outdated
Comment thread src/agents/pi-embedded-runner/compact.ts
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9a8f301e1b

ℹ️ 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".

Comment thread src/config/types.agent-defaults.ts Outdated
Comment thread src/agents/pi-embedded-runner/compact.ts Outdated
@scottgl9
Copy link
Copy Markdown
Contributor Author

Thanks for the review! Both issues addressed in 1d60b67:

  1. Auth profile restoration — Fixed. When a fallback provider matches the original params.provider, authProfileId is now restored to params.authProfileId instead of staying undefined. Added an else branch to the provider check.

  2. Error message includes fallback attempts — Fixed. When all models fail, the error now reports all tried models: All compaction models failed to resolve (tried: vllm/qwen3-coder-next, anthropic/claude-haiku-4-5).

Also fixed the TS type-check failure by avoiding mutable destructuring — now uses explicit final* variables instead of reassigning the result object.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 58b808df91

ℹ️ 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".

Comment thread src/agents/pi-embedded-runner/compact.ts Outdated
Comment thread src/agents/pi-embedded-runner/compact.ts Outdated
@openclaw-barnacle openclaw-barnacle Bot added the gateway Gateway runtime label Mar 22, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b598c1be54

ℹ️ 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".

Comment thread src/agents/pi-embedded-runner/compact.ts Outdated
@scottgl9 scottgl9 force-pushed the feat/compaction-model-fallbacks branch from fda22a0 to 5136655 Compare March 22, 2026 03:32
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 51366552dd

ℹ️ 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".

Comment thread src/agents/pi-embedded-runner/compact.ts
@scottgl9 scottgl9 force-pushed the feat/compaction-model-fallbacks branch from 5136655 to 3c98c59 Compare March 22, 2026 03:46
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3c98c59b89

ℹ️ 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".

Comment thread src/agents/pi-embedded-runner/compact.ts Outdated
@scottgl9 scottgl9 force-pushed the feat/compaction-model-fallbacks branch 2 times, most recently from 3a5c4c8 to a83b020 Compare March 24, 2026 15:42
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a83b0208cf

ℹ️ 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".

Comment thread src/agents/pi-embedded-runner/compact.ts
@scottgl9
Copy link
Copy Markdown
Contributor Author

Conflicts resolved (merged main → branch) and all remaining review items addressed:

  • P2 — Retry fallbacks on auth resolution failure (PRRT_kwDOQb6kR852d8Gt): Fixed in 1075941. If getApiKeyForModel or prepareProviderRuntimeAuth throws for the primary model and modelFallbacks is configured, the code now iterates through fallbacks attempting both model resolution and auth for each. First fully-authenticated fallback wins; if all fail, the error surfaces all tried models. Backward compatible.

Branch: scottgl9:feat/compaction-model-fallbacks — all 10 review threads now resolved.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1075941a0a

ℹ️ 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".

Comment thread src/agents/pi-embedded-runner/compact.ts
Comment thread src/agents/pi-embedded-runner/compact.ts
@scottgl9 scottgl9 force-pushed the feat/compaction-model-fallbacks branch from 1f0584c to d86d217 Compare March 28, 2026 05:06
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d86d21796c

ℹ️ 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".

Comment thread src/agents/pi-embedded-runner/compact.ts
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a9103aa59f

ℹ️ 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".

Comment thread extensions/synology-chat/src/webhook-handler.ts Outdated
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6ad4536006

ℹ️ 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".

Comment thread extensions/synology-chat/src/webhook-handler.test.ts Outdated
@scottgl9 scottgl9 force-pushed the feat/compaction-model-fallbacks branch from 6ad4536 to f27c7d6 Compare April 4, 2026 00:10
@openclaw-barnacle openclaw-barnacle Bot removed docs Improvements or additions to documentation gateway Gateway runtime labels Apr 4, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f27c7d613e

ℹ️ 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".

Comment thread src/agents/pi-embedded-runner/compact.ts
@scottgl9
Copy link
Copy Markdown
Contributor Author

Re P1 (PRRT_kwDOQb6kR854znUK) — Preserve runtime-auth exchange state after model auth prep

Fixed in ca7cbf7d9c. tryAuthForModel now returns hasExchange: boolean. Both the primary call site and the fallback loop update hasRuntimeAuthExchange from this value. Header injection at line 635 now correctly passes null (using stored exchanged token) when an exchange occurred.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ca7cbf7d9c

ℹ️ 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".

Comment thread src/agents/pi-embedded-runner/compact.ts Outdated
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 70c3c58141

ℹ️ 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".

Comment thread src/agents/pi-embedded-runner/compact.ts Outdated
Comment thread src/agents/pi-embedded-runner/compact.ts
@scottgl9 scottgl9 force-pushed the feat/compaction-model-fallbacks branch from 70c3c58 to a7fe08d Compare April 15, 2026 01:12
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a7fe08d71a

ℹ️ 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".

Comment thread src/agents/pi-embedded-runner/compact.ts Outdated
@scottgl9 scottgl9 force-pushed the feat/compaction-model-fallbacks branch from a7fe08d to a452f00 Compare April 15, 2026 01:44
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a452f00176

ℹ️ 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".

Comment thread src/config/zod-schema.agent-defaults.ts
Comment thread src/config/zod-schema.agent-defaults.ts
Comment thread src/config/zod-schema.agent-defaults.ts
@scottgl9 scottgl9 force-pushed the feat/compaction-model-fallbacks branch from a452f00 to c9793db Compare April 15, 2026 17:40
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c9793dba80

ℹ️ 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".

Comment on lines 880 to 884
const settingsManager = createPreparedEmbeddedPiSettingsManager({
cwd: effectiveWorkspace,
agentDir,
cfg: params.config,
contextTokenBudget: ctxInfo.tokens,
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Pass context token budget into prepared settings

This call no longer provides contextTokenBudget, so createPreparedEmbeddedPiSettingsManager cannot cap compaction reserve settings to the active model window. In src/agents/pi-settings.ts, the small-context safety cap only runs when contextTokenBudget is present; without it, the default 20k reserve floor can exceed 16k-class models and drive repeated overflow/compaction behavior instead of stable compaction.

Useful? React with 👍 / 👎.

Comment on lines 625 to 629
cfg: params.config,
provider,
modelId,
modelContextTokens: readPiModelContextTokens(runtimeModel),
modelContextWindow: runtimeModelWithContext.contextWindow,
modelContextWindow: runtimeModel.contextWindow,
defaultTokens: DEFAULT_CONTEXT_TOKENS,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Include modelContextTokens in ctx window resolution

This compaction path now computes ctxInfo from runtimeModel.contextWindow only, so models that publish limits via contextTokens are ignored here and can fall back to DEFAULT_CONTEXT_TOKENS. That can inflate the token budget used for compaction and lead to oversized prompts or avoidable overflow failures; the inconsistency is visible because the queued compaction path still reads contextTokens explicitly.

Useful? React with 👍 / 👎.

@scottgl9 scottgl9 force-pushed the feat/compaction-model-fallbacks branch from c9793db to e134d3e Compare April 16, 2026 02:17
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e134d3e06a

ℹ️ 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".

Comment on lines +1120 to +1124
!compactFallbacks ||
!Array.isArray(compactFallbacks) ||
compactFallbacks.length === 0
) {
throw compactErr;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Restore think-level retry before failing compaction

This branch now throws immediately when modelFallbacks is unset, but the previous flow retried once with pickFallbackThinkingLevel (typically off -> minimal) for reasoning-required providers. In environments that rely on that compatibility retry and do not configure modelFallbacks, compaction now fails outright on provider rejections that would previously recover on the second attempt.

Useful? React with 👍 / 👎.

Comment on lines +1214 to +1218
await runPostCompactionSideEffects({
config: params.config,
sessionKey: params.sessionKey,
sessionFile: params.sessionFile,
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Persist checkpoints after direct compaction succeeds

After a successful compact, this path now proceeds directly to side effects/metrics without persisting a compaction checkpoint snapshot. I checked src/agents/pi-embedded-runner/compact.queued.ts: checkpoint persistence there is gated behind contextEngine.info.ownsCompaction === true, while the default legacy engine delegates into compactEmbeddedPiSessionDirect, so standard compaction flows lose fresh checkpoints for sessions.compaction.list/restore.

Useful? React with 👍 / 👎.

Comment on lines 769 to 772
sessionId: params.sessionId,
agentId: sessionAgentId,
senderId: params.senderId,
senderIsOwner: params.senderIsOwner,
}),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Pass senderIsOwner into action discovery context

This discovery call no longer forwards senderIsOwner, so compaction prompt action discovery runs without ownership context even when the caller is an owner. That can suppress owner-scoped channel actions/capabilities during compaction and make tool guidance differ from normal run-time action discovery for the same session.

Useful? React with 👍 / 👎.

@scottgl9 scottgl9 force-pushed the feat/compaction-model-fallbacks branch from e134d3e to cef8926 Compare April 17, 2026 13:39
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cef8926457

ℹ️ 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".

Comment on lines +1221 to +1224
const tokensAfter = estimateTokensAfterCompaction({
messagesAfter: session.messages,
observedTokenCount,
fullSessionTokensBefore,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Compute post-compaction metrics from fallback session state

When session.compact(...) fails and fallback compaction succeeds, the code runs compaction on a separate fbSession (tryCompactWithModel) but then computes tokensAfter from session.messages here. Because the primary session never runs the successful fallback compact(), this can report pre-compaction token counts and propagate stale values to hooks/callers, which can in turn trigger incorrect follow-up compaction decisions.

Useful? React with 👍 / 👎.

Comment on lines +849 to 850
const systemPromptOverride = createSystemPromptOverride(appendPrompt);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor configured prompt overrides in compaction prompt build

This now hardwires the compaction prompt to buildEmbeddedSystemPrompt(...) and wraps it directly, but the prior flow applied resolveSystemPromptOverride(...) (and provider prompt transforms) before setting the session override. In setups that rely on agent-level systemPromptOverride or provider prompt hooks, compaction requests will diverge from normal runtime prompts and can regress provider-specific behavior.

Useful? React with 👍 / 👎.

Adds modelFallbacks?: string[] to AgentCompactionConfig. When the
primary compaction model fails (resolution, auth, or runtime), each
fallback is tried in order before giving up.

Changes:
- types.agent-defaults.ts: add modelFallbacks?: string[]
- zod-schema.agent-defaults.ts: add modelFallbacks schema
- compact.ts:
  - Model resolution fallback loop (primary model not in registry)
  - Auth fallback loop with tryAuthForModel returning hasExchange
  - Runtime fallback loop: on session.compact() throw, retry with full
    re-resolve + re-auth + createAgentSession per fallback
  - fbEffectiveModel now passes through applyLocalNoAuthHeaderOverride
    and applyAuthHeaderOverride using fbKeyInfo/fbHasExchange (P1 fix)
  - tryCompactWithModel disposes fbSession in finally block (P2 fix)
  - All error messages surface tried models list
  - Backward compatible when modelFallbacks is unset
@scottgl9 scottgl9 force-pushed the feat/compaction-model-fallbacks branch from cef8926 to 1c6b2f9 Compare April 19, 2026 22:05
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1c6b2f91d6

ℹ️ 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".

Comment on lines +958 to 960
if (providerStreamFn) {
session.agent.streamFn = providerStreamFn;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Wrap provider stream before assigning compaction session streamFn

This direct assignment bypasses the compaction stream preparation stack that previously went through resolveEmbeddedAgentStreamFn and extra-param/provider wrappers. In provider-owned transport paths, that wrapper layer is what injects resolved runtime credentials (including exchanged keys from prepareProviderRuntimeAuth) and applies transport/extra-param behavior; without it, compaction can still fail authentication or ignore provider/runtime stream settings even after model auth succeeded.

Useful? React with 👍 / 👎.

Comment on lines +852 to +854
heartbeatPrompt: isDefaultAgent
? resolveHeartbeatPrompt(params.config?.agents?.defaults?.heartbeat?.prompt)
: undefined,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Respect heartbeat policy when setting compaction heartbeat prompt

Using resolveHeartbeatPrompt(...) here unconditionally injects a heartbeat prompt for the default agent, even when heartbeat guidance should be suppressed by policy (for example includeSystemPromptSection: false or disabled cadence). This regresses prompt parity with the existing heartbeat-system-prompt policy path and can cause /compact prompts to include unintended HEARTBEAT instructions.

Useful? React with 👍 / 👎.

@clawsweeper
Copy link
Copy Markdown
Contributor

clawsweeper Bot commented Apr 28, 2026

Codex review: found issues before merge.

Summary
This PR adds an agents.defaults.compaction.modelFallbacks config/schema field and rewrites direct Pi compaction to retry configured fallback models during resolution, auth, and runtime failures.

Reproducibility: yes. A high-confidence static reproduction exists by comparing PR head 1c6b2f91d6089e2ba91cb7bc9fc2d7ac5ca5e34d with current main: the changed lines directly omit required auth scope, stream wrapping, settings caps, checkpoint persistence, and fallback-session metrics behavior.

Next step before merge
Maintainer judgment is needed on whether to close this as superseded by #74470, request a narrow rebase, or replace it with a current-main implementation; the branch is too broad and stale for a safe automated repair.

Security
Needs attention: The diff has no supply-chain changes, but it introduces a concrete credential-resolution regression by dropping workspace-scoped auth context for compaction models.

Review findings

  • [P1] Wrap provider streams before assigning compaction streams — src/agents/pi-embedded-runner/compact.ts:958-960
  • [P1] Persist checkpoints after successful compaction — src/agents/pi-embedded-runner/compact.ts:1238-1242
  • [P1] Pass the context window into prepared compaction settings — src/agents/pi-embedded-runner/compact.ts:906-910
Review details

Best possible solution:

If maintainers want explicit compaction fallback configuration, implement it as a narrow additive patch on current main that reuses the existing fallback/session-preparation pipeline and preserves the explicit-model semantics introduced by #74470.

Do we have a high-confidence way to reproduce the issue?

Yes. A high-confidence static reproduction exists by comparing PR head 1c6b2f91d6089e2ba91cb7bc9fc2d7ac5ca5e34d with current main: the changed lines directly omit required auth scope, stream wrapping, settings caps, checkpoint persistence, and fallback-session metrics behavior.

Is this the best way to solve the issue?

No. The feature direction is useful, but this PR is not the best implementation because it rewrites stale compaction internals; the safer solution is a small current-main change that layers explicit fallback config onto the existing pipeline.

Full review comments:

  • [P1] Wrap provider streams before assigning compaction streams — src/agents/pi-embedded-runner/compact.ts:958-960
    The PR assigns providerStreamFn directly to session.agent.streamFn. Current compaction goes through resolveEmbeddedAgentStreamFn, provider text transforms, and runtime extra-param wrappers; bypassing that stack can make provider-owned or OAuth-backed compaction calls fail even after auth prep succeeds.
    Confidence: 0.93
  • [P1] Persist checkpoints after successful compaction — src/agents/pi-embedded-runner/compact.ts:1238-1242
    The success path now runs post-compaction side effects and continues without calling persistSessionCompactionCheckpoint. The default legacy engine delegates into this direct path, so successful compactions would stop producing checkpoints for compaction list/restore workflows.
    Confidence: 0.91
  • [P1] Pass the context window into prepared compaction settings — src/agents/pi-embedded-runner/compact.ts:906-910
    createPreparedEmbeddedPiSettingsManager is now called without contextTokenBudget. The reserve-floor safety cap only runs when that value is present, so small-context compaction models can inherit an oversized reserve floor and repeatedly overflow instead of compacting.
    Confidence: 0.9
  • [P2] Use the compacted fallback session for metrics and hooks — src/agents/pi-embedded-runner/compact.ts:1244-1251
    When primary session.compact() fails and fallback fbSession.compact() succeeds, the code disposes the fallback session and then computes tokensAfter, messageCountAfter, diagnostics, and hooks from the original session.messages. That reports pre-compaction state and can trigger incorrect follow-up compaction decisions.
    Confidence: 0.89
  • [P2] Keep workspace auth scope in compaction auth resolution — src/agents/pi-embedded-runner/compact.ts:431-436
    The new auth helper omits workspaceDir when calling getApiKeyForModel, although current main passes the resolved workspace. Workspace-scoped provider auth evidence and env/secret resolution are therefore invisible for primary and fallback compaction models.
    Confidence: 0.86

Overall correctness: patch is incorrect
Overall confidence: 0.92

Security concerns:

  • [medium] Drops workspace-scoped auth context — src/agents/pi-embedded-runner/compact.ts:431
    The new tryAuthForModel helper calls getApiKeyForModel without workspaceDir, which can bypass workspace-specific credential evidence or environment resolution and fall back to less-specific credentials for compaction requests.
    Confidence: 0.82

Acceptance criteria:

  • pnpm test src/agents/pi-embedded-runner/compact.hooks.test.ts src/agents/pi-embedded-runner/compaction-runtime-context.test.ts src/agents/pi-project-settings.test.ts
  • pnpm check:changed in Testbox after code changes

What I checked:

Likely related people:

  • steipete: GitHub path history shows the most recent and largest share of current-main compact.ts maintenance, including recent compaction fixes and config/docs-adjacent work. (role: recent maintainer and heavy adjacent owner; confidence: high; commits: 825ad57513d9, 35335214b324, 714f3b59cce9; files: src/agents/pi-embedded-runner/compact.ts, src/config/types.agent-defaults.ts, docs/concepts/compaction.md)
  • jalehman: Merged fix: add compaction model fallback #74470, the current-main compaction fallback implementation that overlaps this PR's central behavior and docs. (role: recent feature implementer; confidence: high; commits: c09884614864; files: src/agents/pi-embedded-runner/compact.ts, src/agents/pi-embedded-runner/compact.hooks.test.ts, docs/concepts/compaction.md)
  • shakkernerd: Recent current-main commits threaded workspace auth evidence through model/runtime auth paths, directly relevant to the PR head's dropped workspaceDir regression. (role: auth-scope maintainer; confidence: medium; commits: c4e249114d96, 2fe3e779ffb1; files: src/agents/pi-embedded-runner/compact.ts)

Remaining risk / open question:

  • The PR branch is stale against fast-moving compaction code, so incremental fixes may miss additional conflicts with current-main behavior.
  • Maintainers still need to decide whether explicit agents.defaults.compaction.modelFallbacks is a desirable config surface now that implicit fallback shipped through fix: add compaction model fallback #74470.
  • The existing branch changes a large portion of compact.ts, increasing regression risk compared with a narrow current-main patch.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 2d849bbafa41.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: compaction model fallbacks when primary model is unreachable

1 participant