Skip to content

✨ Add ScheduledTaskTool with global scheduler and frontend polling#3216

Open
2862282695gjh-afk wants to merge 1 commit into
ModelEngine-Group:developfrom
2862282695gjh-afk:feature/scheduled-task-tool
Open

✨ Add ScheduledTaskTool with global scheduler and frontend polling#3216
2862282695gjh-afk wants to merge 1 commit into
ModelEngine-Group:developfrom
2862282695gjh-afk:feature/scheduled-task-tool

Conversation

@2862282695gjh-afk

Copy link
Copy Markdown

Summary

Add a ScheduledTaskTool that enables Nexent agents to create, list, and cancel cron-based or one-shot scheduled tasks.

Architecture

ScheduledTaskTool (thin CRUD)
  ├── create → write to scheduled_tasks_t
  ├── list   → read by agent_id + tenant_id
  └── cancel → update status

Global SchedulerService (singleton, survives agent runs)
  └── 10s DB poll → find due tasks → new agent run → save messages

Frontend Polling
  ├── Layer 1: 5s poll active conversation (refresh + scroll)
  └── Layer 2: 10s batch-poll background conversations (silent cache update)

Why not just a tool module?

Nexent creates a new agent instance per message — tools, scheduler threads, and all state are GCd after each run. A scheduled task tool needs to survive beyond a single agent run, which requires:

  1. DB persistence — task data outlives any agent instance
  2. Global scheduler — independent singleton thread, started at runtime_service startup
  3. Frontend polling — SSE is request-response only, no persistent connection for push

Key Design Decisions

Decision Rationale
Global scheduler, not per-tool Agent instances are ephemeral — per-tool schedulers die with the agent
Scheduler strips ScheduledTaskTool from triggered agent Prevents recursive task creation (agent sees the prompt and tries to create another task)
message_idx from DB max() Original save_messages computes idx from history=[] → always 0, causing collisions
Two-layer frontend polling Active conversation gets fast updates; background conversations get silent cache refresh
Ownership check in polling API get_new_messages_service validates conversation.created_by == user_id

Multi-Tenant Compatibility

  • ScheduledTaskRecord includes tenant_id and user_id fields
  • query_tasks_by_agent and cancel_task filter by tenant_id
  • Scheduler is global (polls all pending tasks) but uses each tasks own tenant_id/user_id for agent runs
  • Frontend polling API validates conversation ownership

Files Changed (15 files, +877 lines, all additive)

New files (3):

  • backend/database/scheduled_task_db.py — 5 CRUD functions
  • backend/services/scheduled_task_scheduler.py — global scheduler singleton
  • sdk/nexent/core/tools/scheduled_task_tool.py — tool with cron parser

Modified files (12, no existing logic changed):

  • backend/database/db_models.pyScheduledTaskRecord model
  • backend/database/conversation_db.pyget_max_message_index()
  • backend/services/conversation_management_service.pyget_new_messages_service()
  • backend/apps/conversation_management_app.py — polling endpoints
  • backend/apps/runtime_app.py — scheduler lifecycle
  • backend/agents/create_agent_info.pyconversation_id passthrough
  • sdk/nexent/core/agents/nexent_agent.py — ScheduledTaskTool metadata injection
  • sdk/nexent/core/tools/__init__.py — export ScheduledTaskTool
  • docker/init.sqlscheduled_tasks_t table
  • frontend/services/api.ts — new endpoints
  • frontend/services/conversationService.ts — polling methods
  • frontend/app/[locale]/chat/internal/chatInterface.tsx — polling useEffect

Testing

Tested with Docker (v2.2.0 image + bind mounts):

  • Created "remind me every 5 minutes" task → agent created task in DB ✅
  • Scheduler detected due task → triggered agent run → wrote messages to DB ✅
  • Frontend polling detected new messages → refreshed display ✅
  • Switched conversation → background polling updated cache ✅
  • No ScheduledTaskTool not found errors ✅
  • No recursive task creation ✅

@2862282695gjh-afk 2862282695gjh-afk force-pushed the feature/scheduled-task-tool branch 2 times, most recently from 00e60b9 to 963cdbe Compare June 10, 2026 09:11
Add a scheduled task tool that enables agents to create, list, and cancel
cron-based or one-shot scheduled tasks. The architecture uses a global
scheduler singleton (independent of agent lifecycle), DB persistence, and
frontend polling for real-time message delivery.

Changes:
- SDK: ScheduledTaskTool as thin CRUD wrapper with cron parser
- Backend: global ScheduledTaskScheduler (10s DB poll), scheduled_task_db
- Backend: new_messages polling API + batch endpoint
- Backend: conversation_id passthrough for task-to-session binding
- Backend: scheduler strips ScheduledTaskTool from triggered agent to
  prevent recursive task creation
- Frontend: two-layer polling (5s active, 10s background conversations)
- DB: scheduled_tasks_t table with multi-tenant fields

No existing Nexent logic is modified — all changes are additive.
@2862282695gjh-afk 2862282695gjh-afk force-pushed the feature/scheduled-task-tool branch from 963cdbe to 66f0180 Compare June 11, 2026 02:01
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