1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0
//! The [`Condenser`] contract for durable, replayable context condensation (spec §8).
//!
//! Condensation is distinct from live in-memory compaction (owned by `zeph-context`): it
//! operates at the event-log level and is recorded as a [`crate::event::SessionEvent::Condensation`]
//! event so replay can fold the same summary deterministically. This module defines the trait
//! contract and the [`INV-SP-4`](validate_non_overlap) non-overlap guard; see
//! [`crate::llm_condenser::LlmCondenser`] for the default implementation.
use AnchoredSummary;
use crateSessionError;
use crateSessionEventEnvelope;
use crateReconstructedState;
/// The outcome of one condensation pass: the `seq` range it replaced and the resulting summary.
/// Computes whether and how to durably condense a session's event log.
///
/// Implementors MUST respect INV-SP-4 (spec §8.3): the range returned by [`Self::condense`] must
/// start strictly after the caller's `last_condensed_seq` — see [`validate_non_overlap`].
/// Enforce INV-SP-4: a proposed `(lo, hi)` range must start strictly after `last_condensed_seq`.
///
/// Callers (the `Condenser` implementation and `zeph-agent-persistence`'s live-compaction hook)
/// must call this immediately before emitting a `Condensation`/`Compaction` event, using the
/// `last_condensed_seq` read from `acp_sessions` at the start of the computation — not a
/// stale/cached value — to close the read-then-write race the invariant depends on.
///
/// `last_condensed_seq == 0` is treated as the sentinel "nothing has ever been condensed" rather
/// than "seq 0 was already condensed" — event logs are 0-indexed (a session's first event is
/// `seq == 0`, matching `acp_sessions.last_condensed_seq`'s migration-106 `DEFAULT 0`), so
/// without this carve-out the very first condensation of every session — which necessarily wants
/// to start at `lo == 0` — would be permanently rejected as "overlapping" the default. Verified
/// empirically: `LlmCondenser::condense`'s own doc-mandated caller pattern hit exactly this
/// before the carve-out was added (spec-068 D-11 end-to-end wiring). Residual gap: a
/// condensation whose range happens to end exactly at `hi == 0` (only possible when
/// `keep_recent` leaves just one message event past position 0) leaves `last_condensed_seq == 0`
/// again, indistinguishable from "never condensed" — narrow enough in practice that resolving it
/// properly needs an `Option<u64>` schema change; tracked as a follow-up, not blocking here.
///
/// # Errors
///
/// Returns [`SessionError::CondensationOverlap`] if `last_condensed_seq > 0 && lo <=
/// last_condensed_seq`.