Keep append-only session revisions
This commit is contained in:
@@ -217,6 +217,29 @@ def _write_agent_session_db_payload(
|
||||
updated_at = time.time()
|
||||
payload_json = json.dumps(payload, ensure_ascii=False, sort_keys=True)
|
||||
with _connect_agent_session_db(directory) as conn:
|
||||
current = conn.execute(
|
||||
"""
|
||||
select updated_at, snapshot_path, payload_json
|
||||
from agent_sessions
|
||||
where session_id = ?
|
||||
""",
|
||||
(session_id,),
|
||||
).fetchone()
|
||||
if current is not None and current['payload_json'] != payload_json:
|
||||
_write_agent_session_revision(
|
||||
conn,
|
||||
session_id=session_id,
|
||||
updated_at=float(current['updated_at'] or 0.0),
|
||||
snapshot_path=str(current['snapshot_path'] or ''),
|
||||
payload_json=str(current['payload_json'] or ''),
|
||||
)
|
||||
_write_agent_session_revision(
|
||||
conn,
|
||||
session_id=session_id,
|
||||
updated_at=updated_at,
|
||||
snapshot_path=str(snapshot_path),
|
||||
payload_json=payload_json,
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
insert into agent_sessions (
|
||||
@@ -232,6 +255,39 @@ def _write_agent_session_db_payload(
|
||||
)
|
||||
|
||||
|
||||
def _write_agent_session_revision(
|
||||
conn: sqlite3.Connection,
|
||||
*,
|
||||
session_id: str,
|
||||
updated_at: float,
|
||||
snapshot_path: str,
|
||||
payload_json: str,
|
||||
) -> None:
|
||||
if not payload_json:
|
||||
return
|
||||
latest = conn.execute(
|
||||
"""
|
||||
select payload_json
|
||||
from agent_session_revisions
|
||||
where session_id = ?
|
||||
order by revision_id desc
|
||||
limit 1
|
||||
""",
|
||||
(session_id,),
|
||||
).fetchone()
|
||||
if latest is not None and latest['payload_json'] == payload_json:
|
||||
return
|
||||
conn.execute(
|
||||
"""
|
||||
insert into agent_session_revisions (
|
||||
session_id, updated_at, snapshot_path, payload_json
|
||||
)
|
||||
values (?, ?, ?, ?)
|
||||
""",
|
||||
(session_id, updated_at, snapshot_path, payload_json),
|
||||
)
|
||||
|
||||
|
||||
def _read_agent_session_db_payload(
|
||||
directory: Path,
|
||||
session_id: str,
|
||||
@@ -292,6 +348,23 @@ def _connect_agent_session_db(directory: Path) -> sqlite3.Connection:
|
||||
on agent_sessions(updated_at)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
create table if not exists agent_session_revisions (
|
||||
revision_id integer primary key autoincrement,
|
||||
session_id text not null,
|
||||
updated_at real not null,
|
||||
snapshot_path text default '',
|
||||
payload_json text not null
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
create index if not exists idx_agent_session_revisions_session
|
||||
on agent_session_revisions(session_id, revision_id)
|
||||
"""
|
||||
)
|
||||
return conn
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user