-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.zig
More file actions
575 lines (483 loc) · 20.7 KB
/
Copy pathstate.zig
File metadata and controls
575 lines (483 loc) · 20.7 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// Application state management (Phase 1: Todo tracking for master loop)
// Extended with Time-Keeper memory tools: scratchpad, key-value store, vector database
const std = @import("std");
const mem = std.mem;
/// Compute cosine similarity between two embedding vectors
/// Returns value between -1.0 and 1.0 (typically 0.0 to 1.0 for normalized embeddings)
fn cosineSimilarity(a: []const f32, b: []const f32) f32 {
if (a.len != b.len) return 0.0;
var dot_product: f32 = 0.0;
var norm_a: f32 = 0.0;
var norm_b: f32 = 0.0;
for (a, b) |av, bv| {
dot_product += av * bv;
norm_a += av * av;
norm_b += bv * bv;
}
const denominator = @sqrt(norm_a) * @sqrt(norm_b);
if (denominator == 0.0) return 0.0;
return dot_product / denominator;
}
/// Todo status enum for tracking progress
pub const TodoStatus = enum { pending, in_progress, completed };
/// Entry in vector memory database (simple dictionary approach for Time-Keeper)
pub const VectorMemoryEntry = struct {
id: []const u8, // owned string ID like "vec_1", "vec_2"
text: []const u8, // owned text content
embedding: []f32, // owned embedding vector
};
/// Result from vector similarity search
pub const VectorSearchResult = struct {
id: []const u8, // borrowed from VectorMemoryEntry
text: []const u8, // borrowed from VectorMemoryEntry
similarity: f32, // cosine similarity score (0.0 to 1.0)
};
/// Individual todo with ID, content, and status
pub const Todo = struct {
id: []const u8, // String ID like "todo_1", "todo_2", etc.
content: []const u8,
status: TodoStatus,
};
/// Pending file to be indexed by Graph RAG
pub const PendingIndexFile = struct {
path: []const u8, // owned
content: []const u8, // owned
};
/// Timer notification that has fired and is waiting to be injected as system message
pub const TimerNotification = struct {
label: []const u8, // owned string, e.g., "check_breakfast_order"
duration_ms: u64, // original duration for logging
fired_at: i64, // timestamp when timer expired
};
/// Benchmark metrics for tracking tool usage and loop completions
pub const BenchmarkMetrics = struct {
set_timer_calls: usize = 0,
kv_set_calls: usize = 0,
get_current_time_calls: usize = 0,
complete_loops: usize = 0,
benchmark_start_time: ?i64 = null,
pub fn reset(self: *BenchmarkMetrics) void {
self.set_timer_calls = 0;
self.kv_set_calls = 0;
self.get_current_time_calls = 0;
self.complete_loops = 0;
self.benchmark_start_time = null;
}
pub fn incrementToolCall(self: *BenchmarkMetrics, tool_name: []const u8) void {
if (std.mem.eql(u8, tool_name, "set_timer")) {
self.set_timer_calls += 1;
} else if (std.mem.eql(u8, tool_name, "kv_set")) {
self.kv_set_calls += 1;
} else if (std.mem.eql(u8, tool_name, "get_current_time")) {
self.get_current_time_calls += 1;
}
}
pub fn incrementLoops(self: *BenchmarkMetrics) void {
self.complete_loops += 1;
}
/// Export benchmark metrics to JSON file
/// Returns the file path on success (caller owns the string)
pub fn exportToJson(self: *const BenchmarkMetrics, allocator: mem.Allocator, model: []const u8) ![]const u8 {
const now = std.time.milliTimestamp();
const end_time = now;
const start_time = self.benchmark_start_time orelse now;
const duration_secs = @as(f64, @floatFromInt(end_time - start_time)) / 1000.0;
// Create timestamp for filename
const epoch_seconds = std.time.epoch.EpochSeconds{ .secs = @intCast(@divTrunc(now, 1000)) };
const day_seconds = epoch_seconds.getDaySeconds();
const epoch_day = epoch_seconds.getEpochDay();
const year_day = epoch_day.calculateYearDay();
const month_day = year_day.calculateMonthDay();
// Format: YYYYMMDD_HHMMSS
const timestamp_str = try std.fmt.allocPrint(
allocator,
"{d:0>4}{d:0>2}{d:0>2}_{d:0>2}{d:0>2}{d:0>2}",
.{
year_day.year,
month_day.month.numeric(),
month_day.day_index + 1,
day_seconds.getHoursIntoDay(),
day_seconds.getMinutesIntoHour(),
day_seconds.getSecondsIntoMinute(),
},
);
defer allocator.free(timestamp_str);
// Build output path
const home = std.posix.getenv("HOME") orelse return error.NoHomeDir;
const dir_path = try std.fs.path.join(allocator, &.{ home, ".config", "time-keeper" });
defer allocator.free(dir_path);
// Ensure directory exists
std.fs.cwd().makePath(dir_path) catch {};
const filename = try std.fmt.allocPrint(allocator, "benchmark_results_{s}.json", .{timestamp_str});
defer allocator.free(filename);
const file_path = try std.fs.path.join(allocator, &.{ dir_path, filename });
errdefer allocator.free(file_path);
// Build JSON content
const json_content = try std.fmt.allocPrint(
allocator,
\\{{
\\ "session_id": "{s}",
\\ "model": "{s}",
\\ "benchmark_duration_seconds": {d:.2},
\\ "metrics": {{
\\ "set_timer_calls": {d},
\\ "kv_set_calls": {d},
\\ "get_current_time_calls": {d},
\\ "complete_loops": {d}
\\ }},
\\ "timestamps": {{
\\ "session_start": {d},
\\ "session_end": {d}
\\ }}
\\}}
,
.{
timestamp_str,
model,
duration_secs,
self.set_timer_calls,
self.kv_set_calls,
self.get_current_time_calls,
self.complete_loops,
start_time,
end_time,
},
);
defer allocator.free(json_content);
// Write file
const file = try std.fs.cwd().createFile(file_path, .{ .truncate = true });
defer file.close();
try file.writeAll(json_content);
return file_path;
}
};
/// Session-ephemeral application state
pub const AppState = struct {
allocator: mem.Allocator,
todos: std.ArrayListUnmanaged(Todo),
next_todo_id: usize,
session_start: i64,
read_files: std.StringHashMapUnmanaged(void), // Track files read in this session
indexed_files: std.StringHashMapUnmanaged(void), // Track files indexed in Graph RAG
pending_index_files: std.ArrayListUnmanaged(PendingIndexFile), // Queue for background indexing
// Time-Keeper Memory Tools (session-ephemeral)
scratchpad: std.ArrayListUnmanaged(u8), // Free-form text buffer
kv_store: std.StringHashMapUnmanaged([]const u8), // key -> owned value
vector_memory: std.StringHashMapUnmanaged(VectorMemoryEntry), // id -> entry
next_vector_id: usize, // Auto-incrementing ID for vector entries
// Timer System (for async notifications)
timer_notifications: std.ArrayListUnmanaged(TimerNotification), // Queue of fired timers
timer_mutex: std.Thread.Mutex, // Protects timer_notifications
// Benchmark Metrics (for tracking tool usage and loop completions)
benchmark_metrics: BenchmarkMetrics,
pub fn init(allocator: mem.Allocator) AppState {
return .{
.allocator = allocator,
.todos = .{},
.next_todo_id = 1,
.session_start = std.time.milliTimestamp(),
.read_files = .{},
.indexed_files = .{},
.pending_index_files = .{},
// Time-Keeper Memory Tools
.scratchpad = .{},
.kv_store = .{},
.vector_memory = .{},
.next_vector_id = 1,
// Timer System
.timer_notifications = .{},
.timer_mutex = .{},
// Benchmark Metrics
.benchmark_metrics = .{},
};
}
pub fn addTodo(self: *AppState, content: []const u8) ![]const u8 {
// Generate string ID like "todo_1", "todo_2", etc.
const todo_id = try std.fmt.allocPrint(self.allocator, "todo_{d}", .{self.next_todo_id});
errdefer self.allocator.free(todo_id);
self.next_todo_id += 1;
const owned_content = try self.allocator.dupe(u8, content);
errdefer self.allocator.free(owned_content);
try self.todos.append(self.allocator, .{
.id = todo_id,
.content = owned_content,
.status = .pending,
});
return todo_id;
}
pub fn updateTodo(self: *AppState, todo_id: []const u8, new_status: TodoStatus) !void {
for (self.todos.items) |*todo| {
if (mem.eql(u8, todo.id, todo_id)) {
todo.status = new_status;
return;
}
}
return error.TodoNotFound;
}
pub fn getTodos(self: *AppState) []const Todo {
return self.todos.items;
}
pub fn markFileAsRead(self: *AppState, path: []const u8) !void {
// Check if already tracked to avoid duplicate allocations
if (self.read_files.contains(path)) {
return; // Already marked, nothing to do
}
const owned_path = try self.allocator.dupe(u8, path);
errdefer self.allocator.free(owned_path);
try self.read_files.put(self.allocator, owned_path, {});
}
pub fn wasFileRead(self: *AppState, path: []const u8) bool {
return self.read_files.contains(path);
}
pub fn markFileAsIndexed(self: *AppState, path: []const u8) !void {
// Check if already indexed to avoid duplicate allocations
if (self.indexed_files.contains(path)) {
return; // Already indexed
}
const owned_path = try self.allocator.dupe(u8, path);
errdefer self.allocator.free(owned_path);
try self.indexed_files.put(self.allocator, owned_path, {});
}
pub fn wasFileIndexed(self: *AppState, path: []const u8) bool {
return self.indexed_files.contains(path);
}
/// Queue a file for background Graph RAG indexing
pub fn queueFileForIndexing(self: *AppState, path: []const u8, content: []const u8) !void {
const owned_path = try self.allocator.dupe(u8, path);
errdefer self.allocator.free(owned_path);
const owned_content = try self.allocator.dupe(u8, content);
errdefer self.allocator.free(owned_content);
try self.pending_index_files.append(self.allocator, .{
.path = owned_path,
.content = owned_content,
});
}
/// Check if there are files pending indexing
pub fn hasPendingIndexing(self: *AppState) bool {
return self.pending_index_files.items.len > 0;
}
/// Pop the next pending file from the indexing queue
/// Returns null if queue is empty
/// Caller owns returned memory and must free path and content
pub fn popPendingIndexFile(self: *AppState) ?PendingIndexFile {
if (self.pending_index_files.items.len == 0) return null;
return self.pending_index_files.orderedRemove(0);
}
// ============================================================
// Time-Keeper Memory Tools: Scratchpad
// ============================================================
/// Write/replace entire scratchpad content
pub fn writeScratchpad(self: *AppState, text: []const u8) !void {
self.scratchpad.clearRetainingCapacity();
try self.scratchpad.appendSlice(self.allocator, text);
}
/// Append text to scratchpad
pub fn appendScratchpad(self: *AppState, text: []const u8) !void {
try self.scratchpad.appendSlice(self.allocator, text);
}
/// Read scratchpad content (returns borrowed slice)
pub fn readScratchpad(self: *AppState) []const u8 {
return self.scratchpad.items;
}
/// Clear scratchpad
pub fn clearScratchpad(self: *AppState) void {
self.scratchpad.clearRetainingCapacity();
}
// ============================================================
// Time-Keeper Memory Tools: Key-Value Store
// ============================================================
/// Set/update a key-value pair (makes owned copies)
/// Returns true if key was created, false if updated
pub fn kvSet(self: *AppState, key: []const u8, value: []const u8) !bool {
const owned_value = try self.allocator.dupe(u8, value);
errdefer self.allocator.free(owned_value);
if (self.kv_store.getPtr(key)) |existing_value| {
// Update existing - free old value
self.allocator.free(existing_value.*);
existing_value.* = owned_value;
return false; // updated
} else {
// Create new - also need to copy the key
const owned_key = try self.allocator.dupe(u8, key);
errdefer self.allocator.free(owned_key);
try self.kv_store.put(self.allocator, owned_key, owned_value);
return true; // created
}
}
/// Get value by key (returns borrowed slice, null if not found)
pub fn kvGet(self: *AppState, key: []const u8) ?[]const u8 {
return self.kv_store.get(key);
}
/// Delete key-value pair, returns true if key existed
pub fn kvDelete(self: *AppState, key: []const u8) bool {
if (self.kv_store.fetchRemove(key)) |kv| {
self.allocator.free(kv.key);
self.allocator.free(kv.value);
return true;
}
return false;
}
/// List all keys in KV store
/// Caller must free the returned slice (but not the key strings inside)
pub fn kvListKeys(self: *AppState) ![][]const u8 {
var keys = std.ArrayListUnmanaged([]const u8){};
errdefer keys.deinit(self.allocator);
var iter = self.kv_store.keyIterator();
while (iter.next()) |key| {
try keys.append(self.allocator, key.*);
}
return try keys.toOwnedSlice(self.allocator);
}
// ============================================================
// Time-Keeper Memory Tools: Vector Database
// ============================================================
/// Add text with embedding to vector memory
/// Returns the auto-generated ID (owned by state, borrowed by caller)
pub fn vectorAdd(self: *AppState, text: []const u8, embedding: []const f32) ![]const u8 {
// Generate string ID like "vec_1", "vec_2", etc.
const vec_id = try std.fmt.allocPrint(self.allocator, "vec_{d}", .{self.next_vector_id});
errdefer self.allocator.free(vec_id);
self.next_vector_id += 1;
const owned_text = try self.allocator.dupe(u8, text);
errdefer self.allocator.free(owned_text);
const owned_embedding = try self.allocator.dupe(f32, embedding);
errdefer self.allocator.free(owned_embedding);
const entry = VectorMemoryEntry{
.id = vec_id,
.text = owned_text,
.embedding = owned_embedding,
};
try self.vector_memory.put(self.allocator, vec_id, entry);
return vec_id;
}
/// Search vector memory by cosine similarity
/// Returns top_k results sorted by similarity (descending)
/// Caller must free the returned slice
pub fn vectorSearch(self: *AppState, query_embedding: []const f32, top_k: usize) ![]VectorSearchResult {
// Collect all entries with their similarities
var results = std.ArrayListUnmanaged(VectorSearchResult){};
defer results.deinit(self.allocator);
var iter = self.vector_memory.valueIterator();
while (iter.next()) |entry| {
const similarity = cosineSimilarity(query_embedding, entry.embedding);
try results.append(self.allocator, .{
.id = entry.id,
.text = entry.text,
.similarity = similarity,
});
}
// Sort by similarity descending
std.mem.sort(VectorSearchResult, results.items, {}, struct {
fn lessThan(_: void, a: VectorSearchResult, b: VectorSearchResult) bool {
return a.similarity > b.similarity; // Descending order
}
}.lessThan);
// Take top_k results
const result_count = @min(top_k, results.items.len);
const final_results = try self.allocator.alloc(VectorSearchResult, result_count);
@memcpy(final_results, results.items[0..result_count]);
return final_results;
}
/// Delete vector entry by ID, returns true if ID existed
pub fn vectorDelete(self: *AppState, id: []const u8) bool {
if (self.vector_memory.fetchRemove(id)) |kv| {
self.allocator.free(kv.key);
self.allocator.free(kv.value.id);
self.allocator.free(kv.value.text);
self.allocator.free(kv.value.embedding);
return true;
}
return false;
}
/// Get vector entry by ID (returns null if not found)
pub fn vectorGet(self: *AppState, id: []const u8) ?VectorMemoryEntry {
return self.vector_memory.get(id);
}
// ============================================================
// Timer System (for async notifications)
// ============================================================
/// Add a timer notification (called by timer thread when timer expires)
/// Thread-safe via mutex
pub fn addTimerNotification(self: *AppState, label: []const u8, duration_ms: u64) !void {
self.timer_mutex.lock();
defer self.timer_mutex.unlock();
const owned_label = try self.allocator.dupe(u8, label);
errdefer self.allocator.free(owned_label);
try self.timer_notifications.append(self.allocator, .{
.label = owned_label,
.duration_ms = duration_ms,
.fired_at = std.time.milliTimestamp(),
});
}
/// Pop all pending timer notifications (called by main loop)
/// Thread-safe via mutex. Caller owns returned slice and must free labels.
/// Returns empty slice if no notifications pending.
pub fn popTimerNotifications(self: *AppState) ![]TimerNotification {
self.timer_mutex.lock();
defer self.timer_mutex.unlock();
if (self.timer_notifications.items.len == 0) {
return &[_]TimerNotification{};
}
const result = try self.timer_notifications.toOwnedSlice(self.allocator);
self.timer_notifications = .{};
return result;
}
/// Check if there are pending timer notifications (non-destructive)
/// Thread-safe via mutex.
pub fn hasTimerNotifications(self: *AppState) bool {
self.timer_mutex.lock();
defer self.timer_mutex.unlock();
return self.timer_notifications.items.len > 0;
}
pub fn deinit(self: *AppState) void {
for (self.todos.items) |todo| {
self.allocator.free(todo.id);
self.allocator.free(todo.content);
}
self.todos.deinit(self.allocator);
// Free read_files hashmap
var iter = self.read_files.keyIterator();
while (iter.next()) |key| {
self.allocator.free(key.*);
}
self.read_files.deinit(self.allocator);
// Free indexed_files hashmap
var indexed_iter = self.indexed_files.keyIterator();
while (indexed_iter.next()) |key| {
self.allocator.free(key.*);
}
self.indexed_files.deinit(self.allocator);
// Free pending index files queue
for (self.pending_index_files.items) |pending| {
self.allocator.free(pending.path);
self.allocator.free(pending.content);
}
self.pending_index_files.deinit(self.allocator);
// Free Time-Keeper Memory Tools
// Scratchpad
self.scratchpad.deinit(self.allocator);
// KV Store - free both keys and values
var kv_iter = self.kv_store.iterator();
while (kv_iter.next()) |entry| {
self.allocator.free(entry.key_ptr.*);
self.allocator.free(entry.value_ptr.*);
}
self.kv_store.deinit(self.allocator);
// Vector Memory - free all entry fields
// Note: entry.id is the same pointer as the key, so only free key (not .id)
var vec_iter = self.vector_memory.iterator();
while (vec_iter.next()) |entry| {
self.allocator.free(entry.key_ptr.*); // This is also entry.value_ptr.id
self.allocator.free(entry.value_ptr.text);
self.allocator.free(entry.value_ptr.embedding);
}
self.vector_memory.deinit(self.allocator);
// Timer notifications - free labels
self.timer_mutex.lock();
for (self.timer_notifications.items) |notif| {
self.allocator.free(notif.label);
}
self.timer_notifications.deinit(self.allocator);
self.timer_mutex.unlock();
}
};