Skip to content

Reject OR inside a quoted context definition as a write context - #4229

Merged
djmitche merged 2 commits into
GothenburgBitFactory:developfrom
Yurii201811:fix-3760-quoted-context-or
Nov 15, 2026
Merged

djmitche merged 2 commits into
GothenburgBitFactory:developfrom
Yurii201811:fix-3760-quoted-context-or

Conversation

@Yurii201811

@Yurii201811 Yurii201811 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Closes #3760

The bug

task context define <name> <filter> saves the filter twice. context.<name>.read is the filter used by reports. context.<name>.write holds default values for new tasks: while the context is active, task add splices that same text into its command line as if you had typed it. Only some filters make sense that way. +home or project:Home can become tags and attributes on a new task. +home or +work, a tag exclusion like -work, or a modifier like due.before:today cannot. Before saving the write value, defineContext looks for those three things, and if it finds one it prints a warning and saves the context as read only.

That check worked when the filter was typed as separate words, but not when the same filter was quoted, as in the issue:

task context define mine "+personal or +home or +work"
# before: Context 'mine' defined (read, write).
task context mine
task add this is a test
# before: description "or or this is a test", tags home personal work

Without the quotes the same command already warned and saved the context as read only.

Why it happened

Taskwarrior splits the command line in two different ways.

  1. When it parses the command you type, it looks at each shell word on its own (CLI2::lexArguments). A word that holds a single token, like or or +home, gets its type (operator, tag, attribute). A word that holds several tokens, like the quoted +personal or +home or +work, is kept whole as one plain word. The one exception is a quoted word that starts with an attribute such as due.before:today, which keeps that type. That is why quoted "due.before:today +next" was already read only, while the quoted or filter, which starts with a tag, was not. Every argument after the context command, including define and the context name, is tagged "miscellaneous", which just means "not a filter and not a modification, the command reads these words itself".
  2. When a saved context is applied later (CLI2::addFilter for read, CLI2::addModifications for write), the saved string is run through the lexer as one string, so every token is seen, including the or inside the quotes.

The old check used the miscellaneous arguments from step 1. For a quoted filter it saw one plain word and no or, so it allowed the write context. When task add used it, step 2 split the same text and the or tokens ended up in the description.

What this change does

In defineContext (src/commands/CmdContext.cpp), the write check now lexes the exact string that will be saved, with the same Lexer loop that CLI2::addModifications runs when the write context is applied. So to your question, yes: the check now works on the same series of tokens that task add will later get from the saved text, instead of on the shell words. The rules in validateWriteContext are unchanged: reject or, tag exclusions, and modifiers other than is and equals.

What does not change

  • The lexer and the command-line parser are not modified. CLI2::lexArguments, addFilter and addModifications are untouched, so filtering with a context and applying an existing write context behave as before.
  • The saved strings are the same. The read context is always saved, as before. Only the decision to also save .write can change.
  • No other command is affected. The new loop runs only in task context define, and validateWriteContext has no other caller. After this change CLI2::getMiscellaneous has no callers left. I left it in place.
  • Write contexts that were already saved in a .taskrc are not re-checked or removed.

What can change for users

A definition is now saved as read only when its quoted text contains or, a tag exclusion or a disallowed modifier. In every such case the unquoted form of the same filter was already read only, and keeping the write value would have put that text into new tasks. One case I checked besides the issue: quoted "due:today -work" used to save a write context and is now read only, like due:today -work without quotes. The other change goes the opposite way. The old check also looked at the context name, so task context define or +home used to be read only. Now only the definition is checked, so that command saves read and write, and task add in that context applies +home.

Checked by hand on Debug builds of develop (c564bc3f) and this branch, unchanged before and after: quoted "project:Work +urgent", unquoted project:Home, quoted "+personal" and quoted "+personal and +home" stay read and write. Quoted "due.before:today +next" and quoted "-work" stay read only. xor is still allowed in a write context, because validateWriteContext only rejects or. That is the existing rule and this PR does not change it.

How it was tested

  • New test test_context_define_quoted_or_is_not_a_write_context in test/context.test.py. It failed before the change (Context 'mine' defined (read, write).) and passes after. It also checks that task add this is a test keeps the description as typed.
  • ctest --test-dir build -R '^context\.test\.py$' --output-on-failure passes, including the existing unquoted or, tag exclusion and modifier tests.
  • Full ctest -j 8 on a clean Debug build of this branch: 179 of 179 passed. On the first parallel run view_test.cpp failed once and then passed on every rerun, alone and in parallel. It does not touch context code.
  • GitHub CI on this PR is green.

Prepared with Cursor assistance.

A quoted filter was one command-line word, so context define stored it as
a write context and later prepended "or" onto new task descriptions.

Co-authored-by: Cursor <cursoragent@cursor.com>

@djmitche djmitche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command-line lexer and parser are pretty complex and nobody working on the project understands them at this point. So, I'm a little worried that this fix breaks something else which people might be relying on.

Please rewrite the PR description to include more background and less of the jargony short sentences. This is a better place to describe what "miscellaneous" is and how a different kind of lexing works differently.

As I understand it, the underlying issue is that the definition given to task context define is used, as a series of tokens, in different contexts and can mean different things in those contexts. Can the fix operate at that level, too?

Comment thread src/commands/CmdContext.cpp Outdated
std::vector<A2> lexedArgs = Context::getContext().cli2.getMiscellaneous();
// Lex the definition that will be stored. A quoted filter arrives as one
// command-line word, so getMiscellaneous() hides operators inside it, but
// those operators are applied when the write context is used.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a confusing AIism where the comment says what it doesn't do -- in this case, it describes getMiscellaenous which is not called here.

The comment now says the definition is lexed before the write-context check.

Co-authored-by: Cursor <cursoragent@cursor.com>

@djmitche djmitche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better - thank you!

@djmitche
djmitche merged commit 8eec2c3 into GothenburgBitFactory:develop Nov 15, 2026
16 checks passed
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.

Define a context with an invalid modification string

2 participants