@zalom/plastic 1.0.0-beta.8 → 1.0.0
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.
- package/PLASTIC-reference.md +133 -0
- package/PLASTIC.md +407 -84
- package/README.md +99 -82
- package/agents/plastic-brainstorming.md +12 -9
- package/agents/plastic-enforcer.md +52 -12
- package/agents/plastic-executor.md +10 -10
- package/agents/plastic-future-intent-researcher.md +4 -8
- package/agents/plastic-intent-curator.md +12 -14
- package/agents/plastic-intent-discovery.md +35 -0
- package/agents/plastic-planner.md +15 -10
- package/agents/plastic-spec-specialist.md +15 -10
- package/bin/plastic.js +7 -3
- package/hooks/code-gate +10 -1
- package/hooks/hooks.json +28 -3
- package/hooks/lock-gate +21 -0
- package/hooks/retrieval-gate +10 -0
- package/hooks/savepoint-pre +10 -0
- package/hooks/statusline +48 -6
- package/package.json +2 -1
- package/scripts/agent-report +21 -0
- package/scripts/dashboard.rb +98 -23
- package/scripts/doctor.rb +404 -5
- package/scripts/hook-auto-arm +1 -1
- package/scripts/hook-bash-gate +22 -4
- package/scripts/hook-code-gate +11 -6
- package/scripts/hook-continue +17 -0
- package/scripts/hook-create-gate +45 -9
- package/scripts/hook-gate-check +24 -23
- package/scripts/hook-lock-gate +83 -0
- package/scripts/hook-retrieval-gate +148 -0
- package/scripts/hook-savepoint-pre +32 -0
- package/scripts/hook-session-start +1 -1
- package/scripts/insight-append +51 -0
- package/scripts/install.rb +44 -7
- package/scripts/lib/agent_models.rb +43 -0
- package/scripts/lib/bridge.rb +924 -84
- package/scripts/lib/dashboard_banner.rb +42 -0
- package/scripts/lib/frontmatter_writer.rb +130 -0
- package/scripts/lib/graph_rebuild.rb +328 -0
- package/scripts/lib/hook_registry.rb +95 -0
- package/scripts/lib/insights.rb +86 -0
- package/scripts/lib/installer_core.rb +104 -60
- package/scripts/lib/link_suggestions.rb +319 -0
- package/scripts/lib/links_projection.rb +160 -0
- package/scripts/lib/links_section.rb +207 -0
- package/scripts/lib/lock.rb +375 -0
- package/scripts/lib/power_tools.rb +22 -21
- package/scripts/lib/preflight.rb +79 -0
- package/scripts/lib/qmd_sync.rb +15 -0
- package/scripts/lib/retrieval_gate.rb +211 -0
- package/scripts/lib/worktree.rb +384 -0
- package/scripts/link-suggest +213 -0
- package/scripts/new-intent +138 -29
- package/scripts/plastic-lock +164 -0
- package/scripts/project-links +287 -0
- package/scripts/read-config +4 -0
- package/scripts/rebuild-graph +244 -0
- package/scripts/spawn-preamble +11 -3
- package/scripts/update.rb +16 -7
- package/skills/auto/SKILL.md +126 -19
- package/skills/auto/references/agent-architecture.md +7 -4
- package/skills/auto/references/agent-report-contract.md +36 -1
- package/skills/auto/references/end-tail.md +56 -0
- package/skills/auto/references/human-report-contract.md +55 -0
- package/skills/auto/references/tiers.md +77 -0
- package/skills/brainstorming/SKILL.md +7 -34
- package/skills/brainstorming/references/design-principles.md +49 -0
- package/skills/continuing/SKILL.md +26 -7
- package/skills/creating-intent/SKILL.md +13 -28
- package/skills/creating-intent/references/lifecycle.md +9 -4
- package/skills/creating-project/SKILL.md +11 -74
- package/skills/creating-project/references/project-scaffolding.md +97 -0
- package/skills/creating-skills/SKILL.md +65 -0
- package/skills/creating-skills/evals/evals.json +108 -0
- package/skills/creating-skills/references/agents.md +168 -0
- package/skills/creating-skills/references/evals.md +41 -0
- package/skills/creating-skills/references/hooks.md +248 -0
- package/skills/creating-skills/references/progressive-disclosure.md +176 -0
- package/skills/creating-skills/references/scripts.md +166 -0
- package/skills/creating-skills/references/skills.md +165 -0
- package/skills/creating-skills/scripts/scaffold.rb +313 -0
- package/skills/dashboard/SKILL.md +13 -11
- package/skills/dashboard/references/classification.md +22 -0
- package/skills/doctor/SKILL.md +10 -7
- package/skills/executing-plan/SKILL.md +4 -4
- package/skills/humanizer/SKILL.md +39 -0
- package/skills/humanizer/evals/evals.json +70 -0
- package/skills/humanizer/references/always-on-snippet.md +9 -0
- package/skills/humanizer/references/examples.md +48 -0
- package/skills/install/SKILL.md +75 -84
- package/skills/intent-curator/SKILL.md +5 -1
- package/skills/intent-discovery/SKILL.md +47 -0
- package/skills/intent-starting/SKILL.md +130 -0
- package/skills/intent-starting/evals/evals.json +117 -0
- package/skills/intent-starting/references/boarding-matrix.md +35 -0
- package/skills/linking-intents/SKILL.md +48 -12
- package/skills/linking-intents/references/zettelkasten.md +7 -0
- package/skills/lock/SKILL.md +41 -0
- package/skills/managing-index/SKILL.md +6 -0
- package/skills/managing-index/references/zettelkasten-linking.md +6 -1
- package/skills/releasing/SKILL.md +44 -29
- package/skills/releasing/references/promotion-and-tagging.md +60 -0
- package/skills/roadmap/SKILL.md +51 -0
- package/skills/roadmap/references/file-format.md +91 -0
- package/skills/roadmap/references/operations.md +78 -0
- package/skills/uninstall/SKILL.md +29 -11
- package/skills/update/SKILL.md +34 -23
- package/skills/versions/SKILL.md +27 -12
- package/skills/writing-plans/SKILL.md +10 -88
- package/skills/writing-plans/references/plan-format.md +102 -0
- package/templates/config.yml +8 -0
- package/templates/outcome.md +3 -0
- package/templates/revisions.md +58 -0
- package/templates/roadmap.md +30 -0
- package/skills/writing-instructions/SKILL.md +0 -159
- package/skills/writing-instructions/references/agentskills-spec.md +0 -135
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# LinksProjection — pure logic that projects one intent's sources/chain graph into
|
|
5
|
+
# the canonical I5 `## Links` section text (intent 72). Mirrors the pure-module
|
|
6
|
+
# style of GraphRebuild: module-function helpers, no file IO, no eval, no
|
|
7
|
+
# ENV/global state. The IO shell (scripts/project-links) and doctor build the
|
|
8
|
+
# cross-store resolver and feed it here.
|
|
9
|
+
#
|
|
10
|
+
# Pinned canonical projection (the human's format call):
|
|
11
|
+
# 1. ORDERING (load-bearing): ALL sources first, in frontmatter order, THEN all
|
|
12
|
+
# chain, in frontmatter order. Sources can NEVER appear at the end. No group
|
|
13
|
+
# headings, no per-entry source/chain tags: the ordering carries the meaning.
|
|
14
|
+
# 2. ENTRY SHAPE: one list item `- [[<id>--<slug>|<target's full intent: text>]]`,
|
|
15
|
+
# where the wikilink TARGET is the target intent's resolvable `id--slug` file
|
|
16
|
+
# basename (so it clicks through in Obsidian) and the LABEL is the target's
|
|
17
|
+
# full `intent:` frontmatter text, whitespace-trimmed.
|
|
18
|
+
# 3. CROSS-STORE: a cross-store target renders
|
|
19
|
+
# `- [[<store>:<id>--<slug>|<target's full intent: text>]]`.
|
|
20
|
+
# 4. RESOLVER MISS: a ref that resolves to no intent raises UnresolvedRef. No
|
|
21
|
+
# bare-id, slug-less, or guessed link is ever emitted.
|
|
22
|
+
# 5. EMPTY-STATE: empty sources AND chain yields the heading plus a single
|
|
23
|
+
# explanatory comment.
|
|
24
|
+
module LinksProjection
|
|
25
|
+
module_function
|
|
26
|
+
|
|
27
|
+
HEADING = "## Links"
|
|
28
|
+
EMPTY_COMMENT = "<!-- No sources or chain; this intent has no graph edges to project. -->"
|
|
29
|
+
|
|
30
|
+
# Raised when a sources/chain ref resolves to no intent. Carries the offending
|
|
31
|
+
# ref so the IO shell can report it per-intent and skip the write.
|
|
32
|
+
class UnresolvedRef < StandardError
|
|
33
|
+
attr_reader :ref
|
|
34
|
+
|
|
35
|
+
def initialize(ref)
|
|
36
|
+
@ref = ref
|
|
37
|
+
super("unresolved sources/chain ref: #{ref.inspect}")
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# PURE. Build the canonical `## Links` section text for one intent.
|
|
42
|
+
#
|
|
43
|
+
# sources — array of id / `store:id` ref strings, in frontmatter order
|
|
44
|
+
# chain — array of id / `store:id` ref strings, in frontmatter order
|
|
45
|
+
# resolve — a callable (`->(ref) { ... }`) mapping ONE ref to a Hash like
|
|
46
|
+
# { target: "<id>--<slug>", label: "<full intent: text>" } or
|
|
47
|
+
# { target: "<store>:<id>--<slug>", label: "<full intent: text>" }.
|
|
48
|
+
# Returning nil (or a Hash lacking :target) signals no target and
|
|
49
|
+
# raises UnresolvedRef. Keeping resolution injected keeps this module
|
|
50
|
+
# pure and hermetically testable with in-memory maps.
|
|
51
|
+
#
|
|
52
|
+
# Returns the full section text: the `## Links` heading line, one entry line per
|
|
53
|
+
# ref (sources first, then chain), and a single trailing newline. The empty case
|
|
54
|
+
# returns the heading + the empty-state comment + a single trailing newline.
|
|
55
|
+
def section(sources:, chain:, resolve:)
|
|
56
|
+
src = Array(sources).map(&:to_s)
|
|
57
|
+
chn = Array(chain).map(&:to_s)
|
|
58
|
+
|
|
59
|
+
# Resolve EVERY ref to its { target:, label: } first, then dedup by the RESOLVED
|
|
60
|
+
# target (not the raw ref string). This is load-bearing: the same intent may be
|
|
61
|
+
# referenced as a bare id in one group and as `store:id` in another (or via a
|
|
62
|
+
# relocation), which dedups identically only AFTER resolution. Sources win
|
|
63
|
+
# (formative edge), and frontmatter order is preserved within each group.
|
|
64
|
+
seen = {}
|
|
65
|
+
rendered = []
|
|
66
|
+
src.each { |ref| add_entry(ref, resolve, seen, rendered) }
|
|
67
|
+
chn.each { |ref| add_entry(ref, resolve, seen, rendered) }
|
|
68
|
+
|
|
69
|
+
return empty_section if rendered.empty?
|
|
70
|
+
|
|
71
|
+
(["#{HEADING}\n"] + rendered.map { |line| "#{line}\n" }).join
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# PURE. The canonical empty-state section: heading + the single comment line.
|
|
75
|
+
def empty_section
|
|
76
|
+
"#{HEADING}\n#{EMPTY_COMMENT}\n"
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Resolve `ref`, render its entry, and append it to `rendered` UNLESS its resolved
|
|
80
|
+
# target was already emitted (dedup by resolved target, first-seen wins so sources
|
|
81
|
+
# precede chain). Mutates `seen` and `rendered`. Raises UnresolvedRef on a miss.
|
|
82
|
+
def add_entry(ref, resolve, seen, rendered)
|
|
83
|
+
target, label = resolve_entry(ref, resolve)
|
|
84
|
+
return if seen.key?(target)
|
|
85
|
+
|
|
86
|
+
seen[target] = true
|
|
87
|
+
rendered << "- [[#{target}|#{label}]]"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# PURE. Resolve one ref to [target, label]. Raises UnresolvedRef when the
|
|
91
|
+
# resolver returns nothing usable.
|
|
92
|
+
def resolve_entry(ref, resolve)
|
|
93
|
+
resolved = resolve.call(ref)
|
|
94
|
+
target = resolved.is_a?(Hash) ? resolved[:target] || resolved["target"] : nil
|
|
95
|
+
raise UnresolvedRef, ref if target.nil? || target.to_s.strip.empty?
|
|
96
|
+
|
|
97
|
+
label = (resolved[:label] || resolved["label"]).to_s.strip
|
|
98
|
+
[target.to_s, label]
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# PURE. Render one entry line `- [[<target>|<label>]]` from a single ref. Kept for
|
|
102
|
+
# callers/tests that render one entry; #section uses add_entry for dedup.
|
|
103
|
+
def entry(ref, resolve)
|
|
104
|
+
target, label = resolve_entry(ref, resolve)
|
|
105
|
+
"- [[#{target}|#{label}]]"
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# PURE. Resolve ONE sources/chain ref to its `{ target:, label: }` projection,
|
|
109
|
+
# given the in-memory cross-store maps. This is the single resolver definition
|
|
110
|
+
# shared by the IO shell (scripts/project-links) and the doctor check, so the two
|
|
111
|
+
# can never diverge.
|
|
112
|
+
#
|
|
113
|
+
# ref — "40" (same-store bare id) or "knowdb:1" (cross-store)
|
|
114
|
+
# referer_store — store_key of the intent carrying the ref ("global" / "project:<slug>")
|
|
115
|
+
# relocation_map — from GraphRebuild.build_relocation_map (spans all stores)
|
|
116
|
+
# store_index — { store_key => [bare ids present] } (spans all stores)
|
|
117
|
+
# node_index — { store_key => { id => { basename:, label: } } } (spans all stores)
|
|
118
|
+
#
|
|
119
|
+
# Returns { target:, label: } (target is `<id>--<slug>` for a same-store id, or
|
|
120
|
+
# `<slug>:<id>--<slug>` for a cross-store one), or nil when the ref resolves to no
|
|
121
|
+
# live intent (which makes #section / #entry raise UnresolvedRef).
|
|
122
|
+
#
|
|
123
|
+
# Uses GraphRebuild.resolve_ref so a relocation always wins over a coincidentally
|
|
124
|
+
# reused id (the `global:24` impostor hazard), exactly as the frontmatter rebuild
|
|
125
|
+
# and the cross-store doctor check do.
|
|
126
|
+
def resolve_ref_projection(ref, referer_store:, relocation_map:, store_index:, node_index:)
|
|
127
|
+
require_relative "graph_rebuild"
|
|
128
|
+
|
|
129
|
+
res = GraphRebuild.resolve_ref(ref, referer_store: referer_store,
|
|
130
|
+
relocation_map: relocation_map,
|
|
131
|
+
store_index: store_index)
|
|
132
|
+
case res[:status]
|
|
133
|
+
when :same_store
|
|
134
|
+
node = (node_index[referer_store] || {})[res[:id]]
|
|
135
|
+
return nil if node.nil?
|
|
136
|
+
|
|
137
|
+
{ target: node[:basename], label: node[:label] }
|
|
138
|
+
when :cross_store
|
|
139
|
+
slug, bare = res[:ref].split(":", 2)
|
|
140
|
+
target_key = canonical_store_key(slug, store_index)
|
|
141
|
+
node = (node_index[target_key] || {})[bare]
|
|
142
|
+
return nil if node.nil?
|
|
143
|
+
|
|
144
|
+
{ target: "#{slug}:#{node[:basename]}", label: node[:label] }
|
|
145
|
+
else # :dead
|
|
146
|
+
nil
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# Map a ref store slug ("global", "knowdb", "plastic") to a node_index/store_index
|
|
151
|
+
# key ("global", "project:knowdb", "project:plastic"). Mirrors
|
|
152
|
+
# GraphRebuild.canonical_store_key's intent for the node_index keyspace.
|
|
153
|
+
def canonical_store_key(slug, store_index)
|
|
154
|
+
return "global" if slug == "global"
|
|
155
|
+
return slug if (store_index || {}).key?(slug)
|
|
156
|
+
|
|
157
|
+
projected = "project:#{slug}"
|
|
158
|
+
(store_index || {}).key?(projected) ? projected : slug
|
|
159
|
+
end
|
|
160
|
+
end
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# encoding: UTF-8
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require_relative "intent_validator"
|
|
5
|
+
|
|
6
|
+
# LinksSection — pure, minimal, style-preserving rewrite of ONLY the `## Links`
|
|
7
|
+
# section of an intent file's content string (intent 72). Mirrors the discipline of
|
|
8
|
+
# FrontmatterWriter: pure (no file IO, no eval, no global/ENV state), and returns
|
|
9
|
+
# the original content UNCHANGED when nothing changed (idempotency).
|
|
10
|
+
#
|
|
11
|
+
# `## Links` is canonically the LAST sanctioned section
|
|
12
|
+
# (IntentValidator::SANCTIONED_SECTIONS = Intent, Context, Outcome, Insights,
|
|
13
|
+
# Links), and real intents put it last. So a file with an existing `## Links` has
|
|
14
|
+
# its section replaced in place (from the heading to the next top-level `## `
|
|
15
|
+
# heading or EOF), and a file WITHOUT a `## Links` gets one appended at end-of-body,
|
|
16
|
+
# separated by exactly one blank line, preserving the body's trailing-newline shape.
|
|
17
|
+
#
|
|
18
|
+
# FENCE AWARENESS (intent 72 corruption fix): a `## Links` heading INSIDE a fenced
|
|
19
|
+
# code block (``` or ~~~, possibly with an info string like ```markdown) is part of
|
|
20
|
+
# an EXAMPLE, not a real section. All section scanning here IGNORES headings inside
|
|
21
|
+
# fences and only ever targets the REAL `## Links` section (outside any fence). The
|
|
22
|
+
# section end is the next `## ` heading that is ALSO outside a fence, so a replace
|
|
23
|
+
# never consumes or unbalances a code fence. If more than one REAL `## Links`
|
|
24
|
+
# heading exists, #rewrite raises AmbiguousLinks rather than guess.
|
|
25
|
+
#
|
|
26
|
+
# Frontmatter is NEVER touched: the leading `---`...`---` block is preserved
|
|
27
|
+
# byte-for-byte and only the body is rebuilt.
|
|
28
|
+
module LinksSection
|
|
29
|
+
module_function
|
|
30
|
+
|
|
31
|
+
HEADING = "## Links"
|
|
32
|
+
|
|
33
|
+
# A fence delimiter: ``` or ~~~ (any length >= 3), optional leading whitespace,
|
|
34
|
+
# optional info string (e.g. ```markdown). Mirrors CommonMark fenced-code rules
|
|
35
|
+
# closely enough for intent bodies.
|
|
36
|
+
FENCE_RE = /\A\s*(`{3,}|~{3,})/
|
|
37
|
+
|
|
38
|
+
# Raised when a body has more than one REAL `## Links` heading outside any fence;
|
|
39
|
+
# the tool must fail loud rather than guess which one to rewrite.
|
|
40
|
+
class AmbiguousLinks < StandardError
|
|
41
|
+
def initialize(count)
|
|
42
|
+
super("found #{count} real `## Links` headings outside code fences; refusing to guess")
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# PURE. Replace (or insert) the REAL `## Links` section in `content` with
|
|
47
|
+
# `section_text` (the canonical block from LinksProjection.section, which begins
|
|
48
|
+
# with the `## Links` heading line and ends with a single trailing newline).
|
|
49
|
+
# Returns the new content, or the original when nothing changed. Raises
|
|
50
|
+
# AmbiguousLinks when more than one real `## Links` heading exists.
|
|
51
|
+
def rewrite(content, section_text)
|
|
52
|
+
return content unless content.is_a?(String)
|
|
53
|
+
|
|
54
|
+
fm, body = split_frontmatter(content)
|
|
55
|
+
new_body = rewrite_body(body, section_text)
|
|
56
|
+
updated = "#{fm}#{new_body}"
|
|
57
|
+
updated == content ? content : updated
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Split content into [frontmatter_with_delimiters, body]. When there is no
|
|
61
|
+
# frontmatter block, the frontmatter part is "" and the whole content is the
|
|
62
|
+
# body. The frontmatter part is preserved byte-for-byte by the caller.
|
|
63
|
+
def split_frontmatter(content)
|
|
64
|
+
return ["", content] unless content.start_with?("---")
|
|
65
|
+
|
|
66
|
+
parts = content.split("---", 3)
|
|
67
|
+
return ["", content] if parts.length < 3
|
|
68
|
+
|
|
69
|
+
["---#{parts[1]}---", parts[2]]
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Rewrite ONLY the REAL `## Links` section within the body text.
|
|
73
|
+
def rewrite_body(body, section_text)
|
|
74
|
+
bounds = links_bounds(body)
|
|
75
|
+
if bounds
|
|
76
|
+
replace_section(body, section_text, bounds)
|
|
77
|
+
else
|
|
78
|
+
insert_section(body, section_text)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# PURE. Locate the REAL `## Links` section (fence-aware). Returns
|
|
83
|
+
# [start_index, end_index] line indices into body.lines, where start_index is the
|
|
84
|
+
# `## Links` heading line and end_index is the index of the next out-of-fence
|
|
85
|
+
# `## ` heading (or lines.length at EOF). Returns nil when there is no real
|
|
86
|
+
# `## Links` heading. Raises AmbiguousLinks when more than one exists.
|
|
87
|
+
def links_bounds(body)
|
|
88
|
+
lines = body.to_s.lines
|
|
89
|
+
starts = real_links_heading_indices(lines)
|
|
90
|
+
return nil if starts.empty?
|
|
91
|
+
raise AmbiguousLinks, starts.length if starts.length > 1
|
|
92
|
+
|
|
93
|
+
start = starts.first
|
|
94
|
+
stop = next_out_of_fence_heading(lines, start + 1)
|
|
95
|
+
[start, stop]
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# PURE. Indices of every `## Links` heading line that is OUTSIDE any code fence.
|
|
99
|
+
# Accepts a body String or an Array of lines.
|
|
100
|
+
def real_links_heading_indices(body_or_lines)
|
|
101
|
+
lines = body_or_lines.is_a?(Array) ? body_or_lines : body_or_lines.to_s.lines
|
|
102
|
+
indices = []
|
|
103
|
+
in_fence = false
|
|
104
|
+
fence_marker = nil
|
|
105
|
+
lines.each_with_index do |line, i|
|
|
106
|
+
if (m = fence_open_close(line, in_fence, fence_marker))
|
|
107
|
+
in_fence = m[:in_fence]
|
|
108
|
+
fence_marker = m[:marker]
|
|
109
|
+
next
|
|
110
|
+
end
|
|
111
|
+
indices << i if !in_fence && line.rstrip == HEADING
|
|
112
|
+
end
|
|
113
|
+
indices
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# PURE. Index of the first `## ` heading at or after `from` that is OUTSIDE any
|
|
117
|
+
# code fence. Returns lines.length when none (EOF). Fence state is recomputed
|
|
118
|
+
# from the top so nested example fences after the real heading are respected.
|
|
119
|
+
def next_out_of_fence_heading(lines, from)
|
|
120
|
+
in_fence = false
|
|
121
|
+
fence_marker = nil
|
|
122
|
+
lines.each_with_index do |line, i|
|
|
123
|
+
if (m = fence_open_close(line, in_fence, fence_marker))
|
|
124
|
+
in_fence = m[:in_fence]
|
|
125
|
+
fence_marker = m[:marker]
|
|
126
|
+
next
|
|
127
|
+
end
|
|
128
|
+
return i if i >= from && !in_fence && line.start_with?("## ")
|
|
129
|
+
end
|
|
130
|
+
lines.length
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# PURE. Given the current fence state, decide whether `line` is a fence delimiter
|
|
134
|
+
# and return the new state, or nil when the line is not a fence delimiter.
|
|
135
|
+
# An opening fence records its marker family (` or ~); a closing fence must use a
|
|
136
|
+
# marker of the SAME family and carry no info string.
|
|
137
|
+
def fence_open_close(line, in_fence, fence_marker)
|
|
138
|
+
m = line.match(FENCE_RE)
|
|
139
|
+
return nil unless m
|
|
140
|
+
|
|
141
|
+
marker = m[1]
|
|
142
|
+
family = marker[0] # "`" or "~"
|
|
143
|
+
if in_fence
|
|
144
|
+
# A closing fence uses the same family, length >= the opener, no info string.
|
|
145
|
+
rest = line.sub(FENCE_RE, "").strip
|
|
146
|
+
if family == fence_marker && rest.empty?
|
|
147
|
+
{ in_fence: false, marker: nil }
|
|
148
|
+
else
|
|
149
|
+
# A delimiter of the OTHER family (or an info-string line) inside a fence is
|
|
150
|
+
# literal content, not a fence event.
|
|
151
|
+
nil
|
|
152
|
+
end
|
|
153
|
+
else
|
|
154
|
+
{ in_fence: true, marker: family }
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# True iff the body has a REAL (out-of-fence) `## Links` heading. Used by callers
|
|
159
|
+
# to classify regenerate-vs-add without re-deriving fence state.
|
|
160
|
+
def links_heading?(body)
|
|
161
|
+
!real_links_heading_indices(body.to_s.lines).empty?
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Replace the REAL `## Links` section (the [start, stop] line bounds) with
|
|
165
|
+
# `section_text`, preserving everything before the heading and after the section
|
|
166
|
+
# byte-for-byte (including any fenced example that lives BEFORE the real section).
|
|
167
|
+
def replace_section(body, section_text, bounds)
|
|
168
|
+
lines = body.lines
|
|
169
|
+
start, stop = bounds
|
|
170
|
+
before = lines[0...start].join
|
|
171
|
+
tail = (lines[stop..] || [])
|
|
172
|
+
|
|
173
|
+
# `section_text` already ends with exactly one newline. When there is trailing
|
|
174
|
+
# content (another section follows), separate the block from it with one blank
|
|
175
|
+
# line; otherwise the section ends the body.
|
|
176
|
+
block = tail.empty? ? section_text : "#{section_text}\n"
|
|
177
|
+
"#{before}#{block}#{tail.join}"
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Append a `## Links` section at end-of-body, after the last existing section,
|
|
181
|
+
# separated by exactly one blank line, preserving the body's trailing newline.
|
|
182
|
+
def insert_section(body, section_text)
|
|
183
|
+
trimmed = body.to_s.sub(/\s+\z/, "")
|
|
184
|
+
if trimmed.empty?
|
|
185
|
+
# An empty body (no sections) just becomes the section.
|
|
186
|
+
section_text
|
|
187
|
+
else
|
|
188
|
+
"#{trimmed}\n\n#{section_text}"
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# PURE. Extract the REAL `## Links` section text (fence-aware), normalized to the
|
|
193
|
+
# canonical block shape the projection emits: heading line + entry lines + a
|
|
194
|
+
# single trailing newline. Returns "" when there is no real section. Shared by
|
|
195
|
+
# the IO shell's audit and the doctor drift check so all three agree on the
|
|
196
|
+
# location. Raises AmbiguousLinks when more than one real heading exists.
|
|
197
|
+
def extract_section(body)
|
|
198
|
+
bounds = links_bounds(body)
|
|
199
|
+
return "" if bounds.nil?
|
|
200
|
+
|
|
201
|
+
start, stop = bounds
|
|
202
|
+
lines = body.to_s.lines
|
|
203
|
+
section = lines[(start + 1)...stop].join.sub(/\n+\z/, "\n")
|
|
204
|
+
section = "" if section.strip.empty?
|
|
205
|
+
"#{HEADING}\n#{section}"
|
|
206
|
+
end
|
|
207
|
+
end
|