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>
This commit is contained in:
Anthony Ettinger 2026-08-09 11:46:11 -07:00 committed by GitHub
parent 1bb7ba6e60
commit 3ab8a4b38b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
212 changed files with 17249 additions and 7 deletions

View file

@ -0,0 +1,88 @@
/**
* Canonical serialization and digests.
*
* A bundle digest is what lets a decision record cite exactly the context that
* produced it, and what lets CI prove that a resolution has not drifted. That
* only works if serialization is canonical, so key order, undefined handling,
* and number formatting are all pinned here rather than left to JSON.stringify
* defaults.
*/
import { createHash } from "node:crypto";
/**
* Deterministic JSON: object keys sorted, `undefined` dropped, arrays left in
* their (already deterministic) order, no insignificant whitespace.
*/
export function canonicalJson(value: unknown): string {
return JSON.stringify(canonicalize(value));
}
function canonicalize(value: unknown): unknown {
if (value === null) return null;
if (Array.isArray(value)) return value.map(canonicalize).filter((item) => item !== undefined);
if (value instanceof Date) return value.toISOString();
if (typeof value === "object") {
const source = value as Record<string, unknown>;
const result: Record<string, unknown> = {};
for (const key of Object.keys(source).sort()) {
const canonical = canonicalize(source[key]);
if (canonical !== undefined) result[key] = canonical;
}
return result;
}
if (typeof value === "number" && !Number.isFinite(value)) {
throw new Error(`Cannot canonicalize non-finite number: ${String(value)}`);
}
return value;
}
export function sha256Hex(input: string | Uint8Array): string {
return createHash("sha256").update(input).digest("hex");
}
/** `sha256:<64 hex>` — the form used by source digests and bundle digests. */
export function sha256Uri(input: string | Uint8Array): string {
return `sha256:${sha256Hex(input)}`;
}
/** Digest of a structure, over its canonical JSON. */
export function digestOf(value: unknown): string {
return sha256Uri(canonicalJson(value));
}
/**
* Fields excluded from a bundle's digest.
*
* The digest identifies **the resolved context**, not the moment it was
* computed. So the three clock-and-self fields are excluded:
*
* - `generated_at` wall-clock, differs between two otherwise identical runs
* - `digest` cannot contain itself
* - `bundle_id` derived from the digest
*
* `as_of` is excluded for the same reason, and it is worth being precise about
* why, because it looks like a resolution input. Resolving at two different
* instants only matters if it *changes what was selected* and any such change
* is already covered, because every object's computed `lifecycle`, along with
* the full `objects`, `excluded`, and `warnings` lists, is inside the digest.
* Two resolutions that select the same context at the same lifecycle states are
* the same context, and should digest identically whether they ran a second or
* a month apart. That is precisely the property a decision record needs when it
* cites the context it was made from.
*/
export const BUNDLE_DIGEST_EXCLUDED = ["generated_at", "digest", "bundle_id", "as_of"] as const;
export function digestBundle(bundle: Record<string, unknown>): string {
const subject: Record<string, unknown> = {};
for (const [key, value] of Object.entries(bundle)) {
if ((BUNDLE_DIGEST_EXCLUDED as readonly string[]).includes(key)) continue;
subject[key] = value;
}
return digestOf(subject);
}
/** Bundle ids are derived from the digest so identical input yields an identical id. */
export function bundleIdFromDigest(digest: string): string {
return `ocb_${digest.replace(/^sha256:/, "").slice(0, 16)}`;
}