/* * splinter -- focus plugin tests. * * The rule under test: a reminder every N turns, inserted before the newest * user message, never twice in the same turn, and nothing at all when no goal * is configured. The turn counter is the host's, driven here directly. */ #include #include #include #include #include #include "splinter.h" #include "splinter_plugin.h" #include "i18n.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ failures++; \ } \ } while (0) /* * The focus plugin ignores the budget, but the core compactor does not: it * trims and digests whenever the conversation does not fit. This file tests * reminders, so it hands over a budget nothing can exceed. The old "1" meant * "one byte" the moment the core started reading it, and the core then ate the * reminders the test was looking for. */ #define NO_PRESSURE ((size_t)-1) static int count_focus(const struct sp_conv *c) { size_t i; int n = 0; for (i = 0; i < c->n; i++) if (c->v[i].content != NULL && strstr(c->v[i].content, "[focus]") != NULL) n++; return n; } int main(void) { struct sp_buf ierr; /* the plugin speaks through the catalogue; load the English one */ if (sp_buf_init(&ierr) == 0) { sp_i18n_add_dir("share/locale"); (void)sp_i18n_load(&ierr, "en"); sp_buf_free(&ierr); } struct sp_registry reg; struct sp_agent ag; struct sp_host *host; struct sp_buf err; struct sp_conv conv; memset(®, 0, sizeof(reg)); memset(&ag, 0, sizeof(ag)); if (sp_buf_init(&err) != 0) return 1; /* ---- no goal configured: the plugin stays quiet ---- */ unsetenv("SPLINTER_FOCUS_GOAL"); host = sp_host_new(®, &ag, NULL); CHECK(host != NULL); CHECK(sp_plugin_load(host, "plugins/focus/focus.so", &err) == 0); sp_conv_init(&conv); CHECK(sp_conv_push(&conv, "user", NULL, "hallo") == 0); sp_host_turn(host); CHECK(sp_host_compact(host, &conv, 0) == 0); CHECK(count_focus(&conv) == 0); sp_conv_free(&conv); sp_host_free(host); /* ---- with a goal: one reminder every two turns ---- */ setenv("SPLINTER_FOCUS_GOAL", "Ship the config file", 1); setenv("SPLINTER_FOCUS_EVERY", "2", 1); host = sp_host_new(®, &ag, NULL); CHECK(host != NULL); CHECK(sp_plugin_load(host, "plugins/focus/focus.so", &err) == 0); sp_conv_init(&conv); CHECK(sp_conv_push(&conv, "system", NULL, "du bist knapp") == 0); CHECK(sp_conv_push(&conv, "user", NULL, "erster turn") == 0); sp_host_turn(host); /* the first request carries the goal itself, before the user's line */ CHECK(sp_host_compact(host, &conv, 0) == 1); CHECK(count_focus(&conv) == 1); CHECK(strstr(conv.v[conv.n - 1].content, "erster turn") != NULL); CHECK(strstr(conv.v[conv.n - 2].content, "Ship the config file") != NULL); CHECK(strstr(conv.v[conv.n - 2].content, "GOAL_COMPLETE") != NULL); { char msg[256]; msg[0] = '\0'; CHECK(sp_host_status_take(host, msg, sizeof(msg)) == 1); CHECK(strstr(msg, "goal in the prompt") != NULL); } /* turn 2: the reminder appears, once, and says so */ CHECK(sp_conv_push(&conv, "user", NULL, "zweiter turn") == 0); sp_host_turn(host); CHECK(sp_host_compact(host, &conv, NO_PRESSURE) == 1); CHECK(count_focus(&conv) == 2); { char msg[256], snap[1024]; msg[0] = '\0'; CHECK(sp_host_status_take(host, msg, sizeof(msg)) == 1); CHECK(strstr(msg, "recap applied") != NULL); snap[0] = '\0'; sp_host_widget_snapshot(host, snap, sizeof(snap)); CHECK(strstr(snap, "recap 1, every 2 turns") != NULL); } CHECK(strstr(conv.v[conv.n - 1].content, "zweiter turn") != NULL); { size_t i; /* it sits before the newest user message, not after it */ for (i = 0; i < conv.n; i++) if (conv.v[i].content != NULL && strstr(conv.v[i].content, "[focus]") != NULL) CHECK(i < conv.n - 1); } CHECK(strstr(conv.v[1].content, "Ship the config file") != NULL || strstr(conv.v[2].content, "Ship the config file") != NULL); /* compacting again in the same turn must not add a second one */ CHECK(sp_host_compact(host, &conv, NO_PRESSURE) == 0); CHECK(count_focus(&conv) == 2); /* turn 3: silent */ CHECK(sp_conv_push(&conv, "user", NULL, "dritter turn") == 0); sp_host_turn(host); CHECK(sp_host_compact(host, &conv, NO_PRESSURE) == 0); CHECK(count_focus(&conv) == 2); /* turn 4: second reminder, with the completion rule in it */ CHECK(sp_conv_push(&conv, "user", NULL, "vierter turn") == 0); sp_host_turn(host); CHECK(sp_host_compact(host, &conv, NO_PRESSURE) == 1); CHECK(count_focus(&conv) == 3); CHECK(strstr(conv.v[conv.n - 2].content, "GOAL_COMPLETE") != NULL); /* ---- /goal is the recap feature; /recap is not a command ---- */ { struct sp_buf out; size_t i; int found = 0; CHECK(sp_buf_init(&out) == 0); sp_conv_free(&conv); sp_conv_init(&conv); CHECK(sp_conv_push(&conv, "user", NULL, "start") == 0); sp_buf_clear(&out); CHECK(sp_host_command(host, "goal", "", &out) == 0); CHECK(strstr(out.p, "Ship the config file") != NULL); sp_buf_clear(&out); CHECK(sp_host_command(host, "goal", "neues ziel", &out) == 0); CHECK(strstr(out.p, "neues ziel") != NULL); /* the goal prompt starts a turn; no recap exists yet */ CHECK(sp_conv_push(&conv, "user", NULL, "Goal: neues ziel. Write a short recap.") == 0); sp_host_turn(host); CHECK(sp_host_compact(host, &conv, NO_PRESSURE) == 0); /* the answer is the recap; the next turn injects it */ CHECK(sp_conv_push(&conv, "assistant", NULL, "KURZ: neues ziel, erster Schritt") == 0); CHECK(sp_conv_push(&conv, "user", NULL, "weiter") == 0); sp_host_turn(host); CHECK(sp_host_compact(host, &conv, NO_PRESSURE) == 1); CHECK(count_focus(&conv) == 1); for (i = 0; i < conv.n; i++) if (conv.v[i].content != NULL && strstr(conv.v[i].content, "[focus]") != NULL) { CHECK(strstr(conv.v[i].content, "KURZ") != NULL); found = 1; } CHECK(found); /* /recap is gone; /goal carries the recap now */ sp_buf_clear(&out); CHECK(sp_host_command(host, "recap", "", &out) == -1); CHECK(strstr(out.p, "unknown command") != NULL); /* an unknown command is refused and says so */ sp_buf_clear(&out); CHECK(sp_host_command(host, "gibtsnicht", "", &out) == -1); CHECK(strstr(out.p, "unknown command") != NULL); /* * The goal ends with GOAL_COMPLETE. The next compaction sees the * newest assistant answer, marks the goal succeeded and stops * reminding; /goal and the sidebar say so. */ CHECK(sp_conv_push(&conv, "assistant", NULL, "alles erledigt\nGOAL_COMPLETE") == 0); CHECK(sp_conv_push(&conv, "user", NULL, "und jetzt?") == 0); sp_host_turn(host); { int before = count_focus(&conv); char snap[1024]; CHECK(sp_host_compact(host, &conv, NO_PRESSURE) == 0); CHECK(count_focus(&conv) == before); snap[0] = '\0'; sp_host_widget_snapshot(host, snap, sizeof(snap)); CHECK(strstr(snap, "[done]") != NULL); CHECK(strstr(snap, "neues ziel") != NULL); } sp_buf_clear(&out); CHECK(sp_host_command(host, "goal", "", &out) == 0); CHECK(strstr(out.p, "succeeded") != NULL); /* succeeded is final for this goal: no more reminders */ sp_buf_clear(&out); CHECK(sp_host_command(host, "goal", "", &out) == 0); CHECK(strstr(out.p, "succeeded") != NULL); CHECK(sp_conv_push(&conv, "user", NULL, "noch ein turn") == 0); sp_host_turn(host); CHECK(sp_host_compact(host, &conv, NO_PRESSURE) == 0); /* a new goal opens a new one, and is announced again */ sp_buf_clear(&out); CHECK(sp_host_command(host, "goal", "zweites ziel", &out) == 0); CHECK(sp_conv_push(&conv, "user", NULL, "los") == 0); sp_host_turn(host); CHECK(sp_host_compact(host, &conv, NO_PRESSURE) == 1); { char snap[1024]; snap[0] = '\0'; sp_host_widget_snapshot(host, snap, sizeof(snap)); CHECK(strstr(snap, "[done]") == NULL); CHECK(strstr(snap, "zweites ziel") != NULL); } sp_buf_free(&out); } sp_conv_free(&conv); sp_host_free(host); /* * ---- the tick reads the answer from the session log ---- * * The compactor sees the conversation; a turn that ends has no next * compactor call. The tick watches the session file, so the goal turns * "[done]" without waiting for another turn. */ { static struct sp_session_info si; char dir[] = "/tmp/opencode/focus-XXXXXX"; char snap[1024], st[16]; struct sp_conv c2; CHECK(mkdtemp(dir) != NULL); snprintf(si.dir, sizeof(si.dir), "%s", dir); snprintf(si.id, sizeof(si.id), "sess-goaltest"); CHECK(sp_session_append(&si, "user", "start", &err) == 0); CHECK(sp_session_set_goal(dir, si.id, "tick goal", &err) == 0); host = sp_host_new(®, &ag, &si); CHECK(host != NULL); CHECK(sp_plugin_load(host, "plugins/focus/focus.so", &err) == 0); sp_conv_init(&c2); CHECK(sp_conv_push(&c2, "user", NULL, "erste anfrage") == 0); sp_host_turn(host); CHECK(sp_host_compact(host, &c2, 0) == 1); /* announced, armed */ snap[0] = '\0'; sp_host_widget_snapshot(host, snap, sizeof(snap)); CHECK(strstr(snap, "tick goal") != NULL); CHECK(strstr(snap, "[done]") == NULL); /* the model answers after the goal went into the prompt */ { struct timespec ts = { 1, 100 * 1000 * 1000L }; nanosleep(&ts, NULL); } CHECK(sp_session_append(&si, "assistant", "fertig\nGOAL_COMPLETE", &err) == 0); { struct timespec ts = { 1, 600 * 1000 * 1000L }; nanosleep(&ts, NULL); /* let the tick run */ } snap[0] = '\0'; sp_host_widget_snapshot(host, snap, sizeof(snap)); CHECK(strstr(snap, "[done]") != NULL); CHECK(sp_session_get_goal_state(dir, si.id, st, sizeof(st)) == 0); CHECK(strcmp(st, "succeeded") == 0); /* and the state survives a fresh host: the meta is the truth */ sp_conv_free(&c2); sp_host_free(host); host = sp_host_new(®, &ag, &si); CHECK(host != NULL); CHECK(sp_plugin_load(host, "plugins/focus/focus.so", &err) == 0); { struct sp_buf out; CHECK(sp_buf_init(&out) == 0); CHECK(sp_host_command(host, "goal", "", &out) == 0); CHECK(strstr(out.p, "succeeded") != NULL); sp_buf_free(&out); } sp_host_free(host); { char path[700]; snprintf(path, sizeof(path), "%s/%s.jsonl", dir, si.id); (void)unlink(path); snprintf(path, sizeof(path), "%s/%s.meta", dir, si.id); (void)unlink(path); } (void)rmdir(dir); } sp_buf_free(&err); sp_registry_free(®); if (failures == 0) printf("test_focus: ok\n"); return failures == 0 ? 0 : 1; }