Skip to content

fix: share one OAuth session between run sync and the Pi agent - #1358

Merged
fercgomes merged 6 commits into
codex/wizard-run-syncfrom
posthog/run-sync-no-refresh
Sep 25, 2026
Merged

fercgomes merged 6 commits into
codex/wizard-run-syncfrom
posthog/run-sync-no-refresh

Conversation

@gewenyu99

@gewenyu99 gewenyu99 commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

On the wizard-run variant, run sync refreshes the OAuth token while the agent runs. The Pi agent keeps its boot token for gateway re-mints and PostHog MCP, and the server revokes that token on refresh, so those calls fail.

Changes

sequenceDiagram
  participant C as Run sync, Pi gateway, Pi MCP
  participant S as oauth-session
  participant H as Host (authenticate.ts)
  participant P as PostHog /oauth/token

  H->>S: refreshAccessTokenIfNeeded configures it before the agent starts
  C->>S: currentAccessToken()
  S-->>C: current token
  Note over C,S: later, a read finds the token near expiry
  C->>S: currentAccessToken()
  S->>H: rotateCredentials, once for all callers
  H->>P: refresh-token grant
  P-->>H: new access and refresh token
  H-->>S: rotated credentials
  S-->>C: new token, and the MCP token env follows
Loading

One session holds the run's OAuth token. Every caller asks it for the current token, and the first read that finds the token near expiry rotates it once for everyone.

  • src/shared/oauth-session.ts. Holds the token for the agent, run sync and the host, and runs one rotation at a time.
  • Gateway cache key. Survives a rotation, so it doesn't spend one of the 5 weekly mints.
  • Pi MCP. Reads the current token on connect, and its token env follows each rotation.
  • Log. One [oauth-session] token rotated line per rotation.

The Anthropic harness still reads the boot token.

Test plan

Full local stack with the first-party token TTL cut to 52 minutes, driven in tmux on a one-file app. posthog-integration ran on the Pi orchestrator with the local MCP and wizard-run-sync=wizard-run, and exited 0.

  • Rotation. The token rotated twice before the report task, and each old token was revoked server-side.
  • MCP. The report task connected after the second rotation and created a notebook through posthog_exec.
  • Run sync. One create, 21 task snapshots and a final status, all accepted. The run ended completed.
Key log lines
00:42:03 [oauth-session] token rotated
00:44:19 [oauth-session] token rotated
00:45:10 [pi-mcp] adapter loaded; posthog MCP at http://localhost:8787/mcp
00:45:59 [pi-task] → mcp {"tool":"posthog_exec", ... "call notebooks-create-markdown ...
00:46:08 [pi-task] assistant: [NOTEBOOK_URL] http://localhost:8010/project/1/notebooks/eiiMC9DX
Run log (auth, rotation, MCP, run sync, task usage)
00:39:41 [oauth] starting flow against http://localhost:8010, requested scopes: (list omitted)
00:39:41 [oauth] token exchange succeeded, granted scopes: (list omitted)
00:39:41 [gateway] minted a scoped token: program=posthog-integration team=1 ttl=86399s url=http://localhost:8080
00:40:13 [pi-task] usage task=seed model=openai/gpt-5.6-sol effort=medium dur=28s turns=8 in=5868 out=1331 cacheR=34003 cacheW=0
00:40:42 [pi-task] usage task=install model=openai/gpt-5.6-sol effort=low dur=26s turns=7 in=8760 out=1187 cacheR=42487 cacheW=0
00:41:00 [pi-task] usage task=init model=openai/gpt-5.6-sol effort=low dur=45s turns=8 in=36523 out=2886 cacheR=168309 cacheW=0
00:41:14 [pi-task] usage task=error-tracking model=openai/gpt-5.6-sol effort=low dur=14s turns=4 in=11463 out=968 cacheR=25504 cacheW=0
00:41:25 [pi-task] usage task=identify model=openai/gpt-5.6-sol effort=medium dur=25s turns=4 in=30254 out=1310 cacheR=57609 cacheW=0
00:42:02 [pi-task] usage task=capture model=openai/gpt-5.6-sol effort=medium dur=35s turns=6 in=32012 out=1553 cacheR=109663 cacheW=0
00:42:03 [oauth-session] token rotated
00:42:03 [pi-mcp] adapter loaded; posthog MCP at http://localhost:8787/mcp
00:42:15 [pi-task] usage task=dashboard model=openai/gpt-5.6-sol effort=low dur=13s turns=3 in=9064 out=814 cacheR=14560 cacheW=0
00:42:17 [pi-task] usage task=ai-observability model=openai/gpt-5.6-sol effort=medium dur=14s turns=3 in=20580 out=911 cacheR=26252 cacheW=0
00:43:19 [pi-task] usage task=logs model=openai/gpt-5.6-sol effort=medium dur=64s turns=9 in=22665 out=4956 cacheR=107549 cacheW=0
00:44:19 [oauth-session] token rotated
00:45:10 [pi-task] usage task=review model=openai/gpt-5.6-sol effort=medium dur=111s turns=10 in=26711 out=5213 cacheR=176953 cacheW=0
00:45:10 [pi-mcp] adapter loaded; posthog MCP at http://localhost:8787/mcp
00:45:39 [pi-task] → mcp {"server":"posthog","describe":"posthog_exec"}
00:45:41 [pi-task] → mcp {"tool":"posthog_exec","args":{"command":"info notebooks-create-markdown"},"server":"posthog"}
00:45:59 [pi-task] → mcp {"tool":"posthog_exec","server":"posthog","args":{"command":"call notebooks-create-markdown …"}}
00:46:08 [pi-task] assistant: [NOTEBOOK_URL] http://localhost:8010/project/1/notebooks/eiiMC9DX
[NOTEBOOK_URL] http://localhost:8010/project/1/notebooks/eiiMC9DX
00:46:12 [pi-task] usage task=report model=openai/gpt-5.6-sol effort=low dur=62s turns=7 in=32463 out=4249 cacheR=138187 cacheW=0
Backend requests for the run
POST  /wizard/runs/               201  x1
PUT   /wizard/runs/{run}/tasks/   204  x21
PATCH /wizard/runs/{run}/         200  x1
wizard_wizardrun.status = completed
Checks
  • pnpm typecheck, ESLint and Prettier are clean.
  • 3403 of 3403 unit tests pass.

🤖 Generated with Claude Code

Run sync refreshed the login's OAuth token from its own queue while the
agent kept the token it booted with, so a mid-run rotation left the Pi
gateway re-mint and PostHog MCP on a replaced token.

`src/agent/oauth-session.ts` now holds the run's credentials the way
`gateway-session.ts` holds gateway auth: one process-wide holder with a
shared in-flight rotation. The host supplies the rotation, since the agent
never talks OAuth. Run sync, the Pi gateway re-mints and Pi MCP read the
current token from it, and the MCP token env follows each rotation. Dev
builds log the holder's lifecycle with token fingerprints.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

🧙 Wizard CI

Run the Wizard CI and test your changes against wizard-workbench example apps by replying with a GitHub comment using one of the following commands:

Test all apps:

  • /wizard-ci all

Test all apps in a directory:

  • /wizard-ci ai-observability
  • /wizard-ci basic-integration
  • /wizard-ci feature-flags
  • /wizard-ci mcp-analytics
  • /wizard-ci replay-vision
  • /wizard-ci revenue
  • /wizard-ci self-driving
  • /wizard-ci warehouse
  • /wizard-ci warehouse-seeded

Test an individual app:

  • /wizard-ci ai-observability/anthropic
  • /wizard-ci ai-observability/google-adk
  • /wizard-ci ai-observability/groq
Show more apps
  • /wizard-ci ai-observability/manual-capture
  • /wizard-ci ai-observability/openai
  • /wizard-ci ai-observability/openai-agents
  • /wizard-ci ai-observability/opentelemetry
  • /wizard-ci ai-observability/vercel-ai
  • /wizard-ci basic-integration/android
  • /wizard-ci basic-integration/angular
  • /wizard-ci basic-integration/astro
  • /wizard-ci basic-integration/django
  • /wizard-ci basic-integration/fastapi
  • /wizard-ci basic-integration/flask
  • /wizard-ci basic-integration/flutter
  • /wizard-ci basic-integration/javascript-node
  • /wizard-ci basic-integration/javascript-web
  • /wizard-ci basic-integration/laravel
  • /wizard-ci basic-integration/next-js
  • /wizard-ci basic-integration/nuxt
  • /wizard-ci basic-integration/python
  • /wizard-ci basic-integration/rails
  • /wizard-ci basic-integration/react-native
  • /wizard-ci basic-integration/react-router
  • /wizard-ci basic-integration/sveltekit
  • /wizard-ci basic-integration/swift
  • /wizard-ci basic-integration/tanstack-router
  • /wizard-ci basic-integration/tanstack-start
  • /wizard-ci basic-integration/vue
  • /wizard-ci feature-flags/django
  • /wizard-ci feature-flags/next-js
  • /wizard-ci mcp-analytics/custom-dispatcher
  • /wizard-ci mcp-analytics/typescript-sdk
  • /wizard-ci replay-vision/javascript-node
  • /wizard-ci replay-vision/next-js
  • /wizard-ci replay-vision/react-vite
  • /wizard-ci revenue/stripe
  • /wizard-ci self-driving/astro
  • /wizard-ci self-driving/fastapi
  • /wizard-ci self-driving/nuxt
  • /wizard-ci self-driving/react-router
  • /wizard-ci self-driving/sveltekit
  • /wizard-ci warehouse/monorepo-env
  • /wizard-ci warehouse/multi-source-next
  • /wizard-ci warehouse/stripe-node
  • /wizard-ci warehouse/zero-source
  • /wizard-ci warehouse-seeded/next-stripe
  • /wizard-ci warehouse-seeded/next-stripe-declined

Test against a Context Mill branch:

  • /wizard-ci all context-mill:my-branch

Add context-mill:<branch> to any command above to pin the Context Mill branch. It defaults to main.

Results will be posted here when complete.

gewenyu99 and others added 2 commits September 24, 2026 19:20
The gateway cache was keyed by the OAuth access token, so every rotation
missed it and minted again, and mints are capped per week. The OAuth
session now tracks each login's token lineage, and the gateway keys its
cache by the lineage's first token.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tsx runs leave NODE_ENV unset, so IS_DEV hid the lines there. The --local-* flags use the same !IS_PRODUCTION_BUILD gate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gewenyu99
gewenyu99 marked this pull request as ready for review September 25, 2026 00:05
@gewenyu99
gewenyu99 requested a review from a team as a code owner September 25, 2026 00:05
@gewenyu99
gewenyu99 added this pull request to stack #1359 September 25, 2026 00:05
Comment thread src/agent/oauth-session.ts Fixed
gewenyu99 and others added 2 commits September 24, 2026 20:18
Drop the token hashes and per-reader lines; CodeQL flags hashing a credential.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…zard into posthog/run-sync-no-refresh

# Conflicts:
#	src/lib/task-stream/wizard-run-sync.ts

@edwinyjlim edwinyjlim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

approve, but I think we should move oauth-session to /shared

other services besides agent will call oauth

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

idk if this should live under /agent

The agent, run sync and the host all read it, so it belongs beside auth-session-state rather than behind the agent entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fercgomes
fercgomes merged commit 3655901 into main Sep 25, 2026
19 checks passed
@fercgomes
fercgomes deleted the posthog/run-sync-no-refresh branch September 25, 2026 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants