Skip to content

Add core semantic replay foundation#84

Merged
ProfRandom92 merged 1 commit into
mainfrom
codex/implement-core-foundation-for-comptextv7
May 15, 2026
Merged

Add core semantic replay foundation#84
ProfRandom92 merged 1 commit into
mainfrom
codex/implement-core-foundation-for-comptextv7

Conversation

@ProfRandom92
Copy link
Copy Markdown
Owner

Motivation

  • Provide a small, deterministic core foundation for token-efficient, replayable, and observable AI execution without changing UI or external integrations.
  • Introduce a compact Semantic Reference Protocol to avoid injecting raw context repeatedly and enable deterministic replay manifests and prompts.
  • Add an append-only execution event log and replay snapshot models to record and compare runs at reference granularity.
  • Add a deterministic, rubric-driven decision-quality evaluator to score decisions from replay metadata rather than LLM judgment.

Description

  • Implemented Semantic Reference Protocol primitives: SemanticReference, ContextManifest, SemanticReferenceRegistry, ReferenceResolver typing, TokenBudgetManager, ContextManifestBuilder, and CompactPromptBuilder that hydrate only on explicit request. (dashboard/app/src/core/foundation/semanticReferences.ts)
  • Implemented append-only execution event log primitives and validations: ExecutionEvent, ExecutionEventLog, InMemoryExecutionEventStore, appendExecutionEvent, getExecutionTimeline, summarizeExecutionEvents, and compact-payload size enforcement. (dashboard/app/src/core/foundation/executionEventLog.ts)
  • Added deterministic replay snapshot and comparison models: ReplaySnapshot, ReplayBranch, ReplayTimeline, ReplayComparator, createReplaySnapshot, branchFromStep, and compareReplayRuns. (dashboard/app/src/core/foundation/replaySnapshot.ts)
  • Added a deterministic Decision Quality Engine with QualityRubric, DecisionQualityEngine, and QualityEvalRun that scores validity, specificity, correctness, traceability, rollbackSafety, and tokenEfficiency using a fixed rubric. (dashboard/app/src/core/foundation/decisionQuality.ts)
  • Shared utilities and small sample wiring: stable stringify/hash helpers, JSON assertions, and a coreFoundationSample exposing static sample data for optional lightweight wiring. (dashboard/app/src/core/foundation/shared.ts, sampleData.ts)
  • Added concise docs describing purpose, data shapes, token-efficiency rationale, examples, and limitations for each core area. (docs/*.md)
  • Added focused tests that compile the TS modules and exercise deduplication, token-budget selection/compact prompt, append-only event ordering/summary, replay snapshot/branch/comparison, and decision quality scoring. (tests/test_core_foundation_ts.py)

Testing

  • npm run build (repo root): unavailable, failed with ENOENT because /workspace/Comptextv7/package.json is missing (error: "Could not read package.json: ENOENT: no such file or directory, open '/workspace/Comptextv7/package.json'").
  • npm run lint and npm test -- --runInBand (repo root): unavailable for the same ENOENT package.json error.
  • npm run typecheck (dashboard/app): executed and completed successfully (tsc -b succeeded).
  • npm run build (dashboard/app): executed and completed successfully (Vite build + tsc succeeded; production build produced dist/ assets).
  • pytest tests/test_core_foundation_ts.py -q: passed (4 tests covering the new TypeScript foundation modules).
  • pytest -q: full project test suite passed (51 tests).

Codex Task

@vercel
Copy link
Copy Markdown

vercel Bot commented May 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
comptextv7 Ready Ready Preview, Comment May 15, 2026 6:13pm

@netlify
Copy link
Copy Markdown

netlify Bot commented May 15, 2026

Deploy Preview for comptext-v7 canceled.

Name Link
🔨 Latest commit c4474f6
🔍 Latest deploy log https://app.netlify.com/projects/comptext-v7/deploys/6a0762539d2bae000811829e

@ProfRandom92 ProfRandom92 merged commit b85982a into main May 15, 2026
8 of 10 checks passed
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a core foundation for token-efficient execution replay, featuring a semantic reference protocol, an append-only event log, and a rubric-based decision quality engine. Feedback focuses on improving the robustness of the stableStringify utility to handle edge cases like undefined and toJSON methods, optimizing the performance of reference deduplication from O(N^2) to O(N), and addressing the computational overhead of deep-cloning events during log retrieval.

Comment on lines +4 to +15
export function stableStringify(value: unknown): string {
if (value === null || typeof value !== 'object') {
return JSON.stringify(value);
}
if (Array.isArray(value)) {
return `[${value.map((entry) => stableStringify(entry)).join(',')}]`;
}
const entries = Object.entries(value as Record<string, unknown>)
.filter(([, entry]) => entry !== undefined)
.sort(([left], [right]) => left.localeCompare(right));
return `{${entries.map(([key, entry]) => `${JSON.stringify(key)}:${stableStringify(entry)}`).join(',')}}`;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The stableStringify implementation has several issues that affect its robustness and correctness:

  1. Type Safety & Runtime Error: JSON.stringify(undefined) returns undefined (the value), not a string. This violates the string return type and causes stableHash to throw an error when accessing .length on line 20.
  2. Array Consistency: It doesn't handle undefined values inside arrays correctly. Standard JSON serialization converts undefined in arrays to null, but this implementation would produce an empty string segment (e.g., [undefined] becomes [] instead of [null]).
  3. Missing toJSON Support: It doesn't handle objects with toJSON methods (like Date), resulting in empty objects {} instead of the expected serialized string.
  4. Function/Symbol Handling: It should explicitly handle functions and symbols to match JSON.stringify behavior (omitted in objects, null in arrays).
export function stableStringify(value: unknown): string {
  if (value === undefined || typeof value === 'function' || typeof value === 'symbol') {
    return 'null';
  }
  if (value === null || typeof value !== 'object') {
    return JSON.stringify(value);
  }
  if (typeof (value as any).toJSON === 'function') {
    return stableStringify((value as any).toJSON());
  }
  if (Array.isArray(value)) {
    return `[${value.map((entry) => stableStringify(entry)).join(',')}]`;
  }
  const entries = Object.entries(value as Record<string, unknown>)
    .filter(([, entry]) => entry !== undefined && typeof entry !== 'function' && typeof entry !== 'symbol')
    .sort(([left], [right]) => left.localeCompare(right));
  return `{${entries.map(([key, entry]) => `${JSON.stringify(key)}:${stableStringify(entry)}`).join(',')}}`;
}

Comment on lines +135 to +145
const deduped = new Map<string, SemanticReference>();
for (const reference of references) {
const existing = deduped.get(reference.uri) ?? deduped.get(reference.hash);
if (!existing) {
deduped.set(reference.uri, reference);
deduped.set(reference.hash, reference);
}
}
const candidates = uniqueStable(Array.from(deduped.values()).map((reference) => reference.id))
.map((id) => Array.from(deduped.values()).find((reference) => reference.id === id)!)
.sort((left, right) => right.relevanceScore - left.relevanceScore || left.tokenEstimate - right.tokenEstimate || left.id.localeCompare(right.id));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current deduplication and candidate selection logic is inefficient. Specifically, lines 143-144 perform an O(N^2) operation because they iterate over unique IDs and then perform a find on an array that contains duplicates for every entry in the deduped map. This can be simplified to O(N) (plus the sort) by using a more direct approach to track seen URIs and Hashes.

    const uniqueRefs = new Map<string, SemanticReference>();
    const seenUris = new Set<string>();
    const seenHashes = new Set<string>();

    for (const ref of references) {
      if (!seenUris.has(ref.uri) && !seenHashes.has(ref.hash)) {
        uniqueRefs.set(ref.id, ref);
        seenUris.add(ref.uri);
        seenHashes.add(ref.hash);
      }
    }

    const candidates = Array.from(uniqueRefs.values()).sort(
      (left, right) =>
        right.relevanceScore - left.relevanceScore ||
        left.tokenEstimate - right.tokenEstimate ||
        left.id.localeCompare(right.id)
    );

list(executionId?: string): ExecutionEvent[] {
return this.events
.filter((event) => executionId === undefined || event.executionId === executionId)
.map(cloneEvent);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using JSON.parse(JSON.stringify(...)) for deep cloning on every list() call is computationally expensive and creates significant garbage collection pressure, especially as the event log grows. Since the events are already shallow-frozen upon being appended, consider using structuredClone() for better performance if deep cloning is required, or evaluate if the immutability guarantees provided by Object.freeze are sufficient to avoid cloning on every read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant