Usability pass: tool registry, TUI polish, saved keys and settings
ci / linux (clang) (push) Canceled after 0s
ci / linux (gcc) (push) Canceled after 0s
ci / sanitizers (push) Canceled after 0s
ci / musl (push) Canceled after 0s
ci / macos (push) Canceled after 0s

Tools
- Table-driven tool registry with permission classes and default/minimal
  profiles; new tools: edit_file, list_dir, glob, grep, delete_file,
  move_file, todo_write, web_fetch, ask_user (plus read_file paging).
- Built-in Myers diff; write tools return unified diffs.
- Persistent permission rules (.agent/permissions.json), per-turn undo
  snapshots (.agent/undo), AGENTS.md project notes, auto-compact at 80%
  of the model context, cancellable run_shell with process-group kill.

TUI
- Table-driven slash commands: /provider /key /effort /status /tools
  /permissions /yolo /undo /retry /diff /export /copy /rename /delete
  /init /max-steps /todo, with / and @ completion.
- Permission modal with diff and prefix rules, ask_user modal, todo
  panel, header with model/effort/context, bracketed paste, line editing,
  wide-char cursor, NO_COLOR and ASCII fallback; Esc never quits.

Startup and configuration
- API keys are entered in the TUI (hidden) and saved per provider in
  <config>/credentials (0600); env variables are only a fallback.
- Last provider, per-provider model and reasoning effort are remembered
  in <config>/settings.json; --effort/-e and /effort send
  reasoning_effort (low|medium|high|max) to models that accept it.
- DeepSeek preset uses deepseek-flash / deepseek-v4-pro.
- Session flags --new/--continue/--resume/--list-sessions, --yolo,
  --confirm.

Compatibility and tests
- ncurses detection, macOS/Homebrew support, make install, XDG config
  dir, POSIX cleanups; Gitea CI (gcc/clang/asan/musl/macOS).
- Unit tests for diff, fs tools, registry, undo/perms and config;
  e2e fixtures multi_file_refactor and ask_or_todo; vendored cJSON.
This commit is contained in:
WHC
2026-10-05 01:47:01 +08:00
parent 9877ff48b6
commit 2fde898da0
105 changed files with 19348 additions and 3786 deletions
+132
View File
@@ -1,5 +1,8 @@
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
#include "cJSON.h"
#include "cst_agent.h"
#include "os_compat.h"
@@ -38,6 +41,16 @@ int cst_agent_store_ensure(const char *workspace) {
return -1;
if (os_mkdir(sess, 0755) != 0)
return -1;
/* keep sessions, undo snapshots and exports out of git */
char gi[OS_PATH_MAX];
if (os_path_join(gi, sizeof(gi), base, ".gitignore") == 0 &&
OS_ACCESS(gi, OS_F_OK) != 0) {
FILE *fp = fopen(gi, "w");
if (fp) {
fputs("*\n", fp);
fclose(fp);
}
}
return 0;
}
@@ -187,3 +200,122 @@ int cst_agent_store_list(const char *workspace, char ids[][128], int max_ids) {
free(ents);
return out_n;
}
static int side_path(const char *workspace, const char *id, const char *ext,
char *out, size_t out_sz) {
char dir[OS_PATH_MAX], leaf[192];
if (!id || !id[0] || strchr(id, '/') || strchr(id, '\\') ||
cst_agent_store_dir(workspace, dir, sizeof(dir)) != 0)
return -1;
snprintf(leaf, sizeof(leaf), "%s%s", id, ext);
return os_path_join(out, out_sz, dir, leaf);
}
int cst_agent_store_title_set(const char *workspace, const char *id,
const char *title) {
char p[OS_PATH_MAX];
if (side_path(workspace, id, ".title", p, sizeof(p)) != 0)
return -1;
if (!title || !title[0])
return remove(p) == 0 || errno == ENOENT ? 0 : -1;
FILE *fp = fopen(p, "w");
if (!fp)
return -1;
for (const char *c = title; *c; c++)
fputc(*c == '\n' || *c == '\r' ? ' ' : *c, fp);
fputc('\n', fp);
return fclose(fp) == 0 ? 0 : -1;
}
/* Cut to n bytes without splitting a UTF-8 sequence; newlines → spaces. */
static void clip_line(char *dst, size_t n, const char *src) {
size_t i = 0;
while (src[i] && i + 1 < n) {
dst[i] = (src[i] == '\n' || src[i] == '\r' || src[i] == '\t') ? ' '
: src[i];
i++;
}
while (i > 0 && src[i] && ((unsigned char)src[i] & 0xC0) == 0x80)
i--;
dst[i] = 0;
}
int cst_agent_store_info(const char *workspace, const char *id,
cst_session_info_t *info) {
if (!info)
return -1;
memset(info, 0, sizeof(*info));
snprintf(info->id, sizeof(info->id), "%s", id ? id : "");
char p[OS_PATH_MAX];
if (side_path(workspace, id, ".jsonl", p, sizeof(p)) != 0)
return -1;
struct stat st;
if (stat(p, &st) != 0)
return -1;
info->mtime = (long long)st.st_mtime;
char tp[OS_PATH_MAX];
if (side_path(workspace, id, ".title", tp, sizeof(tp)) == 0) {
FILE *tf = fopen(tp, "r");
if (tf) {
if (fgets(info->title, sizeof(info->title), tf)) {
size_t n = strlen(info->title);
while (n && (info->title[n - 1] == '\n' || info->title[n - 1] == '\r'))
info->title[--n] = 0;
}
fclose(tf);
}
}
FILE *fp = fopen(p, "r");
if (!fp)
return 0;
size_t cap = 1 << 16;
char *line = malloc(cap);
int have_first = 0;
while (line && fgets(line, (int)cap, fp)) {
size_t n = strlen(line);
if (n == cap - 1 && line[n - 1] != '\n') {
/* very long line: skip the rest, it is not the first user line */
int ch;
while ((ch = fgetc(fp)) != EOF && ch != '\n')
;
info->messages++;
continue;
}
if (n <= 1)
continue;
info->messages++;
if (have_first)
continue;
cJSON *o = cJSON_Parse(line);
const cJSON *r = cJSON_GetObjectItemCaseSensitive(o, "role");
const cJSON *c = cJSON_GetObjectItemCaseSensitive(o, "content");
if (cJSON_IsString(r) && strcmp(r->valuestring, "user") == 0 &&
cJSON_IsString(c) && c->valuestring[0]) {
clip_line(info->first_user, sizeof(info->first_user), c->valuestring);
have_first = 1;
}
cJSON_Delete(o);
}
free(line);
fclose(fp);
return 0;
}
int cst_agent_store_delete(const char *workspace, const char *id) {
char p[OS_PATH_MAX];
if (side_path(workspace, id, ".jsonl", p, sizeof(p)) != 0)
return -1;
if (remove(p) != 0)
return -1;
if (side_path(workspace, id, ".title", p, sizeof(p)) == 0)
remove(p);
char cur[128];
if (cst_agent_store_get_current(workspace, cur, sizeof(cur)) == 0 &&
strcmp(cur, id) == 0) {
char dir[OS_PATH_MAX];
if (cst_agent_store_dir(workspace, dir, sizeof(dir)) == 0 &&
os_path_join(p, sizeof(p), dir, "CURRENT") == 0)
remove(p);
}
return 0;
}