import { AppRequirement, AppExposes, OpenAIAppUiExtension } from '@agentproto/app';
/**
* Persisted installed-app + app-run state for the `app_*` daemon verbs
* (app-tools.ts). Mirrors workflow-runner.ts's persistence pattern exactly:
* an in-memory store, write-tmp + rename on every mutation, and persistence
* OFF by default so unit tests never touch ~/.agentproto/.
*/
/** A ref pair as materialized by `@agentproto/app`'s `emit` — an
* agent/workflow id plus the absolute path to its manifest on disk. */
interface InstalledAppRef {
readonly id: string;
readonly path: string;
}
/** The catalog an app was installed from (`app_install {catalogUrl}`):
* `app_updates` / `app_resync` follow THAT catalog's entry for `appId`,
* never another source's. */
interface AppCatalogRef {
url: string;
appId: string;
}
/** Where an installed app came from — the pin `app_resync` compares against.
* Absent on a record (legacy / `{dir}` install) means `{ kind: "local" }`. */
type AppSource = {
kind: "local";
} | {
kind: "git";
url: string;
ref?: string;
sha: string;
subdir?: string;
catalogId?: AppCatalogRef;
} | {
kind: "agentapp";
url: string;
sha256: string;
version: string;
size?: number;
catalogId?: AppCatalogRef;
};
interface InstalledApp {
readonly appId: string;
readonly dir: string;
/** Provenance of a remote install (git URL / `.agentapp`), set by
* `app_install {url}` / `{file}` and kept current by `app_resync`.
* Absent = local dir install. */
readonly source?: AppSource;
/** Absolute root of the app's durable data (the `app_data_*` plane).
* Resolved once at install time by `performInstall` — explicit
* `app_install {dataDir}` > the record's previous `dataDir` > the APP.md
* `data.dir` hint (relative to `dir`) > `
/data`. Absent only on
* records written before the field existed; `appDataDir()` in
* app-data.ts falls back to `/data` for those. Keeping it distinct
* from `dir` is what lets multi-GB generated output live outside the
* app's source tree. */
readonly dataDir?: string;
/** Absolute dir of the app's daemon-owned state ledger
* (`/events.jsonl`, app-state.ts). Assigned by a PERSISTING
* registry to `/app-state/` (next to
* `apps.json`), so a runner-written ledger never lands in the app's
* source tree — a workflow-only app installed from a checkout has no
* business growing a `data/` folder there. Absent on a non-persisting
* (test) registry; `appStateEventsPath()` then falls back to
* `/state/events.jsonl`, the pre-`stateDir` location. */
readonly stateDir?: string;
readonly version?: string;
readonly name?: string;
readonly description?: string;
readonly agents: readonly InstalledAppRef[];
readonly workflows: readonly InstalledAppRef[];
/** Agent-declared (AIP-14) tool refs — the adapter's business, never
* validated at install time. Surfaced for visibility only. */
readonly unvalidatedAgentTools: readonly string[];
/** Flat app-id dependency list (both `requires` forms) — the field existing
* registry files on disk already carry. `app_apply`/`app_unapply`'s
* depended-on check reads it. */
readonly requires?: readonly string[];
/** Per-dependency declarations in normalized object form (bare ids become
* `{ id }`) — the consumer's `version` range and callable-`workflows`
* allowlist that `app_call` and `app_apply`'s version check enforce.
* Absent on records written before the field existed; those keep working
* via `requires` alone (dependency checks) but declare no call surface. */
readonly requiresApps?: readonly AppRequirement[];
/** The app's `exposes` block — the surface OTHER apps may consume:
* `workflows` ids are what a consumer's `requires.apps[].workflows` may
* name and what `app_call` checks against. Absent = exposes nothing. */
readonly exposes?: AppExposes;
readonly ui?: {
readonly path: string;
readonly title?: string;
readonly description?: string;
readonly tools?: readonly string[];
/** AIP-53 link-token scoping (app-link-token.ts): argument-level
* refinement of `tools`, keyed by tool id, enforced only for calls
* made under a valid scoped link/session token. See
* `@agentproto/app`'s `AppUiDefinition.toolScope` for the full
* contract — carried here verbatim from `installed.ui.toolScope` at
* install time. */
readonly toolScope?: Readonly>>>;
readonly csp?: {
readonly connectDomains?: readonly string[];
readonly resourceDomains?: readonly string[];
readonly frameDomains?: readonly string[];
};
/** How to (re)build `path` when it's missing or older than its
* declared sources — see `app-ui-build.ts`'s `ensureAppUiBuilt`, the
* only reader of this field. Absent means "committed bundle, never
* built by the daemon", today's behavior. */
readonly build?: {
readonly command: string;
readonly cwd?: string;
readonly sources?: readonly string[];
};
/** Namespaced OpenAI MCP-extensions declaration (plan §3.1), carried
* structurally from the app's normalized `AppUiDefinition.extensions`
* by `performInstall` (app-tools.ts is the ONLY writer; every other
* reader treats this as already-validated — validation happened in
* app at `defineApp`/load time, install is not a second gate).
* Frozen as a plain JSON-safe object so `apps.json` persistence is
* byte-stable. Absent ⇒ the app is portable-only. */
readonly extensions?: {
readonly openai?: OpenAIAppUiExtension;
};
};
/** A persistent HTML dashboard (Cowork artifact) the app ships. `path` is
* absolute; `app_artifact_get` reads it back at call time. */
readonly artifact?: {
readonly path: string;
readonly title?: string;
readonly description?: string;
};
/** A Cowork skill directory the app ships. `path` is absolute;
* `app_skill_get` reads files back at call time. */
readonly skill?: {
readonly path: string;
readonly title?: string;
readonly description?: string;
};
readonly artifacts?: readonly {
readonly type: string;
readonly description?: string;
}[];
/** Read-only external filesystem roots this app was granted at install
* time (AIP-42 `externalReadRoots`), normalized to absolute, `~`-expanded,
* existing-directory paths — see `performInstall` in app-tools.ts for the
* normalization + existence check. `app_external_list`/`app_external_read`
* (app-external.ts) only ever resolve a path under one of these, and only
* when the caller's `root` argument exact-string-matches an entry here. */
readonly externalReadRoots?: readonly string[];
/** Boundary policy declared by the app (AIP-42 `boundaries`) — see
* app-boundary.ts. `enforce: "required"` makes a spawn refuse instead of
* warn when the fs zones can't be enforced on the chosen harness. */
readonly boundaries?: {
readonly enforce?: "required" | "best-effort";
};
readonly dev?: {
readonly launch: readonly {
readonly name: string;
readonly runtimeExecutable: string;
readonly runtimeArgs?: readonly string[];
readonly port?: number;
readonly url?: string;
}[];
};
readonly installedAt: string;
readonly updatedAt: string;
}
interface AppRunSession {
readonly agentId: string;
readonly sessionId: string;
}
interface AppRun {
readonly appRunId: string;
readonly appId: string;
readonly sessions: AppRunSession[];
readonly startedAt: string;
/**
* AIP-58 §2 state machine, applied to an app run: `running` is the stored
* default; `succeeded` once every session's turn ended without error AND
* every workflow run this app run owns is itself terminal, `failed` on a
* session/workflow-run error (or an unresolvable "0 live sessions, no
* clean signal" zombie — see `errorCode: "orphaned"`), `cancelled` on
* `app_stop`. Mirrored from `app_run`'s adapter/harness pass-through (A)
* so it stays observable through `app_status`/`app_list` even after the
* run's sessions have all ended.
*/
status: "running" | "succeeded" | "failed" | "cancelled";
endedAt?: string;
/** Set when `status === "failed"` — mirrors `WorkflowRun.error`. */
error?: string;
/** AIP-58 §10 error code for `error`, when the failure has one — today
* only the orphan sweep sets this (`"orphaned"`). */
errorCode?: string;
/** Adapter slug the run's sessions were spawned under: the caller-given
* adapter, else the first agent's resolved one (see `app_run` in
* app-tools.ts). */
readonly adapter?: string;
/** Canonical harness slug (defaults to `adapter` when unset). */
readonly harness?: string;
/** Model id passed through to each spawned session, if any. */
readonly model?: string;
}
interface AppliedMount {
readonly scopeId: string;
readonly appId: string;
readonly appliedAt: string;
}
/** A persisted app record that did not load clean. `dropped` records were
* unusable (no `appId`/`dir`) and are not in the registry; `incomplete`
* records were kept with the missing fields defaulted, and are cleared once
* the app is reinstalled (`upsertApp`) — the daemon does that on its own
* from the app's dir (`repairIncompleteApps`, app-tools.ts). `dir-missing`
* is computed live on every `listIssues()` call: an installed app whose
* `dir` no longer exists on disk (e.g. installed from a worktree that was
* later removed), so its agents and workflows cannot load until it is
* reinstalled from a live checkout. A `dir-missing` record the daemon
* healed on its own is no longer an issue; the repair is reported apart,
* as an {@link AppRegistryRepair}. */
interface AppRegistryIssue {
readonly kind: "dropped" | "incomplete" | "dir-missing";
readonly appId: string | null;
readonly dir: string | null;
readonly problems: readonly string[];
}
/** A self-heal the daemon applied: `appId`'s `dir` was missing and was
* re-pointed from `from` to `to` (`repairWorktreeMissingApps`,
* app-tools.ts). Informational, kept apart from {@link AppRegistryIssue} so
* a healed registry does not read as degraded, yet the repair stays visible
* (`daemon_health.appRegistryRepairs`, `app_list`'s `registryRepair`). */
interface AppRegistryRepair {
readonly kind: "dir-repointed";
readonly appId: string;
readonly from: string;
readonly to: string;
readonly at: string;
}
interface AppRegistry {
/** Records that loaded partial or malformed, plus installed apps whose
* `dir` is gone (see {@link AppRegistryIssue}). */
listIssues(): AppRegistryIssue[];
/** Insert or, keyed by `appId`, fully replace an installed-app record. */
upsertApp(input: Omit): InstalledApp;
/** Record (in memory, for `listRepairs`) that `appId`'s `dir` was
* re-pointed from `from` to `to` by a self-heal. One per app: a later
* repair of the same app replaces the earlier one. */
recordRepoint(appId: string, from: string, to: string): void;
/** Self-heals applied since the registry loaded (see {@link AppRegistryRepair}). */
listRepairs(): AppRegistryRepair[];
getApp(appId: string): InstalledApp | undefined;
listApps(): InstalledApp[];
/** Remove an installed-app record. Returns the removed record, or
* undefined if no app with that id was installed. Callers (app_uninstall)
* are responsible for checking there's no applied mount / running run
* first — this does no such validation itself. */
removeApp(appId: string): InstalledApp | undefined;
createRun(input: {
appId: string;
sessions: readonly AppRunSession[];
adapter?: string;
harness?: string;
model?: string;
}): AppRun;
/** Append a session spawned after a run was created (used by non-blocking
* sequential app runs) and persist the updated run immediately. */
addRunSession(appRunId: string, session: AppRunSession): AppRun | undefined;
getRun(appRunId: string): AppRun | undefined;
listRuns(): AppRun[];
/** Mark a run terminal (`endedAt` now, status `opts.status` defaulting to
* "cancelled") — the explicit kill path (`app_stop`), the sequential
* completion path, and the liveness sweep all route through here. No-op
* (returns undefined) for an unknown appRunId. */
endRun(appRunId: string, opts?: {
status?: "succeeded" | "failed" | "cancelled";
error?: string;
errorCode?: string;
}): AppRun | undefined;
/** Apply an app to a scope (idempotent upsert per scopeId+appId pair). */
applyApp(input: {
scopeId: string;
appId: string;
}): AppliedMount;
/** Remove a scope mount. Returns the removed mount or undefined if not applied. */
unapplyApp(input: {
scopeId: string;
appId: string;
}): AppliedMount | undefined;
/** List applied mounts, optionally filtered by scopeId. */
listApplied(scopeId?: string): AppliedMount[];
}
export type { AppSource as A, InstalledApp as I, AppRegistry as a };