diff --git a/include/splinter.h b/include/splinter.h index 08c3451..eb2928d 100644 --- a/include/splinter.h +++ b/include/splinter.h @@ -276,6 +276,8 @@ int sp_session_folder_history(const char *dir, const char *cwd, */ int sp_session_folder_note(const char *dir, const char *skip_id, struct sp_buf *out); +/* a paragraph naming the working directory as the default task scope */ +int sp_task_scope_note(struct sp_buf *out); /* the working directory a session was started in (meta sidecar) */ int sp_session_cwd(const char *dir, const char *id, char *buf, size_t cap); int sp_session_load(const struct sp_session_info *, struct sp_conv *conv, @@ -522,6 +524,9 @@ const volatile int *sp_cancel_flag(void); */ void sp_openai_set_session(const char *id); size_t sp_openai_session_header(const char *url, char *buf, size_t cap); +/* /effort: review depth for the next requests; -1 lets the router decide */ +void sp_openai_set_review_depth(int depth); +int sp_openai_review_depth(void); /* * One route of an endpoint: the config names the API base (https://host/v1), diff --git a/src/backend/engine.c b/src/backend/engine.c index c2d385f..8284818 100644 --- a/src/backend/engine.c +++ b/src/backend/engine.c @@ -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(¬e) == 0) { (void)sp_session_folder_note(e->sess.dir, e->sess.id, ¬e); + if (note.len > 0 && note.p != NULL) + (void)sp_buf_puts(¬e, "\n\n"); + (void)sp_task_scope_note(¬e); used = strlen(e->prompt_expanded); if (note.len > 0 && note.p != NULL && used + note.len + 2 < sizeof(e->prompt_expanded)) { diff --git a/src/backend/session.c b/src/backend/session.c index f323f42..efc7ee4 100644 --- a/src/backend/session.c +++ b/src/backend/session.c @@ -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) diff --git a/src/backend/transport_openai.c b/src/backend/transport_openai.c index 799f2dd..b87179c 100644 --- a/src/backend/transport_openai.c +++ b/src/backend/transport_openai.c @@ -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)); diff --git a/tests/test_engine.c b/tests/test_engine.c index 9e86a6c..e057f3b 100644 --- a/tests/test_engine.c +++ b/tests/test_engine.c @@ -211,6 +211,13 @@ main(void) contains(c->v[0].content, "earlier sessions")); CHECK(c != NULL && c->n > 0 && contains(c->v[0].content, "session_search")); + /* The working directory names the default task scope, so a project + * question does not follow the busiest scope in the store. */ + CHECK(c != NULL && c->n > 0 && + contains(c->v[0].content, "use the task scope")); + CHECK(c != NULL && c->n > 0 && + contains(c->v[0].content, + "Do not present another project's scopes")); sp_prompt_default(def, sizeof(def), "test-model"); CHECK(contains(def, "knowledge tools actively")); CHECK(contains(def, "knowledge bases")); @@ -296,6 +303,27 @@ main(void) CHECK(contains(out.p, "agent build")); sp_buf_clear(&out); + /* /effort sets the review depth the transport puts into the header */ + kind = sp_engine_input(eng, "/effort max", &out); + CHECK(kind == SP_INPUT_HANDLED); + CHECK(sp_openai_review_depth() == 12); + CHECK(contains(out.p, "12 review rounds")); + sp_buf_clear(&out); + kind = sp_engine_input(eng, "/effort 2", &out); + CHECK(kind == SP_INPUT_HANDLED); + CHECK(sp_openai_review_depth() == 2); + sp_buf_clear(&out); + kind = sp_engine_input(eng, "/effort off", &out); + CHECK(kind == SP_INPUT_HANDLED); + CHECK(sp_openai_review_depth() == 0); + CHECK(contains(out.p, "off")); + sp_buf_clear(&out); + kind = sp_engine_input(eng, "/effort nope", &out); + CHECK(kind == SP_INPUT_HANDLED); + CHECK(sp_openai_review_depth() == 0); + CHECK(contains(out.p, "off|low|medium|high|max")); + sp_buf_clear(&out); + kind = sp_engine_input(eng, "/no-such-command", &out); CHECK(kind == SP_INPUT_HANDLED); CHECK(contains(out.p, "/no-such-command"));