forked from kherud/java-llama.cpp
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathChatTranscriptTest.java
More file actions
299 lines (251 loc) · 13.4 KB
/
Copy pathChatTranscriptTest.java
File metadata and controls
299 lines (251 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// SPDX-FileCopyrightText: 2026 Bernard Ladenthin <bernard.ladenthin@gmail.com>
//
// SPDX-License-Identifier: MIT
package net.ladenthin.llama.value;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.List;
import net.ladenthin.llama.Session;
import net.ladenthin.llama.exception.LlamaException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
/**
* Running documentation of the two-phase commit invariant that
* {@link Session#send(String)} and {@link Session#stream(String)} rely on.
*
* <p>The transcript management was extracted from {@code Session} into
* {@link ChatTranscript} precisely so this invariant — "transcript is mutated
* only on the model-call success path; on failure the pending user turn
* evaporates" — could be unit-tested without a GGUF model or the native
* {@code libjllama} library.
*
* <p>The contract is enforced <b>by the API shape itself</b>, not by tests:
*
* <ul>
* <li>The only "commit a full round" method is {@link
* ChatTranscript#appendRound(String, String)}, which appends both turns
* atomically. There is no way to commit just the user turn through this
* API.</li>
* <li>The wire-format the model receives is built by
* {@link ChatTranscript#messagesWithPendingUserTurn(String)}, which
* returns a fresh list and does NOT mutate the transcript. So the
* pending user turn reaches the model without being committed.</li>
* <li>Therefore: if the model call throws after the wire-format is built,
* {@code appendRound} is never reached, and the transcript stays
* exactly as it was before the call.</li>
* </ul>
*
* <p>The tests below pin both the mechanical API behaviour and the higher-level
* two-phase commit pattern as it is composed by {@link Session}.
*/
class ChatTranscriptTest {
/** Helper: simulate {@code Session.send} composing a single round through the API. */
private static void simulateSend(ChatTranscript t, String userMessage, String assistantReply) {
// Phase 1: build wire-format (model would see this).
List<Pair<String, String>> wire = t.messagesWithPendingUserTurn(userMessage);
// The wire format must contain the pending turn the model is about to answer.
assertThat("wire-format must carry the pending user turn", wire, hasItem(new Pair<>("user", userMessage)));
// Phase 2: model returned successfully — commit both turns atomically.
t.appendRound(userMessage, assistantReply);
}
/**
* Helper: simulate {@code Session.send} where the model call throws after the
* wire-format is built. The {@code appendRound} line is never reached.
*/
private static void simulateSendThatModelRejects(
ChatTranscript t, String pendingUserMessage, RuntimeException simulatedModelFailure) {
// Phase 1: build wire-format (model would see this).
@SuppressWarnings("unused")
List<Pair<String, String>> wire = t.messagesWithPendingUserTurn(pendingUserMessage);
// Phase 2: model throws — the caller (Session.send) lets the exception
// propagate; appendRound is NEVER called.
throw simulatedModelFailure;
}
@Nested
@DisplayName("mechanical API behaviour")
class Api {
@Test
@DisplayName("appendRound commits both turns atomically")
void appendRoundCommitsBothTurnsAtomically() {
ChatTranscript t = new ChatTranscript(null);
t.appendRound("hi", "hello back");
assertThat(t.size(), is(2));
List<ChatMessage> snapshot = t.snapshot();
assertThat(snapshot, hasSize(2));
assertThat(snapshot.get(0).getRole(), is("user"));
assertThat(snapshot.get(0).getContent(), is("hi"));
assertThat(snapshot.get(1).getRole(), is("assistant"));
assertThat(snapshot.get(1).getContent(), is("hello back"));
}
@Test
@DisplayName("appendUserTurn + appendAssistantTurn together produce the same shape as appendRound")
void appendUserAndAssistantSeparatelyMatchAppendRound() {
ChatTranscript a = new ChatTranscript(null);
ChatTranscript b = new ChatTranscript(null);
a.appendRound("hi", "hello back");
b.appendUserTurn("hi");
b.appendAssistantTurn("hello back");
assertThat("atomic-round and split-commit must converge", b.snapshot(), is(a.snapshot()));
}
@Test
@DisplayName("removePendingUserTurn drops a trailing user turn and reports true")
void removePendingUserTurnDropsTrailingUserTurn() {
ChatTranscript t = new ChatTranscript(null);
t.appendUserTurn("pending question");
assertThat("precondition: one dangling user turn", t.size(), is(1));
boolean removed = t.removePendingUserTurn();
assertThat("a dangling user turn must be reported as removed", removed, is(true));
assertThat("transcript is rolled back to its pre-stream (empty) shape", t.size(), is(0));
}
@Test
@DisplayName("removePendingUserTurn is a no-op when the last turn is an assistant turn")
void removePendingUserTurnKeepsCommittedRound() {
ChatTranscript t = new ChatTranscript(null);
t.appendRound("q", "a"); // last turn is "assistant", not a dangling user turn
boolean removed = t.removePendingUserTurn();
assertThat("a committed round has no dangling user turn to remove", removed, is(false));
assertThat("the committed round must be left intact", t.size(), is(2));
assertThat(t.snapshot().get(0).getRole(), is("user"));
assertThat(t.snapshot().get(1).getRole(), is("assistant"));
}
@Test
@DisplayName("removePendingUserTurn is a safe no-op on an empty transcript")
void removePendingUserTurnNoOpOnEmptyTranscript() {
ChatTranscript t = new ChatTranscript("system");
boolean removed = t.removePendingUserTurn();
assertThat("an empty transcript has nothing to remove", removed, is(false));
assertThat(t.size(), is(0));
}
@Test
@DisplayName("messagesWithPendingUserTurn does NOT mutate the transcript")
void messagesWithPendingUserTurnDoesNotMutate() {
ChatTranscript t = new ChatTranscript("system");
t.appendRound("first", "reply-1");
int sizeBefore = t.size();
List<ChatMessage> snapshotBefore = t.snapshot();
List<Pair<String, String>> wire = t.messagesWithPendingUserTurn("pending");
// Build a wire-format containing committed turns + pending user.
assertThat("1 user + 1 assistant + 1 pending user", wire, hasSize(3));
assertThat(wire.get(2).getKey(), is("user"));
assertThat(wire.get(2).getValue(), is("pending"));
// The transcript itself MUST be unchanged.
assertThat("transcript size unchanged", t.size(), is(sizeBefore));
assertThat("transcript snapshot unchanged", t.snapshot(), is(snapshotBefore));
}
@Test
@DisplayName("messagesWithPendingUserTurn returns a fresh list each call")
void messagesWithPendingUserTurnReturnsFreshList() {
ChatTranscript t = new ChatTranscript(null);
List<Pair<String, String>> first = t.messagesWithPendingUserTurn("hi");
List<Pair<String, String>> second = t.messagesWithPendingUserTurn("hi");
assertThat(
"each wire-format build returns a fresh list — callers may mutate without affecting peers",
first,
is(not(sameInstance(second))));
}
@Test
@DisplayName("snapshot includes system message when configured")
void snapshotIncludesSystemMessage() {
ChatTranscript t = new ChatTranscript("you are an assistant");
t.appendRound("hi", "hello");
List<ChatMessage> snap = t.snapshot();
assertThat(snap, hasSize(3));
assertThat(snap.get(0).getRole(), is("system"));
assertThat(snap.get(0).getContent(), is("you are an assistant"));
}
@Test
@DisplayName("snapshot omits system message when null or empty")
void snapshotOmitsSystemMessageWhenAbsent() {
assertThat(new ChatTranscript(null).snapshot(), is(empty()));
assertThat(new ChatTranscript("").snapshot(), is(empty()));
}
@Test
@DisplayName("snapshot is unmodifiable")
void snapshotIsUnmodifiable() {
ChatTranscript t = new ChatTranscript(null);
t.appendRound("hi", "hello");
List<ChatMessage> snap = t.snapshot();
assertThrows(UnsupportedOperationException.class, () -> snap.clear());
}
@Test
@DisplayName("getSystemMessage returns null when absent")
void getSystemMessageNullWhenAbsent() {
assertThat(new ChatTranscript(null).getSystemMessage(), is(nullValue()));
}
}
@Nested
@DisplayName("two-phase commit pattern — running documentation")
class TwoPhaseCommit {
@Test
@DisplayName("simulated model failure leaves a FRESH transcript untouched")
void freshTranscriptUntouchedWhenModelThrows() {
ChatTranscript t = new ChatTranscript("system");
assertThat("precondition: fresh transcript has no turns", t.size(), is(0));
int snapshotSizeBefore = t.snapshot().size();
// Caller simulates Session.send where the model rejects the request.
assertThrows(
LlamaException.class,
() -> simulateSendThatModelRejects(
t, "first attempt", new LlamaException("simulated model failure")));
// Two-phase commit: the pending user turn never landed in the transcript.
// (The system message snapshot entry was there before and is still there.)
assertThat("transcript MUST NOT contain the pending user turn after model failure", t.size(), is(0));
assertThat(
"snapshot size unchanged by the failed call", t.snapshot().size(), is(snapshotSizeBefore));
}
@Test
@DisplayName("simulated model failure leaves an EXISTING transcript byte-for-byte unchanged")
void existingTranscriptUntouchedWhenModelThrows() {
ChatTranscript t = new ChatTranscript("system");
simulateSend(t, "hi", "hello back");
simulateSend(t, "how are you", "i'm fine");
List<ChatMessage> before = t.snapshot();
assertThat("precondition: 1 system + 2 user + 2 assistant", before, hasSize(5));
// Now the model rejects a third call.
assertThrows(
LlamaException.class,
() -> simulateSendThatModelRejects(
t, "third attempt", new LlamaException("simulated model failure")));
// Two-phase commit: existing transcript is byte-for-byte unchanged.
List<ChatMessage> after = t.snapshot();
assertThat("failed call must leave the transcript byte-for-byte unchanged", after, is(before));
}
@Test
@DisplayName("simulated model success commits user + assistant atomically — never just one half")
void successCommitsBothTurnsAtomically() {
ChatTranscript t = new ChatTranscript(null);
simulateSend(t, "hi", "hello");
assertThat("both turns committed", t.size(), is(2));
// The shape is invariant: there is no API to commit only one half via appendRound.
// Spot-check that the turn pair is well-formed.
List<ChatMessage> snap = t.snapshot();
assertThat(snap.get(0).getRole(), is("user"));
assertThat(snap.get(0).getContent(), is("hi"));
assertThat(snap.get(1).getRole(), is("assistant"));
assertThat(snap.get(1).getContent(), is("hello"));
}
@Test
@DisplayName("stream() shape — user turn only, assistant follows via commitStreamedReply")
void streamShape() {
ChatTranscript t = new ChatTranscript(null);
// Phase 1: build wire format (would be passed to model.generateChat).
List<Pair<String, String>> wire = t.messagesWithPendingUserTurn("tell me a joke");
assertThat("wire contains the pending user turn", wire, hasSize(1));
// Phase 2: model returned an iterable successfully — commit only the user turn.
t.appendUserTurn("tell me a joke");
assertThat("user turn committed; assistant follows later", t.size(), is(1));
// Later: caller invoked commitStreamedReply with the accumulated text.
t.appendAssistantTurn("knock knock");
assertThat("round closes with the assistant turn", t.size(), is(2));
assertThat(t.snapshot().get(1).getRole(), is("assistant"));
}
}
}