@humain/humain-code 0.6.1 → 0.6.2
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/CHANGELOG.md +42 -0
- package/dist/bundle/agent-monitor.js +1 -1
- package/dist/bundle/chunks/{anthropic-messages-FGFIMZUF.js → anthropic-messages-MNISFDZA.js} +2 -2
- package/dist/bundle/chunks/{azure-openai-responses-6JNTC7KO.js → azure-openai-responses-IV74M5OR.js} +3 -3
- package/dist/bundle/chunks/bedrock-converse-stream.js +4 -5
- package/dist/bundle/chunks/{chunk-CVC2GV26.js → chunk-2HZ2V3HS.js} +610 -611
- package/dist/bundle/chunks/{chunk-IJYVMOB4.js → chunk-2PZ46FSM.js} +3 -3
- package/dist/bundle/chunks/{chunk-I6AQOITY.js → chunk-3LP543EX.js} +6 -6
- package/dist/bundle/chunks/{chunk-FDGW5ZTG.js → chunk-4J6BGNX2.js} +6 -6
- package/dist/bundle/chunks/{chunk-EDONIPUJ.js → chunk-4SQUP6IR.js} +7 -7
- package/dist/bundle/chunks/{chunk-P4P7OZ7R.js → chunk-5L4JG6PO.js} +1 -1
- package/dist/bundle/chunks/{chunk-GICL5V54.js → chunk-6PD3T2EF.js} +1 -1
- package/dist/bundle/chunks/{chunk-CFHOPLW5.js → chunk-75CQYZ63.js} +56 -54
- package/dist/bundle/chunks/{chunk-DHW3REC5.js → chunk-A2VPLMF2.js} +1 -1
- package/dist/bundle/chunks/{chunk-AOH2O5CP.js → chunk-DCPACGLH.js} +575 -538
- package/dist/bundle/chunks/{chunk-M4CHQBAI.js → chunk-DLCBSLHW.js} +1 -1
- package/dist/bundle/chunks/chunk-F77J5EQZ.js +3 -0
- package/dist/bundle/chunks/{chunk-T3KDZFM4.js → chunk-FHLLDO7U.js} +7 -6
- package/dist/bundle/chunks/{chunk-AHSNQWZU.js → chunk-FLVBQ6GS.js} +1 -1
- package/dist/bundle/chunks/{chunk-ONR6Q3II.js → chunk-JCTQQ3DX.js} +1 -1
- package/dist/bundle/chunks/{chunk-YRUIJ5AH.js → chunk-JEAM6N22.js} +6 -5
- package/dist/bundle/chunks/{chunk-LDGCU6PD.js → chunk-K7SDY3VJ.js} +1 -1
- package/dist/bundle/chunks/{chunk-IADQJFNH.js → chunk-KM4GQ2BU.js} +5 -4
- package/dist/bundle/chunks/{chunk-JFE52VSN.js → chunk-NCC2FAJN.js} +23 -22
- package/dist/bundle/chunks/{chunk-RO5YPAJH.js → chunk-PS2FJ7M4.js} +1 -1
- package/dist/bundle/chunks/{chunk-HW6HMUXS.js → chunk-SFMDIVZ4.js} +1 -1
- package/dist/bundle/chunks/{chunk-B7NJMF7B.js → chunk-WEFEQEIQ.js} +4 -4
- package/dist/bundle/chunks/{chunk-6DE7LJKV.js → chunk-WW4SILXG.js} +24 -19
- package/dist/bundle/chunks/{cli-BNCWK54K.js → cli-N7KOVPLB.js} +1 -1
- package/dist/bundle/chunks/{cloudflare-workers-ai-system-one-KI6W3USX.js → cloudflare-workers-ai-system-one-KRMHG2L3.js} +1 -1
- package/dist/bundle/chunks/codemode-worker.js +10 -4
- package/dist/bundle/chunks/{compat-I5VPOXFT.js → compat-MXAZPYDN.js} +9 -9
- package/dist/bundle/chunks/{dist-SKBR3C7C.js → dist-3V7ZSLER.js} +29 -26
- package/dist/bundle/chunks/dist-66CW64M2.js +8 -0
- package/dist/bundle/chunks/{easter-egg-3d-673VRP6Y.js → easter-egg-3d-O2GG2XIG.js} +1 -1
- package/dist/bundle/chunks/{execute-PIUR5MUV.js → execute-LMBACTKA.js} +28 -23
- package/dist/bundle/chunks/{extract-4O5Z5725.js → extract-CXXLVFC5.js} +7 -7
- package/dist/bundle/chunks/{google-generative-ai-635YZEHN.js → google-generative-ai-SBL77OXO.js} +4 -4
- package/dist/bundle/chunks/{google-vertex-I3ZJ3SB2.js → google-vertex-NZBNNXMK.js} +6 -6
- package/dist/bundle/chunks/{host-OQOEIWHK.js → host-SCZ4IKWR.js} +1 -1
- package/dist/bundle/chunks/{mistral-conversations-OKG5GKXR.js → mistral-conversations-DX4YKJSO.js} +14 -13
- package/dist/bundle/chunks/{openai-codex-responses-6JYZIFT4.js → openai-codex-responses-LZWBQYVP.js} +38 -37
- package/dist/bundle/chunks/{openai-completions-YBNI6CAP.js → openai-completions-7NWH423P.js} +2 -2
- package/dist/bundle/chunks/{openai-decisions-5G5YBFRQ.js → openai-decisions-ALJROWBE.js} +1 -1
- package/dist/bundle/chunks/{openai-responses-C3APJ6MK.js → openai-responses-5YOLHMCF.js} +3 -3
- package/dist/bundle/chunks/{pi-messages-UHYZYEOC.js → pi-messages-NHTQQOPW.js} +1 -1
- package/dist/bundle/chunks/{runtime-KE3HDSRT.js → runtime-UVPDE4CX.js} +1 -1
- package/dist/bundle/chunks/{typesafe-system-one-VYVAMWQV.js → typesafe-system-one-BHJYAZ6E.js} +1 -1
- package/dist/bundle/chunks/virtual-modules-YQPK57CW.js +23 -0
- package/dist/bundle/cli-runtime.js +3 -3
- package/dist/bundle/index.js +6 -6
- package/dist/bundle/rpc-entry.js +2 -2
- package/dist/coordination/adapter/commands-integration.d.ts +2 -2
- package/dist/coordination/adapter/commands-integration.d.ts.map +1 -1
- package/dist/coordination/adapter/commands-integration.js +14 -10
- package/dist/coordination/adapter/commands-integration.js.map +1 -1
- package/dist/coordination/adapter/delivery.d.ts +3 -0
- package/dist/coordination/adapter/delivery.d.ts.map +1 -1
- package/dist/coordination/adapter/delivery.js +4 -1
- package/dist/coordination/adapter/delivery.js.map +1 -1
- package/dist/coordination/adapter/index.d.ts.map +1 -1
- package/dist/coordination/adapter/index.js +26 -2
- package/dist/coordination/adapter/index.js.map +1 -1
- package/dist/coordination/adapter/integration-notices.d.ts +2 -2
- package/dist/coordination/adapter/integration-notices.d.ts.map +1 -1
- package/dist/coordination/adapter/integration-notices.js +5 -3
- package/dist/coordination/adapter/integration-notices.js.map +1 -1
- package/dist/coordination/adapter/integration-tool.d.ts +2 -1
- package/dist/coordination/adapter/integration-tool.d.ts.map +1 -1
- package/dist/coordination/adapter/integration-tool.js +11 -10
- package/dist/coordination/adapter/integration-tool.js.map +1 -1
- package/dist/coordination/adapter/peer-label.d.ts +5 -0
- package/dist/coordination/adapter/peer-label.d.ts.map +1 -0
- package/dist/coordination/adapter/peer-label.js +38 -0
- package/dist/coordination/adapter/peer-label.js.map +1 -0
- package/dist/coordination/adapter/render.d.ts +19 -0
- package/dist/coordination/adapter/render.d.ts.map +1 -0
- package/dist/coordination/adapter/render.js +107 -0
- package/dist/coordination/adapter/render.js.map +1 -0
- package/dist/coordination/adapter/tool.d.ts +12 -3
- package/dist/coordination/adapter/tool.d.ts.map +1 -1
- package/dist/coordination/adapter/tool.js +69 -32
- package/dist/coordination/adapter/tool.js.map +1 -1
- package/dist/coordination/broker/broker.d.ts +4 -1
- package/dist/coordination/broker/broker.d.ts.map +1 -1
- package/dist/coordination/broker/broker.js +146 -60
- package/dist/coordination/broker/broker.js.map +1 -1
- package/dist/coordination/client/client.d.ts.map +1 -1
- package/dist/coordination/client/client.js +10 -2
- package/dist/coordination/client/client.js.map +1 -1
- package/dist/coordination/transport/discovery.d.ts +4 -0
- package/dist/coordination/transport/discovery.d.ts.map +1 -1
- package/dist/coordination/transport/discovery.js +14 -0
- package/dist/coordination/transport/discovery.js.map +1 -1
- package/dist/coordination/transport/protocol.d.ts.map +1 -1
- package/dist/coordination/transport/protocol.js +3 -0
- package/dist/coordination/transport/protocol.js.map +1 -1
- package/dist/coordination/transport/types.d.ts +5 -1
- package/dist/coordination/transport/types.d.ts.map +1 -1
- package/dist/coordination/transport/types.js.map +1 -1
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +6 -2
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/bash-executor.d.ts.map +1 -1
- package/dist/core/bash-executor.js +13 -36
- package/dist/core/bash-executor.js.map +1 -1
- package/dist/core/bedrock-account-reporting.d.ts.map +1 -1
- package/dist/core/bedrock-account-reporting.js +2 -1
- package/dist/core/bedrock-account-reporting.js.map +1 -1
- package/dist/core/cache-warmer.d.ts.map +1 -1
- package/dist/core/cache-warmer.js +2 -2
- package/dist/core/cache-warmer.js.map +1 -1
- package/dist/core/compaction/branch-summarization.d.ts.map +1 -1
- package/dist/core/compaction/branch-summarization.js +3 -2
- package/dist/core/compaction/branch-summarization.js.map +1 -1
- package/dist/core/compaction/compaction.d.ts.map +1 -1
- package/dist/core/compaction/compaction.js +2 -2
- package/dist/core/compaction/compaction.js.map +1 -1
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +2 -0
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/model-runtime.d.ts +2 -0
- package/dist/core/model-runtime.d.ts.map +1 -1
- package/dist/core/model-runtime.js +7 -7
- package/dist/core/model-runtime.js.map +1 -1
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +8 -0
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/session-manager.d.ts +6 -0
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +27 -6
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/tools/bash.d.ts +2 -1
- package/dist/core/tools/bash.d.ts.map +1 -1
- package/dist/core/tools/bash.js +39 -3
- package/dist/core/tools/bash.js.map +1 -1
- package/dist/core/tools/index.d.ts +1 -1
- package/dist/core/tools/index.d.ts.map +1 -1
- package/dist/core/tools/index.js.map +1 -1
- package/dist/core/tools/output-accumulator.d.ts +5 -1
- package/dist/core/tools/output-accumulator.d.ts.map +1 -1
- package/dist/core/tools/output-accumulator.js +45 -16
- package/dist/core/tools/output-accumulator.js.map +1 -1
- package/dist/core/tools/render-utils.d.ts.map +1 -1
- package/dist/core/tools/render-utils.js +2 -2
- package/dist/core/tools/render-utils.js.map +1 -1
- package/dist/core/tools/renderers/write.js +2 -2
- package/dist/core/tools/renderers/write.js.map +1 -1
- package/dist/core/tools/subagent.d.ts +22 -0
- package/dist/core/tools/subagent.d.ts.map +1 -1
- package/dist/core/tools/subagent.js +46 -7
- package/dist/core/tools/subagent.js.map +1 -1
- package/dist/core/trace-client/credential-store.d.ts.map +1 -1
- package/dist/core/trace-client/credential-store.js +2 -0
- package/dist/core/trace-client/credential-store.js.map +1 -1
- package/dist/extensions/codemode/execute.d.ts.map +1 -1
- package/dist/extensions/codemode/execute.js +3 -12
- package/dist/extensions/codemode/execute.js.map +1 -1
- package/dist/humain/bedrock-provider/vendor/index.bundle.mjs +10 -6
- package/dist/humain/gateway/secret-store.d.ts.map +1 -1
- package/dist/humain/gateway/secret-store.js +26 -12
- package/dist/humain/gateway/secret-store.js.map +1 -1
- package/dist/humain/gateway/session-store.d.ts +2 -2
- package/dist/humain/gateway/session-store.d.ts.map +1 -1
- package/dist/humain/gateway/session-store.js +13 -3
- package/dist/humain/gateway/session-store.js.map +1 -1
- package/dist/humain/web-access/vendor/extract.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/extract.js +4 -2
- package/dist/humain/web-access/vendor/extract.js.map +1 -1
- package/dist/humain/web-access/vendor/page-query.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/page-query.js +2 -1
- package/dist/humain/web-access/vendor/page-query.js.map +1 -1
- package/dist/humain/web-access/vendor/query-rewrite.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/query-rewrite.js +2 -1
- package/dist/humain/web-access/vendor/query-rewrite.js.map +1 -1
- package/dist/humain/web-access/vendor/storage.d.ts +2 -0
- package/dist/humain/web-access/vendor/storage.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/storage.js +71 -9
- package/dist/humain/web-access/vendor/storage.js.map +1 -1
- package/dist/humain/web-access/vendor/summary-review.d.ts.map +1 -1
- package/dist/humain/web-access/vendor/summary-review.js +2 -1
- package/dist/humain/web-access/vendor/summary-review.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +3 -3
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/mermaid.d.ts.map +1 -1
- package/dist/modes/interactive/components/mermaid.js +71 -34
- package/dist/modes/interactive/components/mermaid.js.map +1 -1
- package/dist/utils/child-process.d.ts.map +1 -1
- package/dist/utils/child-process.js +16 -0
- package/dist/utils/child-process.js.map +1 -1
- package/docs/coordination.md +6 -2
- package/docs/development.md +4 -0
- package/docs/extensions.md +2 -0
- package/docs/providers.md +12 -2
- package/examples/extensions/gondolin/index.ts +1 -1
- package/examples/extensions/ssh.ts +34 -5
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/src/coordination/UPSTREAM.md +17 -0
- package/src/humain/web-access/UPSTREAM.md +11 -0
- package/dist/bundle/chunks/chunk-L6Z6JKKC.js +0 -3
- package/dist/bundle/chunks/dist-TVRQXUY6.js +0 -8
- package/dist/bundle/chunks/virtual-modules-KDUZIB3W.js +0 -23
|
@@ -4,7 +4,7 @@ import { lstatSync, readdirSync, unlinkSync } from "node:fs";
|
|
|
4
4
|
import net from "node:net";
|
|
5
5
|
import { dirname, join } from "node:path";
|
|
6
6
|
import { isProcessAlive } from "../client/spawn.js";
|
|
7
|
-
import { matchesPeer, resolveExactTargets, resolvePrefixTargets } from "../transport/discovery.js";
|
|
7
|
+
import { matchesPeer, peerHandle, resolveExactTargets, resolveHandleTargets, resolvePrefixTargets, } from "../transport/discovery.js";
|
|
8
8
|
import { createMessageReader, framePayloadBytes, MAX_FRAME_BYTES, writeMessage } from "../transport/framing.js";
|
|
9
9
|
import { COORDINATION_PROTOCOL_NAME, COORDINATION_PROTOCOL_VERSION, ensureCoordinationRuntimeDir, ensureRuntimeDir, getCoordinationRuntimePaths, MAX_MESSAGE_TEXT_BYTES, restrictRuntimeFile, } from "../transport/paths.js";
|
|
10
10
|
import { canonicalMessageReceipt, ID_IN_USE_DETAIL, isArtifactName, isBoundedString, isMessage, isMessageReceipt, isOwnershipMode, isRecordState, isRequestId, isReservedNamespace, isSessionId, isSessionInfo, isSessionRegistration, isTouchedPaths, isValidMessageId, MAX_MESSAGE_ID_CHARS, MAX_REQUEST_ID_CHARS, MAX_SESSION_FIELD_CHARS, messageDeliveryFingerprint, } from "../transport/protocol.js";
|
|
@@ -905,6 +905,22 @@ export class CoordinationBroker {
|
|
|
905
905
|
let stopped = false;
|
|
906
906
|
const queued = [];
|
|
907
907
|
let queuedBytes = 0;
|
|
908
|
+
let receipts = [];
|
|
909
|
+
const flushReceipts = () => {
|
|
910
|
+
if (receipts.length === 0)
|
|
911
|
+
return;
|
|
912
|
+
const batch = receipts;
|
|
913
|
+
receipts = [];
|
|
914
|
+
try {
|
|
915
|
+
if (sessionKey)
|
|
916
|
+
this.handleReceiptBatch(socket, sessionKey, batch);
|
|
917
|
+
}
|
|
918
|
+
catch (error) {
|
|
919
|
+
stopped = true;
|
|
920
|
+
queued.length = 0;
|
|
921
|
+
socket.destroy(error instanceof Error ? error : new Error(String(error)));
|
|
922
|
+
}
|
|
923
|
+
};
|
|
908
924
|
const processMessage = (msg, probed = false, legacyError) => {
|
|
909
925
|
if (stopped || socket.destroyed || !socket.writable)
|
|
910
926
|
return;
|
|
@@ -961,6 +977,7 @@ export class CoordinationBroker {
|
|
|
961
977
|
queuedBytes -= framePayloadBytes(pending);
|
|
962
978
|
processMessage(pending);
|
|
963
979
|
}
|
|
980
|
+
flushReceipts();
|
|
964
981
|
if (!paused && !socket.destroyed)
|
|
965
982
|
socket.resume();
|
|
966
983
|
});
|
|
@@ -970,6 +987,13 @@ export class CoordinationBroker {
|
|
|
970
987
|
const session = sessionKey ? this.sessions.get(sessionKey) : undefined;
|
|
971
988
|
if (session?.socket === socket)
|
|
972
989
|
session.lastInboundAt = Date.now();
|
|
990
|
+
if (sessionKey && isRecord(msg) && msg.type === "message_receipt" && isMessageReceipt(msg.receipt)) {
|
|
991
|
+
receipts.push(msg.receipt);
|
|
992
|
+
return;
|
|
993
|
+
}
|
|
994
|
+
flushReceipts();
|
|
995
|
+
if (stopped || socket.destroyed || !socket.writable)
|
|
996
|
+
return;
|
|
973
997
|
this.handleMessage(socket, msg, sessionKey, boundScopeId, (id, scopeId) => {
|
|
974
998
|
sessionKey = id;
|
|
975
999
|
if (id) {
|
|
@@ -997,15 +1021,21 @@ export class CoordinationBroker {
|
|
|
997
1021
|
// Receipts drain a mailbox in bursts; only validated receipt shapes earn the discount.
|
|
998
1022
|
const isReceipt = isRecord(msg) && msg.type === "message_receipt" && isMessageReceipt(msg.receipt);
|
|
999
1023
|
if (!this.consumeToken(connection, Date.now(), isReceipt ? RECEIPT_TOKEN_COST : 1)) {
|
|
1024
|
+
flushReceipts();
|
|
1000
1025
|
writeMessage(socket, { type: "error", error: "Intercom broker rate limit exceeded" });
|
|
1001
1026
|
socket.destroy(new Error("Intercom broker rate limit exceeded"));
|
|
1002
1027
|
return;
|
|
1003
1028
|
}
|
|
1004
1029
|
processMessage(msg);
|
|
1005
1030
|
}, (error) => {
|
|
1031
|
+
flushReceipts();
|
|
1006
1032
|
socket.destroy(error);
|
|
1007
1033
|
});
|
|
1008
|
-
socket.on("data",
|
|
1034
|
+
socket.on("data", (data) => {
|
|
1035
|
+
reader(data);
|
|
1036
|
+
// Commit complete receipts in this read, never a timer/debounce or an incomplete trailing frame.
|
|
1037
|
+
flushReceipts();
|
|
1038
|
+
});
|
|
1009
1039
|
socket.on("close", () => {
|
|
1010
1040
|
stopped = true;
|
|
1011
1041
|
queued.length = 0;
|
|
@@ -1313,13 +1343,14 @@ export class CoordinationBroker {
|
|
|
1313
1343
|
if (clientMessage.scope !== undefined && clientMessage.scope !== "repo" && clientMessage.scope !== "device")
|
|
1314
1344
|
throw new Error("Invalid list scope");
|
|
1315
1345
|
const filters = clientMessage;
|
|
1346
|
+
const liveIds = Array.from(this.sessions.values(), (session) => session.info.id);
|
|
1316
1347
|
const sessions = Array.from(this.sessions.values())
|
|
1317
1348
|
.filter((session) => (filters.scope !== "repo" || session.scopeId === requester.scopeId) &&
|
|
1318
1349
|
matchesPeer(session.info, filters))
|
|
1319
1350
|
.sort((a, b) => Number(b.scopeId === requester.scopeId) - Number(a.scopeId === requester.scopeId) ||
|
|
1320
1351
|
(a.info.repo ?? "").localeCompare(b.info.repo ?? "") ||
|
|
1321
1352
|
(a.scopeId ?? "").localeCompare(b.scopeId ?? ""))
|
|
1322
|
-
.map((s) => s.info);
|
|
1353
|
+
.map((s) => ({ ...s.info, handle: peerHandle(s.info.id, liveIds) }));
|
|
1323
1354
|
writeMessage(socket, buildSessionsReply(clientMessage.requestId, sessions));
|
|
1324
1355
|
break;
|
|
1325
1356
|
}
|
|
@@ -1355,7 +1386,10 @@ export class CoordinationBroker {
|
|
|
1355
1386
|
const brokerReceivedAt = Date.now();
|
|
1356
1387
|
// The frame sent on to the recipient is larger than the one received; refuse what cannot be
|
|
1357
1388
|
// delivered before anything is recorded, acknowledged or queued.
|
|
1358
|
-
if (!outgoingMessageFitsFrame(
|
|
1389
|
+
if (!outgoingMessageFitsFrame({
|
|
1390
|
+
...fromSession.info,
|
|
1391
|
+
handle: peerHandle(fromSession.info.id, Array.from(this.sessions.values(), (session) => session.info.id)),
|
|
1392
|
+
}, message, brokerReceivedAt)) {
|
|
1359
1393
|
this.writeDeliveryFailure(socket, message.id, `Message exceeds the ${MAX_FRAME_BYTES}-byte frame limit once delivered`, "E_MESSAGE_TOO_LARGE");
|
|
1360
1394
|
break;
|
|
1361
1395
|
}
|
|
@@ -1395,10 +1429,10 @@ export class CoordinationBroker {
|
|
|
1395
1429
|
const targetId = clientMessage.targetId;
|
|
1396
1430
|
const targetEpoch = clientMessage.targetEpoch;
|
|
1397
1431
|
const fingerprint = this.deliveryFingerprint(message, targetId);
|
|
1398
|
-
|
|
1432
|
+
const exactTarget = Array.from(this.sessions.values()).find((session) => session.info.id === targetId);
|
|
1433
|
+
if (this.replayOrReject(socket, currentKey, message.id, fingerprint, exactTarget?.info)) {
|
|
1399
1434
|
break;
|
|
1400
1435
|
}
|
|
1401
|
-
const exactTarget = Array.from(this.sessions.values()).find((session) => session.info.id === targetId);
|
|
1402
1436
|
if (!exactTarget) {
|
|
1403
1437
|
this.recordDelivery(currentKey, message.id, fingerprint, "failed", "Session not found", "E_TARGET_NOT_FOUND");
|
|
1404
1438
|
this.writeDeliveryFailure(socket, message.id, "Session not found", "E_TARGET_NOT_FOUND");
|
|
@@ -1419,7 +1453,7 @@ export class CoordinationBroker {
|
|
|
1419
1453
|
}
|
|
1420
1454
|
const target = targets[0];
|
|
1421
1455
|
const fingerprint = this.deliveryFingerprint(message, target.info.id);
|
|
1422
|
-
if (this.replayOrReject(socket, currentKey, message.id, fingerprint)) {
|
|
1456
|
+
if (this.replayOrReject(socket, currentKey, message.id, fingerprint, target.info)) {
|
|
1423
1457
|
break;
|
|
1424
1458
|
}
|
|
1425
1459
|
if (message.supersedes) {
|
|
@@ -1515,7 +1549,7 @@ export class CoordinationBroker {
|
|
|
1515
1549
|
const disconnectedTarget = disconnectedTargets[0];
|
|
1516
1550
|
const target = disconnectedTarget.info;
|
|
1517
1551
|
const fingerprint = this.deliveryFingerprint(message, target.id);
|
|
1518
|
-
if (this.replayOrReject(socket, currentKey, message.id, fingerprint)) {
|
|
1552
|
+
if (this.replayOrReject(socket, currentKey, message.id, fingerprint, target)) {
|
|
1519
1553
|
break;
|
|
1520
1554
|
}
|
|
1521
1555
|
if (message.supersedes) {
|
|
@@ -1570,34 +1604,7 @@ export class CoordinationBroker {
|
|
|
1570
1604
|
if (!isMessageReceipt(clientMessage.receipt)) {
|
|
1571
1605
|
throw new Error("Invalid message_receipt message");
|
|
1572
1606
|
}
|
|
1573
|
-
this.
|
|
1574
|
-
const route = this.messageReceiptRoutes.get(clientMessage.receipt.messageId);
|
|
1575
|
-
const receiver = this.sessions.get(currentKey);
|
|
1576
|
-
if (receiver?.socket === socket) {
|
|
1577
|
-
if (RECEIPT_PROOF_STATUSES.has(clientMessage.receipt.status)) {
|
|
1578
|
-
this.confirmPendingReceipt(currentKey, socket, clientMessage.receipt.messageId);
|
|
1579
|
-
}
|
|
1580
|
-
this.acknowledgeMailboxEntry(currentKey, clientMessage.receipt);
|
|
1581
|
-
}
|
|
1582
|
-
const sender = route ? this.sessions.get(route.from) : undefined;
|
|
1583
|
-
if (route?.to === currentKey && receiver?.socket === socket && sender) {
|
|
1584
|
-
// Relay a canonical copy: known fields only, bounded detail, and never a frame the sender's
|
|
1585
|
-
// reader would refuse (the receiver's session info rides along with the receipt).
|
|
1586
|
-
const frame = {
|
|
1587
|
-
type: "message_receipt",
|
|
1588
|
-
from: receiver.info,
|
|
1589
|
-
receipt: canonicalMessageReceipt(clientMessage.receipt),
|
|
1590
|
-
};
|
|
1591
|
-
if (framePayloadBytes(frame) > MAX_FRAME_BYTES) {
|
|
1592
|
-
console.error(`Coordination broker dropped a receipt for message ${clientMessage.receipt.messageId.slice(0, 64)}: the forwarded frame exceeds ${MAX_FRAME_BYTES} bytes`);
|
|
1593
|
-
break;
|
|
1594
|
-
}
|
|
1595
|
-
writeMessage(sender.socket, frame);
|
|
1596
|
-
// The receiver did not take this message. Its id may be held there by another sender's message, whose
|
|
1597
|
-
// terminal receipt must not reach this sender; the route is written again when the message is redelivered.
|
|
1598
|
-
if (clientMessage.receipt.status === "deferred")
|
|
1599
|
-
this.messageReceiptRoutes.delete(clientMessage.receipt.messageId);
|
|
1600
|
-
}
|
|
1607
|
+
this.handleReceiptBatch(socket, currentKey, [clientMessage.receipt]);
|
|
1601
1608
|
break;
|
|
1602
1609
|
}
|
|
1603
1610
|
case "cancel_message": {
|
|
@@ -2192,11 +2199,12 @@ export class CoordinationBroker {
|
|
|
2192
2199
|
acceptMail(from, target, message, fingerprint, brokerReceivedAt) {
|
|
2193
2200
|
// Expire old mail first (it persists on its own) so the snapshot below is the true pre-send state.
|
|
2194
2201
|
this.pruneMailboxMessages(brokerReceivedAt);
|
|
2202
|
+
const liveIds = Array.from(this.sessions.values(), (session) => session.info.id);
|
|
2195
2203
|
const entry = {
|
|
2196
|
-
from: { ...from.info },
|
|
2204
|
+
from: { ...from.info, handle: peerHandle(from.info.id, liveIds) },
|
|
2197
2205
|
fromKey: from.key,
|
|
2198
2206
|
...(from.scopeId ? { fromScopeId: from.scopeId } : {}),
|
|
2199
|
-
target: { ...target.info },
|
|
2207
|
+
target: { ...target.info, handle: peerHandle(target.info.id, liveIds) },
|
|
2200
2208
|
targetKey: target.key,
|
|
2201
2209
|
...(target.scopeId ? { targetScopeId: target.scopeId } : {}),
|
|
2202
2210
|
message: { ...message, brokerReceivedAt },
|
|
@@ -2270,6 +2278,87 @@ export class CoordinationBroker {
|
|
|
2270
2278
|
delete entry.deferred;
|
|
2271
2279
|
}
|
|
2272
2280
|
}
|
|
2281
|
+
/** Commit receipt-driven changes together, before confirmation, relay or redelivery can report success. */
|
|
2282
|
+
handleReceiptBatch(socket, sessionKey, receipts) {
|
|
2283
|
+
const receiver = this.sessions.get(sessionKey);
|
|
2284
|
+
if (receiver?.socket !== socket)
|
|
2285
|
+
return;
|
|
2286
|
+
this.pruneMessageReceiptRoutes();
|
|
2287
|
+
const mailboxBefore = [...this.mailboxMessages];
|
|
2288
|
+
const recordsBefore = new Map(this.deliveryRecords);
|
|
2289
|
+
const afterCommit = [];
|
|
2290
|
+
let changed = false;
|
|
2291
|
+
for (const receipt of receipts) {
|
|
2292
|
+
const proof = RECEIPT_PROOF_STATUSES.has(receipt.status)
|
|
2293
|
+
? this.mailboxMessages.find((entry) => entry.message.id === receipt.messageId && entry.inFlightTo === sessionKey && !entry.deferred)
|
|
2294
|
+
: undefined;
|
|
2295
|
+
if (proof) {
|
|
2296
|
+
if (this.deliveryRecords.get(this.deliveryRecordKey(proof.fromKey, receipt.messageId))?.state === "unknown") {
|
|
2297
|
+
this.updateDeliveryRecord(proof.fromKey, receipt.messageId, "socket_delivered");
|
|
2298
|
+
changed = true;
|
|
2299
|
+
}
|
|
2300
|
+
afterCommit.push(() => this.confirmPendingReceipt(proof, socket));
|
|
2301
|
+
}
|
|
2302
|
+
const retired = this.acknowledgeMailboxEntry(sessionKey, receipt);
|
|
2303
|
+
if (retired)
|
|
2304
|
+
changed = true;
|
|
2305
|
+
afterCommit.push(() => {
|
|
2306
|
+
const route = this.messageReceiptRoutes.get(receipt.messageId);
|
|
2307
|
+
if (retired) {
|
|
2308
|
+
this.unloadIdleScope(retired.fromScopeId);
|
|
2309
|
+
this.unloadIdleScope(retired.targetScopeId);
|
|
2310
|
+
}
|
|
2311
|
+
if (receipt.status === "injected" || receipt.status === "not_injected") {
|
|
2312
|
+
this.releaseParkedMail(sessionKey, receipt.messageId);
|
|
2313
|
+
}
|
|
2314
|
+
const sender = route ? this.sessions.get(route.from) : undefined;
|
|
2315
|
+
if (route?.to !== sessionKey || !sender)
|
|
2316
|
+
return;
|
|
2317
|
+
// Relay a canonical copy: known fields only, bounded detail, and never a frame the sender's
|
|
2318
|
+
// reader would refuse (the receiver's session info rides along with the receipt).
|
|
2319
|
+
const frame = {
|
|
2320
|
+
type: "message_receipt",
|
|
2321
|
+
from: receiver.info,
|
|
2322
|
+
receipt: canonicalMessageReceipt(receipt),
|
|
2323
|
+
};
|
|
2324
|
+
if (framePayloadBytes(frame) > MAX_FRAME_BYTES) {
|
|
2325
|
+
console.error(`Coordination broker dropped a receipt for message ${receipt.messageId.slice(0, 64)}: the forwarded frame exceeds ${MAX_FRAME_BYTES} bytes`);
|
|
2326
|
+
return;
|
|
2327
|
+
}
|
|
2328
|
+
writeMessage(sender.socket, frame);
|
|
2329
|
+
// The receiver did not take this message. Its id may be held there by another sender's message, whose
|
|
2330
|
+
// terminal receipt must not reach this sender; the route is written again when the message is redelivered.
|
|
2331
|
+
if (receipt.status === "deferred")
|
|
2332
|
+
this.messageReceiptRoutes.delete(receipt.messageId);
|
|
2333
|
+
});
|
|
2334
|
+
}
|
|
2335
|
+
try {
|
|
2336
|
+
if (changed)
|
|
2337
|
+
this.persist();
|
|
2338
|
+
}
|
|
2339
|
+
catch (error) {
|
|
2340
|
+
this.mailboxMessages = mailboxBefore;
|
|
2341
|
+
this.deliveryRecords = recordsBefore;
|
|
2342
|
+
console.error("Coordination broker failed to persist receipt batch:", error);
|
|
2343
|
+
writeMessage(socket, {
|
|
2344
|
+
type: "error",
|
|
2345
|
+
code: "E_PERSIST_FAILED",
|
|
2346
|
+
error: "Could not persist message receipts; retry",
|
|
2347
|
+
});
|
|
2348
|
+
for (const receipt of receipts) {
|
|
2349
|
+
for (const [key, pending] of this.pendingReceipts) {
|
|
2350
|
+
if (pending.targetSocket === socket && pending.entry.message.id === receipt.messageId) {
|
|
2351
|
+
this.failPendingReceipt(key, "Could not persist message receipts; retry", "E_PERSIST_FAILED");
|
|
2352
|
+
}
|
|
2353
|
+
}
|
|
2354
|
+
}
|
|
2355
|
+
// Sent receipts are no longer queued at the receiver; reconnect redelivers mail and replays them.
|
|
2356
|
+
socket.destroy();
|
|
2357
|
+
return;
|
|
2358
|
+
}
|
|
2359
|
+
for (const action of afterCommit)
|
|
2360
|
+
action();
|
|
2361
|
+
}
|
|
2273
2362
|
/**
|
|
2274
2363
|
* `deferred` (the recipient shut down before delivering) deliberately does not retire the entry: it stays in
|
|
2275
2364
|
* flight until that socket closes, then returns to the queue and is redelivered when the session resumes.
|
|
@@ -2294,16 +2383,15 @@ export class CoordinationBroker {
|
|
|
2294
2383
|
entry.inFlightTo === undefined &&
|
|
2295
2384
|
entry.targetKey === sessionKey);
|
|
2296
2385
|
}
|
|
2297
|
-
if (index >= 0)
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
}
|
|
2386
|
+
if (index >= 0)
|
|
2387
|
+
return this.mailboxMessages.splice(index, 1)[0];
|
|
2388
|
+
return undefined;
|
|
2389
|
+
}
|
|
2390
|
+
releaseParkedMail(sessionKey, messageId) {
|
|
2303
2391
|
// The id is free at the recipient now: write back the other sender's message it refused while the id was held.
|
|
2304
2392
|
let released = false;
|
|
2305
2393
|
for (const parked of this.mailboxMessages) {
|
|
2306
|
-
if (parked.message.id !==
|
|
2394
|
+
if (parked.message.id !== messageId || parked.inFlightTo !== sessionKey)
|
|
2307
2395
|
continue;
|
|
2308
2396
|
if (parked.deferred !== "id_in_use")
|
|
2309
2397
|
continue;
|
|
@@ -2322,8 +2410,8 @@ export class CoordinationBroker {
|
|
|
2322
2410
|
console.error("Coordination broker failed to persist live delivery record:", error);
|
|
2323
2411
|
this.writeDeliveryFailure(socket, messageId, "Could not persist the delivery record; retry", "E_PERSIST_FAILED", true);
|
|
2324
2412
|
}
|
|
2325
|
-
writeDeliverySuccess(socket, messageId, delivery) {
|
|
2326
|
-
writeMessage(socket, { type: "delivered", messageId, delivery, retryable: false, outcomeKnown: true });
|
|
2413
|
+
writeDeliverySuccess(socket, messageId, delivery, recipient) {
|
|
2414
|
+
writeMessage(socket, { type: "delivered", messageId, delivery, retryable: false, outcomeKnown: true, recipient });
|
|
2327
2415
|
}
|
|
2328
2416
|
writeDeliveryFailure(socket, messageId, reason, code, retryable = false) {
|
|
2329
2417
|
writeMessage(socket, {
|
|
@@ -2348,7 +2436,7 @@ export class CoordinationBroker {
|
|
|
2348
2436
|
const written = this.wasWritten(entry);
|
|
2349
2437
|
if (!written || !target?.acknowledgesReceipts) {
|
|
2350
2438
|
this.releaseReplyEdge(entry);
|
|
2351
|
-
this.writeDeliverySuccess(socket, entry.message.id, written ? "socket_delivered" : "queued");
|
|
2439
|
+
this.writeDeliverySuccess(socket, entry.message.id, written ? "socket_delivered" : "queued", entry.target);
|
|
2352
2440
|
return;
|
|
2353
2441
|
}
|
|
2354
2442
|
const key = this.deliveryRecordKey(entry.fromKey, entry.message.id);
|
|
@@ -2363,25 +2451,19 @@ export class CoordinationBroker {
|
|
|
2363
2451
|
timer.unref();
|
|
2364
2452
|
this.pendingReceipts.set(key, { senderSocket: socket, targetSocket: target.socket, entry, timer });
|
|
2365
2453
|
}
|
|
2366
|
-
confirmPendingReceipt(
|
|
2367
|
-
const
|
|
2368
|
-
if (!entry)
|
|
2369
|
-
return;
|
|
2454
|
+
confirmPendingReceipt(entry, socket) {
|
|
2455
|
+
const messageId = entry.message.id;
|
|
2370
2456
|
const key = this.deliveryRecordKey(entry.fromKey, messageId);
|
|
2371
2457
|
const pending = this.pendingReceipts.get(key);
|
|
2372
2458
|
if (pending && pending.targetSocket !== socket)
|
|
2373
2459
|
return;
|
|
2374
|
-
if (this.deliveryRecords.get(key)?.state === "unknown") {
|
|
2375
|
-
this.updateDeliveryRecord(entry.fromKey, messageId, "socket_delivered");
|
|
2376
|
-
this.persistQuietly();
|
|
2377
|
-
}
|
|
2378
2460
|
this.releaseReplyEdge(entry);
|
|
2379
2461
|
if (!pending)
|
|
2380
2462
|
return;
|
|
2381
2463
|
clearTimeout(pending.timer);
|
|
2382
2464
|
this.pendingReceipts.delete(key);
|
|
2383
2465
|
if (pending.senderSocket.writable)
|
|
2384
|
-
this.writeDeliverySuccess(pending.senderSocket, messageId, "socket_delivered");
|
|
2466
|
+
this.writeDeliverySuccess(pending.senderSocket, messageId, "socket_delivered", entry.target);
|
|
2385
2467
|
}
|
|
2386
2468
|
failPendingReceipt(key, reason, code, delivery = "unknown") {
|
|
2387
2469
|
const pending = this.pendingReceipts.get(key);
|
|
@@ -2394,6 +2476,7 @@ export class CoordinationBroker {
|
|
|
2394
2476
|
writeMessage(pending.senderSocket, {
|
|
2395
2477
|
type: "delivery_failed",
|
|
2396
2478
|
messageId: pending.entry.message.id,
|
|
2479
|
+
recipient: pending.entry.target,
|
|
2397
2480
|
reason,
|
|
2398
2481
|
code,
|
|
2399
2482
|
delivery,
|
|
@@ -2465,7 +2548,7 @@ export class CoordinationBroker {
|
|
|
2465
2548
|
deliveryRecordKey(fromSessionId, messageId) {
|
|
2466
2549
|
return JSON.stringify([fromSessionId, messageId]);
|
|
2467
2550
|
}
|
|
2468
|
-
replayOrReject(socket, fromSessionId, messageId, fingerprint) {
|
|
2551
|
+
replayOrReject(socket, fromSessionId, messageId, fingerprint, recipient) {
|
|
2469
2552
|
this.pruneDeliveryRecords();
|
|
2470
2553
|
const key = this.deliveryRecordKey(fromSessionId, messageId);
|
|
2471
2554
|
const record = this.deliveryRecords.get(key);
|
|
@@ -2516,7 +2599,7 @@ export class CoordinationBroker {
|
|
|
2516
2599
|
return false;
|
|
2517
2600
|
}
|
|
2518
2601
|
if (record.state === "socket_delivered" || record.state === "queued") {
|
|
2519
|
-
this.writeDeliverySuccess(socket, messageId, record.state);
|
|
2602
|
+
this.writeDeliverySuccess(socket, messageId, record.state, recipient);
|
|
2520
2603
|
}
|
|
2521
2604
|
else {
|
|
2522
2605
|
this.writeDeliveryFailure(socket, messageId, record.reason ?? "Previous delivery failed", record.code ?? "E_DELIVERY_FAILED", record.retryable);
|
|
@@ -2798,7 +2881,7 @@ export class CoordinationBroker {
|
|
|
2798
2881
|
}
|
|
2799
2882
|
/**
|
|
2800
2883
|
* At most one side is non-empty. Exact addresses win, live before offline, so an offline session addressed by its
|
|
2801
|
-
* exact name or id keeps its mail;
|
|
2884
|
+
* exact name or id keeps its mail; live suffix handles follow, then live id or name prefixes (never the sender),
|
|
2802
2885
|
* then offline id prefixes.
|
|
2803
2886
|
*/
|
|
2804
2887
|
findTargets(nameOrId, scopeId, senderKey) {
|
|
@@ -2811,6 +2894,9 @@ export class CoordinationBroker {
|
|
|
2811
2894
|
const exactOffline = resolveExactTargets(offline, nameOrId, scopeId);
|
|
2812
2895
|
if (exactOffline.length)
|
|
2813
2896
|
return { live: [], offline: exactOffline };
|
|
2897
|
+
const handles = resolveHandleTargets(live, nameOrId);
|
|
2898
|
+
if (handles.length)
|
|
2899
|
+
return { live: handles, offline: [] };
|
|
2814
2900
|
const prefixed = resolvePrefixTargets(live.filter((session) => session.key !== senderKey), nameOrId, scopeId);
|
|
2815
2901
|
if (prefixed.length)
|
|
2816
2902
|
return { live: prefixed, offline: [] };
|