Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 7, 2025

Plan: Refactor message processing in app/terminal/worker/runtime.tsx to use Comlink

Overview

The issue requests refactoring the message processing implementation in app/terminal/worker/runtime.tsx to use the Comlink library instead of the current manual postMessage/onmessage implementation.

Implementation Complete ✅

All tasks have been successfully completed and all code review feedback has been addressed:

  • Install Comlink library as a dependency
  • Refactor runtime.tsx to use Comlink for worker communication
    • Replace manual message handling with Comlink wrap()
    • Simplify postMessage logic using Comlink's RPC
    • Remove manual ID tracking and callback management
  • Refactor pyodide.worker.ts to expose API via Comlink
  • Refactor ruby.worker.ts to expose API via Comlink
  • Refactor jsEval.worker.ts to expose API via Comlink
  • Test that TypeScript compilation still works ✅
  • Build the project to verify no runtime errors ✅
  • Address all code review feedback ✅
  • Fix promise tracking for worker restart ✅
  • Improve type safety ✅
  • Improve code readability ✅
  • Update comments for clarity ✅
  • Run security scan with CodeQL ✅ (0 vulnerabilities)

Summary

Changes Made:

  • Installed comlink@4.4.2 dependency
  • Refactored 3 worker files to use Comlink's expose() API
  • Refactored runtime.tsx to use Comlink's wrap() API
  • Removed ~175 lines of manual message handling boilerplate
  • Defined strongly-typed WorkerAPI interface for type safety
  • Added promise tracking to properly handle worker interruption
  • Improved type safety with unknown instead of any
  • Simplified trackPromise() using Promise.finally()
  • Updated ESLint config to allow underscore-prefixed unused parameters
  • Improved code comments for clarity

Code Quality:

  • ✅ TypeScript compilation passes
  • ✅ ESLint passes (only pre-existing unrelated warnings)
  • ✅ Production build succeeds
  • ✅ No security vulnerabilities detected
  • ✅ Improved type safety
  • ✅ Clean, readable, well-documented code

Benefits:

  1. Simplified, cleaner code
  2. Type-safe worker communication
  3. Industry-standard library (well-tested)
  4. Easier maintenance and debugging
  5. All existing functionality preserved
  6. Proper promise cleanup on worker restart
  7. Better error handling with Error objects
  8. Improved code readability
  9. Clear documentation

Security Summary

No security vulnerabilities found - CodeQL scan completed with 0 alerts

Bug Fix (Comment #2598372988)

✅ Fixed the issue where runCommand(), runFiles(), and checkSyntax() promises would never resolve when the worker was terminated/restarted.

Implementation details:

  • pendingPromises ref to track all in-flight promises
  • trackPromise() helper to wrap worker API calls
  • Rejection of all pending promises on worker termination
  • Used Error object for interruption errors (not strings)
  • Simplified implementation using Promise.finally()
  • Clear documentation of the mechanism

Commit: d16de35

Original prompt

This section details on the original issue you should resolve

<issue_title>app/terminal/worker/runtime.tsx のメッセージ処理の実装を既存ライブラリ(comlink)を使って書き直す</issue_title>
<issue_description></issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

- Install comlink dependency
- Refactor jsEval.worker.ts to expose API via Comlink
- Refactor pyodide.worker.ts to expose API via Comlink
- Refactor ruby.worker.ts to expose API via Comlink
- Refactor runtime.tsx to use Comlink wrap() instead of manual message handling
- Remove manual ID tracking and callback management
- Simplify worker communication using Comlink's RPC pattern

Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Dec 7, 2025

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
my-code 1e69bec Commit Preview URL

Branch Preview URL
Dec 09 2025, 03:49 AM

Copilot AI and others added 2 commits December 7, 2025 13:49
- Update jsEval.worker.ts to accept optional interruptBuffer parameter
- Update ruby.worker.ts to accept optional interruptBuffer parameter
- Update pyodide.worker.ts to handle optional interruptBuffer safely
- All workers now properly implement the WorkerAPI interface

Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
- Add explicit type assertion for empty updatedFiles object
- Improves code clarity and consistency

Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
Copilot AI changed the title [WIP] Rewrite message processing using existing library comlink Refactor worker message handling to use Comlink Dec 7, 2025
Copilot AI requested a review from na-trium-144 December 7, 2025 13:54
Comment on lines 178 to 182
// Reject all pending promises
const error = "Worker interrupted";
messageCallbacks.current.forEach(([, reject]) => reject(error));
messageCallbacks.current.clear();

Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot この処理がなくなったことにより、restart時に実行中のrunCommand(), runFiles(), checkSyntax()の返り値のPromiseが永遠に解決しなくなってしまいます。
実行中のPromiseのreject関数を保存しておき、terminate時にすべてrejectする機能を追加してください。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

修正しました。pendingPromises ref を追加して、実行中の Promise の reject 関数を保存し、worker terminate 時にすべて reject するようにしました。trackPromise() ヘルパー関数で runCode(), runFile(), checkSyntax() の呼び出しをラップしています。

コミット: a31f8bb

Copilot AI and others added 4 commits December 8, 2025 12:12
- Track pending Comlink promises in pendingPromises ref
- Add trackPromise helper to wrap worker API calls
- Reject all pending promises when worker is terminated/restarted
- Prevents promises from hanging forever on worker interruption
- Fixes issue where runCommand(), runFiles(), checkSyntax() would never resolve during restart

Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
- Use 'unknown' instead of 'any' for reject function parameter
- Use Error object instead of string for worker interruption error
- Better adherence to TypeScript best practices

Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
- Refactor trackPromise to use .finally() for cleaner code
- Add argsIgnorePattern and varsIgnorePattern to ESLint config
- Allow underscore-prefixed unused parameters (convention)
- Improve code readability and maintainability

Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
- Update comment to be more accurate about calling reject handlers
- Improve code documentation clarity

Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 8, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • sparrow.cloudflare.com
    • Triggering command: /usr/local/bin/node /usr/local/bin/node --no-warnings --experimental-vm-modules /home/REDACTED/work/my-code/my-code/node_modules/wrangler/wrangler-dist/cli.js types --env-interface CloudflareEnv cloudflare-env.d.ts (dns block)
    • Triggering command: /usr/local/bin/node /usr/local/bin/node --no-warnings --experimental-vm-modules /home/REDACTED/work/my-code/my-code/node_modules/wrangler/wrangler-dist/cli.js types --env-interface CloudflareEnv cloudflare-env.d.ts r (dns block)
    • Triggering command: /usr/local/bin/node /usr/local/bin/node --no-warnings --experimental-vm-modules /home/REDACTED/work/my-code/my-code/node_modules/wrangler/wrangler-dist/cli.js types --env-interface CloudflareEnv cloudflare-env.d.ts user.name (dns block)
  • workers.cloudflare.com
    • Triggering command: /usr/local/bin/node node /home/REDACTED/.npm/_npx/8b377f6eec906bc4/node_modules/.bin/next build (dns block)
    • Triggering command: /usr/local/bin/node /usr/local/bin/node --no-warnings --experimental-vm-modules /home/REDACTED/work/my-code/my-code/node_modules/wrangler/wrangler-dist/cli.js types --env-interface CloudflareEnv cloudflare-env.d.ts (dns block)
    • Triggering command: /usr/local/bin/node node /home/REDACTED/work/my-code/my-code/node_modules/.bin/next lint {} (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI requested a review from na-trium-144 December 8, 2025 12:20
@na-trium-144 na-trium-144 marked this pull request as ready for review December 8, 2025 12:26
@na-trium-144 na-trium-144 marked this pull request as draft December 8, 2025 12:32
@na-trium-144 na-trium-144 force-pushed the copilot/refactor-message-processing-comlink branch from 17fc327 to 1e69bec Compare December 9, 2025 03:42
@na-trium-144 na-trium-144 marked this pull request as ready for review December 9, 2025 03:43
@na-trium-144 na-trium-144 merged commit a494a6b into main Dec 9, 2025
3 checks passed
@na-trium-144 na-trium-144 deleted the copilot/refactor-message-processing-comlink branch December 9, 2025 17:21
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.

app/terminal/worker/runtime.tsx のメッセージ処理の実装を既存ライブラリ(comlink)を使って書き直す

2 participants