Skip to content

fix(db): resolve a shared title to the to-do, not the repeating template - #163

Merged
ryanlewis merged 1 commit into
mainfrom
fix/exact-title-template
Sep 5, 2026
Merged

ryanlewis merged 1 commit into
mainfrom
fix/exact-title-template

Conversation

@ryanlewis

Copy link
Copy Markdown
Owner

What changed

Both title lookups in internal/db now sort repeating templates last, through a shared templatesLastOrder():

ORDER BY <recurrence column> IS NOT NULL ASC, t."index" ASC

GetTask's exact-title path gains that ordering ahead of its LIMIT 1, so the single row it returns is the generated to-do. FindTasksByTitle, which feeds the disambiguation picker, keeps returning both but offers the instance first.

Why

A repeating to-do exists twice in the database: the template carrying the recurrence rule, and the instance Things generated from it, sharing its title. The exact-title path took whichever row came first under LIMIT 1, so things complete "Water plants" could land on the template and hit the repeating-write refusal from #143 while a perfectly completable instance sat next to it.

I ordered rather than raised an AmbiguousTaskError, which was the other option on the table. Three reasons:

  • The template is never what a user means by "complete X" or "cancel X" — Things itself won't accept those writes, which is exactly why fix: refuse writes Things drops on repeating items, and verify status changes landed #143 refuses them. Offering it as a candidate asks the user to choose between an answer and a dead end.
  • Ambiguity is a breaking change for scripts. A title that resolves fine today would start demanding a UUID, and --json consumers would have to handle a new failure for a case that has an obvious right answer.
  • Ordering keeps the template reachable. Filtering templates out of the lookups would have been simpler still, but then things show "Pay rent" on a template with no live instance — a real state, between generated occurrences — would report "not found". With ordering it is only deprioritised, so it still resolves when nothing else matches.

To reach a template deliberately, use its UUID or the numeric index from things repeating. That is now documented in both README.md and internal/skill/SKILL.md.

SearchTasks is deliberately untouched: search is a listing, and a user searching for "water" should see both rows in index order rather than have the CLI express a preference.

How tested

make test (race) and make lint clean.

New tests in internal/db/tasks_test.go, seeding a template (recurrence rule, start=2, no startDate) and an instance (start=1, scheduled, no rule) with the same title — and giving the template the lower "index", so plain index order alone would still pick it:

  • TestGetTaskExactTitlePrefersInstance — the exact-title path returns the instance.
  • TestGetTaskExactTitleTemplateOnly — a template with no instance still resolves, proving this orders rather than filters.
  • TestFindTasksByTitleOrdersTemplatesLast — the LIKE path returns both, instance first.
  • TestGetTaskAmbiguousListsInstanceFirst — the candidates in AmbiguousTaskError carry the same order, so the picker offers the instance as option 1.
  • TestTitleLookupsWithoutRecurrenceColumn — with the column dropped the ordering expression is constant, and lookups fall back to index order instead of failing.

And TestRunShowPrefersGeneratedTodoOverTemplate in cmd/things, driving things --json show "Water plants" end to end.

I checked the tests are not vacuous by reverting the ordering to plain t."index" ASC — the three ordering tests failed, the template-only and no-column tests stayed green.

Closes #156

A repeating to-do is stored twice: the template carrying the recurrence
rule, and the instance Things generated from it, sharing its title.
GetTask's exact-title path took the first row with LIMIT 1, so
`things complete "Water plants"` could land on the template and hit the
repeating-write refusal from #143 while an actionable instance sat next
to it.

Both title lookups now order templates last, via templatesLastOrder:
"<recurrence column> IS NOT NULL ASC, t.index ASC". The exact-title path
gets a deterministic single result; FindTasksByTitle, which feeds the
disambiguation picker, offers the instance first.

Ordering rather than filtering keeps a template reachable when it is the
only match, so `things show` on a template-only title still works. On a
schema with no recurrence column the expression is constant and the index
order is unchanged.

Closes #156
@ryanlewis
ryanlewis merged commit f8a28ef into main Sep 5, 2026
7 checks passed
@ryanlewis
ryanlewis deleted the fix/exact-title-template branch September 5, 2026 10:45
ryanlewis added a commit that referenced this pull request Sep 5, 2026
…oting (#168)

## What changed

Six small fixes to code already on main, all found by review passes on
#162 and #163.

**`--json` parse failures (#157).**

- `renderError` used a default `json.Encoder`, which escapes `<`, `>`
and `&` into `\u00xx`. Every kong parse error carries them, so the most
common `--json` failure came out as `expected \"<task>\"` — line noise,
in the field whose doc says it reads as the plain-text error does.
`SetEscapeHTML(false)`.
- `jsonRequested` recognised only bare `--json` and `--json=true|false`,
so `things --json=1 …` and `things --json=yes …` fell through to kong's
usage block on stdout: exactly what the function exists to prevent.
Values now go through `kongBool`, which mirrors kong's bool mapper —
`true/1/yes`, `false/0/no`, case-insensitive. `strconv.ParseBool` was
the wrong set in both directions: it accepts `T`, which kong rejects,
and rejects `yes`, which kong accepts.
- It also missed clustered shorts, so `things -jv --bogus` did the same
thing. It now walks a cluster and stops at the first short that takes a
value, because that one swallows the rest of the cluster as its value —
`-pj` is `--project=j`, not `--json`.
- The JSON branch exited 1 where kong's plain-text path exits 80, so the
same failure returned a different code depending on the flag.
`parseExitCode` now reads kong's own `ExitCoder`, which also gets the
cases right where kong raises a parse error with no code of its own and
exits 1 rather than 80 — this CLI installs a config resolver, so that
path is reachable.
- `SKILL.md` and the `jsonErrorPayload` doc both told agents to parse
stdout whether or not the command succeeded. The write commands (`add`,
`edit`, `complete`, `cancel`, `log`, `open`) print nothing on success,
so an agent following that tried to parse an empty string.

**Tag hint quoting (#150).**

`tagAddHint` builds a copy-pastable `things tag add …` suggestion, but
quoted a handful of characters with `strconv.Quote`, which is Go syntax
rather than shell. `things tag add R&D` backgrounds a command and runs
`D`; `a;b` leaves an unquoted `;`; `"a$HOME"` expands. It now uses POSIX
single-quoting driven by a character allow-list, so a character the list
forgot is merely quoted unnecessarily rather than left live. A name
starting with `-` also gets a `--` separator: quoting stops the shell
splitting it but not kong reading it as a flag.

## Why the two hand-maintained lists have tests that derive them

`boolShorts` and `kongBool` both restate something kong owns, which is
exactly the kind of thing that rots silently.

- `TestBoolShortsMatchesGrammar` walks the real kong model and fails if
a boolean short exists that `boolShorts` does not list, or if a letter
in `boolShorts` is value-taking anywhere. (`-f` is deliberately absent:
boolean on `config init`, a value flag on `import`. Treating an
ambiguous letter as value-taking only costs a fallback to kong's
plain-text usage; the reverse would misread a filename.)
- `TestKongBoolMatchesKong` runs each candidate value through an actual
`parser.Parse` and asserts `kongBool` agrees on both acceptance and
result.

## How tested

`make test` (race) and `make lint` clean.

New and extended tests: the `jsonRequested` table gains cluster cases
(`-jv`, `-vj`, `-pj`, `-fj`), `yes`/`no` casing, config-default
interaction, and values kong rejects;
`TestRenderErrorDoesNotEscapeHTML`; the two derivation tests above; and
`tags_test.go` cases for `&`, `$`, `'`, empty, and a leading dash.

Checked against the built binary: `things --json show` →
`{"error":"error","message":"expected \"<task>\""}` on stdout, exit 80,
empty stderr. `things -jv --bogus` and `things --json=1 --bogus`
likewise. `things -pj --bogus` correctly keeps kong's usage block, since
`-pj` is not a JSON request. `things --json=T lst` also keeps the plain
path, matching kong's rejection of `T`.

Closes #164
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: exact-title lookup can resolve to a repeating template instead of its instance

1 participant