Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/dev/plan.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,8 @@

require "dev/plan/executor"
require "dev/plan/header"
require "dev/plan/frontmatter"
require "dev/plan/content"
require "dev/plan/settings"
require "dev/plan/github_issues"
require "dev/plan/workspace"
Expand Down
94 changes: 51 additions & 43 deletions lib/dev/plan/accessor.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,17 +102,16 @@ def link(args, out:)

def link_to_existing(number, file, org:, out:)
path = file ? Pathname.new(file) : sole_unlinked_plan
content = path.read
header, body = Header.split(content)
raise UsageError, "#{path} is already linked to #{header.issue_ref}" if header
plan = Content.parse(path.read)
raise UsageError, "#{path} is already linked to #{plan.header.issue_ref}" if plan.header

owner_repo = target_repo(org:)
issue = @issues.get(owner_repo, number)
# Record the remote as the sync point but keep the local content: the
# file is a draft ahead of the issue until `dev plan push` publishes it.
new_header = Header.new(owner_repo: owner_repo, number: issue.number, synced_at: issue.updated_at)
target = move_into_convention(path, owner_repo, issue)
target.write(new_header.render + body)
target.write(plan.with_header(new_header).render)
@merge_base.write(owner_repo, issue.number, Plan.from_issue_body(issue.body))
out.puts "dev: linked #{target} to #{owner_repo}##{issue.number} (#{issue.html_url})"
out.puts "dev: local content kept — run `dev plan push` to publish it."
Expand All@@ -122,15 +121,14 @@ def create_from_file(file, org:, out:)
path = Pathname.new(file)
raise UsageError, "no such plan file: #{path}" unless path.exist?

content = path.read
header, body = Header.split(content)
raise UsageError, "#{path} is already linked to #{header.issue_ref}" if header
plan = Content.parse(path.read)
raise UsageError, "#{path} is already linked to #{plan.header.issue_ref}" if plan.header

owner_repo = target_repo(org:)
title = extract_title(body) || path.basename(".plan.md").to_s
issue = @issues.create(owner_repo, title: title, body: Plan.to_issue_body(body))
title = extract_title(plan.body) || path.basename(".plan.md").to_s
issue = @issues.create(owner_repo, title: title, body: Plan.to_issue_body(plan.body))
target = move_into_convention(path, owner_repo, issue)
write_linked_plan(owner_repo, issue, body, path: target)
write_linked_plan(owner_repo, issue, plan.body, path: target, frontmatter: plan.frontmatter)
out.puts "dev: created #{owner_repo}##{issue.number} from #{path} (#{issue.html_url})"
out.puts "dev: plan file: #{target}"
end
Expand All@@ -156,30 +154,31 @@ def pull(args, out:)
return
end

_header, local_body = Header.split(path.read)
plan = Content.parse(path.read)
base = @merge_base.read(owner_repo, number)
local_dirty = base.nil? ? false : local_body != base
local_dirty = base.nil? ? false : plan.body != base
remote_dirty = base.nil? ? true : remote_body != base

if !local_dirty
write_linked_plan(owner_repo, issue, remote_body, path: path)
write_linked_plan(owner_repo, issue, remote_body, path: path, frontmatter: plan.frontmatter)
out.puts(remote_dirty ? "dev: pulled #{owner_repo}##{number} into #{path}" : "dev: #{path} is already up to date.")
elsif !remote_dirty
out.puts "dev: local plan is ahead of #{owner_repo}##{number} — nothing to pull. Run `dev plan push`."
elsif merge
merge_pull(path, issue, owner_repo, number, local_body, base, remote_body, out:)
merge_pull(path, issue, owner_repo, number, plan, base, remote_body, out:)
else
raise "both #{path} and #{owner_repo}##{number} changed since the last sync — " \
"run `dev plan pull #{number} --merge`."
end
end

def merge_pull(path, issue, owner_repo, number, local_body, base, remote_body, out:)
result = Merge.three_way(local: local_body, base: base, remote: remote_body, executor: @executor)
def merge_pull(path, issue, owner_repo, number, plan, base, remote_body, out:)
result = Merge.three_way(local: plan.body, base: base, remote: remote_body, executor: @executor)
# The remote becomes the new base either way: the merged local copy is
# now "ahead" of the issue, and `push` publishes it.
# now "ahead" of the issue, and `push` publishes it. Frontmatter is
# carried through from the local side untouched.
header = Header.new(owner_repo: owner_repo, number: number, synced_at: issue.updated_at)
path.write(header.render + result.content)
path.write(plan.with_header(header).with_body(result.content).render)
@merge_base.write(owner_repo, number, remote_body)
if result.conflicts?
out.puts "dev: merged with conflicts — resolve the markers in #{path}, then run `dev plan push`."
Expand All@@ -197,10 +196,11 @@ def push(args, out:)
raise UsageError, "usage: dev plan push [<file>]" unless args.empty?

path = file ? Pathname.new(file) : sole_linked_plan
header, body = Header.split(path.read)
raise UsageError, "#{path} has no ai-flow header — link it first with `dev plan link`." unless header
raise "#{path} contains unresolved merge conflict markers — resolve them before pushing." if body.include?("<<<<<<<")
plan = Content.parse(path.read)
raise UsageError, "#{path} has no ai-flow header — link it first with `dev plan link`." unless plan.header
raise "#{path} contains unresolved merge conflict markers — resolve them before pushing." if plan.body.include?("<<<<<<<")

header = plan.header
issue = @issues.get(header.owner_repo, header.number)
remote_body = Plan.from_issue_body(issue.body)
base = @merge_base.read(header.owner_repo, header.number)
Expand All@@ -214,19 +214,19 @@ def push(args, out:)
"run `dev plan pull #{header.number} --merge`, then push again."
end

if body == remote_body
record_sync(path, header, issue, body)
if plan.body == remote_body
record_sync(path, plan, issue)
out.puts "dev: #{header.issue_ref} is already in sync."
return
end

title = extract_title(body)
title = extract_title(plan.body)
updated = @issues.update(
header.owner_repo, header.number,
body: Plan.to_issue_body(body),
body: Plan.to_issue_body(plan.body),
title: (title if title && title != issue.title),
)
record_sync(path, header, updated, body)
record_sync(path, plan, updated)
out.puts "dev: pushed #{path} to #{header.issue_ref} (#{updated.html_url})"
end

Expand All@@ -244,8 +244,8 @@ def hook_after_edit(input, out:)
return unless path.to_s.end_with?(".plan.md") && path.exist?
return unless path.expand_path.to_s.start_with?(@workspace.plans_dir.expand_path.to_s)

header, _body = Header.split(path.read)
return unless header
plan = Content.parse(path.read)
return unless plan.header

push([path.to_s], out:)
end
Expand All@@ -260,10 +260,10 @@ def status(out:)
end

files.each do |path|
header, body = Header.split(path.read)
issue = @issues.get(header.owner_repo, header.number)
state = sync_state(header, body, Plan.from_issue_body(issue.body))
out.puts "#{state.ljust(10)} #{header.issue_ref.ljust(30)} #{path}"
plan = Content.parse(path.read)
issue = @issues.get(plan.header.owner_repo, plan.header.number)
state = sync_state(plan.header, plan.body, Plan.from_issue_body(issue.body))
out.puts "#{state.ljust(10)} #{plan.header.issue_ref.ljust(30)} #{path}"
end
end

Expand All@@ -286,22 +286,31 @@ def target_repo(org:)
org ? @settings.plans_repo : @workspace.origin_repo
end

# Write the plan file (header + body) and refresh the merge base — the
# single definition of "synced".
# Write the plan file (header + optional frontmatter + markdown body) and
# refresh the merge base — the single definition of "synced". The merge
# base stores the markdown body only.
#
# @param owner_repo [String]
# @param issue [Dev::Plan::GithubIssues::Issue]
# @param body [String] markdown body
# @param path [Pathname, nil]
# @param frontmatter [String, nil] Cursor YAML block to preserve locally
# @return [Pathname] the written path
def write_linked_plan(owner_repo, issue, body, path: nil)
def write_linked_plan(owner_repo, issue, body, path: nil, frontmatter: nil)
path ||= @workspace.plan_path(owner_repo, issue.number, issue.title)
header = Header.new(owner_repo: owner_repo, number: issue.number, synced_at: issue.updated_at)
FileUtils.mkdir_p(path.dirname)
path.write(header.render + body)
path.write(Content.new(header: header, frontmatter: frontmatter, body: body).render)
@merge_base.write(owner_repo, issue.number, body)
path
end

def record_sync(path, header, issue, body)
path.write(header.with_synced_at(issue.updated_at).render + body)
@merge_base.write(header.owner_repo, header.number, body)
# @param path [Pathname]
# @param plan [Dev::Plan::Content]
# @param issue [Dev::Plan::GithubIssues::Issue]
def record_sync(path, plan, issue)
path.write(plan.with_synced_at(issue.updated_at).render)
@merge_base.write(plan.header.owner_repo, plan.header.number, plan.body)
end

# Move a freshly linked file to the `gh-<n>-<slug>.plan.md` convention
Expand All@@ -326,8 +335,8 @@ def extract_title(body)
# @return [Pathname]
def find_linked_plan(owner_repo, number)
@workspace.linked_plan_files.find do |path|
header, _body = Header.split(path.read)
header.owner_repo == owner_repo && header.number == number
plan = Content.parse(path.read)
plan.header.owner_repo == owner_repo && plan.header.number == number
end
end

Expand All@@ -343,8 +352,7 @@ def sole_unlinked_plan
files =
if @workspace.plans_dir.directory?
@workspace.plans_dir.glob(Workspace::PLAN_GLOB).sort.reject do |path|
header, _body = Header.split(path.read)
header
Content.parse(path.read).header
end
else
[]
Expand Down
81 changes: 81 additions & 0 deletions lib/dev/plan/content.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

require "dev/plan/header"
require "dev/plan/frontmatter"

module Dev
module Plan
# The three on-disk layers of a plan file: ai-flow sync header, optional
# Cursor YAML frontmatter, and the markdown body. Sync compares and ships
# the markdown body only; the header and frontmatter stay local.
class Content
# @return [Dev::Plan::Header, nil]
attr_reader :header

# @return [String, nil] raw frontmatter block including `---` fences
attr_reader :frontmatter

# @return [String] markdown body (canonical plan prose)
attr_reader :body

# Parse a plan file into its layers. Canonical on-disk order is header,
# then optional frontmatter, then body. When frontmatter sits above the
# ai-flow header (hand edit), both are still recognized; {#render}
# rewrites canonical order.
#
# @param content [String]
# @return [Content]
def self.parse(content)
header, remainder = Header.split(content)
if header
frontmatter, body = Frontmatter.split(remainder)
return new(header: header, frontmatter: frontmatter, body: body)
end

# Frontmatter may sit above a misplaced ai-flow header.
frontmatter, after_frontmatter = Frontmatter.split(content)
if frontmatter
header, body = Header.split(after_frontmatter)
return new(header: header, frontmatter: frontmatter, body: body)
end

new(header: nil, frontmatter: nil, body: content)
end

# @param header [Dev::Plan::Header, nil]
# @param frontmatter [String, nil]
# @param body [String]
def initialize(header:, frontmatter:, body:)
@header = header
@frontmatter = frontmatter
@body = body
end

# Serialize in canonical order: ai-flow header, optional frontmatter,
# markdown body.
#
# @return [String]
def render
"#{header&.render}#{frontmatter}#{body}"
end

# @param header [Dev::Plan::Header, nil]
# @return [Content]
def with_header(header)
self.class.new(header: header, frontmatter: frontmatter, body: body)
end

# @param body [String]
# @return [Content]
def with_body(body)
self.class.new(header: header, frontmatter: frontmatter, body: body)
end

# @param synced_at [String]
# @return [Content]
def with_synced_at(synced_at)
with_header(header.with_synced_at(synced_at))
end
end
end
end
56 changes: 56 additions & 0 deletions lib/dev/plan/frontmatter.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require "yaml"

module Dev
module Plan
# Cursor plan YAML frontmatter (`---` … `---`) — local editor state
# (picker label, overview, todos). Peeled before any GitHub sync so the
# issue body stays markdown-only.
class Frontmatter
FENCE_LINE = /\A---\n?\z/

# Peel a Cursor-like YAML frontmatter block from the start of +content+.
# Only a leading `---` … `---` fence whose interior is a YAML mapping is
# removed; ordinary markdown horizontal rules deeper in the body, or a
# leading `---` that is not a mapping, are left alone.
#
# @param content [String]
# @return [Array(String | nil, String)] frontmatter block (including
# fences and a trailing newline after the closing fence) or nil, and
# the remainder
def self.split(content)
lines = content.lines
return [nil, content] if lines.empty? || !fence?(lines.fetch(0))

close_index = (1...lines.length).find { |index| fence?(lines.fetch(index)) }
return [nil, content] unless close_index

yaml_text = lines[1...close_index].join
return [nil, content] unless mapping?(yaml_text)

frontmatter = lines[0..close_index].join
frontmatter = "#{frontmatter}\n" unless frontmatter.end_with?("\n")
body = lines[(close_index + 1)..].join
[frontmatter, body]
end

# @param line [String]
# @return [Boolean]
def self.fence?(line)
line.match?(FENCE_LINE)
end
private_class_method :fence?

# @param yaml_text [String] interior between fences
# @return [Boolean] true when the interior parses as a YAML mapping
def self.mapping?(yaml_text)
parsed = YAML.safe_load(yaml_text)
parsed.is_a?(Hash)
rescue Psych::SyntaxError, Psych::DisallowedClass
false
end
private_class_method :mapping?
end
end
end
12 changes: 7 additions & 5 deletions lib/dev/plan/header.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,18 +4,20 @@ module Dev
module Plan
module_function

# Render a plan body as an issue body: verbatim content, normalized to a
# single trailing newline.
# Normalize a markdown plan body for the issue: LF, single trailing
# newline. Callers pass the markdown body only — not Cursor YAML
# frontmatter or the ai-flow sync header.
#
# @param plan_body [String]
# @return [String]
def to_issue_body(plan_body)
"#{plan_body.rstrip}\n"
end

# Extract the plan body from an issue body. Issue bodies use CRLF line
# endings when edited via the GitHub web UI, so normalize to LF — the
# local file and merge base always use LF.
# Normalize an issue body to a local markdown plan body. Issue bodies use
# CRLF when edited via the GitHub web UI, so normalize to LF — the local
# file and merge base always use LF. The result is markdown only (GitHub
# never stores Cursor frontmatter).
#
# @param issue_body [String, nil]
# @return [String]
Expand Down
3 changes: 1 addition & 2 deletions lib/dev/plan/workspace.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,8 +53,7 @@ def linked_plan_files
return [] unless @plans_dir.directory?

@plans_dir.glob(PLAN_GLOB).sort.select do |path|
header, _body = Header.split(path.read)
!header.nil?
!Content.parse(path.read).header.nil?
end
end

Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/dev/plan.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,8 @@

require "dev/plan/executor"
require "dev/plan/header"
require "dev/plan/frontmatter"
require "dev/plan/content"
require "dev/plan/settings"
require "dev/plan/github_issues"
require "dev/plan/workspace"
Expand Down
94 changes: 51 additions & 43 deletions lib/dev/plan/accessor.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,17 +102,16 @@ def link(args, out:)

def link_to_existing(number, file, org:, out:)
path = file ? Pathname.new(file) : sole_unlinked_plan
content = path.read
header, body = Header.split(content)
raise UsageError, "#{path} is already linked to #{header.issue_ref}" if header
plan = Content.parse(path.read)
raise UsageError, "#{path} is already linked to #{plan.header.issue_ref}" if plan.header

owner_repo = target_repo(org:)
issue = @issues.get(owner_repo, number)
# Record the remote as the sync point but keep the local content: the
# file is a draft ahead of the issue until `dev plan push` publishes it.
new_header = Header.new(owner_repo: owner_repo, number: issue.number, synced_at: issue.updated_at)
target = move_into_convention(path, owner_repo, issue)
target.write(new_header.render + body)
target.write(plan.with_header(new_header).render)
@merge_base.write(owner_repo, issue.number, Plan.from_issue_body(issue.body))
out.puts "dev: linked #{target} to #{owner_repo}##{issue.number} (#{issue.html_url})"
out.puts "dev: local content kept — run `dev plan push` to publish it."
Expand All@@ -122,15 +121,14 @@ def create_from_file(file, org:, out:)
path = Pathname.new(file)
raise UsageError, "no such plan file: #{path}" unless path.exist?

content = path.read
header, body = Header.split(content)
raise UsageError, "#{path} is already linked to #{header.issue_ref}" if header
plan = Content.parse(path.read)
raise UsageError, "#{path} is already linked to #{plan.header.issue_ref}" if plan.header

owner_repo = target_repo(org:)
title = extract_title(body) || path.basename(".plan.md").to_s
issue = @issues.create(owner_repo, title: title, body: Plan.to_issue_body(body))
title = extract_title(plan.body) || path.basename(".plan.md").to_s
issue = @issues.create(owner_repo, title: title, body: Plan.to_issue_body(plan.body))
target = move_into_convention(path, owner_repo, issue)
write_linked_plan(owner_repo, issue, body, path: target)
write_linked_plan(owner_repo, issue, plan.body, path: target, frontmatter: plan.frontmatter)
out.puts "dev: created #{owner_repo}##{issue.number} from #{path} (#{issue.html_url})"
out.puts "dev: plan file: #{target}"
end
Expand All@@ -156,30 +154,31 @@ def pull(args, out:)
return
end

_header, local_body = Header.split(path.read)
plan = Content.parse(path.read)
base = @merge_base.read(owner_repo, number)
local_dirty = base.nil? ? false : local_body != base
local_dirty = base.nil? ? false : plan.body != base
remote_dirty = base.nil? ? true : remote_body != base

if !local_dirty
write_linked_plan(owner_repo, issue, remote_body, path: path)
write_linked_plan(owner_repo, issue, remote_body, path: path, frontmatter: plan.frontmatter)
out.puts(remote_dirty ? "dev: pulled #{owner_repo}##{number} into #{path}" : "dev: #{path} is already up to date.")
elsif !remote_dirty
out.puts "dev: local plan is ahead of #{owner_repo}##{number} — nothing to pull. Run `dev plan push`."
elsif merge
merge_pull(path, issue, owner_repo, number, local_body, base, remote_body, out:)
merge_pull(path, issue, owner_repo, number, plan, base, remote_body, out:)
else
raise "both #{path} and #{owner_repo}##{number} changed since the last sync — " \
"run `dev plan pull #{number} --merge`."
end
end

def merge_pull(path, issue, owner_repo, number, local_body, base, remote_body, out:)
result = Merge.three_way(local: local_body, base: base, remote: remote_body, executor: @executor)
def merge_pull(path, issue, owner_repo, number, plan, base, remote_body, out:)
result = Merge.three_way(local: plan.body, base: base, remote: remote_body, executor: @executor)
# The remote becomes the new base either way: the merged local copy is
# now "ahead" of the issue, and `push` publishes it.
# now "ahead" of the issue, and `push` publishes it. Frontmatter is
# carried through from the local side untouched.
header = Header.new(owner_repo: owner_repo, number: number, synced_at: issue.updated_at)
path.write(header.render + result.content)
path.write(plan.with_header(header).with_body(result.content).render)
@merge_base.write(owner_repo, number, remote_body)
if result.conflicts?
out.puts "dev: merged with conflicts — resolve the markers in #{path}, then run `dev plan push`."
Expand All@@ -197,10 +196,11 @@ def push(args, out:)
raise UsageError, "usage: dev plan push [<file>]" unless args.empty?

path = file ? Pathname.new(file) : sole_linked_plan
header, body = Header.split(path.read)
raise UsageError, "#{path} has no ai-flow header — link it first with `dev plan link`." unless header
raise "#{path} contains unresolved merge conflict markers — resolve them before pushing." if body.include?("<<<<<<<")
plan = Content.parse(path.read)
raise UsageError, "#{path} has no ai-flow header — link it first with `dev plan link`." unless plan.header
raise "#{path} contains unresolved merge conflict markers — resolve them before pushing." if plan.body.include?("<<<<<<<")

header = plan.header
issue = @issues.get(header.owner_repo, header.number)
remote_body = Plan.from_issue_body(issue.body)
base = @merge_base.read(header.owner_repo, header.number)
Expand All@@ -214,19 +214,19 @@ def push(args, out:)
"run `dev plan pull #{header.number} --merge`, then push again."
end

if body == remote_body
record_sync(path, header, issue, body)
if plan.body == remote_body
record_sync(path, plan, issue)
out.puts "dev: #{header.issue_ref} is already in sync."
return
end

title = extract_title(body)
title = extract_title(plan.body)
updated = @issues.update(
header.owner_repo, header.number,
body: Plan.to_issue_body(body),
body: Plan.to_issue_body(plan.body),
title: (title if title && title != issue.title),
)
record_sync(path, header, updated, body)
record_sync(path, plan, updated)
out.puts "dev: pushed #{path} to #{header.issue_ref} (#{updated.html_url})"
end

Expand All@@ -244,8 +244,8 @@ def hook_after_edit(input, out:)
return unless path.to_s.end_with?(".plan.md") && path.exist?
return unless path.expand_path.to_s.start_with?(@workspace.plans_dir.expand_path.to_s)

header, _body = Header.split(path.read)
return unless header
plan = Content.parse(path.read)
return unless plan.header

push([path.to_s], out:)
end
Expand All@@ -260,10 +260,10 @@ def status(out:)
end

files.each do |path|
header, body = Header.split(path.read)
issue = @issues.get(header.owner_repo, header.number)
state = sync_state(header, body, Plan.from_issue_body(issue.body))
out.puts "#{state.ljust(10)} #{header.issue_ref.ljust(30)} #{path}"
plan = Content.parse(path.read)
issue = @issues.get(plan.header.owner_repo, plan.header.number)
state = sync_state(plan.header, plan.body, Plan.from_issue_body(issue.body))
out.puts "#{state.ljust(10)} #{plan.header.issue_ref.ljust(30)} #{path}"
end
end

Expand All@@ -286,22 +286,31 @@ def target_repo(org:)
org ? @settings.plans_repo : @workspace.origin_repo
end

# Write the plan file (header + body) and refresh the merge base — the
# single definition of "synced".
# Write the plan file (header + optional frontmatter + markdown body) and
# refresh the merge base — the single definition of "synced". The merge
# base stores the markdown body only.
#
# @param owner_repo [String]
# @param issue [Dev::Plan::GithubIssues::Issue]
# @param body [String] markdown body
# @param path [Pathname, nil]
# @param frontmatter [String, nil] Cursor YAML block to preserve locally
# @return [Pathname] the written path
def write_linked_plan(owner_repo, issue, body, path: nil)
def write_linked_plan(owner_repo, issue, body, path: nil, frontmatter: nil)
path ||= @workspace.plan_path(owner_repo, issue.number, issue.title)
header = Header.new(owner_repo: owner_repo, number: issue.number, synced_at: issue.updated_at)
FileUtils.mkdir_p(path.dirname)
path.write(header.render + body)
path.write(Content.new(header: header, frontmatter: frontmatter, body: body).render)
@merge_base.write(owner_repo, issue.number, body)
path
end

def record_sync(path, header, issue, body)
path.write(header.with_synced_at(issue.updated_at).render + body)
@merge_base.write(header.owner_repo, header.number, body)
# @param path [Pathname]
# @param plan [Dev::Plan::Content]
# @param issue [Dev::Plan::GithubIssues::Issue]
def record_sync(path, plan, issue)
path.write(plan.with_synced_at(issue.updated_at).render)
@merge_base.write(plan.header.owner_repo, plan.header.number, plan.body)
end

# Move a freshly linked file to the `gh-<n>-<slug>.plan.md` convention
Expand All@@ -326,8 +335,8 @@ def extract_title(body)
# @return [Pathname]
def find_linked_plan(owner_repo, number)
@workspace.linked_plan_files.find do |path|
header, _body = Header.split(path.read)
header.owner_repo == owner_repo && header.number == number
plan = Content.parse(path.read)
plan.header.owner_repo == owner_repo && plan.header.number == number
end
end

Expand All@@ -343,8 +352,7 @@ def sole_unlinked_plan
files =
if @workspace.plans_dir.directory?
@workspace.plans_dir.glob(Workspace::PLAN_GLOB).sort.reject do |path|
header, _body = Header.split(path.read)
header
Content.parse(path.read).header
end
else
[]
Expand Down
81 changes: 81 additions & 0 deletions lib/dev/plan/content.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

require "dev/plan/header"
require "dev/plan/frontmatter"

module Dev
module Plan
# The three on-disk layers of a plan file: ai-flow sync header, optional
# Cursor YAML frontmatter, and the markdown body. Sync compares and ships
# the markdown body only; the header and frontmatter stay local.
class Content
# @return [Dev::Plan::Header, nil]
attr_reader :header

# @return [String, nil] raw frontmatter block including `---` fences
attr_reader :frontmatter

# @return [String] markdown body (canonical plan prose)
attr_reader :body

# Parse a plan file into its layers. Canonical on-disk order is header,
# then optional frontmatter, then body. When frontmatter sits above the
# ai-flow header (hand edit), both are still recognized; {#render}
# rewrites canonical order.
#
# @param content [String]
# @return [Content]
def self.parse(content)
header, remainder = Header.split(content)
if header
frontmatter, body = Frontmatter.split(remainder)
return new(header: header, frontmatter: frontmatter, body: body)
end

# Frontmatter may sit above a misplaced ai-flow header.
frontmatter, after_frontmatter = Frontmatter.split(content)
if frontmatter
header, body = Header.split(after_frontmatter)
return new(header: header, frontmatter: frontmatter, body: body)
end

new(header: nil, frontmatter: nil, body: content)
end

# @param header [Dev::Plan::Header, nil]
# @param frontmatter [String, nil]
# @param body [String]
def initialize(header:, frontmatter:, body:)
@header = header
@frontmatter = frontmatter
@body = body
end

# Serialize in canonical order: ai-flow header, optional frontmatter,
# markdown body.
#
# @return [String]
def render
"#{header&.render}#{frontmatter}#{body}"
end

# @param header [Dev::Plan::Header, nil]
# @return [Content]
def with_header(header)
self.class.new(header: header, frontmatter: frontmatter, body: body)
end

# @param body [String]
# @return [Content]
def with_body(body)
self.class.new(header: header, frontmatter: frontmatter, body: body)
end

# @param synced_at [String]
# @return [Content]
def with_synced_at(synced_at)
with_header(header.with_synced_at(synced_at))
end
end
end
end
56 changes: 56 additions & 0 deletions lib/dev/plan/frontmatter.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require "yaml"

module Dev
module Plan
# Cursor plan YAML frontmatter (`---` … `---`) — local editor state
# (picker label, overview, todos). Peeled before any GitHub sync so the
# issue body stays markdown-only.
class Frontmatter
FENCE_LINE = /\A---\n?\z/

# Peel a Cursor-like YAML frontmatter block from the start of +content+.
# Only a leading `---` … `---` fence whose interior is a YAML mapping is
# removed; ordinary markdown horizontal rules deeper in the body, or a
# leading `---` that is not a mapping, are left alone.
#
# @param content [String]
# @return [Array(String | nil, String)] frontmatter block (including
# fences and a trailing newline after the closing fence) or nil, and
# the remainder
def self.split(content)
lines = content.lines
return [nil, content] if lines.empty? || !fence?(lines.fetch(0))

close_index = (1...lines.length).find { |index| fence?(lines.fetch(index)) }
return [nil, content] unless close_index

yaml_text = lines[1...close_index].join
return [nil, content] unless mapping?(yaml_text)

frontmatter = lines[0..close_index].join
frontmatter = "#{frontmatter}\n" unless frontmatter.end_with?("\n")
body = lines[(close_index + 1)..].join
[frontmatter, body]
end

# @param line [String]
# @return [Boolean]
def self.fence?(line)
line.match?(FENCE_LINE)
end
private_class_method :fence?

# @param yaml_text [String] interior between fences
# @return [Boolean] true when the interior parses as a YAML mapping
def self.mapping?(yaml_text)
parsed = YAML.safe_load(yaml_text)
parsed.is_a?(Hash)
rescue Psych::SyntaxError, Psych::DisallowedClass
false
end
private_class_method :mapping?
end
end
end
12 changes: 7 additions & 5 deletions lib/dev/plan/header.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,18 +4,20 @@ module Dev
module Plan
module_function

# Render a plan body as an issue body: verbatim content, normalized to a
# single trailing newline.
# Normalize a markdown plan body for the issue: LF, single trailing
# newline. Callers pass the markdown body only — not Cursor YAML
# frontmatter or the ai-flow sync header.
#
# @param plan_body [String]
# @return [String]
def to_issue_body(plan_body)
"#{plan_body.rstrip}\n"
end

# Extract the plan body from an issue body. Issue bodies use CRLF line
# endings when edited via the GitHub web UI, so normalize to LF — the
# local file and merge base always use LF.
# Normalize an issue body to a local markdown plan body. Issue bodies use
# CRLF when edited via the GitHub web UI, so normalize to LF — the local
# file and merge base always use LF. The result is markdown only (GitHub
# never stores Cursor frontmatter).
#
# @param issue_body [String, nil]
# @return [String]
Expand Down
3 changes: 1 addition & 2 deletions lib/dev/plan/workspace.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,8 +53,7 @@ def linked_plan_files
return [] unless @plans_dir.directory?

@plans_dir.glob(PLAN_GLOB).sort.select do |path|
header, _body = Header.split(path.read)
!header.nil?
!Content.parse(path.read).header.nil?
end
end

Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/dev/plan.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,8 @@

require "dev/plan/executor"
require "dev/plan/header"
require "dev/plan/frontmatter"
require "dev/plan/content"
require "dev/plan/settings"
require "dev/plan/github_issues"
require "dev/plan/workspace"
Expand Down
94 changes: 51 additions & 43 deletions lib/dev/plan/accessor.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,17 +102,16 @@ def link(args, out:)

def link_to_existing(number, file, org:, out:)
path = file ? Pathname.new(file) : sole_unlinked_plan
content = path.read
header, body = Header.split(content)
raise UsageError, "#{path} is already linked to #{header.issue_ref}" if header
plan = Content.parse(path.read)
raise UsageError, "#{path} is already linked to #{plan.header.issue_ref}" if plan.header

owner_repo = target_repo(org:)
issue = @issues.get(owner_repo, number)
# Record the remote as the sync point but keep the local content: the
# file is a draft ahead of the issue until `dev plan push` publishes it.
new_header = Header.new(owner_repo: owner_repo, number: issue.number, synced_at: issue.updated_at)
target = move_into_convention(path, owner_repo, issue)
target.write(new_header.render + body)
target.write(plan.with_header(new_header).render)
@merge_base.write(owner_repo, issue.number, Plan.from_issue_body(issue.body))
out.puts "dev: linked #{target} to #{owner_repo}##{issue.number} (#{issue.html_url})"
out.puts "dev: local content kept — run `dev plan push` to publish it."
Expand All@@ -122,15 +121,14 @@ def create_from_file(file, org:, out:)
path = Pathname.new(file)
raise UsageError, "no such plan file: #{path}" unless path.exist?

content = path.read
header, body = Header.split(content)
raise UsageError, "#{path} is already linked to #{header.issue_ref}" if header
plan = Content.parse(path.read)
raise UsageError, "#{path} is already linked to #{plan.header.issue_ref}" if plan.header

owner_repo = target_repo(org:)
title = extract_title(body) || path.basename(".plan.md").to_s
issue = @issues.create(owner_repo, title: title, body: Plan.to_issue_body(body))
title = extract_title(plan.body) || path.basename(".plan.md").to_s
issue = @issues.create(owner_repo, title: title, body: Plan.to_issue_body(plan.body))
target = move_into_convention(path, owner_repo, issue)
write_linked_plan(owner_repo, issue, body, path: target)
write_linked_plan(owner_repo, issue, plan.body, path: target, frontmatter: plan.frontmatter)
out.puts "dev: created #{owner_repo}##{issue.number} from #{path} (#{issue.html_url})"
out.puts "dev: plan file: #{target}"
end
Expand All@@ -156,30 +154,31 @@ def pull(args, out:)
return
end

_header, local_body = Header.split(path.read)
plan = Content.parse(path.read)
base = @merge_base.read(owner_repo, number)
local_dirty = base.nil? ? false : local_body != base
local_dirty = base.nil? ? false : plan.body != base
remote_dirty = base.nil? ? true : remote_body != base

if !local_dirty
write_linked_plan(owner_repo, issue, remote_body, path: path)
write_linked_plan(owner_repo, issue, remote_body, path: path, frontmatter: plan.frontmatter)
out.puts(remote_dirty ? "dev: pulled #{owner_repo}##{number} into #{path}" : "dev: #{path} is already up to date.")
elsif !remote_dirty
out.puts "dev: local plan is ahead of #{owner_repo}##{number} — nothing to pull. Run `dev plan push`."
elsif merge
merge_pull(path, issue, owner_repo, number, local_body, base, remote_body, out:)
merge_pull(path, issue, owner_repo, number, plan, base, remote_body, out:)
else
raise "both #{path} and #{owner_repo}##{number} changed since the last sync — " \
"run `dev plan pull #{number} --merge`."
end
end

def merge_pull(path, issue, owner_repo, number, local_body, base, remote_body, out:)
result = Merge.three_way(local: local_body, base: base, remote: remote_body, executor: @executor)
def merge_pull(path, issue, owner_repo, number, plan, base, remote_body, out:)
result = Merge.three_way(local: plan.body, base: base, remote: remote_body, executor: @executor)
# The remote becomes the new base either way: the merged local copy is
# now "ahead" of the issue, and `push` publishes it.
# now "ahead" of the issue, and `push` publishes it. Frontmatter is
# carried through from the local side untouched.
header = Header.new(owner_repo: owner_repo, number: number, synced_at: issue.updated_at)
path.write(header.render + result.content)
path.write(plan.with_header(header).with_body(result.content).render)
@merge_base.write(owner_repo, number, remote_body)
if result.conflicts?
out.puts "dev: merged with conflicts — resolve the markers in #{path}, then run `dev plan push`."
Expand All@@ -197,10 +196,11 @@ def push(args, out:)
raise UsageError, "usage: dev plan push [<file>]" unless args.empty?

path = file ? Pathname.new(file) : sole_linked_plan
header, body = Header.split(path.read)
raise UsageError, "#{path} has no ai-flow header — link it first with `dev plan link`." unless header
raise "#{path} contains unresolved merge conflict markers — resolve them before pushing." if body.include?("<<<<<<<")
plan = Content.parse(path.read)
raise UsageError, "#{path} has no ai-flow header — link it first with `dev plan link`." unless plan.header
raise "#{path} contains unresolved merge conflict markers — resolve them before pushing." if plan.body.include?("<<<<<<<")

header = plan.header
issue = @issues.get(header.owner_repo, header.number)
remote_body = Plan.from_issue_body(issue.body)
base = @merge_base.read(header.owner_repo, header.number)
Expand All@@ -214,19 +214,19 @@ def push(args, out:)
"run `dev plan pull #{header.number} --merge`, then push again."
end

if body == remote_body
record_sync(path, header, issue, body)
if plan.body == remote_body
record_sync(path, plan, issue)
out.puts "dev: #{header.issue_ref} is already in sync."
return
end

title = extract_title(body)
title = extract_title(plan.body)
updated = @issues.update(
header.owner_repo, header.number,
body: Plan.to_issue_body(body),
body: Plan.to_issue_body(plan.body),
title: (title if title && title != issue.title),
)
record_sync(path, header, updated, body)
record_sync(path, plan, updated)
out.puts "dev: pushed #{path} to #{header.issue_ref} (#{updated.html_url})"
end

Expand All@@ -244,8 +244,8 @@ def hook_after_edit(input, out:)
return unless path.to_s.end_with?(".plan.md") && path.exist?
return unless path.expand_path.to_s.start_with?(@workspace.plans_dir.expand_path.to_s)

header, _body = Header.split(path.read)
return unless header
plan = Content.parse(path.read)
return unless plan.header

push([path.to_s], out:)
end
Expand All@@ -260,10 +260,10 @@ def status(out:)
end

files.each do |path|
header, body = Header.split(path.read)
issue = @issues.get(header.owner_repo, header.number)
state = sync_state(header, body, Plan.from_issue_body(issue.body))
out.puts "#{state.ljust(10)} #{header.issue_ref.ljust(30)} #{path}"
plan = Content.parse(path.read)
issue = @issues.get(plan.header.owner_repo, plan.header.number)
state = sync_state(plan.header, plan.body, Plan.from_issue_body(issue.body))
out.puts "#{state.ljust(10)} #{plan.header.issue_ref.ljust(30)} #{path}"
end
end

Expand All@@ -286,22 +286,31 @@ def target_repo(org:)
org ? @settings.plans_repo : @workspace.origin_repo
end

# Write the plan file (header + body) and refresh the merge base — the
# single definition of "synced".
# Write the plan file (header + optional frontmatter + markdown body) and
# refresh the merge base — the single definition of "synced". The merge
# base stores the markdown body only.
#
# @param owner_repo [String]
# @param issue [Dev::Plan::GithubIssues::Issue]
# @param body [String] markdown body
# @param path [Pathname, nil]
# @param frontmatter [String, nil] Cursor YAML block to preserve locally
# @return [Pathname] the written path
def write_linked_plan(owner_repo, issue, body, path: nil)
def write_linked_plan(owner_repo, issue, body, path: nil, frontmatter: nil)
path ||= @workspace.plan_path(owner_repo, issue.number, issue.title)
header = Header.new(owner_repo: owner_repo, number: issue.number, synced_at: issue.updated_at)
FileUtils.mkdir_p(path.dirname)
path.write(header.render + body)
path.write(Content.new(header: header, frontmatter: frontmatter, body: body).render)
@merge_base.write(owner_repo, issue.number, body)
path
end

def record_sync(path, header, issue, body)
path.write(header.with_synced_at(issue.updated_at).render + body)
@merge_base.write(header.owner_repo, header.number, body)
# @param path [Pathname]
# @param plan [Dev::Plan::Content]
# @param issue [Dev::Plan::GithubIssues::Issue]
def record_sync(path, plan, issue)
path.write(plan.with_synced_at(issue.updated_at).render)
@merge_base.write(plan.header.owner_repo, plan.header.number, plan.body)
end

# Move a freshly linked file to the `gh-<n>-<slug>.plan.md` convention
Expand All@@ -326,8 +335,8 @@ def extract_title(body)
# @return [Pathname]
def find_linked_plan(owner_repo, number)
@workspace.linked_plan_files.find do |path|
header, _body = Header.split(path.read)
header.owner_repo == owner_repo && header.number == number
plan = Content.parse(path.read)
plan.header.owner_repo == owner_repo && plan.header.number == number
end
end

Expand All@@ -343,8 +352,7 @@ def sole_unlinked_plan
files =
if @workspace.plans_dir.directory?
@workspace.plans_dir.glob(Workspace::PLAN_GLOB).sort.reject do |path|
header, _body = Header.split(path.read)
header
Content.parse(path.read).header
end
else
[]
Expand Down
81 changes: 81 additions & 0 deletions lib/dev/plan/content.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

require "dev/plan/header"
require "dev/plan/frontmatter"

module Dev
module Plan
# The three on-disk layers of a plan file: ai-flow sync header, optional
# Cursor YAML frontmatter, and the markdown body. Sync compares and ships
# the markdown body only; the header and frontmatter stay local.
class Content
# @return [Dev::Plan::Header, nil]
attr_reader :header

# @return [String, nil] raw frontmatter block including `---` fences
attr_reader :frontmatter

# @return [String] markdown body (canonical plan prose)
attr_reader :body

# Parse a plan file into its layers. Canonical on-disk order is header,
# then optional frontmatter, then body. When frontmatter sits above the
# ai-flow header (hand edit), both are still recognized; {#render}
# rewrites canonical order.
#
# @param content [String]
# @return [Content]
def self.parse(content)
header, remainder = Header.split(content)
if header
frontmatter, body = Frontmatter.split(remainder)
return new(header: header, frontmatter: frontmatter, body: body)
end

# Frontmatter may sit above a misplaced ai-flow header.
frontmatter, after_frontmatter = Frontmatter.split(content)
if frontmatter
header, body = Header.split(after_frontmatter)
return new(header: header, frontmatter: frontmatter, body: body)
end

new(header: nil, frontmatter: nil, body: content)
end

# @param header [Dev::Plan::Header, nil]
# @param frontmatter [String, nil]
# @param body [String]
def initialize(header:, frontmatter:, body:)
@header = header
@frontmatter = frontmatter
@body = body
end

# Serialize in canonical order: ai-flow header, optional frontmatter,
# markdown body.
#
# @return [String]
def render
"#{header&.render}#{frontmatter}#{body}"
end

# @param header [Dev::Plan::Header, nil]
# @return [Content]
def with_header(header)
self.class.new(header: header, frontmatter: frontmatter, body: body)
end

# @param body [String]
# @return [Content]
def with_body(body)
self.class.new(header: header, frontmatter: frontmatter, body: body)
end

# @param synced_at [String]
# @return [Content]
def with_synced_at(synced_at)
with_header(header.with_synced_at(synced_at))
end
end
end
end
56 changes: 56 additions & 0 deletions lib/dev/plan/frontmatter.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require "yaml"

module Dev
module Plan
# Cursor plan YAML frontmatter (`---` … `---`) — local editor state
# (picker label, overview, todos). Peeled before any GitHub sync so the
# issue body stays markdown-only.
class Frontmatter
FENCE_LINE = /\A---\n?\z/

# Peel a Cursor-like YAML frontmatter block from the start of +content+.
# Only a leading `---` … `---` fence whose interior is a YAML mapping is
# removed; ordinary markdown horizontal rules deeper in the body, or a
# leading `---` that is not a mapping, are left alone.
#
# @param content [String]
# @return [Array(String | nil, String)] frontmatter block (including
# fences and a trailing newline after the closing fence) or nil, and
# the remainder
def self.split(content)
lines = content.lines
return [nil, content] if lines.empty? || !fence?(lines.fetch(0))

close_index = (1...lines.length).find { |index| fence?(lines.fetch(index)) }
return [nil, content] unless close_index

yaml_text = lines[1...close_index].join
return [nil, content] unless mapping?(yaml_text)

frontmatter = lines[0..close_index].join
frontmatter = "#{frontmatter}\n" unless frontmatter.end_with?("\n")
body = lines[(close_index + 1)..].join
[frontmatter, body]
end

# @param line [String]
# @return [Boolean]
def self.fence?(line)
line.match?(FENCE_LINE)
end
private_class_method :fence?

# @param yaml_text [String] interior between fences
# @return [Boolean] true when the interior parses as a YAML mapping
def self.mapping?(yaml_text)
parsed = YAML.safe_load(yaml_text)
parsed.is_a?(Hash)
rescue Psych::SyntaxError, Psych::DisallowedClass
false
end
private_class_method :mapping?
end
end
end
12 changes: 7 additions & 5 deletions lib/dev/plan/header.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,18 +4,20 @@ module Dev
module Plan
module_function

# Render a plan body as an issue body: verbatim content, normalized to a
# single trailing newline.
# Normalize a markdown plan body for the issue: LF, single trailing
# newline. Callers pass the markdown body only — not Cursor YAML
# frontmatter or the ai-flow sync header.
#
# @param plan_body [String]
# @return [String]
def to_issue_body(plan_body)
"#{plan_body.rstrip}\n"
end

# Extract the plan body from an issue body. Issue bodies use CRLF line
# endings when edited via the GitHub web UI, so normalize to LF — the
# local file and merge base always use LF.
# Normalize an issue body to a local markdown plan body. Issue bodies use
# CRLF when edited via the GitHub web UI, so normalize to LF — the local
# file and merge base always use LF. The result is markdown only (GitHub
# never stores Cursor frontmatter).
#
# @param issue_body [String, nil]
# @return [String]
Expand Down
3 changes: 1 addition & 2 deletions lib/dev/plan/workspace.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,8 +53,7 @@ def linked_plan_files
return [] unless @plans_dir.directory?

@plans_dir.glob(PLAN_GLOB).sort.select do |path|
header, _body = Header.split(path.read)
!header.nil?
!Content.parse(path.read).header.nil?
end
end

Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/dev/plan.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,8 @@

require "dev/plan/executor"
require "dev/plan/header"
require "dev/plan/frontmatter"
require "dev/plan/content"
require "dev/plan/settings"
require "dev/plan/github_issues"
require "dev/plan/workspace"
Expand Down
94 changes: 51 additions & 43 deletions lib/dev/plan/accessor.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,17 +102,16 @@ def link(args, out:)

def link_to_existing(number, file, org:, out:)
path = file ? Pathname.new(file) : sole_unlinked_plan
content = path.read
header, body = Header.split(content)
raise UsageError, "#{path} is already linked to #{header.issue_ref}" if header
plan = Content.parse(path.read)
raise UsageError, "#{path} is already linked to #{plan.header.issue_ref}" if plan.header

owner_repo = target_repo(org:)
issue = @issues.get(owner_repo, number)
# Record the remote as the sync point but keep the local content: the
# file is a draft ahead of the issue until `dev plan push` publishes it.
new_header = Header.new(owner_repo: owner_repo, number: issue.number, synced_at: issue.updated_at)
target = move_into_convention(path, owner_repo, issue)
target.write(new_header.render + body)
target.write(plan.with_header(new_header).render)
@merge_base.write(owner_repo, issue.number, Plan.from_issue_body(issue.body))
out.puts "dev: linked #{target} to #{owner_repo}##{issue.number} (#{issue.html_url})"
out.puts "dev: local content kept — run `dev plan push` to publish it."
Expand All@@ -122,15 +121,14 @@ def create_from_file(file, org:, out:)
path = Pathname.new(file)
raise UsageError, "no such plan file: #{path}" unless path.exist?

content = path.read
header, body = Header.split(content)
raise UsageError, "#{path} is already linked to #{header.issue_ref}" if header
plan = Content.parse(path.read)
raise UsageError, "#{path} is already linked to #{plan.header.issue_ref}" if plan.header

owner_repo = target_repo(org:)
title = extract_title(body) || path.basename(".plan.md").to_s
issue = @issues.create(owner_repo, title: title, body: Plan.to_issue_body(body))
title = extract_title(plan.body) || path.basename(".plan.md").to_s
issue = @issues.create(owner_repo, title: title, body: Plan.to_issue_body(plan.body))
target = move_into_convention(path, owner_repo, issue)
write_linked_plan(owner_repo, issue, body, path: target)
write_linked_plan(owner_repo, issue, plan.body, path: target, frontmatter: plan.frontmatter)
out.puts "dev: created #{owner_repo}##{issue.number} from #{path} (#{issue.html_url})"
out.puts "dev: plan file: #{target}"
end
Expand All@@ -156,30 +154,31 @@ def pull(args, out:)
return
end

_header, local_body = Header.split(path.read)
plan = Content.parse(path.read)
base = @merge_base.read(owner_repo, number)
local_dirty = base.nil? ? false : local_body != base
local_dirty = base.nil? ? false : plan.body != base
remote_dirty = base.nil? ? true : remote_body != base

if !local_dirty
write_linked_plan(owner_repo, issue, remote_body, path: path)
write_linked_plan(owner_repo, issue, remote_body, path: path, frontmatter: plan.frontmatter)
out.puts(remote_dirty ? "dev: pulled #{owner_repo}##{number} into #{path}" : "dev: #{path} is already up to date.")
elsif !remote_dirty
out.puts "dev: local plan is ahead of #{owner_repo}##{number} — nothing to pull. Run `dev plan push`."
elsif merge
merge_pull(path, issue, owner_repo, number, local_body, base, remote_body, out:)
merge_pull(path, issue, owner_repo, number, plan, base, remote_body, out:)
else
raise "both #{path} and #{owner_repo}##{number} changed since the last sync — " \
"run `dev plan pull #{number} --merge`."
end
end

def merge_pull(path, issue, owner_repo, number, local_body, base, remote_body, out:)
result = Merge.three_way(local: local_body, base: base, remote: remote_body, executor: @executor)
def merge_pull(path, issue, owner_repo, number, plan, base, remote_body, out:)
result = Merge.three_way(local: plan.body, base: base, remote: remote_body, executor: @executor)
# The remote becomes the new base either way: the merged local copy is
# now "ahead" of the issue, and `push` publishes it.
# now "ahead" of the issue, and `push` publishes it. Frontmatter is
# carried through from the local side untouched.
header = Header.new(owner_repo: owner_repo, number: number, synced_at: issue.updated_at)
path.write(header.render + result.content)
path.write(plan.with_header(header).with_body(result.content).render)
@merge_base.write(owner_repo, number, remote_body)
if result.conflicts?
out.puts "dev: merged with conflicts — resolve the markers in #{path}, then run `dev plan push`."
Expand All@@ -197,10 +196,11 @@ def push(args, out:)
raise UsageError, "usage: dev plan push [<file>]" unless args.empty?

path = file ? Pathname.new(file) : sole_linked_plan
header, body = Header.split(path.read)
raise UsageError, "#{path} has no ai-flow header — link it first with `dev plan link`." unless header
raise "#{path} contains unresolved merge conflict markers — resolve them before pushing." if body.include?("<<<<<<<")
plan = Content.parse(path.read)
raise UsageError, "#{path} has no ai-flow header — link it first with `dev plan link`." unless plan.header
raise "#{path} contains unresolved merge conflict markers — resolve them before pushing." if plan.body.include?("<<<<<<<")

header = plan.header
issue = @issues.get(header.owner_repo, header.number)
remote_body = Plan.from_issue_body(issue.body)
base = @merge_base.read(header.owner_repo, header.number)
Expand All@@ -214,19 +214,19 @@ def push(args, out:)
"run `dev plan pull #{header.number} --merge`, then push again."
end

if body == remote_body
record_sync(path, header, issue, body)
if plan.body == remote_body
record_sync(path, plan, issue)
out.puts "dev: #{header.issue_ref} is already in sync."
return
end

title = extract_title(body)
title = extract_title(plan.body)
updated = @issues.update(
header.owner_repo, header.number,
body: Plan.to_issue_body(body),
body: Plan.to_issue_body(plan.body),
title: (title if title && title != issue.title),
)
record_sync(path, header, updated, body)
record_sync(path, plan, updated)
out.puts "dev: pushed #{path} to #{header.issue_ref} (#{updated.html_url})"
end

Expand All@@ -244,8 +244,8 @@ def hook_after_edit(input, out:)
return unless path.to_s.end_with?(".plan.md") && path.exist?
return unless path.expand_path.to_s.start_with?(@workspace.plans_dir.expand_path.to_s)

header, _body = Header.split(path.read)
return unless header
plan = Content.parse(path.read)
return unless plan.header

push([path.to_s], out:)
end
Expand All@@ -260,10 +260,10 @@ def status(out:)
end

files.each do |path|
header, body = Header.split(path.read)
issue = @issues.get(header.owner_repo, header.number)
state = sync_state(header, body, Plan.from_issue_body(issue.body))
out.puts "#{state.ljust(10)} #{header.issue_ref.ljust(30)} #{path}"
plan = Content.parse(path.read)
issue = @issues.get(plan.header.owner_repo, plan.header.number)
state = sync_state(plan.header, plan.body, Plan.from_issue_body(issue.body))
out.puts "#{state.ljust(10)} #{plan.header.issue_ref.ljust(30)} #{path}"
end
end

Expand All@@ -286,22 +286,31 @@ def target_repo(org:)
org ? @settings.plans_repo : @workspace.origin_repo
end

# Write the plan file (header + body) and refresh the merge base — the
# single definition of "synced".
# Write the plan file (header + optional frontmatter + markdown body) and
# refresh the merge base — the single definition of "synced". The merge
# base stores the markdown body only.
#
# @param owner_repo [String]
# @param issue [Dev::Plan::GithubIssues::Issue]
# @param body [String] markdown body
# @param path [Pathname, nil]
# @param frontmatter [String, nil] Cursor YAML block to preserve locally
# @return [Pathname] the written path
def write_linked_plan(owner_repo, issue, body, path: nil)
def write_linked_plan(owner_repo, issue, body, path: nil, frontmatter: nil)
path ||= @workspace.plan_path(owner_repo, issue.number, issue.title)
header = Header.new(owner_repo: owner_repo, number: issue.number, synced_at: issue.updated_at)
FileUtils.mkdir_p(path.dirname)
path.write(header.render + body)
path.write(Content.new(header: header, frontmatter: frontmatter, body: body).render)
@merge_base.write(owner_repo, issue.number, body)
path
end

def record_sync(path, header, issue, body)
path.write(header.with_synced_at(issue.updated_at).render + body)
@merge_base.write(header.owner_repo, header.number, body)
# @param path [Pathname]
# @param plan [Dev::Plan::Content]
# @param issue [Dev::Plan::GithubIssues::Issue]
def record_sync(path, plan, issue)
path.write(plan.with_synced_at(issue.updated_at).render)
@merge_base.write(plan.header.owner_repo, plan.header.number, plan.body)
end

# Move a freshly linked file to the `gh-<n>-<slug>.plan.md` convention
Expand All@@ -326,8 +335,8 @@ def extract_title(body)
# @return [Pathname]
def find_linked_plan(owner_repo, number)
@workspace.linked_plan_files.find do |path|
header, _body = Header.split(path.read)
header.owner_repo == owner_repo && header.number == number
plan = Content.parse(path.read)
plan.header.owner_repo == owner_repo && plan.header.number == number
end
end

Expand All@@ -343,8 +352,7 @@ def sole_unlinked_plan
files =
if @workspace.plans_dir.directory?
@workspace.plans_dir.glob(Workspace::PLAN_GLOB).sort.reject do |path|
header, _body = Header.split(path.read)
header
Content.parse(path.read).header
end
else
[]
Expand Down
81 changes: 81 additions & 0 deletions lib/dev/plan/content.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

require "dev/plan/header"
require "dev/plan/frontmatter"

module Dev
module Plan
# The three on-disk layers of a plan file: ai-flow sync header, optional
# Cursor YAML frontmatter, and the markdown body. Sync compares and ships
# the markdown body only; the header and frontmatter stay local.
class Content
# @return [Dev::Plan::Header, nil]
attr_reader :header

# @return [String, nil] raw frontmatter block including `---` fences
attr_reader :frontmatter

# @return [String] markdown body (canonical plan prose)
attr_reader :body

# Parse a plan file into its layers. Canonical on-disk order is header,
# then optional frontmatter, then body. When frontmatter sits above the
# ai-flow header (hand edit), both are still recognized; {#render}
# rewrites canonical order.
#
# @param content [String]
# @return [Content]
def self.parse(content)
header, remainder = Header.split(content)
if header
frontmatter, body = Frontmatter.split(remainder)
return new(header: header, frontmatter: frontmatter, body: body)
end

# Frontmatter may sit above a misplaced ai-flow header.
frontmatter, after_frontmatter = Frontmatter.split(content)
if frontmatter
header, body = Header.split(after_frontmatter)
return new(header: header, frontmatter: frontmatter, body: body)
end

new(header: nil, frontmatter: nil, body: content)
end

# @param header [Dev::Plan::Header, nil]
# @param frontmatter [String, nil]
# @param body [String]
def initialize(header:, frontmatter:, body:)
@header = header
@frontmatter = frontmatter
@body = body
end

# Serialize in canonical order: ai-flow header, optional frontmatter,
# markdown body.
#
# @return [String]
def render
"#{header&.render}#{frontmatter}#{body}"
end

# @param header [Dev::Plan::Header, nil]
# @return [Content]
def with_header(header)
self.class.new(header: header, frontmatter: frontmatter, body: body)
end

# @param body [String]
# @return [Content]
def with_body(body)
self.class.new(header: header, frontmatter: frontmatter, body: body)
end

# @param synced_at [String]
# @return [Content]
def with_synced_at(synced_at)
with_header(header.with_synced_at(synced_at))
end
end
end
end
56 changes: 56 additions & 0 deletions lib/dev/plan/frontmatter.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require "yaml"

module Dev
module Plan
# Cursor plan YAML frontmatter (`---` … `---`) — local editor state
# (picker label, overview, todos). Peeled before any GitHub sync so the
# issue body stays markdown-only.
class Frontmatter
FENCE_LINE = /\A---\n?\z/

# Peel a Cursor-like YAML frontmatter block from the start of +content+.
# Only a leading `---` … `---` fence whose interior is a YAML mapping is
# removed; ordinary markdown horizontal rules deeper in the body, or a
# leading `---` that is not a mapping, are left alone.
#
# @param content [String]
# @return [Array(String | nil, String)] frontmatter block (including
# fences and a trailing newline after the closing fence) or nil, and
# the remainder
def self.split(content)
lines = content.lines
return [nil, content] if lines.empty? || !fence?(lines.fetch(0))

close_index = (1...lines.length).find { |index| fence?(lines.fetch(index)) }
return [nil, content] unless close_index

yaml_text = lines[1...close_index].join
return [nil, content] unless mapping?(yaml_text)

frontmatter = lines[0..close_index].join
frontmatter = "#{frontmatter}\n" unless frontmatter.end_with?("\n")
body = lines[(close_index + 1)..].join
[frontmatter, body]
end

# @param line [String]
# @return [Boolean]
def self.fence?(line)
line.match?(FENCE_LINE)
end
private_class_method :fence?

# @param yaml_text [String] interior between fences
# @return [Boolean] true when the interior parses as a YAML mapping
def self.mapping?(yaml_text)
parsed = YAML.safe_load(yaml_text)
parsed.is_a?(Hash)
rescue Psych::SyntaxError, Psych::DisallowedClass
false
end
private_class_method :mapping?
end
end
end
12 changes: 7 additions & 5 deletions lib/dev/plan/header.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,18 +4,20 @@ module Dev
module Plan
module_function

# Render a plan body as an issue body: verbatim content, normalized to a
# single trailing newline.
# Normalize a markdown plan body for the issue: LF, single trailing
# newline. Callers pass the markdown body only — not Cursor YAML
# frontmatter or the ai-flow sync header.
#
# @param plan_body [String]
# @return [String]
def to_issue_body(plan_body)
"#{plan_body.rstrip}\n"
end

# Extract the plan body from an issue body. Issue bodies use CRLF line
# endings when edited via the GitHub web UI, so normalize to LF — the
# local file and merge base always use LF.
# Normalize an issue body to a local markdown plan body. Issue bodies use
# CRLF when edited via the GitHub web UI, so normalize to LF — the local
# file and merge base always use LF. The result is markdown only (GitHub
# never stores Cursor frontmatter).
#
# @param issue_body [String, nil]
# @return [String]
Expand Down
3 changes: 1 addition & 2 deletions lib/dev/plan/workspace.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,8 +53,7 @@ def linked_plan_files
return [] unless @plans_dir.directory?

@plans_dir.glob(PLAN_GLOB).sort.select do |path|
header, _body = Header.split(path.read)
!header.nil?
!Content.parse(path.read).header.nil?
end
end

Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/dev/plan.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,8 @@

require "dev/plan/executor"
require "dev/plan/header"
require "dev/plan/frontmatter"
require "dev/plan/content"
require "dev/plan/settings"
require "dev/plan/github_issues"
require "dev/plan/workspace"
Expand Down
94 changes: 51 additions & 43 deletions lib/dev/plan/accessor.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,17 +102,16 @@ def link(args, out:)

def link_to_existing(number, file, org:, out:)
path = file ? Pathname.new(file) : sole_unlinked_plan
content = path.read
header, body = Header.split(content)
raise UsageError, "#{path} is already linked to #{header.issue_ref}" if header
plan = Content.parse(path.read)
raise UsageError, "#{path} is already linked to #{plan.header.issue_ref}" if plan.header

owner_repo = target_repo(org:)
issue = @issues.get(owner_repo, number)
# Record the remote as the sync point but keep the local content: the
# file is a draft ahead of the issue until `dev plan push` publishes it.
new_header = Header.new(owner_repo: owner_repo, number: issue.number, synced_at: issue.updated_at)
target = move_into_convention(path, owner_repo, issue)
target.write(new_header.render + body)
target.write(plan.with_header(new_header).render)
@merge_base.write(owner_repo, issue.number, Plan.from_issue_body(issue.body))
out.puts "dev: linked #{target} to #{owner_repo}##{issue.number} (#{issue.html_url})"
out.puts "dev: local content kept — run `dev plan push` to publish it."
Expand All@@ -122,15 +121,14 @@ def create_from_file(file, org:, out:)
path = Pathname.new(file)
raise UsageError, "no such plan file: #{path}" unless path.exist?

content = path.read
header, body = Header.split(content)
raise UsageError, "#{path} is already linked to #{header.issue_ref}" if header
plan = Content.parse(path.read)
raise UsageError, "#{path} is already linked to #{plan.header.issue_ref}" if plan.header

owner_repo = target_repo(org:)
title = extract_title(body) || path.basename(".plan.md").to_s
issue = @issues.create(owner_repo, title: title, body: Plan.to_issue_body(body))
title = extract_title(plan.body) || path.basename(".plan.md").to_s
issue = @issues.create(owner_repo, title: title, body: Plan.to_issue_body(plan.body))
target = move_into_convention(path, owner_repo, issue)
write_linked_plan(owner_repo, issue, body, path: target)
write_linked_plan(owner_repo, issue, plan.body, path: target, frontmatter: plan.frontmatter)
out.puts "dev: created #{owner_repo}##{issue.number} from #{path} (#{issue.html_url})"
out.puts "dev: plan file: #{target}"
end
Expand All@@ -156,30 +154,31 @@ def pull(args, out:)
return
end

_header, local_body = Header.split(path.read)
plan = Content.parse(path.read)
base = @merge_base.read(owner_repo, number)
local_dirty = base.nil? ? false : local_body != base
local_dirty = base.nil? ? false : plan.body != base
remote_dirty = base.nil? ? true : remote_body != base

if !local_dirty
write_linked_plan(owner_repo, issue, remote_body, path: path)
write_linked_plan(owner_repo, issue, remote_body, path: path, frontmatter: plan.frontmatter)
out.puts(remote_dirty ? "dev: pulled #{owner_repo}##{number} into #{path}" : "dev: #{path} is already up to date.")
elsif !remote_dirty
out.puts "dev: local plan is ahead of #{owner_repo}##{number} — nothing to pull. Run `dev plan push`."
elsif merge
merge_pull(path, issue, owner_repo, number, local_body, base, remote_body, out:)
merge_pull(path, issue, owner_repo, number, plan, base, remote_body, out:)
else
raise "both #{path} and #{owner_repo}##{number} changed since the last sync — " \
"run `dev plan pull #{number} --merge`."
end
end

def merge_pull(path, issue, owner_repo, number, local_body, base, remote_body, out:)
result = Merge.three_way(local: local_body, base: base, remote: remote_body, executor: @executor)
def merge_pull(path, issue, owner_repo, number, plan, base, remote_body, out:)
result = Merge.three_way(local: plan.body, base: base, remote: remote_body, executor: @executor)
# The remote becomes the new base either way: the merged local copy is
# now "ahead" of the issue, and `push` publishes it.
# now "ahead" of the issue, and `push` publishes it. Frontmatter is
# carried through from the local side untouched.
header = Header.new(owner_repo: owner_repo, number: number, synced_at: issue.updated_at)
path.write(header.render + result.content)
path.write(plan.with_header(header).with_body(result.content).render)
@merge_base.write(owner_repo, number, remote_body)
if result.conflicts?
out.puts "dev: merged with conflicts — resolve the markers in #{path}, then run `dev plan push`."
Expand All@@ -197,10 +196,11 @@ def push(args, out:)
raise UsageError, "usage: dev plan push [<file>]" unless args.empty?

path = file ? Pathname.new(file) : sole_linked_plan
header, body = Header.split(path.read)
raise UsageError, "#{path} has no ai-flow header — link it first with `dev plan link`." unless header
raise "#{path} contains unresolved merge conflict markers — resolve them before pushing." if body.include?("<<<<<<<")
plan = Content.parse(path.read)
raise UsageError, "#{path} has no ai-flow header — link it first with `dev plan link`." unless plan.header
raise "#{path} contains unresolved merge conflict markers — resolve them before pushing." if plan.body.include?("<<<<<<<")

header = plan.header
issue = @issues.get(header.owner_repo, header.number)
remote_body = Plan.from_issue_body(issue.body)
base = @merge_base.read(header.owner_repo, header.number)
Expand All@@ -214,19 +214,19 @@ def push(args, out:)
"run `dev plan pull #{header.number} --merge`, then push again."
end

if body == remote_body
record_sync(path, header, issue, body)
if plan.body == remote_body
record_sync(path, plan, issue)
out.puts "dev: #{header.issue_ref} is already in sync."
return
end

title = extract_title(body)
title = extract_title(plan.body)
updated = @issues.update(
header.owner_repo, header.number,
body: Plan.to_issue_body(body),
body: Plan.to_issue_body(plan.body),
title: (title if title && title != issue.title),
)
record_sync(path, header, updated, body)
record_sync(path, plan, updated)
out.puts "dev: pushed #{path} to #{header.issue_ref} (#{updated.html_url})"
end

Expand All@@ -244,8 +244,8 @@ def hook_after_edit(input, out:)
return unless path.to_s.end_with?(".plan.md") && path.exist?
return unless path.expand_path.to_s.start_with?(@workspace.plans_dir.expand_path.to_s)

header, _body = Header.split(path.read)
return unless header
plan = Content.parse(path.read)
return unless plan.header

push([path.to_s], out:)
end
Expand All@@ -260,10 +260,10 @@ def status(out:)
end

files.each do |path|
header, body = Header.split(path.read)
issue = @issues.get(header.owner_repo, header.number)
state = sync_state(header, body, Plan.from_issue_body(issue.body))
out.puts "#{state.ljust(10)} #{header.issue_ref.ljust(30)} #{path}"
plan = Content.parse(path.read)
issue = @issues.get(plan.header.owner_repo, plan.header.number)
state = sync_state(plan.header, plan.body, Plan.from_issue_body(issue.body))
out.puts "#{state.ljust(10)} #{plan.header.issue_ref.ljust(30)} #{path}"
end
end

Expand All@@ -286,22 +286,31 @@ def target_repo(org:)
org ? @settings.plans_repo : @workspace.origin_repo
end

# Write the plan file (header + body) and refresh the merge base — the
# single definition of "synced".
# Write the plan file (header + optional frontmatter + markdown body) and
# refresh the merge base — the single definition of "synced". The merge
# base stores the markdown body only.
#
# @param owner_repo [String]
# @param issue [Dev::Plan::GithubIssues::Issue]
# @param body [String] markdown body
# @param path [Pathname, nil]
# @param frontmatter [String, nil] Cursor YAML block to preserve locally
# @return [Pathname] the written path
def write_linked_plan(owner_repo, issue, body, path: nil)
def write_linked_plan(owner_repo, issue, body, path: nil, frontmatter: nil)
path ||= @workspace.plan_path(owner_repo, issue.number, issue.title)
header = Header.new(owner_repo: owner_repo, number: issue.number, synced_at: issue.updated_at)
FileUtils.mkdir_p(path.dirname)
path.write(header.render + body)
path.write(Content.new(header: header, frontmatter: frontmatter, body: body).render)
@merge_base.write(owner_repo, issue.number, body)
path
end

def record_sync(path, header, issue, body)
path.write(header.with_synced_at(issue.updated_at).render + body)
@merge_base.write(header.owner_repo, header.number, body)
# @param path [Pathname]
# @param plan [Dev::Plan::Content]
# @param issue [Dev::Plan::GithubIssues::Issue]
def record_sync(path, plan, issue)
path.write(plan.with_synced_at(issue.updated_at).render)
@merge_base.write(plan.header.owner_repo, plan.header.number, plan.body)
end

# Move a freshly linked file to the `gh-<n>-<slug>.plan.md` convention
Expand All@@ -326,8 +335,8 @@ def extract_title(body)
# @return [Pathname]
def find_linked_plan(owner_repo, number)
@workspace.linked_plan_files.find do |path|
header, _body = Header.split(path.read)
header.owner_repo == owner_repo && header.number == number
plan = Content.parse(path.read)
plan.header.owner_repo == owner_repo && plan.header.number == number
end
end

Expand All@@ -343,8 +352,7 @@ def sole_unlinked_plan
files =
if @workspace.plans_dir.directory?
@workspace.plans_dir.glob(Workspace::PLAN_GLOB).sort.reject do |path|
header, _body = Header.split(path.read)
header
Content.parse(path.read).header
end
else
[]
Expand Down
81 changes: 81 additions & 0 deletions lib/dev/plan/content.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

require "dev/plan/header"
require "dev/plan/frontmatter"

module Dev
module Plan
# The three on-disk layers of a plan file: ai-flow sync header, optional
# Cursor YAML frontmatter, and the markdown body. Sync compares and ships
# the markdown body only; the header and frontmatter stay local.
class Content
# @return [Dev::Plan::Header, nil]
attr_reader :header

# @return [String, nil] raw frontmatter block including `---` fences
attr_reader :frontmatter

# @return [String] markdown body (canonical plan prose)
attr_reader :body

# Parse a plan file into its layers. Canonical on-disk order is header,
# then optional frontmatter, then body. When frontmatter sits above the
# ai-flow header (hand edit), both are still recognized; {#render}
# rewrites canonical order.
#
# @param content [String]
# @return [Content]
def self.parse(content)
header, remainder = Header.split(content)
if header
frontmatter, body = Frontmatter.split(remainder)
return new(header: header, frontmatter: frontmatter, body: body)
end

# Frontmatter may sit above a misplaced ai-flow header.
frontmatter, after_frontmatter = Frontmatter.split(content)
if frontmatter
header, body = Header.split(after_frontmatter)
return new(header: header, frontmatter: frontmatter, body: body)
end

new(header: nil, frontmatter: nil, body: content)
end

# @param header [Dev::Plan::Header, nil]
# @param frontmatter [String, nil]
# @param body [String]
def initialize(header:, frontmatter:, body:)
@header = header
@frontmatter = frontmatter
@body = body
end

# Serialize in canonical order: ai-flow header, optional frontmatter,
# markdown body.
#
# @return [String]
def render
"#{header&.render}#{frontmatter}#{body}"
end

# @param header [Dev::Plan::Header, nil]
# @return [Content]
def with_header(header)
self.class.new(header: header, frontmatter: frontmatter, body: body)
end

# @param body [String]
# @return [Content]
def with_body(body)
self.class.new(header: header, frontmatter: frontmatter, body: body)
end

# @param synced_at [String]
# @return [Content]
def with_synced_at(synced_at)
with_header(header.with_synced_at(synced_at))
end
end
end
end
56 changes: 56 additions & 0 deletions lib/dev/plan/frontmatter.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require "yaml"

module Dev
module Plan
# Cursor plan YAML frontmatter (`---` … `---`) — local editor state
# (picker label, overview, todos). Peeled before any GitHub sync so the
# issue body stays markdown-only.
class Frontmatter
FENCE_LINE = /\A---\n?\z/

# Peel a Cursor-like YAML frontmatter block from the start of +content+.
# Only a leading `---` … `---` fence whose interior is a YAML mapping is
# removed; ordinary markdown horizontal rules deeper in the body, or a
# leading `---` that is not a mapping, are left alone.
#
# @param content [String]
# @return [Array(String | nil, String)] frontmatter block (including
# fences and a trailing newline after the closing fence) or nil, and
# the remainder
def self.split(content)
lines = content.lines
return [nil, content] if lines.empty? || !fence?(lines.fetch(0))

close_index = (1...lines.length).find { |index| fence?(lines.fetch(index)) }
return [nil, content] unless close_index

yaml_text = lines[1...close_index].join
return [nil, content] unless mapping?(yaml_text)

frontmatter = lines[0..close_index].join
frontmatter = "#{frontmatter}\n" unless frontmatter.end_with?("\n")
body = lines[(close_index + 1)..].join
[frontmatter, body]
end

# @param line [String]
# @return [Boolean]
def self.fence?(line)
line.match?(FENCE_LINE)
end
private_class_method :fence?

# @param yaml_text [String] interior between fences
# @return [Boolean] true when the interior parses as a YAML mapping
def self.mapping?(yaml_text)
parsed = YAML.safe_load(yaml_text)
parsed.is_a?(Hash)
rescue Psych::SyntaxError, Psych::DisallowedClass
false
end
private_class_method :mapping?
end
end
end
12 changes: 7 additions & 5 deletions lib/dev/plan/header.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,18 +4,20 @@ module Dev
module Plan
module_function

# Render a plan body as an issue body: verbatim content, normalized to a
# single trailing newline.
# Normalize a markdown plan body for the issue: LF, single trailing
# newline. Callers pass the markdown body only — not Cursor YAML
# frontmatter or the ai-flow sync header.
#
# @param plan_body [String]
# @return [String]
def to_issue_body(plan_body)
"#{plan_body.rstrip}\n"
end

# Extract the plan body from an issue body. Issue bodies use CRLF line
# endings when edited via the GitHub web UI, so normalize to LF — the
# local file and merge base always use LF.
# Normalize an issue body to a local markdown plan body. Issue bodies use
# CRLF when edited via the GitHub web UI, so normalize to LF — the local
# file and merge base always use LF. The result is markdown only (GitHub
# never stores Cursor frontmatter).
#
# @param issue_body [String, nil]
# @return [String]
Expand Down
3 changes: 1 addition & 2 deletions lib/dev/plan/workspace.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,8 +53,7 @@ def linked_plan_files
return [] unless @plans_dir.directory?

@plans_dir.glob(PLAN_GLOB).sort.select do |path|
header, _body = Header.split(path.read)
!header.nil?
!Content.parse(path.read).header.nil?
end
end

Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/dev/plan.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,8 @@

require "dev/plan/executor"
require "dev/plan/header"
require "dev/plan/frontmatter"
require "dev/plan/content"
require "dev/plan/settings"
require "dev/plan/github_issues"
require "dev/plan/workspace"
Expand Down
94 changes: 51 additions & 43 deletions lib/dev/plan/accessor.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,17 +102,16 @@ def link(args, out:)

def link_to_existing(number, file, org:, out:)
path = file ? Pathname.new(file) : sole_unlinked_plan
content = path.read
header, body = Header.split(content)
raise UsageError, "#{path} is already linked to #{header.issue_ref}" if header
plan = Content.parse(path.read)
raise UsageError, "#{path} is already linked to #{plan.header.issue_ref}" if plan.header

owner_repo = target_repo(org:)
issue = @issues.get(owner_repo, number)
# Record the remote as the sync point but keep the local content: the
# file is a draft ahead of the issue until `dev plan push` publishes it.
new_header = Header.new(owner_repo: owner_repo, number: issue.number, synced_at: issue.updated_at)
target = move_into_convention(path, owner_repo, issue)
target.write(new_header.render + body)
target.write(plan.with_header(new_header).render)
@merge_base.write(owner_repo, issue.number, Plan.from_issue_body(issue.body))
out.puts "dev: linked #{target} to #{owner_repo}##{issue.number} (#{issue.html_url})"
out.puts "dev: local content kept — run `dev plan push` to publish it."
Expand All@@ -122,15 +121,14 @@ def create_from_file(file, org:, out:)
path = Pathname.new(file)
raise UsageError, "no such plan file: #{path}" unless path.exist?

content = path.read
header, body = Header.split(content)
raise UsageError, "#{path} is already linked to #{header.issue_ref}" if header
plan = Content.parse(path.read)
raise UsageError, "#{path} is already linked to #{plan.header.issue_ref}" if plan.header

owner_repo = target_repo(org:)
title = extract_title(body) || path.basename(".plan.md").to_s
issue = @issues.create(owner_repo, title: title, body: Plan.to_issue_body(body))
title = extract_title(plan.body) || path.basename(".plan.md").to_s
issue = @issues.create(owner_repo, title: title, body: Plan.to_issue_body(plan.body))
target = move_into_convention(path, owner_repo, issue)
write_linked_plan(owner_repo, issue, body, path: target)
write_linked_plan(owner_repo, issue, plan.body, path: target, frontmatter: plan.frontmatter)
out.puts "dev: created #{owner_repo}##{issue.number} from #{path} (#{issue.html_url})"
out.puts "dev: plan file: #{target}"
end
Expand All@@ -156,30 +154,31 @@ def pull(args, out:)
return
end

_header, local_body = Header.split(path.read)
plan = Content.parse(path.read)
base = @merge_base.read(owner_repo, number)
local_dirty = base.nil? ? false : local_body != base
local_dirty = base.nil? ? false : plan.body != base
remote_dirty = base.nil? ? true : remote_body != base

if !local_dirty
write_linked_plan(owner_repo, issue, remote_body, path: path)
write_linked_plan(owner_repo, issue, remote_body, path: path, frontmatter: plan.frontmatter)
out.puts(remote_dirty ? "dev: pulled #{owner_repo}##{number} into #{path}" : "dev: #{path} is already up to date.")
elsif !remote_dirty
out.puts "dev: local plan is ahead of #{owner_repo}##{number} — nothing to pull. Run `dev plan push`."
elsif merge
merge_pull(path, issue, owner_repo, number, local_body, base, remote_body, out:)
merge_pull(path, issue, owner_repo, number, plan, base, remote_body, out:)
else
raise "both #{path} and #{owner_repo}##{number} changed since the last sync — " \
"run `dev plan pull #{number} --merge`."
end
end

def merge_pull(path, issue, owner_repo, number, local_body, base, remote_body, out:)
result = Merge.three_way(local: local_body, base: base, remote: remote_body, executor: @executor)
def merge_pull(path, issue, owner_repo, number, plan, base, remote_body, out:)
result = Merge.three_way(local: plan.body, base: base, remote: remote_body, executor: @executor)
# The remote becomes the new base either way: the merged local copy is
# now "ahead" of the issue, and `push` publishes it.
# now "ahead" of the issue, and `push` publishes it. Frontmatter is
# carried through from the local side untouched.
header = Header.new(owner_repo: owner_repo, number: number, synced_at: issue.updated_at)
path.write(header.render + result.content)
path.write(plan.with_header(header).with_body(result.content).render)
@merge_base.write(owner_repo, number, remote_body)
if result.conflicts?
out.puts "dev: merged with conflicts — resolve the markers in #{path}, then run `dev plan push`."
Expand All@@ -197,10 +196,11 @@ def push(args, out:)
raise UsageError, "usage: dev plan push [<file>]" unless args.empty?

path = file ? Pathname.new(file) : sole_linked_plan
header, body = Header.split(path.read)
raise UsageError, "#{path} has no ai-flow header — link it first with `dev plan link`." unless header
raise "#{path} contains unresolved merge conflict markers — resolve them before pushing." if body.include?("<<<<<<<")
plan = Content.parse(path.read)
raise UsageError, "#{path} has no ai-flow header — link it first with `dev plan link`." unless plan.header
raise "#{path} contains unresolved merge conflict markers — resolve them before pushing." if plan.body.include?("<<<<<<<")

header = plan.header
issue = @issues.get(header.owner_repo, header.number)
remote_body = Plan.from_issue_body(issue.body)
base = @merge_base.read(header.owner_repo, header.number)
Expand All@@ -214,19 +214,19 @@ def push(args, out:)
"run `dev plan pull #{header.number} --merge`, then push again."
end

if body == remote_body
record_sync(path, header, issue, body)
if plan.body == remote_body
record_sync(path, plan, issue)
out.puts "dev: #{header.issue_ref} is already in sync."
return
end

title = extract_title(body)
title = extract_title(plan.body)
updated = @issues.update(
header.owner_repo, header.number,
body: Plan.to_issue_body(body),
body: Plan.to_issue_body(plan.body),
title: (title if title && title != issue.title),
)
record_sync(path, header, updated, body)
record_sync(path, plan, updated)
out.puts "dev: pushed #{path} to #{header.issue_ref} (#{updated.html_url})"
end

Expand All@@ -244,8 +244,8 @@ def hook_after_edit(input, out:)
return unless path.to_s.end_with?(".plan.md") && path.exist?
return unless path.expand_path.to_s.start_with?(@workspace.plans_dir.expand_path.to_s)

header, _body = Header.split(path.read)
return unless header
plan = Content.parse(path.read)
return unless plan.header

push([path.to_s], out:)
end
Expand All@@ -260,10 +260,10 @@ def status(out:)
end

files.each do |path|
header, body = Header.split(path.read)
issue = @issues.get(header.owner_repo, header.number)
state = sync_state(header, body, Plan.from_issue_body(issue.body))
out.puts "#{state.ljust(10)} #{header.issue_ref.ljust(30)} #{path}"
plan = Content.parse(path.read)
issue = @issues.get(plan.header.owner_repo, plan.header.number)
state = sync_state(plan.header, plan.body, Plan.from_issue_body(issue.body))
out.puts "#{state.ljust(10)} #{plan.header.issue_ref.ljust(30)} #{path}"
end
end

Expand All@@ -286,22 +286,31 @@ def target_repo(org:)
org ? @settings.plans_repo : @workspace.origin_repo
end

# Write the plan file (header + body) and refresh the merge base — the
# single definition of "synced".
# Write the plan file (header + optional frontmatter + markdown body) and
# refresh the merge base — the single definition of "synced". The merge
# base stores the markdown body only.
#
# @param owner_repo [String]
# @param issue [Dev::Plan::GithubIssues::Issue]
# @param body [String] markdown body
# @param path [Pathname, nil]
# @param frontmatter [String, nil] Cursor YAML block to preserve locally
# @return [Pathname] the written path
def write_linked_plan(owner_repo, issue, body, path: nil)
def write_linked_plan(owner_repo, issue, body, path: nil, frontmatter: nil)
path ||= @workspace.plan_path(owner_repo, issue.number, issue.title)
header = Header.new(owner_repo: owner_repo, number: issue.number, synced_at: issue.updated_at)
FileUtils.mkdir_p(path.dirname)
path.write(header.render + body)
path.write(Content.new(header: header, frontmatter: frontmatter, body: body).render)
@merge_base.write(owner_repo, issue.number, body)
path
end

def record_sync(path, header, issue, body)
path.write(header.with_synced_at(issue.updated_at).render + body)
@merge_base.write(header.owner_repo, header.number, body)
# @param path [Pathname]
# @param plan [Dev::Plan::Content]
# @param issue [Dev::Plan::GithubIssues::Issue]
def record_sync(path, plan, issue)
path.write(plan.with_synced_at(issue.updated_at).render)
@merge_base.write(plan.header.owner_repo, plan.header.number, plan.body)
end

# Move a freshly linked file to the `gh-<n>-<slug>.plan.md` convention
Expand All@@ -326,8 +335,8 @@ def extract_title(body)
# @return [Pathname]
def find_linked_plan(owner_repo, number)
@workspace.linked_plan_files.find do |path|
header, _body = Header.split(path.read)
header.owner_repo == owner_repo && header.number == number
plan = Content.parse(path.read)
plan.header.owner_repo == owner_repo && plan.header.number == number
end
end

Expand All@@ -343,8 +352,7 @@ def sole_unlinked_plan
files =
if @workspace.plans_dir.directory?
@workspace.plans_dir.glob(Workspace::PLAN_GLOB).sort.reject do |path|
header, _body = Header.split(path.read)
header
Content.parse(path.read).header
end
else
[]
Expand Down
81 changes: 81 additions & 0 deletions lib/dev/plan/content.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

require "dev/plan/header"
require "dev/plan/frontmatter"

module Dev
module Plan
# The three on-disk layers of a plan file: ai-flow sync header, optional
# Cursor YAML frontmatter, and the markdown body. Sync compares and ships
# the markdown body only; the header and frontmatter stay local.
class Content
# @return [Dev::Plan::Header, nil]
attr_reader :header

# @return [String, nil] raw frontmatter block including `---` fences
attr_reader :frontmatter

# @return [String] markdown body (canonical plan prose)
attr_reader :body

# Parse a plan file into its layers. Canonical on-disk order is header,
# then optional frontmatter, then body. When frontmatter sits above the
# ai-flow header (hand edit), both are still recognized; {#render}
# rewrites canonical order.
#
# @param content [String]
# @return [Content]
def self.parse(content)
header, remainder = Header.split(content)
if header
frontmatter, body = Frontmatter.split(remainder)
return new(header: header, frontmatter: frontmatter, body: body)
end

# Frontmatter may sit above a misplaced ai-flow header.
frontmatter, after_frontmatter = Frontmatter.split(content)
if frontmatter
header, body = Header.split(after_frontmatter)
return new(header: header, frontmatter: frontmatter, body: body)
end

new(header: nil, frontmatter: nil, body: content)
end

# @param header [Dev::Plan::Header, nil]
# @param frontmatter [String, nil]
# @param body [String]
def initialize(header:, frontmatter:, body:)
@header = header
@frontmatter = frontmatter
@body = body
end

# Serialize in canonical order: ai-flow header, optional frontmatter,
# markdown body.
#
# @return [String]
def render
"#{header&.render}#{frontmatter}#{body}"
end

# @param header [Dev::Plan::Header, nil]
# @return [Content]
def with_header(header)
self.class.new(header: header, frontmatter: frontmatter, body: body)
end

# @param body [String]
# @return [Content]
def with_body(body)
self.class.new(header: header, frontmatter: frontmatter, body: body)
end

# @param synced_at [String]
# @return [Content]
def with_synced_at(synced_at)
with_header(header.with_synced_at(synced_at))
end
end
end
end
56 changes: 56 additions & 0 deletions lib/dev/plan/frontmatter.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require "yaml"

module Dev
module Plan
# Cursor plan YAML frontmatter (`---` … `---`) — local editor state
# (picker label, overview, todos). Peeled before any GitHub sync so the
# issue body stays markdown-only.
class Frontmatter
FENCE_LINE = /\A---\n?\z/

# Peel a Cursor-like YAML frontmatter block from the start of +content+.
# Only a leading `---` … `---` fence whose interior is a YAML mapping is
# removed; ordinary markdown horizontal rules deeper in the body, or a
# leading `---` that is not a mapping, are left alone.
#
# @param content [String]
# @return [Array(String | nil, String)] frontmatter block (including
# fences and a trailing newline after the closing fence) or nil, and
# the remainder
def self.split(content)
lines = content.lines
return [nil, content] if lines.empty? || !fence?(lines.fetch(0))

close_index = (1...lines.length).find { |index| fence?(lines.fetch(index)) }
return [nil, content] unless close_index

yaml_text = lines[1...close_index].join
return [nil, content] unless mapping?(yaml_text)

frontmatter = lines[0..close_index].join
frontmatter = "#{frontmatter}\n" unless frontmatter.end_with?("\n")
body = lines[(close_index + 1)..].join
[frontmatter, body]
end

# @param line [String]
# @return [Boolean]
def self.fence?(line)
line.match?(FENCE_LINE)
end
private_class_method :fence?

# @param yaml_text [String] interior between fences
# @return [Boolean] true when the interior parses as a YAML mapping
def self.mapping?(yaml_text)
parsed = YAML.safe_load(yaml_text)
parsed.is_a?(Hash)
rescue Psych::SyntaxError, Psych::DisallowedClass
false
end
private_class_method :mapping?
end
end
end
12 changes: 7 additions & 5 deletions lib/dev/plan/header.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,18 +4,20 @@ module Dev
module Plan
module_function

# Render a plan body as an issue body: verbatim content, normalized to a
# single trailing newline.
# Normalize a markdown plan body for the issue: LF, single trailing
# newline. Callers pass the markdown body only — not Cursor YAML
# frontmatter or the ai-flow sync header.
#
# @param plan_body [String]
# @return [String]
def to_issue_body(plan_body)
"#{plan_body.rstrip}\n"
end

# Extract the plan body from an issue body. Issue bodies use CRLF line
# endings when edited via the GitHub web UI, so normalize to LF — the
# local file and merge base always use LF.
# Normalize an issue body to a local markdown plan body. Issue bodies use
# CRLF when edited via the GitHub web UI, so normalize to LF — the local
# file and merge base always use LF. The result is markdown only (GitHub
# never stores Cursor frontmatter).
#
# @param issue_body [String, nil]
# @return [String]
Expand Down
3 changes: 1 addition & 2 deletions lib/dev/plan/workspace.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,8 +53,7 @@ def linked_plan_files
return [] unless @plans_dir.directory?

@plans_dir.glob(PLAN_GLOB).sort.select do |path|
header, _body = Header.split(path.read)
!header.nil?
!Content.parse(path.read).header.nil?
end
end

Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/dev/plan.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,8 @@

require "dev/plan/executor"
require "dev/plan/header"
require "dev/plan/frontmatter"
require "dev/plan/content"
require "dev/plan/settings"
require "dev/plan/github_issues"
require "dev/plan/workspace"
Expand Down
94 changes: 51 additions & 43 deletions lib/dev/plan/accessor.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,17 +102,16 @@ def link(args, out:)

def link_to_existing(number, file, org:, out:)
path = file ? Pathname.new(file) : sole_unlinked_plan
content = path.read
header, body = Header.split(content)
raise UsageError, "#{path} is already linked to #{header.issue_ref}" if header
plan = Content.parse(path.read)
raise UsageError, "#{path} is already linked to #{plan.header.issue_ref}" if plan.header

owner_repo = target_repo(org:)
issue = @issues.get(owner_repo, number)
# Record the remote as the sync point but keep the local content: the
# file is a draft ahead of the issue until `dev plan push` publishes it.
new_header = Header.new(owner_repo: owner_repo, number: issue.number, synced_at: issue.updated_at)
target = move_into_convention(path, owner_repo, issue)
target.write(new_header.render + body)
target.write(plan.with_header(new_header).render)
@merge_base.write(owner_repo, issue.number, Plan.from_issue_body(issue.body))
out.puts "dev: linked #{target} to #{owner_repo}##{issue.number} (#{issue.html_url})"
out.puts "dev: local content kept — run `dev plan push` to publish it."
Expand All@@ -122,15 +121,14 @@ def create_from_file(file, org:, out:)
path = Pathname.new(file)
raise UsageError, "no such plan file: #{path}" unless path.exist?

content = path.read
header, body = Header.split(content)
raise UsageError, "#{path} is already linked to #{header.issue_ref}" if header
plan = Content.parse(path.read)
raise UsageError, "#{path} is already linked to #{plan.header.issue_ref}" if plan.header

owner_repo = target_repo(org:)
title = extract_title(body) || path.basename(".plan.md").to_s
issue = @issues.create(owner_repo, title: title, body: Plan.to_issue_body(body))
title = extract_title(plan.body) || path.basename(".plan.md").to_s
issue = @issues.create(owner_repo, title: title, body: Plan.to_issue_body(plan.body))
target = move_into_convention(path, owner_repo, issue)
write_linked_plan(owner_repo, issue, body, path: target)
write_linked_plan(owner_repo, issue, plan.body, path: target, frontmatter: plan.frontmatter)
out.puts "dev: created #{owner_repo}##{issue.number} from #{path} (#{issue.html_url})"
out.puts "dev: plan file: #{target}"
end
Expand All@@ -156,30 +154,31 @@ def pull(args, out:)
return
end

_header, local_body = Header.split(path.read)
plan = Content.parse(path.read)
base = @merge_base.read(owner_repo, number)
local_dirty = base.nil? ? false : local_body != base
local_dirty = base.nil? ? false : plan.body != base
remote_dirty = base.nil? ? true : remote_body != base

if !local_dirty
write_linked_plan(owner_repo, issue, remote_body, path: path)
write_linked_plan(owner_repo, issue, remote_body, path: path, frontmatter: plan.frontmatter)
out.puts(remote_dirty ? "dev: pulled #{owner_repo}##{number} into #{path}" : "dev: #{path} is already up to date.")
elsif !remote_dirty
out.puts "dev: local plan is ahead of #{owner_repo}##{number} — nothing to pull. Run `dev plan push`."
elsif merge
merge_pull(path, issue, owner_repo, number, local_body, base, remote_body, out:)
merge_pull(path, issue, owner_repo, number, plan, base, remote_body, out:)
else
raise "both #{path} and #{owner_repo}##{number} changed since the last sync — " \
"run `dev plan pull #{number} --merge`."
end
end

def merge_pull(path, issue, owner_repo, number, local_body, base, remote_body, out:)
result = Merge.three_way(local: local_body, base: base, remote: remote_body, executor: @executor)
def merge_pull(path, issue, owner_repo, number, plan, base, remote_body, out:)
result = Merge.three_way(local: plan.body, base: base, remote: remote_body, executor: @executor)
# The remote becomes the new base either way: the merged local copy is
# now "ahead" of the issue, and `push` publishes it.
# now "ahead" of the issue, and `push` publishes it. Frontmatter is
# carried through from the local side untouched.
header = Header.new(owner_repo: owner_repo, number: number, synced_at: issue.updated_at)
path.write(header.render + result.content)
path.write(plan.with_header(header).with_body(result.content).render)
@merge_base.write(owner_repo, number, remote_body)
if result.conflicts?
out.puts "dev: merged with conflicts — resolve the markers in #{path}, then run `dev plan push`."
Expand All@@ -197,10 +196,11 @@ def push(args, out:)
raise UsageError, "usage: dev plan push [<file>]" unless args.empty?

path = file ? Pathname.new(file) : sole_linked_plan
header, body = Header.split(path.read)
raise UsageError, "#{path} has no ai-flow header — link it first with `dev plan link`." unless header
raise "#{path} contains unresolved merge conflict markers — resolve them before pushing." if body.include?("<<<<<<<")
plan = Content.parse(path.read)
raise UsageError, "#{path} has no ai-flow header — link it first with `dev plan link`." unless plan.header
raise "#{path} contains unresolved merge conflict markers — resolve them before pushing." if plan.body.include?("<<<<<<<")

header = plan.header
issue = @issues.get(header.owner_repo, header.number)
remote_body = Plan.from_issue_body(issue.body)
base = @merge_base.read(header.owner_repo, header.number)
Expand All@@ -214,19 +214,19 @@ def push(args, out:)
"run `dev plan pull #{header.number} --merge`, then push again."
end

if body == remote_body
record_sync(path, header, issue, body)
if plan.body == remote_body
record_sync(path, plan, issue)
out.puts "dev: #{header.issue_ref} is already in sync."
return
end

title = extract_title(body)
title = extract_title(plan.body)
updated = @issues.update(
header.owner_repo, header.number,
body: Plan.to_issue_body(body),
body: Plan.to_issue_body(plan.body),
title: (title if title && title != issue.title),
)
record_sync(path, header, updated, body)
record_sync(path, plan, updated)
out.puts "dev: pushed #{path} to #{header.issue_ref} (#{updated.html_url})"
end

Expand All@@ -244,8 +244,8 @@ def hook_after_edit(input, out:)
return unless path.to_s.end_with?(".plan.md") && path.exist?
return unless path.expand_path.to_s.start_with?(@workspace.plans_dir.expand_path.to_s)

header, _body = Header.split(path.read)
return unless header
plan = Content.parse(path.read)
return unless plan.header

push([path.to_s], out:)
end
Expand All@@ -260,10 +260,10 @@ def status(out:)
end

files.each do |path|
header, body = Header.split(path.read)
issue = @issues.get(header.owner_repo, header.number)
state = sync_state(header, body, Plan.from_issue_body(issue.body))
out.puts "#{state.ljust(10)} #{header.issue_ref.ljust(30)} #{path}"
plan = Content.parse(path.read)
issue = @issues.get(plan.header.owner_repo, plan.header.number)
state = sync_state(plan.header, plan.body, Plan.from_issue_body(issue.body))
out.puts "#{state.ljust(10)} #{plan.header.issue_ref.ljust(30)} #{path}"
end
end

Expand All@@ -286,22 +286,31 @@ def target_repo(org:)
org ? @settings.plans_repo : @workspace.origin_repo
end

# Write the plan file (header + body) and refresh the merge base — the
# single definition of "synced".
# Write the plan file (header + optional frontmatter + markdown body) and
# refresh the merge base — the single definition of "synced". The merge
# base stores the markdown body only.
#
# @param owner_repo [String]
# @param issue [Dev::Plan::GithubIssues::Issue]
# @param body [String] markdown body
# @param path [Pathname, nil]
# @param frontmatter [String, nil] Cursor YAML block to preserve locally
# @return [Pathname] the written path
def write_linked_plan(owner_repo, issue, body, path: nil)
def write_linked_plan(owner_repo, issue, body, path: nil, frontmatter: nil)
path ||= @workspace.plan_path(owner_repo, issue.number, issue.title)
header = Header.new(owner_repo: owner_repo, number: issue.number, synced_at: issue.updated_at)
FileUtils.mkdir_p(path.dirname)
path.write(header.render + body)
path.write(Content.new(header: header, frontmatter: frontmatter, body: body).render)
@merge_base.write(owner_repo, issue.number, body)
path
end

def record_sync(path, header, issue, body)
path.write(header.with_synced_at(issue.updated_at).render + body)
@merge_base.write(header.owner_repo, header.number, body)
# @param path [Pathname]
# @param plan [Dev::Plan::Content]
# @param issue [Dev::Plan::GithubIssues::Issue]
def record_sync(path, plan, issue)
path.write(plan.with_synced_at(issue.updated_at).render)
@merge_base.write(plan.header.owner_repo, plan.header.number, plan.body)
end

# Move a freshly linked file to the `gh-<n>-<slug>.plan.md` convention
Expand All@@ -326,8 +335,8 @@ def extract_title(body)
# @return [Pathname]
def find_linked_plan(owner_repo, number)
@workspace.linked_plan_files.find do |path|
header, _body = Header.split(path.read)
header.owner_repo == owner_repo && header.number == number
plan = Content.parse(path.read)
plan.header.owner_repo == owner_repo && plan.header.number == number
end
end

Expand All@@ -343,8 +352,7 @@ def sole_unlinked_plan
files =
if @workspace.plans_dir.directory?
@workspace.plans_dir.glob(Workspace::PLAN_GLOB).sort.reject do |path|
header, _body = Header.split(path.read)
header
Content.parse(path.read).header
end
else
[]
Expand Down
81 changes: 81 additions & 0 deletions lib/dev/plan/content.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

require "dev/plan/header"
require "dev/plan/frontmatter"

module Dev
module Plan
# The three on-disk layers of a plan file: ai-flow sync header, optional
# Cursor YAML frontmatter, and the markdown body. Sync compares and ships
# the markdown body only; the header and frontmatter stay local.
class Content
# @return [Dev::Plan::Header, nil]
attr_reader :header

# @return [String, nil] raw frontmatter block including `---` fences
attr_reader :frontmatter

# @return [String] markdown body (canonical plan prose)
attr_reader :body

# Parse a plan file into its layers. Canonical on-disk order is header,
# then optional frontmatter, then body. When frontmatter sits above the
# ai-flow header (hand edit), both are still recognized; {#render}
# rewrites canonical order.
#
# @param content [String]
# @return [Content]
def self.parse(content)
header, remainder = Header.split(content)
if header
frontmatter, body = Frontmatter.split(remainder)
return new(header: header, frontmatter: frontmatter, body: body)
end

# Frontmatter may sit above a misplaced ai-flow header.
frontmatter, after_frontmatter = Frontmatter.split(content)
if frontmatter
header, body = Header.split(after_frontmatter)
return new(header: header, frontmatter: frontmatter, body: body)
end

new(header: nil, frontmatter: nil, body: content)
end

# @param header [Dev::Plan::Header, nil]
# @param frontmatter [String, nil]
# @param body [String]
def initialize(header:, frontmatter:, body:)
@header = header
@frontmatter = frontmatter
@body = body
end

# Serialize in canonical order: ai-flow header, optional frontmatter,
# markdown body.
#
# @return [String]
def render
"#{header&.render}#{frontmatter}#{body}"
end

# @param header [Dev::Plan::Header, nil]
# @return [Content]
def with_header(header)
self.class.new(header: header, frontmatter: frontmatter, body: body)
end

# @param body [String]
# @return [Content]
def with_body(body)
self.class.new(header: header, frontmatter: frontmatter, body: body)
end

# @param synced_at [String]
# @return [Content]
def with_synced_at(synced_at)
with_header(header.with_synced_at(synced_at))
end
end
end
end
56 changes: 56 additions & 0 deletions lib/dev/plan/frontmatter.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require "yaml"

module Dev
module Plan
# Cursor plan YAML frontmatter (`---` … `---`) — local editor state
# (picker label, overview, todos). Peeled before any GitHub sync so the
# issue body stays markdown-only.
class Frontmatter
FENCE_LINE = /\A---\n?\z/

# Peel a Cursor-like YAML frontmatter block from the start of +content+.
# Only a leading `---` … `---` fence whose interior is a YAML mapping is
# removed; ordinary markdown horizontal rules deeper in the body, or a
# leading `---` that is not a mapping, are left alone.
#
# @param content [String]
# @return [Array(String | nil, String)] frontmatter block (including
# fences and a trailing newline after the closing fence) or nil, and
# the remainder
def self.split(content)
lines = content.lines
return [nil, content] if lines.empty? || !fence?(lines.fetch(0))

close_index = (1...lines.length).find { |index| fence?(lines.fetch(index)) }
return [nil, content] unless close_index

yaml_text = lines[1...close_index].join
return [nil, content] unless mapping?(yaml_text)

frontmatter = lines[0..close_index].join
frontmatter = "#{frontmatter}\n" unless frontmatter.end_with?("\n")
body = lines[(close_index + 1)..].join
[frontmatter, body]
end

# @param line [String]
# @return [Boolean]
def self.fence?(line)
line.match?(FENCE_LINE)
end
private_class_method :fence?

# @param yaml_text [String] interior between fences
# @return [Boolean] true when the interior parses as a YAML mapping
def self.mapping?(yaml_text)
parsed = YAML.safe_load(yaml_text)
parsed.is_a?(Hash)
rescue Psych::SyntaxError, Psych::DisallowedClass
false
end
private_class_method :mapping?
end
end
end
12 changes: 7 additions & 5 deletions lib/dev/plan/header.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,18 +4,20 @@ module Dev
module Plan
module_function

# Render a plan body as an issue body: verbatim content, normalized to a
# single trailing newline.
# Normalize a markdown plan body for the issue: LF, single trailing
# newline. Callers pass the markdown body only — not Cursor YAML
# frontmatter or the ai-flow sync header.
#
# @param plan_body [String]
# @return [String]
def to_issue_body(plan_body)
"#{plan_body.rstrip}\n"
end

# Extract the plan body from an issue body. Issue bodies use CRLF line
# endings when edited via the GitHub web UI, so normalize to LF — the
# local file and merge base always use LF.
# Normalize an issue body to a local markdown plan body. Issue bodies use
# CRLF when edited via the GitHub web UI, so normalize to LF — the local
# file and merge base always use LF. The result is markdown only (GitHub
# never stores Cursor frontmatter).
#
# @param issue_body [String, nil]
# @return [String]
Expand Down
3 changes: 1 addition & 2 deletions lib/dev/plan/workspace.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,8 +53,7 @@ def linked_plan_files
return [] unless @plans_dir.directory?

@plans_dir.glob(PLAN_GLOB).sort.select do |path|
header, _body = Header.split(path.read)
!header.nil?
!Content.parse(path.read).header.nil?
end
end

Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/dev/plan.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,8 @@

require "dev/plan/executor"
require "dev/plan/header"
require "dev/plan/frontmatter"
require "dev/plan/content"
require "dev/plan/settings"
require "dev/plan/github_issues"
require "dev/plan/workspace"
Expand Down
94 changes: 51 additions & 43 deletions lib/dev/plan/accessor.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -102,17 +102,16 @@ def link(args, out:)

def link_to_existing(number, file, org:, out:)
path = file ? Pathname.new(file) : sole_unlinked_plan
content = path.read
header, body = Header.split(content)
raise UsageError, "#{path} is already linked to #{header.issue_ref}" if header
plan = Content.parse(path.read)
raise UsageError, "#{path} is already linked to #{plan.header.issue_ref}" if plan.header

owner_repo = target_repo(org:)
issue = @issues.get(owner_repo, number)
# Record the remote as the sync point but keep the local content: the
# file is a draft ahead of the issue until `dev plan push` publishes it.
new_header = Header.new(owner_repo: owner_repo, number: issue.number, synced_at: issue.updated_at)
target = move_into_convention(path, owner_repo, issue)
target.write(new_header.render + body)
target.write(plan.with_header(new_header).render)
@merge_base.write(owner_repo, issue.number, Plan.from_issue_body(issue.body))
out.puts "dev: linked #{target} to #{owner_repo}##{issue.number} (#{issue.html_url})"
out.puts "dev: local content kept — run `dev plan push` to publish it."
Expand All@@ -122,15 +121,14 @@ def create_from_file(file, org:, out:)
path = Pathname.new(file)
raise UsageError, "no such plan file: #{path}" unless path.exist?

content = path.read
header, body = Header.split(content)
raise UsageError, "#{path} is already linked to #{header.issue_ref}" if header
plan = Content.parse(path.read)
raise UsageError, "#{path} is already linked to #{plan.header.issue_ref}" if plan.header

owner_repo = target_repo(org:)
title = extract_title(body) || path.basename(".plan.md").to_s
issue = @issues.create(owner_repo, title: title, body: Plan.to_issue_body(body))
title = extract_title(plan.body) || path.basename(".plan.md").to_s
issue = @issues.create(owner_repo, title: title, body: Plan.to_issue_body(plan.body))
target = move_into_convention(path, owner_repo, issue)
write_linked_plan(owner_repo, issue, body, path: target)
write_linked_plan(owner_repo, issue, plan.body, path: target, frontmatter: plan.frontmatter)
out.puts "dev: created #{owner_repo}##{issue.number} from #{path} (#{issue.html_url})"
out.puts "dev: plan file: #{target}"
end
Expand All@@ -156,30 +154,31 @@ def pull(args, out:)
return
end

_header, local_body = Header.split(path.read)
plan = Content.parse(path.read)
base = @merge_base.read(owner_repo, number)
local_dirty = base.nil? ? false : local_body != base
local_dirty = base.nil? ? false : plan.body != base
remote_dirty = base.nil? ? true : remote_body != base

if !local_dirty
write_linked_plan(owner_repo, issue, remote_body, path: path)
write_linked_plan(owner_repo, issue, remote_body, path: path, frontmatter: plan.frontmatter)
out.puts(remote_dirty ? "dev: pulled #{owner_repo}##{number} into #{path}" : "dev: #{path} is already up to date.")
elsif !remote_dirty
out.puts "dev: local plan is ahead of #{owner_repo}##{number} — nothing to pull. Run `dev plan push`."
elsif merge
merge_pull(path, issue, owner_repo, number, local_body, base, remote_body, out:)
merge_pull(path, issue, owner_repo, number, plan, base, remote_body, out:)
else
raise "both #{path} and #{owner_repo}##{number} changed since the last sync — " \
"run `dev plan pull #{number} --merge`."
end
end

def merge_pull(path, issue, owner_repo, number, local_body, base, remote_body, out:)
result = Merge.three_way(local: local_body, base: base, remote: remote_body, executor: @executor)
def merge_pull(path, issue, owner_repo, number, plan, base, remote_body, out:)
result = Merge.three_way(local: plan.body, base: base, remote: remote_body, executor: @executor)
# The remote becomes the new base either way: the merged local copy is
# now "ahead" of the issue, and `push` publishes it.
# now "ahead" of the issue, and `push` publishes it. Frontmatter is
# carried through from the local side untouched.
header = Header.new(owner_repo: owner_repo, number: number, synced_at: issue.updated_at)
path.write(header.render + result.content)
path.write(plan.with_header(header).with_body(result.content).render)
@merge_base.write(owner_repo, number, remote_body)
if result.conflicts?
out.puts "dev: merged with conflicts — resolve the markers in #{path}, then run `dev plan push`."
Expand All@@ -197,10 +196,11 @@ def push(args, out:)
raise UsageError, "usage: dev plan push [<file>]" unless args.empty?

path = file ? Pathname.new(file) : sole_linked_plan
header, body = Header.split(path.read)
raise UsageError, "#{path} has no ai-flow header — link it first with `dev plan link`." unless header
raise "#{path} contains unresolved merge conflict markers — resolve them before pushing." if body.include?("<<<<<<<")
plan = Content.parse(path.read)
raise UsageError, "#{path} has no ai-flow header — link it first with `dev plan link`." unless plan.header
raise "#{path} contains unresolved merge conflict markers — resolve them before pushing." if plan.body.include?("<<<<<<<")

header = plan.header
issue = @issues.get(header.owner_repo, header.number)
remote_body = Plan.from_issue_body(issue.body)
base = @merge_base.read(header.owner_repo, header.number)
Expand All@@ -214,19 +214,19 @@ def push(args, out:)
"run `dev plan pull #{header.number} --merge`, then push again."
end

if body == remote_body
record_sync(path, header, issue, body)
if plan.body == remote_body
record_sync(path, plan, issue)
out.puts "dev: #{header.issue_ref} is already in sync."
return
end

title = extract_title(body)
title = extract_title(plan.body)
updated = @issues.update(
header.owner_repo, header.number,
body: Plan.to_issue_body(body),
body: Plan.to_issue_body(plan.body),
title: (title if title && title != issue.title),
)
record_sync(path, header, updated, body)
record_sync(path, plan, updated)
out.puts "dev: pushed #{path} to #{header.issue_ref} (#{updated.html_url})"
end

Expand All@@ -244,8 +244,8 @@ def hook_after_edit(input, out:)
return unless path.to_s.end_with?(".plan.md") && path.exist?
return unless path.expand_path.to_s.start_with?(@workspace.plans_dir.expand_path.to_s)

header, _body = Header.split(path.read)
return unless header
plan = Content.parse(path.read)
return unless plan.header

push([path.to_s], out:)
end
Expand All@@ -260,10 +260,10 @@ def status(out:)
end

files.each do |path|
header, body = Header.split(path.read)
issue = @issues.get(header.owner_repo, header.number)
state = sync_state(header, body, Plan.from_issue_body(issue.body))
out.puts "#{state.ljust(10)} #{header.issue_ref.ljust(30)} #{path}"
plan = Content.parse(path.read)
issue = @issues.get(plan.header.owner_repo, plan.header.number)
state = sync_state(plan.header, plan.body, Plan.from_issue_body(issue.body))
out.puts "#{state.ljust(10)} #{plan.header.issue_ref.ljust(30)} #{path}"
end
end

Expand All@@ -286,22 +286,31 @@ def target_repo(org:)
org ? @settings.plans_repo : @workspace.origin_repo
end

# Write the plan file (header + body) and refresh the merge base — the
# single definition of "synced".
# Write the plan file (header + optional frontmatter + markdown body) and
# refresh the merge base — the single definition of "synced". The merge
# base stores the markdown body only.
#
# @param owner_repo [String]
# @param issue [Dev::Plan::GithubIssues::Issue]
# @param body [String] markdown body
# @param path [Pathname, nil]
# @param frontmatter [String, nil] Cursor YAML block to preserve locally
# @return [Pathname] the written path
def write_linked_plan(owner_repo, issue, body, path: nil)
def write_linked_plan(owner_repo, issue, body, path: nil, frontmatter: nil)
path ||= @workspace.plan_path(owner_repo, issue.number, issue.title)
header = Header.new(owner_repo: owner_repo, number: issue.number, synced_at: issue.updated_at)
FileUtils.mkdir_p(path.dirname)
path.write(header.render + body)
path.write(Content.new(header: header, frontmatter: frontmatter, body: body).render)
@merge_base.write(owner_repo, issue.number, body)
path
end

def record_sync(path, header, issue, body)
path.write(header.with_synced_at(issue.updated_at).render + body)
@merge_base.write(header.owner_repo, header.number, body)
# @param path [Pathname]
# @param plan [Dev::Plan::Content]
# @param issue [Dev::Plan::GithubIssues::Issue]
def record_sync(path, plan, issue)
path.write(plan.with_synced_at(issue.updated_at).render)
@merge_base.write(plan.header.owner_repo, plan.header.number, plan.body)
end

# Move a freshly linked file to the `gh-<n>-<slug>.plan.md` convention
Expand All@@ -326,8 +335,8 @@ def extract_title(body)
# @return [Pathname]
def find_linked_plan(owner_repo, number)
@workspace.linked_plan_files.find do |path|
header, _body = Header.split(path.read)
header.owner_repo == owner_repo && header.number == number
plan = Content.parse(path.read)
plan.header.owner_repo == owner_repo && plan.header.number == number
end
end

Expand All@@ -343,8 +352,7 @@ def sole_unlinked_plan
files =
if @workspace.plans_dir.directory?
@workspace.plans_dir.glob(Workspace::PLAN_GLOB).sort.reject do |path|
header, _body = Header.split(path.read)
header
Content.parse(path.read).header
end
else
[]
Expand Down
81 changes: 81 additions & 0 deletions lib/dev/plan/content.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

require "dev/plan/header"
require "dev/plan/frontmatter"

module Dev
module Plan
# The three on-disk layers of a plan file: ai-flow sync header, optional
# Cursor YAML frontmatter, and the markdown body. Sync compares and ships
# the markdown body only; the header and frontmatter stay local.
class Content
# @return [Dev::Plan::Header, nil]
attr_reader :header

# @return [String, nil] raw frontmatter block including `---` fences
attr_reader :frontmatter

# @return [String] markdown body (canonical plan prose)
attr_reader :body

# Parse a plan file into its layers. Canonical on-disk order is header,
# then optional frontmatter, then body. When frontmatter sits above the
# ai-flow header (hand edit), both are still recognized; {#render}
# rewrites canonical order.
#
# @param content [String]
# @return [Content]
def self.parse(content)
header, remainder = Header.split(content)
if header
frontmatter, body = Frontmatter.split(remainder)
return new(header: header, frontmatter: frontmatter, body: body)
end

# Frontmatter may sit above a misplaced ai-flow header.
frontmatter, after_frontmatter = Frontmatter.split(content)
if frontmatter
header, body = Header.split(after_frontmatter)
return new(header: header, frontmatter: frontmatter, body: body)
end

new(header: nil, frontmatter: nil, body: content)
end

# @param header [Dev::Plan::Header, nil]
# @param frontmatter [String, nil]
# @param body [String]
def initialize(header:, frontmatter:, body:)
@header = header
@frontmatter = frontmatter
@body = body
end

# Serialize in canonical order: ai-flow header, optional frontmatter,
# markdown body.
#
# @return [String]
def render
"#{header&.render}#{frontmatter}#{body}"
end

# @param header [Dev::Plan::Header, nil]
# @return [Content]
def with_header(header)
self.class.new(header: header, frontmatter: frontmatter, body: body)
end

# @param body [String]
# @return [Content]
def with_body(body)
self.class.new(header: header, frontmatter: frontmatter, body: body)
end

# @param synced_at [String]
# @return [Content]
def with_synced_at(synced_at)
with_header(header.with_synced_at(synced_at))
end
end
end
end
56 changes: 56 additions & 0 deletions lib/dev/plan/frontmatter.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require "yaml"

module Dev
module Plan
# Cursor plan YAML frontmatter (`---` … `---`) — local editor state
# (picker label, overview, todos). Peeled before any GitHub sync so the
# issue body stays markdown-only.
class Frontmatter
FENCE_LINE = /\A---\n?\z/

# Peel a Cursor-like YAML frontmatter block from the start of +content+.
# Only a leading `---` … `---` fence whose interior is a YAML mapping is
# removed; ordinary markdown horizontal rules deeper in the body, or a
# leading `---` that is not a mapping, are left alone.
#
# @param content [String]
# @return [Array(String | nil, String)] frontmatter block (including
# fences and a trailing newline after the closing fence) or nil, and
# the remainder
def self.split(content)
lines = content.lines
return [nil, content] if lines.empty? || !fence?(lines.fetch(0))

close_index = (1...lines.length).find { |index| fence?(lines.fetch(index)) }
return [nil, content] unless close_index

yaml_text = lines[1...close_index].join
return [nil, content] unless mapping?(yaml_text)

frontmatter = lines[0..close_index].join
frontmatter = "#{frontmatter}\n" unless frontmatter.end_with?("\n")
body = lines[(close_index + 1)..].join
[frontmatter, body]
end

# @param line [String]
# @return [Boolean]
def self.fence?(line)
line.match?(FENCE_LINE)
end
private_class_method :fence?

# @param yaml_text [String] interior between fences
# @return [Boolean] true when the interior parses as a YAML mapping
def self.mapping?(yaml_text)
parsed = YAML.safe_load(yaml_text)
parsed.is_a?(Hash)
rescue Psych::SyntaxError, Psych::DisallowedClass
false
end
private_class_method :mapping?
end
end
end
12 changes: 7 additions & 5 deletions lib/dev/plan/header.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,18 +4,20 @@ module Dev
module Plan
module_function

# Render a plan body as an issue body: verbatim content, normalized to a
# single trailing newline.
# Normalize a markdown plan body for the issue: LF, single trailing
# newline. Callers pass the markdown body only — not Cursor YAML
# frontmatter or the ai-flow sync header.
#
# @param plan_body [String]
# @return [String]
def to_issue_body(plan_body)
"#{plan_body.rstrip}\n"
end

# Extract the plan body from an issue body. Issue bodies use CRLF line
# endings when edited via the GitHub web UI, so normalize to LF — the
# local file and merge base always use LF.
# Normalize an issue body to a local markdown plan body. Issue bodies use
# CRLF when edited via the GitHub web UI, so normalize to LF — the local
# file and merge base always use LF. The result is markdown only (GitHub
# never stores Cursor frontmatter).
#
# @param issue_body [String, nil]
# @return [String]
Expand Down
3 changes: 1 addition & 2 deletions lib/dev/plan/workspace.rb
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,8 +53,7 @@ def linked_plan_files
return [] unless @plans_dir.directory?

@plans_dir.glob(PLAN_GLOB).sort.select do |path|
header, _body = Header.split(path.read)
!header.nil?
!Content.parse(path.read).header.nil?
end
end

Expand Down
Loading
Loading