@indigoai-us/hq-cli 5.345.214 → 5.345.215
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 +93 -0
- package/assets/scaffold/core/scripts/hq-delegate-verify.sh +5 -2
- package/dist/command-catalog.generated.d.ts +180 -13
- package/dist/command-catalog.generated.js +221 -13
- package/dist/commands/agents.d.ts +51 -1
- package/dist/commands/agents.js +154 -2
- package/dist/commands/bot-jobs.d.ts +2 -15
- package/dist/commands/bot-jobs.js +2 -94
- package/dist/commands/bot-runtime.d.ts +46 -5
- package/dist/commands/bot-runtime.js +228 -18
- package/dist/commands/bot.d.ts +16 -9
- package/dist/commands/bot.js +231 -55
- package/dist/commands/capacity.d.ts +4 -1
- package/dist/commands/capacity.js +46 -10
- package/dist/commands/cloud.d.ts +66 -13
- package/dist/commands/cloud.js +121 -40
- package/dist/commands/company-restore.d.ts +23 -0
- package/dist/commands/company-restore.js +128 -0
- package/dist/commands/company.js +2 -0
- package/dist/commands/core-delegate-w1b.js +15 -1
- package/dist/commands/core-policy.js +67 -8
- package/dist/commands/daemon.js +5 -0
- package/dist/commands/files-browse.d.ts +11 -4
- package/dist/commands/files-browse.js +21 -4
- package/dist/commands/files.d.ts +1 -1
- package/dist/commands/files.js +25 -11
- package/dist/commands/integrations-auth-rules.d.ts +27 -0
- package/dist/commands/integrations-auth-rules.js +79 -0
- package/dist/commands/integrations-connect.d.ts +10 -3
- package/dist/commands/integrations-connect.js +87 -13
- package/dist/commands/integrations-import.js +124 -12
- package/dist/commands/lanes.js +28 -11
- package/dist/commands/mesh.d.ts +2 -0
- package/dist/commands/mesh.js +60 -35
- package/dist/commands/sources.js +6 -0
- package/dist/commands/storage-offload.d.ts +12 -0
- package/dist/commands/storage-offload.js +41 -3
- package/dist/commands/sync-held.js +5 -2
- package/dist/commands/sync-mode.js +5 -0
- package/dist/commands/whoami.js +4 -0
- package/dist/lazy-commands.js +3 -5
- package/dist/lib/bot/config.d.ts +1 -0
- package/dist/lib/bot/config.js +10 -0
- package/dist/lib/bot/eventbridge-cron.d.ts +22 -0
- package/dist/lib/bot/eventbridge-cron.js +104 -0
- package/dist/lib/bot/fleet-cron.d.ts +8 -0
- package/dist/lib/bot/fleet-cron.js +35 -3
- package/dist/lib/bot/fleet-daemon.d.ts +46 -0
- package/dist/lib/bot/fleet-daemon.js +99 -1
- package/dist/lib/bot/fleet-local.d.ts +33 -1
- package/dist/lib/bot/fleet-local.js +215 -31
- package/dist/lib/bot/fleet-python.d.ts +3 -0
- package/dist/lib/bot/fleet-python.js +34 -4
- package/dist/lib/bot/fleet-runtime-sources.d.ts +15 -0
- package/dist/lib/bot/fleet-runtime-sources.js +30 -0
- package/dist/lib/bot/legacy-provider-login.d.ts +8 -0
- package/dist/lib/bot/legacy-provider-login.js +19 -2
- package/dist/lib/bot/probe.js +7 -1
- package/dist/lib/bot/prompt.js +1 -1
- package/dist/lib/bot/readiness.d.ts +8 -0
- package/dist/lib/bot/readiness.js +17 -3
- package/dist/lib/bot/run.js +20 -1
- package/dist/lib/bot/runtime/codex.js +1 -1
- package/dist/lib/bot/runtime/grok-acp.d.ts +5 -1
- package/dist/lib/bot/runtime/grok-acp.js +6 -3
- package/dist/lib/bot/runtime/index.js +19 -9
- package/dist/lib/bot/runtime/messages-stream.js +2 -2
- package/dist/lib/bot/runtime/types.d.ts +15 -0
- package/dist/lib/bot/runtime/types.js +9 -0
- package/dist/lib/bot/runtime-auth.d.ts +30 -0
- package/dist/lib/bot/runtime-auth.js +94 -3
- package/dist/lib/bot/runtime-failure.d.ts +23 -0
- package/dist/lib/bot/runtime-failure.js +43 -1
- package/dist/lib/bot/status.d.ts +2 -0
- package/dist/lib/capacity/accounts.d.ts +8 -0
- package/dist/lib/capacity/accounts.js +19 -0
- package/dist/lib/capacity/available.d.ts +154 -0
- package/dist/lib/capacity/available.js +905 -0
- package/dist/lib/capacity/codex-availability.d.ts +14 -1
- package/dist/lib/capacity/codex-availability.js +26 -1
- package/dist/lib/capacity/collectors/claude-oauth.d.ts +29 -2
- package/dist/lib/capacity/collectors/claude-oauth.js +104 -7
- package/dist/lib/capacity/collectors/codex.js +10 -7
- package/dist/lib/capacity/collectors/grok-refresh.d.ts +80 -0
- package/dist/lib/capacity/collectors/grok-refresh.js +189 -0
- package/dist/lib/capacity/collectors/grok.d.ts +5 -0
- package/dist/lib/capacity/collectors/grok.js +56 -5
- package/dist/lib/capacity/ledger.d.ts +37 -1
- package/dist/lib/capacity/ledger.js +207 -30
- package/dist/lib/capacity/project.d.ts +26 -0
- package/dist/lib/capacity/project.js +137 -8
- package/dist/lib/capacity/refusal-rows.d.ts +31 -0
- package/dist/lib/capacity/refusal-rows.js +61 -0
- package/dist/lib/capacity/refusal.d.ts +53 -2
- package/dist/lib/capacity/refusal.js +284 -65
- package/dist/lib/capacity/route.js +46 -11
- package/dist/lib/capacity/widget.d.ts +12 -1
- package/dist/lib/capacity/widget.js +91 -3
- package/dist/lib/daemon/catalog.js +11 -1
- package/dist/lib/daemon/resolver.d.ts +2 -0
- package/dist/lib/daemon/resolver.js +3 -0
- package/dist/lib/daemon/run.d.ts +9 -0
- package/dist/lib/daemon/run.js +54 -1
- package/dist/lib/daemon/server.d.ts +2 -0
- package/dist/lib/daemon/server.js +10 -1
- package/dist/lib/daemon/tcc-eperm.d.ts +153 -0
- package/dist/lib/daemon/tcc-eperm.js +310 -0
- package/dist/lib/db/sqlite-binding.d.ts +1 -0
- package/dist/lib/db/sqlite-binding.js +1 -1
- package/dist/lib/delivery/admission.d.ts +86 -0
- package/dist/lib/delivery/admission.js +148 -0
- package/dist/lib/delivery/artifacts.d.ts +105 -0
- package/dist/lib/delivery/artifacts.js +567 -0
- package/dist/lib/delivery/attempt-id.d.ts +13 -0
- package/dist/lib/delivery/attempt-id.js +16 -0
- package/dist/lib/delivery/backup.d.ts +78 -0
- package/dist/lib/delivery/backup.js +343 -0
- package/dist/lib/delivery/cache.d.ts +29 -0
- package/dist/lib/delivery/cache.js +33 -0
- package/dist/lib/delivery/contracts.d.ts +142 -0
- package/dist/lib/delivery/contracts.js +289 -0
- package/dist/lib/delivery/credentials.d.ts +54 -0
- package/dist/lib/delivery/credentials.js +119 -0
- package/dist/lib/delivery/disposable-target.d.ts +42 -0
- package/dist/lib/delivery/disposable-target.js +111 -0
- package/dist/lib/delivery/effects.d.ts +136 -0
- package/dist/lib/delivery/effects.js +541 -0
- package/dist/lib/delivery/enrollment.d.ts +146 -0
- package/dist/lib/delivery/enrollment.js +460 -0
- package/dist/lib/delivery/escalations.d.ts +51 -0
- package/dist/lib/delivery/escalations.js +60 -0
- package/dist/lib/delivery/identity.d.ts +25 -0
- package/dist/lib/delivery/identity.js +82 -0
- package/dist/lib/delivery/inbox.d.ts +116 -0
- package/dist/lib/delivery/inbox.js +260 -0
- package/dist/lib/delivery/lease.d.ts +80 -0
- package/dist/lib/delivery/lease.js +134 -0
- package/dist/lib/delivery/migrations.d.ts +255 -0
- package/dist/lib/delivery/migrations.js +823 -0
- package/dist/lib/delivery/mqtt-adapter.d.ts +50 -0
- package/dist/lib/delivery/mqtt-adapter.js +242 -0
- package/dist/lib/delivery/mqtt.d.ts +63 -0
- package/dist/lib/delivery/mqtt.js +119 -0
- package/dist/lib/delivery/outbox.d.ts +95 -0
- package/dist/lib/delivery/outbox.js +187 -0
- package/dist/lib/delivery/provenance.d.ts +129 -0
- package/dist/lib/delivery/provenance.js +93 -0
- package/dist/lib/delivery/reconciler.d.ts +215 -0
- package/dist/lib/delivery/reconciler.js +703 -0
- package/dist/lib/delivery/restore.d.ts +35 -0
- package/dist/lib/delivery/restore.js +134 -0
- package/dist/lib/delivery/rpc/client.d.ts +22 -0
- package/dist/lib/delivery/rpc/client.js +159 -0
- package/dist/lib/delivery/rpc/port.d.ts +40 -0
- package/dist/lib/delivery/rpc/port.js +16 -0
- package/dist/lib/delivery/rpc/server.d.ts +30 -0
- package/dist/lib/delivery/rpc/server.js +205 -0
- package/dist/lib/delivery/rpc/wire.d.ts +85 -0
- package/dist/lib/delivery/rpc/wire.js +143 -0
- package/dist/lib/delivery/store.d.ts +163 -0
- package/dist/lib/delivery/store.js +944 -0
- package/dist/lib/delivery/windows-service-install.d.ts +262 -0
- package/dist/lib/delivery/windows-service-install.js +892 -0
- package/dist/lib/delivery/workers/macos-job-helper.d.ts +103 -0
- package/dist/lib/delivery/workers/macos-job-helper.js +927 -0
- package/dist/lib/delivery/workers/macos.d.ts +191 -0
- package/dist/lib/delivery/workers/macos.js +943 -0
- package/dist/lib/delivery/workers/windows-job-helper.d.ts +35 -0
- package/dist/lib/delivery/workers/windows-job-helper.js +264 -0
- package/dist/lib/delivery/workers/windows-service-live.d.ts +121 -0
- package/dist/lib/delivery/workers/windows-service-live.js +711 -0
- package/dist/lib/delivery/workers/windows-service-runner.d.ts +33 -0
- package/dist/lib/delivery/workers/windows-service-runner.js +82 -0
- package/dist/lib/delivery/workers/windows-service.d.ts +140 -0
- package/dist/lib/delivery/workers/windows-service.js +584 -0
- package/dist/lib/delivery/workers/windows.d.ts +251 -0
- package/dist/lib/delivery/workers/windows.js +594 -0
- package/dist/lib/hot-channels/adapters/meetings.d.ts +15 -0
- package/dist/lib/hot-channels/adapters/meetings.js +36 -3
- package/dist/lib/hot-channels/signals-pass.d.ts +10 -0
- package/dist/lib/hot-channels/signals-pass.js +71 -2
- package/dist/lib/hot-channels/signals-redo.js +10 -2
- package/dist/lib/install/provider-wrappers.d.ts +2 -0
- package/dist/lib/install/provider-wrappers.js +68 -3
- package/dist/lib/lanes/watcher.js +35 -0
- package/dist/lib/signals/steps/extract.js +40 -4
- package/dist/schemas/hq-available-ai.v1.schema.json +76 -0
- package/dist/utils/catalog-runtime-paths.d.ts +34 -0
- package/dist/utils/catalog-runtime-paths.js +53 -0
- package/dist/utils/cognito-callback-port.d.ts +14 -0
- package/dist/utils/cognito-callback-port.js +24 -0
- package/dist/utils/cognito-session.js +3 -3
- package/dist/utils/deleted-company-notice.d.ts +59 -0
- package/dist/utils/deleted-company-notice.js +156 -0
- package/dist/utils/files-browse-acl.js +1 -1
- package/dist/utils/login-browser.d.ts +1 -1
- package/dist/utils/login-browser.js +7 -5
- package/dist/utils/resident-package-preload.js +30 -15
- package/dist/utils/vault-api.js +7 -0
- package/package.json +7 -7
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type BrokerHandler, type NotificationBroker } from "./mqtt.js";
|
|
2
|
+
import { type BackoffPolicy } from "./outbox.js";
|
|
3
|
+
export interface MqttBrokerOptions {
|
|
4
|
+
/** Broker URL, for example the loopback URL of a test broker. Required; there is no default. */
|
|
5
|
+
url: string;
|
|
6
|
+
/** Stable client id. The persistent session is keyed by it. Required. */
|
|
7
|
+
clientId: string;
|
|
8
|
+
/** Longest wait for a PUBACK before publish() rejects. Default 10 s. */
|
|
9
|
+
ackTimeoutMs?: number;
|
|
10
|
+
/** Longest wait for the first CONNACK. Default 10 s. */
|
|
11
|
+
connectTimeoutMs?: number;
|
|
12
|
+
/** Backoff for reconnects and for forced redelivery after a handler failure. Default 500 ms to 30 s. */
|
|
13
|
+
reconnect?: BackoffPolicy;
|
|
14
|
+
/** MQTT keepalive in seconds. Default 30. */
|
|
15
|
+
keepaliveSeconds?: number;
|
|
16
|
+
}
|
|
17
|
+
export declare class MqttBroker implements NotificationBroker {
|
|
18
|
+
private readonly client;
|
|
19
|
+
private readonly ackTimeoutMs;
|
|
20
|
+
private readonly backoff;
|
|
21
|
+
private readonly handlers;
|
|
22
|
+
private closed;
|
|
23
|
+
private reconnectFailures;
|
|
24
|
+
private reconnectTimer;
|
|
25
|
+
private redeliverFailures;
|
|
26
|
+
private redeliverTimer;
|
|
27
|
+
private subscribing;
|
|
28
|
+
private connectWaiters;
|
|
29
|
+
private constructor();
|
|
30
|
+
/** Connect to the broker and resolve on the first CONNACK. */
|
|
31
|
+
static connect(opts: MqttBrokerOptions): Promise<MqttBroker>;
|
|
32
|
+
get connected(): boolean;
|
|
33
|
+
publish(topic: string, payload: string): Promise<void>;
|
|
34
|
+
subscribe(filter: string, handler: BrokerHandler): () => void;
|
|
35
|
+
/** Resolve once every current subscription has its SUBACK on the live connection. */
|
|
36
|
+
whenSubscribed(): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Disconnect without ending the persistent session, so the broker keeps
|
|
39
|
+
* queuing for this client id. With a publish still waiting for its ack the
|
|
40
|
+
* connection is dropped at once instead of waiting on the broker.
|
|
41
|
+
*/
|
|
42
|
+
close(): Promise<void>;
|
|
43
|
+
private wire;
|
|
44
|
+
private subscribeAll;
|
|
45
|
+
private scheduleReconnect;
|
|
46
|
+
/** No PUBACK was sent: drop the connection after a backoff so the broker resends on reconnect. */
|
|
47
|
+
private scheduleRedelivery;
|
|
48
|
+
private onMessage;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=mqtt-adapter.d.ts.map
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Real MQTT adapter for delivery notifications (US-005 phase 01b).
|
|
3
|
+
*
|
|
4
|
+
* Implements NotificationBroker over mqtt.js with QoS 1:
|
|
5
|
+
* - publish() resolves only when the broker's PUBACK arrives. It rejects at
|
|
6
|
+
* once while disconnected and rejects when no ack arrives within
|
|
7
|
+
* `ackTimeoutMs`. It never resolves without an ack.
|
|
8
|
+
* - A subscriber handler that returns or resolves sends the PUBACK. One that
|
|
9
|
+
* throws or rejects sends no PUBACK; the adapter then drops the connection
|
|
10
|
+
* after a backoff delay so the broker redelivers the unacked message on
|
|
11
|
+
* reconnect. A message with no local handler yet is treated the same way,
|
|
12
|
+
* so a queued message that arrives before subscribe() is not lost.
|
|
13
|
+
* - The session is persistent (clean session off) under the caller's stable
|
|
14
|
+
* client id, so queued QoS 1 messages survive a client reconnect.
|
|
15
|
+
* - Reconnect uses bounded exponential backoff (`reconnect`). Subscriptions
|
|
16
|
+
* are sent again on every connect, because a restarted broker without
|
|
17
|
+
* persistence has forgotten them.
|
|
18
|
+
*
|
|
19
|
+
* The broker URL and client id are explicit arguments. The adapter reads no
|
|
20
|
+
* environment, has no host default and sets no credentials.
|
|
21
|
+
*/
|
|
22
|
+
import mqtt from "mqtt";
|
|
23
|
+
import { topicMatches } from "./mqtt.js";
|
|
24
|
+
import { backoffDelay } from "./outbox.js";
|
|
25
|
+
const DEFAULT_RECONNECT = { baseMs: 500, maxMs: 30_000 };
|
|
26
|
+
export class MqttBroker {
|
|
27
|
+
client;
|
|
28
|
+
ackTimeoutMs;
|
|
29
|
+
backoff;
|
|
30
|
+
handlers = new Map();
|
|
31
|
+
closed = false;
|
|
32
|
+
reconnectFailures = 0;
|
|
33
|
+
reconnectTimer = null;
|
|
34
|
+
redeliverFailures = 0;
|
|
35
|
+
redeliverTimer = null;
|
|
36
|
+
subscribing = Promise.resolve(true);
|
|
37
|
+
connectWaiters = [];
|
|
38
|
+
constructor(client, ackTimeoutMs, backoff) {
|
|
39
|
+
this.client = client;
|
|
40
|
+
this.ackTimeoutMs = ackTimeoutMs;
|
|
41
|
+
this.backoff = backoff;
|
|
42
|
+
}
|
|
43
|
+
/** Connect to the broker and resolve on the first CONNACK. */
|
|
44
|
+
static async connect(opts) {
|
|
45
|
+
if (typeof opts.url !== "string" || opts.url.length === 0)
|
|
46
|
+
throw new Error("MQTT broker url is required");
|
|
47
|
+
if (typeof opts.clientId !== "string" || opts.clientId.length === 0)
|
|
48
|
+
throw new Error("MQTT client id is required");
|
|
49
|
+
const connectTimeoutMs = opts.connectTimeoutMs ?? 10_000;
|
|
50
|
+
const client = mqtt.connect(opts.url, {
|
|
51
|
+
clientId: opts.clientId,
|
|
52
|
+
clean: false,
|
|
53
|
+
protocolVersion: 4,
|
|
54
|
+
keepalive: opts.keepaliveSeconds ?? 30,
|
|
55
|
+
connectTimeout: connectTimeoutMs,
|
|
56
|
+
reconnectPeriod: 0,
|
|
57
|
+
resubscribe: false,
|
|
58
|
+
});
|
|
59
|
+
const broker = new MqttBroker(client, opts.ackTimeoutMs ?? 10_000, opts.reconnect ?? DEFAULT_RECONNECT);
|
|
60
|
+
// Install the message hook before the CONNACK: a persistent session's
|
|
61
|
+
// queued messages can arrive right after it, and the default hook would
|
|
62
|
+
// ack them with no handler.
|
|
63
|
+
client.handleMessage = (packet, callback) => broker.onMessage(packet, callback);
|
|
64
|
+
try {
|
|
65
|
+
await new Promise((resolve, reject) => {
|
|
66
|
+
const timer = setTimeout(() => finish(new Error(`MQTT connect timed out after ${connectTimeoutMs} ms`)), connectTimeoutMs);
|
|
67
|
+
const onConnect = () => finish(null);
|
|
68
|
+
const onError = (err) => finish(err);
|
|
69
|
+
const onClose = () => finish(new Error("MQTT connection closed before connect"));
|
|
70
|
+
function finish(err) {
|
|
71
|
+
clearTimeout(timer);
|
|
72
|
+
client.off("connect", onConnect);
|
|
73
|
+
client.off("error", onError);
|
|
74
|
+
client.off("close", onClose);
|
|
75
|
+
if (err)
|
|
76
|
+
reject(err);
|
|
77
|
+
else
|
|
78
|
+
resolve();
|
|
79
|
+
}
|
|
80
|
+
client.once("connect", onConnect);
|
|
81
|
+
client.once("error", onError);
|
|
82
|
+
client.once("close", onClose);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
broker.closed = true;
|
|
87
|
+
client.end(true);
|
|
88
|
+
throw err;
|
|
89
|
+
}
|
|
90
|
+
broker.wire();
|
|
91
|
+
return broker;
|
|
92
|
+
}
|
|
93
|
+
get connected() {
|
|
94
|
+
return !this.closed && this.client.connected;
|
|
95
|
+
}
|
|
96
|
+
publish(topic, payload) {
|
|
97
|
+
if (this.closed)
|
|
98
|
+
return Promise.reject(new Error("MQTT adapter is closed"));
|
|
99
|
+
if (!this.client.connected)
|
|
100
|
+
return Promise.reject(new Error("MQTT broker unavailable: not connected"));
|
|
101
|
+
return new Promise((resolve, reject) => {
|
|
102
|
+
let settled = false;
|
|
103
|
+
const timer = setTimeout(() => {
|
|
104
|
+
settled = true;
|
|
105
|
+
reject(new Error(`MQTT publish not acked within ${this.ackTimeoutMs} ms`));
|
|
106
|
+
}, this.ackTimeoutMs);
|
|
107
|
+
this.client.publish(topic, payload, { qos: 1 }, (err) => {
|
|
108
|
+
if (settled)
|
|
109
|
+
return;
|
|
110
|
+
settled = true;
|
|
111
|
+
clearTimeout(timer);
|
|
112
|
+
if (err)
|
|
113
|
+
reject(err);
|
|
114
|
+
else
|
|
115
|
+
resolve();
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
subscribe(filter, handler) {
|
|
120
|
+
let set = this.handlers.get(filter);
|
|
121
|
+
const isNew = !set;
|
|
122
|
+
if (!set) {
|
|
123
|
+
set = new Set();
|
|
124
|
+
this.handlers.set(filter, set);
|
|
125
|
+
}
|
|
126
|
+
set.add(handler);
|
|
127
|
+
if (isNew && this.client.connected)
|
|
128
|
+
this.subscribeAll();
|
|
129
|
+
return () => {
|
|
130
|
+
const current = this.handlers.get(filter);
|
|
131
|
+
if (!current)
|
|
132
|
+
return;
|
|
133
|
+
current.delete(handler);
|
|
134
|
+
if (current.size > 0)
|
|
135
|
+
return;
|
|
136
|
+
this.handlers.delete(filter);
|
|
137
|
+
if (this.client.connected && !this.closed)
|
|
138
|
+
this.client.unsubscribe(filter);
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
/** Resolve once every current subscription has its SUBACK on the live connection. */
|
|
142
|
+
async whenSubscribed() {
|
|
143
|
+
for (;;) {
|
|
144
|
+
if (this.closed)
|
|
145
|
+
throw new Error("MQTT adapter is closed");
|
|
146
|
+
if (!this.client.connected) {
|
|
147
|
+
await new Promise((resolve) => this.connectWaiters.push(resolve));
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const round = this.subscribing;
|
|
151
|
+
const ok = await round;
|
|
152
|
+
if (ok && round === this.subscribing && this.client.connected)
|
|
153
|
+
return;
|
|
154
|
+
if (!ok && round === this.subscribing && this.client.connected)
|
|
155
|
+
this.subscribeAll();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Disconnect without ending the persistent session, so the broker keeps
|
|
160
|
+
* queuing for this client id. With a publish still waiting for its ack the
|
|
161
|
+
* connection is dropped at once instead of waiting on the broker.
|
|
162
|
+
*/
|
|
163
|
+
async close() {
|
|
164
|
+
if (this.closed)
|
|
165
|
+
return;
|
|
166
|
+
this.closed = true;
|
|
167
|
+
if (this.reconnectTimer)
|
|
168
|
+
clearTimeout(this.reconnectTimer);
|
|
169
|
+
if (this.redeliverTimer)
|
|
170
|
+
clearTimeout(this.redeliverTimer);
|
|
171
|
+
this.reconnectTimer = null;
|
|
172
|
+
this.redeliverTimer = null;
|
|
173
|
+
for (const w of this.connectWaiters.splice(0))
|
|
174
|
+
w();
|
|
175
|
+
const force = !this.client.connected || Object.keys(this.client.outgoing).length > 0;
|
|
176
|
+
await new Promise((resolve) => this.client.end(force, {}, () => resolve()));
|
|
177
|
+
}
|
|
178
|
+
wire() {
|
|
179
|
+
this.client.on("connect", () => {
|
|
180
|
+
this.reconnectFailures = 0;
|
|
181
|
+
if (this.handlers.size > 0)
|
|
182
|
+
this.subscribeAll();
|
|
183
|
+
for (const w of this.connectWaiters.splice(0))
|
|
184
|
+
w();
|
|
185
|
+
});
|
|
186
|
+
this.client.on("close", () => this.scheduleReconnect());
|
|
187
|
+
// mqtt.js emits connection errors as events; the close handler retries.
|
|
188
|
+
this.client.on("error", () => undefined);
|
|
189
|
+
}
|
|
190
|
+
subscribeAll() {
|
|
191
|
+
const filters = [...this.handlers.keys()];
|
|
192
|
+
this.subscribing = Promise.all(filters.map((filter) => new Promise((resolve) => this.client.subscribe(filter, { qos: 1 }, (err, granted) => resolve(!err && !(granted ?? []).some((g) => g.qos === 128)))))).then((all) => all.every(Boolean));
|
|
193
|
+
}
|
|
194
|
+
scheduleReconnect() {
|
|
195
|
+
if (this.closed || this.reconnectTimer)
|
|
196
|
+
return;
|
|
197
|
+
this.reconnectFailures += 1;
|
|
198
|
+
this.reconnectTimer = setTimeout(() => {
|
|
199
|
+
this.reconnectTimer = null;
|
|
200
|
+
if (!this.closed && !this.client.connected)
|
|
201
|
+
this.client.reconnect();
|
|
202
|
+
}, backoffDelay(this.backoff, this.reconnectFailures));
|
|
203
|
+
}
|
|
204
|
+
/** No PUBACK was sent: drop the connection after a backoff so the broker resends on reconnect. */
|
|
205
|
+
scheduleRedelivery() {
|
|
206
|
+
if (this.closed || this.redeliverTimer)
|
|
207
|
+
return;
|
|
208
|
+
this.redeliverFailures += 1;
|
|
209
|
+
this.redeliverTimer = setTimeout(() => {
|
|
210
|
+
this.redeliverTimer = null;
|
|
211
|
+
if (!this.closed && this.client.connected)
|
|
212
|
+
this.client.stream.destroy();
|
|
213
|
+
}, backoffDelay(this.backoff, this.redeliverFailures));
|
|
214
|
+
}
|
|
215
|
+
onMessage(packet, callback) {
|
|
216
|
+
const message = {
|
|
217
|
+
id: packet.messageId ?? 0,
|
|
218
|
+
topic: packet.topic,
|
|
219
|
+
payload: Buffer.isBuffer(packet.payload) ? packet.payload.toString("utf8") : String(packet.payload),
|
|
220
|
+
};
|
|
221
|
+
const matched = [];
|
|
222
|
+
for (const [filter, set] of this.handlers)
|
|
223
|
+
if (topicMatches(filter, message.topic))
|
|
224
|
+
matched.push(...set);
|
|
225
|
+
if (matched.length === 0) {
|
|
226
|
+
callback(new Error("no handler for topic yet"));
|
|
227
|
+
this.scheduleRedelivery();
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
(async () => {
|
|
231
|
+
for (const h of matched)
|
|
232
|
+
await h(message);
|
|
233
|
+
})().then(() => {
|
|
234
|
+
this.redeliverFailures = 0;
|
|
235
|
+
callback();
|
|
236
|
+
}, (err) => {
|
|
237
|
+
callback(err instanceof Error ? err : new Error("handler failed"));
|
|
238
|
+
this.scheduleRedelivery();
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
//# sourceMappingURL=mqtt-adapter.js.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MQTT transport for delivery notifications (US-005).
|
|
3
|
+
*
|
|
4
|
+
* Notifications are hints. A message on the broker never authorizes, creates
|
|
5
|
+
* or claims a job: the consumer re-reads the ledger through the access layer
|
|
6
|
+
* before any claim, and reconciliation finds ready work without the broker.
|
|
7
|
+
* A broker ack is a transport fact only and is never written to the ledger.
|
|
8
|
+
*
|
|
9
|
+
* This module defines the broker interface (QoS 1 semantics: publish resolves
|
|
10
|
+
* on the broker's ack; a subscriber handler that returns or resolves acks the
|
|
11
|
+
* message, one that throws leaves it for redelivery) and an in-process fake
|
|
12
|
+
* for tests. No production broker or credential is used here.
|
|
13
|
+
*/
|
|
14
|
+
export interface BrokerMessage {
|
|
15
|
+
/** Broker-assigned delivery id. Redeliveries of one message keep the same id. */
|
|
16
|
+
id: number;
|
|
17
|
+
topic: string;
|
|
18
|
+
payload: string;
|
|
19
|
+
}
|
|
20
|
+
export type BrokerHandler = (message: BrokerMessage) => void | Promise<void>;
|
|
21
|
+
export interface NotificationBroker {
|
|
22
|
+
/** Resolves when the broker has acked the message; rejects when it is unavailable. */
|
|
23
|
+
publish(topic: string, payload: string): Promise<void>;
|
|
24
|
+
/** Handler return or resolve acks the message; a throw or rejection leaves it unacked. */
|
|
25
|
+
subscribe(filter: string, handler: BrokerHandler): () => void;
|
|
26
|
+
}
|
|
27
|
+
export declare const NOTIFICATION_TOPIC_ROOT = "hq/delivery/v1";
|
|
28
|
+
/** US-009: escalated deployment decisions for a company's admins. Separate from worker events. */
|
|
29
|
+
export declare function escalationTopic(tenantId: string): string;
|
|
30
|
+
/** The one topic a tenant's notifications use. Tenant ids that could widen a filter are refused. */
|
|
31
|
+
export declare function notificationTopic(tenantId: string): string;
|
|
32
|
+
/** MQTT topic filter matching with `+` (one level) and a trailing `#` (any remaining levels). */
|
|
33
|
+
export declare function topicMatches(filter: string, topic: string): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* In-process fake broker. Messages stay queued until every matching
|
|
36
|
+
* subscriber acks them. A message with no matching subscriber stays queued.
|
|
37
|
+
* `restart()` models a broker that loses its queue; `outage()` makes publish
|
|
38
|
+
* fail until `restore()`.
|
|
39
|
+
*/
|
|
40
|
+
export declare class InMemoryBroker implements NotificationBroker {
|
|
41
|
+
private queue;
|
|
42
|
+
private readonly subscribers;
|
|
43
|
+
private down;
|
|
44
|
+
private nextId;
|
|
45
|
+
publish(topic: string, payload: string): Promise<void>;
|
|
46
|
+
subscribe(filter: string, handler: BrokerHandler): () => void;
|
|
47
|
+
/** Put a message on the queue as any client could, bypassing the outbox. */
|
|
48
|
+
inject(topic: string, payload: string): void;
|
|
49
|
+
outage(): void;
|
|
50
|
+
restore(): void;
|
|
51
|
+
/** The broker restarts without persistence: every queued message is lost. */
|
|
52
|
+
restart(): void;
|
|
53
|
+
queued(): BrokerMessage[];
|
|
54
|
+
/**
|
|
55
|
+
* Deliver each queued message once to its matching subscribers, in queue
|
|
56
|
+
* order. Returns how many messages were handed to at least one subscriber.
|
|
57
|
+
* With `dropAcks`, acks are lost in transit and every message stays queued.
|
|
58
|
+
*/
|
|
59
|
+
deliver(options?: {
|
|
60
|
+
dropAcks?: boolean;
|
|
61
|
+
}): Promise<number>;
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=mqtt.d.ts.map
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MQTT transport for delivery notifications (US-005).
|
|
3
|
+
*
|
|
4
|
+
* Notifications are hints. A message on the broker never authorizes, creates
|
|
5
|
+
* or claims a job: the consumer re-reads the ledger through the access layer
|
|
6
|
+
* before any claim, and reconciliation finds ready work without the broker.
|
|
7
|
+
* A broker ack is a transport fact only and is never written to the ledger.
|
|
8
|
+
*
|
|
9
|
+
* This module defines the broker interface (QoS 1 semantics: publish resolves
|
|
10
|
+
* on the broker's ack; a subscriber handler that returns or resolves acks the
|
|
11
|
+
* message, one that throws leaves it for redelivery) and an in-process fake
|
|
12
|
+
* for tests. No production broker or credential is used here.
|
|
13
|
+
*/
|
|
14
|
+
export const NOTIFICATION_TOPIC_ROOT = "hq/delivery/v1";
|
|
15
|
+
const TOPIC_SEGMENT = /^[A-Za-z0-9._-]+$/;
|
|
16
|
+
/** US-009: escalated deployment decisions for a company's admins. Separate from worker events. */
|
|
17
|
+
export function escalationTopic(tenantId) {
|
|
18
|
+
if (!TOPIC_SEGMENT.test(tenantId)) {
|
|
19
|
+
throw new Error("tenant id is not a valid notification topic segment");
|
|
20
|
+
}
|
|
21
|
+
return `${NOTIFICATION_TOPIC_ROOT}/${tenantId}/escalations`;
|
|
22
|
+
}
|
|
23
|
+
/** The one topic a tenant's notifications use. Tenant ids that could widen a filter are refused. */
|
|
24
|
+
export function notificationTopic(tenantId) {
|
|
25
|
+
if (!TOPIC_SEGMENT.test(tenantId)) {
|
|
26
|
+
throw new Error("tenant id is not a valid notification topic segment");
|
|
27
|
+
}
|
|
28
|
+
return `${NOTIFICATION_TOPIC_ROOT}/${tenantId}/events`;
|
|
29
|
+
}
|
|
30
|
+
/** MQTT topic filter matching with `+` (one level) and a trailing `#` (any remaining levels). */
|
|
31
|
+
export function topicMatches(filter, topic) {
|
|
32
|
+
const f = filter.split("/");
|
|
33
|
+
const t = topic.split("/");
|
|
34
|
+
for (let i = 0; i < f.length; i += 1) {
|
|
35
|
+
if (f[i] === "#")
|
|
36
|
+
return i === f.length - 1;
|
|
37
|
+
if (i >= t.length)
|
|
38
|
+
return false;
|
|
39
|
+
if (f[i] !== "+" && f[i] !== t[i])
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return f.length === t.length;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* In-process fake broker. Messages stay queued until every matching
|
|
46
|
+
* subscriber acks them. A message with no matching subscriber stays queued.
|
|
47
|
+
* `restart()` models a broker that loses its queue; `outage()` makes publish
|
|
48
|
+
* fail until `restore()`.
|
|
49
|
+
*/
|
|
50
|
+
export class InMemoryBroker {
|
|
51
|
+
queue = [];
|
|
52
|
+
subscribers = [];
|
|
53
|
+
down = false;
|
|
54
|
+
nextId = 1;
|
|
55
|
+
async publish(topic, payload) {
|
|
56
|
+
if (this.down)
|
|
57
|
+
throw new Error("broker unavailable");
|
|
58
|
+
this.queue.push({ id: this.nextId++, topic, payload });
|
|
59
|
+
}
|
|
60
|
+
subscribe(filter, handler) {
|
|
61
|
+
const entry = { filter, handler };
|
|
62
|
+
this.subscribers.push(entry);
|
|
63
|
+
return () => {
|
|
64
|
+
const i = this.subscribers.indexOf(entry);
|
|
65
|
+
if (i >= 0)
|
|
66
|
+
this.subscribers.splice(i, 1);
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/** Put a message on the queue as any client could, bypassing the outbox. */
|
|
70
|
+
inject(topic, payload) {
|
|
71
|
+
this.queue.push({ id: this.nextId++, topic, payload });
|
|
72
|
+
}
|
|
73
|
+
outage() {
|
|
74
|
+
this.down = true;
|
|
75
|
+
}
|
|
76
|
+
restore() {
|
|
77
|
+
this.down = false;
|
|
78
|
+
}
|
|
79
|
+
/** The broker restarts without persistence: every queued message is lost. */
|
|
80
|
+
restart() {
|
|
81
|
+
this.queue = [];
|
|
82
|
+
}
|
|
83
|
+
queued() {
|
|
84
|
+
return this.queue.map((m) => ({ ...m }));
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Deliver each queued message once to its matching subscribers, in queue
|
|
88
|
+
* order. Returns how many messages were handed to at least one subscriber.
|
|
89
|
+
* With `dropAcks`, acks are lost in transit and every message stays queued.
|
|
90
|
+
*/
|
|
91
|
+
async deliver(options = {}) {
|
|
92
|
+
if (this.down)
|
|
93
|
+
return 0;
|
|
94
|
+
let delivered = 0;
|
|
95
|
+
const kept = [];
|
|
96
|
+
for (const message of this.queue.splice(0)) {
|
|
97
|
+
const handlers = this.subscribers.filter((s) => topicMatches(s.filter, message.topic));
|
|
98
|
+
if (handlers.length === 0) {
|
|
99
|
+
kept.push(message);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
delivered += 1;
|
|
103
|
+
let acked = true;
|
|
104
|
+
for (const s of handlers) {
|
|
105
|
+
try {
|
|
106
|
+
await s.handler({ ...message });
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
acked = false;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (!acked || options.dropAcks)
|
|
113
|
+
kept.push(message);
|
|
114
|
+
}
|
|
115
|
+
this.queue.unshift(...kept);
|
|
116
|
+
return delivered;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=mqtt.js.map
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { DeliveryAccess } from "./enrollment.js";
|
|
2
|
+
import type { StoreState } from "./migrations.js";
|
|
3
|
+
import { type NotificationBroker } from "./mqtt.js";
|
|
4
|
+
export declare const NOTIFICATION_SCHEMA = "hq-delivery.notification.v1";
|
|
5
|
+
/** Larger payloads are refused unparsed. */
|
|
6
|
+
export declare const MAX_NOTIFICATION_BYTES: number;
|
|
7
|
+
export type LedgerEventType = "job.accepted" | "attempt.resolved";
|
|
8
|
+
export interface LedgerEvent {
|
|
9
|
+
schemaVersion: typeof NOTIFICATION_SCHEMA;
|
|
10
|
+
eventId: string;
|
|
11
|
+
type: LedgerEventType;
|
|
12
|
+
tenantId: string;
|
|
13
|
+
runId: string;
|
|
14
|
+
jobId: string;
|
|
15
|
+
attemptId: string | null;
|
|
16
|
+
/** Epoch of the commit that wrote the record: acceptedEpoch or resolvedEpoch. */
|
|
17
|
+
epoch: number;
|
|
18
|
+
}
|
|
19
|
+
/** Every event the committed ledger implies, in ledger order: accepted jobs, then resolved attempts. */
|
|
20
|
+
export declare function ledgerEvents(state: Readonly<StoreState>): LedgerEvent[];
|
|
21
|
+
/** Strict parse of a notification payload. Anything else, including extra keys, is null. */
|
|
22
|
+
export declare function parseNotification(payload: string): LedgerEvent | null;
|
|
23
|
+
/** True when two events carry the same values in every field. */
|
|
24
|
+
export declare function sameEvent(a: LedgerEvent, b: LedgerEvent): boolean;
|
|
25
|
+
export interface SqliteHandle {
|
|
26
|
+
exec(sql: string): unknown;
|
|
27
|
+
prepare(sql: string): {
|
|
28
|
+
run(...params: unknown[]): unknown;
|
|
29
|
+
get(...params: unknown[]): unknown;
|
|
30
|
+
all(...params: unknown[]): unknown[];
|
|
31
|
+
};
|
|
32
|
+
close(): void;
|
|
33
|
+
}
|
|
34
|
+
/** Open a transport-side sqlite file (outbox progress or consumer inbox) with WAL and a busy timeout. */
|
|
35
|
+
export declare function openTransportDb(path: string): SqliteHandle;
|
|
36
|
+
export interface BackoffPolicy {
|
|
37
|
+
baseMs: number;
|
|
38
|
+
maxMs: number;
|
|
39
|
+
}
|
|
40
|
+
export declare const DEFAULT_BACKOFF: BackoffPolicy;
|
|
41
|
+
export declare function backoffDelay(policy: BackoffPolicy, failures: number): number;
|
|
42
|
+
export interface PublishReport {
|
|
43
|
+
published: number;
|
|
44
|
+
/** Events still unpublished after this call. */
|
|
45
|
+
remaining: number;
|
|
46
|
+
/** Broker error text when this call stopped early, else null. */
|
|
47
|
+
error: string | null;
|
|
48
|
+
/** Suggested wait before the next call: 0 after success, growing after consecutive failures. */
|
|
49
|
+
nextDelayMs: number;
|
|
50
|
+
}
|
|
51
|
+
export interface OutboxOptions {
|
|
52
|
+
access: DeliveryAccess;
|
|
53
|
+
broker: NotificationBroker;
|
|
54
|
+
/** Path of the publisher's own sqlite file. Not the ledger. */
|
|
55
|
+
path: string;
|
|
56
|
+
/** Most events one publishPending call sends. Default 100. */
|
|
57
|
+
batchSize?: number;
|
|
58
|
+
backoff?: BackoffPolicy;
|
|
59
|
+
clock?: () => number;
|
|
60
|
+
/**
|
|
61
|
+
* Test-only crash barrier, inert unless passed. Called after the broker
|
|
62
|
+
* acks an event and before its published row is written.
|
|
63
|
+
* process-crash.test.ts uses it to SIGKILL a child at that point.
|
|
64
|
+
*/
|
|
65
|
+
testBarrier?: (stage: "acked", eventId: string) => void;
|
|
66
|
+
}
|
|
67
|
+
export declare const MAX_BATCH = 1000;
|
|
68
|
+
export declare function checkBatch(size: number): number;
|
|
69
|
+
export declare class OutboxPublisher {
|
|
70
|
+
private readonly access;
|
|
71
|
+
private readonly broker;
|
|
72
|
+
private readonly db;
|
|
73
|
+
private readonly batchSize;
|
|
74
|
+
private readonly backoff;
|
|
75
|
+
private readonly clock;
|
|
76
|
+
private readonly testBarrier;
|
|
77
|
+
private failures;
|
|
78
|
+
private crashAfterAck;
|
|
79
|
+
private constructor();
|
|
80
|
+
static open(opts: OutboxOptions): OutboxPublisher;
|
|
81
|
+
/** Committed events the broker has not acked yet, in ledger order. */
|
|
82
|
+
pending(): LedgerEvent[];
|
|
83
|
+
/** Event ids the broker has acked, in ack order. */
|
|
84
|
+
published(): string[];
|
|
85
|
+
/**
|
|
86
|
+
* Publish up to one batch of pending events, oldest first. Stops at the
|
|
87
|
+
* first broker error and reports a backoff delay; nothing is lost because
|
|
88
|
+
* the events stay derivable from the ledger.
|
|
89
|
+
*/
|
|
90
|
+
publishPending(): Promise<PublishReport>;
|
|
91
|
+
/** Test hook: the next publishPending throws after the first broker ack and before its row is written. */
|
|
92
|
+
crashAfterAckForTest(): void;
|
|
93
|
+
close(): void;
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=outbox.d.ts.map
|