Fiber improvements: configurable keepAlive, E2E tests, type safety, docs#1252
Merged
threepointone merged 2 commits intomainfrom Apr 2, 2026
Merged
Fiber improvements: configurable keepAlive, E2E tests, type safety, docs#1252threepointone merged 2 commits intomainfrom
threepointone merged 2 commits intomainfrom
Conversation
Introduce a ref-counted keepAlive alarm (configurable keepAliveIntervalMs, default 30s) and wire Agent to use ctx.storage.setAlarm for durable, invisible heartbeats. Update Agent options and internals to use DEFAULT_KEEP_ALIVE_INTERVAL_MS/keepAliveIntervalMs and ensure _scheduleNextAlarm uses the resolved option. Small docs updates clarifying schedule purpose and alarm behavior (experimental/forever.md, forever-fibers README, design/think.md). Add comprehensive e2e eviction tests and a test worker: new vitest e2e script, expanded fiber-eviction.test.ts (poll helper, kill/restart flow, multiple scenarios), and worker enhancements (short keepAlive interval for tests, lifecycle hooks, callable helpers). Misc: minor test robustness and cleanup improvements (process killing, persistence directory handling) and a clarified checkFibers comment.
Move the experimental fiber warning to only log once (add _fiberWarningShown) to reduce noisy repeated warnings. Tighten spawn method types to keyof this & string across agents and tests and add the _fiberWarningShown property to the Fiber interface. Adjust test behavior to reduce flakiness and improve CI speed: re-enable fiber tests in vitest config, shorten long fiber timeouts (e.g. 5000/10000 -> 500), add TestFiberAgent options, reduce many setTimeouts in ai-chat tests from 200ms to 50ms and add agentStub.waitForIdleForTest calls, and tweak message-concurrency chunk counts/delays and delays. Bump experimental/session-search's ai dependency to ^6.0.143. Remove examples/vsco-onboarding/.gitignore.
|
agents
@cloudflare/ai-chat
@cloudflare/codemode
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This branch improves the experimental fiber system across several dimensions: testability, type safety, developer experience, and documentation accuracy.
Configurable
keepAliveIntervalMskeepAliveIntervalMstoAgentStaticOptions(default 30s, matching existing behavior)_scheduleNextAlarm()now usesthis._resolvedOptions.keepAliveIntervalMsinstead of a hardcoded constantComprehensive E2E fiber tests
Since workerd now persists alarm state to disk (cloudflare/workerd#6104), alarms survive process restarts — matching production behavior. The E2E test suite leverages this with 5 real-eviction scenarios (SIGKILL + restart, no manual
triggerAlarm()workaround):Added
test:e2escript topackages/agents/package.json.Fiber unit tests re-enabled in CI
The 28 fiber unit tests in
packages/agents/src/tests/fiber.test.tswere excluded from CI because they hung. Root cause: tests spawnedslowWorkwith 5-10 second durations, then simulated eviction without stopping the background execution. StalesetTimeouttimers heldkeepAliverefs, generating alarm chains the vitest worker pool couldn't shut down.Fix: shortened all
slowWorkdurations from 5000-10000ms to 500ms (tests only need fibers "running" at the moment of eviction) and setkeepAliveIntervalMs: 2_000on the test agent. Removed the exclusion fromvitest.config.ts— fiber tests now run in CI.spawnFibertype safetyChanged
FiberMethods.spawnFiberparameter frommethodName: stringtomethodName: keyof this & string. TypeScript's polymorphicthisresolves to the concrete class at the call site, giving users autocomplete for their method names.Experimental warning moved to first use
Moved the
console.warnfrom thewithFibersconstructor tospawnFiber()(fires once). Think agents that only use chat never see the experimental warning.Flaky test fixes (ai-chat)
client-tool-duplicate-message.test.ts: Replaced 17 instances ofsetTimeout(200)withwaitForIdleForTest()— waits for actual processing instead of a fixed timermessage-concurrency.test.ts: Increased streaming duration in the merge test so req-merge-1 is definitely still active when overlapping requests arriveDocs updated
experimental/forever.md: Rewrote Layer 1 (was describing unshippedscheduleEvery/_cf_fiberHeartbeatmechanism), alarm interaction, tradeoffs section, and open questions (now "resolved decisions")design/think.md: Fixedcf_agents_schedulesdescriptionforever.ts: FixedcheckFibersJSDocforever-fibers/README.md: Added real eviction testing noteTest plan
npx nx run agents:test:workers— 903 tests pass (including 28 fiber unit tests, previously excluded)npm run test:e2e -w packages/agents— 5 E2E fiber eviction tests passnpx tsc --noEmit -p packages/agents/tsconfig.json— cleannpx tsc --noEmit -p packages/think/tsconfig.json— cleannpx tsc --noEmit -p packages/agents/src/tests/tsconfig.json— cleannpx tsc --noEmit -p packages/think/src/tests/tsconfig.json— cleanMade with Cursor