From 2dbe76a9785da41afa71f9a31d4eef9a8c3fd73e Mon Sep 17 00:00:00 2001 From: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Date: Sat, 3 Oct 2026 15:26:01 +0530 Subject: [PATCH] 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. --- hermes_cli/sessions_cmd.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/hermes_cli/sessions_cmd.py b/hermes_cli/sessions_cmd.py index 8e6ef41cd0..fa00c0008b 100644 --- a/hermes_cli/sessions_cmd.py +++ b/hermes_cli/sessions_cmd.py @@ -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] )