Add remote workers and AMD host support for 2.4.0 #16

Merged
nimmo merged 1 commit from feature/remote-worker into dev 2026-10-05 21:31:04 +01:00
Owner

Trusted remote hosts can now run Redview jobs against shared media paths while the main server owns SQLite. The opt-in worker API uses a dedicated key, atomic command batches, binary-vector transport and replay receipts; local and remote workers preserve job ownership and recovery. Includes standalone NixOS worker services, setup documentation and Nova ROCm validation. Workers tolerate busy claims, and purge takes write locks before reads with bounded atomic-write retries.

Validation: 185 Node tests, 60 Python tests, Nix package build and installation checks, version policy checks, and main/remote NixOS module evaluations pass. Nova completed real AMD tagging/vectorization and simultaneous local/remote queue work against a disposable API. Development target is 2.4.0-dev4.

Candidate acceptance remains open: production shared mounts and supervised provisioning, host/network loss and mount outages, representative throughput, Lyra contention observation and purge job 92 reconciliation. Deduplication and earlier vectorization failures still need diagnosis. This PR prepares release-candidate testing; it does not publish or deploy a stable release.

Trusted remote hosts can now run Redview jobs against shared media paths while the main server owns SQLite. The opt-in worker API uses a dedicated key, atomic command batches, binary-vector transport and replay receipts; local and remote workers preserve job ownership and recovery. Includes standalone NixOS worker services, setup documentation and Nova ROCm validation. Workers tolerate busy claims, and purge takes write locks before reads with bounded atomic-write retries. Validation: 185 Node tests, 60 Python tests, Nix package build and installation checks, version policy checks, and main/remote NixOS module evaluations pass. Nova completed real AMD tagging/vectorization and simultaneous local/remote queue work against a disposable API. Development target is 2.4.0-dev4. Candidate acceptance remains open: production shared mounts and supervised provisioning, host/network loss and mount outages, representative throughput, Lyra contention observation and purge job 92 reconciliation. Deduplication and earlier vectorization failures still need diagnosis. This PR prepares release-candidate testing; it does not publish or deploy a stable release.
Add remote workers, AMD validation and SQLite contention recovery
All checks were successful
Build and Push Attic Cache / build (pull_request) Successful in 1m1s
Branch and package checks / check (pull_request) Successful in 5s
AI pull-request review / review (pull_request) Successful in 4m3s
c4bfbd7dd4
Author
Owner

Automated code review

Reviewed commit: c4bfbd7dd40b1552077b2fa1d669c44b0d33dbfb

Verdict: Requires further work

Resolve the blocking or important findings and investigate failed deterministic checks before merging.

Overall assessment

This PR introduces remote worker support for Redview v2, allowing trusted workers on separate hosts to execute database commands against a central SQLite instance via an HTTP API. It also includes AMD GPU validation, fixes for SQLite contention (busy claims), and updates the version to 2.4.0-dev4. The implementation uses a dedicated worker key, atomic command batches with retry receipts, and binary-vector transport.

Blocking findings

None.

Important findings

  • Potential SQL Injection in Remote Worker Protocol (src/worker-protocol.ts:75; high confidence)
    The createCommandExecutor function executes SQL statements provided by the remote worker: const prepared = db.prepare(statement.sql); ... prepared.run(...args);. While arguments are parameterized via ...args, the SQL string itself is interpolated directly into the prepared statement. The validation regex /^\s*(SELECT|WITH|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|ALTER)\b/i allows a wide range of statements.
    Impact: If the worker key is compromised, an attacker could potentially execute arbitrary SQL commands on the central database, including schema modifications or data exfiltration, beyond just job-related operations. The protocol currently trusts the worker host implicitly.
    Suggested fix: Restrict the allowed SQL statements to only those necessary for job processing (e.g., SELECT, INSERT, UPDATE, DELETE) and explicitly block DDL (CREATE, DROP, ALTER) unless absolutely required. Consider whitelisting specific table names or prefixes if possible. Ensure that the worker key is treated with the same security as a database superuser.

Suggestions

  • Race Condition in Receipt Pruning (src/worker-protocol.ts:95; medium confidence)
    The receipt pruning logic if (Date.now() - lastPruned > 60_000) is not atomic. If multiple requests trigger this check simultaneously, they might both prune, or one might prune while another is inserting a receipt that is then deleted.
    Impact: Receipts might be deleted prematurely, leading to duplicate writes if the worker retries a command after its receipt was pruned but before it received the response (or if the response was lost).
    Suggested fix: Use a database-level lock or a more robust mechanism for pruning receipts. For example, prune within the transaction itself or use a separate scheduled task with proper locking.

  • SharedArrayBuffer Synchronization Complexity (src/remote-worker.ts:50; medium confidence)
    The remote worker uses SharedArrayBuffer and Atomics for communication between the main thread and the worker thread. This is complex and error-prone. The code relies on precise timing and state management.
    Impact: If the synchronization logic fails (e.g., timeout, incorrect status codes), it could lead to deadlocks or lost commands. The complexity increases the risk of subtle bugs.
    Suggested fix: Consider using a simpler IPC mechanism if performance allows, such as postMessage with JSON serialization, which is already used for other parts of the worker. If SharedArrayBuffer is necessary for performance, add extensive logging and error handling to debug synchronization issues.

  • Missing Error Handling in Remote Worker Thread (src/remote-worker-thread.ts:15; medium confidence)
    The runWorker function is called with a RemoteDb instance. If an error occurs during job execution that is not caught by the worker's internal error handling, it might not be properly reported or handled.
    Impact: Jobs might fail silently or leave the database in an inconsistent state if errors are not propagated correctly through the remote protocol.
    Suggested fix: Ensure that all errors from runWorker are caught and logged. Consider adding a global error handler for the worker thread to catch unhandled exceptions.

  • Documentation Clarity on Security Model (REMOTE-WORKERS.md:10; high confidence)
    The documentation states: 'The worker key grants privileged database access to trusted workers; keep it out of Nix expressions and the Nix store.'
    Impact: Users might not fully understand the implications of the worker key. It is effectively a database superuser key.
    Suggested fix: Explicitly state that the worker key provides full database access and should be treated with the same security as the database itself. Provide clear guidelines on how to secure the key in production environments.

Tests and validation

  • package-checks: success

Questions

  • Why was SharedArrayBuffer chosen for IPC between the main thread and worker thread instead of postMessage? Is it necessary for performance, or could it be simplified?
  • How does the system handle network partitions where the remote worker loses connection to the main server? Are there any mechanisms to detect and recover from such states?
  • What is the maximum number of concurrent remote workers supported? Are there any limits on the total number of statements per batch or total command size that might need tuning for large-scale deployments?
  • How are SQLite contention issues handled in the remote worker protocol? The PR mentions 'bounded atomic-write retries', but how does this interact with the network latency?

Review limitations

  • The review is based on the provided diff and documentation. Live testing of the remote worker functionality, especially under load and with real network conditions, was not performed.
  • The security implications of the SQL injection potential were analyzed statically; actual exploitation would require a compromised worker key.
  • The performance impact of the SharedArrayBuffer synchronization was not benchmarked.
  • The NixOS module configuration was reviewed for syntax and logic but not tested on a live system.

Diff coverage

  • AMD-WORKERS.md: reviewed — included in a context-limited batch
  • CHANGELOG.md: reviewed — included in a context-limited batch
  • ENCODER.md: reviewed — included in a context-limited batch
  • README.md: reviewed — included in a context-limited batch
  • REMOTE-WORKERS.md: reviewed — included in a context-limited batch
  • REQUIREMENTS-TRACE.md: reviewed — included in a context-limited batch
  • REQUIREMENTS.md: reviewed — included in a context-limited batch
  • VERSION: reviewed — included in a context-limited batch
  • VERSIONING.md: reviewed — included in a context-limited batch
  • acceptance/amd-remote-worker-2026-10-05.md: reviewed — included in a context-limited batch
  • flake.nix: reviewed — included in a context-limited batch
  • nix/package.nix: reviewed — included in a context-limited batch
  • nix/redview-v2.nix: reviewed — included in a context-limited batch
  • nix/remote-worker.nix: reviewed — included in a context-limited batch
  • nix/tests/remote-worker-module.nix: reviewed — included in a context-limited batch
  • package.json: reviewed — included in a context-limited batch
  • src/app.ts: reviewed — included in a context-limited batch
  • src/auth.ts: reviewed — included in a context-limited batch
  • src/jobs.ts: reviewed — included in a context-limited batch
  • src/remote-db.ts: reviewed — included in a context-limited batch
  • src/remote-worker-thread.ts: reviewed — included in a context-limited batch
  • src/remote-worker.ts: reviewed — included in a context-limited batch
  • src/server.ts: reviewed — included in a context-limited batch
  • src/sqlite-contention.ts: reviewed — included in a context-limited batch
  • src/worker-protocol.ts: reviewed — included in a context-limited batch
  • src/worker.ts: reviewed — included in a context-limited batch
  • tagger-amd.py: reviewed — included in a context-limited batch
  • tests/remote-db.test.mjs: reviewed — included in a context-limited batch
  • tests/remote-protocol.test.mjs: reviewed — included in a context-limited batch
  • tests/remote-worker.test.mjs: reviewed — included in a context-limited batch
Review metadata
  • Reviewer: forgejo-ai-review v1
  • Model: qwen3.6:35b-a3b-q8_0
  • Guidance loaded: .forgejo/ai-review.md, .forgejo/ai-review.yml, AGENTS.md, README.md
  • Commits considered: 1
  • Previous automated review: none found
  • Findings: 5
<!-- forgejo-ai-review:v1 --> ## Automated code review Reviewed commit: `c4bfbd7dd40b1552077b2fa1d669c44b0d33dbfb` ## Verdict: Requires further work Resolve the blocking or important findings and investigate failed deterministic checks before merging. ### Overall assessment This PR introduces remote worker support for Redview v2, allowing trusted workers on separate hosts to execute database commands against a central SQLite instance via an HTTP API. It also includes AMD GPU validation, fixes for SQLite contention (busy claims), and updates the version to 2.4.0-dev4. The implementation uses a dedicated worker key, atomic command batches with retry receipts, and binary-vector transport. ### Blocking findings None. ### Important findings - **Potential SQL Injection in Remote Worker Protocol** (src/worker-protocol.ts:75; high confidence) The `createCommandExecutor` function executes SQL statements provided by the remote worker: `const prepared = db.prepare(statement.sql); ... prepared.run(...args);`. While arguments are parameterized via `...args`, the SQL string itself is interpolated directly into the prepared statement. The validation regex `/^\s*(SELECT|WITH|INSERT|UPDATE|DELETE|REPLACE|CREATE|DROP|ALTER)\b/i` allows a wide range of statements. Impact: If the worker key is compromised, an attacker could potentially execute arbitrary SQL commands on the central database, including schema modifications or data exfiltration, beyond just job-related operations. The protocol currently trusts the worker host implicitly. Suggested fix: Restrict the allowed SQL statements to only those necessary for job processing (e.g., SELECT, INSERT, UPDATE, DELETE) and explicitly block DDL (CREATE, DROP, ALTER) unless absolutely required. Consider whitelisting specific table names or prefixes if possible. Ensure that the worker key is treated with the same security as a database superuser. ### Suggestions - **Race Condition in Receipt Pruning** (src/worker-protocol.ts:95; medium confidence) The receipt pruning logic `if (Date.now() - lastPruned > 60_000)` is not atomic. If multiple requests trigger this check simultaneously, they might both prune, or one might prune while another is inserting a receipt that is then deleted. Impact: Receipts might be deleted prematurely, leading to duplicate writes if the worker retries a command after its receipt was pruned but before it received the response (or if the response was lost). Suggested fix: Use a database-level lock or a more robust mechanism for pruning receipts. For example, prune within the transaction itself or use a separate scheduled task with proper locking. - **SharedArrayBuffer Synchronization Complexity** (src/remote-worker.ts:50; medium confidence) The remote worker uses `SharedArrayBuffer` and `Atomics` for communication between the main thread and the worker thread. This is complex and error-prone. The code relies on precise timing and state management. Impact: If the synchronization logic fails (e.g., timeout, incorrect status codes), it could lead to deadlocks or lost commands. The complexity increases the risk of subtle bugs. Suggested fix: Consider using a simpler IPC mechanism if performance allows, such as `postMessage` with JSON serialization, which is already used for other parts of the worker. If `SharedArrayBuffer` is necessary for performance, add extensive logging and error handling to debug synchronization issues. - **Missing Error Handling in Remote Worker Thread** (src/remote-worker-thread.ts:15; medium confidence) The `runWorker` function is called with a `RemoteDb` instance. If an error occurs during job execution that is not caught by the worker's internal error handling, it might not be properly reported or handled. Impact: Jobs might fail silently or leave the database in an inconsistent state if errors are not propagated correctly through the remote protocol. Suggested fix: Ensure that all errors from `runWorker` are caught and logged. Consider adding a global error handler for the worker thread to catch unhandled exceptions. - **Documentation Clarity on Security Model** (REMOTE-WORKERS.md:10; high confidence) The documentation states: 'The worker key grants privileged database access to trusted workers; keep it out of Nix expressions and the Nix store.' Impact: Users might not fully understand the implications of the worker key. It is effectively a database superuser key. Suggested fix: Explicitly state that the worker key provides full database access and should be treated with the same security as the database itself. Provide clear guidelines on how to secure the key in production environments. ### Tests and validation - package-checks: **success** ### Questions - Why was `SharedArrayBuffer` chosen for IPC between the main thread and worker thread instead of `postMessage`? Is it necessary for performance, or could it be simplified? - How does the system handle network partitions where the remote worker loses connection to the main server? Are there any mechanisms to detect and recover from such states? - What is the maximum number of concurrent remote workers supported? Are there any limits on the total number of statements per batch or total command size that might need tuning for large-scale deployments? - How are SQLite contention issues handled in the remote worker protocol? The PR mentions 'bounded atomic-write retries', but how does this interact with the network latency? ### Review limitations - The review is based on the provided diff and documentation. Live testing of the remote worker functionality, especially under load and with real network conditions, was not performed. - The security implications of the SQL injection potential were analyzed statically; actual exploitation would require a compromised worker key. - The performance impact of the `SharedArrayBuffer` synchronization was not benchmarked. - The NixOS module configuration was reviewed for syntax and logic but not tested on a live system. ### Diff coverage - `AMD-WORKERS.md`: **reviewed** — included in a context-limited batch - `CHANGELOG.md`: **reviewed** — included in a context-limited batch - `ENCODER.md`: **reviewed** — included in a context-limited batch - `README.md`: **reviewed** — included in a context-limited batch - `REMOTE-WORKERS.md`: **reviewed** — included in a context-limited batch - `REQUIREMENTS-TRACE.md`: **reviewed** — included in a context-limited batch - `REQUIREMENTS.md`: **reviewed** — included in a context-limited batch - `VERSION`: **reviewed** — included in a context-limited batch - `VERSIONING.md`: **reviewed** — included in a context-limited batch - `acceptance/amd-remote-worker-2026-10-05.md`: **reviewed** — included in a context-limited batch - `flake.nix`: **reviewed** — included in a context-limited batch - `nix/package.nix`: **reviewed** — included in a context-limited batch - `nix/redview-v2.nix`: **reviewed** — included in a context-limited batch - `nix/remote-worker.nix`: **reviewed** — included in a context-limited batch - `nix/tests/remote-worker-module.nix`: **reviewed** — included in a context-limited batch - `package.json`: **reviewed** — included in a context-limited batch - `src/app.ts`: **reviewed** — included in a context-limited batch - `src/auth.ts`: **reviewed** — included in a context-limited batch - `src/jobs.ts`: **reviewed** — included in a context-limited batch - `src/remote-db.ts`: **reviewed** — included in a context-limited batch - `src/remote-worker-thread.ts`: **reviewed** — included in a context-limited batch - `src/remote-worker.ts`: **reviewed** — included in a context-limited batch - `src/server.ts`: **reviewed** — included in a context-limited batch - `src/sqlite-contention.ts`: **reviewed** — included in a context-limited batch - `src/worker-protocol.ts`: **reviewed** — included in a context-limited batch - `src/worker.ts`: **reviewed** — included in a context-limited batch - `tagger-amd.py`: **reviewed** — included in a context-limited batch - `tests/remote-db.test.mjs`: **reviewed** — included in a context-limited batch - `tests/remote-protocol.test.mjs`: **reviewed** — included in a context-limited batch - `tests/remote-worker.test.mjs`: **reviewed** — included in a context-limited batch <details><summary>Review metadata</summary> - Reviewer: forgejo-ai-review v1 - Model: qwen3.6:35b-a3b-q8_0 - Guidance loaded: .forgejo/ai-review.md, .forgejo/ai-review.yml, AGENTS.md, README.md - Commits considered: 1 - Previous automated review: none found - Findings: 5 </details>
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
nimmo/redview!16
No description provided.