diff --git a/docs/CONFIG.md b/docs/CONFIG.md index 75d6fce..593133a 100644 --- a/docs/CONFIG.md +++ b/docs/CONFIG.md @@ -239,6 +239,11 @@ lies about what it does. ## Keys +A setting that names a path (`session_dir`, `state_dir`, `skills_dir`) takes a +leading `~` as the home directory, exactly as the shipped template writes it: +`~/src` is `$HOME/src`. Any other path is used verbatim. The `${NAME}` form +inserts an environment variable. + | key | meaning | |-----|---------| | `endpoint` | the API base, e.g. `http://127.0.0.1:8080/v1`; splinter appends `/chat/completions` (or `/messages` for `kind = anthropic`), `/models`, and the usage routes. A full route still works: it is cut back to the base | diff --git a/src/backend/config.c b/src/backend/config.c index 608e7f8..5e95ed6 100644 --- a/src/backend/config.c +++ b/src/backend/config.c @@ -115,6 +115,29 @@ set_str(char *dst, size_t len, const char *src) snprintf(dst, len, "%s", src); } +/* + * A setting that names a place on disk: "~/x" is the home directory and "~" + * is home itself. + * + * The parser does not expand "~" - no shell is involved - so a config that + * says "~/src" would otherwise fence the file tools against a directory + * literally named "~". The shipped template suggests exactly that, so it has to + * work. Only a leading "~/" (or a bare "~") is touched; a "~user" form is left + * alone rather than half-guessed, because there is no portable way to look up + * another user's home here. + */ +static void +set_path(char *dst, size_t len, const char *src) +{ + const char *home = getenv("HOME"); + + if (home != NULL && *home != '\0' && src[0] == '~' && + (src[1] == '/' || src[1] == '\0')) + snprintf(dst, len, "%s%s", home, src + 1); + else + snprintf(dst, len, "%s", src); +} + /* * delegate = id | what it is good at | tags=a,b | endpoint=... | model=... * @@ -717,9 +740,9 @@ load_stream(struct sp_config *c, FILE *f, const char *path, else if (!strcmp(key, "system_prompt")) set_str(c->system_prompt, sizeof(c->system_prompt), val); else if (!strcmp(key, "session_dir")) - set_str(c->session_dir, sizeof(c->session_dir), val); + set_path(c->session_dir, sizeof(c->session_dir), val); else if (!strcmp(key, "state_dir")) - set_str(c->state_dir, sizeof(c->state_dir), val); + set_path(c->state_dir, sizeof(c->state_dir), val); else if (!strcmp(key, "cli_cmd")) set_str(c->cli_cmd, sizeof(c->cli_cmd), val); else if (!strcmp(key, "cli_fallback")) @@ -830,8 +853,8 @@ load_stream(struct sp_config *c, FILE *f, const char *path, SP_CONFIG_MAX_DELEGATES); return -1; } - snprintf(c->skills_dir[c->nskills_dir++], - sizeof(c->skills_dir[0]), "%s", val); + set_path(c->skills_dir[c->nskills_dir++], + sizeof(c->skills_dir[0]), val); } else if (!strcmp(key, "exit")) { if (c->nexits == SP_CONFIG_MAX_DELEGATES) { diff --git a/tests/test_config.c b/tests/test_config.c index 9058efa..b0da5d7 100644 --- a/tests/test_config.c +++ b/tests/test_config.c @@ -105,6 +105,29 @@ main(void) /* untouched defaults survive */ CHECK(strcmp(c.key_env, "SPLINTER_API_KEY") == 0); + /* ---- a leading ~ is the home directory, not a directory named "~" --- */ + { + const char *home = getenv("HOME"); + + write_file(path, + "session_dir = ~/.splinter\n" + "skills_dir = ~/skills\n" + "state_dir = /tmp/state\n"); + sp_config_init(&c); + sp_buf_clear(&err); + CHECK(sp_config_load(&c, path, &err) == 0); + if (home != NULL && *home != '\0') { + char want[512]; + + snprintf(want, sizeof(want), "%s/.splinter", home); + CHECK(strcmp(c.session_dir, want) == 0); + snprintf(want, sizeof(want), "%s/skills", home); + CHECK(strcmp(c.skills_dir[0], want) == 0); + } + /* an absolute path is left exactly as written */ + CHECK(strcmp(c.state_dir, "/tmp/state") == 0); + } + /* ---- an unknown key is refused, with its line number ---- */ write_file(path, "endpoint = x\nwolke = 7\n"); sp_config_init(&c);