refactor(sessions): check an explicit export --session-id once in _cmd_export

Gate finding (BN06 r1 quality, Low): the missing-id check lived in three
places (_collect_sessions, _export_trace, _export_markdown_single), and
_collect_sessions had grown a list|int|None return with an isinstance
branch in _export_flat just to carry the exit code. Resolve the id once at
the top of _cmd_export and return _not_found's non-zero code there; the
per-format copies and the int return are now dead and reverted.
This commit is contained in:
kshitijk4poor
2026-10-03 15:26:01 +05:30
committed by kshitij
parent 814c7de76e
commit 2dbe76a978

View File

@@ -331,6 +331,8 @@ def _cmd_export(db, args):
# A backup includes protected rows; prune's keep rules must not omit them.
filters["archived"] = None
filters["include_pinned"] = True
if args.session_id and not db.resolve_session_id(args.session_id):
return _not_found(args.session_id)
def _redact(data):
if not args.redact or data is None:
@@ -366,7 +368,7 @@ def _cmd_export(db, args):
return False
def _collect_sessions():
"""Return exported sessions, a failure exit code, or None for a dry-run preview."""
"""--session-id / filters / bare export -> redacted session dicts, or None after printing an error."""
def _one(session_id):
return _redact(db.export_session(session_id, **projection))
if args.session_id:
@@ -375,7 +377,8 @@ def _cmd_export(db, args):
return None
data = _one(resolved) if resolved else None
if not data:
return _not_found(args.session_id)
_not_found(args.session_id)
return None
return [data]
if filters:
candidates = db.list_prune_candidates(**filters)
@@ -436,8 +439,6 @@ def _export_flat(kind, args, collect):
print(message)
return
sessions = collect()
if isinstance(sessions, int):
return sessions
if sessions is not None:
from hermes_cli.session_export import default_save_filename
name = (default_save_filename(sessions[0].get("id", ""), args.format) if len(sessions) == 1
@@ -456,8 +457,6 @@ def _export_trace(db, args, filters):
if not session_id:
print("No session found to export. Pass --session-id.")
return
if session_id and not db.resolve_session_id(session_id):
return _not_found(session_id)
from agent.trace_upload import TraceRedactionError, build_trace_jsonl, upload_session_trace
redact_trace = not getattr(args, "no_redact", False)
if getattr(args, "upload", False):
@@ -559,8 +558,6 @@ def _export_markdown_single(db, args, export_one, output_dir, lineage_is_logical
"""--session-id markdown export, optionally + verified delete of it and its delegates."""
from hermes_cli.session_export_md import verify_export_file
resolved_session_id = db.resolve_session_id(args.session_id)
if not resolved_session_id:
return _not_found(args.session_id)
delete_target_ids = (
db.get_session_delete_targets(resolved_session_id) if args.delete_after_verified else [resolved_session_id]
)