@proofoftech/flowsafe 0.3.0 → 0.4.0
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 +29 -0
- package/dist/approval-api/contract.d.ts +29 -0
- package/dist/approval-api/contract.d.ts.map +1 -1
- package/dist/approval-api/d1-store.d.ts.map +1 -1
- package/dist/approval-api/d1-store.js +38 -12
- package/dist/approval-api/d1-store.js.map +1 -1
- package/dist/approval-api/grants.d.ts +1 -1
- package/dist/approval-api/grants.d.ts.map +1 -1
- package/dist/approval-api/grants.js +8 -32
- package/dist/approval-api/grants.js.map +1 -1
- package/dist/approval-api/index.d.ts +1 -1
- package/dist/approval-api/index.d.ts.map +1 -1
- package/dist/approval-api/index.js.map +1 -1
- package/dist/approval-api/service.d.ts +22 -1
- package/dist/approval-api/service.d.ts.map +1 -1
- package/dist/approval-api/service.js +101 -0
- package/dist/approval-api/service.js.map +1 -1
- package/dist/approval-api/store.d.ts +19 -0
- package/dist/approval-api/store.d.ts.map +1 -1
- package/dist/approval-api/store.js +47 -2
- package/dist/approval-api/store.js.map +1 -1
- package/dist/approval-api/tenant-context.d.ts.map +1 -1
- package/dist/approval-api/tenant-context.js +2 -1
- package/dist/approval-api/tenant-context.js.map +1 -1
- package/dist/approval-api/tenant-store.d.ts.map +1 -1
- package/dist/approval-api/tenant-store.js +8 -2
- package/dist/approval-api/tenant-store.js.map +1 -1
- package/dist/approval-api/types.d.ts +11 -0
- package/dist/approval-api/types.d.ts.map +1 -1
- package/dist/approval-api/types.js +16 -0
- package/dist/approval-api/types.js.map +1 -1
- package/dist/approval-ui/App.d.ts +9 -1
- package/dist/approval-ui/App.d.ts.map +1 -1
- package/dist/approval-ui/App.js +10 -5
- package/dist/approval-ui/App.js.map +1 -1
- package/dist/approval-ui/DetailView.d.ts +4 -1
- package/dist/approval-ui/DetailView.d.ts.map +1 -1
- package/dist/approval-ui/DetailView.js +2 -2
- package/dist/approval-ui/DetailView.js.map +1 -1
- package/dist/approval-ui/QueueView.d.ts +7 -1
- package/dist/approval-ui/QueueView.d.ts.map +1 -1
- package/dist/approval-ui/QueueView.js +8 -3
- package/dist/approval-ui/QueueView.js.map +1 -1
- package/dist/approval-ui/client.d.ts +19 -0
- package/dist/approval-ui/client.d.ts.map +1 -1
- package/dist/approval-ui/client.js +29 -1
- package/dist/approval-ui/client.js.map +1 -1
- package/dist/approval-ui/components.d.ts +20 -0
- package/dist/approval-ui/components.d.ts.map +1 -1
- package/dist/approval-ui/components.js +2 -0
- package/dist/approval-ui/components.js.map +1 -1
- package/dist/approval-ui/index.d.ts +4 -2
- package/dist/approval-ui/index.d.ts.map +1 -1
- package/dist/approval-ui/index.js +3 -0
- package/dist/approval-ui/index.js.map +1 -1
- package/dist/approval-ui/stream.d.ts +142 -0
- package/dist/approval-ui/stream.d.ts.map +1 -0
- package/dist/approval-ui/stream.js +136 -0
- package/dist/approval-ui/stream.js.map +1 -0
- package/dist/approval-ui/use-approval-dashboard.d.ts +74 -1
- package/dist/approval-ui/use-approval-dashboard.d.ts.map +1 -1
- package/dist/approval-ui/use-approval-dashboard.js +307 -7
- package/dist/approval-ui/use-approval-dashboard.js.map +1 -1
- package/dist/approval-ui/use-web-socket-transport.d.ts +10 -0
- package/dist/approval-ui/use-web-socket-transport.d.ts.map +1 -0
- package/dist/approval-ui/use-web-socket-transport.js +46 -0
- package/dist/approval-ui/use-web-socket-transport.js.map +1 -0
- package/dist/do-runner/cf-types.d.ts +70 -2
- package/dist/do-runner/cf-types.d.ts.map +1 -1
- package/dist/do-runner/cf-types.js +30 -1
- package/dist/do-runner/cf-types.js.map +1 -1
- package/dist/do-runner/d1-storage.js +1 -1
- package/dist/do-runner/d1-storage.js.map +1 -1
- package/dist/do-runner/durable-object.d.ts +4 -1
- package/dist/do-runner/durable-object.d.ts.map +1 -1
- package/dist/do-runner/durable-object.js +85 -4
- package/dist/do-runner/durable-object.js.map +1 -1
- package/dist/do-runner/hub-do.d.ts +55 -0
- package/dist/do-runner/hub-do.d.ts.map +1 -0
- package/dist/do-runner/hub-do.js +197 -0
- package/dist/do-runner/hub-do.js.map +1 -0
- package/dist/do-runner/index.d.ts +3 -1
- package/dist/do-runner/index.d.ts.map +1 -1
- package/dist/do-runner/index.js +1 -0
- package/dist/do-runner/index.js.map +1 -1
- package/dist/do-runner/path-safe-id.js +1 -1
- package/dist/do-runner/path-safe-id.js.map +1 -1
- package/dist/host-kit/flowsafe-worker.d.ts +23 -1
- package/dist/host-kit/flowsafe-worker.d.ts.map +1 -1
- package/dist/host-kit/flowsafe-worker.js +55 -9
- package/dist/host-kit/flowsafe-worker.js.map +1 -1
- package/dist/host-kit/host-approval-service.d.ts +20 -1
- package/dist/host-kit/host-approval-service.d.ts.map +1 -1
- package/dist/host-kit/host-approval-service.js +15 -0
- package/dist/host-kit/host-approval-service.js.map +1 -1
- package/dist/host-kit/hub-topology.d.ts +35 -0
- package/dist/host-kit/hub-topology.d.ts.map +1 -0
- package/dist/host-kit/hub-topology.js +32 -0
- package/dist/host-kit/hub-topology.js.map +1 -0
- package/dist/host-kit/index.d.ts +5 -0
- package/dist/host-kit/index.d.ts.map +1 -1
- package/dist/host-kit/index.js +3 -0
- package/dist/host-kit/index.js.map +1 -1
- package/dist/host-kit/stream-router.d.ts +22 -0
- package/dist/host-kit/stream-router.d.ts.map +1 -0
- package/dist/host-kit/stream-router.js +191 -0
- package/dist/host-kit/stream-router.js.map +1 -0
- package/dist/host-kit/stream-ticket.d.ts +55 -0
- package/dist/host-kit/stream-ticket.d.ts.map +1 -0
- package/dist/host-kit/stream-ticket.js +156 -0
- package/dist/host-kit/stream-ticket.js.map +1 -0
- package/dist/host-kit/tenant-registry.js +1 -1
- package/dist/host-kit/tenant-registry.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/package.json +2 -2
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The live-streaming seam and its pure reducers. Deliberately DOM-free: the
|
|
3
|
+
// transport is INJECTED behind a structural interface (like client.ts's
|
|
4
|
+
// FetchLike), so this file typechecks in the main (workers-typed) pass with no
|
|
5
|
+
// DOM lib — a browser-WebSocket reference here would break that build far from
|
|
6
|
+
// the cause. The only place a `WebSocket` global lives is the UI-pass-only
|
|
7
|
+
// use-web-socket-transport.ts, which supplies a StreamTransport by injection.
|
|
8
|
+
//
|
|
9
|
+
// The reducers are pure and node-testable (stream.test.ts): they take the
|
|
10
|
+
// current dashboard state plus one wire event and return the next state, so all
|
|
11
|
+
// live-merge / optimistic-decide / reconcile logic stays out of the React hook.
|
|
12
|
+
// Wire types come from ../approval-api — the frame payloads ARE those types
|
|
13
|
+
// serialized (ApprovalStreamEvent for the queue channel); RunSummary and the
|
|
14
|
+
// presence roster are mirrored as minimal local structural shapes so this
|
|
15
|
+
// module keeps approval-ui's "no Mastra, no do-runner" decoupling.
|
|
16
|
+
import { byReviewerOrder, } from '../approval-api/types.js';
|
|
17
|
+
/**
|
|
18
|
+
* Parse a raw text frame to a StreamFrame, or undefined for anything malformed
|
|
19
|
+
* or of an unknown type. This is a same-trust intra-tenant feed (every
|
|
20
|
+
* subscriber is an authenticated reviewer of this tenant), so it validates the
|
|
21
|
+
* discriminant only, not the payload shape.
|
|
22
|
+
*/
|
|
23
|
+
export function parseStreamFrame(data) {
|
|
24
|
+
let parsed;
|
|
25
|
+
try {
|
|
26
|
+
parsed = JSON.parse(data);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
if (typeof parsed !== 'object' || parsed === null)
|
|
32
|
+
return undefined;
|
|
33
|
+
const { type } = parsed;
|
|
34
|
+
if (type === 'queue' || type === 'run' || type === 'presence') {
|
|
35
|
+
return parsed;
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Live-merge one queue event: upsert the post-transition record by id, drop it
|
|
41
|
+
* when it leaves the current status filter, and re-sort by the SAME comparator
|
|
42
|
+
* the hook renders with (byReviewerOrder). Non-mutating.
|
|
43
|
+
*/
|
|
44
|
+
export function mergeApprovalEvent(records, event, options = {}) {
|
|
45
|
+
const { statuses, comparator = byReviewerOrder } = options;
|
|
46
|
+
const incoming = event.record;
|
|
47
|
+
const withoutIncoming = records.filter((record) => record.id !== incoming.id);
|
|
48
|
+
const leavesFilter = statuses !== undefined && !statuses.includes(incoming.status);
|
|
49
|
+
const next = leavesFilter ? withoutIncoming : [...withoutIncoming, incoming];
|
|
50
|
+
return next.sort(comparator);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Mark a record locally decided (it greys out immediately) and return the
|
|
54
|
+
* pending descriptor to track until the authoritative event reconciles it.
|
|
55
|
+
* Non-mutating.
|
|
56
|
+
*/
|
|
57
|
+
export function applyOptimisticDecide(records, id, decision, actorId) {
|
|
58
|
+
const status = decision === 'approve' ? 'approved' : 'rejected';
|
|
59
|
+
const next = records.map((record) => record.id === id
|
|
60
|
+
? { ...record, status, decision, decidedBy: actorId }
|
|
61
|
+
: record);
|
|
62
|
+
return { records: next, pending: { id, decision, actorId } };
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Reconcile an authoritative decided event against the optimistic pending map:
|
|
66
|
+
* clear the matching entry, and surface a conflict when the authoritative
|
|
67
|
+
* decidedBy differs from the reviewer who optimistically decided (someone else
|
|
68
|
+
* decided first). A pending entry with an empty actorId (no id was supplied to
|
|
69
|
+
* the hook) clears WITHOUT raising a conflict — there is no identity to compare.
|
|
70
|
+
* Non-mutating.
|
|
71
|
+
*/
|
|
72
|
+
export function reconcileDecided(pending, event) {
|
|
73
|
+
const { id, decidedBy } = event.record;
|
|
74
|
+
const entry = pending[id];
|
|
75
|
+
if (entry === undefined)
|
|
76
|
+
return { pending };
|
|
77
|
+
const rest = {};
|
|
78
|
+
for (const [key, value] of Object.entries(pending)) {
|
|
79
|
+
if (key !== id)
|
|
80
|
+
rest[key] = value;
|
|
81
|
+
}
|
|
82
|
+
const conflict = entry.actorId !== '' &&
|
|
83
|
+
decidedBy !== undefined &&
|
|
84
|
+
decidedBy !== entry.actorId
|
|
85
|
+
? { id, actualDecider: decidedBy }
|
|
86
|
+
: undefined;
|
|
87
|
+
return { pending: rest, conflict };
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Reduce a presence roster frame: distinct reviewers by actorId (a reviewer
|
|
91
|
+
* with two tabs counts once), in a stable order. Non-mutating.
|
|
92
|
+
*/
|
|
93
|
+
export function presenceReducer(roster) {
|
|
94
|
+
const seen = new Set();
|
|
95
|
+
const distinct = [];
|
|
96
|
+
for (const member of roster) {
|
|
97
|
+
if (!seen.has(member.actorId)) {
|
|
98
|
+
seen.add(member.actorId);
|
|
99
|
+
distinct.push({ actorId: member.actorId, role: member.role });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return distinct.sort((a, b) => a.actorId < b.actorId ? -1 : a.actorId > b.actorId ? 1 : 0);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Optimistic metrics counter deltas (DL-013): apply the derivable movement on
|
|
106
|
+
* each event for instant feedback; the interval poll's wholesale metrics()
|
|
107
|
+
* refetch is the authoritative reconciler (DL-021). Counts never go negative.
|
|
108
|
+
* Returns the same reference when nothing derivable changed (no re-render), and
|
|
109
|
+
* null when metrics have not loaded yet.
|
|
110
|
+
*/
|
|
111
|
+
export function applyMetricsDelta(metrics, event) {
|
|
112
|
+
if (metrics === null)
|
|
113
|
+
return null;
|
|
114
|
+
switch (event.type) {
|
|
115
|
+
case 'created':
|
|
116
|
+
return { ...metrics, openCount: metrics.openCount + 1 };
|
|
117
|
+
case 'decided': {
|
|
118
|
+
const approved = event.record.decision === 'approve';
|
|
119
|
+
return {
|
|
120
|
+
...metrics,
|
|
121
|
+
openCount: Math.max(0, metrics.openCount - 1),
|
|
122
|
+
decidedCount: metrics.decidedCount + 1,
|
|
123
|
+
approvedCount: metrics.approvedCount + (approved ? 1 : 0),
|
|
124
|
+
rejectedCount: metrics.rejectedCount + (approved ? 0 : 1),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
case 'escalated':
|
|
128
|
+
return { ...metrics, escalationCount: metrics.escalationCount + 1 };
|
|
129
|
+
case 'superseded':
|
|
130
|
+
return { ...metrics, openCount: Math.max(0, metrics.openCount - 1) };
|
|
131
|
+
default:
|
|
132
|
+
// claimed / delegated leave the counters untouched.
|
|
133
|
+
return metrics;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/approval-ui/stream.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,4EAA4E;AAC5E,wEAAwE;AACxE,+EAA+E;AAC/E,+EAA+E;AAC/E,2EAA2E;AAC3E,8EAA8E;AAC9E,EAAE;AACF,0EAA0E;AAC1E,gFAAgF;AAChF,gFAAgF;AAChF,4EAA4E;AAC5E,6EAA6E;AAC7E,0EAA0E;AAC1E,mEAAmE;AAGnE,OAAO,EAKL,eAAe,GAChB,MAAM,0BAA0B,CAAC;AAyElC;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAY;IAC3C,IAAI,MAAe,CAAC;IACpB,IAAI,CAAC;QACH,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC5B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;IACD,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI;QAAE,OAAO,SAAS,CAAC;IACpE,MAAM,EAAE,IAAI,EAAE,GAAG,MAA4B,CAAC;IAC9C,IAAI,IAAI,KAAK,OAAO,IAAI,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,UAAU,EAA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G,CAAC,EAAE,CAAC;QACtE,KAAK,YAAY;YACf,OAAO,EAAE,GAAG,OAAO,EAAE,SAAS,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,CAAC,SAAS,GAAG,CAAC,CAAC,EAAE,CAAC;QACvE;YACE,oDAAoD;YACpD,OAAO,OAAO,CAAC;IACnB,CAAC;AACH,CAAC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ApprovalDecision, ApprovalListFilter, ApprovalMetrics, ApprovalRecord, BatchDecideResult } from '../approval-api/types.js';
|
|
2
2
|
import type { ApprovalApiClient } from './client.js';
|
|
3
|
+
import { type DecisionConflict, type PresenceMember, type StreamConnection, type StreamFrame, type StreamTransport } from './stream.js';
|
|
3
4
|
/**
|
|
4
5
|
* The dashboard's default queue slice: open (non-terminal) requests only,
|
|
5
6
|
* page-bounded at 100 in REVIEWER order. The bound is D3 — an unfiltered
|
|
@@ -71,6 +72,51 @@ export interface ApprovalFilterOverride {
|
|
|
71
72
|
* DOM-free testing; the hook applies it every render.
|
|
72
73
|
*/
|
|
73
74
|
export declare function effectiveApprovalFilter(override: ApprovalFilterOverride | null, optionsFilter: ApprovalListFilter): ApprovalListFilter;
|
|
75
|
+
/**
|
|
76
|
+
* A cancelable timer seam — injected in tests, defaults to the host globals.
|
|
77
|
+
* Kept structural (no ambient-timer types) so it typechecks in both the UI and
|
|
78
|
+
* the transitively-included workers-typed test pass.
|
|
79
|
+
*/
|
|
80
|
+
export interface StreamScheduler {
|
|
81
|
+
setTimeout(handler: () => void, ms: number): ReturnType<typeof setTimeout>;
|
|
82
|
+
clearTimeout(handle: ReturnType<typeof setTimeout>): void;
|
|
83
|
+
}
|
|
84
|
+
/** Reconnect backoff (ms); the last entry repeats for every further attempt. */
|
|
85
|
+
export declare const DEFAULT_STREAM_RECONNECT_MS: readonly number[];
|
|
86
|
+
/** Heartbeat ping cadence (ms) — how often the client proves the socket is live. */
|
|
87
|
+
export declare const DEFAULT_STREAM_HEARTBEAT_MS = 20000;
|
|
88
|
+
/** Pong deadline (ms) after a ping — a missed pong forces a reconnect. */
|
|
89
|
+
export declare const DEFAULT_STREAM_HEARTBEAT_TIMEOUT_MS = 10000;
|
|
90
|
+
/** The injected stream option: a transport plus a ticket() thunk (both from the host). */
|
|
91
|
+
export interface ApprovalStreamOption {
|
|
92
|
+
transport: StreamTransport;
|
|
93
|
+
/** Mints a fresh addressing ticket; the socket URL is `url + ticket`. */
|
|
94
|
+
ticket: () => Promise<{
|
|
95
|
+
url: string;
|
|
96
|
+
ticket: string;
|
|
97
|
+
}>;
|
|
98
|
+
}
|
|
99
|
+
export interface SubscribeApprovalStreamOptions extends ApprovalStreamOption {
|
|
100
|
+
onFrame: (frame: StreamFrame) => void;
|
|
101
|
+
/** Fires on each successful (re)open — the run channel resumes pausing its poll here. */
|
|
102
|
+
onOpen?: () => void;
|
|
103
|
+
/** Fires once per disconnect (before the backoff reconnect) — poll fallback resumes here. */
|
|
104
|
+
onClose?: () => void;
|
|
105
|
+
reconnectDelaysMs?: readonly number[];
|
|
106
|
+
scheduler?: StreamScheduler;
|
|
107
|
+
/** Heartbeat ping cadence (ms). Default DEFAULT_STREAM_HEARTBEAT_MS. */
|
|
108
|
+
heartbeatMs?: number;
|
|
109
|
+
/** Pong deadline (ms) after a ping. Default DEFAULT_STREAM_HEARTBEAT_TIMEOUT_MS. */
|
|
110
|
+
heartbeatTimeoutMs?: number;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Open a stream and keep it open across drops (fetch a ticket, open the injected
|
|
114
|
+
* transport, parse each frame to onFrame, reconnect with backoff on
|
|
115
|
+
* close/error). Pure and node-testable — the React hook wires onFrame to its
|
|
116
|
+
* reducers. DOM-free: the transport and timers are injected. Returns a
|
|
117
|
+
* StreamConnection whose close() stops reconnecting and closes the live socket.
|
|
118
|
+
*/
|
|
119
|
+
export declare function subscribeApprovalStream({ transport, ticket, onFrame, onOpen, onClose, reconnectDelaysMs, scheduler, heartbeatMs, heartbeatTimeoutMs, }: SubscribeApprovalStreamOptions): StreamConnection;
|
|
74
120
|
export interface UseApprovalDashboardOptions {
|
|
75
121
|
/** Queue/metrics refresh cadence; <= 0 disables polling. Default 10s. */
|
|
76
122
|
pollIntervalMs?: number;
|
|
@@ -82,6 +128,20 @@ export interface UseApprovalDashboardOptions {
|
|
|
82
128
|
* dashboard never issues an unfiltered full-table scan.
|
|
83
129
|
*/
|
|
84
130
|
filter?: ApprovalListFilter;
|
|
131
|
+
/**
|
|
132
|
+
* Live streaming (Part B): an injected StreamTransport + a ticket() thunk.
|
|
133
|
+
* ABSENT ⇒ poll-only, exactly today's behavior. When present the hook opens
|
|
134
|
+
* the tenant's approval stream and live-merges events on top of the interval
|
|
135
|
+
* poll, which keeps running as the periodic reconciler (DL-021).
|
|
136
|
+
*/
|
|
137
|
+
stream?: ApprovalStreamOption;
|
|
138
|
+
/**
|
|
139
|
+
* The current reviewer's id — attributes an optimistic decide so a live
|
|
140
|
+
* 'decided' event naming a DIFFERENT decider surfaces a conflict. Absent ⇒
|
|
141
|
+
* optimistic decide still greys the row, but no conflict is ever raised (no
|
|
142
|
+
* id to compare against); the host supplies it (showcase, M-008).
|
|
143
|
+
*/
|
|
144
|
+
actorId?: string;
|
|
85
145
|
}
|
|
86
146
|
export interface ApprovalDashboardState {
|
|
87
147
|
/**
|
|
@@ -123,6 +183,19 @@ export interface ApprovalDashboardState {
|
|
|
123
183
|
claim: () => void;
|
|
124
184
|
delegate: (to: string) => void;
|
|
125
185
|
refresh: () => Promise<void>;
|
|
186
|
+
/**
|
|
187
|
+
* Reviewers currently connected to the tenant's live stream (deduped by
|
|
188
|
+
* actorId). Empty in poll-only mode.
|
|
189
|
+
*/
|
|
190
|
+
presence: PresenceMember[];
|
|
191
|
+
/**
|
|
192
|
+
* Set when a live 'decided' event attributes a record this reviewer
|
|
193
|
+
* optimistically decided to a DIFFERENT decider (they decided first). null
|
|
194
|
+
* otherwise. Never set in poll-only mode or without an actorId.
|
|
195
|
+
*/
|
|
196
|
+
conflict: DecisionConflict | null;
|
|
197
|
+
/** Clears the current conflict surface (Toast dismissal). */
|
|
198
|
+
dismissConflict: () => void;
|
|
126
199
|
}
|
|
127
|
-
export declare function useApprovalDashboard(client: ApprovalApiClient, { pollIntervalMs, now, filter, }?: UseApprovalDashboardOptions): ApprovalDashboardState;
|
|
200
|
+
export declare function useApprovalDashboard(client: ApprovalApiClient, { pollIntervalMs, now, filter, stream, actorId, }?: UseApprovalDashboardOptions): ApprovalDashboardState;
|
|
128
201
|
//# sourceMappingURL=use-approval-dashboard.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-approval-dashboard.d.ts","sourceRoot":"","sources":["../../src/approval-ui/use-approval-dashboard.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EACV,gBAAgB,EAChB,kBAAkB,EAClB,eAAe,EACf,cAAc,
|
|
1
|
+
{"version":3,"file":"use-approval-dashboard.d.ts","sourceRoot":"","sources":["../../src/approval-ui/use-approval-dashboard.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EACV,gBAAgB,EAChB,kBAAkB,EAClB,eAAe,EACf,cAAc,EAEd,iBAAiB,EAClB,MAAM,0BAA0B,CAAC;AAElC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAGL,KAAK,gBAAgB,EAGrB,KAAK,cAAc,EAInB,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAEhB,KAAK,eAAe,EACrB,MAAM,aAAa,CAAC;AAGrB;;;;;;;;;;GAUG;AACH,eAAO,MAAM,oBAAoB,EAAE,kBAIlC,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,kBAAkB,GAAG,MAAM,CAEpE;AAED;;;;;;;;;GASG;AACH,wBAAsB,sBAAsB,CAC1C,MAAM,EAAE,IAAI,CAAC,iBAAiB,EAAE,MAAM,GAAG,SAAS,CAAC,E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ef, useState } from 'react';
|
|
8
8
|
import { OPEN_STATUSES } from '../approval-api/types.js';
|
|
9
|
+
import { applyMetricsDelta, applyOptimisticDecide, mergeApprovalEvent, parseStreamFrame, presenceReducer, reconcileDecided, } from './stream.js';
|
|
9
10
|
import { sortQueue } from './view-model.js';
|
|
10
11
|
/**
|
|
11
12
|
* The dashboard's default queue slice: open (non-terminal) requests only,
|
|
@@ -91,7 +92,183 @@ export function effectiveApprovalFilter(override, optionsFilter) {
|
|
|
91
92
|
? override.filter
|
|
92
93
|
: optionsFilter;
|
|
93
94
|
}
|
|
94
|
-
|
|
95
|
+
/** Normalize an ApprovalListFilter.status into a plain status array (or undefined = all). */
|
|
96
|
+
function statusFilterOf(filter) {
|
|
97
|
+
if (filter.status === undefined)
|
|
98
|
+
return undefined;
|
|
99
|
+
return Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
100
|
+
}
|
|
101
|
+
const DEFAULT_STREAM_SCHEDULER = {
|
|
102
|
+
setTimeout: (handler, ms) => setTimeout(handler, ms),
|
|
103
|
+
clearTimeout: (handle) => {
|
|
104
|
+
clearTimeout(handle);
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
/** Reconnect backoff (ms); the last entry repeats for every further attempt. */
|
|
108
|
+
export const DEFAULT_STREAM_RECONNECT_MS = [
|
|
109
|
+
1_000, 2_000, 5_000, 10_000,
|
|
110
|
+
];
|
|
111
|
+
/** Heartbeat ping cadence (ms) — how often the client proves the socket is live. */
|
|
112
|
+
export const DEFAULT_STREAM_HEARTBEAT_MS = 20_000;
|
|
113
|
+
/** Pong deadline (ms) after a ping — a missed pong forces a reconnect. */
|
|
114
|
+
export const DEFAULT_STREAM_HEARTBEAT_TIMEOUT_MS = 10_000;
|
|
115
|
+
/**
|
|
116
|
+
* A permanent (non-retryable) ticket failure: the host is not serving streaming
|
|
117
|
+
* (no STREAM_TICKET_SECRET ⇒ the ticket route 404s), or the caller is not
|
|
118
|
+
* permitted to stream. A 4xx means a retry cannot succeed, so the subscription
|
|
119
|
+
* gives up and the client stays cleanly poll-only — never hammering the ticket
|
|
120
|
+
* route with endless background 404s (F4). A 5xx or a network error (no status)
|
|
121
|
+
* stays transient and reconnects with backoff.
|
|
122
|
+
*/
|
|
123
|
+
function isPermanentStreamError(cause) {
|
|
124
|
+
const status = cause?.status;
|
|
125
|
+
return typeof status === 'number' && status >= 400 && status < 500;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Open a stream and keep it open across drops (fetch a ticket, open the injected
|
|
129
|
+
* transport, parse each frame to onFrame, reconnect with backoff on
|
|
130
|
+
* close/error). Pure and node-testable — the React hook wires onFrame to its
|
|
131
|
+
* reducers. DOM-free: the transport and timers are injected. Returns a
|
|
132
|
+
* StreamConnection whose close() stops reconnecting and closes the live socket.
|
|
133
|
+
*/
|
|
134
|
+
export function subscribeApprovalStream({ transport, ticket, onFrame, onOpen, onClose, reconnectDelaysMs = DEFAULT_STREAM_RECONNECT_MS, scheduler = DEFAULT_STREAM_SCHEDULER, heartbeatMs = DEFAULT_STREAM_HEARTBEAT_MS, heartbeatTimeoutMs = DEFAULT_STREAM_HEARTBEAT_TIMEOUT_MS, }) {
|
|
135
|
+
let active = true;
|
|
136
|
+
let connection;
|
|
137
|
+
let disconnected = false;
|
|
138
|
+
let attempt = 0;
|
|
139
|
+
let timer;
|
|
140
|
+
let heartbeatTimer;
|
|
141
|
+
let livenessTimer;
|
|
142
|
+
const clearHeartbeat = () => {
|
|
143
|
+
if (heartbeatTimer !== undefined) {
|
|
144
|
+
scheduler.clearTimeout(heartbeatTimer);
|
|
145
|
+
heartbeatTimer = undefined;
|
|
146
|
+
}
|
|
147
|
+
if (livenessTimer !== undefined) {
|
|
148
|
+
scheduler.clearTimeout(livenessTimer);
|
|
149
|
+
livenessTimer = undefined;
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
const scheduleReconnect = () => {
|
|
153
|
+
if (!active)
|
|
154
|
+
return;
|
|
155
|
+
const index = Math.min(attempt, reconnectDelaysMs.length - 1);
|
|
156
|
+
const delay = reconnectDelaysMs[index] ?? 1_000;
|
|
157
|
+
attempt += 1;
|
|
158
|
+
timer = scheduler.setTimeout(() => {
|
|
159
|
+
if (active)
|
|
160
|
+
void connect();
|
|
161
|
+
}, delay);
|
|
162
|
+
};
|
|
163
|
+
// Single disconnect funnel — a socket that fires onerror THEN onclose, or a
|
|
164
|
+
// missed-pong force-close, must schedule exactly one reconnect. Closes the
|
|
165
|
+
// (possibly half-open) socket so the transport releases it.
|
|
166
|
+
const handleDisconnect = () => {
|
|
167
|
+
if (!active || disconnected)
|
|
168
|
+
return;
|
|
169
|
+
disconnected = true;
|
|
170
|
+
clearHeartbeat();
|
|
171
|
+
connection?.close();
|
|
172
|
+
connection = undefined;
|
|
173
|
+
onClose?.();
|
|
174
|
+
scheduleReconnect();
|
|
175
|
+
};
|
|
176
|
+
// Liveness heartbeat (F1): a browser cannot detect a silently half-open socket
|
|
177
|
+
// (sleep/wake, NAT drop) — it never fires onclose — so a broadcast-only run
|
|
178
|
+
// channel would sit paused on stale state indefinitely. Ping on an interval;
|
|
179
|
+
// ANY inbound frame (the DO answers 'ping' with 'pong', which parseStreamFrame
|
|
180
|
+
// ignores) proves liveness and clears the deadline; a missed pong forces a
|
|
181
|
+
// disconnect so the poll resumes and the socket reconnects. A transport with
|
|
182
|
+
// no send() gets no heartbeat and relies on its own onClose.
|
|
183
|
+
const armLiveness = () => {
|
|
184
|
+
if (livenessTimer !== undefined)
|
|
185
|
+
scheduler.clearTimeout(livenessTimer);
|
|
186
|
+
livenessTimer = scheduler.setTimeout(() => {
|
|
187
|
+
livenessTimer = undefined;
|
|
188
|
+
handleDisconnect();
|
|
189
|
+
}, heartbeatTimeoutMs);
|
|
190
|
+
};
|
|
191
|
+
const scheduleHeartbeat = () => {
|
|
192
|
+
heartbeatTimer = scheduler.setTimeout(() => {
|
|
193
|
+
heartbeatTimer = undefined;
|
|
194
|
+
if (!active)
|
|
195
|
+
return;
|
|
196
|
+
const conn = connection;
|
|
197
|
+
if (!conn?.send)
|
|
198
|
+
return; // no heartbeat capability — rely on the socket's onClose
|
|
199
|
+
try {
|
|
200
|
+
conn.send('ping');
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
handleDisconnect();
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
armLiveness();
|
|
207
|
+
scheduleHeartbeat();
|
|
208
|
+
}, heartbeatMs);
|
|
209
|
+
};
|
|
210
|
+
const handlers = {
|
|
211
|
+
onMessage: (data) => {
|
|
212
|
+
// Any inbound frame proves the socket is alive — clear the pong deadline.
|
|
213
|
+
if (livenessTimer !== undefined) {
|
|
214
|
+
scheduler.clearTimeout(livenessTimer);
|
|
215
|
+
livenessTimer = undefined;
|
|
216
|
+
}
|
|
217
|
+
const frame = parseStreamFrame(data);
|
|
218
|
+
if (frame)
|
|
219
|
+
onFrame(frame);
|
|
220
|
+
},
|
|
221
|
+
onOpen: () => {
|
|
222
|
+
attempt = 0;
|
|
223
|
+
onOpen?.();
|
|
224
|
+
if (connection?.send)
|
|
225
|
+
scheduleHeartbeat();
|
|
226
|
+
},
|
|
227
|
+
onClose: handleDisconnect,
|
|
228
|
+
onError: handleDisconnect,
|
|
229
|
+
};
|
|
230
|
+
async function connect() {
|
|
231
|
+
if (!active)
|
|
232
|
+
return;
|
|
233
|
+
disconnected = false;
|
|
234
|
+
let address;
|
|
235
|
+
try {
|
|
236
|
+
address = await ticket();
|
|
237
|
+
}
|
|
238
|
+
catch (cause) {
|
|
239
|
+
if (isPermanentStreamError(cause)) {
|
|
240
|
+
// Streaming is not mounted / not permitted here — stop retrying and stay
|
|
241
|
+
// cleanly poll-only (F4), never hammering the ticket route forever.
|
|
242
|
+
active = false;
|
|
243
|
+
clearHeartbeat();
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
// A transient ticket() failure is a disconnect — back off and retry.
|
|
247
|
+
handleDisconnect();
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (!active)
|
|
251
|
+
return;
|
|
252
|
+
try {
|
|
253
|
+
connection = transport.open(`${address.url}${address.ticket}`, handlers);
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
// A failed open() is a disconnect — back off and retry.
|
|
257
|
+
handleDisconnect();
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
void connect();
|
|
261
|
+
return {
|
|
262
|
+
close: () => {
|
|
263
|
+
active = false;
|
|
264
|
+
clearHeartbeat();
|
|
265
|
+
if (timer !== undefined)
|
|
266
|
+
scheduler.clearTimeout(timer);
|
|
267
|
+
connection?.close();
|
|
268
|
+
},
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
export function useApprovalDashboard(client, { pollIntervalMs = 10_000, now = Date.now, filter = DEFAULT_QUEUE_FILTER, stream, actorId, } = {}) {
|
|
95
272
|
const [records, setRecords] = useState([]);
|
|
96
273
|
const [metrics, setMetrics] = useState(null);
|
|
97
274
|
const [selectedId, setSelectedId] = useState(null);
|
|
@@ -101,6 +278,12 @@ export function useApprovalDashboard(client, { pollIntervalMs = 10_000, now = Da
|
|
|
101
278
|
const [override, setOverride] = useState(null);
|
|
102
279
|
const [rawSelectedIds, setRawSelectedIds] = useState([]);
|
|
103
280
|
const [lastBatch, setLastBatch] = useState(null);
|
|
281
|
+
const [presence, setPresence] = useState([]);
|
|
282
|
+
const [conflict, setConflict] = useState(null);
|
|
283
|
+
// Optimistic decisions are a transient tracker, never rendered directly (the
|
|
284
|
+
// greyed row lives in `records`), so a ref avoids a re-render per mutation
|
|
285
|
+
// and gives the frame handler a synchronous read for conflict detection.
|
|
286
|
+
const pendingRef = useRef({});
|
|
104
287
|
// Latest-ref for the injected clock: an inline `now` (a fixed test/story
|
|
105
288
|
// clock) is a new function identity every render — the same request-loop
|
|
106
289
|
// class as an inline filter, but a function cannot be value-keyed, so it
|
|
@@ -124,6 +307,12 @@ export function useApprovalDashboard(client, { pollIntervalMs = 10_000, now = Da
|
|
|
124
307
|
// request loop that never reached the poll interval (2026-07-11 review).
|
|
125
308
|
const filterKey = approvalFilterKey(effective);
|
|
126
309
|
const stableFilter = useMemo(() => JSON.parse(filterKey), [filterKey]);
|
|
310
|
+
// The live-merge status filter, read by the (stable) frame handler via a ref
|
|
311
|
+
// so a filter change never re-subscribes the socket. Kept current here.
|
|
312
|
+
const statusesRef = useRef(statusFilterOf(stableFilter));
|
|
313
|
+
useEffect(() => {
|
|
314
|
+
statusesRef.current = statusFilterOf(stableFilter);
|
|
315
|
+
}, [stableFilter]);
|
|
127
316
|
// `client` stays an identity dependency on purpose: a NEW client means a
|
|
128
317
|
// new endpoint/authorization (the showcase actor switcher), which must
|
|
129
318
|
// refetch immediately — unlike filter/now, its identity carries meaning.
|
|
@@ -160,6 +349,46 @@ export function useApprovalDashboard(client, { pollIntervalMs = 10_000, now = Da
|
|
|
160
349
|
const clearSelection = useCallback(() => {
|
|
161
350
|
setRawSelectedIds([]);
|
|
162
351
|
}, []);
|
|
352
|
+
const dismissConflict = useCallback(() => {
|
|
353
|
+
setConflict(null);
|
|
354
|
+
}, []);
|
|
355
|
+
// Route one live frame into state. Stable ([] deps) — it reads current values
|
|
356
|
+
// through refs and the functional setState updaters, so the subscription
|
|
357
|
+
// effect never re-subscribes when the filter or queue changes (guideline 8.3).
|
|
358
|
+
const handleFrame = useCallback((frame) => {
|
|
359
|
+
if (frame.type === 'queue') {
|
|
360
|
+
const { event } = frame;
|
|
361
|
+
setRecords((current) => mergeApprovalEvent(current, event, { statuses: statusesRef.current }));
|
|
362
|
+
setMetrics((current) => applyMetricsDelta(current, event));
|
|
363
|
+
if (event.type === 'decided') {
|
|
364
|
+
const { pending: next, conflict: found } = reconcileDecided(pendingRef.current, event);
|
|
365
|
+
pendingRef.current = next;
|
|
366
|
+
if (found)
|
|
367
|
+
setConflict(found);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
else if (frame.type === 'presence') {
|
|
371
|
+
setPresence(presenceReducer(frame.roster));
|
|
372
|
+
}
|
|
373
|
+
// 'run' frames carry the wholesale RunSummary for the per-run view (M-008);
|
|
374
|
+
// this queue-focused dashboard holds no run state, so it ignores them.
|
|
375
|
+
}, []);
|
|
376
|
+
// Live subscription — additive to the poll, which keeps running as the queue
|
|
377
|
+
// reconciler (DL-021). Narrow deps (guideline 5.7): re-subscribe only when the
|
|
378
|
+
// injected transport/ticket identity changes (a new client/auth), never on a
|
|
379
|
+
// state or filter change. Absent stream ⇒ no subscription (poll-only).
|
|
380
|
+
const streamTransport = stream?.transport;
|
|
381
|
+
const streamTicket = stream?.ticket;
|
|
382
|
+
useEffect(() => {
|
|
383
|
+
if (streamTransport === undefined || streamTicket === undefined)
|
|
384
|
+
return;
|
|
385
|
+
const connection = subscribeApprovalStream({
|
|
386
|
+
transport: streamTransport,
|
|
387
|
+
ticket: streamTicket,
|
|
388
|
+
onFrame: handleFrame,
|
|
389
|
+
});
|
|
390
|
+
return () => connection.close();
|
|
391
|
+
}, [streamTransport, streamTicket, handleFrame]);
|
|
163
392
|
const act = useCallback(async (action) => {
|
|
164
393
|
setBusy(true);
|
|
165
394
|
// Any new mutation supersedes the previous batch summary.
|
|
@@ -179,17 +408,85 @@ export function useApprovalDashboard(client, { pollIntervalMs = 10_000, now = Da
|
|
|
179
408
|
const ids = selectedIds;
|
|
180
409
|
if (ids.length === 0)
|
|
181
410
|
return;
|
|
411
|
+
// Optimistic: grey every selected row and track each pending decision.
|
|
412
|
+
const decidedBy = actorId ?? '';
|
|
413
|
+
setRecords((current) => ids.reduce((recs, id) => applyOptimisticDecide(recs, id, decision, decidedBy).records, current));
|
|
414
|
+
const nextPending = {
|
|
415
|
+
...pendingRef.current,
|
|
416
|
+
};
|
|
417
|
+
for (const id of ids)
|
|
418
|
+
nextPending[id] = { id, decision, actorId: decidedBy };
|
|
419
|
+
pendingRef.current = nextPending;
|
|
182
420
|
void act(async () => {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
421
|
+
try {
|
|
422
|
+
const outcome = await client.decideBatch(ids, decision, comment === '' ? undefined : comment);
|
|
423
|
+
setLastBatch(outcome);
|
|
424
|
+
setRawSelectedIds([]);
|
|
425
|
+
// Reconcile each attempted id against the authoritative envelope; a
|
|
426
|
+
// failed item (no record) just drops its pending — the post-action
|
|
427
|
+
// refresh (in act) un-greys it from the server.
|
|
428
|
+
let map = pendingRef.current;
|
|
429
|
+
for (const item of outcome.results) {
|
|
430
|
+
if (item.record) {
|
|
431
|
+
map = reconcileDecided(map, {
|
|
432
|
+
type: 'decided',
|
|
433
|
+
record: item.record,
|
|
434
|
+
}).pending;
|
|
435
|
+
}
|
|
436
|
+
else {
|
|
437
|
+
const rest = { ...map };
|
|
438
|
+
delete rest[item.id];
|
|
439
|
+
map = rest;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
pendingRef.current = map;
|
|
443
|
+
}
|
|
444
|
+
catch (cause) {
|
|
445
|
+
// Wholesale batch failure (F3): clear every optimistic pending for
|
|
446
|
+
// these ids so none can later raise a spurious conflict, and resync the
|
|
447
|
+
// greyed rows from the server (covers pollIntervalMs <= 0).
|
|
448
|
+
const rest = { ...pendingRef.current };
|
|
449
|
+
for (const id of ids)
|
|
450
|
+
delete rest[id];
|
|
451
|
+
pendingRef.current = rest;
|
|
452
|
+
await refresh();
|
|
453
|
+
throw cause;
|
|
454
|
+
}
|
|
186
455
|
});
|
|
187
|
-
}, [act, client, selectedIds]);
|
|
456
|
+
}, [act, actorId, client, refresh, selectedIds]);
|
|
188
457
|
const decide = useCallback((decision, comment) => {
|
|
189
458
|
if (!selected)
|
|
190
459
|
return;
|
|
191
|
-
|
|
192
|
-
|
|
460
|
+
const id = selected.id;
|
|
461
|
+
// Optimistic: grey the row immediately, then confirm against the server
|
|
462
|
+
// response (poll-only path) or the authoritative 'decided' stream event.
|
|
463
|
+
const decidedBy = actorId ?? '';
|
|
464
|
+
setRecords((current) => applyOptimisticDecide(current, id, decision, decidedBy).records);
|
|
465
|
+
pendingRef.current = {
|
|
466
|
+
...pendingRef.current,
|
|
467
|
+
[id]: { id, decision, actorId: decidedBy },
|
|
468
|
+
};
|
|
469
|
+
void act(async () => {
|
|
470
|
+
try {
|
|
471
|
+
const result = await client.decide(id, decision, comment === '' ? undefined : comment);
|
|
472
|
+
pendingRef.current = reconcileDecided(pendingRef.current, {
|
|
473
|
+
type: 'decided',
|
|
474
|
+
record: result.record,
|
|
475
|
+
}).pending;
|
|
476
|
+
}
|
|
477
|
+
catch (cause) {
|
|
478
|
+
// Roll back the optimistic decision on failure (F3): clear the pending
|
|
479
|
+
// entry so a later authoritative 'decided' event for this record does
|
|
480
|
+
// NOT read as a spurious conflict, and resync the greyed row from the
|
|
481
|
+
// server (covers pollIntervalMs <= 0, where no interval poll un-greys it).
|
|
482
|
+
const rest = { ...pendingRef.current };
|
|
483
|
+
delete rest[id];
|
|
484
|
+
pendingRef.current = rest;
|
|
485
|
+
await refresh();
|
|
486
|
+
throw cause;
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
}, [act, actorId, client, refresh, selected]);
|
|
193
490
|
const claim = useCallback(() => {
|
|
194
491
|
if (!selected)
|
|
195
492
|
return;
|
|
@@ -224,6 +521,9 @@ export function useApprovalDashboard(client, { pollIntervalMs = 10_000, now = Da
|
|
|
224
521
|
claim,
|
|
225
522
|
delegate,
|
|
226
523
|
refresh,
|
|
524
|
+
presence,
|
|
525
|
+
conflict,
|
|
526
|
+
dismissConflict,
|
|
227
527
|
};
|
|
228
528
|
}
|
|
229
529
|
//# sourceMappingURL=use-approval-dashboard.js.map
|