Skip to content

Commit 520daf9

Browse files
committed
Fix(tasks): a repeating task's block id stays on the open occurrence (#1083)
Checking off a repeating task copies its line: the next occurrence goes above, the finished line stays below as the record. The copy was verbatim, so a trailing `^plants` landed on both lines, and every roll added one more line carrying the same id. Lookup takes the first of a repeated id, so links happened to keep reaching the open task, but the note held two, then three, then N copies of an id that is meant to name one block. #1083 lists this as the first thing to settle before work sessions link tasks by `path#^id`. The id now stays with the open occurrence and leaves the record. A link or embed of a repeating task means the series, the chore you do next, and that is what existing `[[Chores^plants]]` links and `![[Chores^plants]]` embeds already showed; moving the id to the record instead (what the issue first proposed) would have pointed every one of them at finished work. No id is generated, so the zn Go mirror can reproduce the edit exactly from the shared cases. The record already drops `@repeat` and `@remind`, the tokens that only mean something on open work; the id joins them. The subtree follows the same rule: the fresh copies keep their ids, the record's children lose theirs, and a line that is only a marker (`^review` under the task, naming the block above it) is left out of the record. The editor path keeps its "select subtasks with the task" behavior and writes those done from the record's id-less line. A second bug on the same line: a tagged task with no `due:` got the roll's new `due:` appended after the marker (`Stretch ^stretch due:2026-11-04`), so the id no longer ended the line and the open occurrence lost it. The new `due:` now goes in before the marker. It does not touch how ids render. Two display bugs seen while verifying (a Preview embed of a repeating task with subtasks clips its first letters; Home's Today list shows the raw `^plants`) predate this change and are being looked at separately. Docs: one sentence in the Repeat a task entry of help.ts; the website mirror is on website/2.87.0. Tested: 7 new shared cases in task-recurrence.cases.json (own id; due added before the id; subtask and note ids; a marker-only line; a fence after the task keeps its caret; Skip this time; CRLF with a dropped marker line) and an editor test for ticked subtasks with ids. npm run typecheck clean; test:run green (shared-domain 2698, app-core 4948, desktop 1952). The built app over CDP with isolated stores: Mod+L on `Water the plants @repeat:weekly due:2026-11-15 ^plants` with two subtasks writes `^plants` and `^ferns` only on the new 2026-11-22 lines, in the editor and on disk; after a relaunch a Dashboard's `![[Chores^plants]]` shows the open 11-22 task with fresh subtasks. The zn CLI pins task-recurrence.cases.json by hash, so its Go engine needs the same rule and the new file before its next sync. How to test locally: `npm run build --workspace @zennotes/desktop`, then `npx electron apps/desktop/out/main/index.js`. In a note, write `- [ ] Water the plants @repeat:weekly due:2026-11-15 ^plants`, put the cursor on it and press Cmd+L. Before: both lines end in `^plants`. After: only the new open line does, and the `[x]` record has no id.
1 parent 394d060 commit 520daf9

5 files changed

Lines changed: 119 additions & 16 deletions

File tree

‎packages/app-core/src/lib/cm-task-recurrence.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ describe('checking off a recurring task in the editor (#1036)', () => {
5858
)
5959
})
6060

61+
it('block ids stay on the copies; the record, ticked subtasks included, drops them (#1083)', () => {
62+
const doc =
63+
'- [ ] Review @repeat:weekly due:2026-11-01 ^review\n - [ ] Inbox ^inbox\n ^inbox-block\n' +
64+
' - [ ] Plan ^plan\n- [ ] other'
65+
const view = new EditorView({
66+
state: EditorState.create({ doc, selection: EditorSelection.range(0, doc.indexOf('Plan') + 2) })
67+
})
68+
toggleCheckbox(view)
69+
expect(view.state.doc.toString()).toBe(
70+
'- [ ] Review @repeat:weekly due:2026-11-08 ^review\n - [ ] Inbox ^inbox\n ^inbox-block\n' +
71+
' - [ ] Plan ^plan\n- [x] Review due:2026-11-01\n - [x] Inbox\n - [x] Plan\n- [ ] other'
72+
)
73+
})
74+
6175
it('one undo step: the whole edit is a single transaction', () => {
6276
const view = viewOf('- [ ] A @repeat:daily due:2026-11-01', 0)
6377
let transactions = 0

‎packages/app-core/src/lib/cm-task-recurrence.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,16 @@ export function recurringCheckOff(
6262
})
6363
if (!plan) return null
6464
const last = state.doc.line(lineNumber + plan.replaces - 1)
65-
const recordChildren = plan.recordChildren.map((child, k) => {
66-
if (!opts.ticked?.(lineNumber + 1 + k)) return child
65+
const recordChildren = plan.recordChildren.flatMap((child, k) => {
66+
if (child == null) return []
67+
if (!opts.ticked?.(lineNumber + 1 + k)) return [child]
68+
// Ticked: the subtask the record would cancel is written done instead.
69+
// It starts from the record's line, which has already lost its `^id`.
6770
const before = state.doc.line(lineNumber + 1 + k).text.match(TASK_LINE_RE)
68-
return before && (before[2] === ' ' || before[2] === '/') ? `${before[1]}x${before[3]}` : child
71+
const recorded = child.match(TASK_LINE_RE)
72+
return before && recorded && (before[2] === ' ' || before[2] === '/')
73+
? [`${recorded[1]}x${recorded[3]}`]
74+
: [child]
6975
})
7076
return {
7177
change: {

‎packages/app-core/src/lib/help.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ export const HELP_CORE_CONCEPTS: HelpCard<HelpText>[] = [
579579
{
580580
title: 'Repeat a task',
581581
body:
582-
'Add `@repeat:` to a task and checking it off schedules the next one: `- [ ] Water the plants @repeat:weekly due:2026-11-01`. The rules are `daily`, `weekdays`, `weekly`, `monthly` and `yearly`; `2d`, `3w`, `6m` or `1y` for every so many days, weeks, months or years; weekdays like `mon/thu`; and `after-3d` (any interval) to count from the day you finish instead of the due date. Check it off with `⌘L`, a click on its checkbox, `x` in the Tasks views or a drop into Done, and the next occurrence is written right above it with the next date in `due:`, while the finished line stays below as the record, without `@repeat` (so unchecking it never makes a second copy). Its subtasks repeat too, every checkbox open again, and the record cancels (`[-]`) the ones you left open, so only the fresh copies stay on your lists; subtasks you select along with the task (`Vjj` then `⌘L`) are checked off instead. The next date counts from the due date (or, without one, the daily note’s date, then today) and is never today or earlier: a daily task done three days late comes back tomorrow, not as three overdue copies. `monthly` from the 31st lands on the last day of shorter months. Press `r` in the Tasks views (or Repeat… in the right-click menu, or `:repeat weekly` in a note) to set or stop it; Skip this time in the menu schedules the next one and records this one as cancelled. In the Tasks views a repeating task shows a repeat icon beside its date. Typing an `x` into the brackets by hand is just text and does not repeat. Whole-note task files do not repeat yet.'
582+
'Add `@repeat:` to a task and checking it off schedules the next one: `- [ ] Water the plants @repeat:weekly due:2026-11-01`. The rules are `daily`, `weekdays`, `weekly`, `monthly` and `yearly`; `2d`, `3w`, `6m` or `1y` for every so many days, weeks, months or years; weekdays like `mon/thu`; and `after-3d` (any interval) to count from the day you finish instead of the due date. Check it off with `⌘L`, a click on its checkbox, `x` in the Tasks views or a drop into Done, and the next occurrence is written right above it with the next date in `due:`, while the finished line stays below as the record, without `@repeat` (so unchecking it never makes a second copy). A block id such as `^plants` moves to the next occurrence and leaves the record, so a `[[Chores^plants]]` link or embed keeps showing the task you do next. Its subtasks repeat too, every checkbox open again, and the record cancels (`[-]`) the ones you left open, so only the fresh copies stay on your lists; subtasks you select along with the task (`Vjj` then `⌘L`) are checked off instead. The next date counts from the due date (or, without one, the daily note’s date, then today) and is never today or earlier: a daily task done three days late comes back tomorrow, not as three overdue copies. `monthly` from the 31st lands on the last day of shorter months. Press `r` in the Tasks views (or Repeat… in the right-click menu, or `:repeat weekly` in a note) to set or stop it; Skip this time in the menu schedules the next one and records this one as cancelled. In the Tasks views a repeating task shows a repeat icon beside its date. Typing an `x` into the brackets by hand is just text and does not repeat. Whole-note task files do not repeat yet.'
583583
},
584584
{
585585
title: 'Remind me before a task is due',

‎packages/shared-domain/src/task-recurrence.cases.json‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,56 @@
158158
"today": "2026-11-01",
159159
"expected": null
160160
},
161+
{
162+
"name": "a block id stays on the open occurrence and leaves the record",
163+
"body": "- [ ] Water the plants @repeat:weekly due:2026-11-01 ^plants",
164+
"taskIndex": 0,
165+
"today": "2026-11-01",
166+
"expected": "- [ ] Water the plants @repeat:weekly due:2026-11-08 ^plants\n- [x] Water the plants due:2026-11-01"
167+
},
168+
{
169+
"name": "a due the roll adds goes before the block id, which must end the line",
170+
"body": "- [ ] Stretch @repeat:3d ^stretch",
171+
"taskIndex": 0,
172+
"today": "2026-11-01",
173+
"expected": "- [ ] Stretch @repeat:3d due:2026-11-04 ^stretch\n- [x] Stretch"
174+
},
175+
{
176+
"name": "subtask and note ids stay on the copies and leave the record",
177+
"body": "- [ ] Review @repeat:weekly due:2026-11-01 ^review\n - [ ] Inbox ^inbox\n - [x] Plan ^plan\n Notes on the week ^notes",
178+
"taskIndex": 0,
179+
"today": "2026-11-01",
180+
"expected": "- [ ] Review @repeat:weekly due:2026-11-08 ^review\n - [ ] Inbox ^inbox\n - [ ] Plan ^plan\n Notes on the week ^notes\n- [x] Review due:2026-11-01\n - [-] Inbox\n - [x] Plan\n Notes on the week"
181+
},
182+
{
183+
"name": "a marker alone on its line stays with the copy, and the record drops the line",
184+
"body": "- [ ] Weekly review @repeat:weekly due:2026-11-01\n - [ ] Inbox\n ^review\n- [ ] after",
185+
"taskIndex": 0,
186+
"today": "2026-11-01",
187+
"expected": "- [ ] Weekly review @repeat:weekly due:2026-11-08\n - [ ] Inbox\n ^review\n- [x] Weekly review due:2026-11-01\n - [-] Inbox\n- [ ] after"
188+
},
189+
{
190+
"name": "a fenced sample after the task is not its subtree and keeps its caret",
191+
"body": "- [ ] Deploy @repeat:weekly due:2026-11-01\n ```\n echo 2 ^two\n ```",
192+
"taskIndex": 0,
193+
"today": "2026-11-01",
194+
"expected": "- [ ] Deploy @repeat:weekly due:2026-11-08\n- [x] Deploy due:2026-11-01\n ```\n echo 2 ^two\n ```"
195+
},
196+
{
197+
"name": "skip this time moves the block id the same way",
198+
"body": "- [ ] Gym @repeat:mon/thu due:2026-11-02 ^gym",
199+
"taskIndex": 0,
200+
"today": "2026-11-01",
201+
"skip": true,
202+
"expected": "- [ ] Gym @repeat:mon/thu due:2026-11-05 ^gym\n- [-] Gym due:2026-11-02"
203+
},
204+
{
205+
"name": "windows line endings survive a record that drops a marker line",
206+
"body": "- [ ] Win @repeat:daily due:2026-11-01 ^win\r\n - [ ] sub ^sub\r\n ^tail\r\n- [ ] after\r\n",
207+
"taskIndex": 0,
208+
"today": "2026-11-01",
209+
"expected": "- [ ] Win @repeat:daily due:2026-11-02 ^win\r\n - [ ] sub ^sub\r\n ^tail\r\n- [x] Win due:2026-11-01\r\n - [-] sub\r\n- [ ] after\r\n"
210+
},
161211
{
162212
"name": "a task without a rule is ordinary",
163213
"body": "- [ ] Plain due:2026-11-01",

‎packages/shared-domain/src/task-recurrence.ts‎

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* daylight-saving change can never shift a date. The zn CLI mirrors this edit
2525
* in Go; `task-recurrence.cases.json` holds the cases both test suites run.
2626
*/
27+
import { trailingBlockIdRange } from './block-anchors'
2728
import { TASK_LINE_RE, leadingIndentWidth, taskBlockEnd, taskLineNumbers } from './tasklists'
2829
import { noteFrontmatterDue } from './tasks'
2930

@@ -218,6 +219,11 @@ function bare(line: string): string {
218219
return line.endsWith('\r') ? line.slice(0, -1) : line
219220
}
220221

222+
/** `text` cut at a block marker found at `from`, trailing blanks trimmed. */
223+
function withoutMarker(text: string, from: number): string {
224+
return text.slice(0, from).replace(/[ \t]+$/, '')
225+
}
226+
221227
export interface RecurringCompletionOptions {
222228
/** The day the task is finished, `YYYY-MM-DD` in the user's time zone. */
223229
today: string
@@ -233,9 +239,11 @@ export interface RecurringLinePlan {
233239
next: string[]
234240
/** The finished task's own line, rewritten as the record. */
235241
record: string
236-
/** Its subtree as the record keeps it: the subtasks still open (`[ ]`,
237-
* `[/]`) cancelled, everything else as it was. */
238-
recordChildren: string[]
242+
/** Its subtree as the record keeps it, one entry per line under the task:
243+
* the subtasks still open (`[ ]`, `[/]`) cancelled, block ids dropped,
244+
* everything else as it was. Null for a line the record leaves out (a
245+
* `^id` marker alone on its line). */
246+
recordChildren: (string | null)[]
239247
/** How many lines the plan replaces: the task's line and its subtree. */
240248
replaces: number
241249
nextDue: string
@@ -270,27 +278,52 @@ export function planRecurringCompletion(
270278
// The occurrence keeps the text, priority, tags and every other field, in
271279
// their order; `due:` is replaced where it stood. Its per-occurrence state
272280
// does not carry: `@waiting` and the board's `@status:` start fresh.
281+
// A `^id` block marker has to end its line, so a `due:` the roll adds goes
282+
// in before it.
283+
const ownMarker = trailingBlockIdRange(rest)
273284
let nextRest = DUE_TOKEN_RE.test(rest)
274285
? rest.replace(DUE_TOKEN_RE, `$1due:${nextDue}`)
275-
: `${rest.replace(/\s+$/u, '')} due:${nextDue}`
286+
: ownMarker
287+
? `${withoutMarker(rest, ownMarker.from)} due:${nextDue} ${rest.slice(ownMarker.from)}`
288+
: `${rest.replace(/\s+$/u, '')} due:${nextDue}`
276289
nextRest = tidy(nextRest.replace(WAITING_TOKEN_RE, '$1').replace(STATUS_TOKEN_RE, '$1'))
277290
const next = [`${match[1]} ]${nextRest}`]
278291

292+
// Block ids (`^id`) stay with the open occurrence and leave the record
293+
// (#1083). A link or embed of a repeating task means the series, and lookup
294+
// takes the first of a repeated id, which was the occurrence above: copying
295+
// the id to both lines grew one more duplicate per roll, while moving it to
296+
// the record would point every existing link at finished work. A fence
297+
// ends a task's subtree, so no line here is code and each `^id` is one.
298+
const end = taskBlockEnd(lines.map(bare), at, leadingIndentWidth(bare(line)))
299+
const block = lines.slice(at, end).map(bare)
300+
279301
// The subtree comes along with every checkbox open again: a weekly review's
280302
// checklist starts each week unticked. Notes under it copy verbatim.
281303
// The record cancels the subtasks it left open, so only the fresh copies
282304
// above read as live work; ticked, cancelled and forwarded ones stay.
283-
const end = taskBlockEnd(lines.map(bare), at, leadingIndentWidth(bare(line)))
284-
const recordChildren: string[] = []
285-
for (let i = at + 1; i < end; i++) {
286-
const child = bare(lines[i])
305+
const recordChildren: (string | null)[] = []
306+
for (let k = 1; k < block.length; k++) {
307+
let child = block[k]
287308
const childMatch = child.match(TASK_LINE_RE)
288309
next.push(childMatch ? `${childMatch[1]} ${childMatch[3]}` : child)
289-
const open = childMatch && (childMatch[2] === ' ' || childMatch[2] === '/')
290-
recordChildren.push(open ? `${childMatch[1]}-${childMatch[3]}` : child)
310+
const marker = trailingBlockIdRange(child)
311+
if (marker) {
312+
// A marker alone on its line names the block above it; the record
313+
// has no use for the line once the id is gone.
314+
child = withoutMarker(child, marker.from)
315+
if (child.trim() === '') {
316+
recordChildren.push(null)
317+
continue
318+
}
319+
}
320+
const recordMatch = childMatch && child.match(TASK_LINE_RE)
321+
const open = recordMatch && (recordMatch[2] === ' ' || recordMatch[2] === '/')
322+
recordChildren.push(open ? `${recordMatch[1]}-${recordMatch[3]}` : child)
291323
}
292324

293-
const recordRest = tidy(rest.replace(REPEAT_TOKEN_RE, '$1').replace(REMIND_TOKEN_RE, '$1'))
325+
const recordSource = ownMarker ? withoutMarker(rest, ownMarker.from) : rest
326+
const recordRest = tidy(recordSource.replace(REPEAT_TOKEN_RE, '$1').replace(REMIND_TOKEN_RE, '$1'))
294327
const record = `${match[1]}${opts.skip ? '-' : 'x'}]${recordRest}`
295328
return { next, record, recordChildren, replaces: end - at, nextDue, rule }
296329
}
@@ -343,7 +376,7 @@ export function completeRecurringTaskAtLine(
343376
plan.replaces,
344377
...plan.next.map((l) => l + ending),
345378
plan.record + ending,
346-
...plan.recordChildren.map((l, k) => l + endingOf(at + 1 + k))
379+
...plan.recordChildren.flatMap((l, k) => (l == null ? [] : [l + endingOf(at + 1 + k)]))
347380
)
348381
return {
349382
body: lines.join('\n'),

0 commit comments

Comments
 (0)