tensaku 0.0.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,21 +1,25 @@
1
1
  module Tensaku
2
2
  module Source
3
3
  # Markdown blocks, addressed by the ranges a producer writes:
4
- # data-tensaku-md="path:startLine:startCol-endLine:endCol", 1-based lines and byte columns, end
4
+ # data-md-source="path:startLine:startCol-endLine:endCol", 1-based lines and byte columns, end
5
5
  # column inclusive (commonmarker's sourcepos). Tensaku doesn't render Markdown; it replaces text
6
6
  # inside a block's range and leaves the rest of the file alone.
7
7
  class Markdown
8
8
  EXTENSIONS = %w[.md .markdown].freeze
9
+ NBSP = "\u00A0"
9
10
 
10
11
  # Block syntax at the start of a range that stays locked: heading hashes, list markers, quotes.
11
12
  PREFIX = /\A(?:\#{1,6}[ \t]+|[-*+][ \t]+(?:\[[ xX]\][ \t]+)?|\d{1,9}[.)][ \t]+|>[ \t]?)/
12
13
 
13
- # Block tags whose text is edited; others (lists, tables, code) only get notes.
14
- EDITABLE = %w[p h1 h2 h3 h4 h5 h6 li blockquote].freeze
14
+ # Block tags whose text is edited; other blocks Kiln marks (pre, table, hr) only get notes. A
15
+ # whole list is edited as its source, markers included.
16
+ EDITABLE = %w[p h1 h2 h3 h4 h5 h6 li blockquote ul ol].freeze
17
+ WHOLE = %w[ul ol].freeze
15
18
 
16
19
  def self.parse_range(value)
17
20
  match = value.to_s.match(/\A(?<path>.+):(?<sl>\d+):(?<sc>\d+)-(?<el>\d+):(?<ec>\d+)\z/)
18
21
  raise Error, "Not a Markdown range: #{value.inspect}" unless match
22
+ raise Error, "Markdown ranges count lines and columns from 1: #{value.inspect}" if %i[sl sc el ec].any? { match[it].to_i < 1 }
19
23
 
20
24
  [ match[:path], *%i[sl sc el ec].map { match[it].to_i } ]
21
25
  end
@@ -30,7 +34,7 @@ module Tensaku
30
34
 
31
35
  def describe(tag: nil)
32
36
  raw = block_source
33
- prefix = raw[PREFIX].to_s
37
+ prefix = prefix_of(raw, tag)
34
38
  {
35
39
  file: relative_path,
36
40
  range: "#{@start_line}:#{@start_column}-#{@end_line}:#{@end_column}",
@@ -43,21 +47,45 @@ module Tensaku
43
47
 
44
48
  # original: the block's whole source when the editor opened. text: the new source after the
45
49
  # locked prefix.
46
- def edit(original:, text:)
50
+ def edit(original:, text:, tag: nil)
47
51
  @source = File.read(@path, mode: "rb").force_encoding(Encoding::UTF_8)
48
52
  raw = block_source
49
53
  raise StaleSource, "#{relative_path} changed since the page was rendered. Reload the page and try again." unless raw == original.to_s
50
54
 
51
- replacement = raw[PREFIX].to_s + text.to_s.tr(" ", " ").delete("\r")
55
+ prefix = prefix_of(raw, tag)
56
+ replacement = prefix + merge(raw.delete_prefix(prefix), text.to_s)
52
57
  return { changed: false, file: relative_path } if replacement == raw
53
58
 
54
59
  start, finish = byte_range
55
- FileWrite.atomic(@path, FileWrite.splice(@source, [ { start:, end: finish, text: replacement } ]))
56
- { changed: true, file: relative_path }
60
+ updated = FileWrite.splice(@source, [ { start:, end: finish, text: replacement } ])
61
+ FileWrite.atomic(@path, updated)
62
+ { changed: true, file: relative_path, diff: FileWrite.diff(@source, updated) }
57
63
  end
58
64
 
59
65
  private
60
66
 
67
+ # The block syntax that stays locked, or nothing when a whole list is edited as source.
68
+ def prefix_of(raw, tag)
69
+ WHOLE.include?(tag.to_s.downcase) ? "" : raw[PREFIX].to_s
70
+ end
71
+
72
+ # The new block text, keeping the old characters where the text didn't change: a non-breaking space
73
+ # typed in the browser reads as a plain space, so an untouched one in the file survives, and only
74
+ # the changed middle is turned into plain spaces. The file's line endings are kept.
75
+ def merge(old, new)
76
+ crlf = @source.include?("\r\n")
77
+ old = old.gsub("\r\n", "\n")
78
+ new = new.delete("\r")
79
+ same = ->(a, b) { a == b || (a.tr(NBSP, " ") == b.tr(NBSP, " ")) }
80
+ prefix = 0
81
+ prefix += 1 while prefix < old.length && prefix < new.length && same.(old[prefix], new[prefix])
82
+ limit = [ old.length, new.length ].min - prefix
83
+ suffix = 0
84
+ suffix += 1 while suffix < limit && same.(old[-1 - suffix], new[-1 - suffix])
85
+ merged = old[0, prefix] + new[prefix...(new.length - suffix)].tr(NBSP, " ") + old[(old.length - suffix)..]
86
+ crlf ? merged.gsub("\n", "\r\n") : merged
87
+ end
88
+
61
89
  def block_source
62
90
  start, finish = byte_range
63
91
  @source.byteslice(start...finish).force_encoding(Encoding::UTF_8)
@@ -32,6 +32,26 @@ module Tensaku
32
32
  [ new(path), line, column ]
33
33
  end
34
34
 
35
+ # Whether each stamped element has text written in its template, for edit mode's outlines. An
36
+ # element whose text all comes from Ruby (<%= %>) has nothing to edit. A stamp Tensaku can't read
37
+ # (a file outside the allow-list, a line that moved) counts as not editable.
38
+ def self.editable(stamps)
39
+ templates = {}
40
+ stamps.to_h do |stamp|
41
+ path, line, column = parse_stamp(stamp)
42
+ unless templates.key?(path)
43
+ templates[path] = begin
44
+ new(path)
45
+ rescue Error
46
+ nil
47
+ end
48
+ end
49
+ [ stamp, templates[path]&.describe(line, column)&.fetch(:editable) || false ]
50
+ rescue Error
51
+ [ stamp, false ]
52
+ end
53
+ end
54
+
35
55
  def initialize(relative_path)
36
56
  @relative_path = relative_path
37
57
  @path = Guard.writable_file!(relative_path, extensions: EXTENSIONS)
@@ -109,13 +129,15 @@ module Tensaku
109
129
  replacements = replacements.reject { source.byteslice(it[:start]...it[:end]) == it[:text] }
110
130
  return { changed: false } if replacements.empty?
111
131
 
112
- FileWrite.atomic(@path, FileWrite.splice(source, replacements))
113
- { changed: true, file: relative_path }
132
+ updated = FileWrite.splice(source, replacements)
133
+ FileWrite.atomic(@path, updated)
134
+ { changed: true, file: relative_path, diff: FileWrite.diff(source, updated) }
114
135
  end
115
136
 
116
- # Browsers hand back non-breaking spaces for typed runs of spaces in editable text.
137
+ # Browsers hand back "\n" line breaks; a file written with "\r\n" keeps them.
117
138
  def clean(text)
118
- text.to_s.tr(" ", " ").delete("\r")
139
+ text = text.to_s.delete("\r")
140
+ source.include?("\r\n") ? text.gsub("\n", "\r\n") : text
119
141
  end
120
142
 
121
143
  def pieces_of(element)
@@ -0,0 +1,196 @@
1
+ require "sqlite3"
2
+ require "monitor"
3
+ require "set"
4
+
5
+ module Tensaku
6
+ # The SQLite database in Tensaku.home that holds every app's notes, the agents connected to them and
7
+ # the page's selection. The dev server and every `tensaku mcp` open it at once: WAL lets readers run
8
+ # while one process writes, writes take the database's write lock (BEGIN IMMEDIATE), and a process
9
+ # waits up to five seconds for another's write to finish.
10
+ #
11
+ # Project keys every row by app, and logs each write in `changes` for the readers that wait.
12
+ module Store
13
+ FILE = "tensaku.sqlite3"
14
+ BUSY_TIMEOUT = 5000
15
+
16
+ # The tables at schema version BASE. A database below BASE is from before Tensaku's first release:
17
+ # its tables are dropped and made again from this script. Every database then runs the MIGRATIONS
18
+ # after its version.
19
+ BASE = 5
20
+ TABLES = %w[screenshots messages changes selections agents notes projects].freeze
21
+ SCHEMA = <<~SQL
22
+ CREATE TABLE IF NOT EXISTS projects (
23
+ root TEXT PRIMARY KEY,
24
+ remote TEXT,
25
+ base_url TEXT,
26
+ created_at TEXT NOT NULL
27
+ );
28
+ CREATE TABLE IF NOT EXISTS notes (
29
+ id TEXT PRIMARY KEY,
30
+ project TEXT NOT NULL REFERENCES projects(root) ON DELETE CASCADE,
31
+ url TEXT NOT NULL,
32
+ comment TEXT NOT NULL,
33
+ status TEXT NOT NULL,
34
+ targets TEXT NOT NULL DEFAULT '[]',
35
+ rect TEXT,
36
+ viewport TEXT,
37
+ "commit" TEXT,
38
+ session TEXT,
39
+ agent TEXT,
40
+ sent_at TEXT,
41
+ delivered_at TEXT,
42
+ delivered_to TEXT,
43
+ archived_at TEXT,
44
+ created_at TEXT NOT NULL,
45
+ updated_at TEXT NOT NULL
46
+ );
47
+ CREATE INDEX IF NOT EXISTS notes_project ON notes (project, archived_at, status);
48
+ CREATE TABLE IF NOT EXISTS messages (
49
+ id INTEGER PRIMARY KEY,
50
+ note_id TEXT NOT NULL REFERENCES notes(id) ON DELETE CASCADE,
51
+ author TEXT NOT NULL,
52
+ agent TEXT,
53
+ text TEXT NOT NULL,
54
+ at TEXT NOT NULL
55
+ );
56
+ CREATE INDEX IF NOT EXISTS messages_note ON messages (note_id, id);
57
+ CREATE TABLE IF NOT EXISTS agents (
58
+ session TEXT NOT NULL,
59
+ project TEXT NOT NULL REFERENCES projects(root) ON DELETE CASCADE,
60
+ name TEXT NOT NULL,
61
+ handle TEXT NOT NULL,
62
+ listening INTEGER NOT NULL DEFAULT 0,
63
+ channel INTEGER NOT NULL DEFAULT 0,
64
+ cwd TEXT,
65
+ pid INTEGER NOT NULL,
66
+ started_at TEXT,
67
+ last TEXT,
68
+ version TEXT,
69
+ seen_at REAL NOT NULL,
70
+ PRIMARY KEY (session, project)
71
+ );
72
+ CREATE TABLE IF NOT EXISTS selections (
73
+ project TEXT NOT NULL REFERENCES projects(root) ON DELETE CASCADE,
74
+ tab TEXT NOT NULL,
75
+ url TEXT,
76
+ data TEXT NOT NULL,
77
+ at TEXT NOT NULL,
78
+ PRIMARY KEY (project, tab)
79
+ );
80
+ CREATE TABLE IF NOT EXISTS changes (
81
+ seq INTEGER PRIMARY KEY AUTOINCREMENT,
82
+ project TEXT NOT NULL,
83
+ kind TEXT NOT NULL,
84
+ note_id TEXT,
85
+ at REAL NOT NULL
86
+ );
87
+ CREATE INDEX IF NOT EXISTS changes_project ON changes (project, seq);
88
+ CREATE TABLE IF NOT EXISTS screenshots (
89
+ note_id TEXT PRIMARY KEY REFERENCES notes(id) ON DELETE CASCADE,
90
+ png BLOB NOT NULL,
91
+ width INTEGER NOT NULL,
92
+ height INTEGER NOT NULL
93
+ );
94
+ SQL
95
+
96
+ # Each change to the schema after BASE, in order, applied once: MIGRATIONS[0] takes a database from
97
+ # BASE to BASE + 1. Add to the end; never change a step that may have run.
98
+ MIGRATIONS = [].freeze
99
+ VERSION = BASE + MIGRATIONS.size
100
+
101
+ LOCK = Monitor.new
102
+
103
+ module_function
104
+
105
+ def path
106
+ Tensaku.home.join(FILE)
107
+ end
108
+
109
+ # The process's connection, opened again after a fork (Puma workers) or when TENSAKU_HOME changes
110
+ # (tests). Threads share it one at a time.
111
+ def db
112
+ LOCK.synchronize do
113
+ if @db.nil? || @pid != Process.pid || @path != path
114
+ @db = connect(path)
115
+ @pid = Process.pid
116
+ @path = path
117
+ end
118
+ @db
119
+ end
120
+ end
121
+
122
+ # A write. Holds the database's write lock from the start, so what the block read is still true
123
+ # when it writes. Nested calls join the outer transaction.
124
+ # The apps a write changed (ring) are rung on the doorbell once it commits.
125
+ def write
126
+ LOCK.synchronize do
127
+ connection = db
128
+ return yield connection if connection.transaction_active?
129
+
130
+ rings = Thread.current[:tensaku_rings] = Set.new
131
+ begin
132
+ result = connection.transaction(:immediate) { yield connection }
133
+ ensure
134
+ Thread.current[:tensaku_rings] = nil
135
+ end
136
+ rings.each { Doorbell.ring(it) }
137
+ result
138
+ end
139
+ end
140
+
141
+ # Wakes the readers of the app at root once the current write commits.
142
+ def ring(root)
143
+ Thread.current[:tensaku_rings]&.add(Pathname(root).expand_path.to_s)
144
+ end
145
+
146
+ def read
147
+ LOCK.synchronize { yield db }
148
+ end
149
+
150
+ def connect(file)
151
+ file.dirname.mkpath
152
+ # Notes can quote unreleased work, so only this user can open the folder.
153
+ file.dirname.chmod(0o700) unless Gem.win_platform?
154
+ connection = SQLite3::Database.new(file.to_s, results_as_hash: true)
155
+ connection.busy_timeout = BUSY_TIMEOUT
156
+ connection.execute("PRAGMA journal_mode = WAL")
157
+ connection.execute("PRAGMA foreign_keys = ON")
158
+ connection.execute("PRAGMA synchronous = NORMAL")
159
+ migrate(connection)
160
+ connection
161
+ end
162
+
163
+ def migrate(connection)
164
+ connection.transaction(:immediate) do
165
+ current = connection.get_first_value("PRAGMA user_version").to_i
166
+ next if current >= VERSION
167
+
168
+ if current < BASE
169
+ TABLES.each { connection.execute("DROP TABLE IF EXISTS #{it}") }
170
+ connection.execute_batch(SCHEMA)
171
+ end
172
+ MIGRATIONS.drop([ current - BASE, 0 ].max).each { connection.execute_batch(it) }
173
+ connection.execute("PRAGMA user_version = #{VERSION}")
174
+ end
175
+ end
176
+
177
+ # Rows of agents that stopped without cleaning up, and the change log, are deleted after this long.
178
+ STALE_AFTER = 86_400
179
+
180
+ # Deletes those rows, for every app.
181
+ def prune!(now: Time.now)
182
+ write do |db|
183
+ db.execute("DELETE FROM changes WHERE at < ?", [ now.to_f - STALE_AFTER ])
184
+ db.execute("DELETE FROM agents WHERE seen_at < ?", [ now.to_f - STALE_AFTER ])
185
+ end
186
+ end
187
+
188
+ # Forgets the connection, for tests that delete the database file.
189
+ def reset!
190
+ LOCK.synchronize do
191
+ @db&.close
192
+ @db = nil
193
+ end
194
+ end
195
+ end
196
+ end
@@ -1,3 +1,3 @@
1
1
  module Tensaku
2
- VERSION = "0.0.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/tensaku.rb CHANGED
@@ -11,6 +11,14 @@ module Tensaku
11
11
  autoload :Middleware, "tensaku/middleware"
12
12
  autoload :Endpoints, "tensaku/endpoints"
13
13
  autoload :Notes, "tensaku/notes"
14
+ autoload :Agents, "tensaku/agents"
15
+ autoload :Project, "tensaku/project"
16
+ autoload :Session, "tensaku/session"
17
+ autoload :Store, "tensaku/store"
18
+ autoload :Repo, "tensaku/repo"
19
+ autoload :Doorbell, "tensaku/doorbell"
20
+ autoload :Presence, "tensaku/presence"
21
+ autoload :Build, "tensaku/build"
14
22
  autoload :Mcp, "tensaku/mcp"
15
23
  autoload :Caches, "tensaku/caches"
16
24
  autoload :I18nMarkers, "tensaku/i18n"
@@ -32,30 +40,25 @@ module Tensaku
32
40
  class Forbidden < Error; end
33
41
 
34
42
  class Config
35
- # Directories, relative to Rails.root, that Tensaku may write to.
43
+ # Directories and files, relative to Rails.root, that Tensaku may write to.
36
44
  attr_reader :writable_paths
37
45
 
38
- # Where note files live, relative to Rails.root.
39
- attr_accessor :notes_path
40
-
41
- # Resolved notes older than this are deleted.
42
- attr_accessor :prune_after
43
-
44
- # Keyboard shortcut that toggles the overlay, as modifier keys and a KeyboardEvent#code.
45
- attr_accessor :shortcut
46
+ # Resolved and declined notes older than this move to the archive.
47
+ attr_accessor :archive_after
46
48
 
47
49
  # Where the middleware serves the overlay and the endpoints.
48
50
  attr_accessor :mount_path
49
51
 
50
52
  def initialize
51
53
  @writable_paths = %w[app/views app/components config/locales]
52
- @notes_path = "tmp/tensaku/notes"
53
- @prune_after = 3 * 24 * 60 * 60
54
- @shortcut = "alt+shift+KeyT"
54
+ @archive_after = 3 * 24 * 60 * 60
55
55
  @mount_path = "/__tensaku"
56
56
  end
57
57
  end
58
58
 
59
+ TOKEN_LOCK = Mutex.new
60
+ TOKEN = /\A\h{32}\z/
61
+
59
62
  class << self
60
63
  def config
61
64
  @config ||= Config.new
@@ -65,7 +68,7 @@ module Tensaku
65
68
  yield config
66
69
  end
67
70
 
68
- # Lets another gem allow writes to its own source directory, for example Kiln's content/.
71
+ # Lets another gem allow writes to its own source directory or file.
69
72
  def register_writable_path(path)
70
73
  config.writable_paths << path.to_s unless config.writable_paths.include?(path.to_s)
71
74
  end
@@ -74,16 +77,72 @@ module Tensaku
74
77
  defined?(Rails) && Rails.env.development?
75
78
  end
76
79
 
77
- # A per-boot secret embedded in the page. Write requests must echo it back.
80
+ # A secret embedded in the page. Write requests must echo it back. It lives in tmp/tensaku/token,
81
+ # so every Puma worker and the next server boot use the same one (a page stays valid across a
82
+ # restart). Deleting the file makes a new one.
78
83
  def token
79
- @token ||= SecureRandom.hex(16)
84
+ @token || TOKEN_LOCK.synchronize { @token ||= read_or_create_token }
85
+ end
86
+
87
+ def token_path
88
+ Pathname(Rails.root).join("tmp/tensaku/token")
80
89
  end
81
90
 
82
91
  # The app root that relative source paths resolve against. Tests point it at a scratch copy.
92
+ # `tensaku mcp` runs without Rails and sets it per app with with_root.
83
93
  attr_writer :root
84
94
 
85
95
  def root
86
- Pathname(@root || Rails.root)
96
+ Pathname(Thread.current[:tensaku_root] || @root || Rails.root)
97
+ end
98
+
99
+ # The folder for Tensaku's database, shared by every app and checkout on this machine: TENSAKU_HOME,
100
+ # else the platform's per-user data folder.
101
+ def home
102
+ return Pathname(ENV["TENSAKU_HOME"]) unless ENV["TENSAKU_HOME"].to_s.empty?
103
+
104
+ if Gem.win_platform?
105
+ Pathname(ENV["LOCALAPPDATA"].to_s.empty? ? File.join(Dir.home, "AppData", "Local") : ENV["LOCALAPPDATA"]).join("tensaku")
106
+ elsif RUBY_PLATFORM.include?("darwin")
107
+ Pathname(Dir.home).join("Library", "Application Support", "tensaku")
108
+ else
109
+ Pathname(ENV["XDG_DATA_HOME"].to_s.empty? ? File.join(Dir.home, ".local", "share") : ENV["XDG_DATA_HOME"]).join("tensaku")
110
+ end
111
+ end
112
+
113
+ # Runs the block with root set to path, in this thread only.
114
+ def with_root(path)
115
+ previous = Thread.current[:tensaku_root]
116
+ Thread.current[:tensaku_root] = Pathname(path)
117
+ yield
118
+ ensure
119
+ Thread.current[:tensaku_root] = previous
120
+ end
121
+
122
+ private
123
+
124
+ def read_or_create_token
125
+ path = token_path
126
+ path.dirname.mkpath
127
+ existing = path.file? ? path.read.strip : ""
128
+ return existing if existing.match?(TOKEN)
129
+
130
+ path.delete if path.file?
131
+ # Written to a temp file and linked into place, so a worker never reads a half-written token,
132
+ # and the first worker to link wins.
133
+ temp = path.dirname.join(".token-#{SecureRandom.hex(4)}")
134
+ temp.write(SecureRandom.hex(16))
135
+ temp.chmod(0o600)
136
+ begin
137
+ File.link(temp, path)
138
+ rescue Errno::EEXIST
139
+ nil
140
+ ensure
141
+ temp.delete
142
+ end
143
+ path.read.strip.then { it.match?(TOKEN) ? it : SecureRandom.hex(16) }
144
+ rescue SystemCallError
145
+ SecureRandom.hex(16)
87
146
  end
88
147
  end
89
148
  end
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: tensaku
3
- description: Work through Tensaku notes, the comments a person left on pages of this Rails app in development, each pointing at the exact file and line that rendered the element. Use when the user mentions Tensaku, notes on the page, "my notes", or asks you to go through what they marked up in the browser.
3
+ description: Work through Tensaku notes, the comments a person left on pages of this Rails app in development, each pointing at the exact file and line that rendered the element. Use when the user mentions Tensaku, notes on the page, "my notes", asks you to go through what they marked up in the browser, or asks you to watch or listen for their notes.
4
4
  ---
5
5
 
6
6
  # Tensaku notes
@@ -13,10 +13,15 @@ one or more targets with the source position Herb stamped on the element.
13
13
 
14
14
  Use whichever is available, in this order:
15
15
 
16
- 1. **MCP**: the `tensaku` server (`/__tensaku/mcp` on the dev server). Tools: `list_notes`, `get_note`,
17
- `resolve_note`, `ask_question`, `watch_notes`, `current_selection`. The dev server must be running.
18
- 2. **Files**: `tmp/tensaku/notes/*.json`, one note per file. Read them directly when the server isn't
19
- running. Edit `status` and `reply` in the file to resolve one.
16
+ 1. **MCP**: the `tensaku` server (`bundle exec tensaku mcp`, started from the project's MCP config).
17
+ Tools: `list_notes`, `get_note`, `get_screenshot`, `start_note`, `resolve_note`, `decline_note`, `reply_to_note`,
18
+ `ask_question`, `watch_notes`, `channel_ready`, `current_selection`. It reads Tensaku's database, so the dev server
19
+ doesn't have to be running.
20
+ 2. **Command line**: `bundle exec tensaku notes`, when the MCP server isn't configured. It does what the
21
+ tools do and prints the same text: `tensaku notes` lists the open notes, then `tensaku notes start
22
+ ID`, `resolve ID REPLY`, `decline ID REASON`, `ask ID QUESTION`, `reply ID MESSAGE`, `show ID`,
23
+ `screenshot ID` (saves the note's screenshot as a PNG and prints where) and `watch`. Pass `--as NAME`
24
+ with your name, so the page shows who is working.
20
25
 
21
26
  A note looks like this:
22
27
 
@@ -27,16 +32,22 @@ A note looks like this:
27
32
  "comment": "tighten this, and the heading is wrong",
28
33
  "targets": [
29
34
  { "kind": "element", "file": "app/views/pages/_hero.html.erb", "range": "4:3-4:40", "tag": "h1", "text": "About me",
35
+ "classes": ["hero__title", "text-4xl"],
30
36
  "context": { "partials": ["app/views/pages/_hero.html.erb", "app/views/pages/about.html.erb"], "controller": "hero" } },
31
37
  { "kind": "translation", "key": "pages.about.title", "locale": "en", "file": "config/locales/en.yml", "range": "12" }
32
38
  ],
33
39
  "rect": { "x": 120, "y": 340, "width": 600, "height": 180 },
34
40
  "status": "open",
35
- "reply": null
41
+ "sent_at": "2026-11-10T09:12:00.000000Z",
42
+ "thread": []
36
43
  }
37
44
  ```
38
45
 
39
- - `range` is `line:column-line:column` in `file` (1-based). For translations it's the line of the key.
46
+ - `sent_at` is when the person last saved, edited or answered the note.
47
+ - `classes` are the element's own CSS classes, for notes about how something looks.
48
+
49
+ - `range` is `line:column-line:column` in `file` (1-based). For translations it's the line of the key. It's
50
+ from when the note was written: if the file has changed since, look for the target's `text` in it.
40
51
  - `kind` is `element`, `text` (the person selected that text), `translation` (the text came from that
41
52
  I18n key) or `markdown` (a Markdown source block).
42
53
  - `rect` is set for area notes: the person dragged over a region, and `targets` lists the elements in it.
@@ -45,17 +56,48 @@ A note looks like this:
45
56
 
46
57
  ## Working through notes
47
58
 
48
- 1. List the open notes (`list_notes`, or read the files with `"status": "open"`).
49
- 2. For each note, open the target file at the range, read the comment, and make the change. Look at
59
+ 1. List the open notes (`list_notes`, or `tensaku notes`). Each is marked *Yours* or *Nobody's yet*;
60
+ notes other sessions hold aren't listed.
61
+ 2. Call `start_note` on the note you take, before you change anything, so the person sees it's being
62
+ worked on and no other session takes it. If it fails, leave that note. `resolve_note` refuses a
63
+ note you didn't start, and a note the person took back from you. A note marked as stopped was
64
+ started by a session that ended: part of its change may already be in the file. Then open the
65
+ target file at the range, read the comment, and make the change. Look at
50
66
  `context.partials` when the fix belongs in a parent template, and at the translation target when the
51
67
  text comes from a locale file (change every locale that needs it).
52
- 3. If the comment is ambiguous, don't guess: call `ask_question` with a short, specific question. The
68
+ 3. Read the note's `thread`: the person's answers to earlier questions are there. Use `reply_to_note`
69
+ to say what you found when that helps the person. If the comment is ambiguous, don't guess: call `ask_question` with a short, specific question. The
53
70
  question shows on the page and the note waits for an answer.
54
- 4. When done, call `resolve_note` with a one-line reply that says what changed and where, for example
55
- `Shortened the intro in app/views/pages/_hero.html.erb:6`. The pin on the page turns green so the
56
- person can check it.
71
+ 4. As soon as the change is made, call `resolve_note` with a one-line reply that says what changed and
72
+ where, for example `Shortened the intro in app/views/pages/_hero.html.erb:6`. The pin on the page
73
+ turns green so the person can check it.
74
+ 5. If the change shouldn't be made (it would break something, or belongs somewhere else), call
75
+ `decline_note` with the reason instead of resolving it.
76
+
77
+ If `start_note` says a note belongs to another agent session, leave that note and take the next. When
78
+ you start watching, tell the person the session name the first `watch_notes` answer gives you (such as
79
+ amber-fox): the page shows it next to your name.
80
+
81
+ Take the notes one at a time, and resolve each before you start the next. The person is watching the page,
82
+ so don't make every change first and update the notes at the end. These calls are small: `start_note` and
83
+ `resolve_note` answer with one line. `list_notes` already has each note's text, so don't call
84
+ `get_note` after it, unless a thread says earlier messages were left out and you need them.
85
+
86
+ In Claude Code, Tensaku may arrive as a channel: a `<channel source="tensaku">` message asks you to
87
+ call `channel_ready` with a code, and later ones hold notes the person saved, already yours. Work
88
+ through those like notes from `watch_notes`; there's no need to call `watch_notes` then.
89
+
90
+ Once you have called `watch_notes`, notes the person saves while you work come with the answer of
91
+ your next Tensaku call ("Notes the person saved while you worked"), already yours: work through them
92
+ after the current note. Call `watch_notes` again only when you have nothing else to do. When an answer
93
+ ends with "notes were saved on the page since you last looked", call `watch_notes` or `list_notes`
94
+ after finishing the current note.
57
95
 
58
- To keep going while the person adds notes, call `watch_notes` in a loop, passing the previous
59
- `checked_at` as `since`.
96
+ When the person asks you to watch or listen for their notes, call `watch_notes` in a loop: work through
97
+ the notes it returns, then call it again, passing the `checked_at` line of the previous answer as
98
+ `since`. When it times out with no notes, call it again. The page shows you as listening while you
99
+ wait, and the notes they save reach you as they write them. Stop when they ask you to.
60
100
 
61
- You can take a screenshot of `url` with a browser tool if the layout matters; notes don't include one.
101
+ A note that says "Screenshot:" has a picture of what it points at, as the page looked when the person
102
+ wrote it. Get it with `get_screenshot` (or `tensaku notes screenshot ID`) when the comment is about how
103
+ something looks.