config: a leading ~ is the home directory

The shipped --init-config template suggests "session_dir = ~/.splinter" and
"tools_root = ~/src". Nothing expanded "~": no shell is involved in reading
the file, so the first created a directory literally named "~" in the cwd and
the second made the tool fence match nothing.

set_path() now expands a leading "~/" (or a bare "~") for the settings that
name a place on disk: session_dir, state_dir, skills_dir, tools_root. A
"~user" form is left alone rather than half-guessed.

Verified: the template with both lines uncommented starts, writes its sessions
under $HOME and leaves no "~" directory behind; tests/test_config covers the
expansion (3 checks red without it, green with it); make check: all tests
passed.
This commit is contained in:
lotherk committed 2026-11-23 16:55:49 +00:00
1 parent 1a17d25659
commit 6166acfdea
3 files changed
+55 -4

No files matched your search

+5
View File
@@ -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 |
+27 -4
View File
@@ -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) {
+23
View File
@@ -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);