Skip to content

fix(cost): preserve worker activity when a session log is unreadable - #970

Merged
mldangelo-oai merged 9 commits into
openai:mainfrom
feiiiiii5:fix/cost-quarantine-inaccessible-session-log
Nov 4, 2026
Merged

mldangelo-oai merged 9 commits into
openai:mainfrom
feiiiiii5:fix/cost-quarantine-inaccessible-session-log

Conversation

@feiiiiii5

@feiiiiii5 feiiiiii5 commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

An unreadable session log could hide progress from healthy workers in the same scan. A scan without a spending limit can now deliver activity, progress, and session events from workers it can read while cost accounting is unavailable. Issue #223

This is the smaller alternative to #2178 for #223. Merge only one. #2178 also limits repeated denied opens and tracks changing diagnostics across individual files.

Changes

When an observer requests worker events, an uncapped poll delivers the available events before returning the original read error. If permission is denied for an unidentified session, its error still takes priority, even when a later directory or resource error stops the search. Later polls retry denied file reads, so verified accounting can resume when access returns. A denied stat on a directory entry stops traversal immediately, avoiding repeated metadata calls against its siblings.

Testing

On current main, an inaccessible session prevented delivery for both EACCES and EPERM. On the reviewed branch the cost suite and the SDK/CLI cost controls passed. Nine installed-package checks on Linux covered permission failures, real file-descriptor exhaustion, and error order in capped and uncapped scans.

Risk and rollout

With an explicit spending limit, or when only accounting is requested, the poll stops as soon as it establishes that usage cannot be verified, including during replay of an associated session. Failed polls publish no new cost or usage total. The scan API continues to suppress repeated optional warnings for denied opens on the same path. PR #977

Public disclosure review

  • No customer, partner, prospect, or user identities, data, or identifying details are included.
  • No credentials, personal data, private source, scan findings, or nonpublic links or tickets are included.
  • I reviewed the branch name, title, description, commits, changes, comments, logs, screenshots, attachments, and links for public disclosure.

@github-actions github-actions Bot added the bug Something isn't working label Sep 18, 2026
@feiiiiii5

Copy link
Copy Markdown
Contributor Author

Flagging an overlap I should have caught before opening this.

Staff PR #465 — fix(cost): enforce verifiable scan spending limits, open since 2026-08-15 — already covers this root cause. Its sdk/typescript/src/cost.ts diff adds the same predicate and the same helper this PR introduces:

function isSessionAccessDenied(error: unknown): boolean {
  return (
    isRecord(error) && (error["code"] === "EACCES" || error["code"] === "EPERM")
  );
}

and calls quarantineSession(session, error) at both sites this PR touches: the open() failure inside readSessionUsage (if (session.threadId === null && isSessionAccessDenied(error))), and the unreadable loop in #readSessions (else if (isSessionAccessDenied(error))). It also reworks session.unreadable from a boolean into { error } and adds requireReadableSessions / readFailures, which looks like it reaches the stop() interaction I listed under "Not changed, deliberately". I have not traced that path end to end, so treat that last part as unverified.

To be precise about the current state: main as of writing has no quarantineSession, no isSessionAccessDenied and no EACCES handling in sdk/typescript/src/cost.ts, so #223 is still unfixed and this PR is not stale today.

If #465 lands first, though, this one is redundant and should be closed rather than rebased. I have not closed it myself because #465 is still open, so the fix is not on main yet. Happy to close on a maintainer's word, or to narrow this to whatever #465 leaves uncovered if that is more useful than closing it.

The note in the description about #224 stands: it was closed by its author on 2026-09-01 with no maintainer decision recorded.

@feiiiiii5

Copy link
Copy Markdown
Contributor Author

CI is green here (one check, passing), and this has been sitting since 18 September.

The change is one guard: readSessionUsage had two failure exits and only one of them set session.unreadable, so a log it could not read kept failing on every later cost poll — including a poll after the scan had already finished. Now both exits record the session as bad and the top-of-function guard skips it, so the cost report says the log is inaccessible once instead of repeating it.

Is there anything you would like changed before this can go in?

readSessionUsage quarantines a log it cannot parse, so the guard at the top
of the function skips it on later polls, but a log it cannot open records
nothing. The next poll opens the same file and fails identically for the life
of the scan, and because an unopenable log never produced a thread id, the
failure also escapes the thread-tree filter in #readSessions. stop() awaits
refresh() unguarded, so the final poll can reject after the turn completed and
discard the caller's usage.

Quarantine the session on an access refusal the process cannot clear by
retrying, mirroring the parse-failure exit. Other open failures keep retrying:
a process-wide shortage such as EMFILE clears on its own, and retiring a
session for it would stop observing usage that could still be read.
@feiiiiii5
feiiiiii5 force-pushed the fix/cost-quarantine-inaccessible-session-log branch from a2485be to 3bcc75d Compare October 5, 2026 04:22
@mldangelo-oai mldangelo-oai added area:cost Token usage, cost estimates, spending limits, and quota visibility. and removed area:cost Token usage, cost estimates, spending limits, and quota visibility. labels Oct 8, 2026
@mldangelo-oai mldangelo-oai changed the title fix(cost): report an inaccessible session log once, not on every poll fix(cost): preserve worker activity when a session log is unreadable Nov 1, 2026
@mldangelo-oai

Copy link
Copy Markdown
Collaborator

Thanks!

@mldangelo-oai
mldangelo-oai merged commit 6703d0a into openai:main Nov 4, 2026
60 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 4, 2026
3 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants