mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-21 23:50:45 +00:00
fix: address Codex review on PR #3604
This commit is contained in:
1 parent
140b69a05e
commit
abb7d22a56
2 files changed
+58
-45
No files matched your search
@@ -222,12 +222,11 @@ test('ToolOutputStore global quotas keep the borrowed fork alias when every reco
|
||||
// The global handle quota is already full when the fork is created. Every
|
||||
// registry entry is protected by the rehome — the fork's retained messages
|
||||
// advertise the cloned handle (the alias), and the source record backs the
|
||||
// original conversation — so the protected pass cannot make room. The
|
||||
// overshoot is resolved during the rehome itself (not deferred to the next
|
||||
// plain `store()`, whose oldest-first sweep would kill both copies): the
|
||||
// borrowed alias owns no durable content, so it is the copy sacrificed,
|
||||
// and the source-owned record keeps the handle readable with the registry
|
||||
// back within quota.
|
||||
// original conversation — so no eviction can admit the clone without
|
||||
// destroying output a live conversation still resolves. The fork is
|
||||
// refused: the borrowed alias (which owns nothing) is rolled back, the
|
||||
// source-owned record keeps the handle readable for the original
|
||||
// conversation, and no spill file is deleted.
|
||||
const original = new ToolOutputStore({ spillThresholdChars: 0, maxHandlesGlobal: 1, persistence });
|
||||
const handle = original.store({
|
||||
chatSessionId: 'chat-source',
|
||||
@@ -236,17 +235,17 @@ test('ToolOutputStore global quotas keep the borrowed fork alias when every reco
|
||||
});
|
||||
await handle.spillPromise;
|
||||
|
||||
// `read` cannot serve the durable content, so the clone aliases the
|
||||
// `read` cannot serve the durable content, so the clone would alias the
|
||||
// source-owned spill path as a non-owning borrow.
|
||||
await original.rehomeChatSession('chat-source', 'chat-fork');
|
||||
|
||||
const sourceCopy = original.get(handle.id, 'chat-source');
|
||||
assert.ok(sourceCopy);
|
||||
assert.ok(files.has(`/netcatty/${handle.id}-chat-source.log`));
|
||||
// Evicting the alias never deletes the shared file.
|
||||
// Rolling the alias back never deletes the shared file.
|
||||
assert.equal(deletedPaths.length, 0);
|
||||
// The alias (which owns nothing) is the copy sacrificed; the fork's read
|
||||
// of this handle id now misses, but the original conversation keeps it.
|
||||
// The fork is refused: its read of this handle id misses, but the original
|
||||
// conversation keeps it.
|
||||
assert.equal(original.get(handle.id, 'chat-fork'), undefined);
|
||||
});
|
||||
|
||||
@@ -354,7 +353,7 @@ test('ToolOutputStore retires the borrowed fork alias before its owner is evicte
|
||||
assert.equal(await original.readChunkAsync({ handleId: handle.id, mode: 'head', maxChars: 10 }, 'chat-fork'), null);
|
||||
});
|
||||
|
||||
test('ToolOutputStore global quotas resolve an over-quota protected set during rehoming', async () => {
|
||||
test('ToolOutputStore global quotas refuse a fork whose copies cannot fit', async () => {
|
||||
const files = new Map<string, { record: PersistedToolOutputRecord; content: string }>();
|
||||
const deletedPaths: string[] = [];
|
||||
const persistence: ToolOutputPersistence = {
|
||||
@@ -391,13 +390,12 @@ test('ToolOutputStore global quotas resolve an over-quota protected set during r
|
||||
// the only unprotected entry is nothing — the freshly cloned handle and
|
||||
// its source are both protected, because the clone must survive quota
|
||||
// enforcement (the fork advertises its handle id) and the source record
|
||||
// backs the original conversation. The overshoot is resolved during the
|
||||
// rehome itself (not deferred to the next plain `store()`, whose
|
||||
// oldest-first sweep would kill both copies before the newly stored
|
||||
// handle): the follow-up unprotected pass sacrifices the older source
|
||||
// copy so only one copy of the handle id dies, and the clone — which
|
||||
// re-read the source's durable content — re-spills a durable
|
||||
// target-owned record the fork keeps reading.
|
||||
// backs the original conversation. No eviction can fit both copies, so
|
||||
// the fork is refused: the fresh clone (which re-read the source's
|
||||
// durable content but has not yet spilled it) is rolled back, and the
|
||||
// source-owned record — the one the original conversation still resolves —
|
||||
// keeps its durable file. The fork's read of the handle id misses instead
|
||||
// of invalidating either live copy.
|
||||
const original = new ToolOutputStore({ spillThresholdChars: 0, maxHandlesGlobal: 1, persistence });
|
||||
const handle = original.store({
|
||||
chatSessionId: 'chat-source',
|
||||
@@ -408,17 +406,18 @@ test('ToolOutputStore global quotas resolve an over-quota protected set during r
|
||||
|
||||
await original.rehomeChatSession('chat-source', 'chat-fork');
|
||||
|
||||
// The source copy is what got sacrificed to bring the registry within
|
||||
// quota; its source-owned spill file is gone with it.
|
||||
assert.equal(original.get(handle.id, 'chat-source'), undefined);
|
||||
assert.deepEqual(deletedPaths, [`/netcatty/${handle.id}-chat-source.log`]);
|
||||
assert.equal(files.has(`/netcatty/${handle.id}-chat-source.log`), false);
|
||||
// The clone survives and owns its own durable record.
|
||||
assert.ok(original.get(handle.id, 'chat-fork'));
|
||||
assert.ok(files.has(`/netcatty/${handle.id}-chat-fork.log`));
|
||||
// The source copy survives untouched with its durable spill file.
|
||||
const sourceCopy = original.get(handle.id, 'chat-source');
|
||||
assert.ok(sourceCopy);
|
||||
assert.deepEqual(deletedPaths, []);
|
||||
assert.ok(files.has(`/netcatty/${handle.id}-chat-source.log`));
|
||||
assert.equal((
|
||||
await original.readChunkAsync({ handleId: handle.id, mode: 'head', maxChars: 100 }, 'chat-fork')
|
||||
await original.readChunkAsync({ handleId: handle.id, mode: 'head', maxChars: 100 }, 'chat-source')
|
||||
)?.content?.length, 100);
|
||||
// The fork is refused: no clone, no target-owned record for it.
|
||||
assert.equal(original.get(handle.id, 'chat-fork'), undefined);
|
||||
assert.equal(files.has(`/netcatty/${handle.id}-chat-fork.log`), false);
|
||||
assert.equal(await original.readChunkAsync({ handleId: handle.id, mode: 'head', maxChars: 100 }, 'chat-fork'), null);
|
||||
});
|
||||
|
||||
test('ToolOutputStore global quotas evict an unrelated session\'s older handle, not the fresh clone', async () => {
|
||||
|
||||
@@ -443,24 +443,35 @@ export class ToolOutputStore {
|
||||
// still reads): when the quota is already full, the oldest unprotected
|
||||
// pre-existing handle (mirroring the plain `store()` eviction policy)
|
||||
// absorbs the overflow instead. When the protected set itself exceeds
|
||||
// the quota (e.g. a very small `maxHandlesGlobal`), the pass above
|
||||
// cannot finish the job; deferring the rebalance to the next plain
|
||||
// `store()` would let its oldest-first sweep — run without protection
|
||||
// and with the source and its clone sharing the same older `accessedAt`
|
||||
// — evict *both* copies before the newly stored handle, killing the
|
||||
// handle in the fork and the original conversation at once. So a
|
||||
// follow-up unprotected pass resolves the overshoot right here: the two
|
||||
// copies still coexist, the borrow protection keeps the durable owner
|
||||
// alive when a clone aliases its path, and at most one copy per handle
|
||||
// id is sacrificed (the pass breaks as soon as the quota is met, so the
|
||||
// twin the other session reads survives).
|
||||
this.enforceGlobalLimits(new Set([
|
||||
// the quota (e.g. a very small `maxHandlesGlobal`), no eviction can
|
||||
// admit the clones without destroying output a live conversation still
|
||||
// resolves: dropping one copy would either delete the source-owned file
|
||||
// the original conversation reads, or — when the clone is a borrowed
|
||||
// alias — leave the fork publishing a handle id that immediately
|
||||
// misses. Refuse the fork instead: roll back the fresh clones so the
|
||||
// registry returns to its pre-fork (compliant) state, keeping the
|
||||
// source record intact. The fork's retained messages then miss on
|
||||
// `tool_output_read`, but no existing conversation's output is
|
||||
// invalidated.
|
||||
const withinQuota = this.enforceGlobalLimits(new Set([
|
||||
...selected.map(([, handle]) => handle),
|
||||
...freshClones,
|
||||
]));
|
||||
// A no-op when the protected pass already brought the registry within
|
||||
// quota; otherwise trims it immediately as described above.
|
||||
this.enforceGlobalLimits();
|
||||
if (!withinQuota) {
|
||||
// The fresh clones own no durable files yet (their spills happen below),
|
||||
// so removing them deletes nothing and retires no borrow aliases.
|
||||
for (const clone of freshClones) {
|
||||
if (targetMap.get(clone.id) === clone) targetMap.delete(clone.id);
|
||||
}
|
||||
if (targetMap.size === 0) {
|
||||
this.bySession.delete(targetChatSessionId);
|
||||
} else {
|
||||
this.bySession.set(targetChatSessionId, targetMap);
|
||||
}
|
||||
// The registry the clones were added to was within quota, so rolling
|
||||
// them back is guaranteed to restore compliance; no further eviction
|
||||
// pass runs here.
|
||||
}
|
||||
// Copies still holding in-memory content (fresh or read-back) become
|
||||
// target-owned through the normal spill path, which re-writes a durable
|
||||
// record carrying the target's chat session id. Await the writes so the
|
||||
@@ -688,16 +699,19 @@ export class ToolOutputStore {
|
||||
}
|
||||
}
|
||||
|
||||
private enforceGlobalLimits(protect?: ReadonlySet<ToolOutputHandle>): void {
|
||||
private enforceGlobalLimits(protect?: ReadonlySet<ToolOutputHandle>): boolean {
|
||||
// Returns true when the loop ends with the registry within quota, and
|
||||
// false when every remaining entry is protected and the registry is still
|
||||
// over quota (the caller decides what to sacrifice, if anything).
|
||||
const allHandles = () => [...this.bySession.entries()].flatMap(([chatSessionId, sessionMap]) => (
|
||||
[...sessionMap.values()].map(handle => ({ chatSessionId, sessionMap, handle }))
|
||||
));
|
||||
while (true) {
|
||||
const entries = allHandles();
|
||||
const totalChars = entries.reduce((sum, entry) => sum + entry.handle.storedChars, 0);
|
||||
if (entries.length <= this.maxHandlesGlobal && totalChars <= this.maxCharsGlobal) break;
|
||||
if (entries.length <= this.maxHandlesGlobal && totalChars <= this.maxCharsGlobal) return true;
|
||||
const oldest = this.pickEvictionEntry(entries, protect);
|
||||
if (!oldest) break;
|
||||
if (!oldest) return false;
|
||||
oldest.sessionMap.delete(oldest.handle.id);
|
||||
this.evictHandle(oldest.handle);
|
||||
if (oldest.sessionMap.size === 0) this.bySession.delete(oldest.chatSessionId);
|
||||
|
||||
Reference in new issue
Block a user