Skip to content

Fix /ctx error handling in interactive REPL#47

Closed
superbling wants to merge 1 commit intoantirez:mainfrom
superbling:fix/repl-ctx-error-handling
Closed

Fix /ctx error handling in interactive REPL#47
superbling wants to merge 1 commit intoantirez:mainfrom
superbling:fix/repl-ctx-error-handling

Conversation

@superbling
Copy link
Copy Markdown

Summary

Fix two issues with the /ctx command that caused the interactive REPL to terminate unexpectedly.

  1. /ctx with an invalid value kills the process. The REPL handler called parse_int(), which calls exit(2) on bad input. That is correct for command-line argument parsing, but in the REPL it drops the KV cache and the entire conversation on a typo (e.g. /ctx abc). Added a parse_int_repl() twin that prints the same error and returns false, so the REPL can simply prompt again.

  2. /ctx with a valid value but failing session creation also kills the chat. repl_chat_set_ctx() freed the existing session before trying to create the replacement. If creation failed (e.g. allocation error for the new context size), the old session was already gone and run_repl broke out of the loop. Dropped the redundant pre-free; repl_chat_create_session() already keeps the old session intact on failure, so /ctx now reports the error and keeps the current context.

Test plan

  • make — clean build, no new warnings
  • Piped commands into ./ds4 -c 4096:
    • /ctx abc → prints ds4: invalid value for /ctx: abc, REPL continues (previously: process exit)
    • /ctx 4096 → successfully recreates the session (logs new context buffers)
    • /quit → exits cleanly

In REPL, parse_int() exits the process via exit(2) on invalid input,
which is correct for command-line parsing but kills the interactive
session, dropping the KV cache and conversation. Add parse_int_repl()
which prints the same error and returns false instead, so the REPL
can just prompt again.

repl_chat_set_ctx() freed the existing session before trying to
create the replacement. If creation failed (e.g. allocation error
for the new context size), the old session was already gone and
run_repl broke out of the loop. Drop the redundant pre-free;
repl_chat_create_session already keeps the old session intact on
failure, so /ctx can now report the error and keep the current
context.
@superbling
Copy link
Copy Markdown
Author

Closing in favor of two smaller PRs that split this work apart:

This original PR bundled both, but the second piece is really a design call about whether /ctx failure should terminate the REPL or stay at the prompt — different in nature from the parse-error fix. Splitting them lets the unambiguous fix land independently.

Sorry for the noise.

@superbling superbling closed this May 10, 2026
@superbling superbling deleted the fix/repl-ctx-error-handling branch May 10, 2026 05:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant