feat(heartbeat): trigger periodic memory maintenance#233
feat(heartbeat): trigger periodic memory maintenance#233mczabca-boop wants to merge 2 commits intoTinyAGI:mainfrom
Conversation
Greptile SummaryThis PR integrates periodic memory maintenance into the existing heartbeat cron flow: when an agent hasn't run maintenance in 7 days, the contents of Key design decisions:
Remaining minor concerns:
Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Cron as heartbeat-cron.sh
participant FS as Agent Filesystem
participant API as API Server
participant Agent as Agent Process
loop Every BASE_INTERVAL seconds
Cron->>Cron: cleanup_stale_pending_maintenance(NOW)
loop For each AGENT_ID
Cron->>FS: read .tinyclaw/last-memory-maintenance
FS-->>Cron: timestamp (or missing)
alt maintenance overdue (≥7 days) AND not already pending
Cron->>FS: read memory-maintenance-heartbeat.md
FS-->>Cron: maintenance prompt
Cron->>Cron: prepend prompt to PROMPT
end
Cron->>API: POST /api/message {agent, channel:heartbeat, message}
API-->>Cron: {ok, messageId}
Cron->>Cron: store PENDING_MAINTENANCE_DIR[messageId] = agentDir
end
Cron->>Cron: sleep 10s
Cron->>API: GET /api/responses?limit=20
API-->>Cron: [{channel, messageId, ...}, ...]
loop For each pending messageId
alt response with matching messageId found
Cron->>FS: write .tinyclaw/last-memory-maintenance
Cron->>Cron: clear_pending_maintenance(messageId, agentId)
end
end
end
Last reviewed commit: "fix(heartbeat): clea..." |
|
@greptile-apps review again |
| mark_memory_maintenance_completed() { | ||
| local agent_dir="$1" | ||
| local now="$2" | ||
| mkdir -p "$agent_dir/.tinyclaw" | ||
| printf '%s\n' "$now" > "$agent_dir/.tinyclaw/last-memory-maintenance" | ||
| } |
There was a problem hiding this comment.
Missing guard for empty
agent_dir
mark_memory_maintenance_completed doesn't validate that agent_dir is non-empty before writing. If PENDING_MAINTENANCE_DIR[$MESSAGE_ID] somehow resolves to an empty string (e.g. an unexpected bash edge case or array misuse), mkdir -p "/.tinyclaw" and printf … > "/.tinyclaw/last-memory-maintenance" would execute — silently succeeding on systems where the process has root privileges, or failing with a cryptic permission error otherwise.
A one-line guard at the top of the function keeps this safe:
| mark_memory_maintenance_completed() { | |
| local agent_dir="$1" | |
| local now="$2" | |
| mkdir -p "$agent_dir/.tinyclaw" | |
| printf '%s\n' "$now" > "$agent_dir/.tinyclaw/last-memory-maintenance" | |
| } | |
| mark_memory_maintenance_completed() { | |
| local agent_dir="$1" | |
| local now="$2" | |
| if [ -z "$agent_dir" ]; then | |
| log " ✗ mark_memory_maintenance_completed: agent_dir is empty, skipping" | |
| return 1 | |
| fi | |
| mkdir -p "$agent_dir/.tinyclaw" | |
| printf '%s\n' "$now" > "$agent_dir/.tinyclaw/last-memory-maintenance" | |
| } |
Summary
Changes
lib/heartbeat-cron.shto prepend a maintenance prompt when an agent has not run memory maintenance for 7 daysmemory-maintenance-heartbeat.mdlast-memory-maintenanceafter a matching heartbeat response is observedTesting
bash -n lib/heartbeat-cron.shmemorytestermaintenance by setting.tinyclaw/last-memory-maintenanceto0and lowering heartbeat interval to10smemorytester/home/cheng/tinyclaw-workspace/memorytester/.tinyclaw/last-memory-maintenancewas updated only after a matching heartbeat response was observedmemory/maintenance-test/and confirmed the maintenance run cleaned them up and kept the existing authoritative memoryBreaking Changes