0xBUFF / retention

History replay

Rebuild from complete history, reconcile controls and verify prior identity when advancing.

Replay reconstructs a scope's review schedule from its history. Retention validates the policy and reconciles all events in the scope before computing practice schedules and optional candidate readiness.

During reconciliation, Retention validates and deduplicates events, checks the supplied clock and sorts distinct records by durable sequence. It indexes credited decisions, resolves their replacements and revocations, and applies event and scope deletion controls. It then sorts the surviving observations by effective time, root sequence and ASCII event ID.

Corrections preserve the original observation's effective time and sequence but replace its evidence, decision ID and version. The active replacement's event ID identifies the correction. A revocation leaves no active observation. All previous decision IDs remain reserved; a fork, reused ID, missing earlier target, wrong version or cross-concept target fails the entire operation.

Event deletions resolve against the complete history, including records that arrive after a deletion. A deletion of any credited root, correction or revocation suppresses its chain. Scope deletion uses the highest inclusive cutoff and compares it against each observation's root sequence. Deletions do not bypass correction-chain integrity checks. An unresolved deletion target is retained as a tombstone. Known cross-concept targets and deletion controls cannot be deleted by another event deletion.

Future observations are validated but excluded until their effective time. Future corrections or revocations are invalid. Deletions apply regardless of their timestamps.

Reconciliation produces active observations and controls internally. The public result carries applied event IDs in replay order, control IDs in sequence order, the scope, evaluation clock and watermark. The version-one watermark is the canonical JSON array of all distinct validated events in ascending sequence, including inactive, future and suppressed events and unresolved tombstones. Reordering input, adding exact duplicates or changing the evaluation time does not change the watermark. It identifies history content but cannot establish authenticity or completeness. Because it contains event data, protect it as you would the history itself.

The host supplies complete relevant history and owns authorization, durable sequence, physical erasure and stale-generation protection. The library neither persists data nor grants credit. See event validation for shape and input limits.

Public review operations

import { advanceReview, evaluateReadiness, explainReview, rebuildReview } from "@0xbuff/retention";

const first = rebuildReview(policy, scopeId, events, nowMs);
if (first.ok) {
  const explanation = explainReview(first.value);
  const next = advanceReview(policy, scopeId, first.value.identity, events, appendedEvents, nextMs);
}
const candidate = evaluateReadiness(policy, scopeId, conceptId, events, nowMs);

The example assumes the application supplies a policy, complete scoped events and explicit clocks. All operations return discriminated results. Rebuild returns an identity, ordered applied/control event IDs, lifecycle reasons, and concept projections in ASCII concept-ID order. Concepts named in the history but with no surviving observation have an unobserved practice projection; an empty history names no concepts. No eligibility policy means no candidate field, and the single-concept readiness query returns null. With eligibility configured, querying a concept without active credited observations returns unknown.

Practice scheduling uses all active observations. Candidate aggregation uses only reconciled credited evidence, independent of practice. The qualifying span runs from the first to last qualifying success, never the evaluation clock. A reset clears all streak counters and timestamps. A candidate is advisory. See candidate readiness.

Advance requires the exact prior identity and complete prior history, including suppressed and future events. If the policy changed, the identity is incompatible, the history is incomplete or the clock moved backward, rebuild explicitly. Appended records can contain observations with earlier effective times or exact duplicates. Retention validates and replays the combined history from scratch. A supplied identity is not authenticated evidence. Both raw input arrays, including duplicates, count toward the raw-entry and aggregate string limits. Advance checks those limits across the policy, scope, prior identity and raw events before replaying either history. A resource-limit failure returns RESOURCE_LIMIT_EXCEEDED, including when the prior prefix alone exceeds the limit. Retention reuses the validated events for both reference replays without counting them twice against the budget.

Explain accepts a successful rebuild or advance value and returns only fixed reason codes, bounded numeric arguments and concept identifiers. It does not interpret raw correction reason strings, revalidate evidence or rerun scheduling. It validates the closed result envelope, identity, event IDs, projection states, counts, timestamps and reason bounds. The canonical identity strings have separate derived size limits and are treated as opaque content identifiers here; only advance verifies them against replayed input. Explanation is not a projection authenticity check.

Advance with a complete prefix

// retention-docs: runnable
import { advanceReview, rebuildReview, explainReview } from "@0xbuff/retention";

const day = 86_400_000;
const policy = {
  schemaVersion: 1,
  algorithmVersion: "1",
  policyId: "deck",
  policyVersion: "v1",
  scheduling: {
    intervalsDays: [1, 3, 7],
    retryDays: 1,
    maintenanceDays: 30,
    supportAfterMisses: 2,
    longProbeAction: "restart",
    dateArithmetic: "elapsed_milliseconds",
  },
};
const event = {
  schemaVersion: 1,
  kind: "practice_observation",
  scopeId: "deck",
  conceptId: "binary-search",
  eventId: "review-1",
  sequence: 0,
  atMs: 0,
  sessionId: "session-1",
  outcome: "pass",
};
function value(result) {
  if (!result.ok) throw new Error(JSON.stringify(result.errors));
  return result.value;
}

const prior = [event];
const first = value(rebuildReview(policy, "deck", prior, day));
const appended = [{ ...event, eventId: "review-2", sequence: 1, atMs: day }];
const next = value(advanceReview(policy, "deck", first.identity, prior, appended, day));
const explanation = value(explainReview(next));
console.log(next.concepts[0].stageIndex, explanation.concepts[0].practiceReasons.at(-1).code);

Expected output:

1 DUE_PASS

Use due ranking to select recommendations from a current review result without replaying its history.

On this page