Skip to content

Conversation

@ghostwriternr
Copy link
Member

Summary

Enables TypeScript typechecking for test files across all packages while maintaining correct build output paths.

Implementation

Separates build and typecheck configurations:

  • tsconfig.json - IDE and typechecking (src + tests)
  • tsconfig.build.json - Build only (src → dist/)

Applied to @repo/shared and @repo/sandbox-container.

Test Plan

  • All builds pass
  • All typechecks pass
  • 440 unit tests pass

Added tests/e2e/ to typecheck pipeline.

Created test-worker/types.ts defining response types for test
infrastructure endpoints. All tests now use proper SDK types from
@repo/shared for SDK operations and test-worker types for test
infrastructure.

Removed non-null assertions with proper type guards. Found and fixed
bugs where tests used wrong field names (contextId vs id, processId
vs id) that were masked by inline types.
Build output was going to dist/src/ instead of dist/ due to
rootDir: "." in tsconfig, breaking Docker container startup
which expects dist/index.js.

Split tsconfig into separate build and typecheck configs.
Build emits src only to dist/, typecheck includes tests.
@changeset-bot
Copy link

changeset-bot bot commented Nov 21, 2025

⚠️ No Changeset found

Latest commit: 2f5eb72

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@ghostwriternr ghostwriternr merged commit ce1468a into fix-memory-leak Nov 21, 2025
4 checks passed
@ghostwriternr ghostwriternr deleted the fix-test-types branch November 21, 2025 12:51
Copy link
Contributor

@claude claude bot left a comment

Choose a reason for hiding this comment

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

Claude Code Review

Looks good. The tsconfig split pattern is correct - tsconfig.json for IDE/typecheck (includes tests), tsconfig.build.json for builds (src only). This is a standard best practice.

Type safety improvements are solid:

  • Mock loggers now properly implement the Logger interface with child() method
  • Error codes use ErrorCode enum instead of string literals
  • New types (VersionResult, ProxyErrorResponse.port) properly defined
  • Remaining any usage in tests is appropriate for dynamic test data

The E2E test tsconfig correctly excludes test-worker/** to prevent type conflicts between test context and Worker runtime.

No issues found. Ready to merge.

@github-actions
Copy link
Contributor

🐳 Docker Image Published

FROM cloudflare/sandbox:0.0.0-pr-238-5b0b863

Version: 0.0.0-pr-238-5b0b863

You can use this Docker image with the preview package from this PR.

ghostwriternr added a commit that referenced this pull request Nov 24, 2025
* Update default formatter

* Fix memory leaks in process management

Memory leaks occurred from three sources: event listeners accumulated
when log streaming was cancelled, completed processes remained in
memory indefinitely, and Durable Object state retained invalid
references after container restarts.

The ProcessStore now persists completed processes to disk while
keeping active processes in memory. ReadableStream cancel handlers
properly remove listeners when log streaming stops. The Sandbox
onStop lifecycle hook clears container-specific state to prevent
stale references.

* Add changeset

* Fix async cleanup and listener lifecycle

The onStop lifecycle hook now properly awaits storage deletions to
ensure cleanup completes before shutdown. Processes are deleted from
memory before persisting to disk to prevent race conditions during
concurrent access. Stream listeners are cleaned up on all close paths,
not just cancellation. The list method now scans disk to include
completed processes, maintaining backward compatibility.

* Use structured logging for disk write failures

* Fix data loss when persisting completed processes

Write to disk before deleting from memory to prevent data loss if write
fails. Always delete from memory regardless of write success to prevent
memory leaks.

* Fix list() directory check that prevented disk scanning

Bun.file().exists() returns false for directories, causing list() to skip
disk scanning and miss completed processes. Remove the directory existence
check and rely on error handling instead.

* Add comprehensive unit tests for ProcessStore

Test coverage for memory/disk hybrid storage, terminal state transitions,
disk persistence, write failure handling, and list() filtering.

* Add E2E tests for completed process retrieval and listing

Verify completed processes can be retrieved via get() with exit codes and
timestamps, and that list() includes both running and completed processes.

* Remove dot-prefix from process storage directory

Change from /tmp/.sandbox-internal to /tmp/sandbox-internal for better
debuggability. Makes it easier to discover and inspect process files during
development and troubleshooting.

* Fix race condition in process status updates

Event handler called store.update() fire-and-forget, causing API
queries to return stale data before disk writes completed.

Changed SessionManager to accept async callbacks and await them,
ensuring store updates complete before processing next event.

This provides strong consistency while maintaining the memory leak
fix that moves completed processes to disk.

* Update lockfile

* Clear activeMounts on container stop

Applies memory leak prevention from this PR to the activeMounts feature
that was merged from main. When a container stops or restarts, mount
points become invalid but the Map still held stale references.
Clearing it maintains consistency with portTokens and defaultSession.

* Enable TypeScript checking for test files (#238)

* Enable TypeScript checking for test files

* Enable TypeScript checking for e2e tests

Added tests/e2e/ to typecheck pipeline.

Created test-worker/types.ts defining response types for test
infrastructure endpoints. All tests now use proper SDK types from
@repo/shared for SDK operations and test-worker types for test
infrastructure.

Removed non-null assertions with proper type guards. Found and fixed
bugs where tests used wrong field names (contextId vs id, processId
vs id) that were masked by inline types.

* Fix incorrect assertions

* Use public Process interface correctly

* Revert enum usage

* Enrich interpreter error

* Fix TypeScript build output paths

Build output was going to dist/src/ instead of dist/ due to
rootDir: "." in tsconfig, breaking Docker container startup
which expects dist/index.js.

Split tsconfig into separate build and typecheck configs.
Build emits src only to dist/, typecheck includes tests.

* Fix type errors

* Update lockfile

* Fix code context e2e response checks

* Prevent just-completed process appearing twice
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.

1 participant