copilot-session-tools 0.16.0__tar.gz → 0.18.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/CHANGELOG.md +18 -0
  2. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/PKG-INFO +1 -1
  3. copilot_session_tools-0.18.0/pyproject.toml +186 -0
  4. copilot_session_tools-0.16.0/pyproject.toml → copilot_session_tools-0.18.0/pyproject.toml.orig +1 -1
  5. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/__init__.py +1 -1
  6. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/database.py +3 -0
  7. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/db_schema.py +7 -0
  8. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/db_storage.py +60 -14
  9. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/scanner/__init__.py +1 -1
  10. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/scanner/cli.py +176 -3
  11. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/scanner/diff.py +161 -0
  12. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/scanner/shared.py +11 -0
  13. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/scanner/vscode.py +118 -0
  14. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/LICENSE +0 -0
  15. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/README.md +0 -0
  16. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/cli.py +0 -0
  17. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/content_types.py +0 -0
  18. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/db_retrieval.py +0 -0
  19. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/db_search.py +0 -0
  20. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/html_exporter.py +0 -0
  21. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/markdown_exporter.py +0 -0
  22. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/py.typed +0 -0
  23. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/refresh.py +0 -0
  24. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/scanner/content.py +0 -0
  25. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/scanner/discovery.py +0 -0
  26. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/scanner/git.py +0 -0
  27. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/scanner/models.py +0 -0
  28. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/transcript_cleanup.py +0 -0
  29. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/utils.py +0 -0
  30. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/version_check.py +0 -0
  31. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/web/__init__.py +0 -0
  32. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/web/templates/error.html +0 -0
  33. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/web/templates/index.html +0 -0
  34. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/web/templates/session.html +0 -0
  35. {copilot_session_tools-0.16.0 → copilot_session_tools-0.18.0}/src/copilot_session_tools/web/webapp.py +0 -0
@@ -7,6 +7,24 @@ All notable changes to this project will be documented in this file.
7
7
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
8
8
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
9
9
 
10
+ ## [0.17.1] - 2026-06-30
11
+
12
+ ### Fixed
13
+
14
+ - **Scanner / Database**: CLI enrichment no longer re-writes already-current sessions on every scan. Staleness is now detected by storing each session's Chronicle turn count (`cst_sessions.builtin_turns`) and comparing it like-for-like, instead of comparing Chronicle turns against the enriched user-message count (an incompatible quantity that never matched, so hundreds of sessions were re-enriched on every scan and the scan never converged).
15
+ - **Database**: Deleting a session's full-text-search rows now targets `cst_messages_fts` by `rowid` via the indexed message lookup rather than the `UNINDEXED` `session_id` column, which forced a full scan of every FTS document. On a large database this cuts per-session FTS deletion from ~1 s to ~1.5 ms, making each enrichment write roughly an order of magnitude faster.
16
+
17
+ ### Changed
18
+
19
+ - **Database**: Schema migrated to v12, adding the nullable `cst_sessions.builtin_turns` column. The first scan after upgrading performs a one-time backfill of existing CLI sessions; subsequent incremental scans skip unchanged sessions.
20
+
21
+ ## [0.17.0] - 2026-06-29
22
+
23
+ ### Added
24
+
25
+ - **CLI File Changes**: CLI `edit`/`create` tool calls now render as per-message file-change diffs, matching the VS Code view across the web viewer, Markdown export, and HTML export.
26
+ - **Per-turn consolidation**: Multiple edits to the same file within a turn collapse into one card — a true net diff when the file is created in-turn, or reconstructed from a prior full-file `view` (including subagent views), with a stacked-hunks fallback when no base is available.
27
+
10
28
  ## [0.16.0] - 2026-06-06
11
29
 
12
30
  ### Added
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: copilot-session-tools
3
- Version: 0.16.0
3
+ Version: 0.18.0
4
4
  Summary: A collection of tools for VS Code GitHub Copilot chat history
5
5
  Keywords: copilot,github,vscode,chat,archive,history
6
6
  Author: Arithmomaniac
@@ -0,0 +1,186 @@
1
+ [build-system]
2
+ requires = ["uv_build>=0.6.0,<0.10.0"]
3
+ build-backend = "uv_build"
4
+
5
+ [project]
6
+ name = "copilot-session-tools"
7
+ version = "0.18.0"
8
+ description = "A collection of tools for VS Code GitHub Copilot chat history"
9
+ readme = "README.md"
10
+ requires-python = ">=3.12"
11
+ license = "MIT"
12
+ license-files = ["LICENSE"]
13
+ keywords = [
14
+ "copilot",
15
+ "github",
16
+ "vscode",
17
+ "chat",
18
+ "archive",
19
+ "history",
20
+ ]
21
+ classifiers = [
22
+ "Development Status :: 3 - Alpha",
23
+ "Intended Audience :: Developers",
24
+ "License :: OSI Approved :: MIT License",
25
+ "Programming Language :: Python :: 3",
26
+ "Programming Language :: Python :: 3.12",
27
+ "Topic :: Software Development :: Libraries :: Python Modules",
28
+ "Topic :: Utilities",
29
+ ]
30
+ dependencies = [
31
+ "ssrjson>=0.0.16",
32
+ "jinja2>=3.0.0",
33
+ "markdown>=3.4.0",
34
+ "pygments>=2.17.0",
35
+ ]
36
+
37
+ [[project.authors]]
38
+ name = "Arithmomaniac"
39
+
40
+ [project.optional-dependencies]
41
+ cli = [
42
+ "typer>=0.12.0",
43
+ "rich>=13.0.0",
44
+ ]
45
+ web = ["flask>=3.0.0"]
46
+ llm = ["litellm>=1.70.0"]
47
+ all = ["copilot-session-tools[cli,web,llm]"]
48
+
49
+ [project.scripts]
50
+ copilot-session-tools = "copilot_session_tools.cli:run"
51
+
52
+ [project.urls]
53
+ Homepage = "https://github.com/Arithmomaniac/copilot-session-tools"
54
+ Repository = "https://github.com/Arithmomaniac/copilot-session-tools"
55
+ Issues = "https://github.com/Arithmomaniac/copilot-session-tools/issues"
56
+
57
+ [tool.uv]
58
+ package = true
59
+
60
+ [tool.uv.build-backend]
61
+ source-include = ["CHANGELOG.md"]
62
+
63
+ [tool.pytest.ini_options]
64
+ testpaths = ["tests"]
65
+ pythonpath = ["src"]
66
+ markers = ["""slow: marks tests as slow (scanning real user data, run with '-m slow' or all with '-m ""')"""]
67
+
68
+ [tool.ruff]
69
+ line-length = 180
70
+ exclude = [
71
+ "*.pyi",
72
+ "extract_chats.py",
73
+ ]
74
+
75
+ [tool.ruff.lint]
76
+ select = [
77
+ "F",
78
+ "E",
79
+ "FURB",
80
+ "RUF",
81
+ "PERF",
82
+ "UP",
83
+ "SIM",
84
+ "C4",
85
+ "A",
86
+ "S",
87
+ "FBT",
88
+ "PTH",
89
+ "DTZ",
90
+ "SLOT",
91
+ "ASYNC",
92
+ "LOG",
93
+ "T20",
94
+ "PLE",
95
+ "PLW",
96
+ "TRY",
97
+ "PD",
98
+ "NPY",
99
+ "ISC",
100
+ "I",
101
+ "B",
102
+ ]
103
+ ignore = [
104
+ "E402",
105
+ "FBT001",
106
+ "FBT002",
107
+ "TRY003",
108
+ "S105",
109
+ "S108",
110
+ "SIM108",
111
+ "PERF401",
112
+ "DTZ001",
113
+ "DTZ006",
114
+ "TRY300",
115
+ "PLW2901",
116
+ ]
117
+
118
+ [tool.ruff.lint.per-file-ignores]
119
+ "tests/**/*.py" = [
120
+ "S101",
121
+ "S106",
122
+ "T201",
123
+ "PTH123",
124
+ ]
125
+ "src/**/cli.py" = ["T201"]
126
+ "scripts/**/*.py" = ["T201"]
127
+ "src/**/web/__init__.py" = ["T201"]
128
+
129
+ [tool.ty.environment]
130
+ python-version = "3.12"
131
+
132
+ [tool.ty.src]
133
+ include = [
134
+ "src/**/*.py",
135
+ "tests/**/*.py",
136
+ ]
137
+
138
+ [tool.ty.rules]
139
+ invalid-assignment = "error"
140
+ invalid-return-type = "error"
141
+ unknown-argument = "error"
142
+ missing-argument = "error"
143
+ too-many-positional-arguments = "error"
144
+ invalid-type-form = "error"
145
+ invalid-base = "error"
146
+ invalid-legacy-type-variable = "error"
147
+ invalid-type-variable-constraints = "error"
148
+ invalid-exception-caught = "error"
149
+ invalid-overload = "error"
150
+ invalid-parameter-default = "error"
151
+ invalid-super-argument = "error"
152
+ subclass-of-final-class = "error"
153
+ override-of-final-method = "error"
154
+ invalid-metaclass = "error"
155
+ invalid-context-manager = "error"
156
+ invalid-declaration = "error"
157
+ conflicting-declarations = "error"
158
+ cyclic-class-definition = "error"
159
+ duplicate-base = "error"
160
+ invalid-await = "error"
161
+ not-subscriptable = "error"
162
+ index-out-of-bounds = "error"
163
+ not-iterable = "error"
164
+ invalid-raise = "error"
165
+ static-assert-error = "error"
166
+ type-assertion-failure = "error"
167
+ call-non-callable = "error"
168
+ unsupported-operator = "error"
169
+ division-by-zero = "error"
170
+ undefined-reveal = "error"
171
+ unresolved-import = "warn"
172
+ possibly-unresolved-reference = "warn"
173
+ unresolved-attribute = "warn"
174
+ invalid-argument-type = "warn"
175
+ redundant-cast = "warn"
176
+
177
+ [dependency-groups]
178
+ dev = [
179
+ "pytest>=7.0.0",
180
+ "pytest-cov>=4.0.0",
181
+ "pytest-playwright>=0.4.0",
182
+ "pytest-benchmark>=4.0.0",
183
+ "ruff>=0.14.11",
184
+ "ty>=0.0.12",
185
+ "pytest-regressions>=2.10.0",
186
+ ]
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "copilot-session-tools"
7
- version = "0.16.0"
7
+ version = "0.18.0"
8
8
  description = "A collection of tools for VS Code GitHub Copilot chat history"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.12"
@@ -12,7 +12,7 @@ This project borrows patterns from several open-source projects:
12
12
  - tad-hq/universal-session-viewer: FTS5 full-text search design
13
13
  """
14
14
 
15
- __version__ = "0.16.0"
15
+ __version__ = "0.18.0"
16
16
 
17
17
  from .content_types import (
18
18
  CONTENT_TYPES,
@@ -665,6 +665,9 @@ class Database:
665
665
  Idempotent: deletes existing data for this session_id, then inserts fresh.
666
666
  """
667
667
  with self._get_connection() as conn:
668
+ # Attach Chronicle (best-effort, idempotent) so the turn count can be
669
+ # recorded on cst_sessions for like-for-like incremental staleness checks.
670
+ self._attach_chronicle(conn)
668
671
  db_storage.enrich_session(conn, session)
669
672
 
670
673
  def cleanup_orphaned_cst_sessions(self) -> list[str]:
@@ -34,6 +34,7 @@ CST_SESSION_COLUMNS = (
34
34
  "parser_version",
35
35
  "source_format",
36
36
  "enrichment_version",
37
+ "builtin_turns",
37
38
  )
38
39
 
39
40
  CST_MESSAGE_COLUMNS = (
@@ -147,11 +148,16 @@ def session_to_row(
147
148
  *,
148
149
  enrichment_version: str | None = None,
149
150
  updated_at_fallback: str | None = None,
151
+ builtin_turns: int | None = None,
150
152
  ) -> tuple:
151
153
  """Map a ChatSession to a parameter tuple matching CST_SESSION_COLUMNS.
152
154
 
153
155
  *updated_at_fallback* is used when ``session.updated_at`` is falsy
154
156
  (e.g. the enrichment path fills it with a timestamp).
157
+
158
+ *builtin_turns* records the Chronicle turn count at enrichment time so
159
+ incremental scans can detect "this CLI session grew" with a like-for-like
160
+ comparison (mirrors how VS Code sessions compare ``source_file_mtime``).
155
161
  """
156
162
  return (
157
163
  session.session_id,
@@ -171,6 +177,7 @@ def session_to_row(
171
177
  session.parser_version,
172
178
  session.source_format,
173
179
  enrichment_version,
180
+ builtin_turns,
174
181
  )
175
182
 
176
183
 
@@ -31,7 +31,7 @@ from .db_schema import (
31
31
  from .markdown_exporter import message_to_markdown
32
32
  from .scanner import ChatSession
33
33
 
34
- CST_SCHEMA_VERSION = 11
34
+ CST_SCHEMA_VERSION = 12
35
35
  CHRONICLE_SCHEMA_VERSION = 4
36
36
 
37
37
 
@@ -57,7 +57,8 @@ CREATE TABLE IF NOT EXISTS cst_sessions (
57
57
  repository_url TEXT,
58
58
  parser_version INTEGER NOT NULL DEFAULT 1,
59
59
  source_format TEXT,
60
- enrichment_version TEXT
60
+ enrichment_version TEXT,
61
+ builtin_turns INTEGER
61
62
  );
62
63
 
63
64
  CREATE TABLE IF NOT EXISTS cst_messages (
@@ -402,6 +403,13 @@ def ensure_schema(conn: sqlite3.Connection) -> None:
402
403
  # v10 → v11: Add CLI source event ids for fork-aware search de-duplication.
403
404
  if current_version < 11:
404
405
  cursor.execute("CREATE INDEX IF NOT EXISTS idx_cst_messages_source_event ON cst_messages(source_event_id)")
406
+ # v11 → v12: Store Chronicle turn count per enriched CLI session so
407
+ # incremental scans detect growth via a like-for-like comparison
408
+ # instead of comparing Chronicle turns against enriched user-message
409
+ # counts (which differ and caused perpetual re-enrichment).
410
+ if current_version < 12:
411
+ with contextlib.suppress(sqlite3.OperationalError):
412
+ cursor.execute("ALTER TABLE cst_sessions ADD COLUMN builtin_turns INTEGER")
405
413
  cursor.execute(
406
414
  "UPDATE cst_schema_version SET version = ?",
407
415
  (CST_SCHEMA_VERSION,),
@@ -444,15 +452,23 @@ def has_cst_tables(conn: sqlite3.Connection, *, unenriched_only: bool = False) -
444
452
 
445
453
 
446
454
  def discover_sessions_needing_enrichment(conn: sqlite3.Connection, *, has_chronicle: bool = False) -> list[dict]:
447
- """Find CLI sessions needing enrichment by comparing Chronicle turns vs cst_messages.
455
+ """Find CLI sessions needing enrichment by comparing Chronicle turn counts.
448
456
 
449
- When Chronicle is ATTACHed (``has_chronicle=True``), compares
450
- ``chronicle.sessions``/``chronicle.turns`` against ``cst_messages``.
451
- Without Chronicle, returns an empty list (no discovery source).
457
+ When Chronicle is ATTACHed (``has_chronicle=True``), compares the current
458
+ ``chronicle.turns`` count for each session against the turn count stored on
459
+ ``cst_sessions.builtin_turns`` at the last enrichment. Without Chronicle,
460
+ returns an empty list (no discovery source).
452
461
 
453
462
  Returns sessions where:
454
463
  - No cst_sessions row exists (new, never enriched)
455
- - Turn count differs (session has new messages since last enrichment)
464
+ - ``builtin_turns`` was never recorded (enriched before v12 — backfilled once)
465
+ - The Chronicle turn count changed since last enrichment (session grew)
466
+
467
+ The comparison is like-for-like (stored Chronicle turn count vs current
468
+ Chronicle turn count). An earlier version compared Chronicle turns against
469
+ the count of enriched ``role='user'`` messages; those counts differ by a
470
+ source-dependent offset, so already-current sessions were re-flagged stale
471
+ and fully re-written on *every* scan.
456
472
 
457
473
  Uses direct sqlite_master probe (not has_cst_tables()) so this works
458
474
  correctly even when --unenriched-only is set — scan should still enrich.
@@ -469,23 +485,23 @@ def discover_sessions_needing_enrichment(conn: sqlite3.Connection, *, has_chroni
469
485
  SELECT
470
486
  s.id as session_id,
471
487
  COUNT(DISTINCT t.turn_index) as builtin_turns,
472
- (SELECT COUNT(*) FROM cst_messages cm
473
- WHERE cm.session_id = s.id AND cm.role = 'user') as cst_user_msgs,
488
+ cs.builtin_turns as stored_turns,
474
489
  CASE WHEN cs.session_id IS NULL THEN 'new'
475
490
  ELSE 'stale' END as status
476
491
  FROM chronicle.sessions s
477
492
  LEFT JOIN chronicle.turns t ON s.id = t.session_id
478
493
  LEFT JOIN cst_sessions cs ON s.id = cs.session_id
479
494
  GROUP BY s.id
480
- HAVING cst_user_msgs != builtin_turns
481
- OR cs.session_id IS NULL
495
+ HAVING cs.session_id IS NULL
496
+ OR cs.builtin_turns IS NULL
497
+ OR cs.builtin_turns != COUNT(DISTINCT t.turn_index)
482
498
  """).fetchall()
483
499
  else:
484
500
  rows = conn.execute("""
485
501
  SELECT
486
502
  s.id as session_id,
487
503
  COUNT(DISTINCT t.turn_index) as builtin_turns,
488
- 0 as cst_user_msgs,
504
+ NULL as stored_turns,
489
505
  'new' as status
490
506
  FROM chronicle.sessions s
491
507
  LEFT JOIN chronicle.turns t ON s.id = t.session_id
@@ -526,7 +542,15 @@ def _delete_session_data(cursor: sqlite3.Cursor, session_id: str) -> None:
526
542
  "DELETE FROM cst_command_runs WHERE message_id IN (SELECT id FROM cst_messages WHERE session_id = ?)",
527
543
  (session_id,),
528
544
  )
529
- cursor.execute("DELETE FROM cst_messages_fts WHERE session_id = ?", (session_id,))
545
+ # Delete FTS rows by rowid (== cst_messages.id) via the indexed session
546
+ # lookup. ``session_id`` is UNINDEXED in the FTS5 table, so deleting by it
547
+ # forces a full external scan of every FTS doc; deleting by rowid is a
548
+ # direct lookup. Must run before the cst_messages delete below so the
549
+ # subquery can still resolve the message ids.
550
+ cursor.execute(
551
+ "DELETE FROM cst_messages_fts WHERE rowid IN (SELECT id FROM cst_messages WHERE session_id = ?)",
552
+ (session_id,),
553
+ )
530
554
  cursor.execute("DELETE FROM cst_root_agent_intervals WHERE session_id = ?", (session_id,))
531
555
  cursor.execute("DELETE FROM cst_session_contexts WHERE session_id = ?", (session_id,))
532
556
  cursor.execute("DELETE FROM cst_messages WHERE session_id = ?", (session_id,))
@@ -949,6 +973,24 @@ def get_all_file_metadata(conn: sqlite3.Connection) -> dict[str, tuple[float, in
949
973
  # ---------------------------------------------------------------------------
950
974
 
951
975
 
976
+ def _builtin_turn_count(conn: sqlite3.Connection, session_id: str) -> int | None:
977
+ """Return the current Chronicle turn count for *session_id*, or None.
978
+
979
+ Returns None when Chronicle is not ATTACHed (so the caller stores NULL and
980
+ the session is re-evaluated on a later scan that has Chronicle). Returns 0
981
+ for a Chronicle session that genuinely has no turns, so it converges instead
982
+ of being re-flagged forever.
983
+ """
984
+ try:
985
+ row = conn.execute(
986
+ "SELECT COUNT(DISTINCT turn_index) FROM chronicle.turns WHERE session_id = ?",
987
+ (session_id,),
988
+ ).fetchone()
989
+ except sqlite3.OperationalError:
990
+ return None # Chronicle not attached
991
+ return row[0] if row else 0
992
+
993
+
952
994
  def enrich_session(conn: sqlite3.Connection, session: ChatSession) -> None:
953
995
  """Write/update cst_* tables for a parsed ChatSession.
954
996
 
@@ -958,6 +1000,10 @@ def enrich_session(conn: sqlite3.Connection, session: ChatSession) -> None:
958
1000
 
959
1001
  enriched_at = datetime.now(UTC).isoformat()
960
1002
 
1003
+ # Record the Chronicle turn count so incremental scans can detect growth
1004
+ # with a like-for-like comparison (see discover_sessions_needing_enrichment).
1005
+ builtin_turns = _builtin_turn_count(conn, session.session_id)
1006
+
961
1007
  cursor = conn.cursor()
962
1008
 
963
1009
  # Delete existing data for idempotency
@@ -969,7 +1015,7 @@ def enrich_session(conn: sqlite3.Connection, session: ChatSession) -> None:
969
1015
  # Insert session
970
1016
  cursor.execute(
971
1017
  insert_sql("cst_sessions", CST_SESSION_COLUMNS),
972
- session_to_row(session, enrichment_version=__version__, updated_at_fallback=enriched_at),
1018
+ session_to_row(session, enrichment_version=__version__, updated_at_fallback=enriched_at, builtin_turns=builtin_turns),
973
1019
  )
974
1020
 
975
1021
  for interval in session.root_agent_intervals:
@@ -5,7 +5,7 @@ Data structures are informed by:
5
5
  - microsoft/vscode (https://github.com/microsoft/vscode)
6
6
  """
7
7
 
8
- PARSER_VERSION = 4
8
+ PARSER_VERSION = 8
9
9
 
10
10
  from .cli import _parse_cli_jsonl_file, _parse_workspace_yaml
11
11
  from .content import (
@@ -11,6 +11,7 @@ import ssrjson
11
11
  from copilot_session_tools.scanner import PARSER_VERSION
12
12
 
13
13
  from .content import _format_tool_display_message, _get_file_metadata
14
+ from .diff import CliFileOp, cli_file_op_from_tool, consolidate_cli_file_ops, strip_view_line_numbers
14
15
  from .git import _normalize_git_url, detect_repository_url
15
16
  from .models import (
16
17
  ChatMessage,
@@ -22,8 +23,8 @@ from .models import (
22
23
  ShellIOEntry,
23
24
  ToolInvocation,
24
25
  )
26
+ from .shared import strip_ansi_control_sequences
25
27
 
26
- _ANSI_ESCAPE_PATTERN = re.compile(r"\x1b(?:\[[0-9;]*[A-Za-z]|\][^\x07]*\x07|\][^\x1b]*\x1b\\)")
27
28
  _SESSION_ID_PATTERN = r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"
28
29
  _SESSION_REFERENCE_PATTERN = re.compile(
29
30
  rf'(?:"(?P<name>[^"\n]+)" \((?P<named_id>{_SESSION_ID_PATTERN})\)|(?P<bare_id>{_SESSION_ID_PATTERN}))',
@@ -60,7 +61,7 @@ def _stringify_event_value(value: object) -> str:
60
61
  if value is None:
61
62
  return ""
62
63
  if isinstance(value, str):
63
- return _ANSI_ESCAPE_PATTERN.sub("", value).strip()
64
+ return strip_ansi_control_sequences(value)
64
65
  if isinstance(value, bool | int | float):
65
66
  return str(value)
66
67
  if isinstance(value, list):
@@ -170,6 +171,11 @@ def _format_session_event_status(event_type: str, event_data: dict) -> tuple[str
170
171
  if event_type == "auto_mode_switch.completed":
171
172
  response = _stringify_event_value(event_data.get("response"))
172
173
  return f"Auto mode switch completed: {response}" if response else "Auto mode switch completed", "auto-mode"
174
+ if event_type == "session.auto_mode_resolved":
175
+ model = _stringify_event_value(event_data.get("chosenModel"))
176
+ bucket = _stringify_event_value(event_data.get("reasoningBucket"))
177
+ details = f" ({bucket} reasoning)" if bucket else ""
178
+ return f"Auto selected {model}{details}" if model else "Auto model resolved", "auto-mode"
173
179
  if event_type == "command.queued":
174
180
  command = _stringify_event_value(event_data.get("command"))
175
181
  return f"Command queued: {command}" if command else "Command queued", "command"
@@ -188,6 +194,9 @@ def _format_session_event_status(event_type: str, event_data: dict) -> tuple[str
188
194
  error = _stringify_event_value(event_data.get("error"))
189
195
  label = f"Hook failed: {hook_type}" if hook_type else "Hook failed"
190
196
  return f"{label} - {error}" if error else label, "hook-error"
197
+ if event_type == "hook.progress":
198
+ message = _stringify_event_value(event_data.get("message"))
199
+ return f"Hook progress: {message}" if message else "Hook progress", "hook-progress"
191
200
  if event_type == "mcp_app.tool_call_complete":
192
201
  server = _stringify_event_value(event_data.get("serverName"))
193
202
  tool = _stringify_event_value(event_data.get("toolName"))
@@ -248,6 +257,77 @@ def _format_session_event_status(event_type: str, event_data: dict) -> tuple[str
248
257
  return None
249
258
 
250
259
 
260
+ _FUSION_PHASE_LABELS = {
261
+ "primary": "Drafted answer",
262
+ "draft": "Drafted",
263
+ "judge": "Checked",
264
+ "repair": "Repaired",
265
+ "critic": "Reviewed",
266
+ "revision": "Revised",
267
+ "follow_up": "Continued",
268
+ }
269
+
270
+
271
+ def _build_fusion_statuses(events: list[dict]) -> dict[str, str]:
272
+ """Reduce durable HydraFusion events to one safe transcript status per run."""
273
+ summaries: dict[str, dict[str, object]] = {}
274
+
275
+ for event in events:
276
+ event_type = event.get("type")
277
+ data = event.get("data", {})
278
+ if not isinstance(data, dict):
279
+ continue
280
+ fusion_id = data.get("fusionId")
281
+ if not isinstance(fusion_id, str) or not fusion_id:
282
+ continue
283
+
284
+ summary = summaries.setdefault(fusion_id, {"milestones": []})
285
+ milestones = cast("list[str]", summary["milestones"])
286
+
287
+ if event_type == "session.fusion_resolved":
288
+ summary["pattern"] = data.get("pattern")
289
+ summary["primaryModel"] = data.get("primaryModel")
290
+ summary["secondaryModel"] = data.get("secondaryModel")
291
+ elif event_type == "assistant.fusion_phase_completed":
292
+ phase_kind = _stringify_event_value(data.get("phaseKind"))
293
+ label = _FUSION_PHASE_LABELS.get(phase_kind, phase_kind.replace("_", " ").title())
294
+ verdict = _stringify_event_value(data.get("verdict"))
295
+ if phase_kind == "judge" and verdict == "accept":
296
+ label = "Accepted"
297
+ elif phase_kind == "judge" and verdict == "reject":
298
+ label = "Changes requested"
299
+ if label and label not in milestones:
300
+ milestones.append(label)
301
+ elif event_type == "assistant.fusion_phase_failed":
302
+ phase_kind = _stringify_event_value(data.get("phaseKind"))
303
+ label = _FUSION_PHASE_LABELS.get(phase_kind, phase_kind.replace("_", " ").title())
304
+ failure = f"Warning: {label or 'Fusion phase'} failed"
305
+ if data.get("degradedToPhaseId"):
306
+ failure += "; continuing with reduced Fusion coverage"
307
+ milestones.append(failure)
308
+ elif event_type == "session.fusion_completed":
309
+ degraded = data.get("outcome") == "degraded" or bool(data.get("degradedReason"))
310
+ milestones.append("Warning: final response prepared with reduced Fusion coverage" if degraded else "Final response prepared")
311
+ summary["completed"] = True
312
+
313
+ statuses: dict[str, str] = {}
314
+ for fusion_id, summary in summaries.items():
315
+ pattern = _stringify_event_value(summary.get("pattern"))
316
+ pattern_label = pattern.title() if pattern else "Workflow"
317
+ models = [
318
+ _stringify_event_value(summary.get("primaryModel")),
319
+ _stringify_event_value(summary.get("secondaryModel")),
320
+ ]
321
+ model_text = " + ".join(model for model in models if model)
322
+ header = f"Fusion · {pattern_label}"
323
+ if model_text:
324
+ header += f" · {model_text}"
325
+ milestones = cast("list[str]", summary["milestones"])
326
+ statuses[fusion_id] = "; ".join([header, *milestones])
327
+
328
+ return statuses
329
+
330
+
251
331
  def _parse_workspace_yaml(session_dir: Path) -> dict[str, str]:
252
332
  """Parse a workspace.yaml file from a CLI session directory.
253
333
 
@@ -345,10 +425,12 @@ class _CliSessionBuilder:
345
425
  self.subagent_child_structured = subagent_child_structured or {}
346
426
  self.agent_display_names = agent_display_names or {}
347
427
  self.shell_title_map: dict[str, str] = {} # shellId → title for async shell backlinks
428
+ self.view_base: dict[str, str] = {} # path → last full-file view content (reconstruction base)
348
429
  self.messages: list[ChatMessage] = []
349
430
  self.current_assistant_content_blocks: list[ContentBlock] = []
350
431
  self.current_assistant_tool_invocations: list[ToolInvocation] = []
351
432
  self.current_assistant_command_runs: list[CommandRun] = []
433
+ self.current_assistant_file_ops: list[CliFileOp] = []
352
434
  self.current_assistant_timestamp: str | None = None
353
435
  self.current_assistant_source_event_id: str | None = None
354
436
  self.pending_tool_requests: dict[str, dict] = {}
@@ -360,7 +442,7 @@ class _CliSessionBuilder:
360
442
 
361
443
  def flush_assistant_message(self) -> None:
362
444
  """Flush accumulated assistant content blocks into a single message."""
363
- has_content = self.current_assistant_content_blocks or self.current_assistant_tool_invocations or self.current_assistant_command_runs
445
+ has_content = self.current_assistant_content_blocks or self.current_assistant_tool_invocations or self.current_assistant_command_runs or self.current_assistant_file_ops
364
446
  if not has_content:
365
447
  return
366
448
 
@@ -389,6 +471,7 @@ class _CliSessionBuilder:
389
471
  tool_invocations=self.current_assistant_tool_invocations.copy(),
390
472
  command_runs=self.current_assistant_command_runs.copy(),
391
473
  content_blocks=self.current_assistant_content_blocks.copy(),
474
+ file_changes=consolidate_cli_file_ops(self.current_assistant_file_ops, self.view_base),
392
475
  children=children,
393
476
  )
394
477
  )
@@ -397,6 +480,7 @@ class _CliSessionBuilder:
397
480
  self.current_assistant_content_blocks = []
398
481
  self.current_assistant_tool_invocations = []
399
482
  self.current_assistant_command_runs = []
483
+ self.current_assistant_file_ops = []
400
484
  self.current_assistant_timestamp = None
401
485
  self.current_assistant_source_event_id = None
402
486
 
@@ -573,6 +657,20 @@ class _CliSessionBuilder:
573
657
  self.current_assistant_tool_invocations.append(tool_inv)
574
658
  return
575
659
 
660
+ # Capture full-file view results as reconstruction bases for later
661
+ # edit-only sequences. Only whole-file reads (no view_range) are trusted;
662
+ # the result is line-number-prefixed, so strip those to recover content.
663
+ if tool_name == "view" and not argument_dict.get("view_range"):
664
+ view_path = _get_tool_arg_str(argument_dict, "path")
665
+ if view_path:
666
+ execution = self.tool_executions.get(tool_call_id, {})
667
+ complete = execution.get("complete")
668
+ if complete and complete.get("data", {}).get("success"):
669
+ result_obj = complete["data"].get("result", {})
670
+ text = result_obj.get("content", "") if isinstance(result_obj, dict) else ""
671
+ if text:
672
+ self.view_base[view_path] = strip_view_line_numbers(text)
673
+
576
674
  tool_inv, cmd_run = self.build_tool_invocation(tool_call_id, tool_name, arguments)
577
675
 
578
676
  if cmd_run:
@@ -600,6 +698,13 @@ class _CliSessionBuilder:
600
698
  # Don't render task tool inline — the subagent block replaces it
601
699
  self.current_assistant_tool_invocations.append(tool_inv)
602
700
  return
701
+ # Reshape successful edit/create into a CLI file op; multiple ops on
702
+ # the same file in this turn are consolidated into one FileChange at
703
+ # flush (VS Code parity). Gated on success like the runtime hook.
704
+ if tool_name in ("edit", "create") and tool_inv.status == "success":
705
+ file_op = cli_file_op_from_tool(tool_name, argument_dict)
706
+ if file_op is not None:
707
+ self.current_assistant_file_ops.append(file_op)
603
708
  # Add tool invocation inline as a content block
604
709
  display_text = tool_inv.invocation_message or tool_inv.name
605
710
  # Use intentionSummary or toolTitle as enhanced description
@@ -661,6 +766,8 @@ def _parse_cli_jsonl_file(file_path: Path) -> ChatSession | None:
661
766
  if not events:
662
767
  return None
663
768
 
769
+ fusion_statuses = _build_fusion_statuses(events)
770
+
664
771
  # Extract session metadata from session.start event
665
772
  session_id = None
666
773
  created_at = None
@@ -1139,12 +1246,29 @@ def _parse_cli_jsonl_file(file_path: Path) -> ChatSession | None:
1139
1246
  nested_blocks: list[ContentBlock] = []
1140
1247
  nested_tool_invocations: list[ToolInvocation] = []
1141
1248
  nested_command_runs: list[CommandRun] = []
1249
+ nested_file_ops: list[CliFileOp] = []
1142
1250
  for child_tcid, child_tool_name, child_args in builder.subagent_child_structured.get(tool_call_id, []):
1251
+ child_args_dict = _as_tool_argument_dict(child_args)
1252
+ # Capture subagent full-file views so its own edit-only sequences can
1253
+ # reconstruct a net diff (parity with the main-pass capture).
1254
+ if child_tool_name == "view" and not child_args_dict.get("view_range"):
1255
+ vp = _get_tool_arg_str(child_args_dict, "path")
1256
+ complete = builder.tool_executions.get(child_tcid, {}).get("complete")
1257
+ if vp and complete and complete.get("data", {}).get("success"):
1258
+ r = complete["data"].get("result", {})
1259
+ vt = r.get("content", "") if isinstance(r, dict) else ""
1260
+ if vt:
1261
+ builder.view_base[vp] = strip_view_line_numbers(vt)
1143
1262
  tool_inv, cmd_run = builder.build_tool_invocation(child_tcid, child_tool_name, child_args)
1144
1263
  display = _format_tool_display_message(child_tool_name, child_args)
1145
1264
  if tool_inv:
1146
1265
  nested_tool_invocations.append(tool_inv)
1147
1266
  nested_blocks.append(ContentBlock(kind="toolInvocation", content=display or child_tool_name))
1267
+ # Reshape successful child edit/create into a file op (VS Code parity)
1268
+ if child_tool_name in ("edit", "create") and tool_inv.status == "success":
1269
+ child_op = cli_file_op_from_tool(child_tool_name, child_args_dict)
1270
+ if child_op is not None:
1271
+ nested_file_ops.append(child_op)
1148
1272
  elif cmd_run:
1149
1273
  nested_command_runs.append(cmd_run)
1150
1274
  # Use "$ command" format so metadata matching connects them inline
@@ -1164,6 +1288,7 @@ def _parse_cli_jsonl_file(file_path: Path) -> ChatSession | None:
1164
1288
  nested_blocks.append(ContentBlock(kind="text", content=f"**Error:** {error}" if error else "**Error:** Unknown error"))
1165
1289
 
1166
1290
  # Create child ChatMessage for the subagent
1291
+ nested_consolidated_changes = consolidate_cli_file_ops(nested_file_ops, builder.view_base)
1167
1292
  child_msg = ChatMessage(
1168
1293
  role="assistant",
1169
1294
  content=content,
@@ -1174,6 +1299,7 @@ def _parse_cli_jsonl_file(file_path: Path) -> ChatSession | None:
1174
1299
  tool_invocations=nested_tool_invocations,
1175
1300
  command_runs=nested_command_runs,
1176
1301
  content_blocks=nested_blocks,
1302
+ file_changes=nested_consolidated_changes,
1177
1303
  )
1178
1304
 
1179
1305
  block = ContentBlock(
@@ -1189,6 +1315,7 @@ def _parse_cli_jsonl_file(file_path: Path) -> ChatSession | None:
1189
1315
  block.content_blocks = nested_blocks
1190
1316
  block.tool_invocations = nested_tool_invocations
1191
1317
  block.command_runs = nested_command_runs
1318
+ block.file_changes = nested_consolidated_changes
1192
1319
  builder.current_assistant_content_blocks.append(block)
1193
1320
 
1194
1321
  elif event_type == "subagent.completed":
@@ -1218,6 +1345,12 @@ def _parse_cli_jsonl_file(file_path: Path) -> ChatSession | None:
1218
1345
  fork_message = _format_fork_info_message(message, session_id)
1219
1346
  builder.current_assistant_content_blocks.append(ContentBlock(kind="status", content=fork_message, description="fork"))
1220
1347
 
1348
+ elif event_type == "session.fusion_resolved":
1349
+ fusion_id = _stringify_event_value(event_data.get("fusionId"))
1350
+ status = fusion_statuses.get(fusion_id)
1351
+ if status:
1352
+ builder.current_assistant_content_blocks.append(ContentBlock(kind="status", content=status, description="fusion"))
1353
+
1221
1354
  elif event_type == "session.handoff":
1222
1355
  source_type = event_data.get("sourceType") or "unknown"
1223
1356
  repo = event_data.get("repository") or {}
@@ -1370,6 +1503,13 @@ def _parse_cli_jsonl_file(file_path: Path) -> ChatSession | None:
1370
1503
  "assistant.message_delta",
1371
1504
  "assistant.reasoning_delta",
1372
1505
  "assistant.streaming_delta",
1506
+ "assistant.tool_call_delta",
1507
+ "assistant.server_tool_progress",
1508
+ "assistant.fusion_phase_activity",
1509
+ "assistant.fusion_phase_started",
1510
+ "assistant.fusion_phase_completed",
1511
+ "assistant.fusion_phase_failed",
1512
+ "assistant.turn_retry",
1373
1513
  # Plan mode lifecycle
1374
1514
  "exit_plan_mode.requested",
1375
1515
  "exit_plan_mode.completed",
@@ -1383,18 +1523,51 @@ def _parse_cli_jsonl_file(file_path: Path) -> ChatSession | None:
1383
1523
  "sampling.requested",
1384
1524
  "session.background_tasks_changed",
1385
1525
  "session.canvas.opened",
1526
+ "session.canvas.closed",
1386
1527
  "session.canvas.registry_changed",
1528
+ "session.canvas.recorded",
1529
+ "session.canvas.removed",
1387
1530
  "session.custom_notification",
1388
1531
  "session.custom_agents_updated",
1389
1532
  "session.extensions_loaded",
1533
+ "session.extensions.attachments_pushed",
1390
1534
  "session.idle",
1391
1535
  "session.import_legacy",
1392
1536
  "session.mcp_server_status_changed",
1393
1537
  "session.mcp_servers_loaded",
1538
+ "session.autopilot_objective_changed",
1539
+ "session.binary_asset",
1540
+ "session.permissions_changed",
1394
1541
  "session.skills_loaded",
1395
1542
  "session.snapshot_rewind",
1543
+ "session.todos_changed",
1396
1544
  "session.tools_updated",
1397
1545
  "session.usage_info",
1546
+ "session.usage_checkpoint",
1547
+ "session.context_cleared",
1548
+ "session.fusion_commit_started",
1549
+ "session.fusion_completed",
1550
+ "session.fusion_handoff",
1551
+ "session.fusion_route_started",
1552
+ "session.fusion_route_failed",
1553
+ "subagent.configured",
1554
+ "skill.context_delivered",
1555
+ "skill.context_delivered_ref",
1556
+ "skill.invoked_ref",
1557
+ "model.call_start",
1558
+ "model.call_finished",
1559
+ "permission.carriedForward",
1560
+ "permission.messageAuthorization",
1561
+ "permission.messageAuthorizationDegraded",
1562
+ "permission.messageAuthorizationRead",
1563
+ "prompt_cache_break",
1564
+ "sandbox.decision",
1565
+ "session.completion_receipt",
1566
+ "session.session_limits_changed",
1567
+ "tool_search.activated",
1568
+ "factory.run_started",
1569
+ "factory.run_updated",
1570
+ "factory.run_settled",
1398
1571
  ):
1399
1572
  pass
1400
1573
 
@@ -1,6 +1,8 @@
1
1
  """Diff generation and text edit parsing for scanner."""
2
2
 
3
3
  import difflib
4
+ import re
5
+ from dataclasses import dataclass
4
6
  from pathlib import Path
5
7
 
6
8
  from .content import _extract_uri_filename, _extract_uri_path
@@ -91,6 +93,165 @@ def _generate_unified_diff(original: str, modified: str, filename: str = "file")
91
93
  return "".join(diff)
92
94
 
93
95
 
96
+ # File extension → language id, mirroring VS Code's languageId values so CLI
97
+ # file changes render with the same language badge as VS Code sessions.
98
+ _EXTENSION_LANGUAGE_MAP: dict[str, str] = {
99
+ ".py": "python",
100
+ ".ts": "typescript",
101
+ ".tsx": "typescriptreact",
102
+ ".js": "javascript",
103
+ ".jsx": "javascriptreact",
104
+ ".mjs": "javascript",
105
+ ".cjs": "javascript",
106
+ ".json": "json",
107
+ ".jsonc": "jsonc",
108
+ ".html": "html",
109
+ ".htm": "html",
110
+ ".css": "css",
111
+ ".scss": "scss",
112
+ ".less": "less",
113
+ ".md": "markdown",
114
+ ".markdown": "markdown",
115
+ ".rs": "rust",
116
+ ".go": "go",
117
+ ".java": "java",
118
+ ".kt": "kotlin",
119
+ ".c": "c",
120
+ ".h": "c",
121
+ ".cpp": "cpp",
122
+ ".cc": "cpp",
123
+ ".hpp": "cpp",
124
+ ".cs": "csharp",
125
+ ".rb": "ruby",
126
+ ".php": "php",
127
+ ".sh": "shellscript",
128
+ ".bash": "shellscript",
129
+ ".ps1": "powershell",
130
+ ".psm1": "powershell",
131
+ ".yaml": "yaml",
132
+ ".yml": "yaml",
133
+ ".toml": "toml",
134
+ ".xml": "xml",
135
+ ".sql": "sql",
136
+ ".swift": "swift",
137
+ ".lua": "lua",
138
+ ".r": "r",
139
+ ".dart": "dart",
140
+ ".vue": "vue",
141
+ ".svelte": "svelte",
142
+ }
143
+
144
+
145
+ def language_id_from_path(path: str) -> str | None:
146
+ """Infer a VS Code-style ``languageId`` from a file path's extension."""
147
+ if not path:
148
+ return None
149
+ return _EXTENSION_LANGUAGE_MAP.get(Path(path).suffix.lower())
150
+
151
+
152
+ @dataclass
153
+ class CliFileOp:
154
+ """A single CLI ``edit``/``create`` operation captured from tool arguments."""
155
+
156
+ tool_name: str
157
+ path: str
158
+ old_str: str | None = None
159
+ new_str: str | None = None
160
+ file_text: str | None = None
161
+
162
+
163
+ def cli_file_op_from_tool(tool_name: str, arguments: dict) -> CliFileOp | None:
164
+ """Extract a :class:`CliFileOp` from a successful ``edit``/``create`` call."""
165
+ path = arguments.get("path")
166
+ if not isinstance(path, str) or not path:
167
+ return None
168
+ if tool_name == "create":
169
+ file_text = arguments.get("file_text")
170
+ if not isinstance(file_text, str):
171
+ return None
172
+ return CliFileOp("create", path, file_text=file_text)
173
+ if tool_name == "edit":
174
+ old_str = arguments.get("old_str")
175
+ new_str = arguments.get("new_str")
176
+ if not isinstance(old_str, str) or not isinstance(new_str, str):
177
+ return None
178
+ return CliFileOp("edit", path, old_str=old_str, new_str=new_str)
179
+ return None
180
+
181
+
182
+ def strip_view_line_numbers(text: str) -> str:
183
+ """Strip the CLI ``view`` tool's ``N. `` prefixes, preserving blank lines and EOL."""
184
+ return "".join(re.sub(r"^[ \t]*\d+\.[ \t]?", "", line) for line in text.splitlines(keepends=True))
185
+
186
+
187
+ def _diff_body(original: str, modified: str) -> str:
188
+ """Unified-diff hunks without the ``---``/``+++`` file header lines."""
189
+ full = _generate_unified_diff(original, modified, "file")
190
+ return "".join(line for line in full.splitlines(keepends=True) if not line.startswith(("--- ", "+++ ")))
191
+
192
+
193
+ def _consolidate_one(path: str, ops: list[CliFileOp], base: str | None = None) -> tuple[FileChange, str | None]:
194
+ """Collapse one file's ops within a turn into a single :class:`FileChange`.
195
+
196
+ Returns ``(change, net_content)`` where *net_content* is the reconstructed
197
+ final file content (or ``None`` when not reconstructable) so callers can
198
+ advance the base for subsequent turns.
199
+ """
200
+ filename = Path(path).name or "file"
201
+ language_id = language_id_from_path(path)
202
+
203
+ if len(ops) == 1 and ops[0].tool_name == "create":
204
+ op = ops[0]
205
+ diff = _generate_unified_diff("", op.file_text or "", filename)
206
+ return FileChange(path=path, diff=diff or None, content=op.file_text, language_id=language_id), op.file_text
207
+
208
+ current: str | None = base
209
+ base_content: str | None = base
210
+ reconstructable = base is not None
211
+ for op in ops:
212
+ if op.tool_name == "create":
213
+ current = op.file_text or ""
214
+ if base_content is None:
215
+ base_content = ""
216
+ reconstructable = True
217
+ elif current is not None and (op.old_str or "") in current:
218
+ current = current.replace(op.old_str or "", op.new_str or "", 1)
219
+ else:
220
+ reconstructable = False
221
+ break
222
+ if reconstructable and base_content is not None and current is not None:
223
+ diff = _generate_unified_diff(base_content, current, filename)
224
+ content = current if base_content == "" else None
225
+ return FileChange(path=path, diff=diff or None, content=content, language_id=language_id), current
226
+
227
+ # Fallback: stack each op's hunks (create as all-additions) under one header.
228
+ header = f"--- a/{filename}\n+++ b/{filename}\n"
229
+ bodies = [_diff_body("", op.file_text or "") if op.tool_name == "create" else _diff_body(op.old_str or "", op.new_str or "") for op in ops]
230
+ diff = header + "".join(b for b in bodies if b)
231
+ return FileChange(path=path, diff=diff or None, content=None, language_id=language_id), None
232
+
233
+
234
+ def consolidate_cli_file_ops(ops: list[CliFileOp], bases: dict[str, str] | None = None) -> list[FileChange]:
235
+ """Group CLI file ops by path (first-occurrence order) into one card each.
236
+
237
+ Multiple edits to the same file within a turn collapse into a single
238
+ :class:`FileChange`. *bases* maps a path to a prior full-file read; on a
239
+ successful reconstruction the entry is advanced in place so later turns
240
+ diff against current content instead of stale views.
241
+ """
242
+ bases = bases if bases is not None else {}
243
+ grouped: dict[str, list[CliFileOp]] = {}
244
+ for op in ops:
245
+ grouped.setdefault(op.path, []).append(op)
246
+ out = []
247
+ for path, group in grouped.items():
248
+ change, net_content = _consolidate_one(path, group, bases.get(path))
249
+ if net_content is not None:
250
+ bases[path] = net_content
251
+ out.append(change)
252
+ return out
253
+
254
+
94
255
  def _format_edits_as_diff(edits: list[list[dict]], original_content: str | None = None, filename: str = "file") -> str | None:
95
256
  """Format textEditGroup edits as a diff-style representation.
96
257
 
@@ -4,8 +4,19 @@ Provides normalizations that ensure both scanners produce compatible output
4
4
  for the same logical concepts (status values, command runs, invocation messages).
5
5
  """
6
6
 
7
+ import re
8
+
7
9
  from .models import CommandRun
8
10
 
11
+ _ANSI_ESCAPE_PATTERN = re.compile(r"\x1b(?:\[[0-9;]*[A-Za-z]|\][^\x07]*\x07|\][^\x1b]*\x1b\\)")
12
+ _CONTROL_CHARACTER_PATTERN = re.compile(r"[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]")
13
+
14
+
15
+ def strip_ansi_control_sequences(text: str) -> str:
16
+ """Remove terminal escape sequences and non-printing control characters."""
17
+ return _CONTROL_CHARACTER_PATTERN.sub("", _ANSI_ESCAPE_PATTERN.sub("", text)).strip()
18
+
19
+
9
20
  # ---------------------------------------------------------------------------
10
21
  # Status normalisation (Issue #55 — D1)
11
22
  # ---------------------------------------------------------------------------
@@ -2,6 +2,7 @@
2
2
 
3
3
  import sqlite3
4
4
  from pathlib import Path
5
+ from typing import cast
5
6
 
6
7
  import ssrjson
7
8
 
@@ -29,6 +30,7 @@ from .shared import (
29
30
  extract_command_run,
30
31
  normalize_invocation_message,
31
32
  normalize_tool_status,
33
+ strip_ansi_control_sequences,
32
34
  )
33
35
 
34
36
  RawBlock = (
@@ -56,6 +58,90 @@ RawBlock = (
56
58
  )
57
59
 
58
60
 
61
+ def _response_text(value: object) -> str:
62
+ """Extract sanitized text from a VS Code string or MarkdownString."""
63
+ if isinstance(value, str):
64
+ return strip_ansi_control_sequences(value)
65
+ if isinstance(value, dict):
66
+ nested = cast("dict[str, object]", value).get("value")
67
+ if isinstance(nested, str):
68
+ return strip_ansi_control_sequences(nested)
69
+ return ""
70
+
71
+
72
+ def _format_question_carousel(item: dict) -> str:
73
+ parts = []
74
+ message = _response_text(item.get("message"))
75
+ if message:
76
+ parts.append(message)
77
+
78
+ answers = item.get("data")
79
+ answer_map = answers if isinstance(answers, dict) else {}
80
+ questions = item.get("questions")
81
+ if not isinstance(questions, list):
82
+ questions = []
83
+
84
+ for index, question in enumerate(questions, start=1):
85
+ if not isinstance(question, dict):
86
+ continue
87
+ title = _response_text(question.get("title")) or f"Question {index}"
88
+ prompt = _response_text(question.get("message"))
89
+ line = f"{index}. **{title}**"
90
+ if prompt and prompt != title:
91
+ line += f": {prompt}"
92
+ options = question.get("options")
93
+ if isinstance(options, list):
94
+ labels = [_response_text(option.get("label")) for option in options if isinstance(option, dict)]
95
+ labels = [label for label in labels if label]
96
+ if labels:
97
+ line += f"\n Options: {', '.join(labels)}"
98
+ question_id = question.get("id")
99
+ if isinstance(question_id, str) and question_id in answer_map:
100
+ answer = answer_map[question_id]
101
+ if isinstance(answer, dict):
102
+ answer = answer.get("selectedValue") or answer.get("selectedValues") or answer.get("freeformValue")
103
+ if isinstance(answer, list):
104
+ answer = ", ".join(str(value) for value in answer)
105
+ if answer not in (None, ""):
106
+ line += f"\n Answer: {answer}"
107
+ parts.append(line)
108
+
109
+ if item.get("isUsed") and not answer_map:
110
+ parts.append("Answered")
111
+ return "\n\n".join(parts)
112
+
113
+
114
+ def _format_interactive_response(item: dict) -> str:
115
+ parts = []
116
+ title = _response_text(item.get("title"))
117
+ message = _response_text(item.get("message"))
118
+ content = _response_text(item.get("content"))
119
+ if title:
120
+ parts.append(f"**{title}**")
121
+ if message:
122
+ parts.append(message)
123
+ if content:
124
+ parts.append(content)
125
+
126
+ buttons = item.get("buttons")
127
+ if isinstance(buttons, list):
128
+ labels = [str(button) for button in buttons if button]
129
+ if labels:
130
+ parts.append(f"Options: {', '.join(labels)}")
131
+
132
+ state = _response_text(item.get("state"))
133
+ data = item.get("data")
134
+ if state:
135
+ parts.append(f"Response: {state}")
136
+ elif isinstance(data, dict):
137
+ action = _response_text(data.get("action") or data.get("actionId"))
138
+ if data.get("rejected"):
139
+ parts.append("Response: rejected")
140
+ elif action:
141
+ parts.append(f"Response: {action}")
142
+ return "\n\n".join(parts)
143
+
144
+
59
145
  def _parse_tool_invocation_serialized(item: dict) -> ToolInvocation | None:
60
146
  """Parse a single toolInvocationSerialized item from VS Code response.
61
147
 
@@ -397,6 +483,38 @@ def _process_response_items(
397
483
  response_content.append(inline_text)
398
484
  raw_blocks.append(("toolInvocation", inline_text, None))
399
485
 
486
+ elif kind == "questionCarousel":
487
+ content = _format_question_carousel(item)
488
+ if content:
489
+ response_content.append(content)
490
+ raw_blocks.append(("ask_user", content, "questions"))
491
+ elif kind in ("elicitationSerialized", "confirmation", "planReview"):
492
+ content = _format_interactive_response(item)
493
+ if content:
494
+ response_content.append(content)
495
+ raw_blocks.append(("ask_user", content, kind))
496
+ elif kind in ("progressMessage", "systemNotification", "warning", "info"):
497
+ content = _response_text(item.get("content"))
498
+ if content:
499
+ response_content.append(content)
500
+ raw_blocks.append(("status", content, kind))
501
+ elif kind == "hook":
502
+ hook_type = _response_text(item.get("hookType"))
503
+ message = _response_text(item.get("stopReason") or item.get("systemMessage"))
504
+ if message:
505
+ label = f"{hook_type}: {message}" if hook_type else message
506
+ response_content.append(label)
507
+ raw_blocks.append(("status", label, "hook"))
508
+ elif kind == "autoModeResolution":
509
+ resolved = item.get("resolved")
510
+ model = _response_text(resolved.get("name") or resolved.get("id")) if isinstance(resolved, dict) else ""
511
+ label = f"Auto selected {model}" if model else "Auto is selecting a model"
512
+ response_content.append(label)
513
+ raw_blocks.append(("status", label, "auto-mode"))
514
+ elif kind == "disabledClaudeHooks":
515
+ label = "Claude hooks are disabled"
516
+ response_content.append(label)
517
+ raw_blocks.append(("status", label, "warning"))
400
518
  # Extract text content with kind info
401
519
  elif item.get("value"):
402
520
  value = item["value"]