// Copyright 2024-2026 Tymofii Pidlisnyi. Apache-2.0 license. See LICENSE.
// ══════════════════════════════════════════════════════════════════
// memory_provenance signal_type (v0.1): TypeScript types
// ══════════════════════════════════════════════════════════════════
// Substrate for matrix v2 candidate C-II-2 (Memory provenance
// attestation envelope). OWASP ASI06 names a signed provenance envelope
// for memory entries as a missing primitive; MINJA and MemoryGraft
// research motivate it.
//
// What the primitive attests: where a memory entry came from, when it
// was ingested, and under which transformation. It does NOT detect
// poisoning. The truth of the memory content and the integrity of the
// reduction_map_ref are downstream consumer responsibilities.
//
// source_ref is the sha256 hex of the ORIGINAL source content before
// transformation. memory_ref is the sha256 hex of the memory entry
// AFTER transformation. reduction_map_ref names the transformation that
// was applied; future versions will carry a full (F, Omega, D) reduction
// map structure in this slot.
// ══════════════════════════════════════════════════════════════════
const HEX_KEY = /^[0-9a-f]{64}$/;
const HEX_SIGNATURE = /^[0-9a-f]{128}$/;
const HEX_DIGEST = /^[0-9a-f]{64}$/;
// ISO 8601 date-time with required time portion. Accepts second
// precision, optional fractional seconds, and either 'Z' or a numeric
// offset. The regex is the gate; Date.parse is the second gate so that
// invalid days like 2026-02-30 still fail.
const ISO_8601 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/;
/**
 * Returns true when value is a syntactically well-formed ISO 8601
 * timestamp string. Checks the lexical form and that Date.parse yields
 * a finite value, which together reject malformed dates such as
 * impossible months or days.
 */
export function isIso8601(value) {
    if (typeof value !== 'string')
        return false;
    if (!ISO_8601.test(value))
        return false;
    const ms = Date.parse(value);
    return Number.isFinite(ms);
}
/**
 * Validates the source sub-object shape. Does NOT check that source_ref
 * matches any actual content; the consumer carries that.
 */
export function isMemoryProvenanceSource(value) {
    if (typeof value !== 'object' || value === null)
        return false;
    const s = value;
    if (typeof s.issuer_id !== 'string' || !HEX_KEY.test(s.issuer_id))
        return false;
    if (!isIso8601(s.issued_at))
        return false;
    if (typeof s.source_ref !== 'string' || !HEX_DIGEST.test(s.source_ref))
        return false;
    if (typeof s.reduction_map_ref !== 'string' || s.reduction_map_ref.length === 0)
        return false;
    return true;
}
/**
 * Runtime type guard. Returns true when value is structurally a signed
 * memory_provenance envelope. Does NOT verify the signature; use
 * verifyMemoryProvenance for that.
 *
 * Invariants checked:
 *   - signal_type literal matches.
 *   - memory_ref is 64 lowercase hex chars.
 *   - source has all four required fields with valid shapes.
 *   - ingester_id is 64 lowercase hex chars.
 *   - ingested_at is a valid ISO 8601 timestamp string.
 *   - signature is 128 lowercase hex chars.
 */
export function isMemoryProvenance(value) {
    if (typeof value !== 'object' || value === null)
        return false;
    const v = value;
    if (v.signal_type !== 'memory_provenance')
        return false;
    if (typeof v.memory_ref !== 'string' || !HEX_DIGEST.test(v.memory_ref))
        return false;
    if (!isMemoryProvenanceSource(v.source))
        return false;
    if (typeof v.ingester_id !== 'string' || !HEX_KEY.test(v.ingester_id))
        return false;
    if (!isIso8601(v.ingested_at))
        return false;
    if (typeof v.signature !== 'string' || !HEX_SIGNATURE.test(v.signature))
        return false;
    return true;
}
//# sourceMappingURL=types.js.map