@humain/humain-code 0.8.0 → 0.8.1
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 +21 -0
- package/dist/bundle/chunks/{anthropic-messages-UZB5HJE3.js → anthropic-messages-XFCDMGD6.js} +4 -4
- package/dist/bundle/chunks/{azure-openai-responses-J3LCMWPQ.js → azure-openai-responses-F4KQM3NK.js} +3 -3
- package/dist/bundle/chunks/{chunk-7EONJCBE.js → chunk-2JNJJ6SZ.js} +1 -1
- package/dist/bundle/chunks/{chunk-WRYGY5XG.js → chunk-4MKATHT2.js} +1 -1
- package/dist/bundle/chunks/{chunk-ATFZAFMS.js → chunk-65FLSKDA.js} +1 -1
- package/dist/bundle/chunks/{chunk-CET2MXXN.js → chunk-6P6MMQUH.js} +1 -1
- package/dist/bundle/chunks/{chunk-CU6KU7JK.js → chunk-6TTMNQXI.js} +13 -13
- package/dist/bundle/chunks/{chunk-KQXXBQ57.js → chunk-6YVORXTX.js} +155 -125
- package/dist/bundle/chunks/{chunk-SQ7NTRLJ.js → chunk-7X6DNT45.js} +17 -16
- package/dist/bundle/chunks/{chunk-PGVDL4KB.js → chunk-CQ2CE3RB.js} +1 -1
- package/dist/bundle/chunks/{chunk-QRYDGY5I.js → chunk-DJG2WUNM.js} +1 -1
- package/dist/bundle/chunks/{chunk-R3VDXQW2.js → chunk-E7OBH44W.js} +5 -5
- package/dist/bundle/chunks/{chunk-4EV7CK26.js → chunk-F2H47FRF.js} +1 -1
- package/dist/bundle/chunks/chunk-FQ4KS5S4.js +23 -0
- package/dist/bundle/chunks/{chunk-TYN46EVI.js → chunk-GPLWWO5Q.js} +1 -1
- package/dist/bundle/chunks/{chunk-EMBIUIQ2.js → chunk-IMUFBWEK.js} +4 -4
- package/dist/bundle/chunks/{chunk-Z5UFSRO4.js → chunk-KI7CHPWC.js} +1 -1
- package/dist/bundle/chunks/{chunk-VKQNYZUA.js → chunk-KRBELWQH.js} +1 -1
- package/dist/bundle/chunks/{chunk-AI7EHDAX.js → chunk-LSRXA7OS.js} +1 -1
- package/dist/bundle/chunks/{chunk-XVIQYONJ.js → chunk-MIAXMK5F.js} +1 -1
- package/dist/bundle/chunks/{chunk-NNWNIBLG.js → chunk-NICPFMOP.js} +1 -1
- package/dist/bundle/chunks/{chunk-4SFWYKL5.js → chunk-P42E6YY6.js} +441 -415
- package/dist/bundle/chunks/{chunk-WGXMYMPE.js → chunk-PFBAIRCY.js} +2 -2
- package/dist/bundle/chunks/{chunk-OVDMYBIT.js → chunk-PWFM6AQQ.js} +1 -1
- package/dist/bundle/chunks/{chunk-JV7VHOAY.js → chunk-QRRVZYTG.js} +1 -1
- package/dist/bundle/chunks/{chunk-IOLLZXQN.js → chunk-ROFZVEFN.js} +125 -114
- package/dist/bundle/chunks/{chunk-C4DHVDYT.js → chunk-SXOIQWNX.js} +1 -1
- package/dist/bundle/chunks/{chunk-IXQYPQRQ.js → chunk-XZ7FTBXS.js} +1446 -1423
- package/dist/bundle/chunks/{cloudflare-workers-ai-system-one-ETKC6B2S.js → cloudflare-workers-ai-system-one-JEZVYITT.js} +1 -1
- package/dist/bundle/chunks/{compat-HS4ARZK3.js → compat-SRCNS6OQ.js} +6 -6
- package/dist/bundle/chunks/{curator-server-TC7RVVN2.js → curator-server-QB4GITGS.js} +1 -1
- package/dist/bundle/chunks/{dist-5AQPJ2TI.js → dist-4OMLHRJE.js} +1 -1
- package/dist/bundle/chunks/{dist-NMEXYUP3.js → dist-CBODG4CA.js} +5 -5
- package/dist/bundle/chunks/{dist-DYB3AR7C.js → dist-TRAK7EXI.js} +7 -7
- package/dist/bundle/chunks/{extract-QHZTEVJP.js → extract-L373D6O7.js} +2 -2
- package/dist/bundle/chunks/{flow-implementer-QMN3HKE2.js → flow-implementer-4KO3Q4B5.js} +4 -4
- package/dist/bundle/chunks/github-copilot.js +53 -56
- package/dist/bundle/chunks/{google-generative-ai-ZND73WDQ.js → google-generative-ai-WIASMWS3.js} +2 -2
- package/dist/bundle/chunks/{google-vertex-YAH62YRM.js → google-vertex-WSPHRZIZ.js} +3 -3
- package/dist/bundle/chunks/{host-Q565BCXB.js → host-WOLZCWQQ.js} +1 -1
- package/dist/bundle/chunks/{inprocess-453SLFL5.js → inprocess-TOBRVK4C.js} +5 -5
- package/dist/bundle/chunks/{interactive-mode-MVZOJ5OU.js → interactive-mode-BJJKPBXD.js} +3 -3
- package/dist/bundle/chunks/{llama-cpp-classify-Z4GZY4GC.js → llama-cpp-classify-KYZT532X.js} +1 -1
- package/dist/bundle/chunks/{mistral-conversations-7I5JURDP.js → mistral-conversations-7JYDDRUT.js} +2 -2
- package/dist/bundle/chunks/{node-IMSYVRPL.js → node-N7TPBUJB.js} +1 -1
- package/dist/bundle/chunks/{openai-codex-responses-LTHOMN37.js → openai-codex-responses-7PAML4NF.js} +3 -3
- package/dist/bundle/chunks/{openai-completions-W2X635EE.js → openai-completions-UJDAIGTK.js} +3 -3
- package/dist/bundle/chunks/{openai-decisions-TN2GNOYI.js → openai-decisions-JMSDGR7K.js} +1 -1
- package/dist/bundle/chunks/{openai-responses-DVY4QXOG.js → openai-responses-2WNCE6AY.js} +3 -3
- package/dist/bundle/chunks/{openrouter-images-SE2JMS44.js → openrouter-images-V3UCCO6X.js} +1 -1
- package/dist/bundle/chunks/{pi-messages-WAS6Z5L5.js → pi-messages-TQ7LJUNQ.js} +2 -2
- package/dist/bundle/chunks/{session-picker-XNVLSJ22.js → session-picker-4JQEGLDD.js} +1 -1
- package/dist/bundle/chunks/{startup-ui-3TCT465D.js → startup-ui-AKTA4NDP.js} +1 -1
- package/dist/bundle/chunks/{typesafe-system-one-AAZH6S3I.js → typesafe-system-one-YP7GGN4C.js} +1 -1
- package/dist/bundle/chunks/{virtual-modules-CIVXCEAN.js → virtual-modules-3CT5BMBA.js} +8 -8
- package/dist/bundle/cli-runtime.js +5 -5
- package/dist/bundle/index.js +8 -8
- package/dist/bundle/rpc-entry.js +4 -4
- package/dist/cli/args.js +1 -1
- package/dist/cli/args.js.map +1 -1
- package/dist/cli/list-models.d.ts.map +1 -1
- package/dist/cli/list-models.js +16 -1
- package/dist/cli/list-models.js.map +1 -1
- package/dist/cli/setup.d.ts +1 -1
- package/dist/cli/setup.d.ts.map +1 -1
- package/dist/cli/setup.js +3 -2
- package/dist/cli/setup.js.map +1 -1
- package/dist/cli.js.map +1 -1
- package/dist/core/agent-session-runtime.d.ts +3 -0
- package/dist/core/agent-session-runtime.d.ts.map +1 -1
- package/dist/core/agent-session-runtime.js +11 -2
- package/dist/core/agent-session-runtime.js.map +1 -1
- package/dist/core/keybindings.d.ts +24 -0
- package/dist/core/keybindings.d.ts.map +1 -1
- package/dist/core/keybindings.js +3 -0
- package/dist/core/keybindings.js.map +1 -1
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +12 -1
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/model-resolver.d.ts.map +1 -1
- package/dist/core/model-resolver.js +11 -7
- package/dist/core/model-resolver.js.map +1 -1
- package/dist/core/model-runtime.d.ts +11 -2
- package/dist/core/model-runtime.d.ts.map +1 -1
- package/dist/core/model-runtime.js +23 -6
- package/dist/core/model-runtime.js.map +1 -1
- package/dist/core/oauth-account-pool.d.ts +4 -0
- package/dist/core/oauth-account-pool.d.ts.map +1 -1
- package/dist/core/oauth-account-pool.js +104 -20
- package/dist/core/oauth-account-pool.js.map +1 -1
- package/dist/core/sdk.js +2 -2
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/slash-commands.d.ts.map +1 -1
- package/dist/core/slash-commands.js +1 -0
- package/dist/core/slash-commands.js.map +1 -1
- package/dist/extensions/usage/confirmation.d.ts.map +1 -1
- package/dist/extensions/usage/confirmation.js +1 -1
- package/dist/extensions/usage/confirmation.js.map +1 -1
- package/dist/extensions/usage/index.js +1 -1
- package/dist/extensions/usage/index.js.map +1 -1
- package/dist/extensions/usage/panes.d.ts +5 -0
- package/dist/extensions/usage/panes.d.ts.map +1 -0
- package/dist/extensions/usage/panes.js +10 -0
- package/dist/extensions/usage/panes.js.map +1 -0
- package/dist/extensions/usage/popup.d.ts.map +1 -1
- package/dist/extensions/usage/popup.js +9 -18
- package/dist/extensions/usage/popup.js.map +1 -1
- package/dist/humain/hcode/contracts.d.ts +15 -0
- package/dist/humain/hcode/contracts.d.ts.map +1 -1
- package/dist/humain/hcode/contracts.js +85 -10
- package/dist/humain/hcode/contracts.js.map +1 -1
- package/dist/humain/hcode/environment.d.ts.map +1 -1
- package/dist/humain/hcode/environment.js +4 -2
- package/dist/humain/hcode/environment.js.map +1 -1
- package/dist/humain/hcode/images.d.ts.map +1 -1
- package/dist/humain/hcode/images.js +4 -2
- package/dist/humain/hcode/images.js.map +1 -1
- package/dist/humain/hcode/login-copy.js +1 -1
- package/dist/humain/hcode/login-copy.js.map +1 -1
- package/dist/humain/hcode/notices.d.ts +22 -1
- package/dist/humain/hcode/notices.d.ts.map +1 -1
- package/dist/humain/hcode/notices.js +34 -3
- package/dist/humain/hcode/notices.js.map +1 -1
- package/dist/humain/hcode/provider.d.ts +3 -1
- package/dist/humain/hcode/provider.d.ts.map +1 -1
- package/dist/humain/hcode/provider.js +126 -46
- package/dist/humain/hcode/provider.js.map +1 -1
- package/dist/humain/hcode/run-credential.d.ts +21 -0
- package/dist/humain/hcode/run-credential.d.ts.map +1 -0
- package/dist/humain/hcode/run-credential.js +140 -0
- package/dist/humain/hcode/run-credential.js.map +1 -0
- package/dist/humain/hcode/sandbox.d.ts +6 -0
- package/dist/humain/hcode/sandbox.d.ts.map +1 -0
- package/dist/humain/hcode/sandbox.js +139 -0
- package/dist/humain/hcode/sandbox.js.map +1 -0
- package/dist/humain/hcode/selection.d.ts +7 -2
- package/dist/humain/hcode/selection.d.ts.map +1 -1
- package/dist/humain/hcode/selection.js +29 -5
- package/dist/humain/hcode/selection.js.map +1 -1
- package/dist/humain/hcode/transport.d.ts +3 -2
- package/dist/humain/hcode/transport.d.ts.map +1 -1
- package/dist/humain/hcode/transport.js +22 -7
- package/dist/humain/hcode/transport.js.map +1 -1
- package/dist/humain/remote/broker.d.ts +84 -0
- package/dist/humain/remote/broker.d.ts.map +1 -0
- package/dist/humain/remote/broker.js +427 -0
- package/dist/humain/remote/broker.js.map +1 -0
- package/dist/humain/remote/deny-store.d.ts +12 -0
- package/dist/humain/remote/deny-store.d.ts.map +1 -0
- package/dist/humain/remote/deny-store.js +164 -0
- package/dist/humain/remote/deny-store.js.map +1 -0
- package/dist/humain/remote/enablement.d.ts +5 -0
- package/dist/humain/remote/enablement.d.ts.map +1 -0
- package/dist/humain/remote/enablement.js +8 -0
- package/dist/humain/remote/enablement.js.map +1 -0
- package/dist/humain/remote/environment.d.ts +11 -0
- package/dist/humain/remote/environment.d.ts.map +1 -0
- package/dist/humain/remote/environment.js +19 -0
- package/dist/humain/remote/environment.js.map +1 -0
- package/dist/humain/remote/host-watch.d.ts +21 -0
- package/dist/humain/remote/host-watch.d.ts.map +1 -0
- package/dist/humain/remote/host-watch.js +186 -0
- package/dist/humain/remote/host-watch.js.map +1 -0
- package/dist/humain/remote/ipc-posix.d.ts +5 -0
- package/dist/humain/remote/ipc-posix.d.ts.map +1 -0
- package/dist/humain/remote/ipc-posix.js +29 -0
- package/dist/humain/remote/ipc-posix.js.map +1 -0
- package/dist/humain/remote/ipc-windows.d.ts +3 -0
- package/dist/humain/remote/ipc-windows.d.ts.map +1 -0
- package/dist/humain/remote/ipc-windows.js +5 -0
- package/dist/humain/remote/ipc-windows.js.map +1 -0
- package/dist/humain/remote/ipc.d.ts +10 -0
- package/dist/humain/remote/ipc.d.ts.map +1 -0
- package/dist/humain/remote/ipc.js +19 -0
- package/dist/humain/remote/ipc.js.map +1 -0
- package/dist/humain/remote/protocol.d.ts +43 -0
- package/dist/humain/remote/protocol.d.ts.map +1 -0
- package/dist/humain/remote/protocol.js +76 -0
- package/dist/humain/remote/protocol.js.map +1 -0
- package/dist/humain/remote/relay-socket.d.ts +30 -0
- package/dist/humain/remote/relay-socket.d.ts.map +1 -0
- package/dist/humain/remote/relay-socket.js +137 -0
- package/dist/humain/remote/relay-socket.js.map +1 -0
- package/dist/humain/remote/secure-channel.d.ts +10 -0
- package/dist/humain/remote/secure-channel.d.ts.map +1 -0
- package/dist/humain/remote/secure-channel.js +5 -0
- package/dist/humain/remote/secure-channel.js.map +1 -0
- package/dist/humain/remote/terminal.d.ts +48 -0
- package/dist/humain/remote/terminal.d.ts.map +1 -0
- package/dist/humain/remote/terminal.js +119 -0
- package/dist/humain/remote/terminal.js.map +1 -0
- package/dist/humain/remote/tickets.d.ts +35 -0
- package/dist/humain/remote/tickets.d.ts.map +1 -0
- package/dist/humain/remote/tickets.js +132 -0
- package/dist/humain/remote/tickets.js.map +1 -0
- package/dist/main.d.ts +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +13 -1
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/account-picker.d.ts +31 -0
- package/dist/modes/interactive/components/account-picker.d.ts.map +1 -0
- package/dist/modes/interactive/components/account-picker.js +150 -0
- package/dist/modes/interactive/components/account-picker.js.map +1 -0
- package/dist/modes/interactive/interactive-mode.d.ts +15 -2
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +225 -36
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/oauth-account-format.d.ts +9 -0
- package/dist/modes/interactive/oauth-account-format.d.ts.map +1 -1
- package/dist/modes/interactive/oauth-account-format.js +31 -22
- package/dist/modes/interactive/oauth-account-format.js.map +1 -1
- package/docs/cli.md +1 -1
- package/docs/forge-run-bootstrap.md +46 -0
- package/docs/humain-code.md +2 -2
- package/docs/index.md +2 -1
- package/docs/keybindings.md +10 -0
- package/docs/providers.md +2 -2
- package/docs/quickstart.md +16 -2
- package/docs/slash-commands.md +4 -2
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/schemas/keybindings.schema.json +12 -0
- package/dist/bundle/chunks/chunk-J5TSAJGC.js +0 -22
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { addAbortListener } from "node:events";
|
|
3
|
+
import { performance } from "node:perf_hooks";
|
|
4
|
+
import { raceWithAbortSignal } from "../../utils/abort.js";
|
|
5
|
+
import { streamWatch } from "./host-watch.js";
|
|
6
|
+
const DAY = 86_400_000;
|
|
7
|
+
const CONSENT_MS = 120_000;
|
|
8
|
+
const VIEW_MS = 300_000;
|
|
9
|
+
const LEASE_MS = 30_000;
|
|
10
|
+
export const MAX_WATCH_DENIES = 4096;
|
|
11
|
+
const principalFields = ["issuer", "subject", "accountId", "tenantId", "environment"];
|
|
12
|
+
const routeFields = ["installId", "brokerEpoch", "sessionId", "attachmentId", "channelEpoch"];
|
|
13
|
+
const candidateFields = [
|
|
14
|
+
"pendingId",
|
|
15
|
+
"offerId",
|
|
16
|
+
"hostGeneration",
|
|
17
|
+
"browserGeneration",
|
|
18
|
+
"installKey",
|
|
19
|
+
"deviceKey",
|
|
20
|
+
"transcript",
|
|
21
|
+
"credential",
|
|
22
|
+
"mode",
|
|
23
|
+
"expiresAt",
|
|
24
|
+
];
|
|
25
|
+
function samePrincipal(a, b) {
|
|
26
|
+
return principalFields.every((key) => a[key] === b[key]);
|
|
27
|
+
}
|
|
28
|
+
function sameRoute(a, b) {
|
|
29
|
+
return routeFields.every((key) => a[key] === b[key]);
|
|
30
|
+
}
|
|
31
|
+
function exactCandidate(a, b) {
|
|
32
|
+
return (sameRoute(a.route, b.route) &&
|
|
33
|
+
samePrincipal(a.principal, b.principal) &&
|
|
34
|
+
candidateFields.every((key) => a[key] === b[key]));
|
|
35
|
+
}
|
|
36
|
+
/** Install-local watch fences. No socket, user input, agent or provider capability is accepted here. */
|
|
37
|
+
export class WatchBroker {
|
|
38
|
+
#options;
|
|
39
|
+
#now;
|
|
40
|
+
#epoch = randomUUID();
|
|
41
|
+
#sessions = new Map();
|
|
42
|
+
#sessionIds = new WeakMap();
|
|
43
|
+
#routes = new Map();
|
|
44
|
+
#pending = new Map();
|
|
45
|
+
#usedOffers = new Map();
|
|
46
|
+
#approved = new Map();
|
|
47
|
+
#active = new Map();
|
|
48
|
+
#denied = new Set();
|
|
49
|
+
#startup;
|
|
50
|
+
#shutdown;
|
|
51
|
+
#stopAccountWatch;
|
|
52
|
+
#persist = Promise.resolve();
|
|
53
|
+
#storeOpen = false;
|
|
54
|
+
#reconciling = 0;
|
|
55
|
+
#rendezvousAllowed = false;
|
|
56
|
+
#closed = false;
|
|
57
|
+
constructor(options) {
|
|
58
|
+
this.#options = { ...options, principal: { ...options.principal }, reference: { ...options.reference } };
|
|
59
|
+
this.#now = options.now ?? (() => performance.now());
|
|
60
|
+
}
|
|
61
|
+
start() {
|
|
62
|
+
if (this.#closed)
|
|
63
|
+
return Promise.reject(new Error("Remote unavailable"));
|
|
64
|
+
this.#startup ??= this.#restore();
|
|
65
|
+
return this.#startup;
|
|
66
|
+
}
|
|
67
|
+
async #restore() {
|
|
68
|
+
try {
|
|
69
|
+
const { installId, principal, store, account, reference, hostGeneration } = this.#options;
|
|
70
|
+
if (principal.environment !== reference.environmentId ||
|
|
71
|
+
principal.accountId !== reference.accountId ||
|
|
72
|
+
!hostGeneration) {
|
|
73
|
+
throw new Error("Remote account scope mismatch");
|
|
74
|
+
}
|
|
75
|
+
this.#stopAccountWatch = bindWatchAccount(this, account, reference);
|
|
76
|
+
if (this.#closed)
|
|
77
|
+
throw new Error("Remote account unavailable");
|
|
78
|
+
const stored = await store.read({ installId, principal });
|
|
79
|
+
if (!Array.isArray(stored) || stored.length > MAX_WATCH_DENIES || stored.some((id) => !opaqueId(id))) {
|
|
80
|
+
throw new Error();
|
|
81
|
+
}
|
|
82
|
+
this.#storeOpen = true;
|
|
83
|
+
if (this.#closed)
|
|
84
|
+
throw new Error();
|
|
85
|
+
for (const id of stored)
|
|
86
|
+
this.#denied.add(id);
|
|
87
|
+
if (this.#denied.size)
|
|
88
|
+
await this.#options.authority.reconcile([...this.#denied]);
|
|
89
|
+
if (this.#closed)
|
|
90
|
+
throw new Error();
|
|
91
|
+
this.#rendezvousAllowed = true;
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
this.close();
|
|
95
|
+
throw new Error("Remote unavailable: account or deny state could not be verified and reconciled");
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** Bind the share to the owning terminal's notice, consent and session lifetime. */
|
|
99
|
+
shareSession(source, notify, lifetime, confirm, onUnshare) {
|
|
100
|
+
if (!this.#rendezvousAllowed ||
|
|
101
|
+
this.#reconciling ||
|
|
102
|
+
typeof notify !== "function" ||
|
|
103
|
+
typeof confirm !== "function" ||
|
|
104
|
+
!(lifetime instanceof AbortSignal) ||
|
|
105
|
+
lifetime.aborted) {
|
|
106
|
+
throw new Error("Remote unavailable");
|
|
107
|
+
}
|
|
108
|
+
const existing = this.#sessionIds.get(source);
|
|
109
|
+
const share = existing && this.#sessions.get(existing);
|
|
110
|
+
if (existing && share) {
|
|
111
|
+
if (share.lifetime !== lifetime)
|
|
112
|
+
throw new Error("Remote unavailable");
|
|
113
|
+
return existing;
|
|
114
|
+
}
|
|
115
|
+
if (this.#sessions.size >= 32)
|
|
116
|
+
throw new Error("Remote share limit reached");
|
|
117
|
+
const id = randomUUID();
|
|
118
|
+
const stopListening = addAbortListener(lifetime, () => this.unshare(id));
|
|
119
|
+
this.#sessions.set(id, { source, notify, confirm, lifetime, stopListening, onUnshare });
|
|
120
|
+
this.#sessionIds.set(source, id);
|
|
121
|
+
return id;
|
|
122
|
+
}
|
|
123
|
+
routeFor(sessionId) {
|
|
124
|
+
if (!this.#rendezvousAllowed || this.#reconciling || this.#sessions.get(sessionId)?.lifetime.aborted !== false) {
|
|
125
|
+
throw new Error("Remote share unavailable");
|
|
126
|
+
}
|
|
127
|
+
const now = this.#now();
|
|
128
|
+
for (const [id, value] of this.#routes) {
|
|
129
|
+
if (value.until <= now) {
|
|
130
|
+
this.#routes.delete(id);
|
|
131
|
+
this.#approved.delete(id);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (this.#routes.size >= 16)
|
|
135
|
+
throw new Error("Remote attachment setup limit reached");
|
|
136
|
+
const route = Object.freeze({
|
|
137
|
+
installId: this.#options.installId,
|
|
138
|
+
brokerEpoch: this.#epoch,
|
|
139
|
+
sessionId,
|
|
140
|
+
attachmentId: randomUUID(),
|
|
141
|
+
channelEpoch: randomUUID(),
|
|
142
|
+
});
|
|
143
|
+
this.#routes.set(route.attachmentId, { route, until: now + VIEW_MS });
|
|
144
|
+
return route;
|
|
145
|
+
}
|
|
146
|
+
unshare(sessionId) {
|
|
147
|
+
const share = this.#sessions.get(sessionId);
|
|
148
|
+
this.#sessions.delete(sessionId);
|
|
149
|
+
share?.stopListening[Symbol.dispose]();
|
|
150
|
+
for (const [id, setup] of this.#routes) {
|
|
151
|
+
if (setup.route.sessionId === sessionId) {
|
|
152
|
+
this.#routes.delete(id);
|
|
153
|
+
this.#approved.delete(id);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
for (const entry of this.#pending.values())
|
|
157
|
+
if (entry.sessionId === sessionId)
|
|
158
|
+
entry.abort.abort();
|
|
159
|
+
for (const attachment of this.#active.values())
|
|
160
|
+
if (attachment.sessionId === sessionId)
|
|
161
|
+
attachment.close();
|
|
162
|
+
try {
|
|
163
|
+
share?.onUnshare?.();
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
// Terminal cleanup cannot interrupt broker shutdown or account revocation.
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/** Trusted ingress requests consent from the share's terminal; it cannot supply the decision callback. */
|
|
170
|
+
async confirmPairing(candidate, setupSignal) {
|
|
171
|
+
const exact = Object.freeze({
|
|
172
|
+
...candidate,
|
|
173
|
+
route: Object.freeze({ ...candidate.route }),
|
|
174
|
+
principal: Object.freeze({ ...candidate.principal }),
|
|
175
|
+
});
|
|
176
|
+
const share = this.#sessions.get(exact.route.sessionId);
|
|
177
|
+
const now = this.#now();
|
|
178
|
+
for (const [id, until] of this.#usedOffers)
|
|
179
|
+
if (until <= now)
|
|
180
|
+
this.#usedOffers.delete(id);
|
|
181
|
+
if (!share ||
|
|
182
|
+
!(setupSignal instanceof AbortSignal) ||
|
|
183
|
+
setupSignal.aborted ||
|
|
184
|
+
!this.#currentSetup(exact.route) ||
|
|
185
|
+
!samePrincipal(exact.principal, this.#options.principal) ||
|
|
186
|
+
exact.hostGeneration !== this.#options.hostGeneration ||
|
|
187
|
+
this.#pending.size >= 8 ||
|
|
188
|
+
this.#usedOffers.size >= 32 ||
|
|
189
|
+
this.#usedOffers.has(exact.offerId) ||
|
|
190
|
+
this.#pending.has(exact.pendingId) ||
|
|
191
|
+
!(exact.expiresAt > now && exact.expiresAt <= now + CONSENT_MS) ||
|
|
192
|
+
(exact.mode === "uv" ? !exact.credential : exact.mode !== "short" || exact.credential !== null))
|
|
193
|
+
return false;
|
|
194
|
+
const abort = new AbortController();
|
|
195
|
+
const timer = setTimeout(() => abort.abort(), exact.expiresAt - now);
|
|
196
|
+
timer.unref();
|
|
197
|
+
this.#usedOffers.set(exact.offerId, now + VIEW_MS);
|
|
198
|
+
this.#pending.set(exact.pendingId, { sessionId: exact.route.sessionId, abort });
|
|
199
|
+
const signal = AbortSignal.any([abort.signal, setupSignal, share.lifetime]);
|
|
200
|
+
const current = () => !signal.aborted && this.#currentSetup(exact.route) && this.#now() < exact.expiresAt;
|
|
201
|
+
try {
|
|
202
|
+
if ((await raceWithAbortSignal(share.confirm(exact, signal), signal)) !== "allow" || !current())
|
|
203
|
+
return false;
|
|
204
|
+
const commit = await raceWithAbortSignal(this.#options.authority.commit(exact, signal), signal);
|
|
205
|
+
if (!current() ||
|
|
206
|
+
!commit ||
|
|
207
|
+
!exactCandidate(commit.candidate, exact) ||
|
|
208
|
+
!opaqueId(commit.pairingId) ||
|
|
209
|
+
this.#denied.has(commit.pairingId) ||
|
|
210
|
+
!(commit.pairedAt >= now && commit.pairedAt <= this.#now()))
|
|
211
|
+
return false;
|
|
212
|
+
this.#approved.set(exact.route.attachmentId, {
|
|
213
|
+
candidate: exact,
|
|
214
|
+
pairingId: commit.pairingId,
|
|
215
|
+
pairedAt: commit.pairedAt,
|
|
216
|
+
setupSignal,
|
|
217
|
+
});
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
finally {
|
|
224
|
+
clearTimeout(timer);
|
|
225
|
+
this.#pending.delete(exact.pendingId);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
attachWatch(route, peer, channel) {
|
|
229
|
+
const share = this.#sessions.get(route.sessionId);
|
|
230
|
+
const approved = this.#approved.get(route.attachmentId);
|
|
231
|
+
const durable = peer.reconnect ? peer.durable : approved;
|
|
232
|
+
const now = this.#now();
|
|
233
|
+
const absoluteCutoff = Math.min(peer.candidate.mode === "uv" ? peer.verifiedAt + VIEW_MS : peer.pairedAt + VIEW_MS, peer.pairedAt + 30 * DAY);
|
|
234
|
+
let cutoff = Math.min(peer.leaseUntil, absoluteCutoff);
|
|
235
|
+
if (channel.signal.aborted ||
|
|
236
|
+
!share ||
|
|
237
|
+
!this.#currentSetup(route) ||
|
|
238
|
+
!sameRoute(peer.candidate.route, route) ||
|
|
239
|
+
!samePrincipal(peer.candidate.principal, this.#options.principal) ||
|
|
240
|
+
peer.candidate.hostGeneration !== this.#options.hostGeneration ||
|
|
241
|
+
!durable ||
|
|
242
|
+
durable.candidate.hostGeneration !== this.#options.hostGeneration ||
|
|
243
|
+
!samePairing(durable, peer) ||
|
|
244
|
+
(!peer.reconnect && (approved?.setupSignal.aborted || !exactCandidate(durable.candidate, peer.candidate))) ||
|
|
245
|
+
(peer.reconnect && (peer.candidate.mode !== "uv" || durable.candidate.mode !== "uv")) ||
|
|
246
|
+
this.#denied.has(peer.pairingId) ||
|
|
247
|
+
this.#active.size >= 8 ||
|
|
248
|
+
!(peer.pairedAt <= now && peer.verifiedAt <= now && cutoff > now && peer.leaseUntil <= now + LEASE_MS)) {
|
|
249
|
+
channel.close();
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
252
|
+
const pinnedRoute = this.#routes.get(route.attachmentId).route;
|
|
253
|
+
const { attachmentId, sessionId } = pinnedRoute;
|
|
254
|
+
const { pairingId, reconnect } = peer;
|
|
255
|
+
this.#routes.delete(attachmentId);
|
|
256
|
+
this.#approved.delete(attachmentId);
|
|
257
|
+
let watch;
|
|
258
|
+
let streamStarted = false;
|
|
259
|
+
const close = () => {
|
|
260
|
+
if (!this.#active.delete(attachmentId))
|
|
261
|
+
return;
|
|
262
|
+
clearTimeout(timer);
|
|
263
|
+
try {
|
|
264
|
+
if (streamStarted)
|
|
265
|
+
watch?.close();
|
|
266
|
+
else
|
|
267
|
+
channel.close();
|
|
268
|
+
}
|
|
269
|
+
catch {
|
|
270
|
+
// Delivery is already fenced; adapter cleanup cannot interrupt revocation or shutdown.
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
let timer = setTimeout(close, cutoff - now).unref();
|
|
274
|
+
const isLive = (at) => !channel.signal.aborted &&
|
|
275
|
+
!share.lifetime.aborted &&
|
|
276
|
+
this.#rendezvousAllowed &&
|
|
277
|
+
this.#active.has(attachmentId) &&
|
|
278
|
+
!this.#denied.has(pairingId) &&
|
|
279
|
+
cutoff > at;
|
|
280
|
+
this.#active.set(attachmentId, { sessionId, pairingId, close });
|
|
281
|
+
try {
|
|
282
|
+
// The terminal must be able to announce presence before watch data is released.
|
|
283
|
+
share.notify(pairingId, reconnect);
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
close();
|
|
287
|
+
return undefined;
|
|
288
|
+
}
|
|
289
|
+
if (!this.#active.has(attachmentId))
|
|
290
|
+
return undefined;
|
|
291
|
+
streamStarted = true;
|
|
292
|
+
watch = streamWatch(share.source, () => isLive(this.#now()), channel, close);
|
|
293
|
+
if (!this.#active.has(attachmentId)) {
|
|
294
|
+
// Startup can reenter teardown before the broker owns the stream handle.
|
|
295
|
+
watch.close();
|
|
296
|
+
return undefined;
|
|
297
|
+
}
|
|
298
|
+
return {
|
|
299
|
+
close,
|
|
300
|
+
renewLease: (grantRoute, grantPairingId, leaseUntil) => {
|
|
301
|
+
if (!sameRoute(pinnedRoute, grantRoute) || grantPairingId !== pairingId)
|
|
302
|
+
return false;
|
|
303
|
+
const at = this.#now();
|
|
304
|
+
if (!isLive(at) || leaseUntil <= at) {
|
|
305
|
+
close();
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
if (!(leaseUntil <= at + LEASE_MS))
|
|
309
|
+
return false;
|
|
310
|
+
cutoff = Math.min(leaseUntil, absoluteCutoff);
|
|
311
|
+
clearTimeout(timer);
|
|
312
|
+
timer = setTimeout(close, cutoff - at).unref();
|
|
313
|
+
return true;
|
|
314
|
+
},
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
#currentSetup(route) {
|
|
318
|
+
const setup = this.#routes.get(route.attachmentId);
|
|
319
|
+
return (this.#rendezvousAllowed &&
|
|
320
|
+
!this.#reconciling &&
|
|
321
|
+
!!setup &&
|
|
322
|
+
this.#sessions.get(setup.route.sessionId)?.lifetime.aborted === false &&
|
|
323
|
+
setup.until > this.#now() &&
|
|
324
|
+
sameRoute(route, setup.route));
|
|
325
|
+
}
|
|
326
|
+
async revokePairing(id) {
|
|
327
|
+
if (!opaqueId(id) || !this.#rendezvousAllowed)
|
|
328
|
+
throw new Error("Remote revoke unavailable");
|
|
329
|
+
this.#reconciling++;
|
|
330
|
+
this.#denied.add(id);
|
|
331
|
+
// Queue durability before invoking any callbacks that could shut down the broker.
|
|
332
|
+
const saved = this.#persist.then(async () => {
|
|
333
|
+
if (this.#denied.size > MAX_WATCH_DENIES)
|
|
334
|
+
throw new Error();
|
|
335
|
+
await this.#options.store.save([...this.#denied]);
|
|
336
|
+
});
|
|
337
|
+
this.#persist = saved;
|
|
338
|
+
try {
|
|
339
|
+
for (const attachment of this.#active.values())
|
|
340
|
+
if (attachment.pairingId === id)
|
|
341
|
+
attachment.close();
|
|
342
|
+
for (const pending of this.#pending.values())
|
|
343
|
+
pending.abort.abort();
|
|
344
|
+
try {
|
|
345
|
+
await saved;
|
|
346
|
+
}
|
|
347
|
+
catch {
|
|
348
|
+
this.close();
|
|
349
|
+
throw new Error("Remote disabled: revoke could not be saved durably");
|
|
350
|
+
}
|
|
351
|
+
if (this.#closed)
|
|
352
|
+
return; // A clean restart reconciles the durable deny before sharing.
|
|
353
|
+
try {
|
|
354
|
+
await this.#options.authority.reconcile([id]);
|
|
355
|
+
}
|
|
356
|
+
catch {
|
|
357
|
+
this.#rendezvousAllowed = false;
|
|
358
|
+
this.#detachAll();
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
finally {
|
|
362
|
+
this.#reconciling--;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
/** Fence immediately, then release the restart guard only if local persistence succeeded. */
|
|
366
|
+
shutdown() {
|
|
367
|
+
this.close();
|
|
368
|
+
this.#shutdown ??= this.#releaseStore();
|
|
369
|
+
return this.#shutdown;
|
|
370
|
+
}
|
|
371
|
+
async #releaseStore() {
|
|
372
|
+
await this.#startup?.catch(() => { });
|
|
373
|
+
await this.#persist;
|
|
374
|
+
if (this.#storeOpen)
|
|
375
|
+
await this.#options.store.release();
|
|
376
|
+
}
|
|
377
|
+
close() {
|
|
378
|
+
this.#closed = true;
|
|
379
|
+
this.#rendezvousAllowed = false;
|
|
380
|
+
const unsubscribe = this.#stopAccountWatch;
|
|
381
|
+
this.#stopAccountWatch = undefined;
|
|
382
|
+
this.#detachAll();
|
|
383
|
+
try {
|
|
384
|
+
unsubscribe?.();
|
|
385
|
+
}
|
|
386
|
+
catch {
|
|
387
|
+
// Delivery is already fenced; account observer cleanup must not strand the durable deny store.
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
#detachAll() {
|
|
391
|
+
for (const id of this.#sessions.keys())
|
|
392
|
+
this.unshare(id);
|
|
393
|
+
this.#approved.clear();
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
/** A live broker is pinned to one account generation; later login needs a new broker and trust reconciliation. */
|
|
397
|
+
export function bindWatchAccount(broker, account, reference) {
|
|
398
|
+
const { environmentId, accountId, generation } = reference;
|
|
399
|
+
const check = (current) => {
|
|
400
|
+
if (current?.environmentId !== environmentId ||
|
|
401
|
+
current.accountId !== accountId ||
|
|
402
|
+
current.generation !== generation)
|
|
403
|
+
broker.close();
|
|
404
|
+
};
|
|
405
|
+
const unsubscribe = account.subscribe(check);
|
|
406
|
+
try {
|
|
407
|
+
check(account.status(environmentId));
|
|
408
|
+
return unsubscribe;
|
|
409
|
+
}
|
|
410
|
+
catch (error) {
|
|
411
|
+
unsubscribe();
|
|
412
|
+
throw error;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
export function opaqueId(id) {
|
|
416
|
+
return typeof id === "string" && /^[A-Za-z0-9_-]{1,128}$/.test(id);
|
|
417
|
+
}
|
|
418
|
+
function samePairing(a, b) {
|
|
419
|
+
return (a.pairingId === b.pairingId &&
|
|
420
|
+
a.pairedAt === b.pairedAt &&
|
|
421
|
+
samePrincipal(a.candidate.principal, b.candidate.principal) &&
|
|
422
|
+
a.candidate.route.installId === b.candidate.route.installId &&
|
|
423
|
+
a.candidate.installKey === b.candidate.installKey &&
|
|
424
|
+
a.candidate.deviceKey === b.candidate.deviceKey &&
|
|
425
|
+
a.candidate.credential === b.candidate.credential);
|
|
426
|
+
}
|
|
427
|
+
//# sourceMappingURL=broker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"broker.js","sourceRoot":"","sources":["../../../src/humain/remote/broker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;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AC;IACF,MAAM,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC7C,IAAI,CAAC;QACJ,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC;QACrC,OAAO,WAAW,CAAC;IACpB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,WAAW,EAAE,CAAC;QACd,MAAM,KAAK,CAAC;IACb,CAAC;AACF,CAAC;AAED,MAAM,UAAU,QAAQ,CAAC,EAAW;IACnC,OAAO,OAAO,EAAE,KAAK,QAAQ,IAAI,wBAAwB,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACpE,CAAC;AAED,SAAS,WAAW,CAAC,CAAmB,EAAE,CAAkB;IAC3D,OAAO,CACN,CAAC,CAAC,SAAS,KAAK,CAAC,CAAC,SAAS;QAC3B,CAAC,CAAC,QAAQ,KAAK,CAAC,CAAC,QAAQ;QACzB,aAAa,CAAC,CAAC,CAAC,SAAS,CAAC,SAAS,EAAE,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC;QAC3D,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,SAAS,KAAK,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,SAAS;QAC3D,CAAC,CAAC,SAAS,CAAC,UAAU,KAAK,CAAC,CAAC,SAAS,CAAC,UAAU;QACjD,CAAC,CAAC,SAAS,CAAC,SAAS,KAAK,CAAC,CAAC,SAAS,CAAC,SAAS;QAC/C,CAAC,CAAC,SAAS,CAAC,UAAU,KAAK,CAAC,CAAC,SAAS,CAAC,UAAU,CACjD,CAAC;AACH,CAAC","sourcesContent":["import { randomUUID } from \"node:crypto\";\nimport { addAbortListener } from \"node:events\";\nimport { performance } from \"node:perf_hooks\";\nimport { raceWithAbortSignal } from \"../../utils/abort.ts\";\nimport type { HcodeAccountPort, HcodeAccountRef } from \"../hcode/ports.ts\";\nimport { streamWatch, type WatchAttachment, type WatchProjection } from \"./host-watch.ts\";\nimport type { LiveRoute, PrincipalKey } from \"./protocol.ts\";\nimport type { SecureChannel } from \"./secure-channel.ts\";\n\nconst DAY = 86_400_000;\nconst CONSENT_MS = 120_000;\nconst VIEW_MS = 300_000;\nconst LEASE_MS = 30_000;\n\n/** Trusted ingress supplies a verified, claimed candidate, never a decoded browser object. */\nexport interface PendingWatchCandidate {\n\treadonly pendingId: string;\n\treadonly offerId: string;\n\treadonly route: Readonly<LiveRoute>;\n\treadonly principal: Readonly<PrincipalKey>;\n\treadonly hostGeneration: string;\n\treadonly browserGeneration: string;\n\treadonly installKey: string;\n\treadonly deviceKey: string;\n\treadonly transcript: string;\n\treadonly credential: string | null;\n\treadonly mode: \"uv\" | \"short\";\n\treadonly expiresAt: number;\n}\n\ntype TerminalWatchConsent = (candidate: PendingWatchCandidate, signal: AbortSignal) => Promise<\"allow\" | \"deny\">;\n\ninterface CommittedPairing {\n\treadonly candidate: PendingWatchCandidate;\n\treadonly pairingId: string;\n\treadonly pairedAt: number;\n}\n\n/** Only the future trusted proof/lease adapter may construct this input. Times use the broker clock. */\nexport interface VerifiedPairing extends CommittedPairing {\n\treadonly verifiedAt: number;\n\treadonly leaseUntil: number;\n\treadonly reconnect: boolean;\n\treadonly durable?: CommittedPairing;\n}\n\n/** Only the trusted lease adapter may apply fresh, ordered grants for this attachment. */\ninterface LeasedWatchAttachment extends WatchAttachment {\n\trenewLease(route: LiveRoute, pairingId: string, leaseUntil: number): boolean;\n}\n\nexport interface WatchAuthority {\n\tcommit(candidate: PendingWatchCandidate, signal: AbortSignal): Promise<CommittedPairing | undefined>;\n\treconcile(installScopedDenied: readonly string[]): Promise<void>;\n}\n\nexport interface WatchDenyScope {\n\tinstallId: string;\n\tprincipal: PrincipalKey;\n}\n\nexport const MAX_WATCH_DENIES = 4096;\n\n/** One exclusive store lifetime per broker. An unclean exit must leave restart fenced. */\nexport interface WatchDenyStore {\n\tread(scope: WatchDenyScope): Promise<readonly string[]>;\n\tsave(ids: readonly string[]): Promise<void>;\n\t/** Release only after closing delivery and draining successful writes. */\n\trelease(): Promise<void>;\n}\n\ninterface BrokerOptions extends WatchDenyScope {\n\tstore: WatchDenyStore;\n\tauthority: WatchAuthority;\n\taccount: Pick<HcodeAccountPort, \"status\" | \"subscribe\">;\n\treference: Readonly<HcodeAccountRef>;\n\t/** Verified Remote authority generation, distinct from the CLI's local account generation. */\n\thostGeneration: string;\n\t/** Monotonic clock on the same timeline as the authority adapter's converted deadlines. */\n\tnow?: () => number;\n}\n\nconst principalFields = [\"issuer\", \"subject\", \"accountId\", \"tenantId\", \"environment\"] as const;\nconst routeFields = [\"installId\", \"brokerEpoch\", \"sessionId\", \"attachmentId\", \"channelEpoch\"] as const;\nconst candidateFields = [\n\t\"pendingId\",\n\t\"offerId\",\n\t\"hostGeneration\",\n\t\"browserGeneration\",\n\t\"installKey\",\n\t\"deviceKey\",\n\t\"transcript\",\n\t\"credential\",\n\t\"mode\",\n\t\"expiresAt\",\n] as const;\n\nfunction samePrincipal(a: PrincipalKey, b: PrincipalKey): boolean {\n\treturn principalFields.every((key) => a[key] === b[key]);\n}\n\nfunction sameRoute(a: LiveRoute, b: LiveRoute): boolean {\n\treturn routeFields.every((key) => a[key] === b[key]);\n}\n\nfunction exactCandidate(a: PendingWatchCandidate, b: PendingWatchCandidate): boolean {\n\treturn (\n\t\tsameRoute(a.route, b.route) &&\n\t\tsamePrincipal(a.principal, b.principal) &&\n\t\tcandidateFields.every((key) => a[key] === b[key])\n\t);\n}\n\n/** Install-local watch fences. No socket, user input, agent or provider capability is accepted here. */\nexport class WatchBroker {\n\treadonly #options: BrokerOptions;\n\treadonly #now: () => number;\n\treadonly #epoch = randomUUID();\n\treadonly #sessions = new Map<\n\t\tstring,\n\t\t{\n\t\t\tsource: WatchProjection;\n\t\t\tnotify: (pairingId: string, reconnect: boolean) => void;\n\t\t\tconfirm: TerminalWatchConsent;\n\t\t\tlifetime: AbortSignal;\n\t\t\tstopListening: Disposable;\n\t\t\tonUnshare?: () => void;\n\t\t}\n\t>();\n\treadonly #sessionIds = new WeakMap<WatchProjection, string>();\n\treadonly #routes = new Map<string, { route: LiveRoute; until: number }>();\n\treadonly #pending = new Map<string, { sessionId: string; abort: AbortController }>();\n\treadonly #usedOffers = new Map<string, number>();\n\treadonly #approved = new Map<string, CommittedPairing & { setupSignal: AbortSignal }>();\n\treadonly #active = new Map<string, { sessionId: string; pairingId: string; close(): void }>();\n\treadonly #denied = new Set<string>();\n\t#startup?: Promise<void>;\n\t#shutdown?: Promise<void>;\n\t#stopAccountWatch?: () => void;\n\t#persist = Promise.resolve();\n\t#storeOpen = false;\n\t#reconciling = 0;\n\t#rendezvousAllowed = false;\n\t#closed = false;\n\n\tconstructor(options: BrokerOptions) {\n\t\tthis.#options = { ...options, principal: { ...options.principal }, reference: { ...options.reference } };\n\t\tthis.#now = options.now ?? (() => performance.now());\n\t}\n\n\tstart(): Promise<void> {\n\t\tif (this.#closed) return Promise.reject(new Error(\"Remote unavailable\"));\n\t\tthis.#startup ??= this.#restore();\n\t\treturn this.#startup;\n\t}\n\n\tasync #restore(): Promise<void> {\n\t\ttry {\n\t\t\tconst { installId, principal, store, account, reference, hostGeneration } = this.#options;\n\t\t\tif (\n\t\t\t\tprincipal.environment !== reference.environmentId ||\n\t\t\t\tprincipal.accountId !== reference.accountId ||\n\t\t\t\t!hostGeneration\n\t\t\t) {\n\t\t\t\tthrow new Error(\"Remote account scope mismatch\");\n\t\t\t}\n\t\t\tthis.#stopAccountWatch = bindWatchAccount(this, account, reference);\n\t\t\tif (this.#closed) throw new Error(\"Remote account unavailable\");\n\t\t\tconst stored = await store.read({ installId, principal });\n\t\t\tif (!Array.isArray(stored) || stored.length > MAX_WATCH_DENIES || stored.some((id) => !opaqueId(id))) {\n\t\t\t\tthrow new Error();\n\t\t\t}\n\t\t\tthis.#storeOpen = true;\n\t\t\tif (this.#closed) throw new Error();\n\t\t\tfor (const id of stored) this.#denied.add(id);\n\t\t\tif (this.#denied.size) await this.#options.authority.reconcile([...this.#denied]);\n\t\t\tif (this.#closed) throw new Error();\n\t\t\tthis.#rendezvousAllowed = true;\n\t\t} catch {\n\t\t\tthis.close();\n\t\t\tthrow new Error(\"Remote unavailable: account or deny state could not be verified and reconciled\");\n\t\t}\n\t}\n\n\t/** Bind the share to the owning terminal's notice, consent and session lifetime. */\n\tshareSession(\n\t\tsource: WatchProjection,\n\t\tnotify: (pairingId: string, reconnect: boolean) => void,\n\t\tlifetime: AbortSignal,\n\t\tconfirm: TerminalWatchConsent,\n\t\tonUnshare?: () => void,\n\t): string {\n\t\tif (\n\t\t\t!this.#rendezvousAllowed ||\n\t\t\tthis.#reconciling ||\n\t\t\ttypeof notify !== \"function\" ||\n\t\t\ttypeof confirm !== \"function\" ||\n\t\t\t!(lifetime instanceof AbortSignal) ||\n\t\t\tlifetime.aborted\n\t\t) {\n\t\t\tthrow new Error(\"Remote unavailable\");\n\t\t}\n\t\tconst existing = this.#sessionIds.get(source);\n\t\tconst share = existing && this.#sessions.get(existing);\n\t\tif (existing && share) {\n\t\t\tif (share.lifetime !== lifetime) throw new Error(\"Remote unavailable\");\n\t\t\treturn existing;\n\t\t}\n\t\tif (this.#sessions.size >= 32) throw new Error(\"Remote share limit reached\");\n\t\tconst id = randomUUID();\n\t\tconst stopListening = addAbortListener(lifetime, () => this.unshare(id));\n\t\tthis.#sessions.set(id, { source, notify, confirm, lifetime, stopListening, onUnshare });\n\t\tthis.#sessionIds.set(source, id);\n\t\treturn id;\n\t}\n\n\trouteFor(sessionId: string): LiveRoute {\n\t\tif (!this.#rendezvousAllowed || this.#reconciling || this.#sessions.get(sessionId)?.lifetime.aborted !== false) {\n\t\t\tthrow new Error(\"Remote share unavailable\");\n\t\t}\n\t\tconst now = this.#now();\n\t\tfor (const [id, value] of this.#routes) {\n\t\t\tif (value.until <= now) {\n\t\t\t\tthis.#routes.delete(id);\n\t\t\t\tthis.#approved.delete(id);\n\t\t\t}\n\t\t}\n\t\tif (this.#routes.size >= 16) throw new Error(\"Remote attachment setup limit reached\");\n\t\tconst route = Object.freeze({\n\t\t\tinstallId: this.#options.installId,\n\t\t\tbrokerEpoch: this.#epoch,\n\t\t\tsessionId,\n\t\t\tattachmentId: randomUUID(),\n\t\t\tchannelEpoch: randomUUID(),\n\t\t});\n\t\tthis.#routes.set(route.attachmentId, { route, until: now + VIEW_MS });\n\t\treturn route;\n\t}\n\n\tunshare(sessionId: string): void {\n\t\tconst share = this.#sessions.get(sessionId);\n\t\tthis.#sessions.delete(sessionId);\n\t\tshare?.stopListening[Symbol.dispose]();\n\t\tfor (const [id, setup] of this.#routes) {\n\t\t\tif (setup.route.sessionId === sessionId) {\n\t\t\t\tthis.#routes.delete(id);\n\t\t\t\tthis.#approved.delete(id);\n\t\t\t}\n\t\t}\n\t\tfor (const entry of this.#pending.values()) if (entry.sessionId === sessionId) entry.abort.abort();\n\t\tfor (const attachment of this.#active.values()) if (attachment.sessionId === sessionId) attachment.close();\n\t\ttry {\n\t\t\tshare?.onUnshare?.();\n\t\t} catch {\n\t\t\t// Terminal cleanup cannot interrupt broker shutdown or account revocation.\n\t\t}\n\t}\n\n\t/** Trusted ingress requests consent from the share's terminal; it cannot supply the decision callback. */\n\tasync confirmPairing(candidate: PendingWatchCandidate, setupSignal: AbortSignal): Promise<boolean> {\n\t\tconst exact = Object.freeze({\n\t\t\t...candidate,\n\t\t\troute: Object.freeze({ ...candidate.route }),\n\t\t\tprincipal: Object.freeze({ ...candidate.principal }),\n\t\t});\n\t\tconst share = this.#sessions.get(exact.route.sessionId);\n\t\tconst now = this.#now();\n\t\tfor (const [id, until] of this.#usedOffers) if (until <= now) this.#usedOffers.delete(id);\n\t\tif (\n\t\t\t!share ||\n\t\t\t!(setupSignal instanceof AbortSignal) ||\n\t\t\tsetupSignal.aborted ||\n\t\t\t!this.#currentSetup(exact.route) ||\n\t\t\t!samePrincipal(exact.principal, this.#options.principal) ||\n\t\t\texact.hostGeneration !== this.#options.hostGeneration ||\n\t\t\tthis.#pending.size >= 8 ||\n\t\t\tthis.#usedOffers.size >= 32 ||\n\t\t\tthis.#usedOffers.has(exact.offerId) ||\n\t\t\tthis.#pending.has(exact.pendingId) ||\n\t\t\t!(exact.expiresAt > now && exact.expiresAt <= now + CONSENT_MS) ||\n\t\t\t(exact.mode === \"uv\" ? !exact.credential : exact.mode !== \"short\" || exact.credential !== null)\n\t\t)\n\t\t\treturn false;\n\t\tconst abort = new AbortController();\n\t\tconst timer = setTimeout(() => abort.abort(), exact.expiresAt - now);\n\t\ttimer.unref();\n\t\tthis.#usedOffers.set(exact.offerId, now + VIEW_MS);\n\t\tthis.#pending.set(exact.pendingId, { sessionId: exact.route.sessionId, abort });\n\t\tconst signal = AbortSignal.any([abort.signal, setupSignal, share.lifetime]);\n\t\tconst current = () => !signal.aborted && this.#currentSetup(exact.route) && this.#now() < exact.expiresAt;\n\t\ttry {\n\t\t\tif ((await raceWithAbortSignal(share.confirm(exact, signal), signal)) !== \"allow\" || !current()) return false;\n\t\t\tconst commit = await raceWithAbortSignal(this.#options.authority.commit(exact, signal), signal);\n\t\t\tif (\n\t\t\t\t!current() ||\n\t\t\t\t!commit ||\n\t\t\t\t!exactCandidate(commit.candidate, exact) ||\n\t\t\t\t!opaqueId(commit.pairingId) ||\n\t\t\t\tthis.#denied.has(commit.pairingId) ||\n\t\t\t\t!(commit.pairedAt >= now && commit.pairedAt <= this.#now())\n\t\t\t)\n\t\t\t\treturn false;\n\t\t\tthis.#approved.set(exact.route.attachmentId, {\n\t\t\t\tcandidate: exact,\n\t\t\t\tpairingId: commit.pairingId,\n\t\t\t\tpairedAt: commit.pairedAt,\n\t\t\t\tsetupSignal,\n\t\t\t});\n\t\t\treturn true;\n\t\t} catch {\n\t\t\treturn false;\n\t\t} finally {\n\t\t\tclearTimeout(timer);\n\t\t\tthis.#pending.delete(exact.pendingId);\n\t\t}\n\t}\n\n\tattachWatch(route: LiveRoute, peer: VerifiedPairing, channel: SecureChannel): LeasedWatchAttachment | undefined {\n\t\tconst share = this.#sessions.get(route.sessionId);\n\t\tconst approved = this.#approved.get(route.attachmentId);\n\t\tconst durable = peer.reconnect ? peer.durable : approved;\n\t\tconst now = this.#now();\n\t\tconst absoluteCutoff = Math.min(\n\t\t\tpeer.candidate.mode === \"uv\" ? peer.verifiedAt + VIEW_MS : peer.pairedAt + VIEW_MS,\n\t\t\tpeer.pairedAt + 30 * DAY,\n\t\t);\n\t\tlet cutoff = Math.min(peer.leaseUntil, absoluteCutoff);\n\t\tif (\n\t\t\tchannel.signal.aborted ||\n\t\t\t!share ||\n\t\t\t!this.#currentSetup(route) ||\n\t\t\t!sameRoute(peer.candidate.route, route) ||\n\t\t\t!samePrincipal(peer.candidate.principal, this.#options.principal) ||\n\t\t\tpeer.candidate.hostGeneration !== this.#options.hostGeneration ||\n\t\t\t!durable ||\n\t\t\tdurable.candidate.hostGeneration !== this.#options.hostGeneration ||\n\t\t\t!samePairing(durable, peer) ||\n\t\t\t(!peer.reconnect && (approved?.setupSignal.aborted || !exactCandidate(durable.candidate, peer.candidate))) ||\n\t\t\t(peer.reconnect && (peer.candidate.mode !== \"uv\" || durable.candidate.mode !== \"uv\")) ||\n\t\t\tthis.#denied.has(peer.pairingId) ||\n\t\t\tthis.#active.size >= 8 ||\n\t\t\t!(peer.pairedAt <= now && peer.verifiedAt <= now && cutoff > now && peer.leaseUntil <= now + LEASE_MS)\n\t\t) {\n\t\t\tchannel.close();\n\t\t\treturn undefined;\n\t\t}\n\t\tconst pinnedRoute = this.#routes.get(route.attachmentId)!.route;\n\t\tconst { attachmentId, sessionId } = pinnedRoute;\n\t\tconst { pairingId, reconnect } = peer;\n\t\tthis.#routes.delete(attachmentId);\n\t\tthis.#approved.delete(attachmentId);\n\t\tlet watch: WatchAttachment | undefined;\n\t\tlet streamStarted = false;\n\t\tconst close = () => {\n\t\t\tif (!this.#active.delete(attachmentId)) return;\n\t\t\tclearTimeout(timer);\n\t\t\ttry {\n\t\t\t\tif (streamStarted) watch?.close();\n\t\t\t\telse channel.close();\n\t\t\t} catch {\n\t\t\t\t// Delivery is already fenced; adapter cleanup cannot interrupt revocation or shutdown.\n\t\t\t}\n\t\t};\n\t\tlet timer = setTimeout(close, cutoff - now).unref();\n\t\tconst isLive = (at: number) =>\n\t\t\t!channel.signal.aborted &&\n\t\t\t!share.lifetime.aborted &&\n\t\t\tthis.#rendezvousAllowed &&\n\t\t\tthis.#active.has(attachmentId) &&\n\t\t\t!this.#denied.has(pairingId) &&\n\t\t\tcutoff > at;\n\t\tthis.#active.set(attachmentId, { sessionId, pairingId, close });\n\t\ttry {\n\t\t\t// The terminal must be able to announce presence before watch data is released.\n\t\t\tshare.notify(pairingId, reconnect);\n\t\t} catch {\n\t\t\tclose();\n\t\t\treturn undefined;\n\t\t}\n\t\tif (!this.#active.has(attachmentId)) return undefined;\n\t\tstreamStarted = true;\n\t\twatch = streamWatch(share.source, () => isLive(this.#now()), channel, close);\n\t\tif (!this.#active.has(attachmentId)) {\n\t\t\t// Startup can reenter teardown before the broker owns the stream handle.\n\t\t\twatch.close();\n\t\t\treturn undefined;\n\t\t}\n\t\treturn {\n\t\t\tclose,\n\t\t\trenewLease: (grantRoute, grantPairingId, leaseUntil) => {\n\t\t\t\tif (!sameRoute(pinnedRoute, grantRoute) || grantPairingId !== pairingId) return false;\n\t\t\t\tconst at = this.#now();\n\t\t\t\tif (!isLive(at) || leaseUntil <= at) {\n\t\t\t\t\tclose();\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\tif (!(leaseUntil <= at + LEASE_MS)) return false;\n\t\t\t\tcutoff = Math.min(leaseUntil, absoluteCutoff);\n\t\t\t\tclearTimeout(timer);\n\t\t\t\ttimer = setTimeout(close, cutoff - at).unref();\n\t\t\t\treturn true;\n\t\t\t},\n\t\t};\n\t}\n\n\t#currentSetup(route: LiveRoute): boolean {\n\t\tconst setup = this.#routes.get(route.attachmentId);\n\t\treturn (\n\t\t\tthis.#rendezvousAllowed &&\n\t\t\t!this.#reconciling &&\n\t\t\t!!setup &&\n\t\t\tthis.#sessions.get(setup.route.sessionId)?.lifetime.aborted === false &&\n\t\t\tsetup.until > this.#now() &&\n\t\t\tsameRoute(route, setup.route)\n\t\t);\n\t}\n\n\tasync revokePairing(id: string): Promise<void> {\n\t\tif (!opaqueId(id) || !this.#rendezvousAllowed) throw new Error(\"Remote revoke unavailable\");\n\t\tthis.#reconciling++;\n\t\tthis.#denied.add(id);\n\t\t// Queue durability before invoking any callbacks that could shut down the broker.\n\t\tconst saved = this.#persist.then(async () => {\n\t\t\tif (this.#denied.size > MAX_WATCH_DENIES) throw new Error();\n\t\t\tawait this.#options.store.save([...this.#denied]);\n\t\t});\n\t\tthis.#persist = saved;\n\t\ttry {\n\t\t\tfor (const attachment of this.#active.values()) if (attachment.pairingId === id) attachment.close();\n\t\t\tfor (const pending of this.#pending.values()) pending.abort.abort();\n\t\t\ttry {\n\t\t\t\tawait saved;\n\t\t\t} catch {\n\t\t\t\tthis.close();\n\t\t\t\tthrow new Error(\"Remote disabled: revoke could not be saved durably\");\n\t\t\t}\n\t\t\tif (this.#closed) return; // A clean restart reconciles the durable deny before sharing.\n\t\t\ttry {\n\t\t\t\tawait this.#options.authority.reconcile([id]);\n\t\t\t} catch {\n\t\t\t\tthis.#rendezvousAllowed = false;\n\t\t\t\tthis.#detachAll();\n\t\t\t}\n\t\t} finally {\n\t\t\tthis.#reconciling--;\n\t\t}\n\t}\n\n\t/** Fence immediately, then release the restart guard only if local persistence succeeded. */\n\tshutdown(): Promise<void> {\n\t\tthis.close();\n\t\tthis.#shutdown ??= this.#releaseStore();\n\t\treturn this.#shutdown;\n\t}\n\n\tasync #releaseStore(): Promise<void> {\n\t\tawait this.#startup?.catch(() => {});\n\t\tawait this.#persist;\n\t\tif (this.#storeOpen) await this.#options.store.release();\n\t}\n\n\tclose(): void {\n\t\tthis.#closed = true;\n\t\tthis.#rendezvousAllowed = false;\n\t\tconst unsubscribe = this.#stopAccountWatch;\n\t\tthis.#stopAccountWatch = undefined;\n\t\tthis.#detachAll();\n\t\ttry {\n\t\t\tunsubscribe?.();\n\t\t} catch {\n\t\t\t// Delivery is already fenced; account observer cleanup must not strand the durable deny store.\n\t\t}\n\t}\n\n\t#detachAll(): void {\n\t\tfor (const id of this.#sessions.keys()) this.unshare(id);\n\t\tthis.#approved.clear();\n\t}\n}\n\n/** A live broker is pinned to one account generation; later login needs a new broker and trust reconciliation. */\nexport function bindWatchAccount(\n\tbroker: WatchBroker,\n\taccount: Pick<HcodeAccountPort, \"status\" | \"subscribe\">,\n\treference: HcodeAccountRef,\n): () => void {\n\tconst { environmentId, accountId, generation } = reference;\n\tconst check = (current: HcodeAccountRef | null) => {\n\t\tif (\n\t\t\tcurrent?.environmentId !== environmentId ||\n\t\t\tcurrent.accountId !== accountId ||\n\t\t\tcurrent.generation !== generation\n\t\t)\n\t\t\tbroker.close();\n\t};\n\tconst unsubscribe = account.subscribe(check);\n\ttry {\n\t\tcheck(account.status(environmentId));\n\t\treturn unsubscribe;\n\t} catch (error) {\n\t\tunsubscribe();\n\t\tthrow error;\n\t}\n}\n\nexport function opaqueId(id: unknown): id is string {\n\treturn typeof id === \"string\" && /^[A-Za-z0-9_-]{1,128}$/.test(id);\n}\n\nfunction samePairing(a: CommittedPairing, b: VerifiedPairing): boolean {\n\treturn (\n\t\ta.pairingId === b.pairingId &&\n\t\ta.pairedAt === b.pairedAt &&\n\t\tsamePrincipal(a.candidate.principal, b.candidate.principal) &&\n\t\ta.candidate.route.installId === b.candidate.route.installId &&\n\t\ta.candidate.installKey === b.candidate.installKey &&\n\t\ta.candidate.deviceKey === b.candidate.deviceKey &&\n\t\ta.candidate.credential === b.candidate.credential\n\t);\n}\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type WatchDenyScope, type WatchDenyStore } from "./broker.ts";
|
|
2
|
+
/** Local deny metadata only. Use a dedicated directory under a trusted, owner-private install root. */
|
|
3
|
+
export declare class PosixWatchDenyStore implements WatchDenyStore {
|
|
4
|
+
#private;
|
|
5
|
+
constructor(directory: string);
|
|
6
|
+
/** For a fresh authoritative installation only. Never use this to repair an existing install. */
|
|
7
|
+
static provision(directory: string, scope: WatchDenyScope): Promise<void>;
|
|
8
|
+
read(scope: WatchDenyScope): Promise<readonly string[]>;
|
|
9
|
+
save(ids: readonly string[]): Promise<void>;
|
|
10
|
+
release(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=deny-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deny-store.d.ts","sourceRoot":"","sources":["../../../src/humain/remote/deny-store.ts"],"names":[],"mappings":"AAGA,OAAO,EAA8B,KAAK,cAAc,EAAE,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC;AAOnG,uGAAuG;AACvG,qBAAa,mBAAoB,YAAW,cAAc;;IAMzD,YAAY,SAAS,EAAE,MAAM,EAE5B;IAED,iGAAiG;IACjG,OAAa,SAAS,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAiB9E;IAEK,IAAI,CAAC,KAAK,EAAE,cAAc,GAAG,OAAO,CAAC,SAAS,MAAM,EAAE,CAAC,CAwC5D;IAEK,IAAI,CAAC,GAAG,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAgBhD;IAEK,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAU7B;CAOD"}
|