repl: configurable prompt template via config.shell.prompt (closes #10)

At-a-glance situational awareness: see the active model, context fill,
mode flags, and cwd in the prompt itself — prevents "wait, am I still
in plan mode?" surprises.

Example config:

    shell = {
        prompt = "[{model} {ctx_used}/{ctx_max}t T{turn} {mode}] {cwd_short} > ",
    }

Variables (substituted via {name}):
  {model}        active preset name
  {ctx_used}     char/4 token heuristic (Phase 0 §8; accurate is Q1)
  {ctx_max}      config.context.token_budget
  {turn}         #ctx.turns
  {cwd}          libc.getcwd() (chdir-aware; PWD env may drift)
  {cwd_short}    cwd with $HOME -> ~
  {last_status}  last exec exit code, "" if none yet
  {mode}         "norris" | "plan" | "normal"

Default behavior unchanged when shell.prompt is unset — keeps the
"[aish:<model>]>" form with norris  and plan markers.

Side wiring:
  - ffi/libc.lua gains getcwd() (chdir() doesn't update PWD).
  - run_shell records exit code into last_exec_code for {last_status}.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-16 21:14:43 +00:00
parent 10d2501cff
commit d738f339cb
3 changed files with 57 additions and 0 deletions
+15
View File
@@ -42,6 +42,11 @@ int flock(int fd, int operation);
/* TTY detection for non-interactive mode (`aish -p`). Returns 1 if the
fd refers to a terminal, 0 otherwise (sets errno on error). */
int isatty(int fd);
/* getcwd — chdir() doesn't update PWD env, so prompt {cwd} needs the
real cwd. NULL buffer + size 0 is the GNU extension that malloc()s
the buffer; we use a fixed-size stack buffer instead. */
char *getcwd(char *buf, size_t size);
]]
local C = ffi.C
@@ -183,4 +188,14 @@ function M.isatty(fd)
return C.isatty(fd) == 1
end
-- ---------------------------------------------------------------- getcwd
local CWD_BUF = ffi.new("char[?]", 4096)
function M.getcwd()
local p = C.getcwd(CWD_BUF, 4096)
if p == nil then
return nil, ffi.string(C.strerror(C.__errno_location()[0]))
end
return ffi.string(CWD_BUF)
end
return M