Files
PaperClipAI/packages/adapters/codex-local/src/server/codex-auth-merge-decision.cjs
T
Nicky LeachandPaperclip 9ed55f6931 fix: allow concurrent agent runs on one OpenAI or xAI subscription connection (#13452)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Paperclip starts local command-line sessions and stores provider
credentials through managed connections
> - One OpenAI Codex or xAI Grok subscription connection held a
credential lease for the full agent run
> - A second run then waited for the first run, and concurrent runs
could overwrite a newer credential
> - The write-back must compare fresh credentials while it holds the row
lock
> - This pull request removes the run lease, keeps the revocation guard,
and bounds Codex timestamps against the host clock
> - The benefit is safe concurrent use of one subscription connection
with newest-credential selection

## Linked Issues or Issue Description

**What happened?**

A managed OpenAI Codex or xAI Grok subscription connection held a
credential lease for the full agent run. A second run waited for the
first run to finish. The write-back gate also rejected any row change
before it compared credential freshness.

**Expected behavior**

Concurrent runs should start on one subscription connection. The server
should keep the newest valid credential and reject a credential write
after a person revokes the connection.

**Steps to reproduce**

1. Start two runs that use one OpenAI or xAI subscription connection.
2. Let both provider tools refresh the credential.
3. Finish the runs in either order.
4. Confirm that the newest valid credential remains in the connection.

**Paperclip version or commit**

`ec25bf1e4a81d1729a6d7276e6a486587cff4a0b`

**Deployment mode**

Local dev (`pnpm dev`), built from source.

**Agent adapter(s) involved**

Codex. The server path also covers xAI Grok subscription connections.

**Database mode**

Embedded PGlite for local development, and external Postgres for
deployments.

**Access context**

Both board and agent runs can use managed connections.

**Additional context**

The provider command-line tool refreshes credentials inside the sandbox.
The server copies the result back after the run. Two long runs can still
refresh one token hours apart, so the provider can reject the second
refresh. The server cannot observe that provider call.

## What Changed

- Remove the full-run credential lease for OpenAI Codex and xAI Grok
subscription connections.
- Lock and re-read the connection row before credential write-back.
- Accept only a strictly newer credential, while keeping the connection
revocation guard.
- Reject Codex freshness timestamps more than five minutes ahead of the
host clock.
- Add tests for both completion orders, xAI cleanup, revocation, the
Codex time bound, and agent hiring.
- Update the connection and run-log documentation.

## Verification

- `server/src/__tests__/ai-connections.test.ts` passes with 42 tests.
-
`packages/adapters/codex-local/src/server/codex-auth-merge-decision.test.ts`
covers the five-minute boundary and the one-millisecond overflow.
- `packages/adapters/codex-local/src/server/codex-auth-merge.test.ts`
passes.
- `server/src/__tests__/agent-hire-ai-connections.test.ts` covers OpenAI
and Anthropic.
- The project type check reports no new error in changed files.
- GitHub Actions must pass on this pull request.

## Risks

The write-back now permits concurrent runs, so the provider may reject a
later refresh when both long runs use one token. The server keeps the
revocation guard and rejects future-dated Codex timestamps. No schema
change occurs.

## Model Used

OpenAI Codex, GPT-5. Context window and exact deployment build are not
exposed in this run. The model used tool calls, code inspection, and
test verification.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` /
`Closes: #` / `Refs: #` OR (b) described the issue in-PR following the
relevant issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-09-15 01:10:54 -07:00

171 lines
7.3 KiB
JavaScript

const fs = require("fs");
// Co-change notice: parseAuth below mirrors hasUsableAuthPayload in
// packages/adapters/codex-local/src/server/codex-home.ts. If the auth format
// changes (new shape, renamed field), update both sites together.
function parseAuth(filePath) {
let parsed;
try {
parsed = JSON.parse(fs.readFileSync(filePath, "utf8"));
} catch {
return { kind: "unusable" };
}
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
return { kind: "unusable" };
}
if (typeof parsed.OPENAI_API_KEY === "string" && parsed.OPENAI_API_KEY.trim().length > 0) {
return { kind: "apikey" };
}
const tokens = parsed.tokens;
if (tokens === null || typeof tokens !== "object" || Array.isArray(tokens)) {
return { kind: "unusable" };
}
const accountId = typeof tokens.account_id === "string" ? tokens.account_id.trim() : "";
const hasTokenMaterial = ["id_token", "access_token", "refresh_token"].some((key) => {
const value = tokens[key];
return typeof value === "string" && value.trim().length > 0;
});
if (!accountId || !hasTokenMaterial) {
return { kind: "unusable" };
}
const lastRefresh = typeof parsed.last_refresh === "string" ? Date.parse(parsed.last_refresh) : NaN;
return {
kind: "subscription",
accountId,
lastRefresh: Number.isFinite(lastRefresh) ? lastRefresh : null,
};
}
// This predicate answers a single, direction-agnostic question: should the
// caller replace the `destination` auth.json with the `source` auth.json? The
// caller picks which copy is source and which is destination from its own frame
// of reference (an inbound restore, an outbound copy-back, …) purely by argument
// order — there is no `--direction` flag and no hard-coded sandbox/host notion:
//
// argv[0] (first positional) = source auth.json path
// argv[1] (second positional) = destination auth.json path
//
// The predicate has two modes, selected by a leading positional flag:
//
// default (no flag) — fail closed to the destination; used by the host default
// store, whose fail-closed behavior must never change. An absent or
// unusable destination keeps the destination (no seed from empty).
// --seed-if-dest-absent — the opt-in cache-slot mode; used only by the
// per-identity cache slot helper. It ADDS one behavior on top of the default:
// when the destination is unusable (absent or unparseable) AND the source is
// a usable subscription credential, use the source (fill the empty slot). It
// never relaxes the different-identity, api-key, or unusable-source guards.
//
// A leading positional flag (not an environment variable) keeps the mode
// explicit per call, so a host-default two-path call can never enter seed mode.
//
// Exit contract. Exit 10 = use source; exit 20 = keep destination; exit 22 =
// keep destination, the source `last_refresh` sat further ahead of the host
// clock than the plausible skew allowance. The predicate only ever reads the
// two files and exits with a code — it never prints token bytes.
const USE_SOURCE = 10;
const KEEP_DESTINATION = 20;
const IMPLAUSIBLE_LAST_REFRESH = 22;
const SEED_IF_DEST_ABSENT_FLAG = "--seed-if-dest-absent";
// A `last_refresh` records an event that already happened, so an honest value
// always sits at or before the host clock. Only clock skew between a sandbox
// and the host explains a small future value. Five minutes is larger than
// real skew on a time-synchronised host, and it is short enough that a source
// which claims a `last_refresh` further ahead than this cannot be trusted.
const MAX_FUTURE_LAST_REFRESH_SKEW_MS = 5 * 60 * 1000;
// `decide` takes already-parsed `{ kind, accountId, lastRefresh }` shapes plus
// a caller-supplied `nowMs` and the `seedIfDestAbsent` flag, so a test can
// drive the exact skew bound without spawning a process and racing wall-clock
// drift. Guard order, first match wins:
// 1. Seed mode, the destination is unusable, and the source is a usable
// subscription credential -> USE_SOURCE, unless the source fails the
// skew bound in step 3 below, in which case the absent slot stays
// absent (IMPLAUSIBLE_LAST_REFRESH). A poisoned seed would out-live
// every honest refresh, since nothing could ever compare as "strictly
// newer" than an unbounded future value, so the bound applies here too.
// 2. Either side unusable, a kind mismatch, the destination is an api-key
// credential, or the two sides carry a different account_id ->
// KEEP_DESTINATION.
// 3. The source `last_refresh` sits further ahead of `nowMs` than
// MAX_FUTURE_LAST_REFRESH_SKEW_MS -> IMPLAUSIBLE_LAST_REFRESH. Only the
// source is bounded, and only against the caller's clock: the source is
// the sandbox-supplied side, so it must never supply the reference time.
// 4. The source `last_refresh` is strictly greater than the destination's
// -> USE_SOURCE.
// 5. Otherwise (a tie, a null value on either side, or an older source) ->
// KEEP_DESTINATION.
function decide(source, destination, nowMs, seedIfDestAbsent) {
const sourceIsImplausible =
source.lastRefresh !== null &&
source.lastRefresh - nowMs > MAX_FUTURE_LAST_REFRESH_SKEW_MS;
// Seed mode only: fill an ABSENT (unusable) destination slot from a usable
// subscription source. A subscription-kind source is guaranteed usable and to
// carry a real account_id (parseAuth returns "subscription" only then), so this
// is never a random pick. This branch changes ONLY the destination-unusable
// case; the api-key and unusable-source guards below still keep the destination.
if (
seedIfDestAbsent &&
destination.kind === "unusable" &&
source.kind === "subscription"
) {
return sourceIsImplausible ? IMPLAUSIBLE_LAST_REFRESH : USE_SOURCE;
}
// Fail closed to the destination unless both sides are the same usable,
// subscription-kind identity — an unusable side, an api-key credential, a kind
// mismatch, or a different account_id all keep the destination copy.
if (
destination.kind === "unusable" ||
source.kind === "unusable" ||
source.kind !== destination.kind ||
destination.kind === "apikey" ||
source.accountId !== destination.accountId
) {
return KEEP_DESTINATION;
}
if (sourceIsImplausible) {
return IMPLAUSIBLE_LAST_REFRESH;
}
// Use the source credential only when it is strictly fresher: both sides must
// carry a parseable last_refresh and the source one must be strictly greater.
// Ties and null/unparseable freshness keep the destination copy so a spent
// single-use refresh token is never written over a good one.
if (
source.lastRefresh !== null &&
destination.lastRefresh !== null &&
source.lastRefresh > destination.lastRefresh
) {
return USE_SOURCE;
}
return KEEP_DESTINATION;
}
if (require.main === module) {
const rawArgs = process.argv.slice(2);
const seedIfDestAbsent = rawArgs[0] === SEED_IF_DEST_ABSENT_FLAG;
const [sourceAuthPath, destinationAuthPath] = seedIfDestAbsent ? rawArgs.slice(1) : rawArgs;
const sourceAuth = parseAuth(sourceAuthPath);
const destinationAuth = parseAuth(destinationAuthPath);
process.exit(decide(sourceAuth, destinationAuth, Date.now(), seedIfDestAbsent));
}
module.exports = {
decide,
parseAuth,
USE_SOURCE,
KEEP_DESTINATION,
IMPLAUSIBLE_LAST_REFRESH,
MAX_FUTURE_LAST_REFRESH_SKEW_MS,
};