mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-08-14 14:57:28 +00:00
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>
124 lines
4.1 KiB
TypeScript
124 lines
4.1 KiB
TypeScript
/**
|
|
* Audit events.
|
|
*
|
|
* The specification defines the event shape and deliberately does not mandate
|
|
* storage: an NDJSON file committed next to the context is a conforming sink,
|
|
* and so is a warehouse. What matters is that after an agent is retired its
|
|
* reads, writes, and refusals remain attributable — and that a recorded bundle
|
|
* digest makes the record verifiable rather than merely descriptive.
|
|
*/
|
|
|
|
import { appendFileSync, mkdirSync } from "node:fs";
|
|
import { dirname } from "node:path";
|
|
import type { ContextBundle, EffectiveScope, Manifest } from "./types.js";
|
|
import { resolveInside } from "./adapters/file.js";
|
|
import { SPEC_VERSION } from "./manifest.js";
|
|
|
|
export type AuditEventName =
|
|
| "context.read"
|
|
| "context.search"
|
|
| "context.resolve"
|
|
| "context.bundle"
|
|
| "context.write"
|
|
| "context.supersede"
|
|
| "context.conflict"
|
|
| "context.denied"
|
|
| "decision.record";
|
|
|
|
export interface AuditEvent {
|
|
opencontext: string;
|
|
event: AuditEventName;
|
|
at: string;
|
|
actor: { type: "agent" | "human" | "role" | "service"; id: string; roles?: string[]; on_behalf_of?: string };
|
|
task?: string;
|
|
objects?: string[];
|
|
bundle?: { bundle_id?: string; digest?: string; object_count?: number };
|
|
outcome?: "allowed" | "denied" | "partial" | "error";
|
|
reason?: string;
|
|
namespace?: string;
|
|
extensions?: Record<string, unknown>;
|
|
}
|
|
|
|
/** Whether the manifest asks for this event to be recorded. */
|
|
export function isAuditEnabled(manifest: Manifest, event: AuditEventName): boolean {
|
|
const audit = manifest.audit;
|
|
if (!audit) return false;
|
|
|
|
switch (event) {
|
|
case "context.read":
|
|
case "context.search":
|
|
case "context.resolve":
|
|
case "context.bundle":
|
|
return audit.context_reads === true;
|
|
case "context.write":
|
|
case "context.supersede":
|
|
return audit.context_writes === true;
|
|
case "context.conflict":
|
|
return audit.conflicts === true;
|
|
case "context.denied":
|
|
return audit.context_reads === true || audit.context_writes === true;
|
|
case "decision.record":
|
|
return audit.decisions === true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export interface AuditContext {
|
|
manifest: Manifest;
|
|
dir: string;
|
|
scope?: EffectiveScope;
|
|
}
|
|
|
|
export function buildEvent(
|
|
event: AuditEventName,
|
|
ctx: AuditContext,
|
|
details: Omit<Partial<AuditEvent>, "event"> = {}
|
|
): AuditEvent {
|
|
const scope = ctx.scope;
|
|
return {
|
|
opencontext: SPEC_VERSION,
|
|
event,
|
|
at: new Date().toISOString(),
|
|
actor: details.actor ?? {
|
|
type: scope?.consumer.type ?? "human",
|
|
id: scope?.consumer.id ?? "local",
|
|
...(scope && scope.consumer.roles.length > 0 ? { roles: scope.consumer.roles } : {})
|
|
},
|
|
namespace: ctx.manifest.id,
|
|
...details
|
|
};
|
|
}
|
|
|
|
export function eventForBundle(ctx: AuditContext, bundle: ContextBundle, excludedCount: number): AuditEvent {
|
|
return buildEvent("context.resolve", ctx, {
|
|
task: bundle.task,
|
|
objects: bundle.objects.map((object) => object.id),
|
|
bundle: { bundle_id: bundle.bundle_id, digest: bundle.digest, object_count: bundle.objects.length },
|
|
// "partial" is the honest and normal outcome: a resolution that excluded
|
|
// nothing is rare, and recording it as "allowed" would hide the scoping.
|
|
outcome: bundle.objects.length === 0 ? "denied" : excludedCount > 0 ? "partial" : "allowed"
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Append an event to the configured sink.
|
|
*
|
|
* Only `file://` sinks are written by the reference implementation; anything
|
|
* else is returned for the caller to ship. A failure to write audit is
|
|
* reported, never swallowed — silently losing the audit trail is worse than a
|
|
* noisy command.
|
|
*/
|
|
export function recordEvent(ctx: AuditContext, event: AuditEvent): { written: boolean; sink?: string } {
|
|
const sink = ctx.manifest.audit?.sink;
|
|
if (!sink) return { written: false };
|
|
|
|
if (!sink.startsWith("file:") && !sink.startsWith("./") && !sink.startsWith("/")) {
|
|
return { written: false, sink };
|
|
}
|
|
|
|
const path = resolveInside(ctx.dir, sink);
|
|
mkdirSync(dirname(path), { recursive: true });
|
|
appendFileSync(path, `${JSON.stringify(event)}\n`, "utf8");
|
|
return { written: true, sink: path };
|
|
}
|