2026-09-21 12:52:21 -07:00
/** Protocol/process qualification only; no application fixture enters runtime code. */
import assert from "node:assert/strict" ;
import { mkdir , writeFile } from "node:fs/promises" ;
import { compileCapabilityResourceSource , compileCapabilitySource } from "../src/capability-language/index.js" ;
import { compileExecutionPlan } from "../src/execution-world/plan.js" ;
import { compileKernelProgram } from "../src/execution-world/kernel.js" ;
2026-09-21 14:03:14 -07:00
import { compileQuery } from "../src/query/compile.js" ;
import {
generateInterfaceClientBindings ,
generatePackageQueryClientBindings ,
} from "../src/execution-world/client-contracts.js" ;
2026-09-21 12:52:21 -07:00
import { compileQueryProgram } from "../src/execution-world/queries.js" ;
2026-09-30 22:03:17 -07:00
import { presentationDeclarations , preparationArtifact } from "../src/react-build/declarations.js" ;
import { compileQueryComposition } from "../src/query/composition.js" ;
import type { PackageRevision } from "../src/capability-model/types.js" ;
2026-09-21 12:52:21 -07:00
const source = { repository : "https://example.test/device-owner.git" , commit : "a" . repeat ( 40 ) };
2026-09-21 13:30:33 -07:00
const factory = compileCapabilityResourceSource (
`external atom Entry id "atom:entry";
interface EntryFactory id "interface:entry-factory" revision "interface:entry-factory@1" {
static operation create id "entry:create" : unit -> atom-ref<Entry> {
2026-09-28 20:29:21 -07:00
call id "entry:create:call" execution transaction;
2026-09-21 13:30:33 -07:00
}
2026-09-21 12:52:21 -07:00
}` ,
{ source },
);
2026-09-21 13:30:33 -07:00
assert . ok ( factory . ok && factory . resource . kind === "interface" , JSON . stringify ( factory . diagnostics ));
2026-09-21 14:03:14 -07:00
const data = compileCapabilityResourceSource (
`interface Data id "interface:data" revision "interface:data@1" {
2026-09-28 20:29:21 -07:00
queryable value body id "body" : string { get id "body:get" execution snapshot; edit id "body:edit" execution transaction; }
queryable value notes id "notes" : string { get id "notes:get" execution snapshot; edit id "notes:edit" execution transaction; }
operation revise id "revise" : record {body:change<string>; notes:change<string>; reject:bool;} -> unit {call id "revise:call" execution transaction;}
2026-09-21 14:03:14 -07:00
queryable value label id "label" : string {
2026-09-28 20:29:21 -07:00
get id "label:get" execution snapshot;
set id "label:set" execution transaction;
2026-09-21 14:03:14 -07:00
}
}` ,
{ source },
);
const constructors = compileCapabilityResourceSource (
`external atom Entry id "atom:entry"; external atom Independent id "atom:independent";
interface Factories id "interface:factories" revision "interface:factories@1" {
2026-09-28 20:29:21 -07:00
static operation createOne id "create-one" : string -> atom-ref<Entry> { call id "create-one:call" execution transaction; }
static operation createTwo id "create-two" : unit -> atom-ref<Independent> { call id "create-two:call" execution transaction; }
2026-09-21 14:03:14 -07:00
}` ,
{ source },
);
assert . ok ( data . ok && data . resource . kind === "interface" , JSON . stringify ( data . diagnostics ));
assert . ok ( constructors . ok && constructors . resource . kind === "interface" , JSON . stringify ( constructors . diagnostics ));
const dataInterface = data . resource . revision ,
factoriesInterface = constructors . resource . revision ;
2026-09-22 13:16:23 -07:00
const view = compileCapabilityResourceSource (
'import interface Data; interface View id "interface:device-view" revision "interface:device-view@1" { module render id "member:device-view:render" contract "org.quixos.react.subject-only/1" subject interface Data; }' ,
2026-09-21 15:24:05 -07:00
{ source , environment : { interfaces : new Map ([[ "Data" , dataInterface ]]), interfaceClosure : [ dataInterface ] } },
);
2026-09-22 13:16:23 -07:00
assert . ok ( view . ok && view . resource . kind === "interface" , JSON . stringify ( view . diagnostics ));
const viewInterface = view . resource . revision ;
2026-09-30 22:03:17 -07:00
function declareInputs ( pkg : PackageRevision ) {
const declarations = presentationDeclarations (
JSON . stringify ({
modules : Object.fromEntries (( pkg . modules ?? []). map (( module ) => [ module . displayName , { inputs : [] }])),
}),
pkg ,
[ dataInterface , viewInterface ],
);
pkg . buildArtifacts = { [ preparationArtifact ] : declarations . artifact };
pkg . queryCompositions = declarations . compositions . map (( entry ) => compileQueryComposition ( entry , new Map ()));
}
2026-09-22 13:16:23 -07:00
const ui = compileCapabilityResourceSource (
'import interface Data; import interface View; package UI id "package:ui" revision "package:ui@1" { module Card id "component:card" implements View.render; }' ,
{
source ,
environment : {
interfaces : new Map ([
[ "Data" , dataInterface ],
[ "View" , viewInterface ],
]),
interfaceClosure : [ dataInterface , viewInterface ],
},
},
);
2026-09-21 15:24:05 -07:00
assert . ok ( ui . ok && ui . resource . kind === "package" , JSON . stringify ( ui . diagnostics ));
2026-09-30 22:03:17 -07:00
declareInputs ( ui . resource . revision );
2026-09-21 14:03:14 -07:00
for ( const variant of [ "base" , "added" , "gateway" , "gateway-next" , "session" ]) {
const session = variant === "session" ,
gateway = session || variant . startsWith ( "gateway" ),
commit = { base : "a" , added : "b" , gateway : "c" , "gateway-next" : "d" , session : "e" }[ variant ] ! . repeat ( 40 );
2026-09-21 13:30:33 -07:00
const pkg = compileCapabilityResourceSource (
2026-09-28 20:29:21 -07:00
`symbol JavaScript id "target:org.quixos.execution.javascript@1";
${ gateway ? 'external atom Entry id "atom:entry";' : "" }
2026-09-22 13:16:23 -07:00
${ session ? 'external atom Independent id "atom:independent"; import interface Data; import interface View;' : "" }
2026-09-21 13:30:33 -07:00
package Owner id "package:owner" revision "package:owner@1" {
function identity id "identity" : int32 -> record { pid: int32; child: int32; } execution remote;
${ gateway ? 'function create id "create" : unit -> atom-ref<Entry> execution transaction targets [server] requires { creation entry id "entry:birth" : Entry input unit; };' : "" }
2026-09-21 14:03:14 -07:00
${
session
2026-09-22 13:16:23 -07:00
? `module Editor id "component:owner-editor" implements View.render;
2026-09-28 20:29:21 -07:00
function createOne id "create-one" : string -> atom-ref<Entry> execution transaction targets [JavaScript, server] requires { creation entry id "one:birth" : Entry input unit; };
function createTwo id "create-two" : unit -> atom-ref<Independent> execution transaction targets [JavaScript, server] requires { creation entry id "two:birth" : Independent input unit; };
operation revise id "revise" : record {body:change<string>; notes:change<string>; reject:bool;} -> unit mode call execution transaction targets [JavaScript, server] receiver any requires {
2026-09-21 14:45:58 -07:00
state body id "revise:body" : string [edit]; state notes id "revise:notes" : string [edit];
};
2026-09-21 14:03:14 -07:00
query Inputs id "inputs" root workspace Data document "inputs.graphql" operation "Inputs" { replica; }
query View id "view" root workspace Data document "view.graphql" operation "View" { local from Inputs; watch; }
2026-09-21 14:45:58 -07:00
query EditorView id "editor-view" root Data document "editor-view.graphql" operation "EditorView" { local from EditorInputs; watch; }
2026-09-21 14:03:14 -07:00
query EditorInputs id "editor-inputs" root Data document "editor.graphql" operation "Editor" { replica; }`
: ""
}
2026-09-21 13:30:33 -07:00
}` ,
2026-09-21 14:03:14 -07:00
{
source ,
...( session
2026-09-22 13:16:23 -07:00
? {
environment : {
interfaces : new Map ([
[ "Data" , dataInterface ],
[ "View" , viewInterface ],
]),
interfaceClosure : [ dataInterface , viewInterface ],
},
}
2026-09-21 14:03:14 -07:00
: {}),
},
2026-09-21 13:30:33 -07:00
);
assert . ok ( pkg . ok && pkg . resource . kind === "package" , JSON . stringify ( pkg . diagnostics ));
2026-09-30 22:03:17 -07:00
if ( session ) declareInputs ( pkg . resource . revision );
2026-09-21 14:03:14 -07:00
if ( session )
pkg . resource . revision . checkedQueries = await Promise . all (
pkg . resource . revision . queries ! . map (( query ) =>
compileQuery (
query ,
[ dataInterface ],
async ( name ) =>
({
"inputs.graphql" :
2026-09-22 15:31:42 -07:00
"query Inputs($label: String!) { root { objects(all: 100, where: {label: {eq: $label}}) { entries { key node { _camino {ref} label } } } } }" ,
2026-09-21 14:03:14 -07:00
"view.graphql" :
2026-09-22 15:31:42 -07:00
"query View { root { availableObjects(all: 100) { entries { key node { _camino {ref} label } } } } }" ,
"editor.graphql" : "query Editor { root { _camino {ref} label body notes } }" ,
2026-09-21 14:45:58 -07:00
"editor-view.graphql" : "query EditorView { root { label body notes } }" ,
2026-09-21 14:03:14 -07:00
})[ name ] ! ,
),
),
);
2026-09-21 12:52:21 -07:00
const result = compileCapabilitySource (
`workspace Managed id "workspace:managed-process" revision "workspace:managed-process@ ${ variant } " commit " ${ commit } " {
import package Owner;
2026-09-21 15:24:05 -07:00
${ session ? "import package UI;" : "" }
2026-09-21 14:03:14 -07:00
${
session
2026-09-22 13:16:23 -07:00
? `import interface Data; import interface View; import interface Factories;
2026-09-21 14:03:14 -07:00
conform Entry as Factories id "owner:factories" { bind createOne.call to package Owner.createOne with { entry to creation Entry with {}; }; bind createTwo.call to package Owner.createTwo with { entry to creation Independent with {}; }; }
2026-09-21 14:45:58 -07:00
${ [ "Entry" , "Independent" ]. map (( atom ) => `conform ${ atom } as Data id "owner: ${ atom } :data" { private state ${ atom } Label id "label: ${ atom } " on ${ atom } : string policy register default " ${ atom } "; bind label.get to state ${ atom } Label.read; bind label.set to state ${ atom } Label.write; private state ${ atom } Body id "body: ${ atom } " on ${ atom } : string policy crdt(string) default "hello 🌍"; private state ${ atom } Notes id "notes: ${ atom } " on ${ atom } : string policy crdt(string) default "notes"; bind body.get to state ${ atom } Body.read; bind body.edit to state ${ atom } Body.edit; bind notes.get to state ${ atom } Notes.read; bind notes.edit to state ${ atom } Notes.edit; bind revise.call to package Owner.revise with {body to state ${ atom } Body; notes to state ${ atom } Notes;}; }` ). join ( "\n" ) }
2026-09-21 14:03:14 -07:00
`
: ""
}
2026-09-21 13:30:33 -07:00
${ variant !== "base" ? 'atom Independent id "atom:independent";' : "" }
${
gateway
? `import interface EntryFactory; atom Entry id "atom:entry";
conform Entry as EntryFactory id "owner:entry-factory" {
bind create.call to package Owner.create with { entry to creation Entry with {}; };
}`
: ""
}
2026-09-21 12:52:21 -07:00
}` ,
"workspace.qx" ,
2026-09-21 13:30:33 -07:00
{
2026-09-21 15:24:05 -07:00
packages : new Map ([
[ "Owner" , pkg . resource . revision ],
...( session ? [[ "UI" , ui . resource . revision ] as const ] : []),
]),
packageClosure : [ pkg . resource . revision , ...( session ? [ ui . resource . revision ] : [])],
2026-09-21 13:30:33 -07:00
...( gateway
? {
2026-09-21 14:03:14 -07:00
interfaces : new Map ([
[ "EntryFactory" , factory . resource . revision ],
...( session
? ([
[ "Data" , dataInterface ],
2026-09-22 13:16:23 -07:00
[ "View" , viewInterface ],
2026-09-21 14:03:14 -07:00
[ "Factories" , factoriesInterface ],
] as const )
: []),
]),
2026-09-22 13:16:23 -07:00
interfaceClosure : [
factory . resource . revision ,
...( session ? [ dataInterface , viewInterface , factoriesInterface ] : []),
],
2026-09-21 13:30:33 -07:00
}
: {}),
},
2026-09-21 12:52:21 -07:00
);
assert . ok ( result . ok , JSON . stringify ( result . diagnostics ));
const plan = compileExecutionPlan ( result . workspace ),
root = `dist/managed/ ${ variant } ` ;
await mkdir ( root , { recursive : true });
2026-09-21 14:03:14 -07:00
if ( session ) {
await writeFile (
` ${ root } /client.ts` ,
generateInterfaceClientBindings ({ Data : dataInterface , Factories : factoriesInterface }),
);
await writeFile ( ` ${ root } /queries.ts` , generatePackageQueryClientBindings ( pkg . resource . revision ));
}
2026-09-21 12:52:21 -07:00
for ( const [ file , value ] of Object . entries ({
"candidate.json" : result . workspace ,
2026-09-21 13:30:33 -07:00
"entry.json" : gateway
? { atom : "atom:entry" , interface : "interface:entry-factory@1" , operation : "entry:create:call" }
: null ,
2026-09-21 12:52:21 -07:00
"execution-plan.json" : plan ,
"kernel.json" : compileKernelProgram ( plan ),
"queries.json" : compileQueryProgram ( plan ),
"source.json" : { kind : "workspace" , repository : "https://example.test/managed.git" , commit },
}))
await writeFile ( ` ${ root } / ${ file } ` , JSON . stringify ( value ));
}
2026-09-21 17:38:45 -07:00
await import ( "./emit-controllers.js" );
2026-09-22 13:51:32 -07:00
// Standalone public builder roots must work without opting into the other modes.
for ( const kind of [ "portable" , "service" ]) {
const pkg = compileCapabilityResourceSource (
2026-09-28 20:29:21 -07:00
`symbol JavaScript id "target:org.quixos.execution.javascript@1";
package Owner id "package:owner" revision "package:owner@1" {
2026-09-22 13:51:32 -07:00
function identity id "identity" : int32 -> record {pid:int32;child:int32;}
2026-09-28 20:29:21 -07:00
execution ${ kind === "portable" ? "snapshot targets [JavaScript,server]" : "remote" } ;
2026-09-22 13:51:32 -07:00
}` ,
{ source },
);
assert . ok ( pkg . ok && pkg . resource . kind === "package" , JSON . stringify ( pkg . diagnostics ));
const workspace = compileCapabilitySource (
`workspace Simple id "workspace:simple" revision "workspace:simple@1" commit " ${ source . commit } " {import package Owner;}` ,
"workspace.qx" ,
{ packages : new Map ([[ "Owner" , pkg . resource . revision ]]) },
);
assert . ok ( workspace . ok , JSON . stringify ( workspace . diagnostics ));
const directory = `dist/managed/standalone- ${ kind } ` ;
await mkdir ( directory , { recursive : true });
await writeFile ( ` ${ directory } /execution-plan.json` , JSON . stringify ( compileExecutionPlan ( workspace . workspace )));
}