Skip to content

AgentAPI: State persistence #1256

@mafredri

Description

@mafredri

Repo: coder/agentapi

Scope: Add state file support to AgentAPI binary so conversation history survives restarts.

Changes:

  • Add --state-file <path> flag to agentapi server command
  • Add --load-state flag (default true when state-file set)
  • Add --save-state flag (default true when state-file set)
  • Add --pid-file <path> flag so shutdown scripts can signal the correct process
  • Implement Conversation.SaveState(path string) error
    • Use atomic write: write to temp file, then rename to target path
  • Implement LoadState(path string) (*Conversation, error)
  • Save state on SIGTERM, SIGINT, SIGHUP (skip if not dirty)
  • Add SIGUSR1 handler to save without exit (for shutdown script coordination, can be called periodically)
  • Track dirty flag: set on conversation change, clear on save
  • State file format (JSON for schema compatibility):
    {
      "version": 1,
      "messages": [...],
      "initial_prompt": "...",
      "initial_prompt_sent": true
    }

Files (in agentapi repo):

  • lib/screentracker/conversation.go
  • cmd/server.go

Acceptance criteria:

  • State saved to file on graceful shutdown
  • State loaded from file on startup when flag set
  • SIGUSR1 saves state without exiting process
  • SIGTERM skips save if not dirty (no changes since last save)
  • State file written atomically (temp file + rename)
  • /messages endpoint returns restored history after restart
  • Version field allows future format migrations

Dependencies: None

References:

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions