Files
CST-agent/tests/test_session_roundtrip.c
T
WHC 2a43ae6622 Windows: official port -- UCRT static build, UTF-8 throughout, Low IL sandbox, ncurses TUI, installer; v0.6.0
Build
  * cross build switches to pkgsCross.ucrt64 (shell-win.nix); curl (Schannel),
    winpthreads and ncurses 6.6 are linked statically, so the release is
    cst-agent.exe + busybox.exe (busybox-w32 Unicode) with no extra DLLs
  * windres embeds a manifest with activeCodePage=UTF-8 and a version resource
  * ncurses 6.6 Windows console driver patches (mouse button state, A_DIM,
    wide-char declarations): scripts/patches/
  * scripts/bootstrap_msys2.sh builds the same release natively in MSYS2
    UCRT64; PR #1's MINGW64 notes now point there
  * vendor/tre-regex (musl's TRE) provides <regex.h> on MinGW

UTF-8
  * paths cross the system call boundary as UTF-16 (*W APIs); console code
    pages set to 65001 and restored on exit; files are read and written in
    binary mode
  * child output that is not valid UTF-8 is decoded from the console, OEM or
    ANSI system code page
  * read_file hides CR; edit_file, apply_patch and write_file keep CRLF line
    endings and a UTF-8 BOM

OS layer and processes
  * os_dir_* and cst_fnmatch replace the POSIX-only directory walks in
    subagent, plugins, skills, undo and fs_tools; realpath / path_under and
    symlink rejection work on Windows
  * every child goes through CreateProcessW with its own environment block
    inside a Job Object, so timeouts and Esc end the whole process tree

Sandbox (auto mode, "auto · lowil")
  * run_shell runs with a restricted Low integrity token; the workspace gets
    an inheritable Low label once, the sandbox TEMP is labeled Low, and the
    credentials file gets a no-read-up label
  * drive roots, the profile and system folders are never labeled
  * network commands still ask, since Low IL does not block the network;
    Access denied and MSYS2's 0xc0000142 offer a rerun outside the sandbox
  * /sandbox lists labeled folders, /sandbox reset-labels restores them

TUI
  * raster console fonts are switched to Consolas / NSimSun while running
  * the home logo falls back to text when the console font lacks U+2580
  * mouse drag copy goes to the Windows clipboard

Distribution
  * scripts/install_windows.ps1 installs a Gitea release into
    %LOCALAPPDATA%\Programs\cst-agent and adds it to the user PATH;
    cst-agent-update.cmd reruns it; scripts/release_windows.sh uploads the
    zip with the token taken from the environment
  * scripts/run_windows.bat is gone; docs/README-windows.md is rewritten

Tests
  * new test_lowil, test_win_utf8 and test_os_dir; tests made portable
    (cst_test_util.h)
  * scripts/win_test.sh and scripts/winvm/ run the suite on English and
    Chinese Windows Server VMs as Administrator, a standard user and a
    Chinese user name: 29/29 on all six
2026-10-06 23:08:14 +08:00

162 lines
5.8 KiB
C

#define _POSIX_C_SOURCE 200809L
#include "cJSON.h"
#include "cst_agent.h"
#include "os_compat.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int nfail;
#define EXPECT(cond, msg) \
do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, msg); \
nfail++; \
} \
} while (0)
static const char *TRICKY = "tab\there \"q\" back\\slash\nnew line \x01 中文";
static const char *TC = "[{\"id\":\"c1\",\"type\":\"function\",\"function\":{"
"\"name\":\"read_file\",\"arguments\":"
"\"{\\\"path\\\":\\\"a.c\\\"}\"}}]";
/* compare JSON texts structurally */
static int json_eq(const char *a, const char *b) {
cJSON *ja = cJSON_Parse(a), *jb = cJSON_Parse(b);
int eq = ja && jb && cJSON_Compare(ja, jb, 1);
cJSON_Delete(ja);
cJSON_Delete(jb);
return eq;
}
static void append(cst_session_t *s, cst_role_t role, const char *content,
const char *tid, const char *tname, const char *tc,
const char *meta) {
cst_message_t m;
memset(&m, 0, sizeof(m));
m.role = role;
m.content = (char *)content;
if (tid)
snprintf(m.tool_call_id, sizeof(m.tool_call_id), "%s", tid);
if (tname)
snprintf(m.tool_name, sizeof(m.tool_name), "%s", tname);
m.tool_calls_json = (char *)tc;
m.meta_json = (char *)meta;
EXPECT(cst_session_append(s, &m) == 0, "append");
}
static void check_messages(cst_session_t *s) {
EXPECT(cst_session_count(s) == 5, "5 messages");
const cst_message_t *m = cst_session_get(s, 0);
EXPECT(m && m->role == CST_ROLE_SYSTEM, "system role");
m = cst_session_get(s, 1);
EXPECT(m && m->role == CST_ROLE_USER && strcmp(m->content, TRICKY) == 0,
"tricky user text exact");
m = cst_session_get(s, 2);
EXPECT(m && m->role == CST_ROLE_ASSISTANT, "assistant role");
EXPECT(m && m->tool_calls_json && json_eq(m->tool_calls_json, TC),
"tool_calls preserved");
EXPECT(m && m->meta_json && json_eq(m->meta_json, "{\"protocol\":\"native-eos\"}"),
"assistant meta");
m = cst_session_get(s, 3);
EXPECT(m && m->role == CST_ROLE_TOOL && strcmp(m->tool_call_id, "c1") == 0 &&
strcmp(m->tool_name, "read_file") == 0,
"tool id/name");
EXPECT(m && strcmp(m->content, "int a;\n\tx\n") == 0, "tool content");
m = cst_session_get(s, 4);
EXPECT(m && m->role == CST_ROLE_USER && strcmp(m->tool_call_id, "c2") == 0,
"text-protocol result frame keeps tool_call_id");
EXPECT(m && m->meta_json &&
json_eq(m->meta_json,
"{\"kind\":\"tool_result\",\"protocol\":\"ctl\"}"),
"result frame meta");
}
int main(void) {
char path[1024], tmp[900];
if (os_temp_dir(tmp, sizeof(tmp)) != 0)
snprintf(tmp, sizeof(tmp), ".");
snprintf(path, sizeof(path), "%s/cst-session-test-%d.jsonl", tmp,
os_getpid());
remove(path);
cst_session_t *s = cst_session_open(path);
EXPECT(s != NULL, "open new");
if (!s)
return 1;
append(s, CST_ROLE_SYSTEM, "sys", NULL, NULL, NULL, NULL);
append(s, CST_ROLE_USER, TRICKY, NULL, NULL, NULL, NULL);
append(s, CST_ROLE_ASSISTANT, "", NULL, NULL, TC,
"{\"protocol\":\"native-eos\"}");
append(s, CST_ROLE_TOOL, "int a;\n\tx\n", "c1", "read_file", NULL, NULL);
append(s, CST_ROLE_USER, "result c2 exit=0 2\nok\nend", "c2", "run_shell",
NULL, "{\"kind\":\"tool_result\",\"protocol\":\"ctl\"}");
check_messages(s);
cst_session_close(s);
/* reload from disk */
s = cst_session_open(path);
EXPECT(s != NULL, "reopen");
if (!s)
return 1;
check_messages(s);
/* every line on disk is standalone valid JSON with tool_calls as array */
FILE *fp = fopen(path, "r");
char line[8192];
int lines = 0, arrays = 0;
while (fp && fgets(line, sizeof(line), fp)) {
cJSON *j = cJSON_Parse(line);
EXPECT(j != NULL, "jsonl line parses");
if (cJSON_IsArray(cJSON_GetObjectItemCaseSensitive(j, "tool_calls")))
arrays++;
cJSON_Delete(j);
lines++;
}
if (fp)
fclose(fp);
EXPECT(lines == 5 && arrays == 1, "5 lines, tool_calls stored as array");
/* rewrite + clear(keep_system) */
const cst_message_t *first = cst_session_get(s, 0);
cst_message_t keep[2];
keep[0] = *first;
keep[1] = *cst_session_get(s, 1);
EXPECT(cst_session_rewrite(s, keep, 2) == 0, "rewrite");
EXPECT(cst_session_count(s) == 2, "rewrite count");
EXPECT(cst_session_clear(s, 1) == 0 && cst_session_count(s) == 1,
"clear keeps system");
cst_session_close(s);
/* legacy format: tool_calls_json stored as a string */
fp = fopen(path, "wb");
fprintf(fp, "{\"role\":\"assistant\",\"content\":\"\",\"tool_calls_json\":"
"\"[{\\\"id\\\":\\\"c9\\\",\\\"type\\\":\\\"function\\\","
"\\\"function\\\":{\\\"name\\\":\\\"run_shell\\\","
"\\\"arguments\\\":\\\"{}\\\"}}]\"}\n");
fprintf(fp, "not json at all\n");
fprintf(fp, "{\"role\":\"tool\",\"content\":\"out\",\"tool_call_id\":\"c9\"}\n");
fclose(fp);
s = cst_session_open(path);
EXPECT(s != NULL, "open legacy");
if (s) {
EXPECT(cst_session_count(s) == 2, "legacy: bad line skipped");
const cst_message_t *m = cst_session_get(s, 0);
EXPECT(m && m->tool_calls_json && strstr(m->tool_calls_json, "c9"),
"legacy tool_calls_json read");
cst_session_close(s);
}
remove(path);
if (nfail) {
fprintf(stderr, "test_session_roundtrip: %d failed\n", nfail);
return 1;
}
printf("test_session_roundtrip: OK\n");
return 0;
}