v0.8.7: tool capability metadata, in-process engine, Qt app, Android engine

The engine can be embedded now instead of only driven from a terminal: the
same objects link into a Qt front end (app/) that owns a cst_agent_t inside
its own process -- no port, no token, no second process -- and that front end
now runs on the desktop and, engine-side, on the phone.

Capability metadata (include/cst_tools.h, src/tools/registry.c):
- cst_tool_def_t gains perm_name, platforms and available (a probe that
  returns a reason), so a permission is addressable by name and a tool can
  say why it cannot run here.
- cst_platform_name(), cst_tool_platform_ok(), cst_tool_available(),
  cst_tool_meta_reload(), cst_tool_perm_name() and CST_PLAT_DESKTOP[].
- cst_tool_register() may take over a built-in name that is not runnable on
  this platform, so a front end replaces it with its own implementation.
- the registry entries carry their class (delete_file -> "delete"), the
  desktop clipboard tools carry a clipboard probe; find/active/run gate on it.
- /api/settings.tools[] exposes perm_name, platforms, available, why and the
  declared parameters.

Engine as a library (Makefile, CMakeLists.txt):
- libcstcore.a from the CORE_OBJ set, and a CMake cstcore target shared by
  the CLI and by app/ (-DCST_BUILD_APP=ON): the engine is compiled once.
- Android branch for the vendored sashidown: a per-ABI archive built with the
  NDK's aarch64-linux-android24-clang wrapper, because CMake's compiler is
  plain clang and keeps --target/--sysroot in its own variables.

Qt front end (app/, new):
- AgentCore wraps one cst_agent_t: turns run on a worker thread, every
  cst_agent_hooks_t callback becomes a Qt signal, and the two hooks that need
  an answer (on_permission, on_ask_user) block the worker until the GUI
  replies; mode, approval and plan mode go through the engine's config.
- ToolProvider hands the front end's tools to the engine (owner "app:*"); the
  Linux one is notification_send with a probe for notify-send/D-Bus.
- Main.qml: transcript, tool blocks, consent dialog (Deny/Once/Always ->
  CST_PERM_*), ask_user, mode switch, Esc to cancel; --selftest stays
  headless and runs the embedding checks without QML.

Android (scripts/, docs/android.md, new):
- scripts/build_third_party_android.sh builds static mbedTLS + libcurl for an
  ABI (CA path /system/etc/security/cacerts); build_android.sh builds
  cst-agent, libcstcore.a and the test binaries for the phone, can push them
  and run the whole suite there.
- sandbox_exec.c keeps the plain exec path on Android: the seccomp supervisor
  compiled in and the probe reported "seccomp", but writes outside the
  workspace were not denied on the device -- claiming a sandbox that does not
  hold is worse than reporting none.
- tests/test_diff.c compares layouts only against real GNU diffutils: toybox's
  diff prints the same hunk header with its own hunk/context rules.

Tests: test_tool_meta (metadata, platform gate, probe reasons, takeover of a
built-in name) and test_embed (in-process embedding contract) are new; the
existing registry/llm_body assertions now follow availability. 38 test
binaries pass on the host, 36/36 on arm64-v8a Android 16, cst-app-selftest
passes in both the standalone app/ build and the repo build.

Version 0.8.7.
This commit is contained in:
WHC
2026-10-11 11:42:04 +08:00
parent e0b0eddf3a
commit 5ba215b36c
28 changed files with 2452 additions and 34 deletions
+196
View File
@@ -0,0 +1,196 @@
/* Tool capability metadata: platform declarations, availability probes and
* the shadowing rule (an app-provided tool may take over a name whose
* built-in cannot run on this host). */
#define _POSIX_C_SOURCE 200809L
#include "cst_tools.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.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 int echo_run(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
cst_tool_result_t *r) {
(void)ctx;
(void)c;
r->out = strdup("ran");
r->out_len = r->out ? strlen(r->out) : 0;
r->exit_code = 0;
return 0;
}
static int probe_fail(char *why, size_t why_sz) {
snprintf(why, why_sz, "no notification daemon on this host");
return -1;
}
static const char *const ANDROID_ONLY[] = {"android", NULL};
static void test_platform_gating(void) {
cst_tool_def_t d;
memset(&d, 0, sizeof(d));
d.name = "meta_android_only";
d.desc = "Android-only test tool.";
d.perm = CST_TP_ASK;
d.perm_name = "notify";
d.platforms = ANDROID_ONLY;
d.run = echo_run;
d.owner = "test:meta";
EXPECT(cst_tool_register(&d) == 0, "register an android-only tool");
const cst_tool_def_t *t = cst_tool_find("meta_android_only");
EXPECT(t != NULL, "it is in the registry");
if (!t)
return;
EXPECT(strcmp(cst_tool_perm_name(t), "notify") == 0, "perm_name is kept");
setenv("CST_PLATFORM", "linux", 1);
cst_tool_meta_reload();
char why[160];
EXPECT(cst_tool_available(t, why, sizeof(why)) != 0,
"an android-only tool is unavailable on linux");
EXPECT(strstr(why, "android") != NULL, "and says what it is for");
EXPECT(!cst_tool_active(NULL, t), "so it is not offered at all");
/* the model must not be able to force it either: the call is answered with
* an error result (run() reports 0 for a tool-level failure, the exit code
* and the text carry it) */
ctl_tool_call_t call;
memset(&call, 0, sizeof(call));
snprintf(call.name, sizeof(call.name), "meta_android_only");
cst_tool_result_t r;
cst_tool_ctx_t ctx;
memset(&ctx, 0, sizeof(ctx));
ctx.workspace_root = ".";
EXPECT(cst_tool_run(&ctx, &call, &r) == 0 && r.out && r.exit_code != 0 &&
strstr(r.out, "cannot run here") != NULL,
"calling it anyway is refused with a reason");
EXPECT(strstr(r.out, "android") != NULL, "the reason names the platform");
cst_tool_result_free(&r);
setenv("CST_PLATFORM", "android", 1);
cst_tool_meta_reload();
EXPECT(cst_tool_available(t, why, sizeof(why)) == 0,
"available once the host is an android device");
EXPECT(cst_tool_active(NULL, t), "and offered to the model");
char *names = cst_tools_names(NULL);
EXPECT(names && strstr(names, "meta_android_only") != NULL,
"it shows up in the active tool names");
free(names);
/* a Termux host counts as android; "termux" is not matched by "android" */
setenv("CST_PLATFORM", "termux", 1);
cst_tool_meta_reload();
EXPECT(cst_tool_available(t, why, sizeof(why)) == 0,
"a Termux host is an android device");
/* the desktop pseudo-platform covers linux / win / macos */
cst_tool_def_t desk;
memset(&desk, 0, sizeof(desk));
desk.name = "meta_desktop";
desk.desc = "Desktop-only test tool.";
desk.perm = CST_TP_READ;
desk.platforms = CST_PLAT_DESKTOP;
desk.run = echo_run;
desk.owner = "test:meta";
EXPECT(cst_tool_register(&desk) == 0, "register a desktop-only tool");
const cst_tool_def_t *dt = cst_tool_find("meta_desktop");
setenv("CST_PLATFORM", "linux", 1);
cst_tool_meta_reload();
EXPECT(dt && cst_tool_available(dt, why, sizeof(why)) == 0, "desktop on linux");
setenv("CST_PLATFORM", "android", 1);
cst_tool_meta_reload();
EXPECT(dt && cst_tool_available(dt, why, sizeof(why)) != 0,
"not on android");
}
static void test_probe(void) {
cst_tool_def_t d;
memset(&d, 0, sizeof(d));
d.name = "meta_no_daemon";
d.desc = "A tool whose service is missing.";
d.perm = CST_TP_ASK;
d.available = probe_fail;
d.run = echo_run;
d.owner = "test:meta";
EXPECT(cst_tool_register(&d) == 0, "register a tool with a failing probe");
const cst_tool_def_t *t = cst_tool_find("meta_no_daemon");
char why[160];
setenv("CST_PLATFORM", "linux", 1);
cst_tool_meta_reload();
EXPECT(t && cst_tool_available(t, why, sizeof(why)) != 0, "probe says no");
EXPECT(strstr(why, "notification daemon") != NULL,
"the probe's own reason is passed on");
EXPECT(t && !cst_tool_active(NULL, t), "and it is not offered");
/* the cache answers the same way, and a reload re-probes */
EXPECT(t && cst_tool_available(t, NULL, 0) != 0, "cached answer");
cst_tool_meta_reload();
EXPECT(t && cst_tool_available(t, why, sizeof(why)) != 0, "re-probed answer");
}
static void test_shadowing(void) {
/* A tool the app provides may take a built-in name when that built-in
* cannot run here; the app's entry then shadows it. */
cst_tool_def_t d;
memset(&d, 0, sizeof(d));
d.name = "clipboard_read";
d.desc = "Read the clipboard (provided by the app).";
d.perm = CST_TP_READ;
d.perm_name = "read";
d.run = echo_run;
d.owner = "app:test";
int registered = cst_tool_register(&d) == 0;
const cst_tool_def_t *t = cst_tool_find("clipboard_read");
EXPECT(t != NULL, "clipboard_read stays findable either way");
if (registered) {
EXPECT(t && t->owner && strcmp(t->owner, "app:test") == 0,
"the app's tool shadows the unavailable built-in");
EXPECT(t && cst_tool_active(NULL, t), "and it is offered");
EXPECT(cst_tool_unregister_owner("app:test") == 1, "dropping it again");
t = cst_tool_find("clipboard_read");
EXPECT(t && (t->owner == NULL || strcmp(t->owner, "app:test") != 0),
"the built-in is back");
} else {
EXPECT(t && cst_tool_available(t, NULL, 0) == 0,
"a name is only taken by a tool that can run here");
}
}
static void test_perm_names(void) {
const cst_tool_def_t *t = cst_tool_find("delete_file");
EXPECT(t && strcmp(cst_tool_perm_name(t), "delete") == 0,
"delete_file is a delete, not just a write");
t = cst_tool_find("read_file");
EXPECT(t && strcmp(cst_tool_perm_name(t), "read") == 0, "read_file is read");
t = cst_tool_find("run_shell");
EXPECT(t && strcmp(cst_tool_perm_name(t), "shell") == 0, "carried by class");
t = cst_tool_find("clipboard_write");
EXPECT(t && strcmp(cst_tool_perm_name(t), "write") == 0, "clipboard_write");
EXPECT(cst_platform_name() != NULL && cst_platform_name()[0],
"host platform has a name");
}
int main(void) {
test_platform_gating();
test_probe();
test_shadowing();
test_perm_names();
cst_tool_unregister_owner("test:meta");
cst_tool_unregister_owner("app:test");
if (nfail) {
fprintf(stderr, "test_tool_meta: %d failed\n", nfail);
return 1;
}
printf("test_tool_meta: OK\n");
return 0;
}