Skip to content

Split task and alert updates into one ALTER per clause - #88

Merged
noel merged 4 commits into
datacoves:mainfrom
GClunies:fix/task-update-multiple-clauses
Nov 12, 2026
Merged

noel merged 4 commits into
datacoves:mainfrom
GClunies:fix/task-update-multiple-clauses

Conversation

@GClunies

@GClunies GClunies commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

What and why

A task or alert update can now change several fields in one apply. Before, update_task and update_alert raised NotImplementedError when state, as_, or when came with another field, so a common edit failed the whole plan:

update_task cannot combine ['as_'] with other fields in one ALTER for
urn::...:task/DB.SCHEMA.MY_TASK; got delta keys ['as_', 'comment']

Snowflake rejects these clauses in one statement (MODIFY AS SELECT 3 SET COMMENT = 'x' is syntax error 001003). Both functions now return one ALTER per clause, built on _update_with_separate_clauses:

Order Field Task statement Alert statement
1 state to SUSPENDED SUSPEND SUSPEND
2 clause fields MODIFY AS, MODIFY WHEN or REMOVE WHEN MODIFY CONDITION EXISTS (...), MODIFY ACTION
3 every other field update__default (SET, UNSET) update__default
4 state to STARTED RESUME RESUME

A suspend runs first and a resume runs last, so the object never runs with part of its new definition. Alerts need this order: Snowflake refuses every alert change while the alert is started (error 392005).

The delta must not half-apply, so these cases raise before any SQL renders:

  • after on a task. ALTER TASK has no SET AFTER.
  • An empty as_. A bare MODIFY AS is a syntax error. An empty when renders REMOVE WHEN.
  • name or owner with other fields. The error names the fields ("cannot combine 'name'" or "cannot combine 'owner'").

Before, alert condition and then rendered as SET IF(EXISTS(...)) and SET THEN .... Snowflake rejects both.

Validation

All on head commit 3963911, which has main merged in.

  • TestUpdateTask and the alert tests in tests/test_lifecycle.py assert the exact ordered statements and each refusal. The 13 new cases failed before the fix. Full suite: 2333 passed, 267 skipped, 1 xfailed. make lint and make typecheck are clean.
  • Live Blueprint plan and apply on the snowcap test account. Each re-plan showed 0 changes:
Step Task SQL Alert SQL
Body, suspended to started MODIFY AS, RESUME none
Body + comment, suspended to started SET COMMENT MODIFY CONDITION, MODIFY ACTION, SET COMMENT, RESUME
Body + comment, started to suspended SUSPEND, MODIFY AS, SET COMMENT SUSPEND, MODIFY CONDITION, MODIFY ACTION, SET COMMENT
Body + after refused, body stayed SELECT 3 none

How to review

Start at _update_with_state_change in snowcap/lifecycle.py, then update_task and update_alert below it.

Feedback wanted: is SUSPEND first, RESUME last the right order for a task in a DAG?

Out of scope, each predates this PR:

  • A started alert with no state change still fails on any other change (error 392005). A fix must read the current state from after and wrap the change in SUSPEND and RESUME.
  • CREATE TASK for a task declared state: STARTED leaves it suspended until the next apply.
  • A task after change still has no update path (see the FIXME above update_task).

update_task raised NotImplementedError when a delta combined as_, when,
or state with any other field, so editing a task body and its comment
in one apply failed. Snowflake rejects these clauses in one ALTER
(001003), but accepts them as separate ALTERs on a started task.

update_task now returns one ALTER per clause: MODIFY AS, MODIFY/REMOVE
WHEN, the remaining fields via update__default, then RESUME/SUSPEND
last so a resumed task starts with its new definition.
@noel

noel commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Nice refactor, replacing the NotImplementedError guard with real multi-clause support is a clear improvement, and the "state last" ordering is the right call so a resumed task starts with its new definition.

One latent ordering bug: when a delta combines name (rename) with state, the emitted RESUME/SUSPEND statement is still built from urn.fqn, the pre-rename name, after the RENAME TO statement (from update__default) has already run against that name. Verified directly:

>>> update_task(urn, {"name": "NEW_TASK", "state": "STARTED"}, props)
['ALTER TASK DB.SCHEMA.MY_TASK RENAME TO NEW_TASK', 'ALTER TASK DB.SCHEMA.MY_TASK RESUME']

The second statement targets a task name that no longer exists once the first one runs.

This looks currently unreachable through the normal plan/apply path, since Blueprint._raise_for_nonconforming_plan (blueprint.py:1527) rejects any delta containing name before a plan is produced. But update_task itself has no such guard, so any other caller (direct API use, a future relaxation of that restriction, or a test) would hit broken SQL. Might be worth a defensive check here too, or at least a comment noting the dependency on that upstream guard.

Ponytail-wise this diff is lean already, nothing to cut.

update_task sent a rename to update__default alone and then appended
RESUME/SUSPEND against the pre-rename name, so a delta with both name and
state rendered a second statement for a task that no longer exists. Main
refused that delta. Route any delta with name to update__default whole,
which already refuses to combine RENAME TO with other fields.

plan() rejects renames, but apply(session, plan=...) only warns on a
supplied plan, so this path is reachable.
@GClunies

GClunies commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @noel, good catch. The rename + state ordering bug was real.

Fixed in f616f5f. update_task now sends any delta with name to update__default whole. update__default already refuses to combine RENAME TO with other fields, so a rename can no longer be followed by an ALTER against the old name. This restores the behavior on main, where update_task also refused these deltas.

>>> update_task(urn, {"name": "NEW_TASK", "state": "STARTED"}, props)
NotImplementedError: update__default cannot combine 'name' (RENAME TO) with other fields ...

The path is reachable, not only latent. plan() rejects renames (blueprint.py:2200), but apply(session, plan=...) only calls _warning_for_nonconforming_plan on a supplied plan (blueprint.py:2324), so a prebuilt plan can reach update_task with a rename.

Proof: test_rename_combined_with_other_fields_raises covers name with state, as_, and when. All 3 cases fail without the fix. Full suite, black, ruff, codespell, and mypy pass.

@noel

noel commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Re-reviewed the latest commit (f616f5f). The rename+state ordering bug from my last comment is fixed, any delta containing name now routes whole through update__default, which refuses to combine it with other fields (nice, and covered by the new test_rename_combined_with_other_fields_raises).

A few more things found this round:

  1. Most severe: update_alert (untouched by this PR) still raises NotImplementedError when state combines with other fields, the same Snowflake constraint this PR just fixed for tasks. Your own new comment in update_task even draws the parallel ("Alerts, like tasks, reach STARTED via ALTER ... RESUME"). Might be worth generalizing the fix to update_alert too while this pattern is fresh.

  2. Confirmed, verified directly: combining owner with as_/when/state now falls through to update__default's bare raise NotImplementedError with no message:

    >>> update_task(urn, {"owner": "SOME_ROLE", "as_": "SELECT 1"}, props)
    NotImplementedError('')

    The old code gave a descriptive error naming the conflicting keys, this is a diagnostics regression for an admittedly rare case.

  3. Confirmed, verified directly: as_=None (a legal value, Task.as_: Optional[str] = None) produces malformed SQL: ALTER TASK <fqn> MODIFY AS with nothing after AS. Same gap would apply to when="".

  4. Untested: combining two specials with no other fields (e.g. {"as_": ..., "when": ...}) now silently succeeds (two ALTER statements) where it used to raise. Looks correct on inspection, but no regression test covers it.

Ponytail/complexity pass: lean overall, one note. commands[0] if len(commands) == 1 else commands is now duplicated between update__default and this new update_task, and _as_command_list in blueprint.py:2704 does the same str-or-list normalization independently. Not a clean one-line dedupe today since blueprint.py imports lifecycle.py (reverse import would be circular), so it'd need a shared helper moved to something like builder.py. Not blocking, just a maintenance note, this is exactly the kind of duplication that let the update_alert inconsistency above happen.

@noel

noel commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Re-reviewed the branch in a worktree. The rename plus state ordering fix from my last pass still holds, and the suite is green (2231 passed, 264 skipped, 1 xfailed). Two findings I missed last time, both in update_task.

  1. lifecycle.py:572, dropping the "cannot combine" guard lets after through, and the apply now half-lands. A delta containing after reaches update__default, which emits the invalid ALTER TASK ... SET AFTER .... Paired with as_, the MODIFY AS commits first, so the task's body is changed and then the second statement fails. Verified on the branch:

    >>> update_task(urn, {"as_": "SELECT 1", "after": ["DB.SC.T1"]}, ...)
    ['... MODIFY AS SELECT 1', '... SET AFTER DB.SC.T1']

    Before this PR the NotImplementedError guard aborted at plan-compile time, which left nothing applied. A partial apply is the worse outcome. The FIXME above update_task already notes that after has no working update path, so the fix is probably to keep refusing after specifically rather than to reinstate the whole guard.

  2. lifecycle.py:576, state is unconditionally emitted last. That's correct for RESUME and wrong for a STARTED to SUSPENDED change: the new body is installed while the task is still running, so a frequently scheduled task can fire the new statement in the window before SUSPEND lands. SUSPEND first, RESUME last.

Checked and cleared: _as_command_list (blueprint.py:2704) already normalizes a list return, so no caller changes are needed. Single-clause deltas still return a bare string, and the pre-existing test_modify_* / test_resume / test_suspend tests pass unchanged. The name early return correctly reuses update__default's RENAME guard, so {"name": ...} alone still renames and name plus anything else still raises "cannot combine 'name'". owner plus a special field still raises. An empty delta still yields [].

@noel

noel commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

@GClunies checking in, quiet since my Oct 19 re-review.

The rename plus state-ordering fix from the earlier round still holds. What's open is finding 1, dropping the "cannot combine" guard lets after through and the apply half-lands, changing the task body and then failing on SET AFTER. A partial apply is worse than the plan-time abort it replaced, so that's the blocker. Finding 2, SUSPEND first and RESUME last, is a small follow-on in the same function.

Are you planning to pick these up?

GClunies and others added 2 commits November 10, 2026 18:00
Address review on datacoves#88:

- Refuse a task delta with `after` before any SQL renders. ALTER TASK has
  no SET AFTER, so the other statements applied and then the apply failed.
- Run SUSPEND first and RESUME last, so a task or alert never runs with
  part of its new definition.
- Refuse an empty task body. A bare MODIFY AS is a syntax error. An empty
  WHEN renders REMOVE WHEN.
- Send an owner change with other fields to update__default whole, so the
  error names the fields ("cannot combine 'owner'").
- Apply the same state order to alerts. Render condition and action as
  MODIFY CONDITION and MODIFY ACTION. Snowflake rejects SET IF(...) and
  SET THEN, and refuses any alert change while the alert is started.
- Build update_task and update_alert on _update_with_separate_clauses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011dXsGJUx4ixC1Ti5vXcDh3
@GClunies GClunies changed the title fix(task): apply a body change together with other fields Split task and alert updates into one ALTER per clause Nov 11, 2026
@GClunies

Copy link
Copy Markdown
Contributor Author

Thanks @noel. 3963911 addresses both rounds (Oct 13 and Oct 19), and merges main in.

Oct 19

  1. after: fixed. update_task raises "cannot change 'after'" before it renders any SQL. Live on the test account, a plan with as_ plus after failed at compile time, and the task body stayed SELECT 3.
  2. State order: fixed. SUSPEND runs first and RESUME runs last.

Oct 13

  1. update_alert: done. It uses the same order through the new _update_with_state_change. A live probe showed that alerts need this order: Snowflake refuses every alert change while the alert is started (error 392005). The probe also showed that SET IF(EXISTS(...)) and SET THEN ... are syntax errors, so condition and then now render as MODIFY CONDITION EXISTS (...) and MODIFY ACTION.
  2. owner with a clause field: fixed. It goes to update__default whole and raises "cannot combine 'owner'".
  3. Empty as_: raises "cannot remove the body". An empty when renders REMOVE WHEN.
  4. Two clause fields: covered by a new case, {"as_", "when"}.
  5. Duplication: main now has _update_with_separate_clauses. update_task and update_alert are built on it, so the clause handling has one home.

Live Blueprint plan and apply, each re-plan with 0 changes:

Step Task SQL Alert SQL
Body, suspended to started MODIFY AS, RESUME none
Body + comment, suspended to started SET COMMENT MODIFY CONDITION, MODIFY ACTION, SET COMMENT, RESUME
Body + comment, started to suspended SUSPEND, MODIFY AS, SET COMMENT SUSPEND, MODIFY CONDITION, MODIFY ACTION, SET COMMENT

Unit suite: 2333 passed, 267 skipped, 1 xfailed. make lint and make typecheck are clean.

Not changed here: a started alert with no state change still fails on any other change (392005). It predates this PR. A fix must read the current state from after.

@noel
noel merged commit dc63fb6 into datacoves:main Nov 12, 2026
6 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.

2 participants