7212 lines · 241.7 KB · Zig
RawDownload
const std = @import("std");const builtin = @import("builtin");const debug_trace = @import("../shared/debug_trace.zig");const io_mod = @import("../shared/io.zig");const profile_paths = @import("../shared/profile_paths.zig");const session = @import("session.zig");const session_child_store = @import("session_child_store.zig");const session_codec = @import("session_codec.zig");const types = @import("../shared/types.zig");const session_event = @import("session_event.zig");const session_layout = @import("session_layout.zig");const session_projection = @import("session_projection.zig");const session_replay = @import("session_replay.zig");const session_display_metadata = @import("session_display_metadata.zig");const session_resume_view = @import("session_resume_view.zig");const session_usage = @import("session_usage.zig");const session_usage_sidecar = @import("session_usage_sidecar.zig"); const Allocator = std.mem.Allocator;const Identifier = session_event.Identifier;const private_dir_permissions = std.Io.File.Permissions.fromMode(0o700);const private_file_permissions = std.Io.File.Permissions.fromMode(0o600);const lock_deadline_ms: u64 = 2000;const authority_max_bytes: usize = 16 * 1024;const authority_intent_max_bytes: usize = 32 * 1024;const watermark_max_bytes: usize = 16 * 1024;const publication_intent_max_bytes: usize = 32 * 1024;const events_file = "events.jsonl";const authority_file = "authority.json";const authority_intent_file = "authority.pending.json";const publication_intent_file = "commit.pending.json";const manifest_file = "session.json";const checkpoint_file = "checkpoint.json";const session_lock_file = "session.lock";const commit_lock_file = "commit.lock"; pub const Boundary = enum {    after_event_append,    after_event_sync,    after_commit_intent_sync,    after_watermark_rename,    after_target_namespace_sync,    before_usage_sidecar_write,    after_commit_intent_remove,    after_authority_intent_sync,    after_authority_marker_rename,    after_authority_namespace_sync,    after_authority_intent_remove,    after_compaction_temp_sync,    after_compaction_watermark_sync,    after_compaction_intent_sync,    after_compaction_log_rename,    after_compaction_namespace_sync,    after_compaction_live_confirmation,    after_compaction_intent_remove,    after_latest_cache_pending,    before_latest_cache_ready,    before_recovery_proposed_validation,    before_recovery_prior_validation,    after_recovery_latest_snapshot,    before_recovery_latest_publication,    latest_barrier_contended,    latest_barrier_completed,}; pub const LockKind = enum {    session,    commit,}; pub const TestControls = struct {    context: ?*anyopaque = null,    boundary_fn: ?*const fn (?*anyopaque, Boundary) anyerror!void = null,    lock_fn: ?*const fn (?*anyopaque, LockKind) void = null,     pub fn boundary(self: TestControls, point: Boundary) !void {        if (self.boundary_fn) |callback| try callback(self.context, point);    }     fn lock(self: TestControls, kind: LockKind) void {        if (self.lock_fn) |callback| callback(self.context, kind);    }}; pub const Options = struct {    test_controls: TestControls = .{},    session_lock_deadline_ms: u64 = lock_deadline_ms,    commit_lock_deadline_ms: u64 = lock_deadline_ms,    checkpoint_interval: u64 = 32,    compaction_frame_threshold: u64 = 4096,    compaction_byte_threshold: u64 = 128 * 1024 * 1024,    resolve_authority_intent: bool = true,}; pub const OpenMode = enum {    read_only,    writable,}; pub const CommitPosition = session_replay.CommitPosition; test {    _ = session_replay;} pub const FailedTailDisposition = enum {    retry_expected_tail,    rollback_before_adapter_continue,}; pub const FailedTailKind = union(enum) {    event: Identifier,    state_replacement: struct {        replacement_id: Identifier,        final_event_id: Identifier,    },}; pub const FailedTail = struct {    prior: CommitPosition,    proposed: CommitPosition,    kind: FailedTailKind,    bytes: []u8,    sha256: session_projection.Digest,     pub fn deinit(self: *FailedTail, alloc: Allocator) void {        alloc.free(self.bytes);        self.* = undefined;    }}; pub const PublicationKind = enum {    watermark_advance,    log_generation_replace,}; pub const ProjectionStatus = enum {    current,    stale,}; const ReplaySource = enum {    checkpoint,    event_log,}; const OpenState = struct {    state: session_codec.DurableSessionState,    generation_base_seq: u64,    generation_base_bytes: u64,    checkpoint_seq: ?u64,    checkpoint_sha256: ?session_projection.Digest,    projection_status: ProjectionStatus,    source: ReplaySource,    tail_bytes: u64,}; const CheckpointReplay = struct {    state: session_codec.DurableSessionState,    through_event_log_bytes: u64,}; const OpenManifest = struct {    manifest: session_projection.Manifest,    event_file_matches: bool,}; pub const CommitWatermarkStatus = enum {    valid,    missing,    invalid,    mismatched,}; pub const CommitWatermarkInspection = struct {    status: CommitWatermarkStatus,    position: ?CommitPosition = null,}; pub const CleanupMode = enum {    report_only,    delete,}; pub const CleanupReport = struct {    removed: usize = 0,    report_only: usize = 0,    ignored: usize = 0,}; const AuthoritySource = enum {    native_create,    legacy_migration,}; const AuthorityKind = enum {    session_create,    legacy_to_v3,}; const AuthorityMarker = struct {    session_id: []u8,    authority_id: Identifier,    source: AuthoritySource,     fn deinit(self: *AuthorityMarker, alloc: Allocator) void {        alloc.free(self.session_id);        self.* = undefined;    }}; const LegacyFingerprint = struct {    storage_format: []u8,    primary_bytes: u64,    primary_sha256: session_projection.Digest,     fn deinit(self: *LegacyFingerprint, alloc: Allocator) void {        alloc.free(self.storage_format);        self.* = undefined;    }}; const AuthorityIntent = struct {    session_id: []u8,    operation_id: Identifier,    kind: AuthorityKind,    authority_id: Identifier,    prior: ?LegacyFingerprint,    proposed: CommitPosition,     fn deinit(self: *AuthorityIntent, alloc: Allocator) void {        alloc.free(self.session_id);        if (self.prior) |*prior| prior.deinit(alloc);        self.* = undefined;    }}; const PublicationIntent = struct {    session_id: []u8,    operation_id: Identifier,    kind: PublicationKind,    prior: CommitPosition,    proposed: CommitPosition,     fn deinit(self: *PublicationIntent, alloc: Allocator) void {        alloc.free(self.session_id);        self.* = undefined;    }}; const DecodedWatermark = struct {    session_id: []u8,    position: CommitPosition,     fn deinit(self: *DecodedWatermark, alloc: Allocator) void {        alloc.free(self.session_id);        self.* = undefined;    }}; pub const WritableSessionDir = struct {    dir: io_mod.VerifiedDir,    writer_lock: ?io_mod.TimedAdvisoryLock,    session_id: []u8,     pub fn deinit(self: *WritableSessionDir, alloc: Allocator) void {        if (self.writer_lock) |*lock| lock.release();        self.dir.close();        alloc.free(self.session_id);        self.* = undefined;    }     pub fn isParked(self: *const WritableSessionDir) bool {        return self.writer_lock == null;    }     /// Release `session.lock` while keeping the session directory open for a    /// later `unpark` in the same process (idle job-control suspend).    pub fn park(self: *WritableSessionDir) void {        const lock = &(self.writer_lock orelse return);        lock.release();        self.writer_lock = null;    }     /// Reacquire `session.lock` after `park`. Fails with `SessionBusy` when    /// another process already owns the writer lock.    pub fn unpark(self: *WritableSessionDir) !void {        if (self.writer_lock != null) return;        self.writer_lock = acquireLock(&self.dir, session_lock_file, true) catch |err| {            return mapSessionLockError(err);        };    }     pub fn eventLogLengthForTest(self: *WritableSessionDir) !u64 {        var file = try openManagedFile(&self.dir, events_file, .read_only);        defer file.close(io_mod.getIo());        return file.length(io_mod.getIo());    }     fn entryExistsForTest(self: *WritableSessionDir, name: []const u8) !bool {        return entryExists(&self.dir, name);    }}; pub const ResumeViewAdmission = struct {    writable: ?WritableSessionDir,    view: session_resume_view.LoadOutcome,     pub fn deinit(self: *ResumeViewAdmission, alloc: Allocator) void {        if (self.writable) |*writable| writable.deinit(alloc);        self.view.deinit(alloc);        self.* = undefined;    }     pub fn sessionId(self: *const ResumeViewAdmission) []const u8 {        return self.writable.?.session_id;    }     pub fn resumeForWrite(        self: *ResumeViewAdmission,        alloc: Allocator,        options: Options,    ) !LoadedWritableSession {        var writable = self.writable orelse return error.SessionResumeAdmissionConsumed;        self.writable = null;        return openWritableSession(alloc, &writable, options) catch |err| {            writable.deinit(alloc);            return err;        };    }}; pub const ReadBoundary = struct {    log_file: std.Io.File,    position: CommitPosition,    usage_sidecar: session_usage_sidecar.Captured,    alloc: Allocator,     pub fn deinit(self: *ReadBoundary) void {        self.log_file.close(io_mod.getIo());        self.usage_sidecar.deinit(self.alloc);        self.* = undefined;    }}; /// Loads the exact manifest boundary while deliberately ignoring a corrupt/// commit watermark. The source directory remains unchanged.pub fn recoverManifestBoundary(    alloc: Allocator,    writable: *WritableSessionDir,    options: Options,) !session_codec.DurableSessionState {    if (writable.writer_lock == null) return error.SessionBusy;    try requireIntentAbsent(alloc, &writable.dir, authority_intent_file);    options.test_controls.lock(.commit);    var commit_lock = acquireLockWithDeadline(        &writable.dir,        commit_lock_file,        true,        options.commit_lock_deadline_ms,    ) catch |err| return mapCommitLockError(err);    defer commit_lock.release();    try requireIntentAbsent(alloc, &writable.dir, authority_intent_file);    try requireIntentAbsent(alloc, &writable.dir, publication_intent_file);     var authority = try loadAuthority(        alloc,        &writable.dir,        writable.session_id,    );    defer authority.deinit(alloc);    const watermark = try inspectCommitWatermark(        alloc,        &writable.dir,        writable.session_id,    );    if (watermark.status == .valid) return error.SessionRecoveryNotNeeded;     var manifest = try loadManifest(alloc, &writable.dir);    defer manifest.deinit(alloc);    if (!std.mem.eql(u8, manifest.id, writable.session_id) or        !std.mem.eql(u8, &manifest.authority_id, &authority.authority_id))    {        return error.InvalidSessionFormat;    }     var event_log = try openManagedFile(        &writable.dir,        events_file,        .read_only,    );    defer event_log.close(io_mod.getIo());    const stat = try eventStat(event_log, manifest.event_log_bytes);    if (session_projection.isManifestStale(manifest, stat)) {        return error.InvalidSessionFormat;    }     var replayed = try session_replay.replayExactBoundary(        alloc,        event_log,        manifest.log_generation,        manifest.last_event_seq,        manifest.event_log_bytes,    );    errdefer replayed.deinit(alloc);    if (!session_projection.stateMatchesManifest(replayed, manifest)) {        return error.InvalidSessionFormat;    }    return replayed;} pub const CommitLifecycle = struct {    context: ?*anyopaque = null,    prepare_fn: ?*const fn (        ?*anyopaque,        Allocator,        []const u8,        []const u8,        []const u8,        u64,    ) anyerror!void = null,    publish_fn: ?*const fn (        ?*anyopaque,        Allocator,        session_codec.DurableSessionState,    ) anyerror!void = null,    abort_fn: ?*const fn (?*anyopaque) void = null,    deinit_fn: ?*const fn (?*anyopaque, Allocator) void = null,     pub fn prepare(        self: *CommitLifecycle,        alloc: Allocator,        session_id: []const u8,        previous_workspace_root: []const u8,        next_workspace_root: []const u8,        commit_lock_deadline_ms: u64,    ) !void {        if (self.prepare_fn) |callback| {            try callback(                self.context,                alloc,                session_id,                previous_workspace_root,                next_workspace_root,                commit_lock_deadline_ms,            );        }    }     pub fn publish(        self: *CommitLifecycle,        alloc: Allocator,        state: session_codec.DurableSessionState,    ) !void {        if (self.publish_fn) |callback| try callback(self.context, alloc, state);    }     pub fn abort(self: *CommitLifecycle) void {        if (self.abort_fn) |callback| callback(self.context);    }     pub fn deinit(self: *CommitLifecycle, alloc: Allocator) void {        self.abort();        if (self.deinit_fn) |callback| callback(self.context, alloc);        self.* = undefined;    }}; pub const LoadedWritableSession = struct {    pub const ExternalPromptOrigin = enum {        root,        persistent_child,    };     active_id: []u8,    state: session_codec.DurableSessionState,    log: WritableSessionDir,    freshly_started: bool = false,    state_replacement_pending: bool = false,    child_capability: ?*session_child_store.SessionChildCapability = null,    commit_lifecycle: ?CommitLifecycle = null,    position: CommitPosition,    authority_id: Identifier,    generation_base_seq: u64,    generation_base_bytes: u64,    checkpoint_seq: ?u64 = null,    checkpoint_sha256: ?session_projection.Digest = null,    projection_status: ProjectionStatus = .current,    namespace_confirmation_required: bool = false,    degraded_tail: ?FailedTail = null,    compaction_warning_active: bool = false,    migration_source_schema_version: ?u8 = null,    migration_source_bytes: ?u64 = null,    usage_sidecar_reseal_pending: bool = false,    resume_view_stale: bool = false,    /// Runtime-only provenance installed by subagent resume admission. These    /// fields are never written into the session event log.    external_prompt_origin: ExternalPromptOrigin = .root,    external_root_user_messages: [][]u8 = &.{},    external_root_user_evidence_complete: bool = false,

Lines 1–500 of 7212.