Skip to content

goal: add timer-owned continuation handoff - #1

Merged
vincenthcui merged 1 commit into
mainfrom
fix/goal-timer-continuation-handoff
Aug 26, 2026
Merged

vincenthcui merged 1 commit into
mainfrom
fix/goal-timer-continuation-handoff

Conversation

@vincenthcui

@vincenthcui vincenthcui commented Aug 25, 2026 •

Copy link
Copy Markdown
Owner

Problem

An active Goal owns the thread-idle lifecycle. If the remaining work is deferred to a scheduled task, creating the schedule does not currently replace that idle continuation. The Goal can therefore start immediate synthetic turns while the scheduler is supposed to wait, as reported in openai#28923 and openai#28144.

Design

This change separates Goal state from the mechanism that starts its next turn:

active Goal
├─ onIdle                         -> Goal runtime starts synthetic idle work
└─ timer(automation, generation) -> scheduler owns the next explicit turn

The Goal remains active in both cases. Installing a timer trigger suppresses only automatic idle turns and idle accounting; it does not pause, complete, or grant the agent permission to mutate the Goal.

The handoff contract is:

  1. Read thread/goal/continuation/get and retain its opaque goalVersion.
  2. Persist the automation.
  3. Set timer with goalVersion, automationId, generation, and nextRunAt.
  4. Let the scheduler start one explicit turn when due.
  5. For recurring schedules, advance the generation and keep the timer trigger installed.
  6. For one-shot schedules, start the scheduled turn first, then restore onIdleIfTimer while that turn is active.
  7. On cancellation or disable, restore onIdleIfTimer.

Concurrency and failure boundaries

  • goalVersion prevents an operation created for an old Goal from affecting a replacement Goal.
  • automationId gives one automation ownership of the continuation trigger.
  • Monotonic generation rejects stale schedule updates.
  • Conditional SQL makes set/clear compare-and-set operations safe across app-server processes.
  • A post-write Goal-version check prevents runtime effects if the Goal changes during the database operation.
  • Automation creation and dispatch remain scheduler-owned. The scheduler must persist the schedule before installing the trigger, compensate if the handoff fails, and use its normal claim/lease to ensure one dispatch per run.

What changed

  • Persist timer-owned Goal continuation state in the Goal SQLite database.
  • Add experimental thread/goal/continuation/get and thread/goal/continuation/set app-server methods.
  • Suppress Goal idle continuation while a matching timer trigger is installed.
  • Restore normal Goal continuation through a Goal/automation/generation compare-and-set.
  • Add state and app-server integration coverage for stale versions, ownership, generations, recurring turns, one-shot restoration, and Goal replacement.

Scope

This is the Goal-side handoff primitive. It does not modify the currently shipped Desktop automation_update handler. That handler, or a future Rust automation extension such as the direction explored in openai#28609 through openai#28620, must call this API after a schedule mutation succeeds.

The split is intentional: the agent keeps only automation_update authority; the trusted host or automation service performs the internal Goal continuation handoff.

Addresses openai#28923. Related: openai#28144, openai#35601, openai#29184, openai#32793.

Verification

  • just test -p codex-state: 184 passed.
  • just test -p codex-goal-extension: 24 passed.
  • just test -p codex-app-server-protocol: 294 passed, 1 skipped.
  • just test -p codex-app-server timer_goal_continuation_suppresses_idle_until_restored: passed.
  • Full codex-app-server: 1295/1300 passed; the five failures require unavailable local code-mode/executor fixtures and do not touch Goal paths. The new integration test passed in the full run.
  • Scoped Clippy passed for codex-state, codex-goal-extension, codex-app-server-protocol, and codex-app-server.
  • Stable and experimental app-server schema fixtures were regenerated and verified.

Workspace-wide Clippy was attempted but stopped in the unchanged code-mode-host dependency because the upstream rusty_v8 v150.4.0 macOS archive URL returned 404.

@vincenthcui

vincenthcui commented Aug 25, 2026 •

Copy link
Copy Markdown
Owner Author

Review request

@jif-oai — could you review the Goal idle-lifecycle boundary, especially the onIdle versus timer-owned continuation split introduced around the mechanism from openai#25060?

@etraut-openai — could you review whether keeping the Goal active while replacing only its continuation trigger is preferable to adding another stopped/waiting Goal status, given openai#23094?

@eternal-openai — could you review the automation ownership and dispatch contract against the Rust automation direction explored in openai#28609 through openai#28620?

Reporter validation would also be very useful from @titanwings, @jerryfane, and @Countermarch. The intended acceptance behavior is:

  • zero automatic model turns while a timer owns continuation;
  • one explicit turn per scheduled dispatch;
  • recurring timers remain installed;
  • one-shot/cancel restores onIdle with CAS;
  • stale automation operations cannot affect a replacement Goal.

The main design questions are:

  1. Is continuation ownership the right abstraction, rather than a new Goal status?
  2. Is the goalVersion + automationId + generation CAS contract sufficient for cross-process callers?
  3. Should the current Desktop handler consume this RPC first, with a future Rust automation extension calling the same Goal service directly?

The upstream repository currently rejects CreatePullRequest for this account, so this fork PR is the reviewable artifact. If the direction is acceptable, guidance or an invitation for the upstream landing path would be appreciated.

@vincenthcui

Copy link
Copy Markdown
Owner Author

Desktop integration and protocol migration

This PR intentionally stops at the Goal-side primitive. The shipped behavior changes only after Desktop wires its automation handler to the new continuation API.

Required Desktop changes

Do not replace the model-facing automation_update tool with the Goal RPC. Keep the agent authority unchanged:

agent -> automation_update
             |
             v
trusted Desktop automation handler
   1. persists and schedules the automation
   2. calls thread/goal/continuation/* internally

Only same-thread heartbeat/wakeup automations participate in the handoff. Detached cron automations that create a new thread remain unchanged.

Desktop should persist the following binding metadata with a same-thread automation:

  • target thread id;
  • opaque goalVersion returned by thread/goal/continuation/get;
  • continuation generation;
  • the existing automation id and nextRunAt.

The binding metadata is host-owned and does not need to grant the model access to Goal APIs.

Create and bind

The safest sequence is a two-phase automation mutation:

  1. Read thread/goal/get. Continue only when the target thread has an active Goal.
  2. Read thread/goal/continuation/get and retain goalVersion.
  3. Persist the automation as non-dispatchable or bindingPending with generation 1.
  4. Call thread/goal/continuation/set with kind: timer.
  5. Mark the automation active only after the Goal handoff succeeds.

If step 4 fails, remove or disable the pending automation and return an automation_update error. Do not report success with an active schedule but no Goal handoff. If step 5 fails, retain a recoverable pending record so startup reconciliation can either activate it or clear the timer binding.

Reschedule and recurring runs

  • Serialize mutations for one automation.
  • Persist the updated schedule, incremented generation, and nextRunAt before updating the Goal trigger.
  • Call continuation/set(timer) with the same automation id and the new generation.
  • Keep the timer trigger installed while a recurring scheduled turn runs, so turn completion does not start an immediate Goal idle turn.
  • Continue using the scheduler's claim/lease for one dispatch per occurrence. The Goal CAS prevents stale state changes; it does not replace scheduler deduplication.

One-shot delivery

  1. Claim the due automation.
  2. Start the explicit scheduled turn.
  3. Only after turn/start is accepted, call continuation/set(onIdleIfTimer) with the captured Goal version, automation id, and generation.
  4. Complete the automation run.

If turn start fails or the target is busy, keep the timer binding and retry/defer. Clearing before the scheduled turn is accepted reopens the idle-loop race.

Restoring onIdle while the scheduled turn is active lets normal Goal continuation resume after that turn completes.

Disable, delete, and target conversion

Use the inverse two-phase order:

  1. Persist the automation as non-dispatchable.
  2. Clear the binding with onIdleIfTimer CAS.
  3. Delete the automation record or finish conversion to detached cron.

Do not use unconditional onIdle for automation cleanup. A CAS mismatch means the Goal was replaced or another automation owns the trigger; the old handler must not overwrite that state.

For deletion, keep a tombstone or non-dispatchable record until step 2 succeeds. This gives startup reconciliation enough information to retry cleanup after a crash.

Model-facing automation protocol

The first Desktop release should extend the existing dynamic-tool schema additively:

  • keep legacy detached cron requests working;
  • add an explicit same-thread heartbeat target, either kind: heartbeat plus thread_id, or an equivalent tagged target union;
  • return the resolved delivery target in the response;
  • keep Goal binding metadata internal or read-only.

This matches the Cron versus Heartbeat { thread_id } direction explored in openai#28615 and avoids silently treating a detached cron as a same-thread wakeup.

App-server protocol migration

The new methods are experimental. Desktop must:

  1. initialize app-server with capabilities.experimentalApi: true;
  2. update its pinned app-server binary/commit;
  3. regenerate the internal app-server-types TypeScript bindings from the experimental exports;
  4. add handlers for ThreadGoalContinuationGetResponse.goalVersion and the tagged continuation unions;
  5. feature-detect support before mutating an automation.

thread/goal/continuation/get is the feature probe because it is read-only. Handle these cases separately:

  • method supported, active Goal: use the new handoff;
  • method supported, no active Goal: create the automation without a Goal binding;
  • method not found or experimental API disabled, no active Goal: use the legacy automation path;
  • method not found or experimental API disabled, active Goal: fail closed with an upgrade-required error instead of creating a schedule that leaves Goal idle continuation running.

Existing automation migration

Existing heartbeat automations have no Goal binding metadata. Migrate them lazily at startup or next update:

  • if the target thread has no active Goal, leave the automation unchanged;
  • if the effective trigger is onIdle, install a new binding and persist its Goal version/generation;
  • if the timer is already owned by the same automation, adopt and persist the returned binding;
  • if another automation owns the timer, surface a conflict and do not overwrite it;
  • never rebind an old automation automatically after the user replaces the Goal.

Startup reconciliation should also finish bindingPending, non-dispatchable cleanup, and one-shot completion records left by crashes.

SQLite migration and rollback

The Goal database migration is additive: it creates thread_goal_timer_continuations. Desktop should not write this table directly; app-server remains the only writer.

Older binaries can open a database with the newer applied migration because the runtime migrator ignores unknown newer versions. That is storage-compatible but not behavior-compatible: an older Goal runtime does not read the timer table and may resume idle continuation.

Rollout order should therefore be:

  1. ship the new app-server support;
  2. ship the capability-gated Desktop integration;
  3. backfill existing same-thread heartbeat bindings;
  4. promote or stabilize the protocol after mixed-version coverage.

Before downgrading to an app-server without this feature, disable new binding creation and clear existing timer bindings with the new binary. Do not rely on the older binary merely tolerating the table.

Desktop acceptance matrix

  • old Desktop + new app-server: unchanged behavior;
  • new Desktop + old app-server: active-Goal handoff fails closed;
  • new Desktop + new app-server: Goal produces zero idle turns before the timer;
  • restart with a timer installed: binding remains effective;
  • crash after each two-phase step: reconciliation converges without duplicate dispatch or permanent suppression;
  • one-shot, recurring, reschedule, disable, delete, and cron/heartbeat conversion;
  • Goal replacement while a timer operation is delayed;
  • two app-server processes sharing one Goal database;
  • target busy at scheduled time;
  • scheduler retry and duplicate delivery.

@vincenthcui
vincenthcui force-pushed the fix/goal-timer-continuation-handoff branch from 5fa3e17 to e17e0f2 Compare August 26, 2026 04:58
@vincenthcui
vincenthcui merged commit 074400b into main Aug 26, 2026
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