Class: ClaudeTaskMaster::State

Inherits:
Object
  • Object
show all
Defined in:
lib/claude_task_master/state.rb

Overview

Manages state persistence in .claude-task-master/ All state is file-based for easy inspection and resumption

Constant Summary collapse

GOAL_FILE =
'goal.txt'
CRITERIA_FILE =
'criteria.txt'
PLAN_FILE =
'plan.md'
STATE_FILE =
'state.json'
PROGRESS_FILE =
'progress.md'
CONTEXT_FILE =
'context.md'
LOGS_DIR =
'logs'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_dir = Dir.pwd) ⇒ State

Returns a new instance of State.



20
21
22
# File 'lib/claude_task_master/state.rb', line 20

def initialize(project_dir = Dir.pwd)
  @dir = File.join(project_dir, STATE_DIR)
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



18
19
20
# File 'lib/claude_task_master/state.rb', line 18

def dir
  @dir
end

Instance Method Details

#append_context(content) ⇒ Object

Append to context



105
106
107
108
# File 'lib/claude_task_master/state.rb', line 105

def append_context(content)
  current = context || ''
  write_file(CONTEXT_FILE, "#{current}\n#{content}")
end

#append_progress(content) ⇒ Object

Append to progress



94
95
96
97
# File 'lib/claude_task_master/state.rb', line 94

def append_progress(content)
  current = progress || ''
  write_file(PROGRESS_FILE, "#{current}\n#{content}")
end

#blocked?Boolean

Check if blocked

Returns:

  • (Boolean)


129
130
131
132
# File 'lib/claude_task_master/state.rb', line 129

def blocked?
  state = load_state
  state && state[:status] == 'blocked'
end

#build_contextObject

Build context string for Claude



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/claude_task_master/state.rb', line 135

def build_context
  state = load_state || {}

  <<~CONTEXT
    # Current State

    ## Goal
    #{goal}

    ## Success Criteria
    #{criteria}

    ## Status
    - Phase: #{state[:status] || 'unknown'}
    - Current task: #{state[:current_task] || 'none'}
    - PR: #{state[:pr_number] ? "##{state[:pr_number]}" : 'none'}
    - Session: #{state[:session_count] || 0}

    ## Plan
    #{plan || '_No plan yet. Generate one first._'}

    ## Context from Previous Sessions
    #{context || '_No context yet._'}

    ## Recent Progress
    #{progress || '_No progress yet._'}
  CONTEXT
end

#contextObject

Read accumulated context



100
101
102
# File 'lib/claude_task_master/state.rb', line 100

def context
  read_file(CONTEXT_FILE)
end

#criteriaObject

Read success criteria



74
75
76
# File 'lib/claude_task_master/state.rb', line 74

def criteria
  read_file(CRITERIA_FILE)
end

#exists?Boolean

Check if state directory exists (for resume)

Returns:

  • (Boolean)


45
46
47
# File 'lib/claude_task_master/state.rb', line 45

def exists?
  File.directory?(@dir) && File.exist?(state_path)
end

#goalObject

Read goal



69
70
71
# File 'lib/claude_task_master/state.rb', line 69

def goal
  read_file(GOAL_FILE)
end

#init(goal:, criteria:) ⇒ Object

Initialize state directory for new project



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/claude_task_master/state.rb', line 25

def init(goal:, criteria:)
  FileUtils.mkdir_p(@dir)
  FileUtils.mkdir_p(logs_dir)

  write_file(GOAL_FILE, goal)
  write_file(CRITERIA_FILE, criteria)
  write_file(PROGRESS_FILE, "# Progress\n\n_Started: #{Time.now.iso8601}_\n\n")
  write_file(CONTEXT_FILE, "# Context\n\n_Learnings accumulated across sessions._\n\n")

  save_state(
    status: 'planning',
    current_task: nil,
    session_count: 0,
    pr_number: nil,
    started_at: Time.now.iso8601,
    updated_at: Time.now.iso8601
  )
end

#load_stateObject

Load machine state



50
51
52
53
54
# File 'lib/claude_task_master/state.rb', line 50

def load_state
  return nil unless File.exist?(state_path)

  JSON.parse(File.read(state_path), symbolize_names: true)
end

#log_session(session_num, content) ⇒ Object

Log a session



111
112
113
114
# File 'lib/claude_task_master/state.rb', line 111

def log_session(session_num, content)
  filename = format('session-%03d.md', session_num)
  File.write(File.join(logs_dir, filename), content)
end

#next_session_numberObject

Get next session number



117
118
119
120
# File 'lib/claude_task_master/state.rb', line 117

def next_session_number
  existing = Dir.glob(File.join(logs_dir, 'session-*.md'))
  existing.empty? ? 1 : existing.size + 1
end

#planObject

Read plan



79
80
81
# File 'lib/claude_task_master/state.rb', line 79

def plan
  read_file(PLAN_FILE)
end

#progressObject

Read progress notes



89
90
91
# File 'lib/claude_task_master/state.rb', line 89

def progress
  read_file(PROGRESS_FILE)
end

#save_plan(content) ⇒ Object

Write plan



84
85
86
# File 'lib/claude_task_master/state.rb', line 84

def save_plan(content)
  write_file(PLAN_FILE, content)
end

#save_state(data) ⇒ Object

Save machine state



57
58
59
60
# File 'lib/claude_task_master/state.rb', line 57

def save_state(data)
  data[:updated_at] = Time.now.iso8601
  File.write(state_path, JSON.pretty_generate(data))
end

#success?Boolean

Check if success criteria met (Claude writes SUCCESS to state)

Returns:

  • (Boolean)


123
124
125
126
# File 'lib/claude_task_master/state.rb', line 123

def success?
  state = load_state
  state && state[:status] == 'success'
end

#update_state(**fields) ⇒ Object

Update specific state fields



63
64
65
66
# File 'lib/claude_task_master/state.rb', line 63

def update_state(**fields)
  current = load_state || {}
  save_state(current.merge(fields))
end