logicsrc/packages/opencontext/src/adapters/file.ts
Anthony Ettinger ec3ed64f20 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>
2026-08-09 18:22:42 +00:00

103 lines
3.7 KiB
TypeScript

/**
* `file://` adapter, and the resolver for bare relative paths.
*
* This adapter is the one that runs on every local project, so it is also the
* one that has to be careful: a context repository may be authored by someone
* who is not the person running the resolver, and `../../../.ssh/id_rsa` is a
* perfectly ordinary-looking string in a YAML file. Every path is resolved and
* then checked to be inside the manifest directory before anything is read.
*/
import { readFile, stat } from "node:fs/promises";
import { isAbsolute, relative, resolve, sep } from "node:path";
import { fileURLToPath } from "node:url";
import type { Adapter, AdapterContext, AdapterResult } from "../types.js";
import { sha256Uri } from "../digest.js";
export class PathTraversalError extends Error {
readonly uri: string;
constructor(uri: string, resolved: string, root: string) {
super(
`Refusing to read "${uri}": it resolves to ${resolved}, which is outside the context root ${root}. ` +
`Context sources must stay inside the directory containing opencontext.yaml.`
);
this.name = "PathTraversalError";
this.uri = uri;
}
}
/**
* Turn a `file://` URI or a relative path into an absolute path inside `root`.
*
* Throws rather than clamping: silently rewriting an escaping path would hide
* a misconfigured or hostile context repository.
*/
export function resolveInside(root: string, target: string): string {
const raw = target.startsWith("file:") ? fileUriToPath(target) : target;
// An absolute path is allowed only when it is already inside the root, so an
// authored `/etc/passwd` fails the same way `../../etc/passwd` does.
const absolute = isAbsolute(raw) ? resolve(raw) : resolve(root, raw);
const rootResolved = resolve(root);
const rel = relative(rootResolved, absolute);
if (rel === "") return absolute;
if (rel.startsWith("..") || rel.startsWith(`..${sep}`) || isAbsolute(rel)) {
throw new PathTraversalError(target, absolute, rootResolved);
}
return absolute;
}
function fileUriToPath(uri: string): string {
// `file://./context/mission.md` is not a legal file URI but is what people
// write, so treat a non-absolute file: URI as a relative path.
if (/^file:\/\/\/|^file:\/\/[a-zA-Z]/.test(uri) && !uri.startsWith("file://.")) {
try {
return fileURLToPath(uri);
} catch {
// fall through to the lenient reading below
}
}
return uri.replace(/^file:\/\//, "").replace(/^file:/, "");
}
export const fileAdapter: Adapter = {
name: "file",
schemes: ["file"],
remote: false,
async load(uri: string, ctx: AdapterContext): Promise<AdapterResult> {
const path = resolveInside(ctx.dir, uri);
let stats;
try {
stats = await stat(path);
} catch {
throw new Error(`Source not found: ${uri} (looked in ${path})`);
}
if (stats.isDirectory()) {
throw new Error(`Source ${uri} is a directory. Point content_uri at a file, or use a collection glob.`);
}
const content = await readFile(path, "utf8");
return {
content,
contentType: guessContentType(path),
digest: sha256Uri(content),
retrievedAt: stats.mtime.toISOString(),
// Local files are inside the trust boundary — they are reviewed the same
// way code is, through the repository they live in.
trust: "trusted"
};
}
};
export function guessContentType(path: string): string {
const lower = path.toLowerCase();
if (lower.endsWith(".json")) return "application/json";
if (lower.endsWith(".yaml") || lower.endsWith(".yml")) return "application/yaml";
if (lower.endsWith(".csv")) return "text/csv";
if (lower.endsWith(".txt")) return "text/plain";
if (lower.endsWith(".html") || lower.endsWith(".htm")) return "text/html";
return "text/markdown";
}