mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-10-02 12:54:03 +00:00
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:
parent
1bb7ba6e60
commit
3ab8a4b38b
212 changed files with 17249 additions and 7 deletions
121
packages/opencontext/src/relevance.ts
Normal file
121
packages/opencontext/src/relevance.ts
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
/**
|
||||
* Task relevance ranking.
|
||||
*
|
||||
* Ranking decides *order* and, when a limit is set, what gets trimmed. It never
|
||||
* decides access — by the time anything reaches this module the candidates have
|
||||
* already been authorized, so a high-scoring object the consumer may not read
|
||||
* does not exist here at all.
|
||||
*
|
||||
* The scorer is lexical on purpose. Semantic search is a legitimate adapter
|
||||
* concern, but requiring an embedding model to resolve context would make
|
||||
* resolution non-deterministic and would put a model vendor in the path of a
|
||||
* specification whose whole point is that agents are replaceable.
|
||||
*/
|
||||
|
||||
import type { ContextObject, Layer } from "./types.js";
|
||||
|
||||
/**
|
||||
* Layers that stay in the bundle even when the task does not mention them.
|
||||
*
|
||||
* Mission and identity are what an agent needs in order to behave like it works
|
||||
* here rather than anywhere; dropping them because a ticket did not name them
|
||||
* is how a replacement agent loses the organization's voice.
|
||||
*/
|
||||
const LAYER_FLOOR: Partial<Record<Layer, number>> = {
|
||||
L0: 10,
|
||||
L1: 6
|
||||
};
|
||||
|
||||
const STOP_WORDS = new Set([
|
||||
"the", "a", "an", "and", "or", "for", "to", "of", "in", "on", "at", "by", "with", "from",
|
||||
"is", "are", "was", "were", "be", "been", "it", "its", "this", "that", "these", "those",
|
||||
"please", "can", "you", "we", "i", "how", "what", "when", "why", "do", "does", "did"
|
||||
]);
|
||||
|
||||
export function tokenize(text: string): string[] {
|
||||
return text
|
||||
.toLowerCase()
|
||||
.split(/[^a-z0-9]+/)
|
||||
.filter((token) => token.length > 1 && !STOP_WORDS.has(token));
|
||||
}
|
||||
|
||||
export interface RelevanceScore {
|
||||
score: number;
|
||||
/** Which fields matched, for `--explain`. */
|
||||
matched: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Score one object against a task.
|
||||
*
|
||||
* With no task every object scores equally and ordering falls back to layer and
|
||||
* authority, which keeps `resolve` with no `--task` fully deterministic.
|
||||
*/
|
||||
export function scoreRelevance(object: ContextObject, taskTokens: string[]): RelevanceScore {
|
||||
const floor = LAYER_FLOOR[object.layer ?? ("L2" as Layer)] ?? 0;
|
||||
if (taskTokens.length === 0) return { score: floor, matched: [] };
|
||||
|
||||
let score = floor;
|
||||
const matched: string[] = [];
|
||||
|
||||
const add = (field: string, weight: number, haystack: string | undefined): void => {
|
||||
if (!haystack) return;
|
||||
const text = haystack.toLowerCase();
|
||||
let hits = 0;
|
||||
for (const token of taskTokens) {
|
||||
if (text.includes(token)) hits += 1;
|
||||
}
|
||||
if (hits > 0) {
|
||||
score += hits * weight;
|
||||
matched.push(field);
|
||||
}
|
||||
};
|
||||
|
||||
// `applies_to` is the strongest signal: it is the author saying, explicitly,
|
||||
// what this context is for.
|
||||
add("applies_to", 6, object.applies_to?.join(" "));
|
||||
add("tags", 4, object.tags?.join(" "));
|
||||
add("title", 4, object.title);
|
||||
add("id", 3, object.id.replace(/[._-]/g, " "));
|
||||
add("summary", 2, object.summary);
|
||||
add("type", 2, object.type);
|
||||
|
||||
// Content is weighted lowest and capped: a long document should not outrank a
|
||||
// precisely-titled one just by containing more words.
|
||||
const content = typeof object.content === "string" ? object.content : undefined;
|
||||
if (content) {
|
||||
const text = content.slice(0, 4000).toLowerCase();
|
||||
let hits = 0;
|
||||
for (const token of taskTokens) {
|
||||
if (text.includes(token)) hits += 1;
|
||||
}
|
||||
if (hits > 0) {
|
||||
score += Math.min(hits, 4);
|
||||
matched.push("content");
|
||||
}
|
||||
}
|
||||
|
||||
return { score, matched };
|
||||
}
|
||||
|
||||
/**
|
||||
* Deterministic bundle ordering: layer, then authority, then id.
|
||||
*
|
||||
* Relevance decides what is *kept*; this decides what a reader sees first.
|
||||
* Ordering by layer means a bundle always opens with mission and closes with
|
||||
* transient operational state, whatever the task was.
|
||||
*/
|
||||
export function compareForBundle(
|
||||
a: ContextObject,
|
||||
b: ContextObject,
|
||||
authorityRankOf: (object: ContextObject) => number
|
||||
): number {
|
||||
const layerA = a.layer ?? "L2";
|
||||
const layerB = b.layer ?? "L2";
|
||||
if (layerA !== layerB) return layerA < layerB ? -1 : 1;
|
||||
|
||||
const authority = authorityRankOf(a) - authorityRankOf(b);
|
||||
if (authority !== 0) return authority;
|
||||
|
||||
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue