-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmtime_tracker.cpp
More file actions
324 lines (289 loc) · 11.2 KB
/
Copy pathmtime_tracker.cpp
File metadata and controls
324 lines (289 loc) · 11.2 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
#include "mtime_tracker.hpp"
#include "utils/utf8_path.hpp"
#include <algorithm>
#include <utility>
namespace acecode {
namespace {
constexpr std::size_t kMaxReadObservationEntries = 100;
std::string normalize_tracker_path_key(const std::string& path) {
std::error_code ec;
auto fs_path = path_from_utf8(path);
auto canonical = std::filesystem::weakly_canonical(fs_path, ec);
if (!ec && !canonical.empty()) {
return path_to_utf8(canonical.lexically_normal());
}
ec.clear();
auto absolute = std::filesystem::absolute(fs_path, ec);
if (!ec && !absolute.empty()) {
return path_to_utf8(absolute.lexically_normal());
}
return path_to_utf8(fs_path.lexically_normal());
}
MtimeTracker::ReadObservationKey make_read_observation_key(
const std::string& path,
int start_line,
int end_line,
bool byte_mode,
uint64_t byte_offset,
size_t max_bytes
) {
return MtimeTracker::ReadObservationKey{
normalize_tracker_path_key(path),
start_line,
end_line,
byte_mode,
byte_offset,
max_bytes
};
}
void remove_read_observations_for_path_locked(
std::map<MtimeTracker::ReadObservationKey, MtimeTracker::ReadObservation>& observations,
std::vector<MtimeTracker::ReadObservationKey>& lru,
const std::string& normalized_path
) {
for (auto it = observations.begin(); it != observations.end();) {
if (it->first.path == normalized_path) {
it = observations.erase(it);
} else {
++it;
}
}
lru.erase(
std::remove_if(lru.begin(), lru.end(), [&](const MtimeTracker::ReadObservationKey& key) {
return key.path == normalized_path;
}),
lru.end());
}
void touch_read_observation_lru_locked(
std::map<MtimeTracker::ReadObservationKey, MtimeTracker::ReadObservation>& observations,
std::vector<MtimeTracker::ReadObservationKey>& lru,
const MtimeTracker::ReadObservationKey& key
) {
lru.erase(std::remove(lru.begin(), lru.end(), key), lru.end());
lru.push_back(key);
while (lru.size() > kMaxReadObservationEntries) {
auto evict = lru.front();
lru.erase(lru.begin());
observations.erase(evict);
}
}
} // namespace
MtimeTracker::FileWriteGuard::FileWriteGuard(std::shared_ptr<std::mutex> mutex)
: mutex_(std::move(mutex)), lock_(*mutex_) {}
MtimeTracker& MtimeTracker::instance() {
static MtimeTracker tracker;
return tracker;
}
void MtimeTracker::record_read(const std::string& path) {
record_read(path, "", false);
}
void MtimeTracker::record_read(const std::string& path, const std::string& content, bool partial) {
record_read(path, content, partial, FileReadEditMetadata{});
}
void MtimeTracker::record_read(const std::string& path,
const std::string& normalized_content,
bool partial,
const FileReadEditMetadata& metadata) {
try {
const std::string key = normalize_tracker_path_key(path);
auto mtime = std::filesystem::last_write_time(path_from_utf8(key));
std::lock_guard<std::mutex> lk(mu_);
records_[key] = Record{
mtime,
partial,
partial ? std::optional<std::string>{} : std::optional<std::string>{normalized_content},
std::optional<FileReadEditMetadata>{metadata}
};
} catch (...) {
// File may not exist yet; that's OK
}
}
void MtimeTracker::seed_transcript_read_baseline(
const std::string& path,
const std::string& normalized_content,
const FileReadEditMetadata& metadata
) {
const std::string key = normalize_tracker_path_key(path);
std::lock_guard<std::mutex> lk(mu_);
records_[key] = Record{
clock::time_point::min(),
false,
std::optional<std::string>{normalized_content},
std::optional<FileReadEditMetadata>{metadata}
};
remove_read_observations_for_path_locked(read_observations_, read_observation_lru_, key);
}
bool MtimeTracker::has_unchanged_read_observation(
const std::string& path,
int start_line,
int end_line,
bool byte_mode,
uint64_t byte_offset,
size_t max_bytes
) const {
return unchanged_read_observation(
path, start_line, end_line, byte_mode, byte_offset, max_bytes).has_value();
}
std::optional<MtimeTracker::ReadObservation> MtimeTracker::unchanged_read_observation(
const std::string& path,
int start_line,
int end_line,
bool byte_mode,
uint64_t byte_offset,
size_t max_bytes
) const {
const auto key = make_read_observation_key(
path, start_line, end_line, byte_mode, byte_offset, max_bytes);
std::lock_guard<std::mutex> lk(mu_);
auto it = read_observations_.find(key);
if (it == read_observations_.end()) return std::nullopt;
try {
auto current_mtime = std::filesystem::last_write_time(path_from_utf8(key.path));
if (current_mtime == it->second.mtime) {
return it->second;
}
} catch (...) {
}
return std::nullopt;
}
void MtimeTracker::record_read_observation(const std::string& path,
int start_line,
int end_line,
bool byte_mode,
uint64_t byte_offset,
size_t max_bytes) {
try {
auto key = make_read_observation_key(
path, start_line, end_line, byte_mode, byte_offset, max_bytes);
auto mtime = std::filesystem::last_write_time(path_from_utf8(key.path));
std::lock_guard<std::mutex> lk(mu_);
read_observations_[key] = ReadObservation{mtime, {}, {}};
touch_read_observation_lru_locked(read_observations_, read_observation_lru_, key);
} catch (...) {
// File may not exist or may be inaccessible; dedup is optional.
}
}
void MtimeTracker::record_read_observation_result(
const std::string& path,
int start_line,
int end_line,
const std::string& tool_call_id,
const std::string& persisted_output_path,
bool byte_mode,
uint64_t byte_offset,
size_t max_bytes
) {
if (tool_call_id.empty() && persisted_output_path.empty()) return;
const auto key = make_read_observation_key(
path, start_line, end_line, byte_mode, byte_offset, max_bytes);
std::lock_guard<std::mutex> lk(mu_);
auto it = read_observations_.find(key);
if (it == read_observations_.end()) return;
if (!tool_call_id.empty()) it->second.tool_call_id = tool_call_id;
if (!persisted_output_path.empty()) {
it->second.persisted_output_path = persisted_output_path;
}
}
void MtimeTracker::invalidate_read_observations(const std::string& path) {
const std::string key = normalize_tracker_path_key(path);
std::lock_guard<std::mutex> lk(mu_);
remove_read_observations_for_path_locked(read_observations_, read_observation_lru_, key);
}
void MtimeTracker::clear_read_observations() {
std::lock_guard<std::mutex> lk(mu_);
read_observations_.clear();
read_observation_lru_.clear();
}
void MtimeTracker::invalidate_agent_read_state(const std::string& path) {
const std::string key = normalize_tracker_path_key(path);
std::lock_guard<std::mutex> lk(mu_);
records_.erase(key);
remove_read_observations_for_path_locked(read_observations_, read_observation_lru_, key);
}
bool MtimeTracker::was_externally_modified(const std::string& path) const {
const std::string key = normalize_tracker_path_key(path);
std::lock_guard<std::mutex> lk(mu_);
auto it = records_.find(key);
if (it == records_.end()) {
return false; // no record, assume safe
}
try {
auto current_mtime = std::filesystem::last_write_time(path_from_utf8(key));
return current_mtime != it->second.mtime;
} catch (...) {
return false; // file deleted or inaccessible
}
}
MtimeTracker::ReadBaselineCheck MtimeTracker::validate_read_baseline_for_edit(
const std::string& path,
const std::string& current_content
) const {
const std::string key = normalize_tracker_path_key(path);
std::lock_guard<std::mutex> lk(mu_);
auto it = records_.find(key);
if (it == records_.end()) {
return {ReadBaselineStatus::NotRead, false};
}
try {
auto current_mtime = std::filesystem::last_write_time(path_from_utf8(key));
if (current_mtime == it->second.mtime) {
if (it->second.metadata && it->second.metadata->lossy) {
return {ReadBaselineStatus::UnsafeRead, false};
}
return {ReadBaselineStatus::Ok, false};
}
if (it->second.partial) {
return it->second.metadata && it->second.metadata->lossy
? ReadBaselineCheck{ReadBaselineStatus::UnsafeRead, false}
: ReadBaselineCheck{ReadBaselineStatus::ExternallyModified, false};
}
// Windows/同步盘/杀毒软件可能只刷新 mtime。全量读过的文件再比一次内容,
// 内容没变就允许继续写,避免 agent 陷入反复重读/重试。
if (it->second.content.has_value() && *it->second.content == current_content) {
return {ReadBaselineStatus::Ok, true};
}
} catch (...) {
// Fall through to the conservative conflict path.
}
return {ReadBaselineStatus::ExternallyModified, false};
}
void MtimeTracker::record_write(const std::string& path) {
try {
const std::string key = normalize_tracker_path_key(path);
auto mtime = std::filesystem::last_write_time(path_from_utf8(key));
std::lock_guard<std::mutex> lk(mu_);
records_[key] = Record{mtime, false, std::optional<std::string>{}, std::optional<FileReadEditMetadata>{}};
remove_read_observations_for_path_locked(read_observations_, read_observation_lru_, key);
} catch (...) {}
}
void MtimeTracker::record_write(const std::string& path, const std::string& content) {
try {
const std::string key = normalize_tracker_path_key(path);
auto mtime = std::filesystem::last_write_time(path_from_utf8(key));
std::lock_guard<std::mutex> lk(mu_);
records_[key] = Record{mtime, false, std::optional<std::string>{content}, std::optional<FileReadEditMetadata>{}};
remove_read_observations_for_path_locked(read_observations_, read_observation_lru_, key);
} catch (...) {}
}
std::optional<FileReadEditMetadata> MtimeTracker::read_metadata(const std::string& path) const {
const std::string key = normalize_tracker_path_key(path);
std::lock_guard<std::mutex> lk(mu_);
auto it = records_.find(key);
if (it == records_.end()) return std::nullopt;
return it->second.metadata;
}
MtimeTracker::FileWriteGuard MtimeTracker::acquire_write_guard(const std::string& path) {
const std::string key = normalize_tracker_path_key(path);
std::shared_ptr<std::mutex> file_mutex;
{
std::lock_guard<std::mutex> lk(mu_);
auto& weak = file_locks_[key];
file_mutex = weak.lock();
if (!file_mutex) {
file_mutex = std::make_shared<std::mutex>();
weak = file_mutex;
}
}
return FileWriteGuard{std::move(file_mutex)};
}
} // namespace acecode