summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/command.zig8
-rw-r--r--src/compaction.zig2
-rw-r--r--src/main.zig20
-rw-r--r--src/system_prompt.zig107
4 files changed, 73 insertions, 64 deletions
diff --git a/src/command.zig b/src/command.zig
index 49ad5b8..0f6fb01 100644
--- a/src/command.zig
+++ b/src/command.zig
@@ -32,10 +32,10 @@ const panto = @import("panto");
pub const Context = struct {
allocator: std.mem.Allocator,
- /// The active agent. Owns the conversation (`agent.conversation`) and
- /// the session store (`agent.session_store`); commands reach both
- /// through it rather than holding separate pointers.
- agent: *panto.agent.Agent,
+ /// The active agent (a copyable public handle). Commands reach the
+ /// conversation via `agent.conversation()` and compact via
+ /// `agent.compact(...)`.
+ agent: panto.Agent,
/// REPL output writer and its backing file writer (for flushing).
stdout: *std.Io.Writer,
diff --git a/src/compaction.zig b/src/compaction.zig
index 4ec5732..5d64f98 100644
--- a/src/compaction.zig
+++ b/src/compaction.zig
@@ -22,7 +22,7 @@ pub fn register(registry: *command.Registry) !void {
fn run(args: []const u8, ctx: *command.Context) anyerror!void {
const extra: ?[]const u8 = if (args.len > 0) args else null;
- const res = ctx.agent.compactAndPersist(ctx.compaction_prompt, extra) catch |err| {
+ const res = ctx.agent.compact(ctx.compaction_prompt, extra) catch |err| {
try ctx.stdout.print("\n[compaction failed: {s}]\n> ", .{@errorName(err)});
try ctx.stdout_file.flush();
return;
diff --git a/src/main.zig b/src/main.zig
index 995735f..61a88bd 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -144,7 +144,7 @@ const CLIRenderer = struct {
/// it terminates (`turn_complete` → `next()` returns null). A failure
/// surfaces as the error from `next()`; the caller renders it. The stream is
/// always `deinit`ed (persisting the turn tail) on every exit path.
-fn driveTurn(agent: *panto.agent.Agent, message: panto.agent.Agent.UserMessage, renderer: *CLIRenderer) !void {
+fn driveTurn(agent: panto.Agent, message: panto.UserMessage, renderer: *CLIRenderer) !void {
var stream = try agent.run(message);
defer stream.deinit();
while (try stream.next()) |ev| try renderer.render(ev);
@@ -358,7 +358,7 @@ pub fn main(init: std.process.Init) !void {
// On resume, reconstruct the conversation from the store. (The
// dangling-prompt recovery feature was dropped in R2.) On a fresh
// session, the agent starts with an empty conversation.
- var adopted_conversation: ?panto.conversation.Conversation = null;
+ var adopted_conversation: ?panto.ConversationData = null;
if (is_resume) {
adopted_conversation = try session.load();
const sid = session.info.id;
@@ -376,11 +376,11 @@ pub fn main(init: std.process.Init) !void {
// built but before the first turn, so in-place registration is visible.
// System-prompt seeding/reconciliation below runs *through* the agent
// so those entries persist.
- const active_config: panto.Config = .{
+ var active_config: panto.Config = .{
.provider = provider_config,
.compaction = compaction_cfg,
};
- var agent = panto.agent.Agent.init(
+ const agent = try panto.Agent.init(
alloc,
io,
&active_config,
@@ -424,7 +424,7 @@ pub fn main(init: std.process.Init) !void {
io,
init.environ_map,
luarocks_rt.layout.agent_dir,
- &agent,
+ agent,
);
} else {
// Fresh session — source and install the system prompt.
@@ -433,7 +433,7 @@ pub fn main(init: std.process.Init) !void {
io,
init.environ_map,
luarocks_rt.layout.agent_dir,
- &agent,
+ agent,
);
}
@@ -476,7 +476,9 @@ pub fn main(init: std.process.Init) !void {
init.environ_map,
luarocks_rt.layout.agent_dir,
);
- agent.compaction_system_prompt = compaction_prompt;
+ // The compaction prompt is resolved after extension load; set it on the
+ // config the agent re-reads each turn (visible before the first turn).
+ active_config.compaction.compaction_prompt = compaction_prompt;
const banner_base: []const u8 = switch (provider_config) {
inline else => |c| c.base_url,
@@ -521,7 +523,7 @@ pub fn main(init: std.process.Init) !void {
var cmd_ctx: command.Context = .{
.allocator = alloc,
- .agent = &agent,
+ .agent = agent,
.stdout = stdout,
.stdout_file = &stdout_file,
.compaction_prompt = compaction_prompt,
@@ -570,7 +572,7 @@ pub fn main(init: std.process.Init) !void {
// auto-compaction) through its session store — the CLI no longer
// touches persistence.
cli_renderer.beginTurn();
- driveTurn(&agent, .{ .text = line }, &cli_renderer) catch |err| {
+ driveTurn(agent, .{ .text = line }, &cli_renderer) catch |err| {
cli_renderer.renderError(err);
try stdout.print("\n[error: {s}]\n", .{@errorName(err)});
};
diff --git a/src/system_prompt.zig b/src/system_prompt.zig
index 500b135..39c3ea8 100644
--- a/src/system_prompt.zig
+++ b/src/system_prompt.zig
@@ -137,7 +137,7 @@ pub fn seedFresh(
io: Io,
environ_map: *const std.process.Environ.Map,
base_dir: []const u8,
- agent: *panto.agent.Agent,
+ agent: panto.Agent,
) !void {
const user_dir = try userLayerDir(arena, environ_map);
const project_dir = try projectLayerDir(arena, io);
@@ -151,13 +151,13 @@ pub fn seedFreshLayers(
base_dir: ?[]const u8,
user_dir: ?[]const u8,
project_dir: ?[]const u8,
- agent: *panto.agent.Agent,
+ agent: panto.Agent,
) !void {
const resolved = try resolveLayers(arena, io, base_dir, user_dir, project_dir);
const blocks = try resolved.blocks(arena);
for (blocks) |text| {
// The agent adds to its conversation and persists the entry.
- try agent.addSystemMessage(text, .append);
+ try agent.addSystemMessage(text);
}
}
@@ -174,7 +174,7 @@ pub fn reconcileResume(
io: Io,
environ_map: *const std.process.Environ.Map,
base_dir: []const u8,
- agent: *panto.agent.Agent,
+ agent: panto.Agent,
) !void {
const user_dir = try userLayerDir(arena, environ_map);
const project_dir = try projectLayerDir(arena, io);
@@ -188,21 +188,21 @@ pub fn reconcileResumeLayers(
base_dir: ?[]const u8,
user_dir: ?[]const u8,
project_dir: ?[]const u8,
- agent: *panto.agent.Agent,
+ agent: panto.Agent,
) !void {
const resolved = try resolveLayers(arena, io, base_dir, user_dir, project_dir);
const config_blocks = try resolved.blocks(arena);
- const window = try effectiveConfigWindow(arena, agent.conversation.messages.items);
+ const window = try effectiveConfigWindow(arena, agent.conversation().messages());
if (blocksEqual(config_blocks, window)) return;
- // Re-seed: one `replace` for the seed, then one `append` per append.
- // The agent adds to its conversation and persists each entry (with the
- // correct system mode).
- try agent.addSystemMessage(config_blocks[0], .replace);
+ // Re-seed: one `setSystemPrompt` (replace) for the seed, then one
+ // `addSystemMessage` (append) per append. The agent adds to its
+ // conversation and persists each entry (with the correct system mode).
+ try agent.setSystemPrompt(config_blocks[0]);
for (config_blocks[1..]) |text| {
- try agent.addSystemMessage(text, .append);
+ try agent.addSystemMessage(text);
}
}
@@ -339,7 +339,7 @@ test "blocksEqual - length and content" {
test "effectiveConfigWindow - no replace anchors to leading system blocks" {
const alloc = testing.allocator;
- var conv = panto.conversation.Conversation.init(alloc);
+ var conv = panto.ConversationData.init(alloc);
defer conv.deinit();
try conv.addSystemMessage("seed");
@@ -357,7 +357,7 @@ test "effectiveConfigWindow - no replace anchors to leading system blocks" {
test "effectiveConfigWindow - anchors to last replace block" {
const alloc = testing.allocator;
- var conv = panto.conversation.Conversation.init(alloc);
+ var conv = panto.ConversationData.init(alloc);
defer conv.deinit();
try conv.addSystemMessage("old seed");
@@ -473,38 +473,45 @@ fn openTmpStore(arena: Allocator, root: []const u8) !panto.FileSystemJSONLStore
/// harness only holds the config to keep the agent's borrowed pointer valid.
const SPAgentHarness = struct {
config: panto.Config,
- agent: panto.agent.Agent,
+ agent: panto.Agent,
+ /// A second handle onto the same session (a copyable value proxying to
+ /// the same store + id) so the test can `forceFlush` without reaching
+ /// agent internals. `session_id` mirrors the agent's session id.
+ session: panto.Session,
+ session_id: []const u8,
fn init(
self: *SPAgentHarness,
session: panto.Session,
- adopted: ?panto.conversation.Conversation,
- ) void {
+ adopted: ?panto.ConversationData,
+ ) !void {
self.config = .{
.provider = .{ .openai_chat = .{ .api_key = "k", .base_url = "u", .model = "m" } },
};
- self.agent = panto.agent.Agent.init(testing.allocator, testing.io, &self.config, session, adopted);
+ self.session = session;
+ self.session_id = session.info.id;
+ self.agent = try panto.Agent.init(testing.allocator, testing.io, &self.config, session, adopted);
}
fn deinit(self: *SPAgentHarness) void {
self.agent.deinit();
}
-};
-/// Force the session to flush to disk by appending an assistant message
-/// through the agent's session. (System/user messages buffer in memory
-/// until the first assistant message; a realistic resume only sees what a
-/// completed turn persisted.)
-fn forceFlush(agent: *panto.agent.Agent) !void {
- var conv = panto.conversation.Conversation.init(testing.allocator);
- defer conv.deinit();
- try conv.addAssistantMessage(&.{});
- const id: panto.WireIdentity = .{ .api_style = .openai_chat, .base_url = "u", .model = "m" };
- var batch = [_]panto.PersistentMessage{
- .{ .message = conv.messages.items[0], .identity = id },
- };
- try agent.session.append(&batch);
-}
+ /// Force the session to flush to disk by appending an assistant message
+ /// through the (shared) session handle. (System/user messages buffer in
+ /// memory until the first assistant message; a realistic resume only
+ /// sees what a completed turn persisted.)
+ fn forceFlush(self: *SPAgentHarness) !void {
+ var conv = panto.ConversationData.init(testing.allocator);
+ defer conv.deinit();
+ try conv.addAssistantMessage(&.{});
+ const id: panto.WireIdentity = .{ .api_style = .openai_chat, .base_url = "u", .model = "m" };
+ var batch = [_]panto.PersistentMessage{
+ .{ .message = conv.messages.items[0], .identity = id },
+ };
+ try self.session.append(&batch);
+ }
+};
test "seedFresh then no-config-change resume is a no-op" {
const alloc = testing.allocator;
@@ -526,12 +533,12 @@ test "seedFresh then no-config-change resume is a no-op" {
var session_id_len: usize = 0;
{
var h: SPAgentHarness = undefined;
- h.init(store.create(), null);
+ try h.init(store.create(), null);
defer h.deinit();
- try seedFreshLayers(arena, testing.io, null, null, project_dir, &h.agent);
- try forceFlush(&h.agent); // persist seed (+ assistant) to disk
- @memcpy(session_id_buf[0..h.agent.session.info.id.len], h.agent.session.info.id);
- session_id_len = h.agent.session.info.id.len;
+ try seedFreshLayers(arena, testing.io, null, null, project_dir, h.agent);
+ try h.forceFlush(); // persist seed (+ assistant) to disk
+ @memcpy(session_id_buf[0..h.session_id.len], h.session_id);
+ session_id_len = h.session_id.len;
}
const session_id = session_id_buf[0..session_id_len];
@@ -547,10 +554,10 @@ test "seedFresh then no-config-change resume is a no-op" {
var sess2 = (try store.resolve(session_id)).?;
const conv2 = try sess2.load();
var h2: SPAgentHarness = undefined;
- h2.init(sess2, conv2);
+ try h2.init(sess2, conv2);
defer h2.deinit();
- try reconcileResumeLayers(arena, testing.io, null, null, project_dir, &h2.agent);
- const eff_after = try panto.effectiveSystemBlocks(arena, h2.agent.conversation.messages.items);
+ try reconcileResumeLayers(arena, testing.io, null, null, project_dir, h2.agent);
+ const eff_after = try panto.effectiveSystemBlocks(arena, h2.agent.conversation().messages());
try testing.expectEqual(seeded_count, eff_after.items.len);
}
@@ -572,12 +579,12 @@ test "resume after config change appends replace + append sequence" {
var session_id_len: usize = 0;
{
var h: SPAgentHarness = undefined;
- h.init(store.create(), null);
+ try h.init(store.create(), null);
defer h.deinit();
- try seedFreshLayers(arena, testing.io, null, null, project_dir, &h.agent);
- try forceFlush(&h.agent); // persist old seed (+ assistant) to disk
- @memcpy(session_id_buf[0..h.agent.session.info.id.len], h.agent.session.info.id);
- session_id_len = h.agent.session.info.id.len;
+ try seedFreshLayers(arena, testing.io, null, null, project_dir, h.agent);
+ try h.forceFlush(); // persist old seed (+ assistant) to disk
+ @memcpy(session_id_buf[0..h.session_id.len], h.session_id);
+ session_id_len = h.session_id.len;
}
const session_id = session_id_buf[0..session_id_len];
@@ -588,20 +595,20 @@ test "resume after config change appends replace + append sequence" {
var sess2 = (try store.resolve(session_id)).?;
const conv2 = try sess2.load();
var h2: SPAgentHarness = undefined;
- h2.init(sess2, conv2);
+ try h2.init(sess2, conv2);
defer h2.deinit();
- try reconcileResumeLayers(arena, testing.io, null, null, project_dir, &h2.agent);
+ try reconcileResumeLayers(arena, testing.io, null, null, project_dir, h2.agent);
// The effective prompt now reflects only the new config blocks.
- const eff = try panto.effectiveSystemBlocks(arena, h2.agent.conversation.messages.items);
+ const eff = try panto.effectiveSystemBlocks(arena, h2.agent.conversation().messages());
try testing.expectEqual(@as(usize, 2), eff.items.len);
try testing.expectEqualStrings("new seed", eff.items[0]);
try testing.expectEqualStrings("new append", eff.items[1]);
// A second no-op resume must not change the effective prompt (anchors
// to the new `replace` window, not the stale original seed).
- try reconcileResumeLayers(arena, testing.io, null, null, project_dir, &h2.agent);
- const eff2 = try panto.effectiveSystemBlocks(arena, h2.agent.conversation.messages.items);
+ try reconcileResumeLayers(arena, testing.io, null, null, project_dir, h2.agent);
+ const eff2 = try panto.effectiveSystemBlocks(arena, h2.agent.conversation().messages());
try testing.expectEqual(@as(usize, 2), eff2.items.len);
}