agentilda 2.0.1 → 2.0.4
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.
- checksums.yaml +4 -4
- data/lib/agentilda/agents/agent.rb +79 -0
- data/lib/agentilda/agents/registry.rb +121 -0
- data/lib/agentilda/agents/roster.rb +92 -0
- data/lib/agentilda/agents.rb +11 -113
- data/lib/agentilda/cli/agents/subcommands/describe.rb +3 -3
- data/lib/agentilda/cli/agents/subcommands/list.rb +1 -1
- data/lib/agentilda/cli/base.rb +3 -3
- data/lib/agentilda/cli/create/create.rb +17 -18
- data/lib/agentilda/cli/docs/docs.rb +1 -1
- data/lib/agentilda/cli/index/index.rb +1 -1
- data/lib/agentilda/cli/linear/subcommands/import.rb +1 -1
- data/lib/agentilda/cli/list_plans/list_plans.rb +1 -1
- data/lib/agentilda/cli/mail/mail.rb +4 -4
- data/lib/agentilda/cli/mail/subcommands/send.rb +1 -1
- data/lib/agentilda/cli/resync/subcommands/dirs.rb +2 -2
- data/lib/agentilda/cli/resync/subcommands/prs.rb +3 -3
- data/lib/agentilda/cli/run/run.rb +40 -40
- data/lib/agentilda/cli/states/states.rb +1 -1
- data/lib/agentilda/cli/unblock/unblock.rb +17 -17
- data/lib/agentilda/cli/worktree/worktree.rb +4 -4
- data/lib/agentilda/cli.rb +1 -1
- data/lib/agentilda/engine/board.rb +93 -0
- data/lib/agentilda/engine/dispatcher.rb +646 -0
- data/lib/agentilda/engine/progress_log.rb +125 -0
- data/lib/agentilda/engine/runner.rb +141 -0
- data/lib/agentilda/engine/state_file.rb +176 -0
- data/lib/agentilda/engine/tally.rb +177 -0
- data/lib/agentilda/engine.rb +19 -0
- data/lib/agentilda/execution/child.rb +94 -0
- data/lib/agentilda/execution/clock.rb +116 -0
- data/lib/agentilda/execution/control.rb +180 -0
- data/lib/agentilda/execution/executor.rb +699 -0
- data/lib/agentilda/execution/transcript.rb +468 -0
- data/lib/agentilda/execution.rb +18 -0
- data/lib/agentilda/lifecycle/adoption.rb +197 -0
- data/lib/agentilda/lifecycle/brief.rb +285 -0
- data/lib/agentilda/lifecycle/creator.rb +125 -0
- data/lib/agentilda/lifecycle/resync.rb +428 -0
- data/lib/agentilda/lifecycle/unblocker.rb +223 -0
- data/lib/agentilda/lifecycle.rb +28 -0
- data/lib/agentilda/linear/api.rb +1 -1
- data/lib/agentilda/linear/attribution.rb +7 -7
- data/lib/agentilda/linear/import.rb +18 -18
- data/lib/agentilda/linear/mapping.rb +6 -6
- data/lib/agentilda/linear/push.rb +5 -5
- data/lib/agentilda/linear/survey.rb +7 -7
- data/lib/agentilda/linear/unit.rb +5 -5
- data/lib/agentilda/plans/dev_work.rb +56 -0
- data/lib/agentilda/plans/feature.rb +131 -0
- data/lib/agentilda/plans/index.rb +208 -0
- data/lib/agentilda/plans/ledger.rb +235 -0
- data/lib/agentilda/plans/mailbox.rb +143 -0
- data/lib/agentilda/plans/ordinal.rb +92 -0
- data/lib/agentilda/plans/pull_request.rb +29 -0
- data/lib/agentilda/plans/pull_requests.rb +229 -0
- data/lib/agentilda/plans/state_machine.rb +383 -0
- data/lib/agentilda/plans/status.rb +365 -0
- data/lib/agentilda/plans/subject.rb +131 -0
- data/lib/agentilda/plans/tree.rb +79 -0
- data/lib/agentilda/plans.rb +39 -0
- data/lib/agentilda/presentation/console.rb +159 -0
- data/lib/agentilda/presentation/dashboard.rb +221 -0
- data/lib/agentilda/presentation/diagram.rb +149 -0
- data/lib/agentilda/presentation/documentation.rb +439 -0
- data/lib/agentilda/presentation/keyboard.rb +158 -0
- data/lib/agentilda/presentation/reporter.rb +181 -0
- data/lib/agentilda/presentation/screen/ratatui/bar.rb +59 -0
- data/lib/agentilda/presentation/screen/ratatui/key_translator.rb +31 -0
- data/lib/agentilda/presentation/screen/ratatui.rb +320 -0
- data/lib/agentilda/presentation/viewer.rb +62 -0
- data/lib/agentilda/presentation.rb +23 -0
- data/lib/agentilda/{ui.rb → support/ui.rb} +5 -5
- data/lib/agentilda/vcs/github.rb +170 -0
- data/lib/agentilda/vcs/publisher.rb +177 -0
- data/lib/agentilda/vcs/worktree.rb +209 -0
- data/lib/agentilda/vcs.rb +19 -0
- data/lib/agentilda/version.rb +1 -1
- data/lib/agentilda.rb +6 -5
- metadata +56 -49
- data/lib/agentilda/adoption.rb +0 -197
- data/lib/agentilda/agent.rb +0 -77
- data/lib/agentilda/board.rb +0 -91
- data/lib/agentilda/brief.rb +0 -283
- data/lib/agentilda/child.rb +0 -92
- data/lib/agentilda/clock.rb +0 -114
- data/lib/agentilda/console.rb +0 -157
- data/lib/agentilda/control.rb +0 -178
- data/lib/agentilda/creator.rb +0 -125
- data/lib/agentilda/dashboard.rb +0 -219
- data/lib/agentilda/dev_work.rb +0 -56
- data/lib/agentilda/diagram.rb +0 -149
- data/lib/agentilda/dispatcher.rb +0 -646
- data/lib/agentilda/documentation.rb +0 -437
- data/lib/agentilda/executor.rb +0 -697
- data/lib/agentilda/feature.rb +0 -132
- data/lib/agentilda/github.rb +0 -168
- data/lib/agentilda/index.rb +0 -206
- data/lib/agentilda/keyboard.rb +0 -156
- data/lib/agentilda/ledger.rb +0 -233
- data/lib/agentilda/mailbox.rb +0 -141
- data/lib/agentilda/ordinal.rb +0 -90
- data/lib/agentilda/progress_log.rb +0 -126
- data/lib/agentilda/publisher.rb +0 -175
- data/lib/agentilda/pull_request.rb +0 -27
- data/lib/agentilda/pull_requests.rb +0 -230
- data/lib/agentilda/reporter.rb +0 -181
- data/lib/agentilda/resync.rb +0 -426
- data/lib/agentilda/roster.rb +0 -93
- data/lib/agentilda/runner.rb +0 -139
- data/lib/agentilda/screen/ratatui/bar.rb +0 -57
- data/lib/agentilda/screen/ratatui/key_translator.rb +0 -29
- data/lib/agentilda/screen/ratatui.rb +0 -318
- data/lib/agentilda/state_file.rb +0 -174
- data/lib/agentilda/state_machine.rb +0 -383
- data/lib/agentilda/status.rb +0 -363
- data/lib/agentilda/subject.rb +0 -133
- data/lib/agentilda/tally.rb +0 -175
- data/lib/agentilda/transcript.rb +0 -466
- data/lib/agentilda/tree.rb +0 -81
- data/lib/agentilda/unblocker.rb +0 -221
- data/lib/agentilda/viewer.rb +0 -60
- data/lib/agentilda/worktree.rb +0 -207
- /data/lib/agentilda/{config.rb → support/config.rb} +0 -0
- /data/lib/agentilda/{frontmatter.rb → support/frontmatter.rb} +0 -0
- /data/lib/agentilda/{markdown.rb → support/markdown.rb} +0 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "tty/command"
|
|
4
|
+
|
|
5
|
+
module Agentilda
|
|
6
|
+
module Lifecycle
|
|
7
|
+
# A new plan's opening brief: four questions, and a best-effort first pass
|
|
8
|
+
# at answering them from what the project already has on disk.
|
|
9
|
+
#
|
|
10
|
+
# This is for work that does not exist yet. A plan created `--after` a
|
|
11
|
+
# shipped pull request already has its facts — {CLI::Create} hands those to
|
|
12
|
+
# `yoda-writer`, which reconstructs a specification from a diff, not this.
|
|
13
|
+
# Guessing at a diff and guessing at a feature nobody has built are
|
|
14
|
+
# different jobs, and conflating them is how a retroactive spec ends up
|
|
15
|
+
# full of hedges about something that already, provably, happened.
|
|
16
|
+
#
|
|
17
|
+
# The four headings are the brief `leah-researcher` and `yoda-writer` both
|
|
18
|
+
# read *before* their own chapters: what research needs to settle is a
|
|
19
|
+
# question about the gaps in `## What already exists`, so a scaffold
|
|
20
|
+
# missing that heading forces both of them to first reconstruct a boundary
|
|
21
|
+
# this class already had for free — it just created the folder.
|
|
22
|
+
class Brief
|
|
23
|
+
# The half-agent's name. It has no `agents/*.md` of its own on purpose —
|
|
24
|
+
# a roster entry would put it in `Runner`'s routing, and this pass runs
|
|
25
|
+
# only inside `create`. The name exists so the UI can show it the same
|
|
26
|
+
# way it shows `leah-researcher` and `yoda-writer` on their spinner lines.
|
|
27
|
+
AGENT_NAME = "anakin-briefster"
|
|
28
|
+
|
|
29
|
+
HEADINGS = [
|
|
30
|
+
"What we are trying to achieve",
|
|
31
|
+
"Why it matters",
|
|
32
|
+
"What already exists",
|
|
33
|
+
"What research needs to settle"
|
|
34
|
+
].freeze
|
|
35
|
+
|
|
36
|
+
# Read whole into the drafting prompt, in order, so the model sees the
|
|
37
|
+
# project's own account of itself before it sees the topic.
|
|
38
|
+
CONTEXT_FILES = %w[CLAUDE.md AGENTS.md README.md].freeze
|
|
39
|
+
|
|
40
|
+
# Relative to the repository root. Not every project keeps one — a
|
|
41
|
+
# missing backlog is silently skipped, not an error.
|
|
42
|
+
BACKLOG_FILE = File.join(Agentilda::PLANS_DIR, "BACKLOG.md")
|
|
43
|
+
|
|
44
|
+
# Long enough for a handful of Read/Grep/Glob calls over a local
|
|
45
|
+
# checkout; short enough that a hung `claude` does not stall `create`
|
|
46
|
+
# past the point where the scaffold is already there to open by hand.
|
|
47
|
+
TIMEOUT = 60
|
|
48
|
+
|
|
49
|
+
# The fastest model, and the draft is disposable: the prompt prefers an
|
|
50
|
+
# honest blank over a clever inference, so there is no reasoning premium
|
|
51
|
+
# worth paying a larger model for inside a 60-second budget.
|
|
52
|
+
BRIEF_MODEL = "haiku"
|
|
53
|
+
|
|
54
|
+
# Read-only, and no `Bash` — this pass surveys what the repository
|
|
55
|
+
# already says about itself, it does not go looking further. That is
|
|
56
|
+
# `leah-researcher`'s job, and denying her tools here is what keeps the
|
|
57
|
+
# two from quietly doing the same work twice.
|
|
58
|
+
ALLOWED_TOOLS = %w[Read Grep Glob Edit].freeze
|
|
59
|
+
DENIED_TOOLS = %w[WebFetch WebSearch Bash].freeze
|
|
60
|
+
|
|
61
|
+
# The `stdout:` and `stderr:` sections of a {TTY::Command::ExitError}
|
|
62
|
+
# message. stdout runs until stderr starts; stderr runs to the end, because
|
|
63
|
+
# what `claude` prints there is not guaranteed to be one line.
|
|
64
|
+
STDOUT_SECTION = /^[ \t]*stdout:[ \t]*(.*?)(?=\n[ \t]*stderr:|\z)/m
|
|
65
|
+
STDERR_SECTION = /^[ \t]*stderr:[ \t]*(.*)\z/m
|
|
66
|
+
|
|
67
|
+
# How much of what `claude` said survives into a one-line report.
|
|
68
|
+
REASON_LIMIT = 300
|
|
69
|
+
|
|
70
|
+
# A one-line account of a failed `claude` run, built from the error rather
|
|
71
|
+
# than its first line: that first line is the escaped prompt, several
|
|
72
|
+
# thousand characters of it, and reporting it says an invocation failed,
|
|
73
|
+
# at length, and never why.
|
|
74
|
+
#
|
|
75
|
+
# Both streams are kept because they carry different halves. `claude`
|
|
76
|
+
# reports its own failures on stdout; the line naming the *cause* of a 401
|
|
77
|
+
# (`ANTHROPIC_API_KEY ... takes precedence over your claude.ai login`) is
|
|
78
|
+
# on stderr. {Executor} used to own this; it now reads a child process
|
|
79
|
+
# directly and has no exit error to parse, so the briefer keeps it.
|
|
80
|
+
#
|
|
81
|
+
# @param error [TTY::Command::ExitError]
|
|
82
|
+
# @return [String]
|
|
83
|
+
def self.failure_reason(error)
|
|
84
|
+
status = error.message[/^[ \t]*exit status:[ \t]*(\S+)/, 1]
|
|
85
|
+
outcome = status ? "exited #{status}" : "failed"
|
|
86
|
+
said = [STDOUT_SECTION, STDERR_SECTION]
|
|
87
|
+
.filter_map { |section| tail(error.message[section, 1]) }
|
|
88
|
+
.join(" | ")
|
|
89
|
+
|
|
90
|
+
said.empty? ? "#{outcome} and said nothing" : "#{outcome}: #{said}"
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# @param text [String, nil]
|
|
94
|
+
# @return [String, nil] the last few meaningful lines, on one line
|
|
95
|
+
def self.tail(text)
|
|
96
|
+
lines = text.to_s.split("\n").map(&:strip).reject { |line| line.empty? || line == "Nothing written" }
|
|
97
|
+
return nil if lines.empty?
|
|
98
|
+
|
|
99
|
+
joined = lines.last(3).join(" ")
|
|
100
|
+
joined.length > REASON_LIMIT ? "#{joined[0, REASON_LIMIT - 1]}..." : joined
|
|
101
|
+
end
|
|
102
|
+
private_class_method :tail
|
|
103
|
+
|
|
104
|
+
# @param path [String] the new plan folder, absolute
|
|
105
|
+
# @param title [String] the feature's proper name, e.g. "Tax Rule DSL"
|
|
106
|
+
# @param root [String] repository root, for project context and `--add-dir`
|
|
107
|
+
# @param command [TTY::Command]
|
|
108
|
+
# @param timeout [Integer] seconds before the drafting attempt is abandoned
|
|
109
|
+
# @param dry_run [Boolean] plan the invocation, do not run it
|
|
110
|
+
# @param seed [String, nil] what the author already wrote about the
|
|
111
|
+
# feature (`create --from`); opens spec.md and outranks the repo survey
|
|
112
|
+
def initialize(
|
|
113
|
+
path:,
|
|
114
|
+
title:,
|
|
115
|
+
root:,
|
|
116
|
+
command: TTY::Command.new(printer: :null),
|
|
117
|
+
timeout: TIMEOUT,
|
|
118
|
+
dry_run: false,
|
|
119
|
+
seed: nil
|
|
120
|
+
)
|
|
121
|
+
@path = path
|
|
122
|
+
@title = title
|
|
123
|
+
@root = File.expand_path(root)
|
|
124
|
+
@command = command
|
|
125
|
+
@timeout = timeout
|
|
126
|
+
@dry_run = dry_run
|
|
127
|
+
@seed = seed.to_s.strip.then { |s| s.empty? ? nil : s }
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# @return [String]
|
|
131
|
+
attr_reader :path, :title, :root
|
|
132
|
+
|
|
133
|
+
# @return [String, nil]
|
|
134
|
+
attr_reader :seed
|
|
135
|
+
|
|
136
|
+
# @return [String] absolute path to the spec, whether or not it exists yet
|
|
137
|
+
def spec_path = File.join(path, "spec.md")
|
|
138
|
+
|
|
139
|
+
# The title and four empty headings — and, when `create --from` supplied
|
|
140
|
+
# one, the author's own prose between them, so the file already says
|
|
141
|
+
# something even if the drafting attempt never returns.
|
|
142
|
+
#
|
|
143
|
+
# @return [String]
|
|
144
|
+
def scaffold
|
|
145
|
+
opening = seed ? "#{seed}\n\n" : ""
|
|
146
|
+
"# #{title}\n\n#{opening}" + HEADINGS.map { |h| "## #{h}\n\n" }.join("\n")
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# @return [String] where it was written
|
|
150
|
+
def write_scaffold!
|
|
151
|
+
File.write(spec_path, scaffold)
|
|
152
|
+
spec_path
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# Make a best-effort attempt at the four headings, in place, by shelling
|
|
156
|
+
# out to `claude` the same way {Executor} does for a specialist agent.
|
|
157
|
+
# A failure here is not this method's to hide: the scaffold {#write_scaffold!}
|
|
158
|
+
# already wrote stands on its own, so the caller reports the failure and
|
|
159
|
+
# moves on rather than treating it as fatal to `create`.
|
|
160
|
+
#
|
|
161
|
+
# @return [Array(Boolean, String)] ok, and a one-line note
|
|
162
|
+
def attempt!
|
|
163
|
+
return [true, "dry run — would draft from project context"] if @dry_run
|
|
164
|
+
|
|
165
|
+
begin
|
|
166
|
+
@command.run(*invocation, timeout: @timeout)
|
|
167
|
+
rescue TTY::Command::TimeoutExceeded
|
|
168
|
+
return [false, "timed out after #{@timeout}s"]
|
|
169
|
+
rescue TTY::Command::ExitError => e
|
|
170
|
+
return [false, "claude #{self.class.failure_reason(e)}"]
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
[true, "drafted"]
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Exposed so a spec can assert the boundary without running anything.
|
|
177
|
+
#
|
|
178
|
+
# @return [Array<String>]
|
|
179
|
+
def invocation
|
|
180
|
+
["claude",
|
|
181
|
+
"-p", prompt, "--add-dir", root,
|
|
182
|
+
"--model", BRIEF_MODEL,
|
|
183
|
+
"--allowedTools", ALLOWED_TOOLS.join(","),
|
|
184
|
+
"--disallowedTools", DENIED_TOOLS.join(",")]
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
private
|
|
188
|
+
|
|
189
|
+
# @return [String]
|
|
190
|
+
def prompt
|
|
191
|
+
<<~PROMPT
|
|
192
|
+
#{author_statement}A new plan folder was just created for a feature that does not exist
|
|
193
|
+
yet: "#{title}".
|
|
194
|
+
|
|
195
|
+
You have 50 seconds. Explore the codebase for the first thirty and
|
|
196
|
+
write for the last twenty; the process is killed at sixty and an
|
|
197
|
+
unwritten draft is lost.
|
|
198
|
+
|
|
199
|
+
#{spec_path} holds a title and four empty headings:
|
|
200
|
+
|
|
201
|
+
#{HEADINGS.map { |h| " * #{h}" }.join("\n")}
|
|
202
|
+
|
|
203
|
+
Make a best-effort first pass at each, drawn only from what THIS
|
|
204
|
+
project already has on disk under #{root} — its own docs, its own
|
|
205
|
+
conventions, code already built, and anything already downloaded or
|
|
206
|
+
gathered that bears on "#{title}". Read/Grep/Glob freely to look for
|
|
207
|
+
it; do not invent facts and do not research the web. Where the
|
|
208
|
+
repository gives you nothing to go on, leave that heading's body as
|
|
209
|
+
a short, honest one-line note saying so, e.g. "_Nothing in the repo
|
|
210
|
+
speaks to this yet — needs research._" A human completes this
|
|
211
|
+
afterward, and a confident-sounding guess costs them more to unwind
|
|
212
|
+
than an honest blank.
|
|
213
|
+
|
|
214
|
+
`## What already exists` matters most: name specific files, modules
|
|
215
|
+
or prior work you found, not just that "some code exists" — that is
|
|
216
|
+
exactly the context a researcher picking this up next cannot see for
|
|
217
|
+
themselves without being told where to look.
|
|
218
|
+
|
|
219
|
+
#{context}
|
|
220
|
+
#{backlog}
|
|
221
|
+
Edit #{spec_path} in place. Keep the title#{", the author's opening prose" if seed} and the four `##`
|
|
222
|
+
headings exactly as they are, in order; add prose under them; write
|
|
223
|
+
nothing outside them; touch no other file.
|
|
224
|
+
PROMPT
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
# The author's own words lead the prompt, ahead of even the feature's
|
|
228
|
+
# announcement, because they outrank everything this pass could survey:
|
|
229
|
+
# where the seed and the repository disagree, the seed is the intent and
|
|
230
|
+
# the repository is merely the past.
|
|
231
|
+
#
|
|
232
|
+
# @return [String] empty without a seed
|
|
233
|
+
def author_statement
|
|
234
|
+
return "" unless seed
|
|
235
|
+
|
|
236
|
+
<<~STATEMENT
|
|
237
|
+
The author has already written the following about this feature. It
|
|
238
|
+
is the primary source: where it answers one of the headings, draw
|
|
239
|
+
from it first, and use the repository survey below only to supplement
|
|
240
|
+
it — never to contradict it.
|
|
241
|
+
|
|
242
|
+
#{seed}
|
|
243
|
+
|
|
244
|
+
STATEMENT
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# AGENTS.md is commonly a symlink to CLAUDE.md; realpath-deduping keeps
|
|
248
|
+
# the shared content from being inlined twice into every drafting prompt.
|
|
249
|
+
#
|
|
250
|
+
# @return [String]
|
|
251
|
+
def context
|
|
252
|
+
seen = []
|
|
253
|
+
found = CONTEXT_FILES.filter_map { |name|
|
|
254
|
+
file = File.join(root, name)
|
|
255
|
+
next unless File.file?(file)
|
|
256
|
+
|
|
257
|
+
real = File.realpath(file)
|
|
258
|
+
next if seen.include?(real)
|
|
259
|
+
|
|
260
|
+
seen << real
|
|
261
|
+
"### #{name}\n\n#{File.read(file, encoding: "UTF-8")}"
|
|
262
|
+
}
|
|
263
|
+
return "" if found.empty?
|
|
264
|
+
|
|
265
|
+
"## Project context\n\n#{found.join("\n\n")}\n"
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
# A relevant line in the project's own backlog is worth more than
|
|
269
|
+
# anything this pass could infer on its own — it is the one place "why
|
|
270
|
+
# now" might already be written down.
|
|
271
|
+
#
|
|
272
|
+
# @return [String]
|
|
273
|
+
def backlog
|
|
274
|
+
file = File.join(root, BACKLOG_FILE)
|
|
275
|
+
return "" unless File.file?(file)
|
|
276
|
+
|
|
277
|
+
"## #{BACKLOG_FILE}\n\n" \
|
|
278
|
+
"If this backlog mentions \"#{title}\", treat that entry as " \
|
|
279
|
+
"authoritative for `## Why it matters` — it is the project's own " \
|
|
280
|
+
"statement of intent.\n\n" \
|
|
281
|
+
"#{File.read(file, encoding: "UTF-8")}\n"
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "dry/monads"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
|
|
6
|
+
module Agentilda
|
|
7
|
+
module Lifecycle
|
|
8
|
+
# Mints new plan folders. Backs `/spec-create`.
|
|
9
|
+
#
|
|
10
|
+
# Every failure is a `Failure`, never an exception: creating a folder is the
|
|
11
|
+
# one place a typo becomes permanent, so the caller is made to look.
|
|
12
|
+
class Creator
|
|
13
|
+
include Dry::Monads[:result]
|
|
14
|
+
|
|
15
|
+
# The state a new plan opens in — a specification and nothing else yet.
|
|
16
|
+
DEFAULT_STATUS = :new
|
|
17
|
+
|
|
18
|
+
# The state a retroactive plan is born in: the work is live, but it has
|
|
19
|
+
# neither a specification nor a plan.
|
|
20
|
+
RETROACTIVE_STATUS = :retroactive
|
|
21
|
+
|
|
22
|
+
# @param dir [String] the `.plans` directory
|
|
23
|
+
def initialize(dir:)
|
|
24
|
+
@dir = File.expand_path(dir)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# @return [String] the `.plans` directory
|
|
28
|
+
attr_reader :dir
|
|
29
|
+
|
|
30
|
+
# Create a plan folder.
|
|
31
|
+
#
|
|
32
|
+
# @param words [Array<String>] the topic; becomes the slug
|
|
33
|
+
# @param after [String, nil] anchor for a retroactive plan, e.g. "002"
|
|
34
|
+
# @param status [String, Symbol, nil] override the default state
|
|
35
|
+
# @param prs [Array<Hash>, nil] pull requests to record, already fetched;
|
|
36
|
+
# their presence is what makes the folder 🕰️ Retroactive rather than ⚪️
|
|
37
|
+
# @return [Dry::Monads::Result] Success(absolute path) or Failure(message)
|
|
38
|
+
def create(words:, after: nil, status: nil, prs: nil)
|
|
39
|
+
resolved = resolve_status(status, after) or
|
|
40
|
+
return Failure("unknown status: #{status}")
|
|
41
|
+
|
|
42
|
+
slug = self.class.slugify(words)
|
|
43
|
+
return Failure("the topic produced an empty slug") if slug.empty?
|
|
44
|
+
|
|
45
|
+
ordinal = after ? retroactive_ordinal(after) : Plans::Ordinal.next_major(existing)
|
|
46
|
+
return ordinal if ordinal.is_a?(Dry::Monads::Result)
|
|
47
|
+
|
|
48
|
+
build(ordinal, resolved, slug).fmap { |path| record_pull_requests(path, prs) }
|
|
49
|
+
rescue Agentilda::Error => e
|
|
50
|
+
Failure(e.message)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# @return [Array<Agentilda::Plans::Ordinal>] every number already taken
|
|
54
|
+
def existing
|
|
55
|
+
@existing ||= begin
|
|
56
|
+
return [] unless File.directory?(dir)
|
|
57
|
+
|
|
58
|
+
Dir.children(dir)
|
|
59
|
+
.select { |c| File.directory?(File.join(dir, c)) }
|
|
60
|
+
.filter_map { |c| Plans::Ordinal.from_dirname(c) }
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Turn free text into the kebab tail of a folder name.
|
|
65
|
+
#
|
|
66
|
+
# @param words [Array<String>, String]
|
|
67
|
+
# @return [String] possibly empty, which the caller must reject
|
|
68
|
+
def self.slugify(words)
|
|
69
|
+
Array(words).join(" ").downcase.gsub(/[^a-z0-9]+/, "-").gsub(/\A-+|-+\z/, "")
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
private
|
|
73
|
+
|
|
74
|
+
# @param status [String, Symbol, nil]
|
|
75
|
+
# @param after [String, nil]
|
|
76
|
+
# @return [Agentilda::Plans::Status, nil]
|
|
77
|
+
def resolve_status(status, after)
|
|
78
|
+
return Agentilda::Plans.status(status) if status
|
|
79
|
+
|
|
80
|
+
Plans::STATUS_BY_KEY.fetch(after ? RETROACTIVE_STATUS : DEFAULT_STATUS)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# @param after [String]
|
|
84
|
+
# @return [Agentilda::Plans::Ordinal, Dry::Monads::Result]
|
|
85
|
+
def retroactive_ordinal(after)
|
|
86
|
+
anchor = Plans::Ordinal.parse(after) or return Failure("not a plan number: #{after}")
|
|
87
|
+
|
|
88
|
+
unless existing.any? { |o| o.major == anchor.major }
|
|
89
|
+
return Failure("no plan #{format("%03d", anchor.major)} to anchor against — " \
|
|
90
|
+
"a retroactive plan names the plan its work landed after")
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
Plans::Ordinal.next_minor(existing, major: anchor.major)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Write the pull requests the folder was created from, so 🕰️ Retroactive
|
|
97
|
+
# is justified the moment the folder exists — that state means "the work is
|
|
98
|
+
# live and undocumented", and it is the recorded pull requests that make
|
|
99
|
+
# the first half of that true.
|
|
100
|
+
#
|
|
101
|
+
# @param path [String] the new folder
|
|
102
|
+
# @param prs [Array<Hash>, nil]
|
|
103
|
+
# @return [String] the path, unchanged
|
|
104
|
+
def record_pull_requests(path, prs)
|
|
105
|
+
return path if prs.nil? || prs.empty?
|
|
106
|
+
|
|
107
|
+
File.write(File.join(path, Plans::PullRequests::FILENAME), Plans::PullRequests.render(prs))
|
|
108
|
+
path
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# @param ordinal [Agentilda::Plans::Ordinal]
|
|
112
|
+
# @param status [Agentilda::Plans::Status]
|
|
113
|
+
# @param slug [String]
|
|
114
|
+
# @return [Dry::Monads::Result]
|
|
115
|
+
def build(ordinal, status, slug)
|
|
116
|
+
target = File.join(dir, Agentilda::Plans.plan_dirname(ordinal, status, slug))
|
|
117
|
+
return Failure("already exists: #{File.basename(target)}") if File.exist?(target)
|
|
118
|
+
|
|
119
|
+
FileUtils.mkdir_p(target)
|
|
120
|
+
@existing = nil
|
|
121
|
+
Success(target)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|