Compaction is not a flag in the loop; it is a compactor registered by a plugin and run on the main thread before every request, with the whole conversation in hand. Returning 1 means changed, 0 nothing to do, -1 failed - and a failure is counted, not fatal. lcm: deterministic lossy compression. Over budget, it keeps the leading system prompt and the last six messages and inserts one system message that says how many messages went, of which roles, and quotes the head of the oldest dropped turn. No second model, so a run stays reproducible and costs nothing extra. cps: continuous parallel compression. The worker thread decides what would go from an index it got under a mutex (role, size, 64 bytes of head per message) and never touches the conversation; the main thread splices the prepared plan in one pass when the budget is hit. The sidebar line shows applied/prepared counts or a ready plan, so the claim is visible. The limit is in both files: they can decide which messages go, not summarise their text, because the text is not the worker's to read. Ten test binaries green; test_compactor drives both against their real .so files, including that lcm leaves the system prompt first, inserts the [lcm] record at index 1, keeps the tail verbatim, and that cps needs two calls (prepare, then apply) to change anything.
95 lines
3.3 KiB
C
95 lines
3.3 KiB
C
/*
|
|
* splinter -- plugin ABI.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* A plugin is a shared object loaded with dlopen(). It links against
|
|
* libsplinter.so, never against the binary, so there is exactly one copy of
|
|
* every symbol in the process.
|
|
*
|
|
* What a plugin may do, and where:
|
|
*
|
|
* init() called once, on the main thread, before any turn. Register
|
|
* tools and widgets here. May fail; a failed plugin is unloaded
|
|
* from the set and reported, never half-used.
|
|
* run() called on the main thread while a turn is being handled.
|
|
* tick() called on the widget's own thread, so it may block, read
|
|
* files, wait on sockets. It must NOT touch curses and must NOT
|
|
* assume it is the only thread.
|
|
* draw() called on the UI thread only, must be fast and non-blocking.
|
|
* A widget without draw() is rendered from its published text.
|
|
*
|
|
* Text that should appear in the sidebar is published with
|
|
* sp_host_widget_text(); the host copies it under a mutex, so the string does
|
|
* not have to outlive the call.
|
|
*/
|
|
#ifndef SPLINTER_PLUGIN_H
|
|
#define SPLINTER_PLUGIN_H
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "splinter.h"
|
|
|
|
#define SP_PLUGIN_ABI 1u
|
|
|
|
struct sp_host;
|
|
|
|
struct sp_widget {
|
|
const char *name; /* unique in the sidebar */
|
|
int (*tick)(struct sp_host *, const char *name, void *ctx);
|
|
void (*draw)(struct sp_host *, const char *name, void *ctx);
|
|
void *ctx;
|
|
unsigned interval_ms; /* 0: tick once, then stop */
|
|
};
|
|
|
|
/*
|
|
* A compactor runs on the main thread before every request, with the whole
|
|
* conversation in hand. It may rewrite it. Returning 1 means "changed", 0
|
|
* "nothing to do", -1 "failed" - and a failure is reported, never ignored.
|
|
*/
|
|
struct sp_compactor {
|
|
const char *name;
|
|
int (*compact)(struct sp_host *, struct sp_conv *,
|
|
size_t budget_bytes, unsigned turn);
|
|
};
|
|
|
|
struct sp_plugin {
|
|
uint32_t abi; /* must equal SP_PLUGIN_ABI */
|
|
const char *name;
|
|
const char *version;
|
|
int (*init)(struct sp_host *);
|
|
int (*run)(struct sp_host *, const char *input,
|
|
struct sp_buf *out);
|
|
void (*shutdown)(struct sp_host *);
|
|
};
|
|
|
|
/* Every plugin exports exactly this. */
|
|
const struct sp_plugin *sp_plugin_describe(void);
|
|
|
|
/* ---- host API, implemented by splinter, called by plugins ---- */
|
|
|
|
uint32_t sp_host_abi(struct sp_host *);
|
|
int sp_host_add_tool(struct sp_host *, const struct sp_tool *);
|
|
int sp_host_add_widget(struct sp_host *, const struct sp_widget *);
|
|
int sp_host_widget_text(struct sp_host *, const char *widget,
|
|
const char *text);
|
|
int sp_host_note(struct sp_host *, const char *text);
|
|
int sp_host_add_compactor(struct sp_host *, const struct sp_compactor *);
|
|
int sp_host_compact(struct sp_host *, struct sp_conv *, size_t budget);
|
|
size_t sp_host_compaction_stats(struct sp_host *, size_t *compactions,
|
|
size_t *failed, unsigned *turns);
|
|
void sp_host_turn(struct sp_host *);
|
|
|
|
/* ---- host side, implemented by splinter, called by the harness ---- */
|
|
|
|
struct sp_host *sp_host_new(struct sp_registry *, struct sp_agent *,
|
|
struct sp_session_info *);
|
|
void sp_host_free(struct sp_host *);
|
|
int sp_plugin_load(struct sp_host *, const char *path,
|
|
struct sp_buf *err);
|
|
size_t sp_host_widget_snapshot(struct sp_host *, char *buf, size_t len);
|
|
size_t sp_host_widget_count(struct sp_host *);
|
|
|
|
#endif /* SPLINTER_PLUGIN_H */
|