Files
NetMesh/electron/bridges/mcpServerBridge/sessionOwnership.cjs
zhaolei 3c72efcb7f
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
[Init] Initial commit - NetMesh terminal manager
2026-09-13 18:24:01 +08:00

93 lines
2.6 KiB
JavaScript

"use strict";
function createSessionOwnershipRegistry() {
const ownedByScope = new Map();
const scopeGenerations = new Map();
function captureGeneration(chatSessionId) {
if (!chatSessionId) return null;
let generation = scopeGenerations.get(chatSessionId);
if (!generation) {
generation = { chatSessionId, revoked: false };
scopeGenerations.set(chatSessionId, generation);
}
return generation;
}
function register(chatSessionId, sessionId, expectedGeneration = null) {
if (!chatSessionId || !sessionId) return false;
if (expectedGeneration !== null) {
const currentGeneration = scopeGenerations.get(chatSessionId);
if (
expectedGeneration.revoked
|| expectedGeneration.chatSessionId !== chatSessionId
|| currentGeneration !== expectedGeneration
) {
return false;
}
}
const owned = ownedByScope.get(chatSessionId) || new Set();
owned.add(sessionId);
ownedByScope.set(chatSessionId, owned);
return true;
}
function validate(chatSessionId, sessionId) {
if (!chatSessionId) return { ok: false, error: "chatSessionId is required." };
if (!ownedByScope.get(chatSessionId)?.has(sessionId)) {
return {
ok: false,
error: `Session "${sessionId}" was not opened by the current AI scope.`,
};
}
return { ok: true };
}
function listOwned(chatSessionId) {
if (!chatSessionId) return [];
const owned = ownedByScope.get(chatSessionId);
return owned ? Array.from(owned) : [];
}
function forgetSession(sessionId) {
for (const [scopeId, owned] of ownedByScope) {
owned.delete(sessionId);
if (owned.size === 0) ownedByScope.delete(scopeId);
}
}
/**
* Drop retained ownership for a chat scope without revoking its host_open
* generation. Used when the renderer pushes an authoritative empty scope
* replace so a later non-empty sync cannot resurrect cleared sessions.
*/
function releaseScopeOwnership(chatSessionId) {
if (!chatSessionId) return;
ownedByScope.delete(chatSessionId);
}
function clearScope(chatSessionId) {
ownedByScope.delete(chatSessionId);
const generation = scopeGenerations.get(chatSessionId);
if (generation) generation.revoked = true;
scopeGenerations.delete(chatSessionId);
}
function getTrackedGenerationCountForTests() {
return scopeGenerations.size;
}
return {
captureGeneration,
register,
validate,
listOwned,
forgetSession,
releaseScopeOwnership,
clearScope,
getTrackedGenerationCountForTests,
};
}
module.exports = { createSessionOwnershipRegistry };