@agent-deck/backend 2.5.4 → 2.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.tsbuildinfo +1 -1
- package/dist/agent-run/records.d.ts +6 -0
- package/dist/agent-run/records.d.ts.map +1 -1
- package/dist/agent-run/records.js.map +1 -1
- package/dist/agent-run/service.d.ts +6 -0
- package/dist/agent-run/service.d.ts.map +1 -1
- package/dist/agent-run/service.js +1 -0
- package/dist/agent-run/service.js.map +1 -1
- package/dist/audit/store.d.ts +2 -2
- package/dist/audit/store.d.ts.map +1 -1
- package/dist/audit/store.js +3 -0
- package/dist/audit/store.js.map +1 -1
- package/dist/bootstrap/real-intent.js +1 -1
- package/dist/bootstrap/real-intent.js.map +1 -1
- package/dist/lib/deck-switch-approval-url.d.ts +16 -0
- package/dist/lib/deck-switch-approval-url.d.ts.map +1 -0
- package/dist/lib/deck-switch-approval-url.js +59 -0
- package/dist/lib/deck-switch-approval-url.js.map +1 -0
- package/dist/lib/mcp-connection-error.d.ts +14 -0
- package/dist/lib/mcp-connection-error.d.ts.map +1 -1
- package/dist/lib/mcp-connection-error.js +36 -0
- package/dist/lib/mcp-connection-error.js.map +1 -1
- package/dist/mcp-server.d.ts +73 -0
- package/dist/mcp-server.d.ts.map +1 -1
- package/dist/mcp-server.js +443 -95
- package/dist/mcp-server.js.map +1 -1
- package/dist/mcp-session-binding.d.ts +29 -0
- package/dist/mcp-session-binding.d.ts.map +1 -1
- package/dist/mcp-session-binding.js +48 -0
- package/dist/mcp-session-binding.js.map +1 -1
- package/dist/mcp-tools/elicitation.d.ts +105 -7
- package/dist/mcp-tools/elicitation.d.ts.map +1 -1
- package/dist/mcp-tools/elicitation.js +291 -48
- package/dist/mcp-tools/elicitation.js.map +1 -1
- package/dist/mcp-tools/register.d.ts +14 -2
- package/dist/mcp-tools/register.d.ts.map +1 -1
- package/dist/mcp-tools/register.js +29 -2
- package/dist/mcp-tools/register.js.map +1 -1
- package/dist/models/database.d.ts.map +1 -1
- package/dist/models/database.js +13 -2
- package/dist/models/database.js.map +1 -1
- package/dist/playbooks/apply-patch-ops.js +1 -1
- package/dist/playbooks/apply-patch-ops.js.map +1 -1
- package/dist/routes/agent-runs.d.ts.map +1 -1
- package/dist/routes/agent-runs.js +19 -4
- package/dist/routes/agent-runs.js.map +1 -1
- package/dist/routes/decks.d.ts.map +1 -1
- package/dist/routes/decks.js.map +1 -1
- package/dist/routes/local-mcp.d.ts.map +1 -1
- package/dist/routes/local-mcp.js.map +1 -1
- package/dist/routes/mcp.d.ts.map +1 -1
- package/dist/routes/mcp.js +1 -1
- package/dist/routes/mcp.js.map +1 -1
- package/dist/routes/oauth.d.ts.map +1 -1
- package/dist/routes/oauth.js.map +1 -1
- package/dist/routes/services.d.ts.map +1 -1
- package/dist/routes/services.js.map +1 -1
- package/dist/routes/trusted-session.d.ts.map +1 -1
- package/dist/routes/trusted-session.js +125 -9
- package/dist/routes/trusted-session.js.map +1 -1
- package/dist/routes/websocket.d.ts.map +1 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/index.js.map +1 -1
- package/dist/services/local-mcp-server-manager.d.ts.map +1 -1
- package/dist/services/mcp-client-manager.d.ts +19 -1
- package/dist/services/mcp-client-manager.d.ts.map +1 -1
- package/dist/services/mcp-client-manager.js +69 -13
- package/dist/services/mcp-client-manager.js.map +1 -1
- package/dist/services/oauth-manager.d.ts +6 -0
- package/dist/services/oauth-manager.d.ts.map +1 -1
- package/dist/services/oauth-manager.js +31 -1
- package/dist/services/oauth-manager.js.map +1 -1
- package/dist/services/service-manager.d.ts.map +1 -1
- package/dist/services/service-manager.js +6 -1
- package/dist/services/service-manager.js.map +1 -1
- package/dist/test-local-mcp.js.map +1 -1
- package/dist/trusted-session/store.d.ts +73 -0
- package/dist/trusted-session/store.d.ts.map +1 -1
- package/dist/trusted-session/store.js +196 -0
- package/dist/trusted-session/store.js.map +1 -1
- package/dist/vault/oauth-token-vault.d.ts +13 -0
- package/dist/vault/oauth-token-vault.d.ts.map +1 -1
- package/dist/vault/oauth-token-vault.js +21 -1
- package/dist/vault/oauth-token-vault.js.map +1 -1
- package/package.json +11 -3
- package/static-ui/assets/{index-B5GT9yb2.js → index-RGHMSp5I.js} +1 -1
- package/static-ui/index.html +1 -1
package/dist/mcp-server.js
CHANGED
|
@@ -122,6 +122,44 @@ function hasBridgeLivenessHeader(req) {
|
|
|
122
122
|
const raw = req.headers[shared_1.AGENT_DECK_BRIDGE_LIVENESS_HEADER];
|
|
123
123
|
return typeof raw === 'string' && raw.trim() === shared_1.AGENT_DECK_BRIDGE_LIVENESS_V1;
|
|
124
124
|
}
|
|
125
|
+
/** NOT-634: `clientInfo.name` from an initialize body, for recovery logs only. */
|
|
126
|
+
function readInitializeClientName(body) {
|
|
127
|
+
const messages = Array.isArray(body) ? body : [body];
|
|
128
|
+
for (const message of messages) {
|
|
129
|
+
if (!message || typeof message !== 'object' || !(0, types_js_1.isInitializeRequest)(message)) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
const name = message.params.clientInfo?.name;
|
|
133
|
+
if (typeof name === 'string' && name.trim()) {
|
|
134
|
+
return name.trim().slice(0, 80);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* NOT-634: the approved session deck of a presented pre-restart session id
|
|
141
|
+
* was deleted. Answered explicitly — never a silent header / folder pin /
|
|
142
|
+
* default-deck fallback.
|
|
143
|
+
*/
|
|
144
|
+
class SessionDeckMissingError extends Error {
|
|
145
|
+
constructor(message) {
|
|
146
|
+
super(message);
|
|
147
|
+
this.name = 'SessionDeckMissingError';
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* NOT-634: a grant client presented its pre-restart session id, but its
|
|
152
|
+
* grant (now revoked / expired) or allowlist no longer permits the approved
|
|
153
|
+
* session deck. Answered as the uniform 401 / 403 — never a fallback bind.
|
|
154
|
+
*/
|
|
155
|
+
class SessionRecoveryDeniedError extends Error {
|
|
156
|
+
reason;
|
|
157
|
+
constructor(reason) {
|
|
158
|
+
super(reason);
|
|
159
|
+
this.reason = reason;
|
|
160
|
+
this.name = 'SessionRecoveryDeniedError';
|
|
161
|
+
}
|
|
162
|
+
}
|
|
125
163
|
/**
|
|
126
164
|
* NOT-318: hosted-mode flag. When `AGENT_DECK_MCP_REQUIRE_BEARER=1`, every
|
|
127
165
|
* `/mcp` request requires a valid per-agent grant secret, regardless of
|
|
@@ -234,6 +272,9 @@ class AgentDeckMCPServer {
|
|
|
234
272
|
unregisterTimeoutMsValue;
|
|
235
273
|
/** NOT-191: keep-alive override (tests); otherwise the env resolution wins. */
|
|
236
274
|
liveTouchKeepAliveMsOverride;
|
|
275
|
+
/** NOT-638: switch_deck elicitation timers (tests); production uses the defaults. */
|
|
276
|
+
deckSwitchToolCallWaitMs;
|
|
277
|
+
deckSwitchFormLifetimeMs;
|
|
237
278
|
/**
|
|
238
279
|
* NOT-191: last real client traffic per session — tool calls, handshakes,
|
|
239
280
|
* stream connects, heartbeat pulses. Server-generated keep-alive touches
|
|
@@ -276,6 +317,11 @@ class AgentDeckMCPServer {
|
|
|
276
317
|
* deck-resolving headers, the alias keeps serving later requests on that
|
|
277
318
|
* id. Bounded with the same oldest-first discipline as the stale tally
|
|
278
319
|
* and the closed set — diagnostics/routing, not a ledger.
|
|
320
|
+
*
|
|
321
|
+
* NOT-634: stale id → (principal → live id), never the presented id
|
|
322
|
+
* alone — see {@link recoveryAliasOwner}. A bearer-free launch recovery
|
|
323
|
+
* and a grant recovery of the same stale id are separate aliases, so one
|
|
324
|
+
* principal's recovered session is never served to another.
|
|
279
325
|
*/
|
|
280
326
|
recoveredSessionAlias = new Map();
|
|
281
327
|
/**
|
|
@@ -318,6 +364,8 @@ class AgentDeckMCPServer {
|
|
|
318
364
|
this.unregisterTimeoutMsValue =
|
|
319
365
|
options?.unregisterTimeoutMs ?? AgentDeckMCPServer.unregisterTimeoutMs();
|
|
320
366
|
this.liveTouchKeepAliveMsOverride = options?.liveTouchKeepAliveMs;
|
|
367
|
+
this.deckSwitchToolCallWaitMs = options?.deckSwitchToolCallWaitMs;
|
|
368
|
+
this.deckSwitchFormLifetimeMs = options?.deckSwitchFormLifetimeMs;
|
|
321
369
|
this.app = (0, express_1.default)();
|
|
322
370
|
this.app.use(process.env.AGENT_DECK_HOSTED_MODE === '1'
|
|
323
371
|
? express_1.default.json({ limit: 1024 * 1024 })
|
|
@@ -727,8 +775,13 @@ class AgentDeckMCPServer {
|
|
|
727
775
|
// NOT-569: drop recovery aliases pointing at the cleaned session, so a
|
|
728
776
|
// later request on a stale id re-recovers from headers instead of
|
|
729
777
|
// resolving to a dead session.
|
|
730
|
-
for (const [staleId,
|
|
731
|
-
|
|
778
|
+
for (const [staleId, byOwner] of this.recoveredSessionAlias) {
|
|
779
|
+
for (const [owner, liveId] of byOwner) {
|
|
780
|
+
if (liveId === sessionId) {
|
|
781
|
+
byOwner.delete(owner);
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
if (byOwner.size === 0) {
|
|
732
785
|
this.recoveredSessionAlias.delete(staleId);
|
|
733
786
|
}
|
|
734
787
|
}
|
|
@@ -989,22 +1042,17 @@ class AgentDeckMCPServer {
|
|
|
989
1042
|
// detection reads the capabilities the client reported at initialize;
|
|
990
1043
|
// elicitation itself goes through the session server so the host UI
|
|
991
1044
|
// answers. Timeouts/errors degrade to the browser fallback downstream.
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
}
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
})
|
|
1002
|
-
|
|
1003
|
-
action: result.action,
|
|
1004
|
-
...(result.content ? { content: { ...result.content } } : {}),
|
|
1005
|
-
};
|
|
1006
|
-
},
|
|
1007
|
-
},
|
|
1045
|
+
// NOT-638: switch_deck passes the form lifetime (time left until the
|
|
1046
|
+
// request expires) as the SDK timeout plus an AbortSignal, so the form
|
|
1047
|
+
// outlives the 5-minute tool-call wait and a late answer still applies.
|
|
1048
|
+
elicitation: (0, elicitation_1.createSdkElicitationProvider)(elicitingServer.server, {
|
|
1049
|
+
...(this.deckSwitchToolCallWaitMs !== undefined
|
|
1050
|
+
? { toolCallWaitMs: this.deckSwitchToolCallWaitMs }
|
|
1051
|
+
: {}),
|
|
1052
|
+
...(this.deckSwitchFormLifetimeMs !== undefined
|
|
1053
|
+
? { formLifetimeMs: this.deckSwitchFormLifetimeMs }
|
|
1054
|
+
: {}),
|
|
1055
|
+
}),
|
|
1008
1056
|
});
|
|
1009
1057
|
}
|
|
1010
1058
|
/**
|
|
@@ -1436,7 +1484,11 @@ class AgentDeckMCPServer {
|
|
|
1436
1484
|
* keeps its normal idle-TTL retention, and a later request on the evicted
|
|
1437
1485
|
* stale id simply recovers again.
|
|
1438
1486
|
*/
|
|
1439
|
-
rememberRecoveryAlias(staleSessionId, liveSessionId) {
|
|
1487
|
+
rememberRecoveryAlias(req, staleSessionId, liveSessionId) {
|
|
1488
|
+
const owner = this.recoveryAliasOwner(req);
|
|
1489
|
+
if (!owner) {
|
|
1490
|
+
return;
|
|
1491
|
+
}
|
|
1440
1492
|
if (!this.recoveredSessionAlias.has(staleSessionId) &&
|
|
1441
1493
|
this.recoveredSessionAlias.size >= AgentDeckMCPServer.STALE_SESSION_SAMPLE_LIMIT) {
|
|
1442
1494
|
const oldest = this.recoveredSessionAlias.keys().next().value;
|
|
@@ -1444,7 +1496,44 @@ class AgentDeckMCPServer {
|
|
|
1444
1496
|
this.recoveredSessionAlias.delete(oldest);
|
|
1445
1497
|
}
|
|
1446
1498
|
}
|
|
1447
|
-
this.recoveredSessionAlias.
|
|
1499
|
+
const byOwner = this.recoveredSessionAlias.get(staleSessionId) ?? new Map();
|
|
1500
|
+
byOwner.set(owner, liveSessionId);
|
|
1501
|
+
this.recoveredSessionAlias.set(staleSessionId, byOwner);
|
|
1502
|
+
}
|
|
1503
|
+
/**
|
|
1504
|
+
* NOT-634: the principal a request presents, as an alias owner key —
|
|
1505
|
+
* `grant:<id>` for a bearer that authenticates right now, `local` for a
|
|
1506
|
+
* bearer-free request where bearer-free access is allowed, and null
|
|
1507
|
+
* otherwise (invalid/revoked bearer, or hosted mode without one), which
|
|
1508
|
+
* matches no alias at all.
|
|
1509
|
+
*/
|
|
1510
|
+
recoveryAliasOwner(req) {
|
|
1511
|
+
const bearer = readBearerToken(req);
|
|
1512
|
+
if (bearer) {
|
|
1513
|
+
const principal = this.authenticateGrant(bearer);
|
|
1514
|
+
return principal ? `grant:${principal.grantId}` : null;
|
|
1515
|
+
}
|
|
1516
|
+
return isBearerGrantRequired() ? null : 'local';
|
|
1517
|
+
}
|
|
1518
|
+
/**
|
|
1519
|
+
* NOT-634: the live session this request's own principal recovered for
|
|
1520
|
+
* `staleSessionId`. A live alias owned by another principal is invisible;
|
|
1521
|
+
* a dead alias of this principal is dropped so the caller re-recovers.
|
|
1522
|
+
*/
|
|
1523
|
+
lookupRecoveryAlias(req, staleSessionId) {
|
|
1524
|
+
const owner = this.recoveryAliasOwner(req);
|
|
1525
|
+
const byOwner = owner ? this.recoveredSessionAlias.get(staleSessionId) : undefined;
|
|
1526
|
+
const aliased = owner ? byOwner?.get(owner) : undefined;
|
|
1527
|
+
if (aliased && this.sessions.has(aliased)) {
|
|
1528
|
+
return aliased;
|
|
1529
|
+
}
|
|
1530
|
+
if (aliased && owner && byOwner) {
|
|
1531
|
+
byOwner.delete(owner);
|
|
1532
|
+
if (byOwner.size === 0) {
|
|
1533
|
+
this.recoveredSessionAlias.delete(staleSessionId);
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
return undefined;
|
|
1448
1537
|
}
|
|
1449
1538
|
/**
|
|
1450
1539
|
* NOT-569: count a stale id recovered from headers as recovered in the
|
|
@@ -1479,6 +1568,11 @@ class AgentDeckMCPServer {
|
|
|
1479
1568
|
workspaceRoot: binding.workspaceRoot,
|
|
1480
1569
|
mode,
|
|
1481
1570
|
});
|
|
1571
|
+
// NOT-634: whether the session sits on its approved session deck
|
|
1572
|
+
// (reported as `active_source: "session"`). Display only.
|
|
1573
|
+
if (typeof data?.sessionOverride === 'boolean') {
|
|
1574
|
+
this.sessionBinding.setApprovedSessionOverride(sessionId, data.sessionOverride);
|
|
1575
|
+
}
|
|
1482
1576
|
return { mode, deckId };
|
|
1483
1577
|
}
|
|
1484
1578
|
sendDeckRequired(res) {
|
|
@@ -1521,7 +1615,13 @@ class AgentDeckMCPServer {
|
|
|
1521
1615
|
* `resolveGrantDeck`, and the owning grant id is linked on the runtime
|
|
1522
1616
|
* session so deck-switch creation/approval enforces the same allowlist.
|
|
1523
1617
|
*/
|
|
1524
|
-
async authenticateGrantDeck(sessionId, req, principal, deckId
|
|
1618
|
+
async authenticateGrantDeck(sessionId, req, principal, deckId,
|
|
1619
|
+
/**
|
|
1620
|
+
* NOT-634: the pre-restart session id the client presented. The backend
|
|
1621
|
+
* aliases the fresh id to its lineage only when that lineage belongs to
|
|
1622
|
+
* this same grant.
|
|
1623
|
+
*/
|
|
1624
|
+
recoverFromMcpSessionId) {
|
|
1525
1625
|
const response = await fetch(`${this.backendUrl}/api/trusted-session/mcp/connect-deck`, {
|
|
1526
1626
|
method: 'POST',
|
|
1527
1627
|
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
|
|
@@ -1529,12 +1629,20 @@ class AgentDeckMCPServer {
|
|
|
1529
1629
|
deckId,
|
|
1530
1630
|
mcpSessionId: sessionId,
|
|
1531
1631
|
grantId: principal.grantId,
|
|
1632
|
+
...(recoverFromMcpSessionId ? { recoverFromMcpSessionId } : {}),
|
|
1532
1633
|
}),
|
|
1533
1634
|
});
|
|
1534
1635
|
const body = (await response.json());
|
|
1535
1636
|
if (!response.ok || !body.success || !body.data) {
|
|
1536
1637
|
throw new Error(body.error ?? 'GRANT_DECK_INVALID');
|
|
1537
1638
|
}
|
|
1639
|
+
this.adoptGrantConnect(sessionId, req, principal, body.data);
|
|
1640
|
+
}
|
|
1641
|
+
/**
|
|
1642
|
+
* Record a successful grant connect on the MCP session binding (shared by
|
|
1643
|
+
* `authenticateGrantDeck` and NOT-634 override recovery).
|
|
1644
|
+
*/
|
|
1645
|
+
adoptGrantConnect(sessionId, req, principal, data) {
|
|
1538
1646
|
const existing = this.sessionBinding.getBinding(sessionId);
|
|
1539
1647
|
const workspaceRoot = readWorkspaceRootHeader(req) ??
|
|
1540
1648
|
existing.workspaceRoot ??
|
|
@@ -1544,10 +1652,10 @@ class AgentDeckMCPServer {
|
|
|
1544
1652
|
label: principal.label,
|
|
1545
1653
|
defaultDeck: principal.defaultDeck,
|
|
1546
1654
|
allowedDecks: principal.allowedDecks,
|
|
1547
|
-
runtimeSessionId:
|
|
1548
|
-
deckId:
|
|
1655
|
+
runtimeSessionId: data.sessionId,
|
|
1656
|
+
deckId: data.deckId,
|
|
1549
1657
|
workspaceRoot,
|
|
1550
|
-
mode:
|
|
1658
|
+
mode: data.mode,
|
|
1551
1659
|
});
|
|
1552
1660
|
const correlationId = readCorrelationIdHeader(req);
|
|
1553
1661
|
if (correlationId) {
|
|
@@ -1632,7 +1740,13 @@ class AgentDeckMCPServer {
|
|
|
1632
1740
|
* the deck header — the folder assignment for workspace-header-only hosts.
|
|
1633
1741
|
* Omitted callers behave exactly as before (header or GRANT_REQUIRED).
|
|
1634
1742
|
*/
|
|
1635
|
-
async authenticateLaunchDeck(sessionId, req, deckIdOverride
|
|
1743
|
+
async authenticateLaunchDeck(sessionId, req, deckIdOverride,
|
|
1744
|
+
/**
|
|
1745
|
+
* NOT-634: the pre-restart session id the client presented. The fresh
|
|
1746
|
+
* session is aliased to its lineage so a later restart still finds an
|
|
1747
|
+
* approved session deck approved on this fresh session.
|
|
1748
|
+
*/
|
|
1749
|
+
recoverFromMcpSessionId) {
|
|
1636
1750
|
const deckId = deckIdOverride ?? readLaunchDeckHeader(req);
|
|
1637
1751
|
if (!deckId) {
|
|
1638
1752
|
throw new Error('GRANT_REQUIRED');
|
|
@@ -1667,20 +1781,128 @@ class AgentDeckMCPServer {
|
|
|
1667
1781
|
deckId,
|
|
1668
1782
|
mcpSessionId: sessionId,
|
|
1669
1783
|
...(executionWorkspace ? { executionWorkspace } : {}),
|
|
1784
|
+
...(recoverFromMcpSessionId ? { recoverFromMcpSessionId } : {}),
|
|
1785
|
+
}),
|
|
1786
|
+
});
|
|
1787
|
+
const body = (await response.json());
|
|
1788
|
+
if (!response.ok || !body.success || !body.data) {
|
|
1789
|
+
if (body.error_code === 'SESSION_DECK_MISSING') {
|
|
1790
|
+
throw new SessionDeckMissingError(body.error ?? 'SESSION_DECK_MISSING');
|
|
1791
|
+
}
|
|
1792
|
+
throw new Error(body.error ?? 'LAUNCH_DECK_INVALID');
|
|
1793
|
+
}
|
|
1794
|
+
this.adoptLaunchConnect(sessionId, req, body.data, executionWorkspace);
|
|
1795
|
+
if (body.data.sessionOverride) {
|
|
1796
|
+
this.sessionBinding.markSessionOverrideRecovery(sessionId);
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
/**
|
|
1800
|
+
* NOT-634: bind a fresh session to the approved session deck of the
|
|
1801
|
+
* pre-restart id the client presented, before any header / folder pin /
|
|
1802
|
+
* default-deck resolution. `principal` is the already-authenticated
|
|
1803
|
+
* bearer grant (omitted for local bearer-free callers); the backend only
|
|
1804
|
+
* restores a lineage owned by that same principal and re-checks the
|
|
1805
|
+
* grant's current state and allowlist. Returns the recovered deck id, or
|
|
1806
|
+
* undefined when that id has no approved session deck (the caller
|
|
1807
|
+
* resolves as before). Throws {@link SessionDeckMissingError} when the
|
|
1808
|
+
* approved deck was deleted and {@link SessionRecoveryDeniedError} when
|
|
1809
|
+
* the grant no longer permits it — the caller answers both explicitly
|
|
1810
|
+
* instead of falling back.
|
|
1811
|
+
*/
|
|
1812
|
+
async bindRecoveredSessionOverride(sessionId, req, presentedSessionId, principal) {
|
|
1813
|
+
const executionWorkspace = principal ? undefined : this.readLaunchExecutionWorkspace(req);
|
|
1814
|
+
const response = await fetch(`${this.backendUrl}/api/trusted-session/mcp/connect-deck`, {
|
|
1815
|
+
method: 'POST',
|
|
1816
|
+
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
|
|
1817
|
+
body: JSON.stringify({
|
|
1818
|
+
mcpSessionId: sessionId,
|
|
1819
|
+
recoverFromMcpSessionId: presentedSessionId,
|
|
1820
|
+
...(principal ? { grantId: principal.grantId } : {}),
|
|
1821
|
+
...(executionWorkspace ? { executionWorkspace } : {}),
|
|
1670
1822
|
}),
|
|
1671
1823
|
});
|
|
1672
1824
|
const body = (await response.json());
|
|
1673
1825
|
if (!response.ok || !body.success || !body.data) {
|
|
1826
|
+
if (body.error_code === 'SESSION_DECK_MISSING') {
|
|
1827
|
+
throw new SessionDeckMissingError(body.error ?? 'SESSION_DECK_MISSING');
|
|
1828
|
+
}
|
|
1829
|
+
if (body.error_code === 'NO_SESSION_OVERRIDE') {
|
|
1830
|
+
return undefined;
|
|
1831
|
+
}
|
|
1832
|
+
if (body.error_code === 'RESOURCE_OUT_OF_SCOPE') {
|
|
1833
|
+
throw new SessionRecoveryDeniedError('out-of-scope');
|
|
1834
|
+
}
|
|
1835
|
+
if (body.error_code === 'GRANT_REQUIRED' && principal) {
|
|
1836
|
+
throw new SessionRecoveryDeniedError('grant-required');
|
|
1837
|
+
}
|
|
1674
1838
|
throw new Error(body.error ?? 'LAUNCH_DECK_INVALID');
|
|
1675
1839
|
}
|
|
1840
|
+
if (!body.data.sessionOverride) {
|
|
1841
|
+
return undefined;
|
|
1842
|
+
}
|
|
1843
|
+
if (principal) {
|
|
1844
|
+
// Defense in depth: the recovered deck must sit inside the grant the
|
|
1845
|
+
// bearer authenticated to right now.
|
|
1846
|
+
if (!(0, client_grants_1.principalAllowsDeck)(principal, body.data.deckId)) {
|
|
1847
|
+
await this.disconnectTrustedSession(sessionId, req).catch(() => { });
|
|
1848
|
+
throw new SessionRecoveryDeniedError('out-of-scope');
|
|
1849
|
+
}
|
|
1850
|
+
this.adoptGrantConnect(sessionId, req, principal, body.data);
|
|
1851
|
+
this.sessionBinding.setApprovedSessionOverride(sessionId, true);
|
|
1852
|
+
return body.data.deckId;
|
|
1853
|
+
}
|
|
1854
|
+
this.adoptLaunchConnect(sessionId, req, body.data, executionWorkspace);
|
|
1855
|
+
this.sessionBinding.markSessionOverrideRecovery(sessionId);
|
|
1856
|
+
return body.data.deckId;
|
|
1857
|
+
}
|
|
1858
|
+
/** NOT-634: answer a recovered id whose approved session deck was deleted. */
|
|
1859
|
+
sendSessionDeckMissing(res, message) {
|
|
1860
|
+
res.status(409).json({
|
|
1861
|
+
jsonrpc: '2.0',
|
|
1862
|
+
error: { code: -32001, message: `SESSION_DECK_MISSING: ${message}` },
|
|
1863
|
+
id: null,
|
|
1864
|
+
});
|
|
1865
|
+
}
|
|
1866
|
+
/**
|
|
1867
|
+
* NOT-634: answer a grant-session recovery that must not fall back
|
|
1868
|
+
* (deleted approved deck, revoked/expired grant, deck outside the
|
|
1869
|
+
* allowlist). Returns false for any other error so the caller resolves as
|
|
1870
|
+
* if no override existed.
|
|
1871
|
+
*/
|
|
1872
|
+
answerRecoveryFailure(res, error, presentedSessionId, body, grantId) {
|
|
1873
|
+
if (error instanceof SessionDeckMissingError) {
|
|
1874
|
+
console.warn(`[agent-deck] MCP request presented pre-restart session ${presentedSessionId} ` +
|
|
1875
|
+
`(client ${readInitializeClientName(body) ?? 'unknown'}, grantId=${grantId}); ` +
|
|
1876
|
+
'its approved session deck is gone:', error.message);
|
|
1877
|
+
this.sendSessionDeckMissing(res, error.message);
|
|
1878
|
+
return true;
|
|
1879
|
+
}
|
|
1880
|
+
if (error instanceof SessionRecoveryDeniedError) {
|
|
1881
|
+
console.warn(`[agent-deck] Grant session recovery denied (no session created): grantId=${grantId}`, error.reason);
|
|
1882
|
+
if (error.reason === 'out-of-scope') {
|
|
1883
|
+
this.sendOutOfScope(res);
|
|
1884
|
+
}
|
|
1885
|
+
else {
|
|
1886
|
+
this.sendGrantRequired(res);
|
|
1887
|
+
}
|
|
1888
|
+
return true;
|
|
1889
|
+
}
|
|
1890
|
+
return false;
|
|
1891
|
+
}
|
|
1892
|
+
/**
|
|
1893
|
+
* Record a successful launch connect on the MCP session binding (shared by
|
|
1894
|
+
* `authenticateLaunchDeck` and NOT-634 override recovery).
|
|
1895
|
+
*/
|
|
1896
|
+
adoptLaunchConnect(sessionId, req, data, executionWorkspace) {
|
|
1897
|
+
const existing = this.sessionBinding.getBinding(sessionId);
|
|
1676
1898
|
const workspaceRoot = readWorkspaceRootHeader(req) ??
|
|
1677
1899
|
existing.workspaceRoot ??
|
|
1678
1900
|
(process.env.AGENT_DECK_WORKSPACE?.trim() || undefined);
|
|
1679
1901
|
this.sessionBinding.setLaunchSession(sessionId, {
|
|
1680
|
-
runtimeSessionId:
|
|
1681
|
-
deckId:
|
|
1902
|
+
runtimeSessionId: data.sessionId,
|
|
1903
|
+
deckId: data.deckId,
|
|
1682
1904
|
workspaceRoot,
|
|
1683
|
-
mode:
|
|
1905
|
+
mode: data.mode,
|
|
1684
1906
|
});
|
|
1685
1907
|
// Freezes presence and absence alike: a launch without workspace metadata
|
|
1686
1908
|
// keeps returning WORKSPACE_REQUIRED for the session lifetime.
|
|
@@ -1778,7 +2000,12 @@ class AgentDeckMCPServer {
|
|
|
1778
2000
|
const defaultDeckFollowUp = !launchDeck &&
|
|
1779
2001
|
this.sessionBinding.isLaunchSession(sessionId) &&
|
|
1780
2002
|
this.sessionBinding.isDefaultDeckSession(sessionId);
|
|
1781
|
-
|
|
2003
|
+
// NOT-634: sessions bound by recovering an approved session deck carry
|
|
2004
|
+
// no deck header for it either — same launch follow-up refresh.
|
|
2005
|
+
const sessionOverrideFollowUp = !launchDeck &&
|
|
2006
|
+
this.sessionBinding.isLaunchSession(sessionId) &&
|
|
2007
|
+
this.sessionBinding.isSessionOverrideRecovery(sessionId);
|
|
2008
|
+
if (launchDeck || useManifestFollowUp || defaultDeckFollowUp || sessionOverrideFollowUp) {
|
|
1782
2009
|
try {
|
|
1783
2010
|
await this.authenticateLaunchDeck(sessionId, req, launchDeck ?? this.sessionBinding.getBinding(sessionId).deckId);
|
|
1784
2011
|
return true;
|
|
@@ -1852,7 +2079,9 @@ class AgentDeckMCPServer {
|
|
|
1852
2079
|
* session to the grant-constrained deck (absent header → default;
|
|
1853
2080
|
* out-of-grant header → 403, no session).
|
|
1854
2081
|
*/
|
|
1855
|
-
async handleGrantInitialize(req, res, bearer
|
|
2082
|
+
async handleGrantInitialize(req, res, bearer,
|
|
2083
|
+
/** NOT-634: the pre-restart session id the initialize presented, if any. */
|
|
2084
|
+
presentedSessionId) {
|
|
1856
2085
|
const principal = this.authenticateGrant(bearer);
|
|
1857
2086
|
if (!principal) {
|
|
1858
2087
|
const presented = (0, client_grants_1.parseGrantToken)(bearer);
|
|
@@ -1860,6 +2089,38 @@ class AgentDeckMCPServer {
|
|
|
1860
2089
|
this.sendGrantRequired(res);
|
|
1861
2090
|
return;
|
|
1862
2091
|
}
|
|
2092
|
+
// NOT-634: only after the bearer authenticated, a presented pre-restart
|
|
2093
|
+
// id owned by this same grant recovers its approved session deck ahead
|
|
2094
|
+
// of the deck header / grant default. The backend re-checks the grant's
|
|
2095
|
+
// current state and allowlist; denials are answered, never fallen back.
|
|
2096
|
+
if (presentedSessionId) {
|
|
2097
|
+
const recoveredSessionId = (0, node_crypto_1.randomUUID)();
|
|
2098
|
+
let recoveredDeck;
|
|
2099
|
+
try {
|
|
2100
|
+
recoveredDeck = await this.bindRecoveredSessionOverride(recoveredSessionId, req, presentedSessionId, principal);
|
|
2101
|
+
}
|
|
2102
|
+
catch (error) {
|
|
2103
|
+
this.sessionBinding.clearSession(recoveredSessionId);
|
|
2104
|
+
if (this.answerRecoveryFailure(res, error, presentedSessionId, req.body, principal.grantId)) {
|
|
2105
|
+
return;
|
|
2106
|
+
}
|
|
2107
|
+
// Any other failure: resolve as an initialize without an override.
|
|
2108
|
+
}
|
|
2109
|
+
if (recoveredDeck) {
|
|
2110
|
+
console.warn(`[agent-deck] MCP initialize presented pre-restart session ${presentedSessionId} ` +
|
|
2111
|
+
`(client ${readInitializeClientName(req.body) ?? 'unknown'}, grantId=${principal.grantId}); ` +
|
|
2112
|
+
`recovered approved session deck ${recoveredDeck} as ${recoveredSessionId}.`);
|
|
2113
|
+
this.getAuditStore()?.append({
|
|
2114
|
+
actor: principal.grantId,
|
|
2115
|
+
event: 'grant.used',
|
|
2116
|
+
targetId: (0, store_1.auditDeckTarget)(recoveredDeck),
|
|
2117
|
+
outcome: 'succeeded',
|
|
2118
|
+
reasonCode: null,
|
|
2119
|
+
});
|
|
2120
|
+
await this.establishTransportSession(req, res, recoveredSessionId);
|
|
2121
|
+
return;
|
|
2122
|
+
}
|
|
2123
|
+
}
|
|
1863
2124
|
const deckId = (0, client_grants_1.resolveGrantDeck)(principal, readLaunchDeckHeader(req));
|
|
1864
2125
|
if (!deckId) {
|
|
1865
2126
|
this.getAuditStore()?.append({
|
|
@@ -1875,7 +2136,7 @@ class AgentDeckMCPServer {
|
|
|
1875
2136
|
}
|
|
1876
2137
|
const sessionId = (0, node_crypto_1.randomUUID)();
|
|
1877
2138
|
try {
|
|
1878
|
-
await this.authenticateGrantDeck(sessionId, req, principal, deckId);
|
|
2139
|
+
await this.authenticateGrantDeck(sessionId, req, principal, deckId, presentedSessionId);
|
|
1879
2140
|
}
|
|
1880
2141
|
catch (error) {
|
|
1881
2142
|
console.warn('[agent-deck] Grant deck bind failed before MCP init (no session created):', `grantId=${principal.grantId}`, error instanceof Error ? error.message : error);
|
|
@@ -2020,14 +2281,12 @@ class AgentDeckMCPServer {
|
|
|
2020
2281
|
return;
|
|
2021
2282
|
}
|
|
2022
2283
|
if (sessionIdHeader) {
|
|
2023
|
-
|
|
2024
|
-
|
|
2284
|
+
// NOT-634: only an alias the request's own principal installed.
|
|
2285
|
+
const aliased = this.lookupRecoveryAlias(req, sessionIdHeader);
|
|
2286
|
+
if (aliased) {
|
|
2025
2287
|
await this.serveFollowUp(aliased, req, res, sessionIdHeader);
|
|
2026
2288
|
return;
|
|
2027
2289
|
}
|
|
2028
|
-
if (aliased) {
|
|
2029
|
-
this.recoveredSessionAlias.delete(sessionIdHeader);
|
|
2030
|
-
}
|
|
2031
2290
|
}
|
|
2032
2291
|
const body = req.body;
|
|
2033
2292
|
const isInit = body &&
|
|
@@ -2083,15 +2342,49 @@ class AgentDeckMCPServer {
|
|
|
2083
2342
|
// A presented bearer always resolves as a remote grant — even in local
|
|
2084
2343
|
// mode — while loopback launcher connections without one keep the
|
|
2085
2344
|
// existing deck-header path with zero config change.
|
|
2345
|
+
const presentedSessionIdForInit = sessionIdHeader && !this.closedSessionIds.has(sessionIdHeader) ? sessionIdHeader : undefined;
|
|
2086
2346
|
const presentedBearer = readBearerToken(req);
|
|
2087
2347
|
if (presentedBearer) {
|
|
2088
|
-
await this.handleGrantInitialize(req, res, presentedBearer);
|
|
2348
|
+
await this.handleGrantInitialize(req, res, presentedBearer, presentedSessionIdForInit);
|
|
2089
2349
|
return;
|
|
2090
2350
|
}
|
|
2091
2351
|
if (bearerRequired) {
|
|
2092
2352
|
this.sendGrantRequired(res);
|
|
2093
2353
|
return;
|
|
2094
2354
|
}
|
|
2355
|
+
// NOT-634: an initialize presenting a pre-restart session id first
|
|
2356
|
+
// recovers that id's approved session deck, ahead of header → folder
|
|
2357
|
+
// pin → default deck. Without an approved deck it resolves exactly as
|
|
2358
|
+
// before (the fresh id is still aliased to the presented lineage).
|
|
2359
|
+
// An initialize without a session id never reaches this branch.
|
|
2360
|
+
const presentedSessionId = presentedSessionIdForInit;
|
|
2361
|
+
if (presentedSessionId) {
|
|
2362
|
+
const recoveredSessionId = (0, node_crypto_1.randomUUID)();
|
|
2363
|
+
let recoveredDeck;
|
|
2364
|
+
try {
|
|
2365
|
+
recoveredDeck = await this.bindRecoveredSessionOverride(recoveredSessionId, req, presentedSessionId);
|
|
2366
|
+
}
|
|
2367
|
+
catch (error) {
|
|
2368
|
+
this.sessionBinding.clearSession(recoveredSessionId);
|
|
2369
|
+
if (error instanceof SessionDeckMissingError) {
|
|
2370
|
+
console.warn(`[agent-deck] MCP initialize presented pre-restart session ${presentedSessionId} ` +
|
|
2371
|
+
`(client ${readInitializeClientName(body) ?? 'unknown'}); its approved session deck is gone:`, error.message);
|
|
2372
|
+
this.sendSessionDeckMissing(res, error.message);
|
|
2373
|
+
return;
|
|
2374
|
+
}
|
|
2375
|
+
// Any other failure: resolve as an initialize without an override.
|
|
2376
|
+
}
|
|
2377
|
+
if (recoveredDeck) {
|
|
2378
|
+
console.warn(`[agent-deck] MCP initialize presented pre-restart session ${presentedSessionId} ` +
|
|
2379
|
+
`(client ${readInitializeClientName(body) ?? 'unknown'}); recovered approved session deck ` +
|
|
2380
|
+
`${recoveredDeck} as ${recoveredSessionId}.`);
|
|
2381
|
+
await this.establishTransportSession(req, res, recoveredSessionId);
|
|
2382
|
+
return;
|
|
2383
|
+
}
|
|
2384
|
+
console.warn(`[agent-deck] MCP initialize presented pre-restart session ${presentedSessionId} ` +
|
|
2385
|
+
`(client ${readInitializeClientName(body) ?? 'unknown'}); no approved session deck — ` +
|
|
2386
|
+
'resolving from deck header, folder pin, or default deck.');
|
|
2387
|
+
}
|
|
2095
2388
|
// NOT-562: workspace-header-only hosts (pure MCP, no launcher) resolve
|
|
2096
2389
|
// the deck from the folder assignment. An explicit deck header still
|
|
2097
2390
|
// wins; the manifest is consulted only when it is absent. NOT-600 adds
|
|
@@ -2104,7 +2397,7 @@ class AgentDeckMCPServer {
|
|
|
2104
2397
|
const sessionId = (0, node_crypto_1.randomUUID)();
|
|
2105
2398
|
if (effectiveDeck) {
|
|
2106
2399
|
try {
|
|
2107
|
-
await this.authenticateLaunchDeck(sessionId, req, effectiveDeck);
|
|
2400
|
+
await this.authenticateLaunchDeck(sessionId, req, effectiveDeck, presentedSessionId);
|
|
2108
2401
|
if (manifestDeck && !launchDeck && effectiveDeck === manifestDeck) {
|
|
2109
2402
|
this.sessionBinding.markUseManifestSession(sessionId);
|
|
2110
2403
|
}
|
|
@@ -2127,7 +2420,7 @@ class AgentDeckMCPServer {
|
|
|
2127
2420
|
const canFallThroughToDefault = manifestDeck && !launchDeck && defaultDeck && defaultDeck !== manifestDeck;
|
|
2128
2421
|
if (canFallThroughToDefault) {
|
|
2129
2422
|
try {
|
|
2130
|
-
await this.authenticateLaunchDeck(sessionId, req, defaultDeck);
|
|
2423
|
+
await this.authenticateLaunchDeck(sessionId, req, defaultDeck, presentedSessionId);
|
|
2131
2424
|
this.sessionBinding.markDefaultDeckSession(sessionId);
|
|
2132
2425
|
}
|
|
2133
2426
|
catch {
|
|
@@ -2215,59 +2508,123 @@ class AgentDeckMCPServer {
|
|
|
2215
2508
|
* failure answers 404, never a new 401 surface on follow-up requests.
|
|
2216
2509
|
*/
|
|
2217
2510
|
async tryRecoverStaleSession(req, res, staleSessionId) {
|
|
2218
|
-
// Local bearer-free mode only: hosted requests keep the 401 gate (or
|
|
2219
|
-
// the 404 below once authenticated), and any request carrying a grant
|
|
2220
|
-
// bearer keeps the restart 404 — grant clients re-initialize.
|
|
2221
|
-
if (isBearerGrantRequired()) {
|
|
2222
|
-
return false;
|
|
2223
|
-
}
|
|
2224
|
-
if (readBearerToken(req)) {
|
|
2225
|
-
return false;
|
|
2226
|
-
}
|
|
2227
2511
|
// A session this process closed stays closed — never resurrect it.
|
|
2228
2512
|
if (this.closedSessionIds.has(staleSessionId)) {
|
|
2229
2513
|
return false;
|
|
2230
2514
|
}
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
if (
|
|
2237
|
-
|
|
2515
|
+
// Bearer / hosted requests: header-based recovery stays local
|
|
2516
|
+
// bearer-free only (grant clients re-initialize). NOT-634 adds one
|
|
2517
|
+
// narrow case — an authenticated grant whose presented id carries an
|
|
2518
|
+
// approved session deck owned by that same grant.
|
|
2519
|
+
const bearer = readBearerToken(req);
|
|
2520
|
+
if (bearer || isBearerGrantRequired()) {
|
|
2521
|
+
const principal = this.authenticateGrant(bearer);
|
|
2522
|
+
if (!principal) {
|
|
2523
|
+
return false;
|
|
2524
|
+
}
|
|
2525
|
+
return this.tryRecoverGrantSessionOverride(req, res, staleSessionId, principal);
|
|
2238
2526
|
}
|
|
2239
2527
|
const freshSessionId = (0, node_crypto_1.randomUUID)();
|
|
2240
|
-
|
|
2528
|
+
// NOT-634: the presented id's approved session deck wins over header →
|
|
2529
|
+
// folder pin → default deck. A deleted approved deck is answered
|
|
2530
|
+
// explicitly (served, so the call site does not 404 into a silent
|
|
2531
|
+
// fallback); any other lookup failure resolves from headers as before.
|
|
2532
|
+
let overrideDeck;
|
|
2241
2533
|
try {
|
|
2242
|
-
await this.
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
this.
|
|
2534
|
+
overrideDeck = await this.bindRecoveredSessionOverride(freshSessionId, req, staleSessionId);
|
|
2535
|
+
}
|
|
2536
|
+
catch (error) {
|
|
2537
|
+
this.sessionBinding.clearSession(freshSessionId);
|
|
2538
|
+
if (error instanceof SessionDeckMissingError) {
|
|
2539
|
+
if (!this.staleSessionsById.get(staleSessionId)?.recovered) {
|
|
2540
|
+
console.warn(`[agent-deck] MCP session ${staleSessionId} presented after restart; its approved session deck is gone:`, error.message);
|
|
2541
|
+
}
|
|
2542
|
+
this.sendSessionDeckMissing(res, error.message);
|
|
2543
|
+
return true;
|
|
2248
2544
|
}
|
|
2249
2545
|
}
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
// line per request (the 404 path logs first-seen ids already).
|
|
2258
|
-
this.sessionBinding.clearSession(freshSessionId);
|
|
2546
|
+
let recoveredDeck;
|
|
2547
|
+
if (overrideDeck) {
|
|
2548
|
+
recoveredDeck = overrideDeck;
|
|
2549
|
+
}
|
|
2550
|
+
else {
|
|
2551
|
+
const { launchDeck, manifestDeck, defaultDeck, effectiveDeck } = this.resolveHeaderDeck(req);
|
|
2552
|
+
if (!effectiveDeck) {
|
|
2259
2553
|
return false;
|
|
2260
2554
|
}
|
|
2555
|
+
// A racing recovery may have installed an alias while headers resolved.
|
|
2556
|
+
if (await this.serveFromRecoveryAlias(req, res, staleSessionId)) {
|
|
2557
|
+
return true;
|
|
2558
|
+
}
|
|
2559
|
+
recoveredDeck = effectiveDeck;
|
|
2261
2560
|
try {
|
|
2262
|
-
await this.authenticateLaunchDeck(freshSessionId, req,
|
|
2263
|
-
|
|
2264
|
-
|
|
2561
|
+
await this.authenticateLaunchDeck(freshSessionId, req, effectiveDeck, staleSessionId);
|
|
2562
|
+
if (manifestDeck && !launchDeck && effectiveDeck === manifestDeck) {
|
|
2563
|
+
this.sessionBinding.markUseManifestSession(freshSessionId);
|
|
2564
|
+
}
|
|
2565
|
+
else if (defaultDeck && !launchDeck && effectiveDeck === defaultDeck) {
|
|
2566
|
+
this.sessionBinding.markDefaultDeckSession(freshSessionId);
|
|
2567
|
+
}
|
|
2265
2568
|
}
|
|
2266
2569
|
catch {
|
|
2267
|
-
|
|
2268
|
-
|
|
2570
|
+
// NOT-604: like initialize, a manifest naming a deleted deck falls
|
|
2571
|
+
// through to the user default deck before recovery gives up.
|
|
2572
|
+
const canFallThroughToDefault = manifestDeck && !launchDeck && defaultDeck && defaultDeck !== manifestDeck;
|
|
2573
|
+
if (!canFallThroughToDefault) {
|
|
2574
|
+
// Opportunistic: a bind failure falls back to the NOT-101 404 the
|
|
2575
|
+
// call site answers — silently, so a retrying host cannot spam one
|
|
2576
|
+
// line per request (the 404 path logs first-seen ids already).
|
|
2577
|
+
this.sessionBinding.clearSession(freshSessionId);
|
|
2578
|
+
return false;
|
|
2579
|
+
}
|
|
2580
|
+
try {
|
|
2581
|
+
await this.authenticateLaunchDeck(freshSessionId, req, defaultDeck, staleSessionId);
|
|
2582
|
+
this.sessionBinding.markDefaultDeckSession(freshSessionId);
|
|
2583
|
+
recoveredDeck = defaultDeck;
|
|
2584
|
+
}
|
|
2585
|
+
catch {
|
|
2586
|
+
this.sessionBinding.clearSession(freshSessionId);
|
|
2587
|
+
return false;
|
|
2588
|
+
}
|
|
2269
2589
|
}
|
|
2270
2590
|
}
|
|
2591
|
+
const recoveredFrom = this.sessionBinding.isSessionOverrideRecovery(freshSessionId)
|
|
2592
|
+
? 'its approved session deck'
|
|
2593
|
+
: 'headers';
|
|
2594
|
+
return this.installRecoveredSession(req, res, staleSessionId, freshSessionId, recoveredDeck, recoveredFrom);
|
|
2595
|
+
}
|
|
2596
|
+
/**
|
|
2597
|
+
* NOT-634: stale follow-up from an authenticated bearer grant. Recovers
|
|
2598
|
+
* only an approved session deck owned by the same grant (the backend
|
|
2599
|
+
* re-checks the grant's current state and allowlist); anything else keeps
|
|
2600
|
+
* the restart 404 — grant clients re-initialize as before. A deleted
|
|
2601
|
+
* deck, revoked grant, or out-of-allowlist deck is answered explicitly.
|
|
2602
|
+
*/
|
|
2603
|
+
async tryRecoverGrantSessionOverride(req, res, staleSessionId, principal) {
|
|
2604
|
+
if (await this.serveFromRecoveryAlias(req, res, staleSessionId)) {
|
|
2605
|
+
return true;
|
|
2606
|
+
}
|
|
2607
|
+
const freshSessionId = (0, node_crypto_1.randomUUID)();
|
|
2608
|
+
let recoveredDeck;
|
|
2609
|
+
try {
|
|
2610
|
+
recoveredDeck = await this.bindRecoveredSessionOverride(freshSessionId, req, staleSessionId, principal);
|
|
2611
|
+
}
|
|
2612
|
+
catch (error) {
|
|
2613
|
+
this.sessionBinding.clearSession(freshSessionId);
|
|
2614
|
+
return this.answerRecoveryFailure(res, error, staleSessionId, undefined, principal.grantId);
|
|
2615
|
+
}
|
|
2616
|
+
if (!recoveredDeck) {
|
|
2617
|
+
this.sessionBinding.clearSession(freshSessionId);
|
|
2618
|
+
return false;
|
|
2619
|
+
}
|
|
2620
|
+
return this.installRecoveredSession(req, res, staleSessionId, freshSessionId, recoveredDeck, `its approved session deck (grantId=${principal.grantId})`);
|
|
2621
|
+
}
|
|
2622
|
+
/**
|
|
2623
|
+
* Shared NOT-569 tail: given a fresh session already bound (launch or
|
|
2624
|
+
* grant), mint its transport, perform the handshake on the host's
|
|
2625
|
+
* behalf, install the stale → fresh alias, and serve the request.
|
|
2626
|
+
*/
|
|
2627
|
+
async installRecoveredSession(req, res, staleSessionId, freshSessionId, recoveredDeck, recoveredFrom) {
|
|
2271
2628
|
// Adopt a concurrent winner instead of minting a second session.
|
|
2272
2629
|
if (await this.serveFromRecoveryAlias(req, res, staleSessionId)) {
|
|
2273
2630
|
await this.disconnectTrustedSession(freshSessionId, req);
|
|
@@ -2304,20 +2661,18 @@ class AgentDeckMCPServer {
|
|
|
2304
2661
|
// Atomic install: a concurrent recovery may have won during the
|
|
2305
2662
|
// handshake above — the check and the set below run with no await
|
|
2306
2663
|
// between them, so exactly one winner installs the alias.
|
|
2307
|
-
|
|
2308
|
-
|
|
2664
|
+
// Only a winner of this same principal is adopted (NOT-634).
|
|
2665
|
+
const lateWinner = this.lookupRecoveryAlias(req, staleSessionId);
|
|
2666
|
+
if (lateWinner) {
|
|
2309
2667
|
await this.startCleanupSession(freshSessionId, { closeTransport: true });
|
|
2310
2668
|
await this.serveFollowUp(lateWinner, req, res, staleSessionId);
|
|
2311
2669
|
return true;
|
|
2312
2670
|
}
|
|
2313
|
-
|
|
2314
|
-
this.recoveredSessionAlias.delete(staleSessionId);
|
|
2315
|
-
}
|
|
2316
|
-
this.rememberRecoveryAlias(staleSessionId, freshSessionId);
|
|
2671
|
+
this.rememberRecoveryAlias(req, staleSessionId, freshSessionId);
|
|
2317
2672
|
this.markStaleSessionRecoveredById(staleSessionId);
|
|
2318
2673
|
// One line per recovered session id, not per request — the host keeps
|
|
2319
2674
|
// sending the stale id on every later call.
|
|
2320
|
-
console.warn(`[agent-deck] MCP session ${staleSessionId} recovered from
|
|
2675
|
+
console.warn(`[agent-deck] MCP session ${staleSessionId} recovered from ${recoveredFrom} as ${freshSessionId} ` +
|
|
2321
2676
|
`(deck ${recoveredDeck}); the client never re-initialized after the restart.`);
|
|
2322
2677
|
// NOT-513: adopt the host-side session id before the first live-display
|
|
2323
2678
|
// register, so the entry carries it from the start.
|
|
@@ -2334,16 +2689,12 @@ class AgentDeckMCPServer {
|
|
|
2334
2689
|
* caller re-recovers from headers instead of sticking to it.
|
|
2335
2690
|
*/
|
|
2336
2691
|
async serveFromRecoveryAlias(req, res, staleSessionId) {
|
|
2337
|
-
const aliased = this.
|
|
2692
|
+
const aliased = this.lookupRecoveryAlias(req, staleSessionId);
|
|
2338
2693
|
if (!aliased) {
|
|
2339
2694
|
return false;
|
|
2340
2695
|
}
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
return true;
|
|
2344
|
-
}
|
|
2345
|
-
this.recoveredSessionAlias.delete(staleSessionId);
|
|
2346
|
-
return false;
|
|
2696
|
+
await this.serveFollowUp(aliased, req, res, staleSessionId);
|
|
2697
|
+
return true;
|
|
2347
2698
|
}
|
|
2348
2699
|
/**
|
|
2349
2700
|
* NOT-569: perform the MCP initialize handshake on the host's behalf so
|
|
@@ -2481,14 +2832,11 @@ class AgentDeckMCPServer {
|
|
|
2481
2832
|
sessionId = sessionIdHeader;
|
|
2482
2833
|
}
|
|
2483
2834
|
else {
|
|
2484
|
-
const aliased = this.
|
|
2485
|
-
if (aliased
|
|
2835
|
+
const aliased = this.lookupRecoveryAlias(req, sessionIdHeader);
|
|
2836
|
+
if (aliased) {
|
|
2486
2837
|
sessionId = aliased;
|
|
2487
2838
|
presentedSessionId = sessionIdHeader;
|
|
2488
2839
|
}
|
|
2489
|
-
else if (aliased) {
|
|
2490
|
-
this.recoveredSessionAlias.delete(sessionIdHeader);
|
|
2491
|
-
}
|
|
2492
2840
|
}
|
|
2493
2841
|
const session = sessionId ? this.sessions.get(sessionId) : undefined;
|
|
2494
2842
|
if (!sessionId || !session) {
|