Skip to content

feat: scheduled actions agent execution#268

Merged
crazygo merged 4 commits into
mainfrom
copilot/finish-code-and-verify
May 29, 2026
Merged

feat: scheduled actions agent execution#268
crazygo merged 4 commits into
mainfrom
copilot/finish-code-and-verify

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 27, 2026

Adds recurring agent tasks that fire automatically via Vercel Cron. Users create scheduled actions through the agent loop; the cron endpoint picks up due actions, runs the agent loop per action, and writes results into chat history.

DB

  • 021 scheduled_actions — stores interval_seconds, next_run_at, status (active/paused/deleted)
  • 022 scheduled_action_runsUNIQUE(scheduled_action_id, scheduled_fire_at) enables idempotent claiming across concurrent cron invocations

Service (scheduledActionService.ts)

Full CRUD + cron helpers: queryDueActions, claimScheduledActionRun (INSERT … ON CONFLICT DO NOTHING), advanceNextRunAt, markRun{Started,Completed,Failed}

Agent tools (localAgentLoopService.ts)

7 new internal tools: scheduled_action.{create,list,get,update,pause,resume,delete}

Cron endpoint (routes/cron.ts)

GET /api/cron/scheduled-actions — processes up to 5 due actions per tick:

  1. Claim run (idempotent)
  2. Advance next_run_at immediately (prevents re-claim on same tick)
  3. Upsert user + dispatch-placeholder assistant messages
  4. Run agent loop; write final assistant message
  5. Mark run completed/failed

Auth: Authorization: ****** verified with crypto.timingSafeEqual` (no length pre-check timing leak).

Infrastructure

  • app.ts: /api/cron registered without JWT middleware
  • vercel.json: "crons": [{"path": "/api/cron/scheduled-actions", "schedule": "* * * * *"}]

Notes

  • updateScheduledAction preserves next_run_at when only intervalSeconds changes — interval takes effect on the next advance, not immediately
  • CRON_SECRET must be set in Vercel project settings to activate

@vercel
Copy link
Copy Markdown

vercel Bot commented May 27, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
bricks Ready Ready Preview, Comment May 27, 2026 9:26am

Request Review

Copilot AI requested a review from crazygo May 27, 2026 09:23
@crazygo crazygo marked this pull request as ready for review May 29, 2026 03:20
Copilot AI review requested due to automatic review settings May 29, 2026 03:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds recurring "scheduled actions" that are stored per user, exposed as agent tools, and executed by a Vercel Cron-driven endpoint that runs the local agent loop and writes the results back into chat history.

Changes:

  • New scheduled_actions / scheduled_action_runs tables (migrations 021/022) with idempotent claim via UNIQUE(scheduled_action_id, scheduled_fire_at), plus a full CRUD + cron-helper service.
  • Seven new internal agent tools (scheduled_action.{create,list,get,update,pause,resume,delete}) wired into localAgentLoopService with unit tests.
  • New GET /api/cron/scheduled-actions endpoint guarded by CRON_SECRET (timing-safe compare); registered without JWT middleware and scheduled every minute in vercel.json.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
apps/node_backend/src/db/migrations/021_create_scheduled_actions.sql Creates scheduled_actions table + indices + updated_at trigger.
apps/node_backend/src/db/migrations/022_create_scheduled_action_runs.sql Creates scheduled_action_runs table with idempotency UNIQUE and trigger.
apps/node_backend/src/services/scheduledActionService.ts CRUD + cron helpers (queryDueActions, claimScheduledActionRun, advanceNextRunAt, markRun*).
apps/node_backend/src/services/localAgentLoopService.ts Registers 7 scheduled_action.* internal tools and their agent-facing tool definitions.
apps/node_backend/src/services/localAgentLoopService.test.ts Mocks scheduledActionService and tests the dispatcher branches.
apps/node_backend/src/routes/cron.ts New cron handler: claims a run, advances next_run_at, runs agent loop, writes assistant message, marks run completed/failed.
apps/node_backend/src/app.ts Mounts /api/cron (no JWT middleware).
vercel.json Registers the per-minute Vercel Cron entry.
docs/code_maps/{feature_map,logic_map}.yaml Adds scheduled_actions feature entry and risks/keywords.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +99 to +101
// Advance next_run_at immediately so the next cron tick won't re-claim.
await advanceNextRunAt(action.id, new Date(action.nextRunAt), action.intervalSeconds);
await markRunStarted(run.id);
Comment on lines +262 to +268
const result = await pool.query<ScheduledActionRow>(
`UPDATE scheduled_actions
SET ${setClauses.join(', ')}
WHERE user_id = $1 AND id = $2 AND status <> 'deleted'
RETURNING ${ACTION_SELECT_COLS}`,
values,
);
Comment on lines +154 to +157
export function clampIntervalSeconds(value: number): number {
const n = Math.max(MIN_INTERVAL_SECONDS, Math.trunc(value));
return Number.isFinite(n) ? n : MIN_INTERVAL_SECONDS;
}
Comment on lines +90 to +92
for (const action of dueActions) {
// Attempt to claim a run for this fire time.
const run = await claimScheduledActionRun(action, fireTime);
@crazygo crazygo merged commit c592d77 into main May 29, 2026
4 checks passed
@crazygo crazygo deleted the copilot/finish-code-and-verify branch May 29, 2026 03:47
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.

3 participants