prompt: name the working directory's task scope; /effort sets the review depth

A session in ~/src/mikronix asked for "open tasks", called task_list without a
scope, got the makronix list and answered with it. The system prompt now names
the working directory as the default task scope (sp_task_scope_note), so the
model says "mikronix" instead of following the busiest scope. tasksd's
task_scopes/task_list descriptions now tell it to pass that scope (separate
repo, commit 4781f60).

/effort (off/low/medium/high/max or 0..12) sets X-Junction-Review-Rounds for
this process; unset keeps the router rule. The transport sends the header on
every request while set, so one session can run 0, 7 or 12 rounds without a
junction config change. /effort alone prints the current value.

test_engine: the prompt carries the scope paragraph; /effort maps to 12/2/0 and
rejects junk. make check: all tests passed. make check-asan: 41/41 clean.
This commit is contained in:
lotherk committed 2026-11-19 11:37:32 +00:00
1 parent 2581885e67
commit eba6f71a2d
5 files changed
+148 -6

No files matched your search

+63 -3
View File
@@ -1948,9 +1948,9 @@ int
sp_engine_command_names(struct sp_engine *e, char (*names)[64], int max)
{
static const char *builtin[] = {
"agent", "agents", "compact", "connect", "continue", "cost", "exit", "model",
"new", "q", "quit", "rename", "reset", "resume", "rm", "self-check", "status",
"swarm", "usage", "widgets"
"agent", "agents", "compact", "connect", "continue", "cost", "effort", "exit",
"model", "new", "q", "quit", "rename", "reset", "resume", "rm",
"self-check", "status", "swarm", "usage", "widgets"
};
struct sp_buf b;
char cdir[700], *p, *tok, *save;
@@ -2664,6 +2664,59 @@ self_check_from(const struct sp_conv *c, size_t turns)
* in it, so it diagnoses itself instead of asking the operator. It runs as a
* real turn, so the model can go and repair the step it got wrong.
*/
/*
* /effort: how many hidden review rounds the router should run for this
* session. The names match junction's X-Junction-Effort header; a bare number
* is the exact count (0..12). Unset means the rule decides.
*/
static void
cmd_effort(const char *args, struct sp_buf *out)
{
int depth = -1;
if (args == NULL || *args == '\0') {
int cur = sp_openai_review_depth();
if (cur < 0)
sp_buf_puts(out, "effort: router rule decides");
else
sp_buf_printf(out, "effort: %d review round%s", cur,
cur == 1 ? "" : "s");
return;
}
if (!strcasecmp(args, "off") || !strcasecmp(args, "none"))
depth = 0;
else if (!strcasecmp(args, "low") || !strcasecmp(args, "default"))
depth = 4;
else if (!strcasecmp(args, "medium"))
depth = 7;
else if (!strcasecmp(args, "high"))
depth = 9;
else if (!strcasecmp(args, "max"))
depth = 12;
else {
char *end = NULL;
long n = strtol(args, &end, 10);
if (end != args) {
while (*end == ' ' || *end == '\t')
end++;
if (*end == '\0' && n >= 0 && n <= 12)
depth = (int)n;
}
}
if (depth < 0) {
sp_buf_puts(out, "effort: off|low|medium|high|max or 0..12");
return;
}
sp_openai_set_review_depth(depth);
if (depth == 0)
sp_buf_puts(out, "effort: 0 review rounds (off)");
else
sp_buf_printf(out, "effort: %d review round%s", depth,
depth == 1 ? "" : "s");
}
static int
cmd_self_check(struct sp_engine *e, struct sp_buf *out)
{
@@ -2804,6 +2857,10 @@ sp_engine_input(struct sp_engine *e, const char *line, struct sp_buf *out)
sp_usage_report(e->conf, out);
return SP_INPUT_HANDLED;
}
if (strcmp(name, "effort") == 0) {
cmd_effort(args, out);
return SP_INPUT_HANDLED;
}
if (strcmp(name, "compact") == 0) {
size_t before = sp_conv_bytes(&e->conv);
size_t msgs = e->conv.n;
@@ -3187,6 +3244,9 @@ setup_conversation(struct sp_engine *e, const struct sp_engine_opts *o,
if (sp_buf_init(&note) == 0) {
(void)sp_session_folder_note(e->sess.dir, e->sess.id,
&note);
if (note.len > 0 && note.p != NULL)
(void)sp_buf_puts(&note, "\n\n");
(void)sp_task_scope_note(&note);
used = strlen(e->prompt_expanded);
if (note.len > 0 && note.p != NULL &&
used + note.len + 2 < sizeof(e->prompt_expanded)) {
+27
View File
@@ -837,6 +837,33 @@ sp_session_folder_note(const char *dir, const char *skip_id, struct sp_buf *out)
return 0;
}
/*
* The task store groups work by scope. A session in ~/src/mikronix is about
* mikronix, not about the project with the most open tasks; say the scope out
* loud so the model does not follow the busiest list it sees.
*/
int
sp_task_scope_note(struct sp_buf *out)
{
char cwd[512];
const char *base;
if (out == NULL)
return -1;
if (getcwd(cwd, sizeof(cwd)) == NULL)
return -1;
base = strrchr(cwd, '/');
if (base == NULL || base[1] == '\0')
return 0;
base++;
return sp_buf_printf(out,
"This working directory is the project \"%s\". For questions about "
"tasks, open work or what to do next, use the task scope \"%s\" (and "
"its children) and say so when it has nothing open. Do not present "
"another project's scopes or tasks as the answer because they are "
"larger or more numerous.\n", base, base);
}
int
sp_session_set_title(const char *dir, const char *id, const char *title,
struct sp_buf *err)
+25 -3
View File
@@ -49,6 +49,7 @@ struct openai_ctx {
*/
static const volatile int *g_cancel;
static const char *g_session;
static int g_review_depth = -1;
void
sp_openai_set_session(const char *id)
@@ -56,6 +57,22 @@ sp_openai_set_session(const char *id)
g_session = id;
}
/* Per-process review depth requested with /effort; -1 means "let the router
* apply its rule". The header is sent on every request while set, so a turn
* started after /effort max uses twelve rounds without touching junction's
* config. */
void
sp_openai_set_review_depth(int depth)
{
g_review_depth = depth;
}
int
sp_openai_review_depth(void)
{
return g_review_depth;
}
/*
* The header that carries the session id for this endpoint, or "" when the
* endpoint does not want one. opencode-go refuses a request without it
@@ -145,9 +162,9 @@ openai_curl(const struct openai_ctx *c, const char *body_path, int stream,
pid_t *pid, struct sp_buf *err, const char *hdr_path)
{
char tmo[32], data[600], auth[sizeof(c->key) + 32];
const char *args[20];
const char *hdrs[4] = { NULL, NULL, NULL, NULL };
char sess[160], ua[64];
const char *args[24];
const char *hdrs[6] = { NULL, NULL, NULL, NULL, NULL, NULL };
char sess[160], ua[64], review[64];
size_t nh = 0;
int n = 0, fd;
FILE *f;
@@ -195,6 +212,11 @@ openai_curl(const struct openai_ctx *c, const char *body_path, int stream,
*/
if (sp_openai_session_header(c->url, sess, sizeof(sess)) > 0)
hdrs[nh++] = sess;
if (g_review_depth >= 0) {
snprintf(review, sizeof(review), "X-Junction-Review-Rounds: %d",
g_review_depth);
hdrs[nh++] = review;
}
hdrs[nh] = NULL;
fd = sp_curl_start(args, c->url, hdrs, 0, pid, err);
memset(auth, 0, sizeof(auth));