-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsession_user_message_search.cpp
More file actions
739 lines (684 loc) · 27.4 KB
/
Copy pathsession_user_message_search.cpp
File metadata and controls
739 lines (684 loc) · 27.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
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#include "session_user_message_search.hpp"
#include "session_serializer.hpp"
#include "session_storage.hpp"
#include "../utils/logger.hpp"
#include "../utils/utf8_path.hpp"
#include <sqlite3.h>
#include <algorithm>
#include <chrono>
#include <cctype>
#include <filesystem>
#include <iterator>
#include <sstream>
#include <unordered_set>
#include <utility>
namespace fs = std::filesystem;
namespace acecode {
namespace {
constexpr int kDefaultSearchLimit = 50;
constexpr int kMaxSearchLimit = 100;
constexpr std::size_t kSnippetMaxBytes = 180;
constexpr std::size_t kSnippetContextBytes = 70;
void set_error(std::string* error, const std::string& value) {
if (error) *error = value;
}
std::string sqlite_error(sqlite3* db, const std::string& prefix) {
std::ostringstream oss;
oss << prefix;
if (db) oss << ": " << sqlite3_errmsg(db);
return oss.str();
}
std::int64_t now_ms() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
}
std::string trim_copy(const std::string& input) {
std::size_t first = 0;
while (first < input.size() &&
std::isspace(static_cast<unsigned char>(input[first])) != 0) {
++first;
}
std::size_t last = input.size();
while (last > first &&
std::isspace(static_cast<unsigned char>(input[last - 1])) != 0) {
--last;
}
return input.substr(first, last - first);
}
std::string collapse_ws(const std::string& input) {
std::string out;
out.reserve(input.size());
bool in_ws = false;
for (unsigned char c : input) {
if (std::isspace(c) != 0) {
if (!in_ws && !out.empty()) out.push_back(' ');
in_ws = true;
continue;
}
out.push_back(static_cast<char>(c));
in_ws = false;
}
while (!out.empty() && out.back() == ' ') out.pop_back();
return out;
}
std::string ascii_lower(std::string value) {
std::transform(value.begin(), value.end(), value.begin(), [](unsigned char c) {
return static_cast<char>(std::tolower(c));
});
return value;
}
std::size_t utf8_safe_prefix_length(const std::string& text, std::size_t max_bytes) {
const std::size_t limit = (std::min)(max_bytes, text.size());
std::size_t i = 0;
std::size_t last_valid = 0;
while (i < limit) {
const unsigned char c = static_cast<unsigned char>(text[i]);
std::size_t seq_len = 0;
if ((c & 0x80u) == 0) {
seq_len = 1;
} else if ((c & 0xE0u) == 0xC0u) {
seq_len = 2;
} else if ((c & 0xF0u) == 0xE0u) {
seq_len = 3;
} else if ((c & 0xF8u) == 0xF0u) {
seq_len = 4;
} else {
break;
}
if (i + seq_len > limit || i + seq_len > text.size()) break;
bool valid = true;
for (std::size_t j = 1; j < seq_len; ++j) {
const unsigned char continuation = static_cast<unsigned char>(text[i + j]);
if ((continuation & 0xC0u) != 0x80u) {
valid = false;
break;
}
}
if (!valid) break;
i += seq_len;
last_valid = i;
}
return last_valid;
}
std::string utf8_prefix(const std::string& text, std::size_t max_bytes) {
if (text.size() <= max_bytes) return text;
return text.substr(0, utf8_safe_prefix_length(text, max_bytes));
}
std::string make_snippet(const std::string& source, const std::string& query_norm) {
const std::string collapsed = collapse_ws(source);
if (collapsed.empty()) return {};
const std::string norm = ascii_lower(collapsed);
const std::size_t hit = query_norm.empty() ? std::string::npos : norm.find(query_norm);
if (hit == std::string::npos || collapsed.size() <= kSnippetMaxBytes) {
std::string snippet = utf8_prefix(collapsed, kSnippetMaxBytes);
if (snippet.size() < collapsed.size()) snippet += "...";
return snippet;
}
std::size_t start = hit > kSnippetContextBytes ? hit - kSnippetContextBytes : 0;
start = utf8_safe_prefix_length(collapsed, start);
std::size_t end = (std::min)(collapsed.size(), hit + query_norm.size() + kSnippetContextBytes);
end = utf8_safe_prefix_length(collapsed, end);
if (end < start) end = start;
std::string snippet = collapsed.substr(start, end - start);
if (start > 0) snippet = "..." + snippet;
if (end < collapsed.size()) snippet += "...";
return snippet;
}
std::string attachment_text_from_names(const std::vector<std::string>& names) {
std::ostringstream oss;
for (std::size_t i = 0; i < names.size(); ++i) {
if (i > 0) oss << "\n";
oss << names[i];
}
return oss.str();
}
nlohmann::json attachment_names_to_json(const std::vector<std::string>& names) {
nlohmann::json arr = nlohmann::json::array();
for (const auto& name : names) arr.push_back(name);
return arr;
}
std::vector<std::string> attachment_names_from_json(const std::string& raw) {
std::vector<std::string> out;
if (raw.empty()) return out;
try {
auto parsed = nlohmann::json::parse(raw);
if (!parsed.is_array()) return out;
for (const auto& item : parsed) {
if (item.is_string()) out.push_back(item.get<std::string>());
}
} catch (...) {
}
return out;
}
std::string column_text(sqlite3_stmt* stmt, int index) {
const unsigned char* text = sqlite3_column_text(stmt, index);
return text ? reinterpret_cast<const char*>(text) : std::string{};
}
bool bind_text(sqlite3_stmt* stmt, int index, const std::string& value) {
return sqlite3_bind_text(stmt, index, value.c_str(), -1, SQLITE_TRANSIENT) == SQLITE_OK;
}
int match_score(const std::string& user_text,
const std::string& attachment_text,
const std::string& query_norm,
int message_ordinal) {
const bool user_hit = ascii_lower(user_text).find(query_norm) != std::string::npos;
const bool attachment_hit = ascii_lower(attachment_text).find(query_norm) != std::string::npos;
int score = 300;
if (attachment_hit) score = 700;
if (user_hit) score = 900;
return score + (std::min)(message_ordinal, 99);
}
int sqlite_cancel_check(void* context) {
const auto* should_cancel =
static_cast<const std::function<bool()>*>(context);
return should_cancel && *should_cancel && (*should_cancel)() ? 1 : 0;
}
} // namespace
bool is_searchable_visible_user_message(const ChatMessage& msg) {
return msg.role == "user" &&
!msg.is_meta &&
!msg.is_compact_summary &&
!(msg.metadata.is_object() && msg.metadata.value("hidden_goal_context", false));
}
std::string searchable_user_message_text(const ChatMessage& msg) {
if (!is_searchable_visible_user_message(msg)) return {};
if (msg.metadata.is_object()) {
auto it = msg.metadata.find("display_text");
if (it != msg.metadata.end() && it->is_string()) {
std::string display = trim_copy(it->get<std::string>());
if (!display.empty()) return display;
}
}
return msg.content;
}
std::vector<std::string> searchable_user_message_attachment_names(const ChatMessage& msg) {
std::vector<std::string> out;
if (!is_searchable_visible_user_message(msg)) return out;
if (!msg.content_parts.is_array()) return out;
std::unordered_set<std::string> seen;
for (const auto& part : msg.content_parts) {
if (!part.is_object()) continue;
const std::string type = part.value("type", std::string{});
if (type != "file" && type != "image") continue;
auto it = part.find("attachment");
if (it == part.end() || !it->is_object()) continue;
auto name_it = it->find("name");
if (name_it == it->end() || !name_it->is_string()) continue;
std::string name = trim_copy(name_it->get<std::string>());
if (name.empty()) continue;
if (seen.insert(name).second) out.push_back(std::move(name));
}
return out;
}
std::optional<SearchableUserMessage> build_searchable_user_message(
const std::string& session_id,
int message_ordinal,
const ChatMessage& msg) {
if (!is_searchable_visible_user_message(msg)) return std::nullopt;
SearchableUserMessage out;
out.session_id = session_id;
out.message_ordinal = message_ordinal;
out.message_uuid = msg.uuid;
out.user_text = searchable_user_message_text(msg);
out.attachment_names = searchable_user_message_attachment_names(msg);
out.attachment_text = attachment_text_from_names(out.attachment_names);
out.search_text = out.user_text;
if (!out.attachment_text.empty()) {
if (!out.search_text.empty()) out.search_text += "\n";
out.search_text += out.attachment_text;
}
out.search_text_norm = ascii_lower(out.search_text);
out.snippet_text = out.user_text.empty() ? out.attachment_text : out.user_text;
if (trim_copy(out.search_text).empty()) return std::nullopt;
return out;
}
SessionUserMessageFileSignature session_user_message_file_signature(
const std::string& jsonl_path) {
SessionUserMessageFileSignature out;
if (jsonl_path.empty()) return out;
std::error_code ec;
const fs::path path = path_from_utf8(jsonl_path);
if (!fs::exists(path, ec) || ec) return out;
out.exists = true;
out.size = static_cast<std::int64_t>(fs::file_size(path, ec));
if (ec) {
out.size = 0;
ec.clear();
}
out.mtime = fs::last_write_time(path, ec).time_since_epoch().count();
if (ec) out.mtime = 0;
return out;
}
SessionUserMessageIndex::SessionUserMessageIndex(std::string project_dir)
: project_dir_(std::move(project_dir)) {}
SessionUserMessageIndex::~SessionUserMessageIndex() {
if (db_) sqlite3_close(db_);
}
bool SessionUserMessageIndex::open(std::string* error) {
if (db_) return true;
if (project_dir_.empty()) {
set_error(error, "project directory required");
return false;
}
std::error_code ec;
fs::create_directories(path_from_utf8(project_dir_), ec);
if (ec) {
set_error(error, "failed to create project directory: " + ec.message());
return false;
}
// 独立于 goal 的 state.sqlite3:SessionManager::existing_goal_store() 以
// 「state.sqlite3 文件存在」作为 goal store 的惰性打开条件。如果索引也
// 写进 state.sqlite3,每个普通会话的第一条消息就会把文件造出来,goal
// store 随之在每个 turn 打开常驻连接(Windows 下还会锁住项目目录)。
// 索引连接是栈对象短开短关,单独一个 db 文件不改变任何既有约定。
const std::string db_path =
path_to_utf8(path_from_utf8(project_dir_) / "user_message_search.sqlite3");
if (sqlite3_open_v2(db_path.c_str(), &db_,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
nullptr) != SQLITE_OK) {
set_error(error, sqlite_error(db_, "failed to open session search database"));
if (db_) {
sqlite3_close(db_);
db_ = nullptr;
}
return false;
}
sqlite3_busy_timeout(db_, 5000);
return true;
}
bool SessionUserMessageIndex::exec(const char* sql, std::string* error) {
char* raw_error = nullptr;
const int rc = sqlite3_exec(db_, sql, nullptr, nullptr, &raw_error);
if (rc != SQLITE_OK) {
std::string msg = raw_error ? raw_error : sqlite3_errmsg(db_);
sqlite3_free(raw_error);
set_error(error, msg);
return false;
}
return true;
}
bool SessionUserMessageIndex::initialize(std::string* error) {
if (!open(error)) return false;
constexpr const char* schema_sql =
"CREATE TABLE IF NOT EXISTS session_user_message_index ("
"session_id TEXT NOT NULL,"
"message_ordinal INTEGER NOT NULL,"
"message_uuid TEXT NOT NULL DEFAULT '',"
"user_text TEXT NOT NULL DEFAULT '',"
"attachment_text TEXT NOT NULL DEFAULT '',"
"attachment_names_json TEXT NOT NULL DEFAULT '[]',"
"search_text TEXT NOT NULL DEFAULT '',"
"search_text_norm TEXT NOT NULL DEFAULT '',"
"snippet_text TEXT NOT NULL DEFAULT '',"
"PRIMARY KEY(session_id, message_ordinal)"
");"
"CREATE INDEX IF NOT EXISTS idx_session_user_message_session "
"ON session_user_message_index(session_id, message_ordinal DESC);"
"CREATE TABLE IF NOT EXISTS session_user_message_sources ("
"session_id TEXT PRIMARY KEY,"
"jsonl_path TEXT NOT NULL,"
"jsonl_mtime INTEGER NOT NULL,"
"jsonl_size INTEGER NOT NULL,"
"indexed_at_ms INTEGER NOT NULL"
");";
if (!exec(schema_sql, error)) return false;
return true;
}
bool SessionUserMessageIndex::source_matches_signature(
const std::string& session_id,
const SessionUserMessageFileSignature& signature,
std::string* error) {
if (!initialize(error)) return false;
sqlite3_stmt* stmt = nullptr;
constexpr const char* sql =
"SELECT jsonl_mtime, jsonl_size FROM session_user_message_sources "
"WHERE session_id = ?;";
if (sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr) != SQLITE_OK) {
set_error(error, sqlite_error(db_, "prepare source read failed"));
return false;
}
bind_text(stmt, 1, session_id);
const int rc = sqlite3_step(stmt);
bool matches = false;
if (rc == SQLITE_ROW) {
matches = signature.exists &&
sqlite3_column_int64(stmt, 0) == signature.mtime &&
sqlite3_column_int64(stmt, 1) == signature.size;
} else if (rc != SQLITE_DONE) {
set_error(error, sqlite_error(db_, "source read failed"));
}
sqlite3_finalize(stmt);
return matches;
}
bool SessionUserMessageIndex::update_source(
const std::string& session_id,
const std::string& jsonl_path,
const SessionUserMessageFileSignature& signature,
std::string* error) {
sqlite3_stmt* stmt = nullptr;
constexpr const char* sql =
"INSERT INTO session_user_message_sources("
"session_id, jsonl_path, jsonl_mtime, jsonl_size, indexed_at_ms"
") VALUES(?, ?, ?, ?, ?) "
"ON CONFLICT(session_id) DO UPDATE SET "
"jsonl_path = excluded.jsonl_path,"
"jsonl_mtime = excluded.jsonl_mtime,"
"jsonl_size = excluded.jsonl_size,"
"indexed_at_ms = excluded.indexed_at_ms;";
if (sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr) != SQLITE_OK) {
set_error(error, sqlite_error(db_, "prepare source update failed"));
return false;
}
bool ok = bind_text(stmt, 1, session_id) &&
bind_text(stmt, 2, jsonl_path) &&
sqlite3_bind_int64(stmt, 3, signature.mtime) == SQLITE_OK &&
sqlite3_bind_int64(stmt, 4, signature.size) == SQLITE_OK &&
sqlite3_bind_int64(stmt, 5, now_ms()) == SQLITE_OK;
if (ok && sqlite3_step(stmt) != SQLITE_DONE) {
ok = false;
set_error(error, sqlite_error(db_, "source update failed"));
}
sqlite3_finalize(stmt);
return ok;
}
bool SessionUserMessageIndex::upsert_searchable_message(
const SearchableUserMessage& message,
std::string* error) {
sqlite3_stmt* stmt = nullptr;
constexpr const char* sql =
"INSERT INTO session_user_message_index("
"session_id, message_ordinal, message_uuid, user_text, attachment_text, "
"attachment_names_json, search_text, search_text_norm, snippet_text"
") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?) "
"ON CONFLICT(session_id, message_ordinal) DO UPDATE SET "
"message_uuid = excluded.message_uuid,"
"user_text = excluded.user_text,"
"attachment_text = excluded.attachment_text,"
"attachment_names_json = excluded.attachment_names_json,"
"search_text = excluded.search_text,"
"search_text_norm = excluded.search_text_norm,"
"snippet_text = excluded.snippet_text;";
if (sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr) != SQLITE_OK) {
set_error(error, sqlite_error(db_, "prepare message upsert failed"));
return false;
}
const std::string names_json = attachment_names_to_json(message.attachment_names).dump();
bool ok = bind_text(stmt, 1, message.session_id) &&
sqlite3_bind_int(stmt, 2, message.message_ordinal) == SQLITE_OK &&
bind_text(stmt, 3, message.message_uuid) &&
bind_text(stmt, 4, message.user_text) &&
bind_text(stmt, 5, message.attachment_text) &&
bind_text(stmt, 6, names_json) &&
bind_text(stmt, 7, message.search_text) &&
bind_text(stmt, 8, message.search_text_norm) &&
bind_text(stmt, 9, message.snippet_text);
if (ok && sqlite3_step(stmt) != SQLITE_DONE) {
ok = false;
set_error(error, sqlite_error(db_, "message upsert failed"));
}
sqlite3_finalize(stmt);
return ok;
}
bool SessionUserMessageIndex::index_appended_message(
const std::string& session_id,
int message_ordinal,
const ChatMessage& msg,
const std::string& jsonl_path,
const SessionUserMessageFileSignature& before_append,
std::string* error) {
if (session_id.empty() || jsonl_path.empty()) return true;
if (!initialize(error)) return false;
const auto after = session_user_message_file_signature(jsonl_path);
const bool source_was_fresh = source_matches_signature(session_id, before_append, error);
if (!source_was_fresh) {
return rebuild_session(session_id, jsonl_path, error);
}
if (auto searchable = build_searchable_user_message(session_id, message_ordinal, msg)) {
if (!upsert_searchable_message(*searchable, error)) return false;
}
return update_source(session_id, jsonl_path, after, error);
}
bool SessionUserMessageIndex::rebuild_session(
const std::string& session_id,
const std::string& jsonl_path,
const std::vector<ChatMessage>& messages,
std::string* error,
const std::function<bool()>& should_cancel) {
if (session_id.empty() || jsonl_path.empty()) return true;
if (should_cancel && should_cancel()) {
set_error(error, "cancelled");
return false;
}
if (!initialize(error)) return false;
const auto signature = session_user_message_file_signature(jsonl_path);
if (!exec("BEGIN IMMEDIATE TRANSACTION;", error)) return false;
bool ok = true;
sqlite3_stmt* del = nullptr;
if (sqlite3_prepare_v2(db_,
"DELETE FROM session_user_message_index WHERE session_id = ?;",
-1, &del, nullptr) != SQLITE_OK) {
set_error(error, sqlite_error(db_, "prepare message delete failed"));
ok = false;
}
if (ok) {
bind_text(del, 1, session_id);
if (sqlite3_step(del) != SQLITE_DONE) {
set_error(error, sqlite_error(db_, "message delete failed"));
ok = false;
}
}
if (del) sqlite3_finalize(del);
if (ok) {
for (std::size_t i = 0; i < messages.size(); ++i) {
if (should_cancel && should_cancel()) {
set_error(error, "cancelled");
ok = false;
break;
}
auto searchable = build_searchable_user_message(
session_id, static_cast<int>(i), messages[i]);
if (!searchable.has_value()) continue;
if (!upsert_searchable_message(*searchable, error)) {
ok = false;
break;
}
}
}
if (ok) ok = update_source(session_id, jsonl_path, signature, error);
if (ok) {
if (!exec("COMMIT;", error)) ok = false;
} else {
std::string ignored;
exec("ROLLBACK;", &ignored);
}
return ok;
}
bool SessionUserMessageIndex::rebuild_session(
const std::string& session_id,
const std::string& jsonl_path,
std::string* error,
const std::function<bool()>& should_cancel) {
if (should_cancel && should_cancel()) {
set_error(error, "cancelled");
return false;
}
return rebuild_session(session_id, jsonl_path,
SessionStorage::load_messages(jsonl_path),
error, should_cancel);
}
bool SessionUserMessageIndex::ensure_session_indexed(
const std::string& session_id,
const std::string& jsonl_path,
std::string* error,
const std::function<bool()>& should_cancel) {
if (session_id.empty() || jsonl_path.empty()) return true;
if (should_cancel && should_cancel()) {
set_error(error, "cancelled");
return false;
}
if (!initialize(error)) return false;
const auto signature = session_user_message_file_signature(jsonl_path);
if (source_matches_signature(session_id, signature, error)) return true;
return rebuild_session(session_id, jsonl_path, error, should_cancel);
}
bool SessionUserMessageIndex::remove_session(const std::string& session_id,
std::string* error) {
if (session_id.empty()) return true;
if (!initialize(error)) return false;
constexpr const char* sqls[] = {
"DELETE FROM session_user_message_index WHERE session_id = ?;",
"DELETE FROM session_user_message_sources WHERE session_id = ?;",
};
for (const char* sql : sqls) {
sqlite3_stmt* stmt = nullptr;
if (sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr) != SQLITE_OK) {
set_error(error, sqlite_error(db_, "prepare session index removal failed"));
return false;
}
bool ok = bind_text(stmt, 1, session_id) &&
sqlite3_step(stmt) == SQLITE_DONE;
if (!ok) set_error(error, sqlite_error(db_, "session index removal failed"));
sqlite3_finalize(stmt);
if (!ok) return false;
}
return true;
}
void SessionUserMessageIndex::prune_removed_sessions() {
// 孤儿回收:sources 里记录的 JSONL 已从磁盘消失(purge / 手工删除),
// 索引里的消息全文必须跟着删,否则已删除会话的用户输入会残留在
// state.sqlite3 且永远不会被重建逻辑清理。
sqlite3_stmt* stmt = nullptr;
if (sqlite3_prepare_v2(db_,
"SELECT session_id, jsonl_path FROM session_user_message_sources;",
-1, &stmt, nullptr) != SQLITE_OK) {
return;
}
std::vector<std::string> removed;
while (sqlite3_step(stmt) == SQLITE_ROW) {
const std::string session_id = column_text(stmt, 0);
const std::string jsonl_path = column_text(stmt, 1);
if (!session_user_message_file_signature(jsonl_path).exists) {
removed.push_back(session_id);
}
}
sqlite3_finalize(stmt);
for (const auto& session_id : removed) {
std::string ignored;
remove_session(session_id, &ignored);
}
}
bool SessionUserMessageIndex::ensure_project_indexed(
std::string* error,
const std::function<bool()>& should_cancel) {
if (!initialize(error)) return false;
for (const auto& meta : SessionStorage::list_session_metadata(
project_dir_, should_cancel)) {
if (should_cancel && should_cancel()) {
set_error(error, "cancelled");
return false;
}
auto candidates = SessionStorage::find_session_files(project_dir_, meta.id);
if (candidates.empty()) continue;
std::string session_error;
if (!ensure_session_indexed(meta.id, candidates.front().jsonl_path,
&session_error, should_cancel)) {
if (session_error == "cancelled") {
set_error(error, session_error);
return false;
}
// 单个 session 的失败(文件被锁/损坏)不挡其它 session 的搜索。
LOG_WARN("[session-search] index refresh skipped session " + meta.id +
": " + session_error);
}
}
if (should_cancel && should_cancel()) {
set_error(error, "cancelled");
return false;
}
prune_removed_sessions();
return true;
}
std::vector<SessionUserMessageSearchResult> SessionUserMessageIndex::search(
const std::string& query,
int limit,
std::string* error,
const std::function<bool()>& should_cancel) {
std::vector<SessionUserMessageSearchResult> out;
const std::string query_norm = ascii_lower(trim_copy(query));
if (query_norm.empty()) return out;
if (limit <= 0) limit = kDefaultSearchLimit;
limit = (std::min)(limit, kMaxSearchLimit);
if (!initialize(error)) return out;
if (should_cancel && should_cancel()) {
set_error(error, "cancelled");
return out;
}
if (should_cancel) {
sqlite3_progress_handler(
db_, 1000, sqlite_cancel_check,
const_cast<std::function<bool()>*>(&should_cancel));
}
sqlite3_stmt* stmt = nullptr;
constexpr const char* sql =
"SELECT i.session_id, i.message_ordinal, i.user_text, i.attachment_text, "
"i.attachment_names_json, i.snippet_text "
"FROM session_user_message_index i "
"JOIN ("
"SELECT session_id, MAX(message_ordinal) AS message_ordinal "
"FROM session_user_message_index "
"WHERE instr(search_text_norm, ?) > 0 "
"GROUP BY session_id"
") latest ON latest.session_id = i.session_id "
"AND latest.message_ordinal = i.message_ordinal "
"ORDER BY i.message_ordinal DESC "
"LIMIT ?;";
if (sqlite3_prepare_v2(db_, sql, -1, &stmt, nullptr) != SQLITE_OK) {
sqlite3_progress_handler(db_, 0, nullptr, nullptr);
set_error(error, sqlite_error(db_, "prepare user message search failed"));
return out;
}
bind_text(stmt, 1, query_norm);
sqlite3_bind_int(stmt, 2, limit * 8);
int step_rc = SQLITE_OK;
while ((step_rc = sqlite3_step(stmt)) == SQLITE_ROW) {
const std::string session_id = column_text(stmt, 0);
const int ordinal = sqlite3_column_int(stmt, 1);
const std::string user_text = column_text(stmt, 2);
const std::string attachment_text = column_text(stmt, 3);
const auto names = attachment_names_from_json(column_text(stmt, 4));
std::string snippet_source = column_text(stmt, 5);
if (ascii_lower(snippet_source).find(query_norm) == std::string::npos &&
ascii_lower(attachment_text).find(query_norm) != std::string::npos) {
snippet_source = attachment_text;
}
SessionUserMessageSearchResult result;
result.session_id = session_id;
result.message_ordinal = ordinal;
result.score = match_score(user_text, attachment_text, query_norm, ordinal);
result.snippet = make_snippet(snippet_source, query_norm);
for (const auto& name : names) {
if (ascii_lower(name).find(query_norm) != std::string::npos) {
result.matched_attachment_names.push_back(name);
}
}
out.push_back(std::move(result));
}
const bool completed = step_rc == SQLITE_DONE || step_rc == SQLITE_ROW;
sqlite3_finalize(stmt);
sqlite3_progress_handler(db_, 0, nullptr, nullptr);
if (!completed) {
set_error(error, step_rc == SQLITE_INTERRUPT
? std::string{"cancelled"}
: sqlite_error(db_, "user message search failed"));
}
std::sort(out.begin(), out.end(),
[](const auto& a, const auto& b) {
if (a.score != b.score) return a.score > b.score;
return a.message_ordinal > b.message_ordinal;
});
if (static_cast<int>(out.size()) > limit) {
out.erase(out.begin() + limit, out.end());
}
return out;
}
} // namespace acecode