Skip to content

Commit 1ca43ab

Browse files
gtrrz-victorclaude
andcommitted
perf(codex): fall back on incremental token error; slice before file extract
Address review feedback: - computeIncrementalTokenUsage now falls back to the full-scan token calc on incremental error instead of silently dropping the checkpoint's token usage. - ExtractAllModifiedFiles slices to the post-offset lines before splitting, so per-turn work scales with the delta rather than the whole session. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Entire-Checkpoint: 01KY9HW4BJJRTQ7T5TMK7KGPR6
1 parent 406904c commit 1ca43ab

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

cmd/entire/cli/agent/codex/transcript.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,14 +386,15 @@ func scanTokenDelta(data []byte, startLineNum, fromOffset int, seed *tokenUsageD
386386
// transcript bytes it already holds, instead of falling back to a second full-file
387387
// disk read via ExtractModifiedFilesFromOffset.
388388
func (c *CodexAgent) ExtractAllModifiedFiles(transcriptData []byte, fromOffset int, _ string) ([]string, error) {
389+
// Slice to the lines added since fromOffset before splitting, so per-turn work
390+
// scales with the delta rather than the whole session.
391+
data := transcriptData
392+
if fromOffset > 0 {
393+
data = transcript.SliceFromLine(transcriptData, fromOffset)
394+
}
389395
seen := make(map[string]struct{})
390396
var files []string
391-
lineNum := 0
392-
for _, lineData := range splitJSONL(transcriptData) {
393-
lineNum++
394-
if lineNum <= fromOffset {
395-
continue
396-
}
397+
for _, lineData := range splitJSONL(data) {
397398
for _, f := range extractFilesFromLine(lineData) {
398399
if _, ok := seen[f]; !ok {
399400
seen[f] = struct{}{}

cmd/entire/cli/lifecycle.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,9 +1011,18 @@ func computeIncrementalTokenUsage(ctx, logCtx context.Context, inc agent.Increme
10111011
}
10121012
usage, next, err := inc.CalculateTokenUsageIncremental(transcriptData, fromOffset, prior)
10131013
if err != nil {
1014-
logging.Debug(logCtx, "incremental token calculation failed",
1014+
// Fall back to the full-scan path so a checkpoint's token usage is not
1015+
// silently dropped; skip persisting a baseline so the next hook re-derives
1016+
// one from a clean full scan.
1017+
logging.Debug(logCtx, "incremental token calculation failed, falling back to full scan",
10151018
slog.String("error", err.Error()))
1016-
return nil, nil
1019+
fallback, fbErr := inc.CalculateTokenUsage(transcriptData, fromOffset)
1020+
if fbErr != nil {
1021+
logging.Debug(logCtx, "full-scan token calculation also failed",
1022+
slog.String("error", fbErr.Error()))
1023+
return nil, nil
1024+
}
1025+
return fallback, nil
10171026
}
10181027
return usage, next
10191028
}

0 commit comments

Comments
 (0)