k0ntrol-cli 0.1.0__py3-none-any.whl

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 (80) hide show
  1. k0ntrol/__init__.py +1 -0
  2. k0ntrol/agents.py +998 -0
  3. k0ntrol/cli.py +190 -0
  4. k0ntrol/config.py +330 -0
  5. k0ntrol/credentials.py +78 -0
  6. k0ntrol/defaults/config.yaml +23 -0
  7. k0ntrol/events.py +838 -0
  8. k0ntrol/frontends/__init__.py +1 -0
  9. k0ntrol/frontends/ask.py +218 -0
  10. k0ntrol/frontends/badges.py +228 -0
  11. k0ntrol/frontends/banner.py +128 -0
  12. k0ntrol/frontends/diffview.py +174 -0
  13. k0ntrol/frontends/editor.py +701 -0
  14. k0ntrol/frontends/klog.html +740 -0
  15. k0ntrol/frontends/klog.py +403 -0
  16. k0ntrol/frontends/logo.py +12 -0
  17. k0ntrol/frontends/mentions.py +29 -0
  18. k0ntrol/frontends/prompt.py +340 -0
  19. k0ntrol/frontends/render.py +485 -0
  20. k0ntrol/frontends/session.py +717 -0
  21. k0ntrol/frontends/setup.py +442 -0
  22. k0ntrol/frontends/terminal.py +396 -0
  23. k0ntrol/frontends/theme.py +24 -0
  24. k0ntrol/helpers.py +108 -0
  25. k0ntrol/init_flow.py +362 -0
  26. k0ntrol/langfuse.py +173 -0
  27. k0ntrol/memory/__init__.py +0 -0
  28. k0ntrol/memory/index.py +235 -0
  29. k0ntrol/memory/layout.py +66 -0
  30. k0ntrol/memory/python_extract.py +189 -0
  31. k0ntrol/memory/reflect.py +366 -0
  32. k0ntrol/memory/refresh.py +21 -0
  33. k0ntrol/memory/scan.py +538 -0
  34. k0ntrol/memory/session.py +145 -0
  35. k0ntrol/otel.py +429 -0
  36. k0ntrol/pricing.py +29 -0
  37. k0ntrol/prompts/chatty.md +17 -0
  38. k0ntrol/prompts/implementer.md +14 -0
  39. k0ntrol/prompts/planner.md +42 -0
  40. k0ntrol/prompts/researcher.md +20 -0
  41. k0ntrol/prompts/reviewer.md +34 -0
  42. k0ntrol/providers.py +718 -0
  43. k0ntrol/search.py +133 -0
  44. k0ntrol/tools/__init__.py +0 -0
  45. k0ntrol/tools/bulk_read.py +93 -0
  46. k0ntrol/tools/errors.py +50 -0
  47. k0ntrol/tools/fetch_url.py +212 -0
  48. k0ntrol/tools/net.py +317 -0
  49. k0ntrol/tools/path_guard.py +45 -0
  50. k0ntrol/tools/read.py +174 -0
  51. k0ntrol/tools/run_tests.py +66 -0
  52. k0ntrol/tools/runner.py +177 -0
  53. k0ntrol/tools/search_memory.py +32 -0
  54. k0ntrol/tools/slice_guard.py +166 -0
  55. k0ntrol/tools/slice_journal.py +481 -0
  56. k0ntrol/tools/tracker.py +115 -0
  57. k0ntrol/tools/web_search.py +199 -0
  58. k0ntrol/tools/write.py +123 -0
  59. k0ntrol/tracker/__init__.py +31 -0
  60. k0ntrol/tracker/github.py +371 -0
  61. k0ntrol/tracker/local_files.py +241 -0
  62. k0ntrol/tracker/protocol.py +210 -0
  63. k0ntrol/workflow/__init__.py +29 -0
  64. k0ntrol/workflow/change.py +605 -0
  65. k0ntrol/workflow/ci_gate.py +96 -0
  66. k0ntrol/workflow/context.py +137 -0
  67. k0ntrol/workflow/implement.py +1150 -0
  68. k0ntrol/workflow/nodes.py +331 -0
  69. k0ntrol/workflow/plan.py +675 -0
  70. k0ntrol/workflow/qa.py +98 -0
  71. k0ntrol/workflow/research.py +533 -0
  72. k0ntrol/workflow/routers.py +273 -0
  73. k0ntrol/workflow/run.py +1165 -0
  74. k0ntrol/workflow/state.py +146 -0
  75. k0ntrol/workflow/triage.py +269 -0
  76. k0ntrol_cli-0.1.0.dist-info/METADATA +154 -0
  77. k0ntrol_cli-0.1.0.dist-info/RECORD +80 -0
  78. k0ntrol_cli-0.1.0.dist-info/WHEEL +4 -0
  79. k0ntrol_cli-0.1.0.dist-info/entry_points.txt +2 -0
  80. k0ntrol_cli-0.1.0.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,174 @@
1
+ """A git diff drawn for a human gate, one panel per file.
2
+
3
+ A file that only gains lines (or only loses them) shows one column; a file
4
+ whose existing code changed shows old and new side by side, each removed
5
+ run paired with the added run that replaced it.
6
+ """
7
+
8
+ import re
9
+ from dataclasses import dataclass
10
+ from itertools import groupby, zip_longest
11
+
12
+ from rich.box import ROUNDED
13
+ from rich.console import Group, RenderableType
14
+ from rich.panel import Panel
15
+ from rich.table import Table
16
+ from rich.text import Text
17
+
18
+ from k0ntrol.frontends.theme import BORDER, DIM, ERROR, OK
19
+
20
+ _DIFF_MAX_LINES = 400
21
+ _HUNK = re.compile(r"^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@")
22
+ _FILE_START = re.compile(r"(?m)^(?=diff --git )")
23
+ _PATH_PREFIXES = ("+++ b/", "--- a/") # both six characters long
24
+ _MARKS = {" ": (" ", ""), "-": ("- ", ERROR), "+": ("+ ", OK), "⋯": ("⋯", DIM)}
25
+
26
+
27
+ @dataclass(frozen=True)
28
+ class Line:
29
+ """One diff line: kind ``" "``, ``"-"``, ``"+"`` or ``"⋯"`` (hunk break)."""
30
+
31
+ kind: str
32
+ text: str
33
+ old: int | None
34
+ new: int | None
35
+
36
+
37
+ _BREAK = Line("⋯", "", None, None)
38
+
39
+
40
+ @dataclass(frozen=True)
41
+ class FileDiff:
42
+ path: str
43
+ lines: tuple[Line, ...]
44
+
45
+ def count(self, kind: str) -> int:
46
+ return sum(1 for line in self.lines if line.kind == kind)
47
+
48
+ @property
49
+ def one_sided(self) -> bool:
50
+ """Only additions or only removals: nothing to put side by side."""
51
+ return not (self.count("+") and self.count("-"))
52
+
53
+
54
+ class _Numbering:
55
+ """Old and new line numbers as a hunk is read top to bottom."""
56
+
57
+ def __init__(self) -> None:
58
+ self.old = self.new = 0
59
+
60
+ def read(self, raw: str) -> Line:
61
+ match = _HUNK.match(raw)
62
+ if match:
63
+ self.old, self.new = int(match[1]), int(match[2])
64
+ return _BREAK
65
+ kind = raw[:1] if raw[:1] in ("-", "+") else " "
66
+ line = Line(kind, raw[1:], None if kind == "+" else self.old, None if kind == "-" else self.new)
67
+ self.old += kind != "+"
68
+ self.new += kind != "-"
69
+ return line
70
+
71
+
72
+ def parse_diff(diff: str) -> list[FileDiff]:
73
+ """A ``git diff`` split into files, each with numbered lines."""
74
+ chunks = _FILE_START.split(diff)
75
+ return [_parse_file(chunk) for chunk in chunks if chunk.startswith("diff --git ")]
76
+
77
+
78
+ def _parse_file(chunk: str) -> FileDiff:
79
+ raw = chunk.splitlines()
80
+ start = next((i for i, line in enumerate(raw) if line.startswith("@@")), len(raw))
81
+ numbering = _Numbering()
82
+ lines = [numbering.read(line) for line in raw[start:] if not line.startswith("\\")]
83
+ return FileDiff(_path(raw[:start]), tuple(lines[1:])) # [1:]: no break before the first hunk
84
+
85
+
86
+ def _path(header: list[str]) -> str:
87
+ """``+++ b/<path>``, else ``--- a/<path>`` (deleted), else the git line."""
88
+ marked = [line[6:] for line in header if line.startswith(_PATH_PREFIXES)]
89
+ return marked[-1] if marked else header[0].rpartition(" b/")[2]
90
+
91
+
92
+ def side_by_side(lines: tuple[Line, ...]) -> list[tuple[Line | None, Line | None]]:
93
+ """``(old, new)`` rows: context on both sides, removed runs beside the
94
+ added runs that replaced them, a blank where one side is shorter."""
95
+ rows: list[tuple[Line | None, Line | None]] = []
96
+ for changed, run in groupby(lines, key=lambda line: line.kind in ("-", "+")):
97
+ block = list(run)
98
+ rows += _paired(block) if changed else [(line, line) for line in block]
99
+ return rows
100
+
101
+
102
+ def _paired(block: list[Line]) -> list[tuple[Line | None, Line | None]]:
103
+ removed = [line for line in block if line.kind == "-"]
104
+ added = [line for line in block if line.kind == "+"]
105
+ return list(zip_longest(removed, added))
106
+
107
+
108
+ # --- rendering --------------------------------------------------------------
109
+
110
+
111
+ def render_diff(diff: str) -> RenderableType:
112
+ """Every file of ``diff`` as a panel, cut at ``_DIFF_MAX_LINES`` lines."""
113
+ lines = diff.splitlines()
114
+ shown = "\n".join(lines[:_DIFF_MAX_LINES])
115
+ parts: list[RenderableType] = [_file_panel(file) for file in parse_diff(shown)]
116
+ hidden = len(lines) - _DIFF_MAX_LINES
117
+ if hidden > 0:
118
+ parts.append(Text(f"… {hidden} more diff lines", style=DIM))
119
+ return Group(*parts)
120
+
121
+
122
+ def _file_panel(file: FileDiff) -> Panel:
123
+ title = Text.assemble(
124
+ (file.path, "bold default"), (f" +{file.count('+')}", OK), (f" -{file.count('-')}", ERROR)
125
+ )
126
+ return Panel(
127
+ _file_body(file),
128
+ box=ROUNDED,
129
+ border_style=BORDER,
130
+ title=title,
131
+ title_align="left",
132
+ padding=(0, 1),
133
+ )
134
+
135
+
136
+ def _file_body(file: FileDiff) -> RenderableType:
137
+ if not file.lines:
138
+ return Text("no text changes", style=DIM)
139
+ return _one_column(file) if file.one_sided else _two_columns(file)
140
+
141
+
142
+ def _one_column(file: FileDiff) -> Table:
143
+ side = "new" if file.count("+") else "old"
144
+ grid = _grid(columns=1)
145
+ for line in file.lines:
146
+ grid.add_row(*_cell(line, side))
147
+ return grid
148
+
149
+
150
+ def _two_columns(file: FileDiff) -> Table:
151
+ grid = _grid(columns=2)
152
+ for old, new in side_by_side(file.lines):
153
+ grid.add_row(*_cell(old, "old"), Text("│", style=BORDER), *_cell(new, "new"))
154
+ return grid
155
+
156
+
157
+ def _grid(*, columns: int) -> Table:
158
+ """A number column and a text column per side, a rule between sides."""
159
+ grid = Table.grid(padding=(0, 1), expand=True)
160
+ for index in range(columns):
161
+ if index:
162
+ grid.add_column() # the rule between old and new
163
+ grid.add_column(justify="right")
164
+ grid.add_column(ratio=1, overflow="fold")
165
+ return grid
166
+
167
+
168
+ def _cell(line: Line | None, side: str) -> tuple[Text, Text]:
169
+ if line is None:
170
+ return Text(""), Text("")
171
+ number = line.old if side == "old" else line.new
172
+ sign, style = _MARKS[line.kind]
173
+ shown = "" if number is None else str(number)
174
+ return Text(shown, style=DIM), Text(sign + line.text, style=style)