Skip to content

Commit 1033cd7

Browse files
committed
wip: apply suggestions from cian
1 parent 2565a3c commit 1033cd7

2 files changed

Lines changed: 20 additions & 20 deletions

File tree

cmd/server/server.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,33 +107,33 @@ func runServer(ctx context.Context, logger *slog.Logger, argsToPass []string) er
107107
}
108108

109109
// Get the variables related to state management
110-
stateFile := viper.GetString(StateFile)
110+
stateFile := viper.GetString(FlagStateFile)
111111
loadState := false
112112
saveState := false
113113

114114
// Validate state file configuration
115115
if stateFile != "" {
116-
if !viper.IsSet(LoadState) {
116+
if !viper.IsSet(FlagLoadState) {
117117
loadState = true
118118
} else {
119-
loadState = viper.GetBool(LoadState)
119+
loadState = viper.GetBool(FlagLoadState)
120120
}
121121

122-
if !viper.IsSet(SaveState) {
122+
if !viper.IsSet(FlagSaveState) {
123123
saveState = true
124124
} else {
125-
saveState = viper.GetBool(SaveState)
125+
saveState = viper.GetBool(FlagSaveState)
126126
}
127127
} else {
128-
if viper.IsSet(LoadState) && viper.GetBool(LoadState) {
128+
if viper.IsSet(FlagLoadState) && viper.GetBool(FlagLoadState) {
129129
return xerrors.Errorf("--load-state requires --state-file to be set")
130130
}
131-
if viper.IsSet(SaveState) && viper.GetBool(SaveState) {
131+
if viper.IsSet(FlagSaveState) && viper.GetBool(FlagSaveState) {
132132
return xerrors.Errorf("--save-state requires --state-file to be set")
133133
}
134134
}
135135

136-
pidFile := viper.GetString(PidFile)
136+
pidFile := viper.GetString(FlagPidFile)
137137

138138
// Write PID file if configured
139139
if pidFile != "" {
@@ -315,10 +315,10 @@ const (
315315
FlagAllowedOrigins = "allowed-origins"
316316
FlagExit = "exit"
317317
FlagInitialPrompt = "initial-prompt"
318-
StateFile = "state-file"
319-
LoadState = "load-state"
320-
SaveState = "save-state"
321-
PidFile = "pid-file"
318+
FlagStateFile = "state-file"
319+
FlagLoadState = "load-state"
320+
FlagSaveState = "save-state"
321+
FlagPidFile = "pid-file"
322322
)
323323

324324
func CreateServerCmd() *cobra.Command {
@@ -357,10 +357,10 @@ func CreateServerCmd() *cobra.Command {
357357
// localhost:3284 is the default origin when you open the chat interface in your browser. localhost:3000 and 3001 are used during development.
358358
{FlagAllowedOrigins, "o", []string{"http://localhost:3284", "http://localhost:3000", "http://localhost:3001"}, "HTTP allowed origins. Use '*' for all, comma-separated list via flag, space-separated list via AGENTAPI_ALLOWED_ORIGINS env var", "stringSlice"},
359359
{FlagInitialPrompt, "I", "", "Initial prompt for the agent. Recommended only if the agent doesn't support initial prompt in interaction mode. Will be read from stdin if piped (e.g., echo 'prompt' | agentapi server -- my-agent)", "string"},
360-
{StateFile, "s", "", "Path to file for saving/loading server state", "string"},
361-
{LoadState, "", false, "Load state from state-file on startup (defaults to true when state-file is set)", "bool"},
362-
{SaveState, "", false, "Save state to state-file on shutdown (defaults to true when state-file is set)", "bool"},
363-
{PidFile, "", "", "Path to file where the server process ID will be written for shutdown scripts", "string"},
360+
{FlagStateFile, "s", "", "Path to file for saving/loading server state", "string"},
361+
{FlagLoadState, "", false, "Load state from state-file on startup (defaults to true when state-file is set)", "bool"},
362+
{FlagSaveState, "", false, "Save state to state-file on shutdown (defaults to true when state-file is set)", "bool"},
363+
{FlagPidFile, "", "", "Path to file where the server process ID will be written for shutdown scripts", "string"},
364364
}
365365

366366
for _, spec := range flagSpecs {

lib/screentracker/pty_conversation.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ func (c *PTYConversation) SaveState() error {
596596
// Clear dirty flag after successful save
597597
c.dirty = false
598598

599-
c.cfg.Logger.Info(fmt.Sprintf("State saved successfully to: %s", stateFile))
599+
c.cfg.Logger.Info("State saved successfully", "path", stateFile)
600600

601601
return nil
602602
}
@@ -606,7 +606,7 @@ func (c *PTYConversation) loadStateLocked() error {
606606
stateFile := c.cfg.StatePersistenceConfig.StateFile
607607
loadState := c.cfg.StatePersistenceConfig.LoadState
608608

609-
if !loadState {
609+
if !loadState || c.loadStateSuccessful {
610610
return nil
611611
}
612612

@@ -647,7 +647,7 @@ func (c *PTYConversation) loadStateLocked() error {
647647

648648
// Store the first stable snapshot for filtering later
649649
snapshots := c.snapshotBuffer.GetAll()
650-
if len(snapshots) > 0 {
650+
if len(snapshots) > 0 && c.cfg.FormatMessage != nil {
651651
c.firstStableSnapshot = c.cfg.FormatMessage(strings.TrimSpace(snapshots[len(snapshots)-1].screen), "")
652652
}
653653

@@ -669,7 +669,7 @@ func (c *PTYConversation) adjustScreenAfterStateLoad(screen string) string {
669669
// Before the first user message after loading state, return the last message from the loaded state.
670670
// This prevents computing incorrect diffs from the restored screen, as the agent's message should
671671
// remain stable until the user continues the conversation.
672-
if !c.userSentMessageAfterLoadState {
672+
if !c.userSentMessageAfterLoadState && len(c.messages) > 0 {
673673
newScreen = "\n" + c.messages[len(c.messages)-1].Message
674674
}
675675

0 commit comments

Comments
 (0)