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.
- k0ntrol/__init__.py +1 -0
- k0ntrol/agents.py +998 -0
- k0ntrol/cli.py +190 -0
- k0ntrol/config.py +330 -0
- k0ntrol/credentials.py +78 -0
- k0ntrol/defaults/config.yaml +23 -0
- k0ntrol/events.py +838 -0
- k0ntrol/frontends/__init__.py +1 -0
- k0ntrol/frontends/ask.py +218 -0
- k0ntrol/frontends/badges.py +228 -0
- k0ntrol/frontends/banner.py +128 -0
- k0ntrol/frontends/diffview.py +174 -0
- k0ntrol/frontends/editor.py +701 -0
- k0ntrol/frontends/klog.html +740 -0
- k0ntrol/frontends/klog.py +403 -0
- k0ntrol/frontends/logo.py +12 -0
- k0ntrol/frontends/mentions.py +29 -0
- k0ntrol/frontends/prompt.py +340 -0
- k0ntrol/frontends/render.py +485 -0
- k0ntrol/frontends/session.py +717 -0
- k0ntrol/frontends/setup.py +442 -0
- k0ntrol/frontends/terminal.py +396 -0
- k0ntrol/frontends/theme.py +24 -0
- k0ntrol/helpers.py +108 -0
- k0ntrol/init_flow.py +362 -0
- k0ntrol/langfuse.py +173 -0
- k0ntrol/memory/__init__.py +0 -0
- k0ntrol/memory/index.py +235 -0
- k0ntrol/memory/layout.py +66 -0
- k0ntrol/memory/python_extract.py +189 -0
- k0ntrol/memory/reflect.py +366 -0
- k0ntrol/memory/refresh.py +21 -0
- k0ntrol/memory/scan.py +538 -0
- k0ntrol/memory/session.py +145 -0
- k0ntrol/otel.py +429 -0
- k0ntrol/pricing.py +29 -0
- k0ntrol/prompts/chatty.md +17 -0
- k0ntrol/prompts/implementer.md +14 -0
- k0ntrol/prompts/planner.md +42 -0
- k0ntrol/prompts/researcher.md +20 -0
- k0ntrol/prompts/reviewer.md +34 -0
- k0ntrol/providers.py +718 -0
- k0ntrol/search.py +133 -0
- k0ntrol/tools/__init__.py +0 -0
- k0ntrol/tools/bulk_read.py +93 -0
- k0ntrol/tools/errors.py +50 -0
- k0ntrol/tools/fetch_url.py +212 -0
- k0ntrol/tools/net.py +317 -0
- k0ntrol/tools/path_guard.py +45 -0
- k0ntrol/tools/read.py +174 -0
- k0ntrol/tools/run_tests.py +66 -0
- k0ntrol/tools/runner.py +177 -0
- k0ntrol/tools/search_memory.py +32 -0
- k0ntrol/tools/slice_guard.py +166 -0
- k0ntrol/tools/slice_journal.py +481 -0
- k0ntrol/tools/tracker.py +115 -0
- k0ntrol/tools/web_search.py +199 -0
- k0ntrol/tools/write.py +123 -0
- k0ntrol/tracker/__init__.py +31 -0
- k0ntrol/tracker/github.py +371 -0
- k0ntrol/tracker/local_files.py +241 -0
- k0ntrol/tracker/protocol.py +210 -0
- k0ntrol/workflow/__init__.py +29 -0
- k0ntrol/workflow/change.py +605 -0
- k0ntrol/workflow/ci_gate.py +96 -0
- k0ntrol/workflow/context.py +137 -0
- k0ntrol/workflow/implement.py +1150 -0
- k0ntrol/workflow/nodes.py +331 -0
- k0ntrol/workflow/plan.py +675 -0
- k0ntrol/workflow/qa.py +98 -0
- k0ntrol/workflow/research.py +533 -0
- k0ntrol/workflow/routers.py +273 -0
- k0ntrol/workflow/run.py +1165 -0
- k0ntrol/workflow/state.py +146 -0
- k0ntrol/workflow/triage.py +269 -0
- k0ntrol_cli-0.1.0.dist-info/METADATA +154 -0
- k0ntrol_cli-0.1.0.dist-info/RECORD +80 -0
- k0ntrol_cli-0.1.0.dist-info/WHEEL +4 -0
- k0ntrol_cli-0.1.0.dist-info/entry_points.txt +2 -0
- k0ntrol_cli-0.1.0.dist-info/licenses/LICENSE +201 -0
k0ntrol/workflow/plan.py
ADDED
|
@@ -0,0 +1,675 @@
|
|
|
1
|
+
"""The ``/plan`` graph: the Q&A definition's rounds (#120), the
|
|
2
|
+
question-limit gate (#127), the spec gate (#121), then the ticket
|
|
3
|
+
gate (#122).
|
|
4
|
+
|
|
5
|
+
Each round is a fresh PLANNER run given the idea and the questions and
|
|
6
|
+
answers so far. It returns at most ``max_round_questions`` numbered
|
|
7
|
+
questions — each with its recommended answer — shown at the round gate;
|
|
8
|
+
the human answers in one line and the next round sees every earlier
|
|
9
|
+
question and answer. A round with no questions ends the Q&A: a fresh
|
|
10
|
+
PLANNER run then drafts the Spec — its own brief carrying every retained
|
|
11
|
+
round — and the spec gate takes ``ok`` or ``redo <feedback>``.
|
|
12
|
+
|
|
13
|
+
A round that would pass ``max_questions`` is never asked: the run pauses
|
|
14
|
+
at the question-limit gate with the open questions shown — ``end``
|
|
15
|
+
drafts the spec now (they land under Not yet decided), ``handoff``
|
|
16
|
+
charts them to a Map for ``/research``, ``continue`` grants ten more
|
|
17
|
+
questions.
|
|
18
|
+
|
|
19
|
+
An approved spec is cut into tickets by one more fresh PLANNER run,
|
|
20
|
+
sized by ``ticket_size``. The harness prepends the first ticket in
|
|
21
|
+
code — it records the spec's glossary and ADR changes and blocks every
|
|
22
|
+
other ticket (PLANNER has no write tools) — and the ticket gate lists
|
|
23
|
+
each ticket's title, blockers, deliverable and criteria count, flagging
|
|
24
|
+
any over its size's count; it takes ``ok`` or ``redo <feedback>``.
|
|
25
|
+
Publishing the spec and its tickets is #123.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
from collections.abc import Callable
|
|
29
|
+
|
|
30
|
+
from langgraph.checkpoint.base import BaseCheckpointSaver
|
|
31
|
+
from langgraph.graph import END, START, StateGraph
|
|
32
|
+
from langgraph.graph.state import CompiledStateGraph
|
|
33
|
+
from langgraph.types import interrupt
|
|
34
|
+
|
|
35
|
+
from k0ntrol.agents import Spec, TicketList
|
|
36
|
+
from k0ntrol.workflow.context import RunContext
|
|
37
|
+
from k0ntrol.workflow.nodes import (
|
|
38
|
+
_attempt_limit,
|
|
39
|
+
_invoke_counted,
|
|
40
|
+
_last_output,
|
|
41
|
+
make_limit_hit,
|
|
42
|
+
)
|
|
43
|
+
from k0ntrol.workflow.qa import (
|
|
44
|
+
DEFAULT_MAX_ROUND_QUESTIONS,
|
|
45
|
+
_answered_rounds,
|
|
46
|
+
_cap,
|
|
47
|
+
_questions,
|
|
48
|
+
_round_detail,
|
|
49
|
+
make_round_gate,
|
|
50
|
+
)
|
|
51
|
+
from k0ntrol.workflow.research import (
|
|
52
|
+
_BLANK,
|
|
53
|
+
_bullets,
|
|
54
|
+
_prose,
|
|
55
|
+
make_publish_decisions,
|
|
56
|
+
make_publish_map,
|
|
57
|
+
)
|
|
58
|
+
from k0ntrol.workflow.routers import (
|
|
59
|
+
_exhausted,
|
|
60
|
+
_max_questions,
|
|
61
|
+
_questions_asked,
|
|
62
|
+
route_after_plan_spec,
|
|
63
|
+
route_after_planner,
|
|
64
|
+
route_after_tickets,
|
|
65
|
+
route_limit_hit,
|
|
66
|
+
route_plan_spec_gate,
|
|
67
|
+
route_plan_start,
|
|
68
|
+
route_question_limit,
|
|
69
|
+
route_ticket_gate,
|
|
70
|
+
)
|
|
71
|
+
from k0ntrol.workflow.state import RunState
|
|
72
|
+
|
|
73
|
+
# The questions a ``continue`` at the question-limit gate grants (#127).
|
|
74
|
+
_CONTINUE_QUESTIONS = 10
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def build_plan_graph(
|
|
78
|
+
ctx: RunContext, checkpointer: BaseCheckpointSaver | None
|
|
79
|
+
) -> CompiledStateGraph:
|
|
80
|
+
"""Compile the ``/plan`` graph for one run context."""
|
|
81
|
+
graph = StateGraph(RunState)
|
|
82
|
+
graph.add_node("planner", make_planner_node(ctx))
|
|
83
|
+
graph.add_node("round_gate", make_round_gate(ctx))
|
|
84
|
+
graph.add_node("question_limit", make_question_limit(ctx))
|
|
85
|
+
graph.add_node("spec", make_spec_node(ctx))
|
|
86
|
+
graph.add_node("spec_gate", make_spec_gate(ctx))
|
|
87
|
+
graph.add_node("tickets", make_tickets_node(ctx))
|
|
88
|
+
graph.add_node("ticket_gate", make_ticket_gate(ctx))
|
|
89
|
+
graph.add_node("publish_map", make_publish_map(ctx))
|
|
90
|
+
graph.add_node("publish_decisions", make_publish_decisions(ctx))
|
|
91
|
+
graph.add_node("publish_spec", make_publish_spec(ctx))
|
|
92
|
+
graph.add_node("publish_tickets", make_publish_tickets(ctx))
|
|
93
|
+
graph.add_node("limit_hit", make_limit_hit(ctx))
|
|
94
|
+
graph.add_conditional_edges(START, route_plan_start)
|
|
95
|
+
graph.add_conditional_edges("planner", route_after_planner)
|
|
96
|
+
graph.add_edge("round_gate", "planner")
|
|
97
|
+
graph.add_conditional_edges("question_limit", route_question_limit)
|
|
98
|
+
graph.add_conditional_edges("spec", route_after_plan_spec)
|
|
99
|
+
graph.add_conditional_edges("spec_gate", route_plan_spec_gate)
|
|
100
|
+
graph.add_conditional_edges("tickets", route_after_tickets)
|
|
101
|
+
graph.add_conditional_edges("ticket_gate", route_ticket_gate)
|
|
102
|
+
graph.add_edge("publish_map", "publish_decisions")
|
|
103
|
+
graph.add_edge("publish_decisions", END)
|
|
104
|
+
graph.add_edge("publish_spec", "publish_tickets")
|
|
105
|
+
graph.add_edge("publish_tickets", END)
|
|
106
|
+
graph.add_conditional_edges("limit_hit", route_limit_hit)
|
|
107
|
+
return graph.compile(checkpointer=checkpointer)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def make_planner_node(
|
|
111
|
+
ctx: RunContext, brief: Callable[[dict], str] | None = None
|
|
112
|
+
) -> Callable[[dict], dict]:
|
|
113
|
+
"""One round of the Q&A: a fresh PLANNER run for the next questions.
|
|
114
|
+
|
|
115
|
+
``brief`` is the run kind's round brief — ``planner_brief`` for
|
|
116
|
+
``/plan``; ``/triage`` passes its own (#128). A limited run never
|
|
117
|
+
reaches the gate and never uses up an attempt; a round with no
|
|
118
|
+
questions ends the Q&A — the spec draft follows.
|
|
119
|
+
"""
|
|
120
|
+
|
|
121
|
+
def node(state: dict) -> dict:
|
|
122
|
+
result, updates = _invoke_counted(
|
|
123
|
+
ctx, "planner", state, brief or planner_brief
|
|
124
|
+
)
|
|
125
|
+
if result.limit_hit:
|
|
126
|
+
return updates
|
|
127
|
+
updates["questions"] = _questions(result, _cap(state))
|
|
128
|
+
return updates
|
|
129
|
+
|
|
130
|
+
return node
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
# --- the question-limit gate (#127) -----------------------------------------------
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def make_question_limit(ctx: RunContext) -> Callable[[dict], dict]:
|
|
137
|
+
"""The question-limit gate: the round that would pass
|
|
138
|
+
``max_questions`` is never asked and stays in ``questions`` as the
|
|
139
|
+
open questions. ``end`` → the spec draft, which lists them under
|
|
140
|
+
Not yet decided; ``handoff`` → the Map they chart for ``/research``;
|
|
141
|
+
``continue`` → the pending round, with ten more questions granted.
|
|
142
|
+
"""
|
|
143
|
+
|
|
144
|
+
def node(state: dict) -> dict:
|
|
145
|
+
verb = _limit_decision(ctx, state)
|
|
146
|
+
if verb == "handoff":
|
|
147
|
+
return {
|
|
148
|
+
"question_limit_route": "publish_map",
|
|
149
|
+
"map": _handoff_map(state),
|
|
150
|
+
}
|
|
151
|
+
if verb == "continue":
|
|
152
|
+
return {
|
|
153
|
+
"question_limit_route": "round_gate",
|
|
154
|
+
"max_questions": (
|
|
155
|
+
_questions_asked(state) + _CONTINUE_QUESTIONS
|
|
156
|
+
),
|
|
157
|
+
}
|
|
158
|
+
return {"question_limit_route": "spec"}
|
|
159
|
+
|
|
160
|
+
return node
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _limit_decision(ctx: RunContext, state: dict) -> str:
|
|
164
|
+
"""The gate's verb, re-asking when a trackerless resume cannot publish
|
|
165
|
+
the Map a ``handoff`` answer would need."""
|
|
166
|
+
decision = interrupt(_limit_payload(ctx, state))
|
|
167
|
+
verb = str(decision).strip().partition(" ")[0].lower()
|
|
168
|
+
while verb == "handoff" and ctx.tracker is None:
|
|
169
|
+
decision = interrupt(_limit_payload(ctx, state))
|
|
170
|
+
verb = str(decision).strip().partition(" ")[0].lower()
|
|
171
|
+
return verb
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _limit_payload(ctx: RunContext, state: dict) -> dict:
|
|
175
|
+
"""The gate's interrupt payload: the open questions, and the choices
|
|
176
|
+
the run can take — the hand off needs a tracker to publish the Map."""
|
|
177
|
+
options = [("end", "end the plan as is"), ("continue", "continue for 10 more")]
|
|
178
|
+
if ctx.tracker is not None:
|
|
179
|
+
options.insert(1, ("handoff", "hand off to research"))
|
|
180
|
+
return {
|
|
181
|
+
"gate": "question_limit",
|
|
182
|
+
"question": (
|
|
183
|
+
f"the Q&A reached the question limit "
|
|
184
|
+
f"({_questions_asked(state)}/{_max_questions(state)}) — "
|
|
185
|
+
f"{_either_line([label for _, label in options])}?"
|
|
186
|
+
),
|
|
187
|
+
"answers": [verb for verb, _ in options],
|
|
188
|
+
"detail": _round_detail(state.get("questions", [])),
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def _either_line(options: list[str]) -> str:
|
|
193
|
+
if len(options) == 2:
|
|
194
|
+
return " or ".join(options)
|
|
195
|
+
return f"{options[0]}, {options[1]} or {options[2]}"
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def _handoff_map(state: dict) -> dict:
|
|
199
|
+
"""The Map the hand off publishes, built in code: one Q&A decision
|
|
200
|
+
ticket per open question — the questions PLANNER still meant to ask
|
|
201
|
+
the human (spec #102, story 30)."""
|
|
202
|
+
idea = " ".join(_idea(state).split())
|
|
203
|
+
source = state.get("item_title") or idea
|
|
204
|
+
return {
|
|
205
|
+
"title": f"Open questions: {source[:72]}",
|
|
206
|
+
"destination": (
|
|
207
|
+
"decide each open question so the plan's spec can be drafted"
|
|
208
|
+
),
|
|
209
|
+
"notes": f"Charted at a /plan Q&A's question limit.\n\nIdea: {idea}",
|
|
210
|
+
"decisions": [
|
|
211
|
+
{"question": q["question"], "type": "Q&A", "blocked_by": []}
|
|
212
|
+
for q in state.get("questions", [])
|
|
213
|
+
],
|
|
214
|
+
"not_yet_specified": [],
|
|
215
|
+
"out_of_scope": [],
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
# --- the spec gate (#121) -------------------------------------------------------
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def make_spec_node(ctx: RunContext) -> Callable[[dict], dict]:
|
|
223
|
+
"""The spec draft: a fresh PLANNER run given the idea and every
|
|
224
|
+
answered round — ``spec_brief``, never the round's brief, so an early
|
|
225
|
+
Spec output during the Q&A is re-drafted here (#121).
|
|
226
|
+
|
|
227
|
+
A run that answers with another structured output drafts again while
|
|
228
|
+
attempts remain, then the limit pause — the same resumable path a
|
|
229
|
+
model-call limit takes (``limit_node="spec"`` names this node).
|
|
230
|
+
"""
|
|
231
|
+
|
|
232
|
+
def node(state: dict) -> dict:
|
|
233
|
+
result, updates = _invoke_counted(
|
|
234
|
+
ctx,
|
|
235
|
+
"planner",
|
|
236
|
+
state,
|
|
237
|
+
spec_brief,
|
|
238
|
+
attempts_key="spec_attempts",
|
|
239
|
+
limit_node="spec",
|
|
240
|
+
)
|
|
241
|
+
if result.limit_hit:
|
|
242
|
+
return updates
|
|
243
|
+
draft = result.structured_response
|
|
244
|
+
if isinstance(draft, Spec):
|
|
245
|
+
updates["spec"] = _drafted_spec(draft, state)
|
|
246
|
+
return updates
|
|
247
|
+
merged = {**state, **updates}
|
|
248
|
+
if _exhausted(merged, "spec_attempts"):
|
|
249
|
+
updates["limit"] = _attempt_limit(
|
|
250
|
+
merged, "spec", "spec", _last_output(result)
|
|
251
|
+
)
|
|
252
|
+
return updates
|
|
253
|
+
|
|
254
|
+
return node
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def make_spec_gate(ctx: RunContext) -> Callable[[dict], dict]:
|
|
258
|
+
"""The spec gate: the draft in its format, ``ok`` or ``redo
|
|
259
|
+
<feedback>``. ``ok`` moves on to the ticket breakdown (#122).
|
|
260
|
+
``redo`` rejects the draft: the next PLANNER run gets the
|
|
261
|
+
feedback."""
|
|
262
|
+
|
|
263
|
+
def node(state: dict) -> dict:
|
|
264
|
+
decision = interrupt(
|
|
265
|
+
{
|
|
266
|
+
"gate": "spec",
|
|
267
|
+
"question": "approve this spec?",
|
|
268
|
+
"answers": ["ok", "redo"],
|
|
269
|
+
"detail": render_spec(state.get("spec")),
|
|
270
|
+
}
|
|
271
|
+
)
|
|
272
|
+
word, _, feedback = str(decision).strip().partition(" ")
|
|
273
|
+
if word.lower() == "ok":
|
|
274
|
+
return {
|
|
275
|
+
"spec_approved": True,
|
|
276
|
+
"spec_feedback": None,
|
|
277
|
+
}
|
|
278
|
+
return {
|
|
279
|
+
"spec": None,
|
|
280
|
+
"spec_approved": False,
|
|
281
|
+
"spec_attempts": 0,
|
|
282
|
+
"spec_feedback": feedback.strip(),
|
|
283
|
+
"spec_rejections": [
|
|
284
|
+
*state.get("spec_rejections", []), feedback.strip()
|
|
285
|
+
],
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return node
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def _drafted_spec(draft: Spec, state: dict) -> dict:
|
|
292
|
+
"""The draft as a state dict: open questions folded in, and on a
|
|
293
|
+
decided Map the map's ref, so the spec it publishes links the map
|
|
294
|
+
it came from (#126)."""
|
|
295
|
+
spec = _with_open_questions(
|
|
296
|
+
draft.model_dump(), state.get("questions", [])
|
|
297
|
+
)
|
|
298
|
+
if state.get("decided_map"):
|
|
299
|
+
spec["map_ref"] = state["item_ref"]
|
|
300
|
+
return spec
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def _with_open_questions(spec: dict, questions: list[dict]) -> dict:
|
|
304
|
+
"""Fold the questions the limit left unasked into Not yet decided —
|
|
305
|
+
in code, so ending as is never silently drops one (#127)."""
|
|
306
|
+
if not questions:
|
|
307
|
+
return spec
|
|
308
|
+
undecided = list(spec.get("not_yet_decided", []))
|
|
309
|
+
undecided += [
|
|
310
|
+
q["question"] for q in questions if q["question"] not in undecided
|
|
311
|
+
]
|
|
312
|
+
return {**spec, "not_yet_decided": undecided}
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def render_spec(spec: dict | None) -> str:
|
|
316
|
+
"""The draft as the gate shows it — every section of the spec format
|
|
317
|
+
(#102); the same rendering becomes the published body in #123."""
|
|
318
|
+
if not spec:
|
|
319
|
+
return "(PLANNER produced no spec)"
|
|
320
|
+
sections = [f"**Title:** {spec.get('title', '')}"]
|
|
321
|
+
if spec.get("map_ref"):
|
|
322
|
+
sections.append(f"**Map:** #{spec['map_ref']}")
|
|
323
|
+
return "\n\n".join(
|
|
324
|
+
[
|
|
325
|
+
*sections,
|
|
326
|
+
_prose("Problem Statement", spec.get("problem_statement", "")),
|
|
327
|
+
_prose("Solution", spec.get("solution", "")),
|
|
328
|
+
_bullets("User Stories", spec.get("user_stories", [])),
|
|
329
|
+
_bullets(
|
|
330
|
+
"Implementation Decisions", spec.get("implementation_decisions", [])
|
|
331
|
+
),
|
|
332
|
+
_testing(spec),
|
|
333
|
+
_bullets("Glossary and decisions", spec.get("glossary_and_decisions", [])),
|
|
334
|
+
_bullets("Not yet decided", spec.get("not_yet_decided", [])),
|
|
335
|
+
_bullets("Out of Scope", spec.get("out_of_scope", [])),
|
|
336
|
+
]
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def _testing(spec: dict) -> str:
|
|
341
|
+
"""Testing Decisions with the seams listed under them."""
|
|
342
|
+
decisions = "\n".join(f"- {item}" for item in spec.get("testing_decisions", []))
|
|
343
|
+
seams = "\n".join(f"- {item}" for item in spec.get("seams", []))
|
|
344
|
+
return f"## Testing Decisions\n\n{decisions or _BLANK}\n\nSeams:\n{seams or _BLANK}"
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
# --- the ticket gate (#122) -------------------------------------------------------
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
#: A ``ticket_size``'s acceptance-criteria cap — small also means one
|
|
351
|
+
#: seam; the gate flags a ticket over its size's count (#122).
|
|
352
|
+
_TICKET_SIZE_CRITERIA = {"small": 3, "medium": 5, "large": 8}
|
|
353
|
+
_TICKET_SIZE_RULE = {
|
|
354
|
+
"small": "one seam and at most 3 acceptance criteria",
|
|
355
|
+
"medium": "at most 5 acceptance criteria",
|
|
356
|
+
"large": "at most 8 acceptance criteria",
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def make_tickets_node(ctx: RunContext) -> Callable[[dict], dict]:
|
|
361
|
+
"""The ticket breakdown: a fresh PLANNER run cuts the approved spec,
|
|
362
|
+
sized by ``ticket_size``. The first ticket is prepended in code —
|
|
363
|
+
the glossary-and-ADR ticket, blocking every other.
|
|
364
|
+
|
|
365
|
+
A run that answers with another structured output cuts again while
|
|
366
|
+
attempts remain, then the limit pause — the same resumable path the
|
|
367
|
+
spec draft takes (``limit_node="tickets"`` names this node).
|
|
368
|
+
"""
|
|
369
|
+
|
|
370
|
+
def node(state: dict) -> dict:
|
|
371
|
+
result, updates = _invoke_counted(
|
|
372
|
+
ctx,
|
|
373
|
+
"planner",
|
|
374
|
+
state,
|
|
375
|
+
tickets_brief,
|
|
376
|
+
attempts_key="tickets_attempts",
|
|
377
|
+
limit_node="tickets",
|
|
378
|
+
)
|
|
379
|
+
if result.limit_hit:
|
|
380
|
+
return updates
|
|
381
|
+
cut = result.structured_response
|
|
382
|
+
if isinstance(cut, TicketList):
|
|
383
|
+
updates["tickets"] = _with_glossary_ticket(
|
|
384
|
+
cut, state.get("spec") or {}
|
|
385
|
+
)
|
|
386
|
+
return updates
|
|
387
|
+
return _uncut(result, updates, state)
|
|
388
|
+
|
|
389
|
+
return node
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
def _uncut(result: object, updates: dict, state: dict) -> dict:
|
|
393
|
+
"""No TicketList this run: cut again while attempts remain, then
|
|
394
|
+
the attempts pause."""
|
|
395
|
+
merged = {**state, **updates}
|
|
396
|
+
if _exhausted(merged, "tickets_attempts"):
|
|
397
|
+
updates["limit"] = _attempt_limit(
|
|
398
|
+
merged, "tickets", "tickets", _last_output(result)
|
|
399
|
+
)
|
|
400
|
+
return updates
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def _with_glossary_ticket(cut: TicketList, spec: dict) -> list[dict]:
|
|
404
|
+
"""PLANNER's tickets after the code-made first ticket: its indexes
|
|
405
|
+
shift one and the glossary ticket blocks each of them — blockers
|
|
406
|
+
still only point backwards (#122)."""
|
|
407
|
+
tickets = [ticket.model_dump() for ticket in cut.tickets]
|
|
408
|
+
for ticket in tickets:
|
|
409
|
+
ticket["blocked_by"] = sorted(
|
|
410
|
+
{0, *(blocker + 1 for blocker in ticket["blocked_by"])}
|
|
411
|
+
)
|
|
412
|
+
return [_glossary_ticket(spec), *tickets]
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
def _glossary_ticket(spec: dict) -> dict:
|
|
416
|
+
"""The spec's first ticket, made in code: record the plan's glossary
|
|
417
|
+
and ADR changes (PLANNER has no write tools) (#122)."""
|
|
418
|
+
entries = spec.get("glossary_and_decisions") or []
|
|
419
|
+
lines = "\n".join(f"- {entry}" for entry in entries) or "- none"
|
|
420
|
+
return {
|
|
421
|
+
"title": "Record the spec's glossary and ADR changes",
|
|
422
|
+
"what_to_build": (
|
|
423
|
+
"record the plan's glossary entries in GLOSSARY.md and its "
|
|
424
|
+
"decisions as ADRs under docs/adr/:\n" + lines
|
|
425
|
+
),
|
|
426
|
+
"seams": ["GLOSSARY.md", "docs/adr/"],
|
|
427
|
+
"acceptance_criteria": [
|
|
428
|
+
"GLOSSARY.md and the ADRs record the spec's glossary and "
|
|
429
|
+
"decisions"
|
|
430
|
+
],
|
|
431
|
+
"blocked_by": [],
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
def make_ticket_gate(ctx: RunContext) -> Callable[[dict], dict]:
|
|
436
|
+
"""The ticket gate: every ticket's title, blockers, deliverable and
|
|
437
|
+
criteria count — a ticket over its ``ticket_size`` count flagged —
|
|
438
|
+
then ``ok`` or ``redo <feedback>``. ``ok`` publishes the spec and
|
|
439
|
+
its tickets (#123); ``redo`` recuts with the feedback."""
|
|
440
|
+
|
|
441
|
+
def node(state: dict) -> dict:
|
|
442
|
+
decision = interrupt(
|
|
443
|
+
{
|
|
444
|
+
"gate": "ticket",
|
|
445
|
+
"question": "approve these tickets?",
|
|
446
|
+
"answers": ["ok", "redo"],
|
|
447
|
+
"detail": render_tickets(
|
|
448
|
+
state.get("tickets") or [],
|
|
449
|
+
state.get("ticket_size", "small"),
|
|
450
|
+
),
|
|
451
|
+
}
|
|
452
|
+
)
|
|
453
|
+
word, _, feedback = str(decision).strip().partition(" ")
|
|
454
|
+
if word.lower() == "ok":
|
|
455
|
+
return {
|
|
456
|
+
"tickets_approved": True,
|
|
457
|
+
"ticket_feedback": None,
|
|
458
|
+
}
|
|
459
|
+
return {
|
|
460
|
+
"tickets": None,
|
|
461
|
+
"tickets_approved": False,
|
|
462
|
+
"tickets_attempts": 0,
|
|
463
|
+
"ticket_feedback": feedback.strip(),
|
|
464
|
+
"ticket_rejections": [
|
|
465
|
+
*state.get("ticket_rejections", []), feedback.strip()
|
|
466
|
+
],
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
return node
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
# --- publishing (#123) ---------------------------------------------------------------
|
|
473
|
+
|
|
474
|
+
READY_LABEL = "ready-for-agent"
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
def make_publish_spec(ctx: RunContext) -> Callable[[dict], dict]:
|
|
478
|
+
"""The Spec work item, marker-idempotent (carrier contract, #13):
|
|
479
|
+
``/plan <idea>`` opens it; ``/plan #<n>`` on a plain item updates
|
|
480
|
+
its plan — the spec posted on it — instead. A decided Map is never
|
|
481
|
+
the spec's parent: its children are decision tickets, so the spec
|
|
482
|
+
gets its own item that links the map (#126, #123). A trackerless
|
|
483
|
+
run has nothing to publish on and ends done."""
|
|
484
|
+
|
|
485
|
+
def node(state: dict) -> dict:
|
|
486
|
+
if ctx.tracker is None:
|
|
487
|
+
return {"outcome": "done"}
|
|
488
|
+
if state.get("item_ref") and not state.get("decided_map"):
|
|
489
|
+
ctx.tracker.comment_work_item(
|
|
490
|
+
state["item_ref"],
|
|
491
|
+
render_spec(state["spec"]),
|
|
492
|
+
run=ctx.run_id,
|
|
493
|
+
node="publish_spec",
|
|
494
|
+
)
|
|
495
|
+
return {}
|
|
496
|
+
item = ctx.tracker.ensure_work_item(
|
|
497
|
+
state["spec"]["title"],
|
|
498
|
+
render_spec(state["spec"]),
|
|
499
|
+
run=ctx.run_id,
|
|
500
|
+
node="publish_spec",
|
|
501
|
+
)
|
|
502
|
+
return {"item_ref": item.ref, "item_title": item.title}
|
|
503
|
+
|
|
504
|
+
return node
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
def make_publish_tickets(ctx: RunContext) -> Callable[[dict], dict]:
|
|
508
|
+
"""Each ticket as a marked child of the spec item, blockers first,
|
|
509
|
+
then its native blocker links and the ready-for-agent label — one
|
|
510
|
+
node marker per ticket so a resume recovers each on its own (#123)."""
|
|
511
|
+
|
|
512
|
+
def node(state: dict) -> dict:
|
|
513
|
+
if ctx.tracker is None:
|
|
514
|
+
return {"outcome": "done"}
|
|
515
|
+
ctx.tracker.ensure_label(READY_LABEL)
|
|
516
|
+
refs: list[str] = []
|
|
517
|
+
for index, ticket in enumerate(state["tickets"]):
|
|
518
|
+
refs.append(
|
|
519
|
+
_publish_ticket(ctx, state["item_ref"], ticket, index, refs)
|
|
520
|
+
)
|
|
521
|
+
return {"ticket_refs": refs, "outcome": "done"}
|
|
522
|
+
|
|
523
|
+
return node
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
def _publish_ticket(
|
|
527
|
+
ctx: RunContext, item_ref: str, ticket: dict, index: int, refs: list[str]
|
|
528
|
+
) -> str:
|
|
529
|
+
"""One ticket as a marked child of the spec item — blockers first —
|
|
530
|
+
then its native blocker links and the ready-for-agent label."""
|
|
531
|
+
child = ctx.tracker.create_child_work_item(
|
|
532
|
+
item_ref,
|
|
533
|
+
ticket["title"],
|
|
534
|
+
ticket_body(ticket, item_ref, refs),
|
|
535
|
+
run=ctx.run_id,
|
|
536
|
+
node=f"publish_ticket_{index}",
|
|
537
|
+
)
|
|
538
|
+
for blocker in ticket.get("blocked_by", []):
|
|
539
|
+
ctx.tracker.link_blocked_by(child.ref, refs[blocker])
|
|
540
|
+
ctx.tracker.add_label(child.ref, READY_LABEL)
|
|
541
|
+
return child.ref
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
def ticket_body(ticket: dict, item_ref: str, refs: list[str]) -> str:
|
|
545
|
+
"""A ticket's body — the one format tickets and agent briefs share:
|
|
546
|
+
what to build, seams, acceptance criteria, blocked by, and the
|
|
547
|
+
parent (ADR 0012, #123)."""
|
|
548
|
+
return "\n\n".join(
|
|
549
|
+
[
|
|
550
|
+
_prose("What to build", ticket["what_to_build"]),
|
|
551
|
+
_bullets("Seams", ticket.get("seams", [])),
|
|
552
|
+
_bullets(
|
|
553
|
+
"Acceptance criteria", ticket.get("acceptance_criteria", [])
|
|
554
|
+
),
|
|
555
|
+
_bullets(
|
|
556
|
+
"Blocked by",
|
|
557
|
+
[f"#{refs[i]}" for i in ticket.get("blocked_by", [])],
|
|
558
|
+
),
|
|
559
|
+
f"Parent: #{item_ref}",
|
|
560
|
+
]
|
|
561
|
+
)
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
def render_tickets(tickets: list[dict], ticket_size: str) -> str:
|
|
565
|
+
"""The breakdown as the gate shows it: title, deliverable, blockers
|
|
566
|
+
and criteria count per ticket, oversized ones flagged (#122)."""
|
|
567
|
+
cap = _TICKET_SIZE_CRITERIA.get(ticket_size, _TICKET_SIZE_CRITERIA["small"])
|
|
568
|
+
lines = [f"{len(tickets)} tickets (ticket_size: {ticket_size})"]
|
|
569
|
+
for index, ticket in enumerate(tickets):
|
|
570
|
+
count = len(ticket.get("acceptance_criteria", []))
|
|
571
|
+
flag = f" — over {ticket_size} ({cap} max)" if count > cap else ""
|
|
572
|
+
blockers = ", ".join(
|
|
573
|
+
str(blocker + 1) for blocker in ticket.get("blocked_by", [])
|
|
574
|
+
) or "—"
|
|
575
|
+
lines.append("")
|
|
576
|
+
lines.append(f"## {index + 1} · {ticket.get('title', '')}")
|
|
577
|
+
lines.append(f"Delivers: {ticket.get('what_to_build', '')}")
|
|
578
|
+
lines.append(f"Blocked by: {blockers}")
|
|
579
|
+
lines.append(f"Acceptance criteria: {count}{flag}")
|
|
580
|
+
lines.extend(f"- {c}" for c in ticket.get("acceptance_criteria", []))
|
|
581
|
+
return "\n".join(lines)
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
def tickets_brief(state: dict) -> str:
|
|
585
|
+
"""PLANNER's fresh context for the breakdown: the approved spec, the
|
|
586
|
+
``ticket_size`` rule, and on a gate ``redo`` the human's feedback.
|
|
587
|
+
|
|
588
|
+
The first ticket is not PLANNER's to write — the harness prepends
|
|
589
|
+
the glossary-and-ADR ticket, so a ``blocked_by`` index only ever
|
|
590
|
+
names an earlier ticket in PLANNER's own list.
|
|
591
|
+
"""
|
|
592
|
+
size = state.get("ticket_size", "small")
|
|
593
|
+
lines = [
|
|
594
|
+
f"Idea: {_idea(state)}",
|
|
595
|
+
"",
|
|
596
|
+
"The spec below is approved — cut it into tickets: answer with "
|
|
597
|
+
f"TicketList, blockers first. Size each ticket {size}: "
|
|
598
|
+
f"{_TICKET_SIZE_RULE[size]}.",
|
|
599
|
+
"",
|
|
600
|
+
"The first ticket is not yours to write: the harness adds the "
|
|
601
|
+
"glossary-and-ADR ticket itself and makes it block every ticket "
|
|
602
|
+
"you cut — a blocked_by index only names an earlier ticket in "
|
|
603
|
+
"your own list.",
|
|
604
|
+
"",
|
|
605
|
+
render_spec(state.get("spec")),
|
|
606
|
+
]
|
|
607
|
+
if state.get("ticket_feedback"):
|
|
608
|
+
lines.append(
|
|
609
|
+
"\nYour last breakdown was rejected with this feedback: "
|
|
610
|
+
f"{state['ticket_feedback']}"
|
|
611
|
+
)
|
|
612
|
+
return "\n".join(lines)
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
def planner_brief(state: dict) -> str:
|
|
616
|
+
"""PLANNER's fresh context for one round: the idea, then every earlier
|
|
617
|
+
round's questions and the line the human answered."""
|
|
618
|
+
lines = [
|
|
619
|
+
f"Idea: {_idea(state)}",
|
|
620
|
+
"",
|
|
621
|
+
f"Ask the next round of questions — at most {_cap(state)}, each "
|
|
622
|
+
"numbered with your recommended answer — or an empty QuestionRound "
|
|
623
|
+
"when nothing is left to decide.",
|
|
624
|
+
]
|
|
625
|
+
lines.extend(_answered_rounds(state))
|
|
626
|
+
return "\n".join(lines)
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
def _spec_lead(state: dict) -> str:
|
|
630
|
+
"""The brief's lead: a decided Map drafts straight from its
|
|
631
|
+
destination and decisions, no Q&A having run (#126)."""
|
|
632
|
+
if state.get("decided_map"):
|
|
633
|
+
return (
|
|
634
|
+
"This map's decision tickets are all decided — draft the "
|
|
635
|
+
"spec from its destination and decisions"
|
|
636
|
+
)
|
|
637
|
+
return "The Q&A is over — draft the spec"
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
def spec_brief(state: dict) -> str:
|
|
641
|
+
"""PLANNER's fresh context for the spec draft: the idea and every
|
|
642
|
+
answered round — or, on a decided Map, the map's destination and
|
|
643
|
+
decided tickets, no Q&A having run (#126) — and on a gate ``redo``
|
|
644
|
+
the human's feedback."""
|
|
645
|
+
lines = [
|
|
646
|
+
f"Idea: {_idea(state)}",
|
|
647
|
+
"",
|
|
648
|
+
f"{_spec_lead(state)}: answer with Spec — the title, "
|
|
649
|
+
"problem statement, solution, user stories, implementation "
|
|
650
|
+
"decisions, testing decisions with the seams, glossary and "
|
|
651
|
+
"decisions, not yet decided, and out of scope.",
|
|
652
|
+
]
|
|
653
|
+
lines.extend(_answered_rounds(state))
|
|
654
|
+
open_ = state.get("questions") or []
|
|
655
|
+
if open_:
|
|
656
|
+
lines.append(
|
|
657
|
+
"\nThe question limit left these questions unasked — they "
|
|
658
|
+
"are not yet decided:"
|
|
659
|
+
)
|
|
660
|
+
lines.extend(f" - {question['question']}" for question in open_)
|
|
661
|
+
if state.get("spec_feedback"):
|
|
662
|
+
lines.append(
|
|
663
|
+
"\nYour last draft was rejected with this feedback: "
|
|
664
|
+
f"{state['spec_feedback']}"
|
|
665
|
+
)
|
|
666
|
+
return "\n".join(lines)
|
|
667
|
+
|
|
668
|
+
|
|
669
|
+
def _idea(state: dict) -> str:
|
|
670
|
+
"""The idea the Q&A defined: the task text, prefixed by its work
|
|
671
|
+
item when ``/plan #<n>`` carries one."""
|
|
672
|
+
idea = state["task"]
|
|
673
|
+
if state.get("item_ref"):
|
|
674
|
+
idea = f"#{state['item_ref']} {state.get('item_title', '')}\n\n{idea}"
|
|
675
|
+
return idea
|