Per-session notepad: agent-curated labelled entries via the notepad tool
(set/remove/clear), injected as a pinned <NOTEPAD> section above DIALOG,
char-budgeted with a consolidate ceiling, exposed in a read-only GUI panel.
Sliding DIALOG window (src/window.rs): retains recent messages by count in
two tiers (MAIN_AREA=40 + BUFFER=10), append-only while the buffer fills so
the prompt prefix stays byte-stable for KV reuse, then stepped hysteresis
eviction snapping to user-turn boundaries (never splits a tool exchange).
window_start persisted per session; full history never deleted.
Engine prefix-cache reuse: leave DIALOG open (a moving </DIALOG> closer broke
the engine prompt_ckpt_prefix every user turn, forcing full re-prefill);
surface usage.cached_tokens as a GUI badge; fix the PerfMeter prefill gauge to
gauge over non-cached tokens; add a static sliding-window marker after the
opener so the model knows older turns are omitted.
Stats clarity: distinguish the current inference context (last_prompt_tokens,
new "Contesto attuale" card) from the cumulative lifetime token total
(relabeled "Token cumulativi ... su tutti i turni"); /api/stats carries both.
Windowed browser transcript: GET /api/sessions/{id}/messages returns only the
retained window (SQL-filtered by window_start, never loads the full history)
plus the total count, wrapped as {messages, window_start, total}; the GUI shows
a banner noting older turns are preserved server-side. Full-chat export deferred.
Settings: the session scope re-reads the persisted instance fresh from the store
(like the general scope already did), fixing a stale-cache display that looked
like the session System Prompt was not being kept.
Docs (README) refreshed; 158 tests + 3 ignored green, clippy and fmt clean.
155 lines
5.7 KiB
Rust
155 lines
5.7 KiB
Rust
//! Tests for the Inkling chat-template layer: the custom tojson/raise_exception
|
|
//! and generation-strip (deterministic, inline template), prepare_messages /
|
|
//! finish_prompt (pure), and an #[ignore] render of the real chat_template.jinja.
|
|
|
|
use serde_json::json;
|
|
use strata_ctl::inkling::dialect::tag;
|
|
use strata_ctl::inkling::template::{finish_prompt, prepare_messages, ChatTemplate};
|
|
|
|
#[test]
|
|
fn tojson_compact_and_sorted() {
|
|
let t =
|
|
ChatTemplate::new("{{ {'b': 1, 'a': 2} | tojson(sort_keys=true, separators=(',', ':')) }}")
|
|
.unwrap();
|
|
assert_eq!(
|
|
t.render(&json!([]), None, None).unwrap(),
|
|
"{\"a\":2,\"b\":1}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn generation_blocks_are_transparent() {
|
|
let t = ChatTemplate::new("A{% generation %}B{% endgeneration %}C").unwrap();
|
|
assert_eq!(t.render(&json!([]), None, None).unwrap(), "ABC");
|
|
}
|
|
|
|
#[test]
|
|
fn namespace_and_dictsort_work() {
|
|
let src = "{%- set s = namespace(x=[]) -%}\
|
|
{%- set s.x = s.x + [1, 2] -%}\
|
|
{{ s.x | tojson(separators=(',', ':')) }}|\
|
|
{%- for k, v in {'b': 1, 'a': 2} | dictsort(case_sensitive=true) -%}\
|
|
{{ k }}={{ v }};{%- endfor -%}";
|
|
let t = ChatTemplate::new(src).unwrap();
|
|
assert_eq!(t.render(&json!([]), None, None).unwrap(), "[1,2]|a=2;b=1;");
|
|
}
|
|
|
|
#[test]
|
|
fn raise_exception_fails_render() {
|
|
let t = ChatTemplate::new("{{ raise_exception('boom') }}").unwrap();
|
|
let err = t.render(&json!([]), None, None).unwrap_err();
|
|
assert!(err.to_string().contains("boom"), "{err}");
|
|
}
|
|
|
|
#[test]
|
|
fn prepare_messages_parses_string_arguments() {
|
|
let mut msgs = json!([
|
|
{"role": "assistant", "tool_calls": [
|
|
{"id": "c1", "type": "function",
|
|
"function": {"name": "f", "arguments": "{\"a\": 1}"}}
|
|
]},
|
|
{"role": "assistant", "tool_calls": [
|
|
{"id": "c2", "function": {"name": "g", "arguments": null}}
|
|
]}
|
|
]);
|
|
prepare_messages(&mut msgs);
|
|
assert_eq!(
|
|
msgs[0]["tool_calls"][0]["function"]["arguments"],
|
|
json!({"a": 1})
|
|
);
|
|
assert_eq!(msgs[1]["tool_calls"][0]["function"]["arguments"], json!({}));
|
|
}
|
|
|
|
#[test]
|
|
fn finish_prompt_only_for_effort_zero() {
|
|
// positive effort: untouched (model opens its own thinking)
|
|
let p = "<|message_system|><|content_text|>Thinking effort level: 0.2<|end_message|>";
|
|
assert_eq!(finish_prompt(p, false, false), p);
|
|
|
|
// effort zero, no opener yet: append the text opener (no tools)
|
|
let z = "<|message_system|><|content_text|>Thinking effort level: 0<|end_message|>";
|
|
let expect = format!("{z}{}{}", tag::MESSAGE_MODEL, tag::CONTENT_TEXT);
|
|
assert_eq!(finish_prompt(z, false, false), expect);
|
|
|
|
// with tools: the empty-thinking opener
|
|
let expect_tools = format!(
|
|
"{z}{}{}{}",
|
|
tag::MESSAGE_MODEL,
|
|
tag::CONTENT_THINKING,
|
|
tag::END_MESSAGE
|
|
);
|
|
assert_eq!(finish_prompt(z, true, false), expect_tools);
|
|
|
|
// idempotent: already ends with the text opener
|
|
assert_eq!(finish_prompt(&expect, false, false), expect);
|
|
}
|
|
|
|
/// Render the REAL template (host file). Run with:
|
|
/// STRATA_TOK_DIR=~/.config/strata-inkling/inkling-tok cargo test -- --ignored
|
|
#[test]
|
|
#[ignore = "needs the real Inkling chat_template.jinja"]
|
|
fn real_template_renders_a_turn() {
|
|
let dir = std::env::var("STRATA_TOK_DIR")
|
|
.unwrap_or_else(|_| "/home/federico/.config/strata-inkling/inkling-tok".to_string());
|
|
let src = std::fs::read_to_string(format!("{dir}/chat_template.jinja")).unwrap();
|
|
let t = ChatTemplate::new(&src).unwrap();
|
|
|
|
let msgs = json!([{"role": "user", "content": "ciao"}]);
|
|
let out = t.render(&msgs, None, Some("low")).unwrap();
|
|
assert!(out.contains("Thinking effort level: 0.2"), "{out}");
|
|
assert!(
|
|
out.contains("<|message_user|><|content_text|>ciao<|end_message|>"),
|
|
"{out}"
|
|
);
|
|
|
|
// effort none renders level 0
|
|
let out0 = t.render(&msgs, None, Some("none")).unwrap();
|
|
assert!(
|
|
out0.contains("Thinking effort level: 0<|end_message|>"),
|
|
"{out0}"
|
|
);
|
|
}
|
|
|
|
/// Ground truth: the composed context, rendered by the REAL template, must keep
|
|
/// each section's closing tag followed by a blank line (what the model reads).
|
|
#[test]
|
|
#[ignore = "needs the real Inkling chat_template.jinja"]
|
|
fn real_prompt_closes_sections_with_blank_line() {
|
|
use strata_ctl::context;
|
|
use strata_ctl::store::{ContextSection, Session, SessionSettings};
|
|
let dir = std::env::var("STRATA_TOK_DIR")
|
|
.unwrap_or_else(|_| "/home/federico/.config/strata-inkling/inkling-tok".to_string());
|
|
let src = std::fs::read_to_string(format!("{dir}/chat_template.jinja")).unwrap();
|
|
let t = ChatTemplate::new(&src).unwrap();
|
|
let sess = Session {
|
|
id: "s".into(),
|
|
title: "Nebula".into(),
|
|
model: "m".into(),
|
|
settings: SessionSettings::default(),
|
|
name_locked: true,
|
|
folder: None,
|
|
created_at: 0,
|
|
updated_at: 0,
|
|
};
|
|
let sections = vec![
|
|
ContextSection {
|
|
name: "SYSTEM_PROMPT".into(),
|
|
content: "{identity}{speaker_note}\n{session_prompt}".into(),
|
|
position: 0,
|
|
},
|
|
ContextSection {
|
|
name: "TOOL_USE".into(),
|
|
content: "manuale".into(),
|
|
position: 1,
|
|
},
|
|
];
|
|
let msgs = context::compose(&sess, &[], "ciao", &[], "Federico", §ions, &[]);
|
|
let val = serde_json::to_value(&msgs).unwrap();
|
|
let out = t.render(&val, None, Some("low")).unwrap();
|
|
assert!(out.contains("</SYSTEM_PROMPT>\n\n"), "{out}");
|
|
assert!(out.contains("</TOOL_USE>\n\n"), "{out}");
|
|
// DIALOG is an open tail: opener present, no moving closer (KV-cache prefix).
|
|
assert!(out.contains("<DIALOG>"), "{out}");
|
|
assert!(!out.contains("</DIALOG>"), "DIALOG must stay open: {out}");
|
|
}
|