maka-agent 0.2.0-dev.23.20260906 → 0.2.0-dev.24.20260907

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/dist/acp/maka-acp-agent.js +2 -1
  2. package/dist/acp/session-configuration.js +143 -0
  3. package/dist/acp/session-registry.js +90 -4
  4. package/dist/cli-core.js +2 -0
  5. package/dist/pi-transcript.js +5 -12
  6. package/dist/pi-tui-runner.js +74 -4
  7. package/dist/runtime-host-cli-context.js +167 -78
  8. package/dist/runtime-host-cli-installation.js +8 -2
  9. package/dist/runtime-host-cli.js +31 -1
  10. package/dist/runtime-host-handoff-surface.js +81 -0
  11. package/dist/runtime-host-installed-update-bootstrap.js +13 -0
  12. package/dist/runtime-host-installed-update-coordinator.js +33 -0
  13. package/dist/runtime-host-lifecycle-transaction.js +9 -1
  14. package/dist/runtime-host-local-handoff.js +37 -4
  15. package/dist/runtime-host-local-managed-activation.js +84 -0
  16. package/dist/runtime-host-local-source-retirement.js +18 -2
  17. package/dist/runtime-host-managed-lifecycle-manager.js +1 -0
  18. package/dist/runtime-host-operator-retirement.js +50 -0
  19. package/dist/runtime-host-service-manager.js +3 -0
  20. package/dist/runtime-host-session-driver.js +4 -25
  21. package/dist/runtime-host-session-update.js +62 -0
  22. package/dist/runtime-host-tui-command.js +38 -73
  23. package/dist/runtime-host-tui-context.js +1 -0
  24. package/dist/runtime-host-tui-owner.js +226 -0
  25. package/dist/tui-copy-catalog.js +140 -3
  26. package/dist/tui-host-owner-copy.js +24 -0
  27. package/native/runtime-host-windows-task-launcher/prebuilds/win32-x64/maka-runtime-host-task-launcher.exe +0 -0
  28. package/node_modules/@maka/core/dist/canonical-runtime-event.js +3 -1
  29. package/node_modules/@maka/core/dist/model-failure.js +40 -0
  30. package/node_modules/@maka/core/dist/model-metadata.generated.js +4 -4
  31. package/node_modules/@maka/core/dist/model-metadata.js +3 -3
  32. package/node_modules/@maka/core/dist/model-web-search.js +5 -4
  33. package/node_modules/@maka/core/dist/provider-registry.js +45 -4
  34. package/node_modules/@maka/core/dist/record-schema.js +9 -2
  35. package/node_modules/@maka/core/dist/runtime-boundary.js +31 -11
  36. package/node_modules/@maka/core/dist/runtime-event.js +37 -6
  37. package/node_modules/@maka/core/dist/runtime-handoff.js +98 -0
  38. package/node_modules/@maka/core/dist/runtime-invocation.js +1 -1
  39. package/node_modules/@maka/core/dist/runtime-logical-execution.js +112 -0
  40. package/node_modules/@maka/core/dist/session.js +54 -52
  41. package/node_modules/@maka/core/dist/slash-command-catalog.js +2 -0
  42. package/node_modules/@maka/core/dist/tool-result-archive-evidence.js +20 -0
  43. package/node_modules/@maka/core/dist/usage-record-schema.js +26 -35
  44. package/node_modules/@maka/core/package.json +4 -0
  45. package/node_modules/@maka/mcp/dist/index.js +131 -1
  46. package/node_modules/@maka/runtime/dist/agent-run.js +272 -105
  47. package/node_modules/@maka/runtime/dist/ai-sdk-backend.js +13 -5
  48. package/node_modules/@maka/runtime/dist/ai-sdk-compaction.js +109 -34
  49. package/node_modules/@maka/runtime/dist/ai-sdk-turn.js +127 -133
  50. package/node_modules/@maka/runtime/dist/context-budget.js +14 -9
  51. package/node_modules/@maka/runtime/dist/conversation-copy.js +5 -4
  52. package/node_modules/@maka/runtime/dist/goal-continuation.js +36 -4
  53. package/node_modules/@maka/runtime/dist/history-compact-checkpoint.js +18 -9
  54. package/node_modules/@maka/runtime/dist/history-compact-summarizer.js +30 -22
  55. package/node_modules/@maka/runtime/dist/history-compaction.js +29 -4
  56. package/node_modules/@maka/runtime/dist/ledger-tool-result-archive-reader.js +106 -0
  57. package/node_modules/@maka/runtime/dist/message-authority.js +9 -0
  58. package/node_modules/@maka/runtime/dist/model-adapter.js +19 -98
  59. package/node_modules/@maka/runtime/dist/model-factory.js +19 -81
  60. package/node_modules/@maka/runtime/dist/model-fetcher.js +7 -0
  61. package/node_modules/@maka/runtime/dist/model-runtime.js +85 -111
  62. package/node_modules/@maka/runtime/dist/native-web-search-tool.js +2 -1
  63. package/node_modules/@maka/runtime/dist/openai-codex-history-compactor.js +2 -2
  64. package/node_modules/@maka/runtime/dist/opencode-session-header.js +4 -2
  65. package/node_modules/@maka/runtime/dist/provider-error-classification.js +84 -123
  66. package/node_modules/@maka/runtime/dist/provider-request-telemetry.js +68 -108
  67. package/node_modules/@maka/runtime/dist/run-handoff-gate.js +97 -0
  68. package/node_modules/@maka/runtime/dist/runtime-event-backfill.js +82 -54
  69. package/node_modules/@maka/runtime/dist/runtime-event-read-model.js +117 -159
  70. package/node_modules/@maka/runtime/dist/runtime-kernel.js +150 -112
  71. package/node_modules/@maka/runtime/dist/runtime-ledger-repair.js +160 -79
  72. package/node_modules/@maka/runtime/dist/runtime-read-model.js +30 -89
  73. package/node_modules/@maka/runtime/dist/runtime-resume.js +80 -4
  74. package/node_modules/@maka/runtime/dist/session-event-runtime-mapper.js +5 -0
  75. package/node_modules/@maka/runtime/dist/session-manager.js +171 -288
  76. package/node_modules/@maka/runtime/dist/session-projection-helpers.js +0 -27
  77. package/node_modules/@maka/runtime/dist/subscription-model-fetch.js +3 -4
  78. package/node_modules/@maka/runtime/dist/test-connection.js +16 -16
  79. package/node_modules/@maka/runtime/dist/tool-result-archive-encoding.js +38 -0
  80. package/node_modules/@maka/runtime/dist/tool-runtime.js +3 -61
  81. package/node_modules/@maka/runtime/package.json +1 -0
  82. package/node_modules/@maka/runtime-host/dist/adapter/session-projector.js +35 -0
  83. package/node_modules/@maka/runtime-host/dist/client/connect-or-spawn.js +9 -1
  84. package/node_modules/@maka/runtime-host/dist/client/connection.js +3 -0
  85. package/node_modules/@maka/runtime-host/dist/client/host-handoff-copy.js +156 -0
  86. package/node_modules/@maka/runtime-host/dist/client/host-handoff.js +273 -0
  87. package/node_modules/@maka/runtime-host/dist/client/host-retirement.js +26 -5
  88. package/node_modules/@maka/runtime-host/dist/client/index.js +2 -0
  89. package/node_modules/@maka/runtime-host/dist/client/launcher.js +3 -0
  90. package/node_modules/@maka/runtime-host/dist/client/remote-compatibility-error.js +2 -0
  91. package/node_modules/@maka/runtime-host/dist/client/session-subscription.js +27 -5
  92. package/node_modules/@maka/runtime-host/dist/operator/index.js +2 -1
  93. package/node_modules/@maka/runtime-host/dist/operator/retirement-cancellation.js +20 -0
  94. package/node_modules/@maka/runtime-host/dist/protocol/host-status.js +21 -12
  95. package/node_modules/@maka/runtime-host/dist/protocol/index.js +25 -1
  96. package/node_modules/@maka/runtime-host/dist/protocol/operations.js +1 -0
  97. package/node_modules/@maka/runtime-host/dist/protocol/session-continuity.js +40 -20
  98. package/node_modules/@maka/runtime-host/dist/protocol/session-transcript.js +0 -5
  99. package/node_modules/@maka/runtime-host/dist/protocol/session-turns.js +29 -58
  100. package/node_modules/@maka/runtime-host/dist/protocol/turn.js +11 -1
  101. package/node_modules/@maka/runtime-host/dist/server/access-credential-store.js +1 -0
  102. package/node_modules/@maka/runtime-host/dist/server/canonical-turn-snapshot.js +19 -4
  103. package/node_modules/@maka/runtime-host/dist/server/connection-session.js +7 -6
  104. package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +100 -24
  105. package/node_modules/@maka/runtime-host/dist/server/execution-model-authority.js +5 -5
  106. package/node_modules/@maka/runtime-host/dist/server/execution-model-composition.js +3 -2
  107. package/node_modules/@maka/runtime-host/dist/server/goal-coordinator.js +40 -1
  108. package/node_modules/@maka/runtime-host/dist/server/host-kernel.js +96 -17
  109. package/node_modules/@maka/runtime-host/dist/server/host-residency-registry.js +13 -7
  110. package/node_modules/@maka/runtime-host/dist/server/hosted-execution-admission-registry.js +3 -1
  111. package/node_modules/@maka/runtime-host/dist/server/hosted-execution-recovery.js +124 -62
  112. package/node_modules/@maka/runtime-host/dist/server/message-coordinator.js +76 -6
  113. package/node_modules/@maka/runtime-host/dist/server/root-turn-coordinator.js +325 -73
  114. package/node_modules/@maka/runtime-host/dist/server/scheduled-task-coordinator.js +37 -2
  115. package/node_modules/@maka/runtime-host/dist/server/serial-outbound-writer.js +45 -3
  116. package/node_modules/@maka/runtime-host/dist/server/session-catalog-coordinator.js +50 -7
  117. package/node_modules/@maka/runtime-host/dist/server/session-continuity-coordinator.js +87 -25
  118. package/node_modules/@maka/runtime-host/dist/server/session-revision-coordinator.js +8 -55
  119. package/node_modules/@maka/runtime-host/dist/server/session-sidecar-purge.js +1 -16
  120. package/node_modules/@maka/runtime-host/dist/server/session-transcript-pager.js +0 -1
  121. package/node_modules/@maka/runtime-host/dist/server/session-transcript-reader.js +460 -70
  122. package/node_modules/@maka/runtime-host/dist/server/shared-session-transcript.js +1 -1
  123. package/node_modules/@maka/runtime-host/dist/server/storage-maintenance.js +109 -0
  124. package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-coordinator.js +0 -1
  125. package/node_modules/@maka/storage/dist/artifact-store.js +21 -10
  126. package/node_modules/@maka/storage/dist/artifact-stores.js +1 -1
  127. package/node_modules/@maka/storage/dist/claude-code-session-adapter.js +0 -4
  128. package/node_modules/@maka/storage/dist/codex-session-adapter.js +0 -2
  129. package/node_modules/@maka/storage/dist/context-offload-snapshot.js +325 -0
  130. package/node_modules/@maka/storage/dist/execution-stores.js +9 -7
  131. package/node_modules/@maka/storage/dist/file-session-repository.js +1012 -0
  132. package/node_modules/@maka/storage/dist/opencode-session-adapter.js +0 -4
  133. package/node_modules/@maka/storage/dist/operational-state-backup.js +9 -5
  134. package/node_modules/@maka/storage/dist/runtime-event-persistence.js +1 -0
  135. package/node_modules/@maka/storage/dist/runtime-transcript-query.js +308 -0
  136. package/node_modules/@maka/storage/dist/session-bundle-policy.js +24 -4
  137. package/node_modules/@maka/storage/dist/session-message-projection.js +15 -0
  138. package/node_modules/@maka/storage/dist/session-repository.js +767 -0
  139. package/node_modules/@maka/storage/dist/session-store.js +25 -99
  140. package/node_modules/@maka/storage/dist/sqlite-artifact-metadata.js +10 -3
  141. package/node_modules/@maka/storage/dist/sqlite-context-offload-store.js +24 -8
  142. package/node_modules/@maka/storage/dist/sqlite-core-execution-schema.js +7 -1
  143. package/node_modules/@maka/storage/dist/sqlite-runtime-schema.js +44 -1
  144. package/node_modules/@maka/storage/dist/sqlite-runtime-store.js +129 -25
  145. package/node_modules/@maka/storage/dist/sqlite-session-metadata-store.js +109 -892
  146. package/node_modules/@maka/storage/dist/tool-result-archive-evidence.js +111 -0
  147. package/node_modules/@maka/storage/package.json +2 -0
  148. package/package.json +1 -1
@@ -17,9 +17,9 @@
17
17
  * under the License.
18
18
  */
19
19
  import { compactionDecisionDiagnosticPatch } from './compaction-boundary.js';
20
- import { buildContextBudgetDiagnosticShell, mergeContextBudgetDiagnostic, } from './context-budget.js';
20
+ import { buildContextBudgetDiagnosticShell, mergeContextBudgetDiagnostic, mergeContextBudgetDiagnosticPatches, } from './context-budget.js';
21
21
  import { isHistoryCompactContentEvent } from './history-compaction.js';
22
- import { canContinueHistoryCompactCheckpointForModel, canReplayHistoryCompactCheckpointForModel, matchHistoryCompactCheckpointPrefix, projectHistoryCompactCheckpointReplay, } from './history-compact-checkpoint.js';
22
+ import { canContinueHistoryCompactCheckpointForModel, canReplayHistoryCompactCheckpointForModel, historyCompactSourceDigest, matchHistoryCompactCheckpointPrefix, projectHistoryCompactCheckpointReplay, } from './history-compact-checkpoint.js';
23
23
  import { HistoryCompactSummarizerError, isMalformedHistoryCompactSummaryReason, } from './history-compact-error.js';
24
24
  import { createHash } from 'node:crypto';
25
25
  import { rewriteActiveToolResultsInMessages, } from './active-tool-result-prune.js';
@@ -186,21 +186,28 @@ export class AiSdkCompaction {
186
186
  : {}),
187
187
  ...(automaticMemoryBoundary ? { memoryExtractionBoundary: automaticMemoryBoundary } : {}),
188
188
  ...(previousCheckpoint ? { previousCheckpoint } : {}),
189
- summarize: async ({ coveredRuntimeEvents, newlyFoldedRuntimeEvents, previousCheckpoint }) => await this.summarizeWithFailureCircuit(summarizer, {
190
- sessionId: this.sessionId,
191
- turnId: input.turnId,
192
- runId: input.runId,
193
- source: {
194
- foldedRuntimeEvents: [...coveredRuntimeEvents],
195
- ...(input.runtimeContextInvocations
196
- ? { invocations: input.runtimeContextInvocations }
197
- : {}),
198
- },
199
- newlyFoldedRuntimeEvents: [...newlyFoldedRuntimeEvents],
200
- ...(previousCheckpoint ? { previousCheckpoint } : {}),
201
- abortSignal: historyCompactAbortController.signal,
202
- ...(tracker ? { providerRequestTracker: tracker } : {}),
203
- }),
189
+ // The planner projects the covered span to its effective view before
190
+ // summarizing and pins its digest as coverage.effectiveSourceDigest:
191
+ // the summary can never quote a body a durable transition removed, and
192
+ // a later transition invalidates the checkpoint at replay (#4845).
193
+ projectEffectiveCoverage: (covered) => this.foldEffectiveModelHistory(covered),
194
+ summarize: async ({ coveredRuntimeEvents, newlyFoldedRuntimeEvents, previousCheckpoint, }) => {
195
+ return await this.summarizeWithFailureCircuit(summarizer, {
196
+ sessionId: this.sessionId,
197
+ turnId: input.turnId,
198
+ runId: input.runId,
199
+ source: {
200
+ foldedRuntimeEvents: [...coveredRuntimeEvents],
201
+ ...(input.runtimeContextInvocations
202
+ ? { invocations: input.runtimeContextInvocations }
203
+ : {}),
204
+ },
205
+ newlyFoldedRuntimeEvents: [...newlyFoldedRuntimeEvents],
206
+ ...(previousCheckpoint ? { previousCheckpoint } : {}),
207
+ abortSignal: historyCompactAbortController.signal,
208
+ ...(tracker ? { providerRequestTracker: tracker } : {}),
209
+ });
210
+ },
204
211
  });
205
212
  if (historyCompactAbortController.signal.aborted) {
206
213
  return { outcome: { kind: 'failed', reason: 'aborted' } };
@@ -333,9 +340,35 @@ export class AiSdkCompaction {
333
340
  */
334
341
  async foldEffectiveModelHistory(events) {
335
342
  const loaded = await this.loadModelProjectionTransitions();
336
- if (loaded.transitions.length === 0)
343
+ if (loaded.transitions.length === 0 && loaded.unreadableTargets.size === 0) {
337
344
  return [...events];
338
- return reduceEffectiveModelProjections(events, loaded.transitions).events;
345
+ }
346
+ // Forward the unreadable set: a target whose transition record this build
347
+ // cannot decode must fold to the withholding sentinel here too, or this
348
+ // path becomes the one consumer that replays the body a record removed.
349
+ return reduceEffectiveModelProjections(events, loaded.transitions, loaded.unreadableTargets)
350
+ .events;
351
+ }
352
+ /**
353
+ * Whether the checkpoint's pinned effective view still is the covered
354
+ * prefix's effective view. The raw identity match happens later in the
355
+ * replay authority; this gate is about content currency: a projection
356
+ * transition committed after the fold changes what the model may see of the
357
+ * covered span without touching the raw ledger, and the checkpoint's summary
358
+ * or provider state must not survive that drift (#4845 review).
359
+ */
360
+ checkpointEffectiveCoverageMatches(checkpoint, effectiveEvents) {
361
+ const pinned = checkpoint.coverage.effectiveSourceDigest;
362
+ if (pinned === undefined)
363
+ return false;
364
+ const covered = effectiveEvents
365
+ .filter(isHistoryCompactContentEvent)
366
+ .slice(0, checkpoint.coverage.eventCount);
367
+ if (covered.length !== checkpoint.coverage.eventCount)
368
+ return false;
369
+ if (covered.at(-1)?.id !== checkpoint.coverage.through.runtimeEventId)
370
+ return false;
371
+ return historyCompactSourceDigest(covered) === pinned;
339
372
  }
340
373
  /**
341
374
  * Fold the durable transition ledger onto this session's prior history, and
@@ -422,10 +455,39 @@ export class AiSdkCompaction {
422
455
  }
423
456
  if (loadedCheckpoint &&
424
457
  canReplayHistoryCompactCheckpointForModel(loadedCheckpoint, this.input.connection, this.targetConnectionId, this.input.modelId)) {
425
- nextPolicy = {
426
- ...nextPolicy,
427
- historyCompact: { ...nextPolicy.historyCompact, checkpoint: loadedCheckpoint },
428
- };
458
+ // Raw identity first: a coverage miss keeps the apply-stage path, whose
459
+ // matcher reports the real identity reason (coverage_miss and friends).
460
+ // This gate is only for the case where the raw identity matches but a
461
+ // projection transition committed after the fold changed the effective
462
+ // view the summary (or provider state) was built from (#4845 review).
463
+ const rawIdentityMatch = matchHistoryCompactCheckpointPrefix(loadedCheckpoint, runtimeContext.filter(isHistoryCompactContentEvent));
464
+ if (rawIdentityMatch.reason) {
465
+ nextPolicy = {
466
+ ...nextPolicy,
467
+ historyCompact: { ...nextPolicy.historyCompact, checkpoint: loadedCheckpoint },
468
+ };
469
+ }
470
+ else if (this.checkpointEffectiveCoverageMatches(loadedCheckpoint, effective.events)) {
471
+ nextPolicy = {
472
+ ...nextPolicy,
473
+ historyCompact: { ...nextPolicy.historyCompact, checkpoint: loadedCheckpoint },
474
+ };
475
+ }
476
+ else {
477
+ // Rejecting keeps the stale block from restoring what the transition
478
+ // removed; the next fold re-summarizes (#4845 review).
479
+ diagnosticPatch = mergeContextBudgetDiagnosticPatches(diagnosticPatch, compactionDecisionDiagnosticPatch({
480
+ stage: 'priorReplay',
481
+ sourceKind: 'runtimeEvents',
482
+ decision: 'failedOpen',
483
+ phase: 'pre_turn',
484
+ boundaryKind: 'historyCompact',
485
+ ...(loadedCheckpoint.coverage.effectiveSourceDigest !== undefined
486
+ ? { boundaryIds: [loadedCheckpoint.checkpointId] }
487
+ : {}),
488
+ failOpenReason: 'effective_history_changed',
489
+ }));
490
+ }
429
491
  }
430
492
  return {
431
493
  policy: nextPolicy,
@@ -543,12 +605,15 @@ export class AiSdkCompaction {
543
605
  headAnchor.sessionId !== this.sessionId ||
544
606
  headAnchor.turnId !== input.turnId ||
545
607
  headAnchor.role !== 'user' ||
546
- headAnchor.author !== 'user' ||
608
+ (headAnchor.author !== 'user' && headAnchor.author !== 'host') ||
547
609
  !isHistoryCompactContentEvent(headAnchor)) {
548
610
  return undefined;
549
611
  }
612
+ // Handoff replay is an authenticated predecessor prefix, including the
613
+ // current logical Turn. The live reader remains physical-run scoped.
614
+ const handoff = input.continuation?.sourceTurnId === input.turnId;
550
615
  const priorContentEvents = (input.runtimeContext ?? [])
551
- .filter((event) => event.turnId !== input.turnId)
616
+ .filter((event) => handoff || event.turnId !== input.turnId)
552
617
  .filter(isHistoryCompactContentEvent);
553
618
  const state = new MidTurnCapacityCompactState(headAnchor, priorContentEvents, input.runtimeContextInvocations ?? [], resolveDeclaredContextWindow(this.input.connection, this.input.modelId));
554
619
  // Seed the turn's FIRST request with the last request the provider
@@ -605,9 +670,8 @@ export class AiSdkCompaction {
605
670
  state.replyReserveTokens = replyReserveTokens(lastUsage?.outputTokens);
606
671
  }
607
672
  }
608
- // The turn's first request folds as a pre_turn boundary, like the
609
- // reactive step-0 recovery; later steps fold mid_turn.
610
- const phase = options.stepNumber === 0 ? 'pre_turn' : 'mid_turn';
673
+ // Physical step zero may already be mid-Turn after a handoff.
674
+ const phase = state.compactionPhase(options.stepNumber);
611
675
  // A skipped trigger is never silent: every failure-driven skip records a
612
676
  // failedOpen decision.
613
677
  const failOpen = (failOpenReason) => {
@@ -767,15 +831,15 @@ export class AiSdkCompaction {
767
831
  const currentTurnEvents = turnLedger
768
832
  .filter((event) => event.turnId === turnId)
769
833
  .filter(isHistoryCompactContentEvent);
770
- // The head anchor is persisted before backend.send() is invoked, so
771
- // its absence is a wiring error, not replication lag — fail open now.
772
- if (!currentTurnEvents.some((event) => event.id === state.headAnchor.id)) {
834
+ const orderedEvents = [...state.priorContentEvents, ...currentTurnEvents];
835
+ // A fresh anchor belongs to the live ledger; after handoff it belongs to
836
+ // the authenticated predecessor prefix. Neither path manufactures it.
837
+ if (!orderedEvents.some((event) => event.id === state.headAnchor.id)) {
773
838
  return {
774
839
  decision: 'fail',
775
840
  diagnosticReason: 'head_anchor_not_durable',
776
841
  };
777
842
  }
778
- const orderedEvents = [...state.priorContentEvents, ...currentTurnEvents];
779
843
  const memoryDecision = input.memoryCompactionDecision?.();
780
844
  const plan = await planHistoryCompaction({
781
845
  sessionId: this.sessionId,
@@ -804,7 +868,12 @@ export class AiSdkCompaction {
804
868
  },
805
869
  }
806
870
  : {}),
871
+ projectEffectiveCoverage: (covered) => this.foldEffectiveModelHistory(covered),
807
872
  summarize: async ({ coveredRuntimeEvents, newlyFoldedRuntimeEvents, previousCheckpoint }) => {
873
+ // Same contract as the standalone path: the planner hands the
874
+ // effective (transition-folded) view to the summarizer and pins its
875
+ // digest, so a summary can never quote a body a durable transition
876
+ // removed (#4845).
808
877
  return await this.summarizeWithFailureCircuit(summarizer, {
809
878
  sessionId: this.sessionId,
810
879
  turnId,
@@ -902,7 +971,7 @@ export class AiSdkCompaction {
902
971
  const state = input.midTurnState;
903
972
  if (input.retryAlreadyUsed || !state)
904
973
  return undefined;
905
- if (this.modelAdapter.classifyError(input.error) !== 'ContextLength')
974
+ if (this.modelAdapter.classifyError(input.error) !== 'context_overflow')
906
975
  return undefined;
907
976
  const eligibleImages = collectHistoricalImageToolResults(state.priorContentEvents);
908
977
  const imageOmission = omitHistoricalImageToolResults(input.currentMessages, eligibleImages);
@@ -914,7 +983,7 @@ export class AiSdkCompaction {
914
983
  state.baselineTokens = undefined;
915
984
  return { messages: imageOmission.messages };
916
985
  }
917
- const phase = input.stepNumber === 0 ? 'pre_turn' : 'mid_turn';
986
+ const phase = state.compactionPhase(input.stepNumber);
918
987
  // Entering the module spends the send's one attempt whether or not a fold
919
988
  // comes out of it; only a selected projection sets `applied`.
920
989
  state.compactionAttemptedThisSend = true;
@@ -1140,6 +1209,12 @@ export class MidTurnCapacityCompactState {
1140
1209
  this.priorInvocations = priorInvocations;
1141
1210
  this.capacity = capacity;
1142
1211
  }
1212
+ compactionPhase(stepNumber) {
1213
+ return stepNumber === 0 &&
1214
+ !this.priorContentEvents.some((event) => event.id === this.headAnchor.id)
1215
+ ? 'pre_turn'
1216
+ : 'mid_turn';
1217
+ }
1143
1218
  }
1144
1219
  /**
1145
1220
  * The baseline one accepted request leaves for the next: its real input plus