πŸ“„ File detail

utils/workloadContext.ts

🧩 .tsπŸ“ 58 linesπŸ’Ύ 2,337 bytesπŸ“ text
← Back to All Files

🎯 Use case

This file lives under β€œutils/”, which covers cross-cutting helpers (shell, tempfiles, settings, messages, process input, …). On the API surface it exposes Workload, WORKLOAD_CRON, getWorkload, and runWithWorkload β€” mainly functions, hooks, or classes. Dependencies touch async_hooks. What the file header says: Turn-scoped workload tag via AsyncLocalStorage. WHY a separate module from bootstrap/state.ts: bootstrap is transitively imported by src/entrypoints/browser-sdk.ts, and the browser bundle cannot import Node's async_hooks. This module is only imported from CLI/SDK code paths that.

Generated from folder role, exports, dependency roots, and inline comments β€” not hand-reviewed for every path.

🧠 Inline summary

Turn-scoped workload tag via AsyncLocalStorage. WHY a separate module from bootstrap/state.ts: bootstrap is transitively imported by src/entrypoints/browser-sdk.ts, and the browser bundle cannot import Node's async_hooks. This module is only imported from CLI/SDK code paths that never end up in the browser build. WHY AsyncLocalStorage (not a global mutable slot): void-detached background agents (executeForkedSlashCommand, AgentTool) yield at their first await. The parent turn's synchronous continuation β€” including any `finally` block β€” runs to completion BEFORE the detached closure resumes. A global setWorkload('cron') at the top of the closure is deterministically clobbered. ALS captures context at invocation time and survives every await in that chain, isolated from the parent. Same pattern as agentContext.ts.

πŸ“€ Exports (heuristic)

  • Workload
  • WORKLOAD_CRON
  • getWorkload
  • runWithWorkload

πŸ“š External import roots

Package roots from from "…" (relative paths omitted).

  • async_hooks

πŸ–₯️ Source preview

/**
 * Turn-scoped workload tag via AsyncLocalStorage.
 *
 * WHY a separate module from bootstrap/state.ts:
 * bootstrap is transitively imported by src/entrypoints/browser-sdk.ts, and
 * the browser bundle cannot import Node's async_hooks. This module is only
 * imported from CLI/SDK code paths that never end up in the browser build.
 *
 * WHY AsyncLocalStorage (not a global mutable slot):
 * void-detached background agents (executeForkedSlashCommand, AgentTool)
 * yield at their first await. The parent turn's synchronous continuation β€”
 * including any `finally` block β€” runs to completion BEFORE the detached
 * closure resumes. A global setWorkload('cron') at the top of the closure
 * is deterministically clobbered. ALS captures context at invocation time
 * and survives every await in that chain, isolated from the parent. Same
 * pattern as agentContext.ts.
 */

import { AsyncLocalStorage } from 'async_hooks'

/**
 * Server-side sanitizer (_sanitize_entrypoint in claude_code.py) accepts
 * only lowercase [a-z0-9_-]{0,32}. Uppercase stops parsing at char 0.
 */
export type Workload = 'cron'
export const WORKLOAD_CRON: Workload = 'cron'

const workloadStorage = new AsyncLocalStorage<{
  workload: string | undefined
}>()

export function getWorkload(): string | undefined {
  return workloadStorage.getStore()?.workload
}

/**
 * Wrap `fn` in a workload ALS context. ALWAYS establishes a new context
 * boundary, even when `workload` is undefined.
 *
 * The previous implementation short-circuited on `undefined` with
 * `return fn()` β€” but that's a pass-through, not a boundary. If the caller
 * is already inside a leaked cron context (REPL: queryGuard.end() β†’
 * _notify() β†’ React subscriber β†’ scheduled re-render captures ALS at
 * scheduling time β†’ useQueueProcessor effect β†’ executeQueuedInput β†’ here),
 * a pass-through lets `getWorkload()` inside `fn` return the leaked tag.
 * Once leaked, it's sticky forever: every turn's end-notify re-propagates
 * the ambient context to the next turn's scheduling chain.
 *
 * Always calling `.run()` guarantees `getWorkload()` inside `fn` returns
 * exactly what the caller passed β€” including `undefined`.
 */
export function runWithWorkload<T>(
  workload: string | undefined,
  fn: () => T,
): T {
  return workloadStorage.run({ workload }, fn)
}