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
25 changes: 25 additions & 0 deletions .kiln/work/20261121-renumber-note-positions-when-repeated/brief.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Brief — note order keeps working after many drops into one place

Follows spike 20261121-review-each-items-above-one-one, finding F5 (audit items 3 and 5).

## What is asked
- Who: the owner, who re-sorts notes by drag and drop or Alt+Up / Alt+Down.
- What: a drop or step must always land where it was aimed, however many times notes go into the same gap.
- Success: 40 drops right under the top note all land second; existing single-move results stay as they are.
- Constraint: positions of neighbours may change, but their `updatedAt` must not (they were not edited).

## What I found
- `extension/model.js:80-84` `positionBetween` takes the midpoint of the two neighbours. Positions start as millisecond timestamps (about 1.7e12, `newNote` in model.js:47), so after about 23 halvings of a 1000 gap the midpoint equals a neighbour; the spike's script showed drop 24 landing wrong.
- `placeNote` (model.js:62-69) returns `{ position, pinned }` for the moved note only; `stepNote` (model.js:71-77) calls it.
- `extension/app.js` `applyPlacement` merges that into the one note and persists it. It is the only caller of both functions (app.js:699, 739).
- `tests/model.test.js:61-74` pin the current single-move results.
- Notes that already share a position (collapsed earlier, or old notes with equal `updatedAt`) hit the same case: the midpoint equals both.

## Still unknown
- None blocking.

## Past learnings
- None: no learnings store (`.kiln/learnings/` does not exist), recall not run.

## Design (bounded: two sentences)
When the midpoint is not strictly between its neighbours, renumber the whole section in its new order, POSITION_STEP apart, and return the other notes' new positions as `renumbered: [{ id, position }]` beside the moved note's `{ position, pinned }`. `applyPlacement` applies and saves those positions with the moved note, leaving their `updatedAt` alone.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Pre-flight: no shared interfaces
Task 1: Step 2 matched (forty-drops test failed at drop 24).
Task 1: regression proved: renumber branch disabled -> 3 fail; restored -> 14/14.
Task 1: complete (tests: node --test "tests/*.test.js" -> exit 0, 217 pass)
Ruling: recorded with --phase full, since the project has no fast phase - cost if wrong: none.
Ruling: applyPlacement also cancels pending scheduled saves of renumbered notes, since persist saves their latest state - cost if wrong: none, the pending edit is in the saved object.
Ruling: the work id lacks the minted "-drops" suffix (typed short at open) - cost if wrong: a branch name one word shorter.
88 changes: 88 additions & 0 deletions .kiln/work/20261121-renumber-note-positions-when-repeated/plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Note order after repeated drops — Implementation Plan

> **For agentic workers:** REQUIRED SUB-SKILL: use kiln-implement to work this plan task by task. Steps use checkbox (`- [ ]`) syntax, and a checked box is a claim kiln re-checks rather than believes.

**Goal:** A drop or step always lands where it was aimed, however many times notes go into the same gap.

**Architecture:** `placeNote` keeps its midpoint; when the midpoint is not strictly between its neighbours it renumbers the section and returns the other notes' new positions. `applyPlacement` saves them with the moved note.

**Tech Stack:** plain ES modules, `node:test`.

**Spec:** `.kiln/work/20261121-renumber-note-positions-when-repeated/brief.md`

## Global Constraints

- Existing results in `tests/model.test.js:61-74` stay exactly as they are (no `renumbered` key when no renumber happens).
- Renumbered neighbours keep their `updatedAt`.
- No new file, no new dependency.

## Risk flags

- none

## I/O & Edge-Case Matrix

| Scenario | Input / State | Expected Output / Behavior | Error Handling |
|----------|---------------|----------------------------|----------------|
| Room left | neighbours 1000 apart | `{ position, pinned }` only, as today | n/a |
| Gap used up | 40 drops right under the top note | every drop lands second | n/a |
| Neighbours already equal | two notes share one position, drop between them | lands between them, order of the rest kept | n/a |
| Pinned section | renumber while dropping into the pinned section | only pinned notes renumbered | n/a |

## Review Focus

- Step with Alt+Up/Down through a collapsed gap: `stepNote` goes through `placeNote`, so it is covered by the same code; one assertion in the repeated-drop test steps once.
- A renumber touches every note of the section: one save of all of them in one `persist` call.

---

### Task 1: Renumber on collision, applied by the app

**Files:**
- Modify: `extension/model.js:62-84` (`placeNote`, `positionBetween`)
- Modify: `extension/app.js` `applyPlacement`
- Test: `tests/model.test.js`

**Acceptance:**
- Every matrix row passes as a test.
- `applyPlacement` saves the moved note and every renumbered note in one `persist` call, renumbered notes without a new `updatedAt`.

**Verification:** `node --test "tests/*.test.js"` → `# fail 0`.
**Depends on:** none · **Size:** S

**Interfaces:**
- Produces: `placeNote(notes, { id, targetId, placement }) → { position: number, pinned: boolean, renumbered?: Array<{ id: string, position: number }> }`; `stepNote` returns the same shape or `null`.

- [ ] **Step 1: Write the failing tests in `tests/model.test.js`**

```js
test("forty drops right under the top note all land second", () => {
// notes: top (position T), under (T - 1000); 40 times: add n_i at T - 5000 - i, place it after "top",
// apply { position } to n_i and every renumbered { id, position }; assert sorted[1].id === n_i each time.
// Then stepNote the second note down once (step: 1) and assert it is third.
});

test("a drop between two notes that share a position lands between them", () => {
const notes = [note({ id: "a", position: 10 }), note({ id: "b", position: 5 }), note({ id: "c", position: 5 }), note({ id: "m", position: 1 })];
// place "m" after "b"; apply; assert the order is a, b, m, c and a stays first.
});

test("renumbering stays inside the pinned section", () => {
// two pinned notes sharing a position and one unpinned note; place a third pinned note between the pinned pair;
// assert renumbered holds only pinned ids.
});
```

Each test applies a result with one helper in the test file: `apply(notes, id, result)` → notes with `result.position`/`pinned` on `id` and each `renumbered` position on its note.

- [ ] **Step 2: Run them to see them fail**

Run: `node --test tests/model.test.js`
Expected: FAIL on the forty-drops test (a drop lands third or lower)

- [ ] **Step 3: In `placeNote`, when `positionBetween` returns a value equal to `above` or `below`'s position, build the section's new order with the moved note inserted, give the i-th note from the top `(count - i) * POSITION_STEP`, and return the moved note's position with `renumbered` for the others; in `applyPlacement`, apply `renumbered` to those notes (no `updatedAt` change) and persist them together with the moved note**

- [ ] **Step 4: Run the tests to see them pass**

Run: `node --test "tests/*.test.js"`
Expected: `# fail 0`, 217 tests (214 baseline + 3; the unit suite on `main` has 214)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 20261121-renumber-note-positions-when-repeated — note order keeps working after many drops

## Change
- Fixed: dragging notes, or moving them with Alt+Up / Alt+Down, always lands them where aimed, even after many moves into the same place or between notes that share a place.

## Before release
- [x] The change carries no migration and reads no new configuration value. (checked)
- [x] `kiln verify` ran the full unit suite: exit 0, 218 of 218 pass on Node 22.17.1. (checked)
- [x] The new tests fail with the renumber disabled (3 fail) and the scale fix undone (1 fails), and pass with both. (checked)
- [ ] In the desktop app, dropping a note right under the top note 30 times keeps it second each time. (confirm)

## Release steps
1. Merge the pull request once CI is green.
2. Ship it in the next release as usual.

## After release
- [ ] After updating, the notes list keeps its manual order, and pinning or unpinning a note still places it by age. (confirm)

## Rollback
- Trigger: notes reorder on their own, or a drop lands in the wrong place more often than before.
- Steps: `git revert` the merge commit, then open and merge that pull request, then release.
- Time: one revert pull request and one release. Positions already rewritten stay valid numbers, so the old code reads them.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Review — 20261121-renumber-note-positions-when-repeated

lenses: reviewer reported, blind-hunter reported, edge-case-hunter reported, verification-gap reported, intent-alignment reported, code-review-specialist reported, security-auditor reported, test-engineer reported

Scope — predicted 3 · actual 3

Rules: no rule is routed to this change, so there is no checklist.

Bar: nothing lowers the bar.

## Triage log

| # | Reader(s) | Location | Finding | Verdict | Grade | Route |
|---|---|---|---|---|---|---|
| 1 | reviewer, blind, edge-case, intent, code-review, test-engineer | extension/model.js `spacedOut` | renumbering to 1000·n put a section on a different scale from timestamps; after it, unpin sends a note to the bottom, pin to the bottom of Pinned, recover and import to the top | real: the reviewer reproduced the unpin case; new test RED before the fix | Important · Likely | fix pass |
| 2 | verification-gap, blind, test-engineer | tests/model.test.js | no renumber test uses `placement: "before"`; a mutant using `index + 1` for both survives | real gap; the code's `index` for "before" is correct by reading (insert before the target) | Important · Unlikely | note |
| 3 | verification-gap, blind, intent, code-review, test-engineer | extension/app.js `applyPlacement` | the tests apply results with their own helper; app.js's apply, `saves.cancel` and single `persist` are untested | real: app.js cannot be imported in Node | Important · Unlikely | note |
| 4 | verification-gap, blind, code-review, test-engineer, reviewer | tests/model.test.js pinned test | asserts only that renumbered ids start with "p", not the order | real, weak assertion | Minor · Possible | note |
| 5 | test-engineer | extension/model.js `fitsBetween` | each half untested alone; spacing values untested | real | Minor · Unlikely | drop |
| 6 | blind, edge-case, test-engineer | merge path | two windows renumbering at once could conflict | single window today | Minor · Unlikely | drop |
| 7 | blind | extension/app.js `saves.cancel` on neighbours | could drop a pending edit | false: `onEditorChange` and `onBoardChange` write the edit into `notes` before scheduling (reviewer checked), and the renumbered note is saved from `notes` | false | drop |
| 8 | blind | extension/model.js comment | "about 23 drops" depends on a 1000 gap | real; the comment now says "a 1000 gap" | Minor · Possible | fixed with 1 |
| 9 | blind, code-review | extension/app.js | the local `spacedOut` array shares the model function's name | readable in context | — | drop |
| 10 | intent, blind, test-engineer | extension/app.js | the `updatedAt` constraint has no test | real; same root as 3 | — | note (with 3) |
| 11 | blind, intent | .gitignore | kiln line not in the diff | kiln setup | — | declined to judge |

## Fixed

fixed 1 (renumbered section on another scale) — a renumbered note that is unpinned lands by its age, not at the bottom of the list RED→GREEN, suite 218/218

## Rulings

- Ruling: findings 2 and 3 are not fixed here — 2 is code correct by reading with an unpinned mutant gap, 3 needs app.js importable in Node; both are below a fix-now cell — cost if wrong: a later edit to the "before" index or to `applyPlacement` could ship unseen.

## Deferred

- A renumber test with `placement: "before"` and a stronger pinned-order assertion (findings 2, 4).
- Move the apply step into a pure model function the tests can call (finding 3, same as #104's deferred helper).

## Declined to judge

- The `.gitignore` kiln line and `.kiln/`: kiln setup, not this change.
- Empty read-only dotfiles at the repo root: sandbox placeholders.
- e2e drag behaviour in the real app: not run in this session.
153 changes: 153 additions & 0 deletions .kiln/work/20261121-renumber-note-positions-when-repeated/state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{
"schema_version": 1,
"id": "20261121-renumber-note-positions-when-repeated",
"follows": "20261121-review-each-items-above-one-one",
"status": "shipped",
"session_id": "0b73138a-cf32-4ba7-ac23-30a61cd6d8d6",
"path": "bounded",
"type": "fix",
"auto": false,
"stage": "VERIFY",
"step": null,
"gates": {
"plan": {
"decision": "approved",
"artifact_sha": "3f491ac5f590c5c587f498d50e915f7c76c86c872008d297cade1e0adfa8ccff",
"answer": "ruled by auto mode, not by the user",
"by": "auto",
"at": "2026-11-21T07:01:10.322Z"
},
"review": {
"decision": "approved",
"artifact_sha": "9c96e2258f9925aee1d933e3080b7529d8ccffeaaa15a9e82398c819dc53267a",
"answer": "ruled by auto mode, not by the user",
"by": "auto",
"at": "2026-11-21T07:05:48.043Z"
}
},
"predicted": [
{
"path": "extension/model.js"
},
{
"path": "extension/app.js"
},
{
"path": "tests/model.test.js"
}
],
"base": "10da06c82d4f1920b2252dcdac2559cc86786f8f",
"last_verified": "10da06c82d4f1920b2252dcdac2559cc86786f8f",
"dirty_at_open": [
".gitignore"
],
"verify": [
{
"id": "unit",
"cmd": "node --test \"tests/*.test.js\"",
"exit": 0,
"ms": 3504,
"range": "10da06c82d4f1920b2252dcdac2559cc86786f8f..10da06c82d4f1920b2252dcdac2559cc86786f8f",
"phase": "full",
"tree": "dfd84b0caf6f2e8dc57092c59b0489487d5042ef"
},
{
"id": "unit",
"cmd": "node --test \"tests/*.test.js\"",
"exit": 0,
"ms": 5446,
"range": "10da06c82d4f1920b2252dcdac2559cc86786f8f..10da06c82d4f1920b2252dcdac2559cc86786f8f",
"phase": "full",
"tree": "c4d51def7ef55c7beff22570b1672ac2d9ece5b2"
},
{
"id": "unit",
"cmd": "node --test \"tests/*.test.js\"",
"exit": 0,
"ms": 5467,
"range": "10da06c82d4f1920b2252dcdac2559cc86786f8f..10da06c82d4f1920b2252dcdac2559cc86786f8f",
"phase": "full",
"tree": "d2dae68ef2e8d411999997bc664560e1e58f422f"
},
{
"id": "unit",
"cmd": "node --test \"tests/*.test.js\"",
"exit": 0,
"ms": 5436,
"range": "10da06c82d4f1920b2252dcdac2559cc86786f8f..10da06c82d4f1920b2252dcdac2559cc86786f8f",
"phase": "full",
"tree": "d2dae68ef2e8d411999997bc664560e1e58f422f"
}
],
"pass": 1,
"carry_over": [],
"displaced": [],
"rules": [
{
"stage": "plan",
"files": []
},
{
"stage": "review",
"files": []
}
],
"knowledge": [],
"practices": [
{
"stage": "investigate",
"ids": []
},
{
"stage": "plan",
"ids": [
"planning-and-task-breakdown"
]
},
{
"stage": "implement",
"ids": [
"incremental-implementation",
"test-driven-development",
"source-driven-development"
]
},
{
"stage": "review",
"ids": [
"reviewer",
"blind-hunter",
"edge-case-hunter",
"verification-gap",
"intent-alignment",
"code-review-specialist",
"security-auditor",
"test-engineer"
]
}
],
"snapshots": [
{
"name": "task/0",
"commits": {
".": "b0f4cd00bd081291b372678a6b2322d6e89dd861"
},
"at": "2026-11-21T07:01:10.353Z"
},
{
"name": "task/1",
"commits": {
".": "eccd15004f3b33a015defde75724dc13727fe677"
},
"at": "2026-11-21T07:02:07.424Z"
}
],
"baseline": null,
"checklist": [],
"opened": [
"https://github.com/ttncode/notelet/pull/105"
],
"branches": {
".": "fix/20261121-renumber-note-positions-when-repeated"
}
}
12 changes: 8 additions & 4 deletions extension/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,10 +704,14 @@ const liveNotes = () => [...notes.values()].filter((note) => note.deletedAt ===

function applyPlacement(id, placement) {
if (!placement) return;
const updated = { ...notes.get(id), ...placement, updatedAt: Date.now() };
saves.cancel(id);
notes.set(id, updated);
persist([updated]).catch(reportSaveError);
const { renumbered = [], ...moved } = placement;
const updated = { ...notes.get(id), ...moved, updatedAt: Date.now() };
const spacedOut = renumbered.map(({ id: otherId, position }) => ({ ...notes.get(otherId), position }));
[updated, ...spacedOut].forEach((note) => {
saves.cancel(note.id);
notes.set(note.id, note);
});
persist([updated, ...spacedOut]).catch(reportSaveError);
renderList();
}

Expand Down
18 changes: 17 additions & 1 deletion extension/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,23 @@ export function placeNote(notes, { id, targetId, placement }) {
const index = section.indexOf(target);
const above = placement === "before" ? section[index - 1] : target;
const below = placement === "before" ? target : section[index + 1];
return { position: positionBetween(above, below), pinned: target.pinned };
const position = positionBetween(above, below);
if (fitsBetween(position, { above, below })) return { position, pinned: target.pinned };
return spacedOut(section, { id, at: placement === "before" ? index : index + 1, pinned: target.pinned });
}

const fitsBetween = (position, { above, below }) => (!above || position < notePosition(above)) && (!below || position > notePosition(below));

// Positions start as millisecond timestamps, so about 23 drops into a 1000 gap use up its
// precision and the midpoint lands on a neighbour; the section is then spaced out again in its new
// order. The spacing hangs down from the section's top note, so its positions stay on the
// timestamp scale the other section, new notes and restored notes use.
function spacedOut(section, { id, at, pinned }) {
const order = [...section.slice(0, at), { id }, ...section.slice(at)].map((entry) => entry.id);
const top = notePosition(section[0]) + (at === 0 ? POSITION_STEP : 0);
const positionAt = (index) => top - index * POSITION_STEP;
const renumbered = order.flatMap((noteId, index) => (noteId === id ? [] : [{ id: noteId, position: positionAt(index) }]));
return { position: positionAt(at), pinned, renumbered };
}

export function stepNote(notes, { id, step }) {
Expand Down
Loading
Loading