mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-22 16:10:50 +00:00
fix: address Codex review on PR #3604
This commit is contained in:
1 parent
6af037064c
commit
f56c9a4675
2 files changed
+85
-1
No files matched your search
@@ -290,11 +290,64 @@ test('ToolOutputStore session quotas evict a borrowed owner instead of the fresh
|
||||
await freshCopy.spillPromise;
|
||||
assert.ok(files.has(`/netcatty/${fresh.id}-chat-source.log`));
|
||||
// The old owner is gone, and evicting it deletes the spill path the fork
|
||||
// alias used to borrow (the alias never owned that file).
|
||||
// alias used to borrow (the alias never owned that file). The alias is
|
||||
// retired alongside so the fork never keeps a handle registered against a
|
||||
// deleted file.
|
||||
assert.equal(original.get(handle.id, 'chat-source'), undefined);
|
||||
assert.equal(original.get(handle.id, 'chat-fork'), undefined);
|
||||
assert.deepEqual(deletedPaths, [`/netcatty/${handle.id}-chat-source.log`]);
|
||||
});
|
||||
|
||||
test('ToolOutputStore retires the borrowed fork alias before its owner is evicted', async () => {
|
||||
const files = new Map<string, { record: PersistedToolOutputRecord; content: string }>();
|
||||
const deletedPaths: string[] = [];
|
||||
const persistence: ToolOutputPersistence = {
|
||||
write: async (record, content) => {
|
||||
const path = `/netcatty/${record.handleId}-${record.chatSessionId}.log`;
|
||||
files.set(path, { record, content });
|
||||
return path;
|
||||
},
|
||||
read: async () => null,
|
||||
delete: async path => {
|
||||
deletedPaths.push(path);
|
||||
files.delete(path);
|
||||
},
|
||||
};
|
||||
|
||||
// A fork holds a non-owning alias of the source session's only spill path.
|
||||
// Filling the source session's quota must retire the alias when its owner
|
||||
// is evicted, so the fork never serves a dangling handle: the read fails
|
||||
// (content is gone) and the alias is gone from the registry, instead of
|
||||
// lingering until a later read trips over the deleted file.
|
||||
const original = new ToolOutputStore({ spillThresholdChars: 0, maxHandlesPerSession: 1, persistence });
|
||||
const handle = original.store({
|
||||
chatSessionId: 'chat-source',
|
||||
capabilityId: 'terminal.execute',
|
||||
content: 'C'.repeat(30_000),
|
||||
});
|
||||
await handle.spillPromise;
|
||||
|
||||
// `read` cannot serve the durable content, so the clone aliases the
|
||||
// source-owned spill path as a non-owning borrow.
|
||||
await original.rehomeChatSession('chat-source', 'chat-fork');
|
||||
const alias = original.get(handle.id, 'chat-fork');
|
||||
assert.ok(alias);
|
||||
assert.equal(alias.borrowedFilePath, true);
|
||||
|
||||
const fresh = original.store({
|
||||
chatSessionId: 'chat-source',
|
||||
capabilityId: 'terminal.execute',
|
||||
content: 'D'.repeat(30_000),
|
||||
});
|
||||
await fresh.spillPromise;
|
||||
assert.ok(files.has(`/netcatty/${fresh.id}-chat-source.log`));
|
||||
// The alias is retired before the owner's file is deleted.
|
||||
assert.equal(original.get(handle.id, 'chat-fork'), undefined);
|
||||
assert.deepEqual(deletedPaths, [`/netcatty/${handle.id}-chat-source.log`]);
|
||||
// The fork's read no longer resolves the retired alias.
|
||||
assert.equal(await original.readChunkAsync({ handleId: handle.id, mode: 'head', maxChars: 10 }, 'chat-fork'), null);
|
||||
});
|
||||
|
||||
test('ToolOutputStore global quotas keep the fresh clone and its source handle over quota', async () => {
|
||||
const files = new Map<string, { record: PersistedToolOutputRecord; content: string }>();
|
||||
const deletedPaths: string[] = [];
|
||||
|
||||
@@ -707,6 +707,9 @@ export class ToolOutputStore {
|
||||
* quota — otherwise a source session whose quota is filled by borrowed
|
||||
* owners would leave a freshly stored handle as the only eviction
|
||||
* candidate, and `store()` would return an immediately dead handle id.
|
||||
* When such an owner *is* evicted by a per-session pass, `evictHandle`
|
||||
* first retires the cross-session aliases of its path so the fork never
|
||||
* keeps a handle registered against a deleted file.
|
||||
* `protect` marks handles that must not be chosen for eviction (e.g. the
|
||||
* records a fresh fork advertises, its clones and their source handles):
|
||||
* they are skipped while any unprotected candidate remains, so making room
|
||||
@@ -744,10 +747,38 @@ export class ToolOutputStore {
|
||||
private evictHandle(handle: ToolOutputHandle): void {
|
||||
handle.evicted = true;
|
||||
if (handle.filePath && !handle.borrowedFilePath && this.persistence) {
|
||||
this.retireBorrowAliases(handle);
|
||||
void this.persistence.delete(handle.filePath).catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove handles in *other* chat sessions that alias `owner.filePath` as a
|
||||
* non-owning borrow, before the owner's durable file is deleted. Eviction
|
||||
* runs per session (`enforceSessionLimits`, `pruneTerminalSession`,
|
||||
* `removeHandle`), so `pickEvictionEntry`'s borrow protection — which only
|
||||
* sees the handles of the eviction set — cannot shield an owner whose path
|
||||
* is aliased by a fork in another session. Deleting the owner's file would
|
||||
* otherwise leave the fork's alias registered against a path that no longer
|
||||
* exists, so a later `tool_output_read` in the fork fails and then drops the
|
||||
* alias. Retiring the alias (removing it from its session map; evicting an
|
||||
* alias never deletes a file) surfaces the loss immediately as a "not found"
|
||||
* read instead of a dangling handle, and frees the fork's quota slot.
|
||||
*/
|
||||
private retireBorrowAliases(owner: ToolOutputHandle): void {
|
||||
if (!owner.filePath || owner.borrowedFilePath) return;
|
||||
for (const [chatSessionId, sessionMap] of this.bySession) {
|
||||
if (chatSessionId === owner.chatSessionId) continue;
|
||||
for (const [handleId, alias] of sessionMap) {
|
||||
if (!alias.borrowedFilePath || alias.filePath !== owner.filePath) continue;
|
||||
sessionMap.delete(handleId);
|
||||
// An alias never owns the shared file, so this recursion stops here.
|
||||
this.evictHandle(alias);
|
||||
}
|
||||
if (sessionMap.size === 0) this.bySession.delete(chatSessionId);
|
||||
}
|
||||
}
|
||||
|
||||
private async restoreHandle(handleId: string, chatSessionId: string): Promise<ToolOutputHandle | undefined> {
|
||||
if (!this.persistence?.restore) return undefined;
|
||||
const pendingDeletion = this.sessionDeletionPromises.get(chatSessionId);
|
||||
|
||||
Reference in new issue
Block a user