logicsrc/packages/opencontext/src/conformance.test.ts
Anthony Ettinger 3ab8a4b38b
Add the LogicSRC OpenContext specification (#132)
* Add the LogicSRC OpenContext specification

OpenContext is an open specification for durable, portable, permissioned,
provenance-aware context shared between humans and AI agents. It defines how
organizational knowledge is described, authorized, versioned, resolved,
audited, and handed between replaceable workers without losing institutional
state.

Follows the OpenPRD/OpenOntology pattern already in the repo: self-contained
JSON Schemas in @logicsrc/schemas, a reference implementation package, CLI
subcommands, docs, examples, and an OpenPRD record.

Schemas (8, all self-contained so a third party can fetch one file and
validate against it with no further resolution):
  manifest, object, bundle, role, provenance, decision, diagnostic,
  audit-event — registered in @logicsrc/validators and schemas:validate.

Reference implementation (@logicsrc/opencontext):
  loader with upward manifest discovery, the full resolution pipeline,
  authority/supersession, permissions, redaction, lifecycle, provenance,
  deterministic digests, doctor, search, graph, history/diff, guarded writes,
  audit events, and file/http/git/sqlite adapters.

CLI: all 15 specified commands, as a standalone `opencontext` binary and as
`logicsrc context`, sharing one implementation so the two cannot drift.

Design decisions worth noting:

- Supersession is declared, never inferred from version numbers. Inferring it
  would hide the governance failure it represents and make
  multiple-active-versions and duplicate-canonical impossible to detect.

- The bundle digest identifies the resolved context, not the moment it was
  computed, so generated_at/bundle_id/digest/as_of are excluded while objects,
  lifecycle states, exclusions and warnings are covered. That is what lets a
  decision record cite exactly the context that produced it.

- A role's own max_classification beats an inherited one, so a ceiling on a
  shared base role cannot silently cap a role deliberately granted more;
  requesting several roles at once still takes the lowest, so combining roles
  never escalates.

- Scope wildcards match whole dotted segments only. A trailing .* covers a
  subtree; an interior * matches exactly one segment. Substring matching here
  would be an access-control bug.

- --include narrows an existing scope and is applied after it, never merged
  into it, so a request can never widen what a role holds.

Verified: 226 tests across core primitives, permissions/redaction, the
resolution pipeline, security, the published conformance fixtures (13 valid,
35 invalid, 8 resolution scenarios), project behaviour, and the five shipped
examples — which are held to --strict and a 100% health score. Benchmarks meet
every published budget (resolve 1,000 objects in ~33ms against a 2s target).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* Point install docs at @logicsrc/opencontext; record the npm name collision

The unscoped `opencontext` name is already published on npm by an unrelated
third party (federicodeponte/opencontext, 2.0.0), so `npx opencontext` would
install a stranger's package. Docs now use `npx @logicsrc/opencontext`; the bin
stays named `opencontext` so the command reads as the PRD specifies once
installed.

Recorded in PRD 0003 as a blocker to resolve before any publication, along with
the fact that no @logicsrc spec package has ever been published, so there is no
existing release path to slot into.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* Advance the logicsrc-mcp next-PRD-id assertion to 0004

standards.test.ts asserts prd_next_id against the live prd/ directory, so
adding PRD 0003 makes the next free id 0004. The test's own comment
anticipates this: "advances with every PRD added".

Caught by CI, not locally — the earlier verification ran per-package tests for
the packages this branch touches, and logicsrc-mcp is coupled to the PRD
directory without importing from it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-09 11:46:11 -07:00

169 lines
6.6 KiB
TypeScript

/**
* The official conformance suite.
*
* Everything here runs against the *published* fixtures under
* `@logicsrc/schemas`, not against private test data, so a third-party
* implementation can run exactly the same cases. Schema fixtures need no
* OpenContext code at all — only a JSON Schema validator. The resolution
* scenarios go further and pin resolver behaviour that schemas cannot express:
* scope, authority, supersession, lifecycle, and redaction.
*/
import { describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { validate } from "@logicsrc/validators";
import { OpenContext } from "./index.js";
import { hasFailure } from "./validate.js";
const FIXTURES = join(dirname(fileURLToPath(import.meta.url)), "../../schemas/fixtures/opencontext");
interface Conformance {
opencontextConformance: string;
valid: Array<{ fixture: string; kind: string }>;
invalid: Array<{ fixture: string; kind: string; why: string }>;
resolution: Array<{ scenario: string; expected: string }>;
}
const conformance = JSON.parse(readFileSync(join(FIXTURES, "conformance.json"), "utf8")) as Conformance;
function readFixture(relative: string): unknown {
return JSON.parse(readFileSync(join(FIXTURES, relative), "utf8"));
}
describe("conformance: schemas", () => {
it("declares a v1 suite", () => {
expect(conformance.opencontextConformance).toBe("1.0");
expect(conformance.valid.length).toBeGreaterThan(0);
expect(conformance.invalid.length).toBeGreaterThan(0);
});
it.each(conformance.valid)("valid: $fixture", ({ fixture, kind }) => {
const result = validate(kind as never, readFixture(fixture));
if (!result.ok) {
const detail = result.errors.map((error) => `${error.instancePath || "/"} ${error.message}`).join("; ");
throw new Error(`${fixture} should satisfy ${kind}: ${detail}`);
}
expect(result.ok).toBe(true);
});
it.each(conformance.invalid)("invalid: $fixture ($why)", ({ fixture, kind }) => {
const result = validate(kind as never, readFixture(fixture));
expect(result.ok).toBe(false);
});
});
interface Expectation {
description: string;
resolve?: { role?: string; agent?: string; task?: string; at?: string; includeHistorical?: boolean };
expect?: {
included?: string[];
includedVersions?: Record<string, number>;
objectCount?: number;
excluded?: Array<{ id: string; reason: string }>;
warnings?: string[];
lifecycle?: Record<string, string>;
redacted?: Record<string, string[]>;
contentAbsent?: Record<string, string[]>;
contentEquals?: Record<string, Record<string, unknown>>;
};
also?: Array<{ resolve: Expectation["resolve"]; expect: Expectation["expect"] }>;
validate?: { expectDiagnostics?: string[]; expectFailure?: boolean };
}
describe("conformance: resolution", () => {
it("declares scenarios", () => {
expect(conformance.resolution.length).toBeGreaterThanOrEqual(8);
});
it.each(conformance.resolution)("$scenario", async ({ scenario, expected }) => {
const spec = readFixture(expected) as Expectation;
const oc = await OpenContext.load(join(FIXTURES, scenario));
if (spec.validate) {
const findings = oc.validate();
for (const code of spec.validate.expectDiagnostics ?? []) {
expect(findings.map((finding) => finding.code)).toContain(code);
}
if (spec.validate.expectFailure) {
expect(hasFailure(findings, "error")).toBe(true);
}
}
const runs = [
...(spec.resolve ? [{ resolve: spec.resolve, expect: spec.expect }] : []),
...(spec.also ?? [])
];
for (const run of runs) {
const result = oc.resolve({ ...run.resolve, explain: true });
const bundle = result.bundle;
const ids = bundle.objects.map((object) => object.id).sort();
const want = run.expect ?? {};
if (want.included) expect(ids).toEqual([...want.included].sort());
if (want.objectCount !== undefined) expect(bundle.objects).toHaveLength(want.objectCount);
if (want.includedVersions) {
for (const [id, version] of Object.entries(want.includedVersions)) {
expect(bundle.objects.find((object) => object.id === id)?.version).toBe(version);
}
}
for (const expectation of want.excluded ?? []) {
expect(
result.excluded.some((item) => item.id === expectation.id && item.reason === expectation.reason),
`expected ${expectation.id} to be excluded as ${expectation.reason}, got ${JSON.stringify(result.excluded)}`
).toBe(true);
}
for (const code of want.warnings ?? []) {
expect(bundle.warnings?.map((warning) => warning.code)).toContain(code);
}
for (const [id, state] of Object.entries(want.lifecycle ?? {})) {
expect(bundle.objects.find((object) => object.id === id)?.lifecycle).toBe(state);
}
for (const [id, paths] of Object.entries(want.redacted ?? {})) {
expect(bundle.objects.find((object) => object.id === id)?.redacted).toEqual(paths);
}
for (const [id, paths] of Object.entries(want.contentAbsent ?? {})) {
const content = bundle.objects.find((object) => object.id === id)?.content as Record<string, unknown>;
for (const path of paths) expect(content?.[path]).toBeUndefined();
}
for (const [id, pairs] of Object.entries(want.contentEquals ?? {})) {
const content = bundle.objects.find((object) => object.id === id)?.content;
for (const [path, value] of Object.entries(pairs)) {
expect(readPath(content, path)).toEqual(value);
}
}
}
});
it("produces identical digests for a repeated run of every scenario", async () => {
// Determinism is a conformance requirement, not an implementation detail.
for (const { scenario, expected } of conformance.resolution) {
const spec = readFixture(expected) as Expectation;
if (!spec.resolve) continue;
const first = (await OpenContext.load(join(FIXTURES, scenario))).bundle({ ...spec.resolve });
const second = (await OpenContext.load(join(FIXTURES, scenario))).bundle({ ...spec.resolve });
expect(second.digest, `${scenario} digest drifted between runs`).toBe(first.digest);
}
});
});
function readPath(node: unknown, path: string): unknown {
let current = node;
for (const segment of path.split(".")) {
if (current === null || current === undefined) return undefined;
if (Array.isArray(current)) current = current[Number.parseInt(segment, 10)];
else if (typeof current === "object") current = (current as Record<string, unknown>)[segment];
else return undefined;
}
return current;
}