feat: implement agent heartbeat and automatic dead-agent cleanup#138
Open
dubemoyibe-star wants to merge 2 commits into
Open
feat: implement agent heartbeat and automatic dead-agent cleanup#138dubemoyibe-star wants to merge 2 commits into
dubemoyibe-star wants to merge 2 commits into
Conversation
|
@dubemoyibe-star is attempting to deploy a commit to the Jaja's projects Team on Vercel. A member of the Team first needs to authorize it. |
devJaja
self-requested a review
July 20, 2026 23:33
devJaja
reviewed
Jul 20, 2026
devJaja
left a comment
Contributor
There was a problem hiding this comment.
Nice Implementation @dubemoyibe-star
resolve the conflicts
Contributor
|
Fix the CI checks failing @dubemoyibe-star |
Author
I'm on it chief |
Contributor
|
@dubemoyibe-star |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Agent Heartbeat Mechanism
Problem
Agents register in
backend/src/db/index.ts(agent_registry/agentstable) but there is no heartbeat mechanism. If an agent process crashes or disconnects, it remains in the registry asonlineindefinitely. The coordinator inbackend/src/coordinator/index.tsmay assign tasks to dead agents.Solution
Implemented a full heartbeat lifecycle spanning the database layer, API routes, agent client, coordinator, and server startup/shutdown.
Files Changed
Database Layer
backend/src/db/agents.tsstatus: 'online' | 'offline'toAgentRecordinterface.status TEXT NOT NULL DEFAULT 'online'column to theagentstable schema.upsert()to persiststatus.list()to accept an optionalstatusfilter.markOffline(olderThan: string)toAgentDb— bulk-updates agents toofflinewhenlastSeenAtis older than the cutoff.Type Definitions
backend/src/types/agent.tsstatus: 'online' | 'offline'toAgentRegistrationso the coordinator can filter stale agents.Coordinator
backend/src/coordinator/coordinator.tsagentsFor(), filtered out agents whosestatus !== 'online'before sorting by cost and dispatching.API Routes
backend/src/api/routes/agents.tsPOST /api/agents/:id/heartbeatendpoint.lastSeenAttonew Date().toISOString(), setsstatus = 'online', and returns204 No Content.404if the agent does not exist.POST /api/agents/registerto setstatus: 'online'on registration.Agent-Side Heartbeat
backend/src/agents/heartbeat.ts(new)HeartbeatClientpingsPOST /api/agents/:id/heartbeatevery 30s (configurable).start()andstop()for lifecycle management.backend/src/agents/base/BaseAgent.tsHeartbeatClient.HeartbeatClientin the constructor whenapiBaseUrlis provided.startHeartbeat()andstopHeartbeat()methods.Agent Registry
backend/src/agents/registry.tsinitialize(), after a successfulinstance.register(), callsinstance.startHeartbeat().shutdown()that iterates all agents and callsstopHeartbeat().Cleanup Service
backend/src/services/agentCleanup.ts(new)AgentCleanupServiceruns on a configurable interval (default 60s).offlineiflastSeenAt < now - ttlMs(default TTL 90s).start()andstop().Server Integration & Graceful Shutdown
backend/src/index.tsAgentCleanupServiceandglobalAgentRegistry.AgentCleanupServiceafter agent initialization.cleanupService.stop(),globalAgentRegistry.shutdown(), andstopAgentSync()before closing the HTTP server.Tests
backend/tests/agents.test.tsstatuscolumn.codingAgentfixture to includestatus: 'online'.returns 204 and updates lastSeenAt on heartbeat.returns 404 when sending heartbeat to an unknown agent.Registry Sync
backend/src/registry/sync.tsstatus: 'online'when upserting agents.Acceptance Criteria Verification
last_seen_at/statuscolumns exist inagentstablePOST /agents/:id/heartbeatupdates timestamp/api/agents/:id/heartbeatHeartbeatClientAgentCleanupServiceagentsFor()filterindex.tsshutdown handlerMigration / Compatibility Notes
agentstables will not be altered on install if they already exist (CREATE TABLE IF NOT EXISTS). To add thestatuscolumn to an existing production database, run:lastSeenAtcolumn was already present in the schema and registration code; this PR standardizes its update via heartbeat.Testing Notes
npm run build).better-sqlite3binaries pass locally (8 suites, 94 tests).better-sqlite3) are currently skipped in the Windows environment due to missing prebuilt bindings; they are expected to pass in CI environments where the native module is compiled.Closes #107