@inflatable-cookie/longhorn 0.1.0 → 0.2.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/README.md +1 -1
- package/package.json +1 -1
- package/src/agent-control/index.ts +13 -0
- package/src/agent-control/selection.ts +94 -0
- package/src/agent-control/shim.ts +50 -6
- package/src/update/client.ts +12 -3
- package/src/update/controller.ts +56 -5
- package/src/update/generated/fields.ts +6 -2
- package/src/update/generated/protocol.ts +75 -6
- package/src/update/generated/variant-fields.ts +2 -2
- package/src/update/ports.ts +12 -3
- package/src/update/serialized.ts +12 -2
- package/src/update/validation.ts +60 -4
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ Every domain is a subpath. `@inflatable-cookie/longhorn` itself resolves to
|
|
|
15
15
|
| Subpath | Domain |
|
|
16
16
|
| --- | --- |
|
|
17
17
|
| `/core` | identity, opaque values, transports, diagnostics |
|
|
18
|
-
| `/agent-control` | in-page semantic shim (
|
|
18
|
+
| `/agent-control` | in-page semantic shim (contract 022 control surface). Synthetic events are untrusted (`isTrusted` stays false); `wait_for` is DOM-relative. `bindFileSelection` is the plugin-dialog replacement at picker call sites. |
|
|
19
19
|
| `/config` | configuration store, backup, restore, recovery |
|
|
20
20
|
| `/layout` | containers, regions, panels, sizing |
|
|
21
21
|
| `/surfaces`, `/surface-transfer` | optional Surface documents and their transfer |
|
package/package.json
CHANGED
|
@@ -18,3 +18,16 @@ export {
|
|
|
18
18
|
type ToolErrorBody,
|
|
19
19
|
type WaitPredicate,
|
|
20
20
|
} from "./shim.ts";
|
|
21
|
+
export {
|
|
22
|
+
BEGIN_SELECTION_COMMAND,
|
|
23
|
+
SELECTION_RESOURCE_URI,
|
|
24
|
+
bindFileSelection,
|
|
25
|
+
type DialogFilter,
|
|
26
|
+
type FileSelectionApi,
|
|
27
|
+
type FileSelectionPorts,
|
|
28
|
+
type FileSelectionWorld,
|
|
29
|
+
type OpenDialogOptions,
|
|
30
|
+
type OpenDialogResult,
|
|
31
|
+
type SaveDialogOptions,
|
|
32
|
+
type SaveDialogResult,
|
|
33
|
+
} from "./selection.ts";
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
//! Consumer call-site replacement for plugin-dialog `open`/`save`.
|
|
2
|
+
//!
|
|
3
|
+
//! Agent-originated calls publish a pending selection before any OS panel
|
|
4
|
+
//! opens. Human-originated calls pass the original options to the plugin
|
|
5
|
+
//! unchanged. Origin is the in-page shim, not an active-server flag.
|
|
6
|
+
|
|
7
|
+
import { SHIM_GLOBAL, type AgentControlApi } from "./shim.ts";
|
|
8
|
+
|
|
9
|
+
/** Renderer invoke name. Host-protocol pairs this with the Rust command. */
|
|
10
|
+
export const BEGIN_SELECTION_COMMAND = "longhorn_agent_control_begin_selection";
|
|
11
|
+
|
|
12
|
+
/** Pending-selection MCP resource. */
|
|
13
|
+
export const SELECTION_RESOURCE_URI = "longhorn://agent-control/selection";
|
|
14
|
+
|
|
15
|
+
export type DialogFilter = {
|
|
16
|
+
name: string;
|
|
17
|
+
extensions: string[];
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type OpenDialogOptions = {
|
|
21
|
+
title?: string;
|
|
22
|
+
defaultPath?: string;
|
|
23
|
+
filters?: DialogFilter[];
|
|
24
|
+
multiple?: boolean;
|
|
25
|
+
directory?: boolean;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type SaveDialogOptions = {
|
|
29
|
+
title?: string;
|
|
30
|
+
defaultPath?: string;
|
|
31
|
+
filters?: DialogFilter[];
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type OpenDialogResult = string | string[] | null;
|
|
35
|
+
export type SaveDialogResult = string | null;
|
|
36
|
+
|
|
37
|
+
export type FileSelectionPorts = {
|
|
38
|
+
invoke: (command: string, args: Record<string, unknown>) => Promise<unknown>;
|
|
39
|
+
pluginOpen: (options?: OpenDialogOptions) => Promise<OpenDialogResult>;
|
|
40
|
+
pluginSave: (options?: SaveDialogOptions) => Promise<SaveDialogResult>;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type FileSelectionWorld = {
|
|
44
|
+
readonly [SHIM_GLOBAL]?: Pick<AgentControlApi, "isAgentOriginated">;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type FileSelectionApi = {
|
|
48
|
+
open: (options?: OpenDialogOptions) => Promise<OpenDialogResult>;
|
|
49
|
+
save: (options?: SaveDialogOptions) => Promise<SaveDialogResult>;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
function isAgentOriginated(world: FileSelectionWorld): boolean {
|
|
53
|
+
return world[SHIM_GLOBAL]?.isAgentOriginated?.() === true;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function beginOptions(
|
|
57
|
+
kind: "open" | "save",
|
|
58
|
+
options: OpenDialogOptions | SaveDialogOptions | undefined,
|
|
59
|
+
): Record<string, unknown> {
|
|
60
|
+
const open = kind === "open" ? (options as OpenDialogOptions | undefined) : undefined;
|
|
61
|
+
return {
|
|
62
|
+
kind,
|
|
63
|
+
directory: open?.directory ?? false,
|
|
64
|
+
multiple: open?.multiple ?? false,
|
|
65
|
+
filters: options?.filters ?? [],
|
|
66
|
+
title: options?.title,
|
|
67
|
+
defaultPath: options?.defaultPath,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Binds plugin-compatible `open`/`save`. Call at consumer picker sites;
|
|
73
|
+
* do not monkey-patch plugin-dialog.
|
|
74
|
+
*/
|
|
75
|
+
export function bindFileSelection(
|
|
76
|
+
ports: FileSelectionPorts,
|
|
77
|
+
world: object = globalThis,
|
|
78
|
+
): FileSelectionApi {
|
|
79
|
+
const originWorld = world as FileSelectionWorld;
|
|
80
|
+
return {
|
|
81
|
+
open: async (options?: OpenDialogOptions) => {
|
|
82
|
+
if (!isAgentOriginated(originWorld)) return ports.pluginOpen(options);
|
|
83
|
+
return (await ports.invoke(BEGIN_SELECTION_COMMAND, {
|
|
84
|
+
options: beginOptions("open", options),
|
|
85
|
+
})) as OpenDialogResult;
|
|
86
|
+
},
|
|
87
|
+
save: async (options?: SaveDialogOptions) => {
|
|
88
|
+
if (!isAgentOriginated(originWorld)) return ports.pluginSave(options);
|
|
89
|
+
return (await ports.invoke(BEGIN_SELECTION_COMMAND, {
|
|
90
|
+
options: beginOptions("save", options),
|
|
91
|
+
})) as SaveDialogResult;
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
@@ -98,6 +98,8 @@ export type AgentControlApi = {
|
|
|
98
98
|
) => ShimResult<Record<string, never>>;
|
|
99
99
|
drag: (source: string, target: string) => ShimResult<Record<string, never>>;
|
|
100
100
|
waitFor: (predicate: WaitPredicate) => ShimResult<{ holds: boolean }>;
|
|
101
|
+
markAgentOrigin: () => void;
|
|
102
|
+
isAgentOriginated: () => boolean;
|
|
101
103
|
readEvents: (sinceSeq?: number) => {
|
|
102
104
|
events: PageEvent[];
|
|
103
105
|
nextSeq: number;
|
|
@@ -114,7 +116,11 @@ export type ShimWorld = {
|
|
|
114
116
|
KeyboardEvent: typeof KeyboardEvent;
|
|
115
117
|
Event: typeof Event;
|
|
116
118
|
InputEvent?: typeof InputEvent;
|
|
117
|
-
addEventListener: (
|
|
119
|
+
addEventListener: (
|
|
120
|
+
type: string,
|
|
121
|
+
listener: (event: Event) => void,
|
|
122
|
+
options?: boolean | { capture?: boolean },
|
|
123
|
+
) => void;
|
|
118
124
|
onunhandledrejection?: ((event: Event) => void) | null;
|
|
119
125
|
} & Record<string, unknown>;
|
|
120
126
|
|
|
@@ -703,18 +709,56 @@ function installEventRing(world: ShimWorld): AgentControlApi["readEvents"] {
|
|
|
703
709
|
});
|
|
704
710
|
}
|
|
705
711
|
|
|
712
|
+
function installOriginTracking(world: ShimWorld): Pick<
|
|
713
|
+
AgentControlApi,
|
|
714
|
+
"markAgentOrigin" | "isAgentOriginated"
|
|
715
|
+
> {
|
|
716
|
+
let agentOriginSeq = 0;
|
|
717
|
+
let humanOriginSeq = 0;
|
|
718
|
+
const markHuman = (event: Event) => {
|
|
719
|
+
if (event.isTrusted) humanOriginSeq = agentOriginSeq + 1;
|
|
720
|
+
};
|
|
721
|
+
world.addEventListener("pointerdown", markHuman, true);
|
|
722
|
+
world.addEventListener("keydown", markHuman, true);
|
|
723
|
+
world.addEventListener("click", markHuman, true);
|
|
724
|
+
return {
|
|
725
|
+
markAgentOrigin: () => {
|
|
726
|
+
agentOriginSeq = humanOriginSeq + 1;
|
|
727
|
+
},
|
|
728
|
+
isAgentOriginated: () => agentOriginSeq > humanOriginSeq,
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
|
|
706
732
|
export function installAgentControlShim(world: ShimWorld): AgentControlApi {
|
|
707
733
|
const existing = world[SHIM_GLOBAL] as AgentControlApi | undefined;
|
|
708
734
|
if (existing) return existing;
|
|
709
735
|
const readEvents = installEventRing(world);
|
|
736
|
+
const origin = installOriginTracking(world);
|
|
710
737
|
const api: AgentControlApi = {
|
|
711
738
|
snapshot: () => snapshot(world),
|
|
712
|
-
click: (element) =>
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
739
|
+
click: (element) => {
|
|
740
|
+
origin.markAgentOrigin();
|
|
741
|
+
return click(world, element);
|
|
742
|
+
},
|
|
743
|
+
type: (element, text) => {
|
|
744
|
+
origin.markAgentOrigin();
|
|
745
|
+
return typeInto(world, element, text);
|
|
746
|
+
},
|
|
747
|
+
press: (key, modifiers, element) => {
|
|
748
|
+
origin.markAgentOrigin();
|
|
749
|
+
return press(world, key, modifiers, element);
|
|
750
|
+
},
|
|
751
|
+
scroll: (deltaX, deltaY, element) => {
|
|
752
|
+
origin.markAgentOrigin();
|
|
753
|
+
return scroll(world, deltaX, deltaY, element);
|
|
754
|
+
},
|
|
755
|
+
drag: (source, target) => {
|
|
756
|
+
origin.markAgentOrigin();
|
|
757
|
+
return drag(world, source, target);
|
|
758
|
+
},
|
|
717
759
|
waitFor: (predicate) => waitFor(world, predicate),
|
|
760
|
+
markAgentOrigin: origin.markAgentOrigin,
|
|
761
|
+
isAgentOriginated: origin.isAgentOriginated,
|
|
718
762
|
readEvents,
|
|
719
763
|
};
|
|
720
764
|
world[SHIM_GLOBAL] = api;
|
package/src/update/client.ts
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
UpdateApplyCommand,
|
|
3
|
+
UpdateCancelCommand,
|
|
2
4
|
UpdateChangedEvent,
|
|
3
5
|
UpdateCheckCommand,
|
|
4
6
|
UpdateDeferCommand,
|
|
5
|
-
UpdateInstallCommand,
|
|
6
7
|
UpdateOutcomeProjection,
|
|
8
|
+
UpdatePrepareCommand,
|
|
9
|
+
UpdateProgressEvent,
|
|
7
10
|
UpdateSelectChannelCommand,
|
|
8
11
|
UpdateSnapshot,
|
|
9
12
|
} from "./generated/protocol.ts";
|
|
10
13
|
import type { CheckedUpdatePort, UpdatePort, UpdateUnlisten } from "./ports.ts";
|
|
11
14
|
import {
|
|
15
|
+
assertUpdateApplyCommand,
|
|
16
|
+
assertUpdateCancelCommand,
|
|
12
17
|
assertUpdateChangedEvent,
|
|
13
18
|
assertUpdateCheckCommand,
|
|
14
19
|
assertUpdateDeferCommand,
|
|
15
|
-
assertUpdateInstallCommand,
|
|
16
20
|
assertUpdateOutcome,
|
|
21
|
+
assertUpdatePrepareCommand,
|
|
22
|
+
assertUpdateProgressEvent,
|
|
17
23
|
assertUpdateSelectChannelCommand,
|
|
18
24
|
assertUpdateSnapshot,
|
|
19
25
|
} from "./validation.ts";
|
|
@@ -24,6 +30,9 @@ export class UpdateClient implements CheckedUpdatePort {
|
|
|
24
30
|
async check(command: UpdateCheckCommand): Promise<UpdateOutcomeProjection> { assertUpdateCheckCommand(command); const value = await this.port.check(command); assertUpdateOutcome(value); return value; }
|
|
25
31
|
async selectChannel(command: UpdateSelectChannelCommand): Promise<UpdateOutcomeProjection> { assertUpdateSelectChannelCommand(command); const value = await this.port.selectChannel(command); assertUpdateOutcome(value); return value; }
|
|
26
32
|
async defer(command: UpdateDeferCommand): Promise<UpdateOutcomeProjection> { assertUpdateDeferCommand(command); const value = await this.port.defer(command); assertUpdateOutcome(value); return value; }
|
|
27
|
-
async
|
|
33
|
+
async prepare(command: UpdatePrepareCommand): Promise<UpdateOutcomeProjection> { assertUpdatePrepareCommand(command); const value = await this.port.prepare(command); assertUpdateOutcome(value); return value; }
|
|
34
|
+
async apply(command: UpdateApplyCommand): Promise<UpdateOutcomeProjection> { assertUpdateApplyCommand(command); const value = await this.port.apply(command); assertUpdateOutcome(value); return value; }
|
|
35
|
+
async cancel(command: UpdateCancelCommand): Promise<UpdateOutcomeProjection> { assertUpdateCancelCommand(command); const value = await this.port.cancel(command); assertUpdateOutcome(value); return value; }
|
|
28
36
|
async listen(listener: (event: UpdateChangedEvent) => void): Promise<UpdateUnlisten> { if (this.port.listen === undefined) return () => {}; return this.port.listen((value) => { assertUpdateChangedEvent(value); listener(value); }); }
|
|
37
|
+
async listenProgress(listener: (event: UpdateProgressEvent) => void): Promise<UpdateUnlisten> { if (this.port.listenProgress === undefined) return () => {}; return this.port.listenProgress((value) => { assertUpdateProgressEvent(value); listener(value); }); }
|
|
29
38
|
}
|
package/src/update/controller.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
type UpdateProgressProjection,
|
|
8
8
|
type UpdateRejectionCode,
|
|
9
9
|
type UpdateSnapshot,
|
|
10
|
+
type UpdateStagedArtifactProjection,
|
|
10
11
|
} from "./generated/protocol.ts";
|
|
11
12
|
import type { UpdatePort, UpdateUnlisten } from "./ports.ts";
|
|
12
13
|
|
|
@@ -60,6 +61,15 @@ export class UpdateController {
|
|
|
60
61
|
#lifecycle = 0;
|
|
61
62
|
#pending = false;
|
|
62
63
|
#unlisten: UpdateUnlisten[] = [];
|
|
64
|
+
/**
|
|
65
|
+
* The most recent live progress report.
|
|
66
|
+
*
|
|
67
|
+
* Held apart from the snapshot because it is *out of band*: a transfer
|
|
68
|
+
* publishes bytes while the controller's retained state still says what it
|
|
69
|
+
* said before the transfer began. It is cleared when the command that
|
|
70
|
+
* started the transfer settles, so a stale report cannot outlive it.
|
|
71
|
+
*/
|
|
72
|
+
#liveProgress?: UpdateProgressProjection;
|
|
63
73
|
|
|
64
74
|
constructor(options: UpdateControllerOptions) {
|
|
65
75
|
this.#client = new UpdateClient(options.port);
|
|
@@ -68,7 +78,16 @@ export class UpdateController {
|
|
|
68
78
|
get status(): UpdateControllerStatus { return this.#status; }
|
|
69
79
|
get snapshot(): UpdateSnapshot | undefined { return this.#snapshot; }
|
|
70
80
|
get availability(): UpdateAvailabilityProjection | undefined { return this.#snapshot?.availability; }
|
|
71
|
-
|
|
81
|
+
/**
|
|
82
|
+
* What the transfer is doing right now.
|
|
83
|
+
*
|
|
84
|
+
* A live report wins over the snapshot, because the snapshot is only as
|
|
85
|
+
* fresh as the last commit and the transfer deliberately does not hold the
|
|
86
|
+
* authority while it runs.
|
|
87
|
+
*/
|
|
88
|
+
get progress(): UpdateProgressProjection | undefined { return this.#liveProgress ?? this.#snapshot?.progress; }
|
|
89
|
+
/** The retained verified artifact, when one is staged. */
|
|
90
|
+
get staged(): UpdateStagedArtifactProjection | undefined { return this.#snapshot?.staged ?? undefined; }
|
|
72
91
|
get channel(): Channel | undefined { return this.#snapshot?.channel; }
|
|
73
92
|
get installedVersion(): string | undefined { return this.#snapshot?.installedVersion; }
|
|
74
93
|
get pending(): boolean { return this.#pending; }
|
|
@@ -123,7 +142,7 @@ export class UpdateController {
|
|
|
123
142
|
const snapshot = this.#snapshot;
|
|
124
143
|
if (snapshot === undefined) return "hidden";
|
|
125
144
|
|
|
126
|
-
switch (
|
|
145
|
+
switch (this.progress?.state) {
|
|
127
146
|
case "downloading":
|
|
128
147
|
case "verifying":
|
|
129
148
|
case "installing":
|
|
@@ -131,6 +150,7 @@ export class UpdateController {
|
|
|
131
150
|
case "readyToInstall":
|
|
132
151
|
return "attention";
|
|
133
152
|
case "idle":
|
|
153
|
+
case undefined:
|
|
134
154
|
break;
|
|
135
155
|
}
|
|
136
156
|
|
|
@@ -160,6 +180,16 @@ export class UpdateController {
|
|
|
160
180
|
return;
|
|
161
181
|
}
|
|
162
182
|
this.#unlisten.push(unlisten);
|
|
183
|
+
const unlistenProgress = await this.#client.listenProgress((event) => {
|
|
184
|
+
if (!this.#started || lifecycle !== this.#lifecycle) return;
|
|
185
|
+
this.#liveProgress = event.progress;
|
|
186
|
+
this.#notify();
|
|
187
|
+
});
|
|
188
|
+
if (!this.#started || lifecycle !== this.#lifecycle) {
|
|
189
|
+
await unlistenProgress();
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
this.#unlisten.push(unlistenProgress);
|
|
163
193
|
await this.refresh();
|
|
164
194
|
} catch (error) {
|
|
165
195
|
if (this.#started && lifecycle === this.#lifecycle) this.#setStatus({ kind: "failed", error });
|
|
@@ -208,10 +238,29 @@ export class UpdateController {
|
|
|
208
238
|
);
|
|
209
239
|
}
|
|
210
240
|
|
|
211
|
-
/** Fetches, verifies
|
|
212
|
-
async
|
|
241
|
+
/** Fetches, verifies and retains an update for a later apply. */
|
|
242
|
+
async prepare(version: string): Promise<void> {
|
|
243
|
+
await this.#command((client, epoch) =>
|
|
244
|
+
client.prepare({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch, version }),
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Applies the retained staged artifact, replacing the application.
|
|
250
|
+
*
|
|
251
|
+
* A gate refusal is a deferral: the artifact stays staged and the surface
|
|
252
|
+
* can offer the restart again.
|
|
253
|
+
*/
|
|
254
|
+
async apply(version: string): Promise<void> {
|
|
255
|
+
await this.#command((client, epoch) =>
|
|
256
|
+
client.apply({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch, version }),
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Discards the retained staged artifact. */
|
|
261
|
+
async cancel(): Promise<void> {
|
|
213
262
|
await this.#command((client, epoch) =>
|
|
214
|
-
client.
|
|
263
|
+
client.cancel({ protocolVersion: UPDATE_PROTOCOL_VERSION, authorityEpoch: epoch }),
|
|
215
264
|
);
|
|
216
265
|
}
|
|
217
266
|
|
|
@@ -227,11 +276,13 @@ export class UpdateController {
|
|
|
227
276
|
}
|
|
228
277
|
const lifecycle = this.#lifecycle;
|
|
229
278
|
this.#pending = true;
|
|
279
|
+
this.#liveProgress = undefined;
|
|
230
280
|
this.#notify();
|
|
231
281
|
try {
|
|
232
282
|
const outcome = await run(this.#client, epoch);
|
|
233
283
|
if (lifecycle !== this.#lifecycle) return;
|
|
234
284
|
this.#snapshot = outcome.snapshot;
|
|
285
|
+
this.#liveProgress = undefined;
|
|
235
286
|
this.#lastRejection = outcome.status === "rejected" ? outcome.code : undefined;
|
|
236
287
|
this.#setStatus({ kind: "ready" });
|
|
237
288
|
} catch (error) {
|
|
@@ -5,10 +5,14 @@
|
|
|
5
5
|
|
|
6
6
|
export const UPDATE_FIELDS: Record<string, readonly string[]> = {
|
|
7
7
|
"UpdateDeferralProjection": ["version", "cause"],
|
|
8
|
-
"
|
|
8
|
+
"UpdateStagedArtifactProjection": ["version", "channel", "digest"],
|
|
9
|
+
"UpdateProgressEvent": ["protocolVersion", "authorityEpoch", "progress"],
|
|
10
|
+
"UpdateSnapshot": ["protocolVersion", "authorityEpoch", "channel", "installedVersion", "availability", "deferral", "staged", "progress"],
|
|
9
11
|
"UpdateCheckCommand": ["protocolVersion", "authorityEpoch"],
|
|
10
12
|
"UpdateSelectChannelCommand": ["protocolVersion", "authorityEpoch", "channel"],
|
|
11
13
|
"UpdateDeferCommand": ["protocolVersion", "authorityEpoch", "version", "cause"],
|
|
12
|
-
"
|
|
14
|
+
"UpdatePrepareCommand": ["protocolVersion", "authorityEpoch", "version"],
|
|
15
|
+
"UpdateApplyCommand": ["protocolVersion", "authorityEpoch", "version"],
|
|
16
|
+
"UpdateCancelCommand": ["protocolVersion", "authorityEpoch"],
|
|
13
17
|
"UpdateChangedEvent": ["protocolVersion", "authorityEpoch", "kind"],
|
|
14
18
|
};
|
|
@@ -9,7 +9,7 @@ export const UPDATE_INSTALL_MANAGERS = ["macAppStore","homebrewCask","flatpak","
|
|
|
9
9
|
export const UPDATE_DEFERRAL_CAUSES = ["userPostponed","workInFlight","installationNotWritable","externallyManaged","installFailed"] as const;
|
|
10
10
|
export const UPDATE_AVAILABILITY_STATES = ["offer","upToDate","aheadOfChannel","withheldByRollout","managedElsewhere"] as const;
|
|
11
11
|
export const UPDATE_PROGRESS_STATES = ["idle","downloading","verifying","readyToInstall","installing"] as const;
|
|
12
|
-
export const UPDATE_INSTALL_AUTHORIZATION_STATUSES = ["
|
|
12
|
+
export const UPDATE_INSTALL_AUTHORIZATION_STATUSES = ["held","deferred"] as const;
|
|
13
13
|
export const UPDATE_REJECTION_CODES = ["staleAuthority","noOffer","unavailable","channelMismatch","unreachable","signatureRejected","notWritable","installFailed"] as const;
|
|
14
14
|
export const UPDATE_OUTCOME_STATUSES = ["committed","rejected"] as const;
|
|
15
15
|
export const UPDATE_CHANGED_KINDS = ["checked","channelSelected","deferred","progressed"] as const;
|
|
@@ -88,13 +88,37 @@ version: string,
|
|
|
88
88
|
*/
|
|
89
89
|
cause: DeferralCause, };
|
|
90
90
|
|
|
91
|
+
export type UpdateStagedArtifactProjection = {
|
|
92
|
+
/**
|
|
93
|
+
* The version the bytes were verified for.
|
|
94
|
+
*/
|
|
95
|
+
version: string,
|
|
96
|
+
/**
|
|
97
|
+
* The channel the bytes were fetched for.
|
|
98
|
+
*/
|
|
99
|
+
channel: Channel,
|
|
100
|
+
/**
|
|
101
|
+
* SHA-256 of the verified bytes, lowercase hex.
|
|
102
|
+
*/
|
|
103
|
+
digest: string, };
|
|
104
|
+
|
|
91
105
|
export type UpdateProgressProjection = { "state": "idle" } | { "state": "downloading",
|
|
106
|
+
/**
|
|
107
|
+
* Bytes received so far.
|
|
108
|
+
*/
|
|
109
|
+
received: number,
|
|
110
|
+
/**
|
|
111
|
+
* Bytes the source said to expect, when it said.
|
|
112
|
+
*/
|
|
113
|
+
expected: number | null,
|
|
92
114
|
/**
|
|
93
115
|
* How far through, when the source reports a length.
|
|
94
116
|
*
|
|
95
117
|
* Absent rather than zero when it does not. A source with no content
|
|
96
118
|
* length cannot produce a fraction, and a bar that invents one is
|
|
97
|
-
* worse than a bar that says it does not know.
|
|
119
|
+
* worse than a bar that says it does not know. Kept on the wire beside
|
|
120
|
+
* the byte counts because the clamp and the absent case are the
|
|
121
|
+
* protocol's answer, not each surface's.
|
|
98
122
|
*/
|
|
99
123
|
fraction: number | null, } | { "state": "verifying" } | { "state": "readyToInstall",
|
|
100
124
|
/**
|
|
@@ -106,6 +130,20 @@ version: string, } | { "state": "installing",
|
|
|
106
130
|
*/
|
|
107
131
|
version: string, };
|
|
108
132
|
|
|
133
|
+
export type UpdateProgressEvent = {
|
|
134
|
+
/**
|
|
135
|
+
* Exact metadata protocol line.
|
|
136
|
+
*/
|
|
137
|
+
protocolVersion: UpdateProtocolVersion,
|
|
138
|
+
/**
|
|
139
|
+
* Live authority lifetime the transfer was started under.
|
|
140
|
+
*/
|
|
141
|
+
authorityEpoch: number,
|
|
142
|
+
/**
|
|
143
|
+
* What the transfer reports right now.
|
|
144
|
+
*/
|
|
145
|
+
progress: UpdateProgressProjection, };
|
|
146
|
+
|
|
109
147
|
export type UpdateSnapshot = {
|
|
110
148
|
/**
|
|
111
149
|
* Exact metadata protocol line.
|
|
@@ -134,6 +172,13 @@ availability: UpdateAvailabilityProjection,
|
|
|
134
172
|
* The standing deferral, when one applies.
|
|
135
173
|
*/
|
|
136
174
|
deferral: UpdateDeferralProjection | null,
|
|
175
|
+
/**
|
|
176
|
+
* The retained verified artifact, when one is staged.
|
|
177
|
+
*
|
|
178
|
+
* Survives across calls: a deferred install keeps it, an explicit cancel
|
|
179
|
+
* discards it, and a verification failure never creates it.
|
|
180
|
+
*/
|
|
181
|
+
staged: UpdateStagedArtifactProjection | null,
|
|
137
182
|
/**
|
|
138
183
|
* What is in flight.
|
|
139
184
|
*/
|
|
@@ -182,7 +227,21 @@ version: string,
|
|
|
182
227
|
*/
|
|
183
228
|
cause: DeferralCause, };
|
|
184
229
|
|
|
185
|
-
export type
|
|
230
|
+
export type UpdatePrepareCommand = {
|
|
231
|
+
/**
|
|
232
|
+
* Exact metadata protocol line.
|
|
233
|
+
*/
|
|
234
|
+
protocolVersion: UpdateProtocolVersion,
|
|
235
|
+
/**
|
|
236
|
+
* Authority lifetime observed by the caller.
|
|
237
|
+
*/
|
|
238
|
+
authorityEpoch: number,
|
|
239
|
+
/**
|
|
240
|
+
* The version to prepare.
|
|
241
|
+
*/
|
|
242
|
+
version: string, };
|
|
243
|
+
|
|
244
|
+
export type UpdateApplyCommand = {
|
|
186
245
|
/**
|
|
187
246
|
* Exact metadata protocol line.
|
|
188
247
|
*/
|
|
@@ -192,13 +251,23 @@ protocolVersion: UpdateProtocolVersion,
|
|
|
192
251
|
*/
|
|
193
252
|
authorityEpoch: number,
|
|
194
253
|
/**
|
|
195
|
-
* The version
|
|
254
|
+
* The version the caller believes is staged.
|
|
196
255
|
*/
|
|
197
256
|
version: string, };
|
|
198
257
|
|
|
199
|
-
export type
|
|
258
|
+
export type UpdateCancelCommand = {
|
|
259
|
+
/**
|
|
260
|
+
* Exact metadata protocol line.
|
|
261
|
+
*/
|
|
262
|
+
protocolVersion: UpdateProtocolVersion,
|
|
263
|
+
/**
|
|
264
|
+
* Authority lifetime observed by the caller.
|
|
265
|
+
*/
|
|
266
|
+
authorityEpoch: number, };
|
|
267
|
+
|
|
268
|
+
export type UpdateInstallAuthorizationProjection = { "status": "held" } | { "status": "deferred",
|
|
200
269
|
/**
|
|
201
|
-
* What was in flight.
|
|
270
|
+
* What was in flight, or why the lease was refused.
|
|
202
271
|
*/
|
|
203
272
|
cause: DeferralCause, };
|
|
204
273
|
|
|
@@ -20,13 +20,13 @@ export const UPDATE_VARIANT_FIELDS: Record<string, Record<string, readonly strin
|
|
|
20
20
|
},
|
|
21
21
|
"UpdateProgressProjection": {
|
|
22
22
|
"idle": ["state"],
|
|
23
|
-
"downloading": ["state", "fraction"],
|
|
23
|
+
"downloading": ["state", "received", "expected", "fraction"],
|
|
24
24
|
"verifying": ["state"],
|
|
25
25
|
"readyToInstall": ["state", "version"],
|
|
26
26
|
"installing": ["state", "version"],
|
|
27
27
|
},
|
|
28
28
|
"UpdateInstallAuthorizationProjection": {
|
|
29
|
-
"
|
|
29
|
+
"held": ["status"],
|
|
30
30
|
"deferred": ["status", "cause"],
|
|
31
31
|
},
|
|
32
32
|
"UpdateOutcomeProjection": {
|
package/src/update/ports.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
UpdateApplyCommand,
|
|
3
|
+
UpdateCancelCommand,
|
|
2
4
|
UpdateChangedEvent,
|
|
3
5
|
UpdateCheckCommand,
|
|
4
6
|
UpdateDeferCommand,
|
|
5
|
-
UpdateInstallCommand,
|
|
6
7
|
UpdateOutcomeProjection,
|
|
8
|
+
UpdatePrepareCommand,
|
|
9
|
+
UpdateProgressEvent,
|
|
7
10
|
UpdateSelectChannelCommand,
|
|
8
11
|
UpdateSnapshot,
|
|
9
12
|
} from "./generated/protocol.ts";
|
|
@@ -21,8 +24,11 @@ export interface UpdatePort {
|
|
|
21
24
|
check(command: UpdateCheckCommand): Promise<unknown>;
|
|
22
25
|
selectChannel(command: UpdateSelectChannelCommand): Promise<unknown>;
|
|
23
26
|
defer(command: UpdateDeferCommand): Promise<unknown>;
|
|
24
|
-
|
|
27
|
+
prepare(command: UpdatePrepareCommand): Promise<unknown>;
|
|
28
|
+
apply(command: UpdateApplyCommand): Promise<unknown>;
|
|
29
|
+
cancel(command: UpdateCancelCommand): Promise<unknown>;
|
|
25
30
|
listen?(listener: (event: unknown) => void): UpdateUnlisten | Promise<UpdateUnlisten>;
|
|
31
|
+
listenProgress?(listener: (event: unknown) => void): UpdateUnlisten | Promise<UpdateUnlisten>;
|
|
26
32
|
}
|
|
27
33
|
|
|
28
34
|
/** The same seam after validation. */
|
|
@@ -31,6 +37,9 @@ export interface CheckedUpdatePort {
|
|
|
31
37
|
check(command: UpdateCheckCommand): Promise<UpdateOutcomeProjection>;
|
|
32
38
|
selectChannel(command: UpdateSelectChannelCommand): Promise<UpdateOutcomeProjection>;
|
|
33
39
|
defer(command: UpdateDeferCommand): Promise<UpdateOutcomeProjection>;
|
|
34
|
-
|
|
40
|
+
prepare(command: UpdatePrepareCommand): Promise<UpdateOutcomeProjection>;
|
|
41
|
+
apply(command: UpdateApplyCommand): Promise<UpdateOutcomeProjection>;
|
|
42
|
+
cancel(command: UpdateCancelCommand): Promise<UpdateOutcomeProjection>;
|
|
35
43
|
listen(listener: (event: UpdateChangedEvent) => void): Promise<UpdateUnlisten>;
|
|
44
|
+
listenProgress(listener: (event: UpdateProgressEvent) => void): Promise<UpdateUnlisten>;
|
|
36
45
|
}
|
package/src/update/serialized.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
UpdateApplyCommand,
|
|
3
|
+
UpdateCancelCommand,
|
|
4
|
+
UpdateCheckCommand,
|
|
5
|
+
UpdateDeferCommand,
|
|
6
|
+
UpdatePrepareCommand,
|
|
7
|
+
UpdateSelectChannelCommand,
|
|
8
|
+
} from "./generated/protocol.ts";
|
|
2
9
|
import type { UpdatePort, UpdateUnlisten } from "./ports.ts";
|
|
3
10
|
export class SerializedUpdatePort implements UpdatePort {
|
|
4
11
|
constructor(readonly inner: UpdatePort) {}
|
|
@@ -6,7 +13,10 @@ export class SerializedUpdatePort implements UpdatePort {
|
|
|
6
13
|
async check(command: UpdateCheckCommand): Promise<unknown> { return clone(await this.inner.check(clone(command))); }
|
|
7
14
|
async selectChannel(command: UpdateSelectChannelCommand): Promise<unknown> { return clone(await this.inner.selectChannel(clone(command))); }
|
|
8
15
|
async defer(command: UpdateDeferCommand): Promise<unknown> { return clone(await this.inner.defer(clone(command))); }
|
|
9
|
-
async
|
|
16
|
+
async prepare(command: UpdatePrepareCommand): Promise<unknown> { return clone(await this.inner.prepare(clone(command))); }
|
|
17
|
+
async apply(command: UpdateApplyCommand): Promise<unknown> { return clone(await this.inner.apply(clone(command))); }
|
|
18
|
+
async cancel(command: UpdateCancelCommand): Promise<unknown> { return clone(await this.inner.cancel(clone(command))); }
|
|
10
19
|
async listen(listener: (event: unknown) => void): Promise<UpdateUnlisten> { if (this.inner.listen === undefined) return () => {}; return this.inner.listen((event) => listener(clone(event))); }
|
|
20
|
+
async listenProgress(listener: (event: unknown) => void): Promise<UpdateUnlisten> { if (this.inner.listenProgress === undefined) return () => {}; return this.inner.listenProgress((event) => listener(clone(event))); }
|
|
11
21
|
}
|
|
12
22
|
function clone<T>(value: T): T { return JSON.parse(JSON.stringify(value)) as T; }
|
package/src/update/validation.ts
CHANGED
|
@@ -4,17 +4,22 @@ import {
|
|
|
4
4
|
UPDATE_CHANGED_KINDS,
|
|
5
5
|
UPDATE_CHANNELS,
|
|
6
6
|
UPDATE_DEFERRAL_CAUSES,
|
|
7
|
+
UPDATE_INSTALL_AUTHORIZATION_STATUSES,
|
|
7
8
|
UPDATE_INSTALL_MANAGERS,
|
|
8
9
|
UPDATE_OFFER_REASONS,
|
|
9
10
|
UPDATE_OUTCOME_STATUSES,
|
|
10
11
|
UPDATE_PROGRESS_STATES,
|
|
11
12
|
UPDATE_PROTOCOL_VERSION,
|
|
12
13
|
UPDATE_REJECTION_CODES,
|
|
14
|
+
type UpdateApplyCommand,
|
|
15
|
+
type UpdateCancelCommand,
|
|
13
16
|
type UpdateChangedEvent,
|
|
14
17
|
type UpdateCheckCommand,
|
|
15
18
|
type UpdateDeferCommand,
|
|
16
|
-
type
|
|
19
|
+
type UpdateInstallAuthorizationProjection,
|
|
17
20
|
type UpdateOutcomeProjection,
|
|
21
|
+
type UpdatePrepareCommand,
|
|
22
|
+
type UpdateProgressEvent,
|
|
18
23
|
type UpdateSelectChannelCommand,
|
|
19
24
|
type UpdateSnapshot,
|
|
20
25
|
} from "./generated/protocol.ts";
|
|
@@ -43,6 +48,13 @@ export function assertUpdateSnapshot(value: unknown): asserts value is UpdateSna
|
|
|
43
48
|
version(root.installedVersion, "$.installedVersion");
|
|
44
49
|
availability(root.availability, "$.availability");
|
|
45
50
|
progress(root.progress, "$.progress");
|
|
51
|
+
if (root.staged !== null) {
|
|
52
|
+
const staged = object(root.staged, "$.staged");
|
|
53
|
+
exact(staged, "$.staged", UPDATE_FIELDS.UpdateStagedArtifactProjection);
|
|
54
|
+
version(staged.version, "$.staged.version");
|
|
55
|
+
oneOf(staged.channel, "$.staged.channel", UPDATE_CHANNELS);
|
|
56
|
+
digest(staged.digest, "$.staged.digest");
|
|
57
|
+
}
|
|
46
58
|
if (root.deferral !== null) {
|
|
47
59
|
const deferral = object(root.deferral, "$.deferral");
|
|
48
60
|
exact(deferral, "$.deferral", UPDATE_FIELDS.UpdateDeferralProjection);
|
|
@@ -87,11 +99,48 @@ export function assertUpdateDeferCommand(value: unknown): asserts value is Updat
|
|
|
87
99
|
cause(root.cause, "$.cause");
|
|
88
100
|
}
|
|
89
101
|
|
|
90
|
-
export function
|
|
91
|
-
commandBase(value, UPDATE_FIELDS.
|
|
102
|
+
export function assertUpdatePrepareCommand(value: unknown): asserts value is UpdatePrepareCommand {
|
|
103
|
+
commandBase(value, UPDATE_FIELDS.UpdatePrepareCommand);
|
|
104
|
+
version(object(value, "$").version, "$.version");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function assertUpdateApplyCommand(value: unknown): asserts value is UpdateApplyCommand {
|
|
108
|
+
commandBase(value, UPDATE_FIELDS.UpdateApplyCommand);
|
|
92
109
|
version(object(value, "$").version, "$.version");
|
|
93
110
|
}
|
|
94
111
|
|
|
112
|
+
export function assertUpdateCancelCommand(value: unknown): asserts value is UpdateCancelCommand {
|
|
113
|
+
commandBase(value, UPDATE_FIELDS.UpdateCancelCommand);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The live progress channel, which carries a report rather than an
|
|
118
|
+
* invalidation. It is the only thing that shows bytes while a transfer runs.
|
|
119
|
+
*/
|
|
120
|
+
export function assertUpdateProgressEvent(value: unknown): asserts value is UpdateProgressEvent {
|
|
121
|
+
noPayload(value);
|
|
122
|
+
const root = object(value, "$");
|
|
123
|
+
exact(root, "$", UPDATE_FIELDS.UpdateProgressEvent);
|
|
124
|
+
protocol(root.protocolVersion, "$.protocolVersion");
|
|
125
|
+
positive(root.authorityEpoch, "$.authorityEpoch");
|
|
126
|
+
progress(root.progress, "$.progress");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The gate's answer: an exclusive admission lease is held, or the install was
|
|
131
|
+
* deferred with its reason. There is no fourth state, and no "approved" one —
|
|
132
|
+
* authorization is a held value, not a point-in-time flag.
|
|
133
|
+
*/
|
|
134
|
+
export function assertUpdateInstallAuthorization(
|
|
135
|
+
value: unknown,
|
|
136
|
+
): asserts value is UpdateInstallAuthorizationProjection {
|
|
137
|
+
noPayload(value);
|
|
138
|
+
const root = object(value, "$");
|
|
139
|
+
oneOf(root.status, "$.status", UPDATE_INSTALL_AUTHORIZATION_STATUSES);
|
|
140
|
+
exact(root, "$", variantFields("UpdateInstallAuthorizationProjection", root, "$"));
|
|
141
|
+
if (root.status === "deferred") cause(root.cause, "$.cause");
|
|
142
|
+
}
|
|
143
|
+
|
|
95
144
|
function availability(value: unknown, path: string): void {
|
|
96
145
|
const root = object(value, path);
|
|
97
146
|
oneOf(root.state, `${path}.state`, UPDATE_AVAILABILITY_STATES);
|
|
@@ -123,7 +172,11 @@ function progress(value: unknown, path: string): void {
|
|
|
123
172
|
// Absent rather than zero when the source declares no length. A validator
|
|
124
173
|
// that coerced `null` to 0 here would put back the invented number the
|
|
125
174
|
// protocol went out of its way to avoid.
|
|
126
|
-
if (root.state === "downloading")
|
|
175
|
+
if (root.state === "downloading") {
|
|
176
|
+
count(root.received, `${path}.received`);
|
|
177
|
+
optionalCount(root.expected, `${path}.expected`);
|
|
178
|
+
fraction(root.fraction, `${path}.fraction`);
|
|
179
|
+
}
|
|
127
180
|
if (root.state === "readyToInstall" || root.state === "installing") {
|
|
128
181
|
version(root.version, `${path}.version`);
|
|
129
182
|
}
|
|
@@ -182,6 +235,9 @@ function object(value: unknown, path: string): Record<string, unknown> { if (val
|
|
|
182
235
|
function exact(value: Record<string, unknown>, path: string, expected: readonly string[]): void { const actual = Object.keys(value).sort(); const wanted = [...expected].sort(); if (actual.length !== wanted.length || actual.some((key, index) => key !== wanted[index])) fail(path, `unexpected keys: ${actual.join(",")}`); }
|
|
183
236
|
function protocol(value: unknown, path: string): void { if (value !== UPDATE_PROTOCOL_VERSION) fail(path, `expected exact protocol ${UPDATE_PROTOCOL_VERSION}`); }
|
|
184
237
|
function positive(value: unknown, path: string): void { if (typeof value !== "number" || !Number.isSafeInteger(value) || value <= 0) fail(path, "expected positive safe integer"); }
|
|
238
|
+
function count(value: unknown, path: string): void { if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) fail(path, "expected a non-negative safe byte count"); }
|
|
239
|
+
function optionalCount(value: unknown, path: string): void { if (value !== null) count(value, path); }
|
|
240
|
+
function digest(value: unknown, path: string): void { if (typeof value !== "string" || !/^[0-9a-f]{64}$/.test(value)) fail(path, "expected 64 lowercase hex digits"); }
|
|
185
241
|
function string(value: unknown, path: string): void { if (typeof value !== "string") fail(path, "expected string"); }
|
|
186
242
|
function optionalString(value: unknown, path: string): void { if (value !== null) string(value, path); }
|
|
187
243
|
/** Versions are strings on the wire, as `semver::Version` serialises them. */
|