Skip to content

An open-source, self-hosted system for apps built with web technologies.

Resolve satisfied state operations silently

zohayrslileh6cd5f3b11c0d
diff --git a/source/server/core/appearance-manager.ts b/source/server/core/appearance-manager.ts
index 633b446..2dd07e4 100644
--- a/source/server/core/appearance-manager.ts
+++ b/source/server/core/appearance-manager.ts
@@ -1,4 +1,5 @@
import Keyv from "keyv"
+import { isDeepStrictEqual } from "node:util"
import { defaultAppearance, parseAppearance, type Appearance } from "@phreshos/core"
import UploadManager from "./upload-manager"
import { wallpaperKind, wallpaperSizeLimit } from "@shared/wallpaper"
@@ -33,6 +34,8 @@ export default class AppearanceManager {
this.validateWallpaper(appearance.desktopWallpaper.light)
this.validateWallpaper(appearance.desktopWallpaper.dark)
+ if (isDeepStrictEqual(this.current, appearance)) return this.current
+
await this.store.set(storageKey, appearance)
this.current = appearance
diff --git a/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts b/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts
index 40da407..1007701 100644
--- a/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts
+++ b/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts
@@ -1177,10 +1177,10 @@ export default class ProcessManager extends TheLink {
if (!process.program.server) throw new Error("This program declared no server half")
- if (process.server) throw new Error("The server endpoint is already running")
-
if (typeof launch !== "object" || launch === null || Array.isArray(launch) || launch.service !== undefined && typeof launch.service !== "boolean") throw new Error("A Server launch must contain an optional boolean service value")
+ if (process.server) return
+
await process.program.validate()
const runtime = this.authManager.programManager.serverRuntime(process.program)
@@ -1225,7 +1225,7 @@ export default class ProcessManager extends TheLink {
await this.transition(process, async () => {
- if (!process.server) throw new Error("The server endpoint is already stopped")
+ if (!process.server) return
if (!process.client) throw new Error("The final live endpoint cannot be stopped; exit the Process instead")
@@ -1245,24 +1245,24 @@ export default class ProcessManager extends TheLink {
const process = this.find(identity)
- const layer = launch.layer ?? process.program.client?.layer
+ const shape = this.authManager.programManager.clientShape(process.program, launch)
- return await this.serializeClientLayer(layer, () => this.startClientInLayer(identity, launch))
+ return await this.serializeClientLayer(shape.layer, () => this.startClientInLayer(identity, launch, shape))
}
- private async startClientInLayer(identity: string, launch: ClientLaunch) {
+ private async startClientInLayer(identity: string, launch: ClientLaunch, shape: StandardShape) {
const process = this.find(identity)
await this.transition(process, async () => {
- if (!process.program.client) throw new Error("This program declared no client half")
+ const declaration = process.program.client
- if (process.client) throw new Error("The client endpoint is already running")
+ if (!declaration) throw new Error("This program declared no client half")
- await process.program.validate()
+ if (process.client) return
- const shape = this.authManager.programManager.clientShape(process.program, launch)
+ await process.program.validate()
const window = this.window(shape)
@@ -1270,7 +1270,7 @@ export default class ProcessManager extends TheLink {
if (isDesktopReplacementLayer(window.layer)) await this.replaceDesktopPresentation(window.layer)
- this.activateClient(process, window, launch.service ?? process.program.client.service)
+ this.activateClient(process, window, launch.service ?? declaration.service)
try {
@@ -1301,7 +1301,7 @@ export default class ProcessManager extends TheLink {
await this.transition(process, async () => {
- if (!process.client) throw new Error("The client endpoint is already stopped")
+ if (!process.client) return
if (!process.server) throw new Error("The final live endpoint cannot be stopped; exit the Process instead")
@@ -3009,7 +3009,7 @@ export default class ProcessManager extends TheLink {
const window = this.mutableWindowOf(identity)
- window.move(position)
+ if (!window.move(position)) return { identity, window }
this.said(identity, "move", window.position)
@@ -3021,7 +3021,7 @@ export default class ProcessManager extends TheLink {
const window = this.mutableWindowOf(identity)
- window.resize(size)
+ if (!window.resize(size)) return { identity, window }
this.said(identity, "resize", window.size)
@@ -3033,13 +3033,15 @@ export default class ProcessManager extends TheLink {
const window = this.mutableWindowOf(identity)
- window.setGeometry(geometry)
+ const changed = window.setGeometry(geometry)
+
+ if (!changed.moved && !changed.resized) return { identity, window }
this.said(identity, "geometry", { position: window.position, size: window.size })
- this.said(identity, "move", window.position)
+ if (changed.moved) this.said(identity, "move", window.position)
- this.said(identity, "resize", window.size)
+ if (changed.resized) this.said(identity, "resize", window.size)
return { identity, window }
}
@@ -3049,7 +3051,7 @@ export default class ProcessManager extends TheLink {
const window = this.mutableWindowOf(identity)
- window.changeTitle(title)
+ if (!window.changeTitle(title)) return { identity, window }
this.said(identity, "changeTitle", window.title)
@@ -3077,6 +3079,8 @@ export default class ProcessManager extends TheLink {
const front = this.front(window.layer)
+ if (front === identity) return { identity, window }
+
window.depth = ++this.highest
this.settleFront(window.layer, front)
@@ -3092,6 +3096,7 @@ export default class ProcessManager extends TheLink {
if (typeof maximized !== "boolean") throw new Error("Window maximize takes a boolean state")
const window = this.mutableWindowOf(identity)
+ if (window.maximized === maximized) return { identity, window }
window.maximized = maximized
this.said(identity, "maximize", maximized)
return { identity, window }
@@ -3103,6 +3108,8 @@ export default class ProcessManager extends TheLink {
if (typeof minimized !== "boolean") throw new Error("Window minimize takes a boolean state")
const window = this.mutableWindowOf(identity)
+ if (window.minimized === minimized) return { identity, window }
+
const front = this.front(window.layer)
window.minimized = minimized
diff --git a/source/server/core/link-manager/auth-manager/process-manager/window.ts b/source/server/core/link-manager/auth-manager/process-manager/window.ts
index c3fd32d..e0eb38c 100644
--- a/source/server/core/link-manager/auth-manager/process-manager/window.ts
+++ b/source/server/core/link-manager/auth-manager/process-manager/window.ts
@@ -66,7 +66,11 @@ export default class Window implements Omit<WindowState, "front"> {
validate(position.y, "y")
+ if (this.position.x === position.x && this.position.y === position.y) return false
+
this.position = position
+
+ return true
}
public resize(size: Size) {
@@ -75,7 +79,11 @@ export default class Window implements Omit<WindowState, "front"> {
validate(size.height, "height")
+ if (this.size.width === size.width && this.size.height === size.height) return false
+
this.size = size
+
+ return true
}
/** Validates and commits a complete geometry without an intermediate state. */
@@ -89,9 +97,17 @@ export default class Window implements Omit<WindowState, "front"> {
validate(geometry.size.height, "height")
+ const moved = this.position.x !== geometry.position.x || this.position.y !== geometry.position.y
+
+ const resized = this.size.width !== geometry.size.width || this.size.height !== geometry.size.height
+
+ if (!moved && !resized) return { moved, resized }
+
this.position = geometry.position
this.size = geometry.size
+
+ return { moved, resized }
}
public changeTitle(title: string) {
@@ -100,7 +116,11 @@ export default class Window implements Omit<WindowState, "front"> {
if (!said) throw new Error("A window's title is something a person can read")
+ if (this.title === said) return false
+
this.title = said
+
+ return true
}
public toJSON() {
diff --git a/source/server/core/link-manager/auth-manager/program-manager/launch-storage.ts b/source/server/core/link-manager/auth-manager/program-manager/launch-storage.ts
index 16490a5..a293c65 100644
--- a/source/server/core/link-manager/auth-manager/program-manager/launch-storage.ts
+++ b/source/server/core/link-manager/auth-manager/program-manager/launch-storage.ts
@@ -1,6 +1,7 @@
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs"
import { randomUUID } from "node:crypto"
import { join } from "node:path"
+import { isDeepStrictEqual } from "node:util"
import { parseLaunch, type Launch } from "@phreshos/core"
import type Program from "./program"
@@ -14,7 +15,15 @@ export default class LaunchStorage {
}
public set(launch: Launch) {
- this.write(`${JSON.stringify(parseLaunch(launch), null, 2)}\n`)
+ const parsed = parseLaunch(launch)
+ let current: Launch | null = null
+
+ try { current = this.get() }
+ catch { /* A valid replacement repairs an invalid stored declaration. */ }
+
+ if (isDeepStrictEqual(current, parsed)) return
+
+ this.write(`${JSON.stringify(parsed, null, 2)}\n`)
}
/** Replace one declaration, retaining exact bytes for operation rollback. */
diff --git a/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts b/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts
index d039618..0da926a 100644
--- a/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts
+++ b/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts
@@ -1181,10 +1181,21 @@ export default class ProgramManager extends TheLink {
return await this.change(program.identity, async () => {
- if (!this.installed(program)) throw new Error("This program is not installed — there is nothing here to remove")
-
const entry = this.find(program.identity)
+ if (!entry.installed) {
+
+ if (!purge) return entry.identity
+
+ await this.authManager.processManager.exitAll(entry.identity, asker)
+
+ await this.release(entry.identity)
+
+ rmSync(this.fileManager.join(entry.identity), { recursive: true, force: true })
+
+ return await this.forgetEntry(entry)
+ }
+
return await this.uninstallEntry(entry, purge, asker, output)
})
}
diff --git a/source/server/core/link-manager/link-manager.ts b/source/server/core/link-manager/link-manager.ts
index b8ab4ec..15b3220 100644
--- a/source/server/core/link-manager/link-manager.ts
+++ b/source/server/core/link-manager/link-manager.ts
@@ -318,9 +318,10 @@ export default class LinkManager extends TheLink {
const update = this.updatingAppearance.then(async () => {
+ const previous = this.application.appearanceManager.value
const appearance = await this.application.appearanceManager.update(value)
- await this.appearance.update(appearance)
+ if (appearance !== previous) await this.appearance.update(appearance)
return appearance
})
diff --git a/tests/appearance.test.ts b/tests/appearance.test.ts
index 837a64f..a2709db 100644
--- a/tests/appearance.test.ts
+++ b/tests/appearance.test.ts
@@ -40,6 +40,10 @@ test("appearance contract", async () => {
assert(Object.isFrozen(manager.value))
assert.deepEqual(await store.get("appearance"), defaultAppearance)
+ const initial = manager.value
+ assert.equal(await manager.update(defaultAppearance), initial)
+ assert.equal(manager.value, initial)
+
for (const theme of ["light", "dark"] as const) {
for (const role of ["background", "foreground", "primary", "secondary", "success", "warning", "danger", "info"] as const) {
const color = "oklch(60% 0.2 260)"
diff --git a/tests/process-lifecycle.test.ts b/tests/process-lifecycle.test.ts
index 2b40247..e044b59 100644
--- a/tests/process-lifecycle.test.ts
+++ b/tests/process-lifecycle.test.ts
@@ -20,7 +20,20 @@ test("process lifecycle contract", async () => {
programManager: {
permission() { return null },
grantsPermission() { return true },
- grantsStorage() { return true }
+ grantsStorage() { return true },
+ clientShape(_program: Program, launch: { service?: boolean, title?: string } = {}) {
+
+ if (launch.service !== undefined && typeof launch.service !== "boolean") throw new Error("A launch client's service role must be true or false")
+
+ return {
+ title: launch.title ?? "Client",
+ position: { x: 0, y: 0 },
+ size: { width: 640, height: 480 },
+ layer: "window" as const,
+ minimize: false,
+ maximize: false
+ }
+ }
},
linkManager: {
application: {
@@ -55,6 +68,61 @@ test("process lifecycle contract", async () => {
return await manager.register(identity, null, program(identity), {}, launch, null, false, null, null)
}
+ // Endpoint start and stop establish incarnation state. Repeating the same
+ // request on a valid permanent handle is a silent success; removing the
+ // owning Process still invalidates that handle.
+ {
+ const manager = processManager()
+ const owner = new Program({
+ identity: "endpoint-state",
+ server: { location: ".", command: "true" },
+ client: { location: "." }
+ })
+ const clientStarts: unknown[][] = []
+ manager.$outbound.subscribe("/client-start", (...values) => { clientStarts.push(values) })
+ const process = await manager.register(
+ "endpoint-state",
+ null,
+ owner,
+ {},
+ launch,
+ null,
+ true,
+ {
+ title: "Client",
+ position: { x: 0, y: 0 },
+ size: { width: 640, height: 480 },
+ layer: "window",
+ minimize: false,
+ maximize: false
+ },
+ null
+ )
+ const client = process.client
+
+ await manager.startClient(process.identity, { title: "Ignored creation value" })
+
+ assert.equal(process.client, client)
+ assert.equal(clientStarts.length, 1)
+
+ await manager.stopServer(process.identity)
+ await manager.stopServer(process.identity)
+
+ await assert.rejects(manager.stopClient(process.identity), /final live endpoint/)
+
+ process.server = {} as ServerProcessBoundary
+ process.stopClient()
+
+ await manager.stopClient(process.identity)
+ await manager.startServer(process.identity)
+ await assert.rejects(manager.startServer(process.identity, { service: "invalid" } as never), /service value/)
+
+ manager.processes.delete(process.identity)
+
+ await assert.rejects(manager.startClient(process.identity), /does not know this process/)
+ await assert.rejects(manager.stopServer(process.identity), /does not know this process/)
+ }
+
// Parentage retains the exact Process entity after it exits. Absence and
// ended lineage therefore remain distinct states at every System boundary.
{
diff --git a/tests/program-installation-lifecycle.test.ts b/tests/program-installation-lifecycle.test.ts
index f612ece..02eee39 100644
--- a/tests/program-installation-lifecycle.test.ts
+++ b/tests/program-installation-lifecycle.test.ts
@@ -15,9 +15,10 @@ function fixture(context: TestContext) {
const server = join(directory, "source", "server")
mkdirSync(server, { recursive: true })
const exitAll = vi.fn()
+ const announceHost = vi.fn()
const auth = Object.assign(new TheLink(), {
linkManager: { application: { storage: new FileManager(directory, "system"), defaultProgramIcon: "" } },
- processManager: { processes: new Map(), exitAll, announceHost: vi.fn(), announceSubject: vi.fn() }
+ processManager: { processes: new Map(), exitAll, announceHost, announceSubject: vi.fn() }
}) as unknown as AuthManager
const manager = new ProgramManager(auth)
const definition = (installCommand = `node -e "process.stdout.write('preparing')"`) => new Program({
@@ -25,7 +26,7 @@ function fixture(context: TestContext) {
startup: { name: "startup" },
server: { location: server, installCommand, command: "node main.js" }
})
- return { directory, manager, exitAll, definition }
+ return { directory, manager, exitAll, announceHost, definition }
}
test("installation output precedes Process exit and handle switch; explicit launch precedes startup", async context => {
@@ -108,3 +109,30 @@ test("explicit launch errors propagate without rolling back the installed Progra
expect(manager.find("example").installed).toBe(true)
expect(start).toHaveBeenCalledTimes(1)
})
+
+test("uninstallation converges silently while its Program handle remains valid", async context => {
+ const { manager, exitAll, announceHost, definition } = fixture(context)
+ const entry = await manager.install(definition())
+ const program = entry.program
+ const home = manager.fileManager.join(program.identity)
+
+ await manager.uninstall(program)
+
+ expect(entry.installed).toBe(false)
+ expect(announceHost.mock.calls.filter(([, event]) => event === "uninstall")).toHaveLength(1)
+
+ await manager.uninstall(program)
+
+ expect(announceHost.mock.calls.filter(([, event]) => event === "uninstall")).toHaveLength(1)
+ expect(exitAll).not.toHaveBeenCalled()
+
+ await manager.uninstall(program, { purge: true })
+
+ expect(manager.programs.has(program.identity)).toBe(false)
+ expect(existsSync(home)).toBe(false)
+ expect(exitAll).toHaveBeenCalledExactlyOnceWith(program.identity, null)
+ expect(announceHost.mock.calls.filter(([, event]) => event === "uninstall")).toHaveLength(1)
+ expect(announceHost.mock.calls.filter(([, event]) => event === "forget")).toHaveLength(1)
+
+ await expect(manager.uninstall(program)).rejects.toThrow("does not know this program")
+})
diff --git a/tests/window-geometry.test.ts b/tests/window-geometry.test.ts
index b71194a..6457ccc 100644
--- a/tests/window-geometry.test.ts
+++ b/tests/window-geometry.test.ts
@@ -61,6 +61,20 @@ test("window geometry contract", async () => {
assert.equal(echo.identity, "process")
assert.equal(echo.window, authority)
+ const unchangedEvents = events.length
+ await ProcessManager.prototype.setGeometry.call(manager as unknown as ProcessManager, "process", next)
+ await ProcessManager.prototype.move.call(manager as unknown as ProcessManager, "process", next.position)
+ await ProcessManager.prototype.resize.call(manager as unknown as ProcessManager, "process", next.size)
+ await ProcessManager.prototype.changeTitle.call(manager as unknown as ProcessManager, "process", "Geometry")
+ assert.equal(events.length, unchangedEvents)
+
+ const resized = { width: "1/2", height: 260 }
+ await ProcessManager.prototype.setGeometry.call(manager as unknown as ProcessManager, "process", { position: next.position, size: resized })
+ assert.deepEqual(events.slice(-2), [
+ ["process", "geometry", { position: next.position, size: resized }],
+ ["process", "resize", resized]
+ ])
+
const publications: unknown[][] = []
const counterpart = new ClientWindow(
{ $outbound: { async publish(...publication: unknown[]) { publications.push(publication); return [] } } } as unknown as ClientProcessManager,
@@ -75,8 +89,11 @@ test("window geometry contract", async () => {
assert.equal(authority.maximized, true)
assert.equal(authority.minimized, true)
assert.deepEqual(authority.position, next.position)
- assert.deepEqual(authority.size, next.size)
+ assert.deepEqual(authority.size, resized)
assert.deepEqual(events.at(-1), ["process", "maximize", true])
+ const maximizedEvents = events.length
+ await ProcessManager.prototype.maximize.call(manager as unknown as ProcessManager, "process", true)
+ assert.equal(events.length, maximizedEvents)
const stored = { position: { x: 44, y: 55 }, size: { width: 440, height: 550 } }
authority.setGeometry(stored)
await ProcessManager.prototype.maximize.call(manager as unknown as ProcessManager, "process", false)