Skip to content

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

Release System 0.1.24

zohayrslileh80d2d3dc23e8
diff --git a/bun.lock b/bun.lock
index 8ef03e3..c7fd31b 100644
--- a/bun.lock
+++ b/bun.lock
@@ -24,7 +24,7 @@
"devDependencies": {
"@babel/core": "^7.29.7",
"@babel/plugin-proposal-decorators": "^7.29.7",
- "@phreshos/core": "^0.1.10",
+ "@phreshos/core": "^0.1.11",
"@phreshos/react-ui": "^0.1.5",
"@rolldown/plugin-babel": "^0.2.3",
"@tailwindcss/vite": "^4.3.3",
@@ -230,7 +230,7 @@
"@oxc-project/types": ["@oxc-project/[email protected]", "", {}, "sha512-nuhZIOLuI6TFQ32I/WnUx+SCPY7SdSKwgnFHydAuoS1+Z4BRcaP+RRJmGzl9lw+0OFF7UmaESf7KQRXaNLHypg=="],
- "@phreshos/core": ["@phreshos/[email protected]", "", {}, "sha512-egqK1TSD7CAinj4/26mejL0/jzsDdOyQzPocEXN+KB0f1Jl+O+cxHU5S5S7Kwx8ySTQfuT9H/J8fw+yowQe5Jw=="],
+ "@phreshos/core": ["@phreshos/[email protected]", "", {}, "sha512-h1Q5ar8ghisWdvWqQ/9/iRp0H0QPCZj6X1VFzSvbkkPd4A7OMVuEM/itRNbpDvaxgmsfDD0D22T4Hm41AlmGzw=="],
"@phreshos/react-ui": ["@phreshos/[email protected]", "", { "dependencies": { "react-aria-components": "^1.20.0" }, "peerDependencies": { "@phreshos/core": "^0.1.9", "react": "^19.2.0", "react-dom": "^19.2.0" } }, "sha512-5yws41rmsxi2/0xpXG/0ZLy3zPyIGURvsGA2R2kLCiq4CiVFUcgbnWU7drENdMRMOj95Np9Vl7EVuJKV3Rfkpg=="],
diff --git a/package.json b/package.json
index 55fddc4..0f06f84 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
{
"name": "phreshos",
"displayName": "PhreshOS",
- "version": "0.1.23",
+ "version": "0.1.24",
"description": "The PhreshOS server, desktop, and authoritative system runtime.",
"type": "module",
"private": true,
@@ -30,7 +30,7 @@
"build": "vite-node scripts/build.ts",
"start": "node --run build && node dist/server/main.js",
"pack": "node --run build && vite-node scripts/pack.ts",
- "verify:contracts": "vite-node scripts/verify-client-launch.mjs && vite-node scripts/verify-client-surface.mjs && vite-node scripts/verify-window-geometry.mjs && vite-node scripts/verify-readiness.mjs && vite-node scripts/verify-endpoint-services.mjs && vite-node scripts/verify-program-install.mjs && vite-node scripts/verify-host-traffic.mjs && vite-node scripts/verify-intake-address.mjs && vite-node scripts/verify-theme.mjs",
+ "verify:contracts": "vite-node scripts/verify-client-launch.mjs && vite-node scripts/verify-process-find-or-create.mjs && vite-node scripts/verify-client-surface.mjs && vite-node scripts/verify-window-geometry.mjs && vite-node scripts/verify-readiness.mjs && vite-node scripts/verify-endpoint-services.mjs && vite-node scripts/verify-program-install.mjs && vite-node scripts/verify-host-traffic.mjs && vite-node scripts/verify-intake-address.mjs && vite-node scripts/verify-theme.mjs",
"verify:distribution": "node scripts/verify-distribution.mjs",
"verify": "node --run check && node --run verify:contracts && node --run verify:distribution"
},
@@ -54,7 +54,7 @@
"devDependencies": {
"@babel/core": "^7.29.7",
"@babel/plugin-proposal-decorators": "^7.29.7",
- "@phreshos/core": "^0.1.10",
+ "@phreshos/core": "^0.1.11",
"@phreshos/react-ui": "^0.1.5",
"@rolldown/plugin-babel": "^0.2.3",
"@tailwindcss/vite": "^4.3.3",
diff --git a/scripts/verify-process-find-or-create.mjs b/scripts/verify-process-find-or-create.mjs
new file mode 100644
index 0000000..f6449f4
--- /dev/null
+++ b/scripts/verify-process-find-or-create.mjs
@@ -0,0 +1,85 @@
+import assert from "node:assert/strict"
+import ProgramManager from "../source/server/core/link-manager/auth-manager/program-manager/program-manager.ts"
+
+const processes = new Map()
+const program = {
+ identity: "example",
+ server: { start: true },
+ client: {
+ start: true,
+ position: undefined,
+ size: undefined,
+ layer: undefined,
+ minimize: undefined
+ },
+ title: "Example",
+ clientLocation: "/",
+ async validate() {}
+}
+
+let starts = 0
+
+const manager = Object.assign(Object.create(ProgramManager.prototype), {
+ authManager: { processManager: { processes } },
+ creating: new Map(),
+ $outbound: { async publish() {} },
+ reach: identity => identity === program.identity ? program : null
+})
+
+manager.start = async (owner, launch, _watching, _parent, _transition, _wallpaper, prepared) => {
+ await Promise.resolve()
+
+ const existing = [...processes.values()].find(process => process.program === owner && process.name === launch.name)
+ if (existing) throw new Error("This program already has a process with that name")
+
+ const identity = `process-${++starts}`
+ processes.set(identity, {
+ identity,
+ name: launch.name ?? null,
+ program: owner,
+ launch: (prepared ?? manager.resolveLaunch(owner, launch)).intent
+ })
+ return identity
+}
+
+const launch = {
+ name: "shared",
+ server: true,
+ client: false,
+ options: { beta: "2", alpha: "1" }
+}
+
+const [first, second] = await Promise.all([
+ manager.findOrCreateProcess(program.identity, launch),
+ manager.findOrCreateProcess(program.identity, {
+ name: "shared",
+ server: true,
+ client: false,
+ options: { alpha: "1", beta: "2" }
+ })
+])
+
+assert.equal(first, second)
+assert.equal(starts, 1)
+
+await assert.rejects(
+ manager.findOrCreateProcess(program.identity, {
+ name: "shared",
+ server: false,
+ client: true,
+ options: { alpha: "1", beta: "2" }
+ }),
+ /different launch/
+)
+
+const omitted = manager.resolveLaunch(program, { name: "defaults" }).intent
+processes.set("unrelated", { program })
+const explicit = manager.resolveLaunch(program, {
+ name: "defaults",
+ server: true,
+ client: true
+}).intent
+
+assert.deepEqual(omitted, explicit)
+assert.equal(omitted.client.position, null)
+assert.equal(omitted.client.size, null)
diff --git a/source/client/core/link-manager/auth-manager/program-manager/program-manager.ts b/source/client/core/link-manager/auth-manager/program-manager/program-manager.ts
index 57e63c1..4c094c6 100644
--- a/source/client/core/link-manager/auth-manager/program-manager/program-manager.ts
+++ b/source/client/core/link-manager/auth-manager/program-manager/program-manager.ts
@@ -52,6 +52,12 @@ export default class ProgramManager extends TheLink {
return await this.$outbound.publishFirst("/create-process", identity, launch, parent) as string
}
+ /** Atomically resolves one named Process at the authoritative host. */
+ public async findOrCreateProcess(identity: string, launch: Launch & { name: string }, parent?: string) {
+
+ return await this.$outbound.publishFirst("/find-or-create-process", identity, launch, parent) as string
+ }
+
public async install(identity: string, asker: string) {
return await this.$outbound.publishFirst("/install-program", identity, asker)
diff --git a/source/client/view/desktop/client-host/host.ts b/source/client/view/desktop/client-host/host.ts
index 842e507..2563894 100644
--- a/source/client/view/desktop/client-host/host.ts
+++ b/source/client/view/desktop/client-host/host.ts
@@ -270,6 +270,15 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
return [record(scope.requireProcess(started))]
}
+ if (word === "program-process-find-or-create") {
+
+ const launch = args[0] as Launch & { name: string }
+
+ const found = await programManager.findOrCreateProcess(process().program, launch, process().identity)
+
+ return [record(scope.requireProcess(found))]
+ }
+
// Every instance ended, the asker last. One implementation on the
// core serves this and a server half both.
if (word === "program-process-exit-all") return [await processManager.exitAll(process().program, pane)]
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 52581b5..58230e9 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
@@ -9,7 +9,7 @@ import TheLink from "@libs/the-link/the-link"
import { Transmitted } from "@libs/messagepack"
import AuthManager from "../auth-manager"
import { signalProcessTree } from "@libs/process-tree"
-import Process, { type HostedProcess } from "./process"
+import Process, { type HostedProcess, type ProcessLaunch } from "./process"
import ServerProcessBoundary from "./server-process-boundary"
import ProcessTraffic, { type Half, type TrafficKind } from "./process-traffic"
import ClientProcessForwarder from "./client-process-forwarder"
@@ -664,7 +664,7 @@ export default class ProcessManager extends TheLink {
return new Window(shown, shape.position, shape.size, ++this.highest, shape.minimize)
}
- public async register(identity: string, name: string | null, program: Program, options: Options, child: ChildProcess | null, client: boolean, shape: Shape | null, parent: Process | null, configure?: (process: Process) => void) {
+ public async register(identity: string, name: string | null, program: Program, options: Options, launch: ProcessLaunch, child: ChildProcess | null, client: boolean, shape: Shape | null, parent: Process | null, configure?: (process: Process) => void) {
if (this.processes.has(identity)) {
@@ -688,7 +688,7 @@ export default class ProcessManager extends TheLink {
const window = shape ? this.window(program, shape) : null
- const process = new Process(identity, name, program, options, parent, this.hostTraffic)
+ const process = new Process(identity, name, program, options, launch, parent, this.hostTraffic)
this.processes.set(identity, process)
@@ -1452,6 +1452,13 @@ export default class ProcessManager extends TheLink {
return [processReference(this.find(await programManager.createProcess(program.identity, rest[1] as Launch, process)))]
}
+ if (word === "program-process-find-or-create") {
+
+ const program = this.heldProgram(rest[0])
+
+ return [processReference(this.find(await programManager.findOrCreateProcess(program.identity, rest[1] as Launch & { name: string }, process)))]
+ }
+
// Named, only that program's instances; unnamed, every one.
if (word === "host-process-list" || word === "program-process-list") {
diff --git a/source/server/core/link-manager/auth-manager/process-manager/process.ts b/source/server/core/link-manager/auth-manager/process-manager/process.ts
index dceaef6..b28c709 100644
--- a/source/server/core/link-manager/auth-manager/process-manager/process.ts
+++ b/source/server/core/link-manager/auth-manager/process-manager/process.ts
@@ -7,7 +7,7 @@ import ServerProcessBoundary from "./server-process-boundary"
import HostTraffic from "./host-traffic"
import ClientState from "./client-state"
import { randomUUID } from "node:crypto"
-import { type PermissionName } from "@phreshos/core"
+import { type Layer, type PermissionName } from "@phreshos/core"
import Tunnel from "@libs/the-link/tunnel"
/**
@@ -47,6 +47,9 @@ export default class Process {
// the process's own state rather than a message it was sent.
public readonly options: Options
+ /** Immutable normalized intent used only to converge named creation. */
+ public readonly launch: ProcessLaunch
+
/** Permission grants that live exactly as long as this Process. */
public readonly permissions = new Set<PermissionName>()
@@ -92,7 +95,7 @@ export default class Process {
private exitProcess: (() => Promise<unknown>) | null = null
- public constructor(identity: string, name: string | null, program: Program, options: Options, parent: Process | null, hostTraffic: HostTraffic) {
+ public constructor(identity: string, name: string | null, program: Program, options: Options, launch: ProcessLaunch, parent: Process | null, hostTraffic: HostTraffic) {
this.identity = identity
@@ -102,6 +105,8 @@ export default class Process {
this.options = options
+ this.launch = launch
+
this.parent = parent
this.hostTraffic = hostTraffic
@@ -322,3 +327,26 @@ export type Ending = (code: number | null, signal: NodeJS.Signals | null) => voi
export type { Position, Size }
export type { Stream } from "./server-process-boundary"
+
+/** Stable launch meaning, excluding mutable endpoint and Window state. */
+export interface ProcessLaunch {
+
+ readonly server: boolean
+
+ readonly client: Readonly<{
+
+ title: string
+
+ position: Position | null
+
+ size: Size | null
+
+ layer: Layer
+
+ location: string
+
+ minimize: boolean
+ }> | null
+
+ readonly options: Readonly<Record<string, string>>
+}
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 2fd11e2..fd0b9d8 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
@@ -10,10 +10,11 @@ import FileArea from "@libs/file-area"
import openStore from "@server/core/open-store"
import AuthManager from "../auth-manager"
import { dirname, isAbsolute, join } from "node:path"
+import { isDeepStrictEqual } from "node:util"
import Logs, { type LogSource } from "./logs"
import { isValue, layers, type Layer, type Position, type ProgramConfig, type Size } from "./config"
import { type PermissionName, type WallpaperLaunch } from "@phreshos/core"
-import { type default as Process, type Stream } from "../process-manager/process"
+import { type default as Process, type ProcessLaunch, type Stream } from "../process-manager/process"
import { type StandardShape, type WallpaperShape } from "../process-manager/process-manager"
import Program from "./program"
import Entry, { type HostedEntry } from "./entry"
@@ -44,6 +45,10 @@ export default class ProgramManager extends TheLink {
// the previous occupant's late echo.
private readonly changing = new Map<string, Promise<void>>()
+ // Named convergence and ordinary creation share one Program-local queue.
+ // This keeps find-or-create atomic even when another caller uses create.
+ private readonly creating = new Map<string, Promise<void>>()
+
// What each program remembers about itself, opened on first use and
// kept by the identity of the program that asked.
private readonly opened = new Map<string, Keyv>()
@@ -297,6 +302,30 @@ export default class ProgramManager extends TheLink {
}
}
+ private async serializeCreation<T>(identity: string, operation: () => Promise<T>) {
+
+ const previous = this.creating.get(identity) ?? Promise.resolve()
+
+ let finish: () => void = () => undefined
+
+ const pending = new Promise<void>(settle => { finish = settle })
+
+ const creating = previous.catch(() => undefined).then(() => pending)
+
+ this.creating.set(identity, creating)
+
+ await previous.catch(() => undefined)
+
+ try { return await operation() }
+
+ finally {
+
+ finish()
+
+ if (this.creating.get(identity) === creating) this.creating.delete(identity)
+ }
+ }
+
// A place is made when it is first wanted, which is also when a
// program has something to put in it. Metadata and content meet in
// this object even though their transports differ.
@@ -850,18 +879,51 @@ export default class ProgramManager extends TheLink {
@Connect("/create-process")
public async createProcess(identity: string, launch: Launch = {}, parent: Process | string | null = null) {
- // Every runtime program, installed or not, is reached through the
- // same registry. A created process therefore starts from the exact
- // Program its caller addressed, never from an installed-only view.
- const program = this.reach(identity)
+ return await this.serializeCreation(identity, async () => {
- if (!program) throw new Error("The system does not know this program")
+ // Every runtime program, installed or not, is reached through the
+ // same registry. A created process therefore starts from the exact
+ // Program its caller addressed, never from an installed-only view.
+ const program = this.reach(identity)
+
+ if (!program) throw new Error("The system does not know this program")
+
+ const creator = typeof parent === "string" ? this.authManager.processManager.processes.get(parent) : parent
+
+ if (typeof parent === "string" && !creator) throw new Error("The system does not know the parent process")
+
+ return await this.start(program, launch, undefined, creator ?? null)
+ })
+ }
+
+ @Connect("/find-or-create-process")
+ public async findOrCreateProcess(identity: string, launch: Launch & { name: string }, parent: Process | string | null = null) {
+
+ return await this.serializeCreation(identity, async () => {
+
+ const program = this.reach(identity)
+
+ if (!program) throw new Error("The system does not know this program")
+
+ const resolved = this.resolveLaunch(program, launch)
+
+ if (typeof launch.name !== "string" || !launch.name) throw new Error("findOrCreate requires a non-empty process name")
+
+ const existing = [...this.authManager.processManager.processes.values()].find(process => process.program === program && process.name === launch.name)
+
+ if (existing) {
+
+ if (!isDeepStrictEqual(existing.launch, resolved.intent)) throw new Error(`The process "${launch.name}" already exists with a different launch`)
- const creator = typeof parent === "string" ? this.authManager.processManager.processes.get(parent) : parent
+ return existing.identity
+ }
+
+ const creator = typeof parent === "string" ? this.authManager.processManager.processes.get(parent) : parent
- if (typeof parent === "string" && !creator) throw new Error("The system does not know the parent process")
+ if (typeof parent === "string" && !creator) throw new Error("The system does not know the parent process")
- return await this.start(program, launch, undefined, creator ?? null)
+ return await this.start(program, launch, undefined, creator ?? null, false, false, resolved)
+ })
}
// An attached launch is the local project's authoritative runtime use of
@@ -961,9 +1023,11 @@ export default class ProgramManager extends TheLink {
if (launch.name !== undefined && (typeof launch.name !== "string" || !launch.name)) throw new Error("A process name must be non-empty text")
- const options = launch.options ?? {}
+ const suppliedOptions = launch.options ?? {}
- if (typeof options !== "object" || options === null || Array.isArray(options) || Object.values(options).some(value => typeof value !== "string")) throw new Error("A launch's options must be text values")
+ if (typeof suppliedOptions !== "object" || suppliedOptions === null || Array.isArray(suppliedOptions) || Object.values(suppliedOptions).some(value => typeof value !== "string")) throw new Error("A launch's options must be text values")
+
+ const options = Object.fromEntries(Object.entries(suppliedOptions).sort(([left], [right]) => left.localeCompare(right)))
if (launch.server !== undefined && typeof launch.server !== "boolean") throw new Error("A launch's server must be true or false")
@@ -988,10 +1052,26 @@ export default class ProgramManager extends TheLink {
// is persisted; system defaults are derived again when it actually runs.
const shape = client ? this.clientShape(program, asked) : null
- return { options, server, client, shape }
+ const intent: ProcessLaunch = {
+
+ server: server !== null,
+
+ client: shape ? {
+
+ ...shape,
+
+ position: asked.position ?? program.client?.position ?? null,
+
+ size: asked.size ?? program.client?.size ?? null
+ } : null,
+
+ options
+ }
+
+ return { options, server, client, shape, intent }
}
- private async start(program: Program, launch: Launch = {}, watching?: Watching, parent: Process | null = null, transitionOwnsIdentity = false, wallpaper = false) {
+ private async start(program: Program, launch: Launch = {}, watching?: Watching, parent: Process | null = null, transitionOwnsIdentity = false, wallpaper = false, prepared?: ReturnType<ProgramManager["resolveLaunch"]>) {
if (!transitionOwnsIdentity && this.changing.has(program.identity)) throw new Error("This program is changing and cannot create a process")
@@ -1000,7 +1080,7 @@ export default class ProgramManager extends TheLink {
// allocated, so a failed launch never briefly exists.
await program.validate()
- const resolved = this.resolveLaunch(program, launch)
+ const resolved = prepared ?? this.resolveLaunch(program, launch)
const { options, server, client } = resolved
@@ -1041,7 +1121,7 @@ export default class ProgramManager extends TheLink {
// Lifecycle consumers are attached before either initial endpoint is
// activated, so even a server command that exits immediately has a
// complete output and exit record.
- await this.authManager.processManager.register(identity, launch.name ?? null, program, options, child, client !== null, shape, parent, record => {
+ await this.authManager.processManager.register(identity, launch.name ?? null, program, options, resolved.intent, child, client !== null, shape, parent, record => {
record.onServerStart(server => server.onOutput((stream, text) => logs.printed(identity, stream === "err" ? "stderr" : "stdout", text)))