Files
quixos-protocol/test/capability-model.test.ts
T
Timothy J. Aveni 01ca965c7f Make workspace authoring converge through immutable Nix candidates
Coordinate registered resource edits bottom-up into retained exact remote sources.
Use one Nix-owned source graph for provisional checking, template publication,
explicit baseline upgrades and host activation; retain independent runtime pins.

Add scoped contract inspection, historical recovery, derived worklists, crash-safe
locks, named dependency adoption and plain-QX structural editing. Repair TODO
ownership and template instantiation, and document the supported agent workflow.

Validated with protocol and command suites, real jj/Nix convergence and cache
checks, TS/React installed-command acceptance, and fresh TODO first-edit acceptance.
No live deployment or public publication performed. Props projection generation
and a one-command rich feature generator remain explicitly outside this delivery.
2026-09-14 12:25:47 -07:00

296 lines
11 KiB
TypeScript

import assert from "node:assert/strict";
import { test } from "node:test";
import {
compileWorkspaceRevision,
computeCapabilityClosure,
resolveOperationPlan,
validateWorkspaceRevision,
valueType,
type CapabilityValidationIssueCode,
type WorkspaceRevision,
} from "../src/capability-model/index.js";
import {
fixtureId,
makeValidCapabilityWorkspace,
} from "./fixtures/capability-model.js";
test("ordinary state rejects managed references even under list/optional wrappers", () => {
const workspace = makeValidCapabilityWorkspace();
const state = [...workspace.sharedAttachments, ...workspace.conformances.flatMap((entry) => entry.privateAttachments)].find((entry) => entry.kind === "state")!;
if (state.kind !== "state") throw new Error("fixture missing state");
state.valueType = {kind: "list", value: {kind: "optional", value: {kind: "object-ref", expectation: {kind: "atom", atomId: state.attachedTo}}}};
assert.ok(validateWorkspaceRevision(workspace).some((entry) => entry.message.includes("graph relationships")));
});
const expectIssue = (
workspace: WorkspaceRevision,
code: CapabilityValidationIssueCode,
) => {
const issues = validateWorkspaceRevision(workspace);
assert.ok(
issues.some((entry) => entry.code === code),
`Expected ${code}, got:\n${issues
.map((entry) => `${entry.code} ${entry.path}: ${entry.message}`)
.join("\n")}`,
);
assert.equal(compileWorkspaceRevision(workspace).ok, false);
};
test("constructor dependency input contracts match the selected constructor", () => {
const workspace = makeValidCapabilityWorkspace();
const port = workspace.packageImports.flatMap((pkg) => pkg.exports).flatMap((entry) => entry.dependencyPorts)
.find((port) => port.requirement.kind === "constructor")!;
assert.equal(port.requirement.kind, "constructor");
if (port.requirement.kind !== "constructor") return;
port.requirement.inputType = valueType.unit;
assert.deepEqual(validateWorkspaceRevision(workspace), []);
port.requirement.inputType = valueType.string;
expectIssue(workspace, "invalid-dependency-binding");
});
const conformance = (
workspace: WorkspaceRevision,
identity: Pick<(typeof workspace.conformances)[number], "atomId" | "interfaceRevisionId">,
) => {
const result = workspace.conformances.find((entry) =>
entry.atomId === identity.atomId &&
entry.interfaceRevisionId === identity.interfaceRevisionId
);
assert.ok(
result,
`Missing fixture conformance ${identity.atomId} as ${identity.interfaceRevisionId}`,
);
return result;
};
test("the representative v1 workspace compiles to native and package plans", () => {
const workspace = makeValidCapabilityWorkspace();
assert.deepEqual(validateWorkspaceRevision(workspace), []);
const compiled = compileWorkspaceRevision(workspace);
assert.equal(compiled.ok, true);
if (!compiled.ok) return;
const title = resolveOperationPlan(
compiled.plan,
fixtureId.project,
fixtureId.namedV1,
fixtureId.namedGet,
);
assert.equal(title?.kind, "state");
if (title?.kind === "state") {
assert.equal(title.binding.slotId, fixtureId.projectTitle);
assert.equal(title.binding.primitive, "read");
assert.deepEqual(title.attachment.owner, { kind: "workspace" });
}
const owner = resolveOperationPlan(
compiled.plan,
fixtureId.project,
fixtureId.ownedV1,
fixtureId.ownerResolve,
);
assert.equal(owner?.kind, "edge");
if (owner?.kind === "edge") {
assert.equal(owner.binding.edgeTypeId, fixtureId.projectOwner);
assert.deepEqual(owner.attachment.owner, {
kind: "conformance",
...fixtureId.projectOwnedConformance,
});
}
const summary = resolveOperationPlan(
compiled.plan,
fixtureId.project,
fixtureId.summaryV1,
fixtureId.summaryGet,
);
assert.equal(summary?.kind, "package");
if (summary?.kind === "package") {
assert.equal(summary.packageRevision.revisionId, fixtureId.todoRuntimeV1);
assert.equal(summary.packageExport.id, fixtureId.summaryGetExport);
assert.deepEqual(
summary.dependencies.map((entry) => [entry.port.id, entry.binding.kind]),
[
[fixtureId.titlePort, "state"],
[fixtureId.namedPort, "interface"],
[fixtureId.personPort, "constructor"],
],
);
}
});
test("the closure is an exact tree-shaking boundary", () => {
const result = compileWorkspaceRevision(makeValidCapabilityWorkspace());
assert.equal(result.ok, true);
if (!result.ok) return;
const closure = computeCapabilityClosure(result.plan, [
{ atomId: fixtureId.project, interfaceRevisionId: fixtureId.summaryV1 },
]);
assert.deepEqual(closure.conformances, [
fixtureId.projectNamedConformance,
fixtureId.projectSummaryConformance,
]);
assert.deepEqual(closure.packageRevisionIds, [fixtureId.todoRuntimeV1]);
assert.deepEqual(closure.attachmentIds, [fixtureId.projectTitle]);
assert.deepEqual(closure.constructorAtomIds, [fixtureId.person]);
});
test("compiled plans are snapshots, not mutable authoring state", () => {
const workspace = makeValidCapabilityWorkspace();
const result = compileWorkspaceRevision(workspace);
assert.equal(result.ok, true);
if (!result.ok) return;
conformance(workspace, fixtureId.projectNamedConformance)
.operationBindings[0]!.binding = {
kind: "state",
slotId: fixtureId.personName,
primitive: "read",
};
const resolved = resolveOperationPlan(
result.plan,
fixtureId.project,
fixtureId.namedV1,
fixtureId.namedGet,
);
assert.equal(resolved?.kind, "state");
if (resolved?.kind === "state") {
assert.equal(resolved.binding.slotId, fixtureId.projectTitle);
}
});
test("a conformance binds every operation exactly once", () => {
const missing = makeValidCapabilityWorkspace();
conformance(missing, fixtureId.projectNamedConformance).operationBindings.pop();
expectIssue(missing, "missing-operation-binding");
const duplicate = makeValidCapabilityWorkspace();
const bindings = conformance(
duplicate,
fixtureId.projectNamedConformance,
).operationBindings;
bindings.push(structuredClone(bindings[0]!));
expectIssue(duplicate, "duplicate-operation-binding");
});
test("private attachments are visible only to their owning conformance", () => {
const workspace = makeValidCapabilityWorkspace();
const binding = conformance(
workspace,
fixtureId.projectNamedConformance,
).operationBindings[0]!.binding;
assert.equal(binding.kind, "state");
if (binding.kind !== "state") return;
binding.slotId = fixtureId.personName;
expectIssue(workspace, "private-attachment-access");
});
test("related-object dependency views cannot traverse another conformance's private edge", () => {
const workspace = makeValidCapabilityWorkspace();
const packageBinding = conformance(
workspace,
fixtureId.projectSummaryConformance,
).operationBindings[0]!.binding;
assert.equal(packageBinding.kind, "package");
if (packageBinding.kind !== "package") return;
const named = packageBinding.dependencies.find(
(dependency) => dependency.portId === fixtureId.namedPort,
);
assert.ok(named && named.binding.kind === "interface");
named.binding.via = {
edgeTypeId: fixtureId.projectOwner,
projectionId: fixtureId.projectOwnerProjection,
};
expectIssue(workspace, "private-attachment-access");
const owner = conformance(workspace, fixtureId.projectOwnedConformance);
const edge = owner.privateAttachments.find((entry) => entry.kind === "edge" && entry.id === fixtureId.projectOwner);
assert.ok(edge?.kind === "edge");
edge.endpoints.find((endpoint) => endpoint.projectionId === fixtureId.projectOwnerProjection)!.publicTraversal = true;
assert.equal(validateWorkspaceRevision(workspace).some((entry) => entry.code === "private-attachment-access"), false, "Only an explicitly exported read-only traversal crosses ownership");
});
test("public traversal permits a native inverse read without exporting mutation authority", () => {
const workspace = makeValidCapabilityWorkspace();
const owned = conformance(workspace, fixtureId.projectOwnedConformance);
const index = owned.privateAttachments.findIndex(entry => entry.kind === "edge" && entry.id === fixtureId.projectOwner);
const edge = owned.privateAttachments.splice(index, 1)[0];
assert.ok(edge?.kind === "edge");
conformance(workspace, fixtureId.projectNamedConformance).privateAttachments.push(edge);
expectIssue(workspace, "private-attachment-access");
edge.endpoints.find(endpoint => endpoint.projectionId === fixtureId.projectOwnerProjection)!.publicTraversal = true;
const readPath = `conformances[${workspace.conformances.indexOf(owned)}].operationBindings[0]`;
assert.equal(validateWorkspaceRevision(workspace).some(issue => issue.code === "private-attachment-access" && issue.path.startsWith(readPath)), false);
const binding = owned.operationBindings[0].binding;
assert.ok(binding.kind === "edge");
binding.primitive = "connect";
expectIssue(workspace, "private-attachment-access");
});
test("native state and edge providers must match operation shape", () => {
const stateWorkspace = makeValidCapabilityWorkspace();
const state = conformance(
stateWorkspace,
fixtureId.projectNamedConformance,
).operationBindings[0]!.binding;
assert.equal(state.kind, "state");
if (state.kind === "state") state.primitive = "write";
expectIssue(stateWorkspace, "invalid-state-binding");
const edgeWorkspace = makeValidCapabilityWorkspace();
const edge = conformance(
edgeWorkspace,
fixtureId.projectOwnedConformance,
).operationBindings[0]!.binding;
assert.equal(edge.kind, "edge");
if (edge.kind === "edge") edge.primitive = "connect";
expectIssue(edgeWorkspace, "invalid-edge-binding");
});
test("package dependencies are complete, exact, and explicitly injected", () => {
const missing = makeValidCapabilityWorkspace();
const packageBinding = conformance(
missing,
fixtureId.projectSummaryConformance,
).operationBindings[0]!.binding;
assert.equal(packageBinding.kind, "package");
if (packageBinding.kind === "package") packageBinding.dependencies.pop();
expectIssue(missing, "invalid-dependency-binding");
const wrongType = makeValidCapabilityWorkspace();
const summaryExport = wrongType.packageImports[0]!.exports.find(
(entry) => entry.id === fixtureId.summaryGetExport,
);
assert.ok(summaryExport);
summaryExport.dependencyPorts[0]!.requirement = {
kind: "state",
valueType: valueType.int32,
primitives: ["read"],
};
expectIssue(wrongType, "invalid-dependency-binding");
});
test("package receiver requirements cannot depend on themselves", () => {
const workspace = makeValidCapabilityWorkspace();
const summaryExport = workspace.packageImports[0]!.exports.find(
(entry) => entry.id === fixtureId.summaryGetExport,
);
assert.ok(summaryExport && summaryExport.kind === "operation");
summaryExport.receiverRequirement = {
kind: "all-interfaces",
interfaceRevisionIds: [fixtureId.summaryV1],
};
summaryExport.dependencyPorts = [];
const packageBinding = conformance(
workspace,
fixtureId.projectSummaryConformance,
).operationBindings[0]!.binding;
assert.equal(packageBinding.kind, "package");
if (packageBinding.kind === "package") packageBinding.dependencies = [];
expectIssue(workspace, "cyclic-conformance-requirement");
});
test("constructors are statically checked", () => {
const badConstructor = makeValidCapabilityWorkspace();
badConstructor.constructors[0]!.exportId = fixtureId.summaryGetExport;
expectIssue(badConstructor, "invalid-constructor");
});