Skip to content

Use real Wrangler translation runtime probe#619

Merged
riderx merged 3 commits into
mainfrom
codex/translation-real-runtime-probe
May 1, 2026
Merged

Use real Wrangler translation runtime probe#619
riderx merged 3 commits into
mainfrom
codex/translation-real-runtime-probe

Conversation

@riderx
Copy link
Copy Markdown
Member

@riderx riderx commented May 1, 2026

Summary

  • switch the translation worker to the JSON-mode Llama model and send a JSON Schema response_format for translation batches
  • remove the mocked translation unit test and add a Wrangler remote probe that uses Workers AI, Cache API, and the development R2 bucket
  • run the real translation runtime probe in the translation deploy workflow before deployment

Verification

  • bun run ci:verify:translation
  • cd apps/translation-worker && bunx wrangler deploy --dry-run
  • cd apps/translation-worker && bunx wrangler deploy --dry-run -c wrangler.real-test.jsonc
  • bun run verify:real-translation

Summary by CodeRabbit

  • New Features

    • Added a pre-deploy real-translation verification step and a runtime diagnostic route to exercise real translation behavior.
    • Introduced strict JSON-schema batch translation mode with protected-token preservation and timeouts.
  • Bug Fixes

    • Improved error handling, logging, and graceful fallback from JSON-batch to per-text translation.
  • Tests

    • Removed in-repo translation unit tests; added a real-environment verification script.
  • Chores

    • Updated default translation model and CI verification scripts/configuration; added a real-test deployment config.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 1, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c3510b97-bcb1-40a4-8a7e-f35ba360040a

📥 Commits

Reviewing files that changed from the base of the PR and between 3bb9e72 and 5b47042.

📒 Files selected for processing (2)
  • apps/translation-worker/scripts/verify-real-ai.ts
  • apps/translation-worker/src/index.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/translation-worker/scripts/verify-real-ai.ts

📝 Walkthrough

Walkthrough

Adds a real-model verification flow and JSON-mode translation to the translation worker: new verification script runs wrangler dev against a test endpoint that exercises real AI/Cache/R2 bindings; CI/workflow and package scripts call this verifier; worker defaults, batch JSON handling, and a runtime test route are updated.

Changes

Cohort / File(s) Summary
CI / Workflow & Root Scripts
\.github/workflows/deploy-translation.yml, package.json
Insert pre-deploy verification step that runs verify:real-translation; add verify:real-translation script in root package.json; remove bun run test from ci:verify:translation.
Translation Worker Package Scripts
apps/translation-worker/package.json
test now delegates to test:real; adds test:real which runs scripts/verify-real-ai.ts, changing what bun test invokes.
Real Translation Verification Script
apps/translation-worker/scripts/verify-real-ai.ts
New script that creates/ensures an R2 bucket, starts wrangler dev on a free port, captures logs, polls /__translation-test__/real-runtime, validates model/cache/R2 flags, translations array, protected tokens, enforces timeouts, and exits nonzero with logs on failure.
Translation Worker Core Logic
apps/translation-worker/src/index.ts
Add optional response_format to AiBinding.run; switch default model and cache version; implement strict JSON-mode batch translation with schema validation and protected-token restore; fall back to per-text translation on JSON-mode failure; add /__translation-test__/real-runtime route gated by TRANSLATION_TEST_MODE=1.
Wrangler Configs
apps/translation-worker/wrangler.jsonc, apps/translation-worker/wrangler.real-test.jsonc
Update TRANSLATION_MODEL default/dev to @cf/meta/llama-3.1-8b-instruct-fast and set account_id; add wrangler.real-test.jsonc with AI and R2 bindings and TRANSLATION_TEST_MODE=1.
Tests
apps/translation-worker/test/index.test.ts
Remove the existing unit test file that mocked queue/caching behavior; replaced by the real-run verification approach.

Sequence Diagram(s)

sequenceDiagram
    participant GHA as GitHub Actions
    participant Script as verify-real-ai.ts
    participant R2 as R2 Bucket
    participant Wrangler as wrangler dev
    participant Worker as Translation Worker
    participant AI as AI Binding/API
    participant Cache as Cache API
    participant Probe as /__translation-test__/real-runtime

    GHA->>Script: run verify:real-translation
    Script->>R2: wrangler r2 bucket create
    R2-->>Script: bucket ready
    Script->>Wrangler: start wrangler dev (free localhost port)
    Wrangler-->>Script: dev server running + logs
    Script->>Probe: GET /__translation-test__/real-runtime
    Probe->>Worker: invoke test handler
    Worker->>AI: request with response_format (JSON-mode)
    AI-->>Worker: JSON { translations: [...] }
    Worker->>Cache: write diagnostics / results
    Worker->>R2: store translations
    Worker-->>Probe: return diagnostic JSON (model, flags, translations)
    Probe-->>Script: payload received
    Script->>Script: validate model, counts, tokens, flags
    Script->>Wrangler: terminate dev
    Script-->>GHA: exit (success/failure)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

🐇
I hopped to spin up buckets and wrangler light,
JSON rows align, translations take flight,
Llama hums softly, tokens kept tight,
A rabbit checks the run — logs gleam through the night.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly corresponds to the main objective of adding a real Wrangler translation runtime probe and removing mocked tests in favor of real runtime verification.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/translation-real-runtime-probe

Review rate limit: 3/5 reviews remaining, refill in 16 minutes and 40 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

@socket-security
Copy link
Copy Markdown

socket-security Bot commented May 1, 2026

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Added@​supabase/​supabase-js@​2.105.171100100100100
Added@​jsr/​std__semver@​1.0.8100100100100100

View full report

@riderx riderx marked this pull request as ready for review May 1, 2026 20:36
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/translation-worker/scripts/verify-real-ai.ts`:
- Around line 105-110: The fetchProbe function currently calls fetch(url)
without a per-request timeout, so a single request can hang beyond the outer
TIMEOUT_MS retry loop; update fetchProbe to create an AbortController for each
fetch, start a per-request timer (e.g., requestTimeoutMs < TIMEOUT_MS or
configurable), pass controller.signal to fetch, and clear the timer on
completion, and handle the abort/timeout error path so the retry loop can
continue; reference the fetchProbe function and TIMEOUT_MS constant when making
the change.
- Around line 91-102: The probe currently only fails when both samples remain
untranslated and misses protected tokens; update the untranslated check in the
translations mapping block to fail if any translation equals its corresponding
SOURCE_TEXTS entry (i.e., change the && to a per-item comparison or use
Array.some comparing translations[i].trim() === SOURCE_TEXTS[i]) so a partially
untranslated batch is rejected, and extend the protected-token loop (the array
used when building joined from translations) to include "npm" and "bun"
alongside the existing tokens to ensure those tokens aren't dropped or
translated.

In `@apps/translation-worker/src/index.ts`:
- Around line 1833-1857: The probeRuntimeStorage function currently may leave
temporary cache entries and R2 objects when an exception occurs; wrap the probe
logic in a try/finally (or use nested try/finally blocks) so that both the cache
probeKey and the R2 key are always deleted regardless of errors. Specifically,
in probeRuntimeStorage generate nonce and construct cacheProbeKey and r2Key as
before, perform the cache put/match and R2 put/get checks inside try, and then
in the finally block call caches.default.delete(cacheProbeKey) and
env.TRANSLATION_STORE.delete(r2Key) (guarding against undefined variables if
needed) so cleanup always runs even when the cache or R2 probe throws.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 147788f1-d765-4887-8cdf-cf91b187e831

📥 Commits

Reviewing files that changed from the base of the PR and between c2bb57b and 7b5b1c0.

📒 Files selected for processing (8)
  • .github/workflows/deploy-translation.yml
  • apps/translation-worker/package.json
  • apps/translation-worker/scripts/verify-real-ai.ts
  • apps/translation-worker/src/index.ts
  • apps/translation-worker/test/index.test.ts
  • apps/translation-worker/wrangler.jsonc
  • apps/translation-worker/wrangler.real-test.jsonc
  • package.json
💤 Files with no reviewable changes (1)
  • apps/translation-worker/test/index.test.ts

Comment thread apps/translation-worker/scripts/verify-real-ai.ts
Comment thread apps/translation-worker/scripts/verify-real-ai.ts Outdated
Comment thread apps/translation-worker/src/index.ts
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/translation-worker/scripts/verify-real-ai.ts`:
- Around line 61-81: runWrangler() can hang because it awaits child.exited with
no timeout; modify runWrangler to race the process completion against a timeout
using Promise.race() (the existing sleep() and REQUEST_TIMEOUT_MS should be
reused) so the function returns/throws when the timeout elapses. Specifically,
keep spawning the child and collecting stdout/stderr as before, but wrap
awaiting child.exited (and the stdout/stderr reads) in a Promise.race with a
timeout promise that rejects or returns a timed-out result consistent with how
the main probe loop/cleanup handles timeouts; ensure ensureDevelopmentBucket()
receives a non-hanging failure when wrangler exceeds REQUEST_TIMEOUT_MS so the
outer deadline can handle it.

In `@apps/translation-worker/src/index.ts`:
- Around line 992-1019: The protectTranslationTokens function currently replaces
protected tokens with placeholders but restore(translated) blindly reinserts
only exact placeholders, allowing the model to drop or rewrite tokens and the
worker to accept bad translations; update restore (in protectTranslationTokens)
to validate that every original protected token occurrence is present in the
final restored string and if any are missing or altered, treat the translation
as invalid (e.g., throw an error or return a failure signal) so callers can
retry/fail the translation; implement the check by comparing counts or positions
of each original token versus occurrences in the restored output after
re-insertion and fail when any token’s expected occurrences are not preserved.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6022f0b8-b481-48d3-baaf-8b1a2c75a2ec

📥 Commits

Reviewing files that changed from the base of the PR and between 7b5b1c0 and 3bb9e72.

📒 Files selected for processing (2)
  • apps/translation-worker/scripts/verify-real-ai.ts
  • apps/translation-worker/src/index.ts

Comment thread apps/translation-worker/scripts/verify-real-ai.ts Outdated
Comment thread apps/translation-worker/src/index.ts
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 1, 2026

@riderx riderx merged commit d9e998b into main May 1, 2026
10 checks passed
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