Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Handle inventory heartbeat append failures

shukieshah66f962e82ad5
diff --git a/cli/beacon/cmd/endpoint_diagnostics.go b/cli/beacon/cmd/endpoint_diagnostics.go
index 4b3e73d0..ca00fd1f 100644
--- a/cli/beacon/cmd/endpoint_diagnostics.go
+++ b/cli/beacon/cmd/endpoint_diagnostics.go
@@ -338,6 +338,8 @@ type inventoryHeartbeatWriteResult struct {
PreviousDigest string `json:"previous_snapshot_digest,omitempty"`
}
+const inventoryAttemptBackoff = 5 * time.Minute
+
func runEndpointInventoryHeartbeat(cmd *cobra.Command, args []string) error {
cfg, err := loadInventoryHeartbeatConfig(endpointUserMode(), endpointOpts.logPath, endpointOpts.inventoryHeartbeatConfig)
if err != nil {
@@ -390,6 +392,9 @@ func writeInventoryHeartbeat(cfg endpointconfig.Config, settings endpointconfig.
if !force && !endpointinventory.TTLExpired(state, now, settings.TTLSeconds) {
return inventoryHeartbeatWriteResult{SkippedReason: "ttl_active", PreviousDigest: state.LastSnapshotDigest}, nil
}
+ if !force && endpointinventory.AttemptBackoffActive(state, now, inventoryAttemptBackoff) {
+ return inventoryHeartbeatWriteResult{SkippedReason: "attempt_backoff", PreviousDigest: state.LastSnapshotDigest}, nil
+ }
scanOpts := endpointinventory.Options{
WorkingDir: workingDir,
Runtimes: settings.Runtimes,
@@ -399,8 +404,9 @@ func writeInventoryHeartbeat(cfg endpointconfig.Config, settings endpointconfig.
digest := endpointinventory.SnapshotDigest(inventoryResult)
writeSnapshot := state.LastSnapshotDigest != digest
if err := locked.Save(endpointinventory.State{
- LastEmittedAt: now.Format(time.RFC3339),
- LastSnapshotDigest: digest,
+ LastEmittedAt: state.LastEmittedAt,
+ LastSnapshotDigest: state.LastSnapshotDigest,
+ LastAttemptAt: now.Format(time.RFC3339),
}); err != nil {
return inventoryHeartbeatWriteResult{SnapshotDigest: digest, PreviousDigest: state.LastSnapshotDigest}, err
}
@@ -408,6 +414,12 @@ func writeInventoryHeartbeat(cfg endpointconfig.Config, settings endpointconfig.
if err != nil {
return writeResult, err
}
+ if err := locked.Save(endpointinventory.State{
+ LastEmittedAt: now.Format(time.RFC3339),
+ LastSnapshotDigest: digest,
+ }); err != nil {
+ return inventoryHeartbeatWriteResult{SnapshotDigest: digest, PreviousDigest: state.LastSnapshotDigest}, err
+ }
return writeResult, nil
}
diff --git a/cli/beacon/cmd/endpoint_test.go b/cli/beacon/cmd/endpoint_test.go
index 78fccf18..b733ed72 100644
--- a/cli/beacon/cmd/endpoint_test.go
+++ b/cli/beacon/cmd/endpoint_test.go
@@ -911,6 +911,49 @@ func TestInventoryHeartbeatTTLAndSnapshotDigest(t *testing.T) {
}
}
+func TestInventoryHeartbeatAppendFailureRecordsAttemptOnly(t *testing.T) {
+ home := t.TempDir()
+ t.Setenv("HOME", home)
+ work := t.TempDir()
+ logDir := t.TempDir()
+ logPath := filepath.Join(logDir, "runtime.jsonl")
+ inventoryLogPath := endpointinventory.LogPath(logPath, true)
+ if err := os.MkdirAll(inventoryLogPath, 0755); err != nil {
+ t.Fatal(err)
+ }
+ writeTestFile(t, filepath.Join(home, ".cursor", "mcp.json"), `{"mcpServers":{"one":{"command":"npx"}}}`)
+ cfg := endpointconfig.Default(true, logPath)
+ cfg.Inventory = &endpointconfig.Inventory{TTLSeconds: 86400, Runtimes: []string{"cursor"}}
+ settings := endpointconfig.InventoryConfig(cfg)
+
+ first, err := writeInventoryHeartbeat(cfg, settings, false, work, "hook", "cursor")
+ if err == nil {
+ t.Fatal("writeInventoryHeartbeat should fail when inventory log path is a directory")
+ }
+ if first.SnapshotDigest == "" {
+ t.Fatalf("failed heartbeat should still report snapshot digest: %#v", first)
+ }
+ statePath := endpointinventory.StatePathForLog(logPath, true)
+ state, err := endpointinventory.ReadState(statePath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if state.LastAttemptAt == "" {
+ t.Fatalf("failed append should persist attempt timestamp: %#v", state)
+ }
+ if state.LastEmittedAt != "" || state.LastSnapshotDigest != "" {
+ t.Fatalf("failed append should not mark emission complete: %#v", state)
+ }
+
+ second, err := writeInventoryHeartbeat(cfg, settings, false, work, "hook", "cursor")
+ if err != nil {
+ t.Fatalf("second heartbeat should be skipped by attempt backoff, got error: %v", err)
+ }
+ if second.SkippedReason != "attempt_backoff" {
+ t.Fatalf("second heartbeat = %#v, want attempt_backoff", second)
+ }
+}
+
func TestTopLevelAliasesRegistered(t *testing.T) {
for _, name := range []string{"doctor", "status", "inventory"} {
cmd, _, err := rootCmd.Find([]string{name})
diff --git a/cli/beacon/internal/endpoint/inventory/heartbeat.go b/cli/beacon/internal/endpoint/inventory/heartbeat.go
index a888e4db..cfdf25cf 100644
--- a/cli/beacon/internal/endpoint/inventory/heartbeat.go
+++ b/cli/beacon/internal/endpoint/inventory/heartbeat.go
@@ -24,6 +24,7 @@ type Counts struct {
type State struct {
LastEmittedAt string `json:"last_emitted_at,omitempty"`
LastSnapshotDigest string `json:"last_snapshot_digest,omitempty"`
+ LastAttemptAt string `json:"last_attempt_at,omitempty"`
}
type LockedState struct {
@@ -154,6 +155,17 @@ func TTLExpired(state State, now time.Time, ttlSeconds int) bool {
return !now.Before(last.Add(time.Duration(ttlSeconds) * time.Second))
}
+func AttemptBackoffActive(state State, now time.Time, backoff time.Duration) bool {
+ if backoff <= 0 || state.LastAttemptAt == "" {
+ return false
+ }
+ last, err := time.Parse(time.RFC3339, state.LastAttemptAt)
+ if err != nil {
+ return false
+ }
+ return now.Before(last.Add(backoff))
+}
+
func existingConfigs(configs []Config) []Config {
out := make([]Config, 0, len(configs))
for _, config := range configs {