Skip to content

fix(automations): a legacy automation on a twin project refuses instead of running on a guessed host - #31234

Merged
nwparker merged 3 commits into
mainfrom
nwparker/legacy-automation-twin-host
Oct 20, 2026
Merged

nwparker merged 3 commits into
mainfrom
nwparker/legacy-automation-twin-host

Conversation

@nwparker

@nwparker nwparker commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​172 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​172
Prod 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​139 $\color{#cf222e}{\Huge{\mathbf{−}}}$​15 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​124

ELI5

Old automations only remember a project id. If that project is now added on two machines (say this laptop and an SSH server), Orca used to guess which machine to run on. It now refuses with a message telling you to open the automation and pick the host. If only one machine has the project, Orca writes that machine down the first time the automation runs, so a second copy added later can't make it ambiguous.

What Changed

Before:

  • An automation saved before Orca stored a run host (no runContext) looked up its project with a host-blind getRepo(id). With the same repo id on two hosts it ran on whichever row was listed first. For example, a local automation ran in the SSH copy's folder, or was refused with the unhelpful "This automation has no host to run on."
  • On startup, the one-time backfill that adds runContext to old records also picked a twin row at random (the last one in a by-id map).

After:

  • Twin repo id: the run is skipped with "This automation was saved before Orca tracked which host it runs on, and its project is on more than one host. Open the automation and pick the host it should run on." The startup backfill no longer guesses a host for a twin, so the record stays host-less until the user picks one.
  • Exactly one host: before the run is dispatched (manual run or scheduler tick), Orca writes runContext to the stored automation once. This goes through a new write-once store operation, stampLegacyAutomationRunContext, and needs no new schema. It only does this when the captured target would run on the same row in the same folder as today. An existing-workspace record whose folder is not the repo root is left as it is. The capture happens before the dispatch snapshot, so the first run still launches instead of being refused as "changed".
  • Paths that only report a refusal (recordRefusedRun) and prechecks never write.

Mechanism: resolveAutomationRunTarget's legacy branch (src/main/automations/run-target-resolution.ts) now counts rows with the id before doing anything else. The new captureLegacyRunTarget is called from AutomationService.runNow and evaluateAutomation. backfillLegacyAutomationContexts maps a twin id to no row.

Why

  • Refusing is the only safe answer for a twin: the record has nothing that names a host, and guessing runs an agent on the wrong machine.
  • Capturing the host once makes the record behave like a modern one, so it stays correct when a twin appears later.
  • Alternatives considered:
    • Capturing inside the resolver. Rejected because it changed the definition that dispatch rechecks, which dropped the first run.
    • Capturing at load. Rejected because the load-time backfill already does this for records it can resolve. The remaining legacy records are the ones it couldn't resolve, or ones whose repo arrived later.
  • Mixed versions: runContext is an existing field that older clients and hosts already read, so a captured record works on older peers unchanged.

Linked Issue

N/A (internal follow-up).

Visual Proof

N/A: main-process scheduling only. The only visible change is the text of a skipped run.

Testing

  • src/main/persistence-legacy-automation-twin-host.test.ts runs against a real store (SQLite profile state) and AutomationService:

    • the load backfill leaves a twin host-less;
    • both twin orderings refuse with the pick-the-host message (on main, one ordering ran in the SSH row's folder and the other refused with "no host to run on");
    • a single-host record captures its host on runNow, still dispatches, and keeps the capture after a reload;
    • recordRefusedRun never captures;
    • an existing-workspace record whose folder is not the repo root stays uncaptured and keeps its folder.
      The first three fail on main.
  • src/main/automations folder (36 files) passes single-worker.

  • Full static-analysis job run locally.

  • Not verified live on a real machine: this is a scheduling decision in the main process, with no SSH or terminal I/O involved.

  • Automated tests added/updated

Review

Codex (gpt-6.1-sol, high) and an independent Opus review.

Agent skill upstream boundary

  • Not applicable

Notes

  • Security: no new inputs.
  • Cross-platform: no path or platform code.
  • SSH: covers local and SSH twins.
  • Backwards compatibility: existing field only.
  • Performance: one getRepos() filter per legacy record per tick.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • N/A visual proof with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote impact considered
  • Lint, typecheck and related tests pass locally

@coderabbitai

coderabbitai Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

📝 Walkthrough

Legacy automation resolution now rejects ambiguous repository IDs and applies target refusal and availability checks. The run-context capture path validates the resolved workspace path and execution host before persisting a context. Immediate and scheduled automation flows capture legacy targets before dispatch processing. Persistence operations add a context only when the automation exists and has none. Tests cover ambiguous host selection, context persistence, refused runs, and workspace-path preservation.

Priority: ⬇️ Low

Merge Risk: 🟡 Moderate · up to c5a16

Legacy automations whose project lives on a remote runtime host can still run from this client when remote scheduling is disabled, because the new legacy resolution path skips that refusal. Automations whose project ID exists on two hosts also keep a permanently empty source context, even after one host is removed. Address or explicitly accept both issues before merging.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 30.77% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 13 functions across 7 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: legacy automations on twin projects now refuse execution instead of selecting a guessed host.
Description check Passed The description is complete and relevant. It explains the user-facing behavior, implementation, rationale, testing, compatibility, and scope. It documents the internal issue exception and marks visual…
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.
Full details: Description check

Explanation

The description is complete and relevant. It explains the user-facing behavior, implementation, rationale, testing, compatibility, and scope. It documents the internal issue exception and marks visual proof as not applicable. It does not confirm manual testing or explicitly report pnpm test and pnpm build, but these are non-critical omissions.

✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f9afbc8f-149a-44e3-ad6f-c24273b8cd29
📥 Commits

Reviewing files that changed from the base of the PR and between 695ac88 and 1017ef0.

📒 Files selected for processing: 7
  • src/main/automations/run-target-resolution.test.ts
  • src/main/automations/run-target-resolution.ts
  • src/main/automations/service.ts
  • src/main/persistence-legacy-automation-twin-host.test.ts
  • src/main/persistence/loading-store/automation-persistence.ts
  • src/main/persistence/scheduling-automations/automation-context-migration.ts
  • src/main/persistence/scheduling-automations/automation-definition-operations.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

Comment on lines +188 to +192
// Why: a repo id on two hosts names no host, so the load leaves it for dispatch to refuse.
const reposById = new Map<string, Repo | undefined>()
for (const repo of state.repos ?? []) {
reposById.set(repo.id, reposById.has(repo.id) ? undefined : repo)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win

🔎 Supported by static analysis

🏁 Script executed:

#!/bin/bash
# Find consumers and writers of Automation.sourceContext to see whether a null value is re-derived later.
rg -nP --type=ts -C3 '\bsourceContext\b' src/main/automations src/main/persistence src/shared | rg -v '\.test\.ts' | head -n 120

Repository: stablyai/orca

Length of output: 11894


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- automation-context-migration.ts ---'
nl -ba src/main/persistence/scheduling-automations/automation-context-migration.ts | sed -n '100,235p'
printf '%s\n' '--- automation-definition-operations.ts ---'
nl -ba src/main/persistence/scheduling-automations/automation-definition-operations.ts | sed -n '1,225p'
printf '%s\n' '--- related selector/run-target references ---'
rg -n -F --type=ts -e 'selectorMoveRequested' -e 'captureLegacyRunTarget' -e 'resolveAutomationRunTarget' -e 'stampLegacyAutomationRunContext' src/main src/shared || test "$?" -eq 1
printf '%s\n' '--- relevant tests ---'
rg -n -F --type=ts -e 'sourceContext' -e 'twin' -e 'duplicate' -e 'ambiguous' src/main/persistence-legacy-automation-twin-host.test.ts src/main/automations src/main/persistence/scheduling-automations || test "$?" -eq 1

Repository: stablyai/orca

Length of output: 29207


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- run-target-resolution.ts ---'
nl -ba src/main/automations/run-target-resolution.ts | sed -n '1,180p'
printf '%s\n' '--- persistence stamp operation ---'
nl -ba src/main/persistence/loading-store/automation-persistence.ts | sed -n '135,175p'
nl -ba src/main/persistence/scheduling-automations/automation-definition-operations.ts | sed -n '225,290p'
printf '%s\n' '--- source-context definitions ---'
rg -n -F --type=ts -e 'function buildTaskSourceContextFromRepo' -e 'buildTaskSourceContextFromRepo' -e 'type TaskSourceContext' -e 'interface TaskSourceContext' src/shared src/main | head -n 80
printf '%s\n' '--- updateAutomation callers and host selection fields ---'
rg -n -P --type=ts 'updateAutomation\(|projectId:|sourceContext:' src/main src/renderer src/shared | rg -v '\.test\.ts' | head -n 220
printf '%s\n' '--- twin-host test ---'
nl -ba src/main/persistence-legacy-automation-twin-host.test.ts | sed -n '1,210p'

Repository: stablyai/orca

Length of output: 44759


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- automation files ---'
rg --files src | rg 'automation|Automation' | head -n 160
printf '%s\n' '--- update IPC and renderer references ---'
rg -n -P --type=ts --type=tsx 'updateAutomation|automations:update|automation.*update|projectHostSetupId|hostId' src/main/ipc src/renderer/src | rg -i 'automation|schedule|editor|host|project' | head -n 260
printf '%s\n' '--- task source context implementation ---'
nl -ba src/shared/task-source-context.ts | sed -n '1,145p'
printf '%s\n' '--- automation RPC contract ---'
nl -ba src/shared/rpc-contract/automation-params.ts | sed -n '1,145p'

Repository: stablyai/orca

Length of output: 19593


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- automation IPC ---'
nl -ba src/main/ipc/automations.ts | sed -n '1,280p'
printf '%s\n' '--- all updateAutomation call sites ---'
rg -n -F -g '*.ts' -g '*.tsx' -- 'updateAutomation' src | head -n 240
printf '%s\n' '--- automation UI files and host terms ---'
rg --files src/renderer | rg -i 'automation|schedule' | head -n 240
rg -n -i -F -g '*.ts' -g '*.tsx' -e 'pick the host' -e 'hostId' -e 'projectHostSetupId' -e 'automation' src/renderer/src | rg -i 'automation|schedule|host' | head -n 260
printf '%s\n' '--- sourceContext consumers in automation paths ---'
rg -n -F -g '*.ts' -g '*.tsx' -- 'sourceContext' src/main/automations src/main/persistence/scheduling-automations src/renderer/src | rg -i 'automation|run|source' | head -n 220

Repository: stablyai/orca

Length of output: 42103


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- renderer automation save operations ---'
nl -ba src/renderer/src/components/automations/automation-orca-save-operations.ts | sed -n '1,330p'
printf '%s\n' '--- renderer automation host client ---'
nl -ba src/renderer/src/components/automations/automation-host-client.ts | sed -n '1,210p'
printf '%s\n' '--- automation management actions ---'
nl -ba src/renderer/src/components/automations/automation-management-actions.ts | sed -n '1,120p'
printf '%s\n' '--- automation target/editor references ---'
rg -n -F -g '*.ts' -g '*.tsx' -e 'destination' -e 'projectId' -e 'hostId' -e 'runContext' src/renderer/src/components/automations src/renderer/src/hooks | head -n 260

Repository: stablyai/orca

Length of output: 41669


🏁 Script executed:

#!/bin/bash
set -e
printf '%s\n' '--- automation run creation and source-context consumers ---'
nl -ba src/main/persistence/scheduling-automations/automation-run-operations.ts | sed -n '60,115p'
rg -n -F -g '*.ts' -g '*.tsx' -e 'run.sourceContext' -e 'automationRun.sourceContext' -e 'sourceContext: run.' -e 'sourceContext: automationRun' src | head -n 180
printf '%s\n' '--- source context migration tests and relevant base diff ---'
git diff --unified=30 3be5039eed0ac7340b55bc92be5a504b1568a668 1017ef06198ed84ceed2ccd732349f7fe529f87a -- src/main/persistence/scheduling-automations/automation-context-migration.ts src/main/persistence/scheduling-automations/automation-run-operations.ts

Repository: stablyai/orca

Length of output: 5912


Do not persist sourceContext: null for an ambiguous repository ID.

An ambiguous ID currently backfills both contexts as null. Later loads then skip sourceContext because the property exists. If one twin is removed, captureLegacyRunTarget stamps only runContext, so a subsequent run can still copy sourceContext: null.

Keep sourceContext absent while the repository ID is ambiguous. The next load can then derive it when one repository remains. Do not derive it from an arbitrary twin because TaskSourceContext includes host identity.

🐛 Suggested fix
   const contextsByAutomationId = new Map<string, Pick<Automation, 'runContext' | 'sourceContext'>>()
   // Why: a repo id on two hosts names no host, so the load leaves it for dispatch to refuse.
   const reposById = new Map<string, Repo | undefined>()
+  const ambiguousRepoIds = new Set<string>()
   for (const repo of state.repos ?? []) {
-    reposById.set(repo.id, reposById.has(repo.id) ? undefined : repo)
+    if (reposById.has(repo.id)) {
+      ambiguousRepoIds.add(repo.id)
+      reposById.set(repo.id, undefined)
+    } else {
+      reposById.set(repo.id, repo)
+    }
   }
   const automations = (state.automations ?? []).map((automation) => {
+    const legacyRepoId = getAutomationLegacyRepoId(automation)
     const contexts = getAutomationContextsForRepo(
-      reposById.get(getAutomationLegacyRepoId(automation)),
+      reposById.get(legacyRepoId),
       state.projectHostSetups ?? []
     )
     const next: Automation = { ...automation }
@@
-    if (!Object.hasOwn(next, 'sourceContext')) {
+    if (!Object.hasOwn(next, 'sourceContext') && !ambiguousRepoIds.has(legacyRepoId)) {
       next.sourceContext = contexts.sourceContext
       changed = true
     }

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

🟠 Major · Apply remote-host refusal checks to uncaptured legacy targets. · run-target-resolution.ts:154

src/main/automations/run-target-resolution.ts:154
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Apply remote-host refusal checks to uncaptured legacy targets.

When automation.runContext is absent, this branch returns resolveLegacyAutomationRunTarget without checking options.allowRemoteHostScheduling or automation.schedulerOwner. A unique legacy automation on a runtime host can therefore proceed when remote scheduling is disabled or the owner is not remote_host_service. captureLegacyRunTarget does not prevent this because it leaves the automation uncaptured before this branch runs. Apply the same refusal used at Lines 157-165 before returning the legacy result.


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 00b3550f-a304-43c9-9ad6-03411dd92bf6
📥 Commits

Reviewing files that changed from the base of the PR and between 1017ef0 and c5a1688.

📒 Files selected for processing: 1
  • src/main/automations/run-target-resolution.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.

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