1"""Detached backfill worker spawned by the session_start hook (BUG-1882).

2

3 Invoked as ``python -m little_loops.cli.backfill_worker <db_path> <path>``,

4 where *path* is either a single ``.jsonl`` transcript file or a project folder

5 whose ``*.jsonl`` files are globbed. Runs :func:`backfill_incremental` and

6 exits. Because it is spawned with ``start_new_session=True`` it outlives the

7 short-lived hook subprocess.

8"""

9

10 from __future__ import annotations

11

12 import sys

13 from pathlib import Path

14

15

16 def main(argv: list[str] | None = None) -> int:

17 args = argv if argv is not None else sys.argv[1:]

18 if len(args) < 2:

19 print(

20 f"Usage: {sys.argv[0]} <db_path> <jsonl_file_or_project_dir>",

21 file=sys.stderr,

22 )

23 return 1

24

25 db_path = Path(args[0])

26 path_arg = Path(args[1])

27

28 if path_arg.is_file() and args[1].endswith(".jsonl"):

29 jsonl_files: list[Path] = [path_arg]

30 elif path_arg.is_dir():

31 jsonl_files = list(path_arg.glob("*.jsonl"))

32 else:

33 print(f"backfill_worker: path not found: {args[1]!r}", file=sys.stderr)

34 return 1

35

36 from little_loops.session_store import backfill_incremental

37

38 backfill_incremental(db_path, jsonl_files=jsonl_files)

39 return 0

40

41

42 if __name__ == "__main__":

43 sys.exit(main())