ImpoDocs
Browse documentation

DocumentationExtend the framework

Add background work

Use durable triggers and idempotent steps so useful work continues while clients are closed.

On this pageUse scheduled Tasks firstAdd an account featureKeep workflow code deterministicMake retries safeReturn useful outcomesVerify the lifecycle

Choose between a user's scheduled Task and an account feature. A daily research request belongs in scheduled Tasks. Platform work that runs on its own cadence for every account, such as consolidating context at night, is an account feature.

iOS calls scheduled entry points Triggers. The API retains /scheduled-tasks; each occurrence creates a separate Task conversation. These schedules do not implement the proposed gadget-event automation runtime.

Use scheduled Tasks first#

Scheduled plans are owned records with a time zone, timing rule and revision. Temporal admits ordinary isolated Tasks when the plan is due. The execution and result appear through the existing task protocol.

Revisions fence obsolete timers. Overlapping runs are skipped. Editing a schedule must not let an old workflow admit work under the previous plan. Use the existing scheduled-task contract before introducing another scheduler.

Add an account feature#

server/src/features/activities.ts defines Feature. Each feature has a key from featureKeys (server/src/features/contract.ts), the connected services it reads, and four functions on the account clock: first (the first run of a new workflow), due (whether a run may happen now), next (the regular run after a planned one) and run(context). Context includes ownerId, scheduledAt, a runKey stable across retries, the account's timeZone and an abort signal. run may ask for an earlier next run and returns the notification event it committed, if any. abandon records a run whose attempts are spent.

Implement a factory that accepts the repositories and services the feature needs, and register it in the array passed to createFeatureActivities in server/src/worker-main.ts. Each account gets one featureWorkflow per feature (impo/feature/<ownerId>/<key>), started when the account is created or resumes. A release that adds a feature also runs a release step that starts it for every active account. See the user database guide, Account features.

Keep workflow code deterministic#

Temporal workflow code coordinates timers and activities. Put network calls, database changes and agent execution in activities or workers. Do not run a model inside a timer callback in the API process.

Use the official client's client.beta.agents with @rebyteai/agent-extensions for generated content. Bound each run and read large inputs in parts. Preserve a persistent Session when its workspace is part of the feature. Select its tool catalog deliberately and retain the account's execution configuration. Unattended steps cannot assume a foreground phone is available.

Make retries safe#

Store a receipt under the stable step identity before reporting completion. Commit related state together through a repository. A repeated activity should return the accepted result or continue reconciliation rather than create another card or external action.

For generated reminders, preserve source resource IDs and connection generations. Recheck their validity when delivering. If a user deletes context or disconnects a service, dependent output must be withdrawn.

Return useful outcomes#

Background work may complete without visible content. The existing Feed path treats an empty check as empty, not as a reason to invent a status card. Surface failures where the user can act on them, and keep operator diagnostics separate from agent copy.

Verify the lifecycle#

Run npm run test:features for timer, restart and idempotency changes. Use npm run test:scheduled-tasks for schedule edits and overlap rules. These suites require PostgreSQL and Temporal tooling; see the server guide.

Also check deletion during a run, revoked connectors and a retry after an uncertain write. Continue with deployment and services when the local lifecycle is sound.