ctxtree 0.2.0 → 0.2.1

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 (3) hide show
  1. package/README.md +9 -2
  2. package/ctxtree.py +20 -14
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -15,10 +15,17 @@ and opens the rest when it needs to. Nothing is summarized away: every part open
15
15
 
16
16
  ```sh
17
17
  pipx install ctxtree # recommended
18
- npx ctxtree # nothing to install; needs Node, and Python 3.8 or newer
18
+ npx ctxtree # nothing to install; needs Node, and Python 3.8 or newer (on Windows, use pipx)
19
19
  ```
20
20
 
21
- If you use uv, `uvx ctxtree` runs it without installing it. No pipx yet? `brew install pipx` on a Mac, `sudo apt install pipx` on Ubuntu or Debian, then `pipx ensurepath`. A plain
21
+ If you use uv, `uvx ctxtree` runs it without installing it. In Claude Code 2.1.275 or newer, the plugin adds the history skill, `/ctxtree:where` and the start-up hook, and
22
+ brings its own copy of ctxtree for Claude:
23
+
24
+ ```
25
+ /plugin install ctxtree --marketplace ctxtree/ctxtree
26
+ ```
27
+
28
+ No pipx yet? `brew install pipx` on a Mac, `sudo apt install pipx` on Ubuntu or Debian, then `pipx ensurepath`. A plain
22
29
  `pip install ctxtree` works inside a virtual environment; many systems (Ubuntu 24.04, Homebrew's Python) block it
23
30
  outside one. Step by step, with pictures: https://ctxtree.com/install/
24
31
 
package/ctxtree.py CHANGED
@@ -30,7 +30,7 @@ such as 30k. --level beats CTXTREE_LEVEL, which beats the level saved with `ctxt
30
30
  """
31
31
  import argparse, glob, json, os, re, shlex, shutil, subprocess, sys, time
32
32
 
33
- __version__ = "0.2.0"
33
+ __version__ = "0.2.1"
34
34
  from datetime import datetime
35
35
 
36
36
  ROOT = os.path.expanduser("~/.claude/projects")
@@ -53,7 +53,8 @@ def installed():
53
53
  return shutil.which("ctxtree", path=os.pathsep.join(dirs))
54
54
 
55
55
 
56
- FULL = "python3 " + HERE.replace(os.path.expanduser("~"), "~", 1)
56
+ # The command that runs this file: on Windows, python (python3 is often missing there) and the full path (no ~)
57
+ FULL = ("python \"%s\"" % HERE) if os.name == "nt" else "python3 " + HERE.replace(os.path.expanduser("~"), "~", 1)
57
58
  # How an agent runs this: plain ctxtree when it's on its PATH (installed, or the plugin's), else this file in full
58
59
  ME = "ctxtree" if (PLUGIN_BIN or installed()) and not NPX else FULL
59
60
  # How a person runs it again
@@ -160,7 +161,7 @@ def entries(path, codex=None):
160
161
  its lines, into the shape Claude Code writes, so everything else reads both; `codex` collects its id, model and
161
162
  window."""
162
163
  state = codex if codex is not None else {}
163
- with open(path, errors="replace") as f:
164
+ with open(path, encoding="utf-8", errors="replace") as f:
164
165
  for line_no, line in enumerate(f, 1):
165
166
  try:
166
167
  e = json.loads(line)
@@ -449,7 +450,7 @@ def level_in_force(flag=None):
449
450
  if os.environ.get("CTXTREE_LEVEL"):
450
451
  return os.environ["CTXTREE_LEVEL"]
451
452
  try:
452
- with open(config_path()) as f:
453
+ with open(config_path(), encoding="utf-8") as f:
453
454
  for line in f:
454
455
  key, _, value = line.partition("=")
455
456
  if key.strip() == "level" and value.strip():
@@ -726,7 +727,7 @@ def titles_file():
726
727
 
727
728
  def load_titles(sid):
728
729
  try:
729
- with open(titles_file()) as f:
730
+ with open(titles_file(), encoding="utf-8") as f:
730
731
  return json.load(f).get(sid, {})
731
732
  except (OSError, ValueError):
732
733
  return {}
@@ -758,13 +759,13 @@ def make_titles(s, run=None):
758
759
  got = json.loads(text[text.index("{"):text.rindex("}") + 1])
759
760
  new = {span(ep): one_line(str(got["e%d" % i]), 60) for i, ep in todo if got.get("e%d" % i)}
760
761
  try:
761
- with open(titles_file()) as f:
762
+ with open(titles_file(), encoding="utf-8") as f:
762
763
  everything = json.load(f)
763
764
  except (OSError, ValueError):
764
765
  everything = {}
765
766
  everything.setdefault(s.id, {}).update(new)
766
767
  os.makedirs(os.path.dirname(titles_file()), exist_ok=True)
767
- with open(titles_file(), "w") as f:
768
+ with open(titles_file(), "w", encoding="utf-8") as f:
768
769
  json.dump(everything, f)
769
770
  return everything[s.id], len(new), reply.get("usage") or {}
770
771
 
@@ -781,7 +782,7 @@ def start_tokens(folder):
781
782
  sizes = []
782
783
  for path in sorted(glob.glob(os.path.join(folder, "*.jsonl")), key=os.path.getmtime, reverse=True)[:12]:
783
784
  try:
784
- with open(path, errors="replace") as f:
785
+ with open(path, encoding="utf-8", errors="replace") as f:
785
786
  for line in f:
786
787
  if '"usage"' not in line:
787
788
  continue
@@ -806,7 +807,7 @@ def opening_tokens(path, level):
806
807
  size and level, so a session that has stopped changing is read in full once. The file keeps only numbers."""
807
808
  key, st = os.path.realpath(path), os.stat(path)
808
809
  try:
809
- with open(cache_file()) as f:
810
+ with open(cache_file(), encoding="utf-8") as f:
810
811
  known = json.load(f)
811
812
  except (OSError, ValueError):
812
813
  known = {}
@@ -817,7 +818,7 @@ def opening_tokens(path, level):
817
818
  known = {k: v for k, v in known.items() if os.path.exists(k)}
818
819
  known[key] = [st.st_size, int(st.st_mtime), level, tokens]
819
820
  os.makedirs(os.path.dirname(cache_file()), mode=0o700, exist_ok=True)
820
- with open(cache_file() + ".new", "w") as f:
821
+ with open(cache_file() + ".new", "w", encoding="utf-8") as f:
821
822
  json.dump(known, f)
822
823
  os.replace(cache_file() + ".new", cache_file())
823
824
  return tokens
@@ -923,7 +924,7 @@ def masked(text):
923
924
  def write_opening(s, tokens):
924
925
  """The opening message, in a file only you can read (it can be bigger than a command line holds)"""
925
926
  path = os.path.join(state_dir(), "opening-%s.md" % s.id[:8])
926
- with os.fdopen(os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600), "w") as f:
927
+ with os.fdopen(os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600), "w", encoding="utf-8") as f:
927
928
  f.write(masked(prime(s, tokens)[0]))
928
929
  os.chmod(path, 0o600)
929
930
  return path
@@ -948,12 +949,14 @@ def resume(s, tokens, dry=False, upto=None, level=None):
948
949
  return 0
949
950
  if not shutil.which("claude"):
950
951
  sys.exit("claude isn't on your PATH. The opening is in %s" % path)
951
- w = ways_back(s, level or "auto", opening=len(open(path).read()) // CHARS)
952
+ w = ways_back(s, level or "auto", opening=len(open(path, encoding="utf-8").read()) // CHARS)
952
953
  if s.window and w["fresh_usd"] is not None and w["back_usd"] is not None:
953
954
  print("Starting fresh: about %dk tokens, $%.2f at list price, against $%.2f to go back%s." % (
954
955
  w["fresh"] // 1000, w["fresh_usd"], w["back_usd"],
955
956
  " (it's still warm, so going back was the cheaper way)" if w["back_first"] else ""))
956
957
  os.chdir(folder)
958
+ if os.name == "nt": # Windows has no exec: run Claude Code as a child, and end when it ends
959
+ sys.exit(subprocess.call([shutil.which("claude"), ask]))
957
960
  os.execvp("claude", ["claude", ask])
958
961
 
959
962
 
@@ -1128,7 +1131,7 @@ def main(argv):
1128
1131
  except ValueError as e:
1129
1132
  sys.exit(str(e))
1130
1133
  os.makedirs(os.path.dirname(config_path()), exist_ok=True)
1131
- with open(config_path(), "w") as f:
1134
+ with open(config_path(), "w", encoding="utf-8") as f:
1132
1135
  f.write("# ctxtree: how much the opening message holds. auto, low, medium, high, xhigh, max or a "
1133
1136
  "size such as 30k.\nlevel = %s\n" % a.name)
1134
1137
  print(slider(a.name))
@@ -1349,7 +1352,7 @@ def report(days=7, root=None, now=None):
1349
1352
  if os.path.getmtime(path) < since:
1350
1353
  continue
1351
1354
  replies, order, folder, after_compact = {}, [], None, False
1352
- with open(path, errors="replace") as f:
1355
+ with open(path, encoding="utf-8", errors="replace") as f:
1353
1356
  for line in f:
1354
1357
  if '"assistant"' not in line and '"compact_boundary"' not in line:
1355
1358
  continue
@@ -1424,6 +1427,9 @@ def entry():
1424
1427
  import signal
1425
1428
  if hasattr(signal, "SIGPIPE"):
1426
1429
  signal.signal(signal.SIGPIPE, signal.SIG_DFL) # piped into head: stop quietly
1430
+ for stream in (sys.stdout, sys.stderr): # a console that can't show a character gets a ? instead of an error
1431
+ if hasattr(stream, "reconfigure"):
1432
+ stream.reconfigure(errors="replace")
1427
1433
  sys.exit(main(sys.argv[1:]))
1428
1434
 
1429
1435
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ctxtree",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Pick up a Claude Code session from a folded, lossless view of its history, instead of re-caching its whole window",
5
5
  "bin": {
6
6
  "ctxtree": "ctxtree.py"