Skip to content

Couple a data generator's data source to the organisation it runs for - #2634

Merged
Flix6x merged 9 commits into
mainfrom
feat/2625-source-organisation
Oct 3, 2026
Merged

Flix6x merged 9 commits into
mainfrom
feat/2625-source-organisation

Conversation

@Flix6x

@Flix6x Flix6x commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

Closes #2625. Closes #2630.

A data source carries an organisation, but until now only sources of type user ever got one, because nothing on the generator side passed one to get_or_create_source. Two consequences, both from the issue: two organisations running the same generator under the same configuration shared one source row, and a source-account filter — which selects sources linked to an organisation — matched user-recorded data only, which is the opposite of what a multi-tenant filter is for.

What this does

The organisation joins what identifies a source, including when there is none: asking for a source without naming an organisation now looks for one belonging to none, which is what the host's own generators record under, rather than matching whichever source happens to fit otherwise.

A data generator knows which organisation it computes for. set_source_account says it outright, which the automation paths use, since at creation time the parameters are not on the generator yet. Failing that, a generator goes by the sensors it writes to — deliberately the same rule the migration applies to the sources that already exist, so a new source and a coupled one agree instead of two rules drifting apart. A public asset has no organisation, and writing to one does not make a generator the host's, so public outputs are passed over rather than counted as a second answer. Output sensors of several organisations leave the source belonging to none and say so in the log, since that is the one way newly computed data can still land unowned; refusing there would fail a schedule or a report over a question of provenance, which seemed the worse trade. A scheduler is always handed an asset or a sensor, so it needs no telling.

The sources that already exist are coupled on upgrade. Which organisation a source recorded for is readable from its data: a belief's sensor belongs to an asset, and that asset names the organisation. Annotations say the same, and so does the asset an automation hangs off — which is the only signal a source whose automation has not run yet can offer. Those signals are unioned rather than ranked, because two of them disagreeing is what a shared source looks like, and that belongs in front of a host rather than being resolved by precedence.

  • one organisation → the column is filled in place, keeping the id that automations and source filters refer to;
  • none → the source keeps none, which is what a generator the host runs for everyone looks like;
  • several → the upgrade stops and names the sources and the organisations sharing them.

A source that looks shared is asked a second time, with each claim checked against data that still exists. sensor_data_source is a summary that outlives the beliefs that made it, so a source which wrote to another organisation's sensor years ago would otherwise still look shared with it, stopping an upgrade over data that is gone. A source whose every claim turns out to be stale is left alone rather than split into sources that would receive nothing.

Splitting is the same command with a flag, and because a source id can be referred to from configurations the upgrade cannot read, the host can overrule the default choice:

flexmeasures db upgrade -x split-shared-sources=true -x keep-source=42:3

A split keeps the id for the organisation which recorded under it most recently — by belief time, not by the event a belief is about, since a generator mostly records about the future and the furthest event start would rank by forecast horizon instead. It moves the other organisations' beliefs, annotations, sensor links and automations onto new sources of their own, and prints every decision. Every statement against timed_belief names the sensors involved, so it rides that table's primary key, which leads with sensor_id, rather than scanning what is usually the largest table in the database.

The downgrade leaves the couplings in place. The previous release reads a source's organisation only when one is passed, so a coupled source costs it nothing. Clearing the column, on the other hand, cannot tell this migration's couplings from the ones that were always there — flexmeasures add source --account sets one on the host's say-so, and a copied automation's generator has had one since #2531 — so it would lose data rather than roll anything back, and would leave a split source and its siblings identical but for an organisation now NULL on each, which is the duplicate-source state #2611 was about.

#2630 is folded in, because AutomationHandler.create was being touched anyway: an automation type registered by a plugin takes that branch, which has its own path for a named source and never reached the check added in #2584.

Upgrade order matters, and the changelog says so

Run the migration before deploying the code, with workers stopped. New code against an unmigrated database creates a second source beside every generator's; a worker still on the previous version after the migration passes no organisation, misses the row just filled, and leaves an unowned sibling that #2613's merge cannot clean up, because the two rows differ in account_id. The migration closes by printing how many sources belong to no organisation, so a host who sees that number grow afterwards knows one was still writing.

What is deliberately not here

  • No self-healing of unowned rows at runtime. Tempting, but the code cannot tell an unambiguous legacy row from one two organisations share, so whoever ran first would silently claim it. That distinction is what the migration exists to make.
  • Ranking a split by "still named in a source filter". A pinned id can sit in generic_asset.flex_context, flex_model and attributes, in automation.parameters, and in another source's own attributes, in shapes that differ per field. The ranking decides only which organisation keeps the id — every other one gets a new source either way — so the scan would change who wins, not whether references break. -x keep-source= lets a host state the answer instead.
  • A visibility flag. Publishing a generator should not be done by clearing its organisation (Couple a data generator's data source to the organisation it runs for #2625 lists four reasons); carrying visibility in its own column is a separate change.
  • A standing diagnostic listing generator sources with no organisation. The migration's closing count covers the moment a host is watching; a command to ask later belongs on flexmeasures jobs check-source-references, which arrives with Refuse duplicate data sources, merging existing ones and their references #2613.

Verification

CI on this branch runs the whole suite, green on all three: 2855 passed and 4 xfailed on Python 3.11 and 3.12, and 2854 on 3.10, which deselects one test more. The Docker build runs flexmeasures db upgrade on an empty database, so the migration is executed, not only unit-tested.

Eleven migration tests run the migration body against a real database: fill in place; public assets leaving a source unowned; an automation coupling a source that has not run yet; the refusal naming sources and organisations; a split moving beliefs, sensor links, annotations and automations while the id stays put; the host override; a named organisation that never recorded under the source being refused; the -x parsing; recency being belief time rather than event start; a sensor link left behind by deleted beliefs not stopping an upgrade; and a source whose every claim is stale being left alone. Four more cover the runtime side and the plugin path.

Each was confirmed red without the change it covers — all eleven fail if the migration no-ops, and the automation signal, the belief move, the belief-time ranking, the staleness check, the stale-husk rule, the lookup key, the public-sensor rule and the plugin refusal were each falsified on their own.

One of those tests was rewritten because it was not pinning what its name said: the original ranking test used zero-horizon beliefs, so belief time and event start coincided and it passed with the bug and without it. test_splitting_moves_what_the_other_organisation_recorded_and_keeps_the_id has the same property and was renamed rather than changed, since what it pins is which rows move; the ranking rule is pinned next to it.

🤖 Generated with Claude Code

https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC

Flix6x and others added 4 commits October 1, 2026 14:54
…tes for

A data source carries an organisation, but only sources of type user ever got one,
because nothing on the generator side passed one to `get_or_create_source`.
Two organisations running the same generator under the same configuration therefore shared one source row,
and a `source-account` filter, which selects sources linked to an organisation, matched user-recorded data only.

The organisation is now part of what identifies a source, including when there is none:
asking for a source without naming an organisation looks for one belonging to none, which is what the host's own generators record under.

A data generator says which organisation it computes for through `set_source_account`,
and otherwise goes by the sensors it writes to, which is the same rule the migration derives for the sources that already exist.
A public asset has no organisation, and writing to one does not make a generator the host's, so public outputs are passed over rather than counted.
Sensors of several organisations leave the source belonging to none, which is logged, since that is the one way new data can still land unowned.
A scheduler is always handed an asset or a sensor, so it knows this without being told.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…ecorded for

Which organisation a source recorded for can be read off its data: a belief's sensor belongs to an asset, and that asset names the organisation.
Annotations say the same, and so does the asset an automation hangs off, which is what a source whose automation has not run yet has to go by.
Those signals are unioned rather than ranked, because two of them disagreeing is what a shared source looks like,
and that is a case to put in front of a host rather than to resolve by precedence.

A source with one organisation among them has its column filled in place, keeping the id that automations and source filters refer to.
A source with none keeps none, which is what a generator the host runs for everyone looks like.
A source shared by several is not guessed at: the upgrade stops and names the sources and the organisations sharing them.
Splitting them is the same command with a flag, and the host can say which organisation keeps a source's id where freshness is the wrong answer:

    flexmeasures db upgrade -x split-shared-sources=true -x keep-source=42:3

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>
… automation types too

The check added with the data source picker sits in `_create_builtin_automation`, which an automation type registered by a plugin never reaches:
it takes the `AutomationHandler.create` branch, which has its own path for a named source and only ever checked the sensors.
A user could therefore name a source of another organisation on an automation of a plugin type, and record under it.
Setting up the generator from a named source moved into a method of its own, which is also what keeps that branch within the complexity the linter allows.

Closes #2630.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…rds for

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>

@Flix6x Flix6x left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Reviewed as asked, and ran the suite: the eight migration tests plus test_data_source.py pass here (28), against a sandboxed Postgres. Both conditions I asked for are in and correct — public outputs are passed over rather than counted (DataGenerator.source_account), and an ambiguous derivation warns with the organisations named. The -x keep-source override, including its refusal of an organisation that never recorded under the source, is better than what I proposed, because it refuses a typo rather than silently picking freshness.

Three findings, the first of which I would not merge without.

1. The downgrade uncouples sources this migration never coupled, and can leave an instance unable to look a source up.

UPDATE data_source SET account_id = NULL WHERE user_id IS NULL AND type <> 'user' (line 381) clears the column on every non-user source, including those that carried an organisation before this migration ran:

  • flexmeasures add source --account sets one explicitly (cli/data_add.py), and nothing restores it afterwards — that is data loss, not a rollback.
  • Copied automations have had one since #2531 (automation_copies.py), set by code that survives the downgrade, so the downgrade silently undoes it.

The second half is worse. After a split, downgrading leaves two rows with the same name, model, version and attributes, both uncoupled. The previous release only filters on account when one is passed, and generators pass none, so its lookup matches both and scalar_one_or_none() raises MultipleResultsFound — the failure #2611 was about, reintroduced by rolling back.

What I would do instead: leave the couplings alone. The old code ignores the column unless an account is passed, so a filled account_id costs it nothing, and not touching it keeps the pre-existing couplings intact. That leaves only the split siblings, which do need a record to undo — a small bookkeeping table written by _split_source and consumed by downgrade is the exact version. Note that the record cannot live in attributes, since those feed attributes_hash and therefore the source's identity. If a reversible split is more than this PR should carry, refusing to downgrade while splits exist is honest; silently destroying couplings is not.

2. MAX(tb.event_start) ranks by forecast horizon, not by recency of use (line 114).

These are generator sources, so their beliefs are mostly about the future: a forecaster that last ran a month ago with a 7-day horizon outranks one that ran this morning with a 1-hour horizon. The quantity that answers "which organisation is still using this source" is the belief time, MAX(tb.event_start - tb.belief_horizon), both columns being on timed_belief. The ordering decides which organisation keeps an id that installations pin, so it is worth being the right quantity. (test_splitting_a_shared_source_keeps_its_id_with_the_freshest_organisation may need its fixture adjusted along with it.)

3. The sensor_data_source signal is a superset, so deleted data can block an upgrade (line 43).

A pair survives the beliefs being deleted — the model's own docstring says to read a row as "recorded here at some point". So a source that recorded on another organisation's sensor years ago, whose beliefs are long gone, still counts as a second organisation: the upgrade refuses over data that no longer exists, and -x split-shared-sources=true then creates a source for that organisation and moves nothing onto it.

Since this only matters for the handful of sources the union calls shared, confirming each claim is cheap: for those sources only, keep an organisation whose claim is backed by a belief that exists (a LIMIT 1 existence check), an annotation, or an automation. The signal union stays as it is; it is only the ambiguous set that gets a second look.

Two smaller things

  • -x keep-source=... is read only in the splitting branch, so passing it without split-shared-sources=true does nothing. The refusal message mentions both together, so this is unlikely to bite, but naming an unknown source id could be reported there too.
  • A source filled with one organisation that also wrote to public assets is the one case where filling could hide a host-run and a tenant-run sharing a row. Printing those few alongside the count would let a host notice; the count alone does not distinguish them.

On the parts you flagged

  • The annotation links through assets and sensors belong in the union, yes. They say the same thing a belief says, by the same route.
  • Ranking organisations with no beliefs last is right, and reads well with finding 3: a claim with no data behind it should not keep an id, and arguably should not force a split either.
  • The AutomationHandler.create extraction is behaviour-preserving as far as I can tell, and the C901 limit left no choice. Worth keeping the refusal message identical to the built-in path, which it is.

…e, check a claim still holds

Three things the review found in the migration.

The downgrade cleared the organisation from every source that names no user, which is more than it added:
`flexmeasures add source --account` sets one on the host's say-so, and a copied automation's generator has had one since #2531.
It would also leave a split source and its siblings identical but for an organisation now NULL on each, which is what #2611 was about.
The previous release reads the column only when an account is passed, so leaving the couplings in place costs it nothing.

Ranking a split by the furthest event start ranks by forecast horizon rather than by recency,
since a generator records beliefs about the future: a forecaster that last ran a month ago looking a week ahead
would outrank one that ran this morning looking an hour ahead. It now ranks by belief time.

A `sensor_data_source` pair is a summary which outlives the beliefs that made it,
so a source that wrote to another organisation's sensor years ago still looked shared with it long after that data was deleted,
stopping an upgrade over data that no longer exists. The sources which look shared now have each claim checked against what is still there.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>
@Flix6x

Flix6x commented Oct 1, 2026

Copy link
Copy Markdown
Member Author

All three findings accepted and fixed in aa64207. Each was right, and the first two were places where my own tests would have passed either way.

The downgrade no longer clears anything. It was clearing couplings this migration never made — flexmeasures add source --account sets one on the host's say-so, and a copied automation's generator has had one since #2531 — which is data loss rather than a rollback. It would also have left a split source and its siblings identical but for an organisation now NULL on each, which is exactly the duplicate state #2611 was about. Since the previous release reads the column only when an account is passed, leaving the couplings in place costs it nothing, so the downgrade is now a documented no-op that says why. The splits genuinely cannot be undone: the beliefs point at the new sources, and no record of which rows the migration created can live in attributes, which feed the identity hash.

Ranking now uses belief time, MAX(event_start - belief_horizon). Ranking by event start ranked by forecast horizon, so a forecaster that last ran a month ago looking 60 days ahead beat one that ran this morning. Worth noting my original test did not catch this, because both its beliefs had a zero horizon — belief time and event start coincided, so it passed with and without the bug. The new test uses a 60-day horizon against a 1-hour one, and is verified red under the old ranking.

A claim is now checked against data that still exists. sensor_data_source is a summary that outlives its beliefs, so a source that wrote to another organisation's sensor years ago still looked shared with it, which would stop an upgrade over data that is gone and then create a source for that organisation with nothing to move onto it. Only the sources the union calls shared are asked a second time, since a source naming one organisation is coupled to it either way. An automation or an annotation counts as a claim in itself; a belief is checked for with an existence query rather than taken from the summary.

Also renamed _freshest_belief_per_organisation to _most_recently_recording_organisations, and reworded the refusal message and the changelog, which both still said "freshest beliefs".

Twelve migration tests now, each verified red without the change it covers, plus the runtime and plugin ones: 49 passing across the three files.

🤖 Generated with Claude Code

https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC

@Flix6x Flix6x left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Re-reviewed aa64207. All three are addressed, and I checked rather than took them on trust: the ten migration tests pass here against a sandboxed Postgres, and putting MAX(tb.event_start) back makes test_recency_is_when_a_belief_was_recorded_not_what_it_is_about fail on its own, so the replacement test does bind.

The downgrade reads right, and for the reason that matters: the previous release only filters on the organisation when one is passed, so leaving the couplings costs it nothing, while clearing them could not tell this migration's work from what flexmeasures add source --account and #2531 had already set. Saying in the docstring that a split cannot be undone here is better than a downgrade that pretends otherwise.

One new finding, from the query the verification added.

_still_recorded_for asks timed_belief for a belief by source_id alone, and _most_recently_recording_organisations does the same. The primary key is (sensor_id, source_id, event_start, belief_horizon, cumulative_probability), so a filter that does not name a sensor cannot use it; the single-column indexes on event_start and sensor_id were dropped as redundant in b7e5a2c40f18, and nothing else leads with source_id. Both queries therefore scan what is typically the largest table in the database.

The cost falls exactly where the check is meant to help. An organisation that still records is found quickly, since EXISTS stops at the first row. An organisation whose claim is stale has no row at all, so proving its absence means reading the whole table — and that is the case this check exists to detect. On an installation with a few shared sources, that is several full scans during an upgrade, with the database locked into the migration.

_split_source already avoids this: it names the organisation's sensors so its UPDATE rides the primary key. The same _sensors_of(account_id) would do here — AND tb.sensor_id = ANY(:sensor_ids) in both queries, which is also a tighter statement of the question, since an organisation's claim is about its own sensors.

On the fallback you asked about. I would leave such a source alone rather than fall back to the unverified claims. If no organisation's claim survives verification, the source has no beliefs, no annotations and no automations anywhere: a husk whose only trace is the sensor_data_source summary. Falling back makes that husk block an upgrade, or be split into sources that receive nothing — which is the shape of the finding the verification fixes, surviving in the corner where there is least reason for it. Leaving it unowned is harmless now that the organisation is part of the lookup: nothing matches it, so a later run of that generator creates its own owned source and the husk simply sits there, visible in the unowned count at the end of the migration. "Records for nobody" also seems the more honest description of a source that records for nobody.

That said, this is a judgement call on a rare row, and you have thought about it more than I have; if you keep the fallback, the one thing I would add is that the refusal says the claims rest on data that is no longer there, so a host is not sent looking for beliefs they cannot find.

Nothing else from me. The rename and the reworded refusal and changelog read well, and the note about your original ranking test — that zero-horizon fixtures made belief time and event start coincide, so it passed either way — is worth keeping in mind for the rest of this migration's tests, since several of them build beliefs the same way.

…eave a source with no live claim alone

Both of the queries the claim check added asked `timed_belief` by source alone.
Its primary key leads with `sensor_id`, and the single-column indexes on `event_start` and `sensor_id` were dropped as redundant in b7e5a2c40f18,
so nothing on that table leads with `source_id` and both queries scanned it.
The cost landed where the check is meant to help: an organisation that still records is found at the first row,
while proving that a stale claim has no row at all reads the whole table.
Both now name the organisation's sensors, as `_split_source` already did.

A source whose every claim turns out to be stale is left alone, rather than falling back to the claims that did not hold up.
Such a source has no beliefs, no annotations and no automations anywhere: splitting it would make sources that receive nothing,
and refusing on it would stop an upgrade over data that is gone. It is left belonging to no organisation, and counted as such.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>
@Flix6x

Flix6x commented Oct 1, 2026

Copy link
Copy Markdown
Member Author

Fourth round, both fixed in 2df40ee.

The claim check was scanning timed_belief. Its primary key leads with sensor_id, and b7e5a2c40f18 dropped the single-column indexes on event_start and sensor_id as redundant, so nothing on that table leads with source_id — and the cost landed precisely where the check is meant to help, since an organisation that still records is found at the first row while proving a stale claim has no row at all reads everything. Both queries now name the organisation's sensors through _sensors_of, as _split_source already did. The ranking changed shape for it: one grouped query over the source became one per organisation, each riding the primary key. A source is shared by a handful of organisations at most, so that is a good trade.

A source whose every claim turns out to be stale is now left alone, rather than falling back to the claims that did not hold up. Such a source has no beliefs, no annotations and no automations anywhere, so splitting it would create sources that receive nothing, and refusing on it would stop an upgrade over data that is gone. It keeps no organisation and is counted in the closing total. Test added, verified red with the fallback restored.

Thirteen tests now. For the record on how one of these was found: the original ranking test used zero-horizon beliefs, so belief time and event start coincided and it passed with the bug and without it — the thing it was named for was the one thing it did not pin. I have been back through the module since; every test is verified red against its own change, and the only one that still cannot distinguish two readings is the split test, which uses zero-horizon beliefs but exists to pin which rows move rather than how the winner is chosen.

🤖 Generated with Claude Code

https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC

… cannot see

Its beliefs carry no horizon, so belief time and event start coincide and it would pass under either ranking.
What it does pin is the split mechanics: which rows move, and which organisation keeps the id.
Giving it horizons would make the old name true at the cost of pinning the ranking in two places,
where the trap is a future reader taking the name as coverage of a rule the test cannot see.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>
@Flix6x Flix6x added the Data label Oct 1, 2026
@Flix6x Flix6x self-assigned this Oct 1, 2026
@Flix6x Flix6x added this to the 1.1.0 milestone Oct 1, 2026
Comment thread documentation/changelog.rst Resolved
…point at it where it belongs

The docstring explained the derivation by saying the migration derives the same answer, which reads as a note about this pull request rather than about the code.
The rule stands on its own, so it is stated on its own, and the migration is named where the connection actually helps:
beside the one branch which leaves newly computed data on a source belonging to no organisation.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>
`refresh_data_source` looks a source up by name, type, model, version and attributes when it has no id of its own,
which is how a transient source, such as the one a dry run attributes beliefs to, is turned into a stored one.
Now that the organisation is part of what identifies a source, leaving it out of that lookup asks for a source belonging to none,
so a forecaster of an organisation would get a second, unowned source beside its own.

Found while reviewing #2645, which calls this from the forecasting service on the dry-run path.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>
@Flix6x

Flix6x commented Oct 3, 2026

Copy link
Copy Markdown
Member Author

Found a defect in this PR while reviewing #2645, and fixed it in 38402da.

refresh_data_source (flexmeasures/data/models/forecasting/utils.py, already on main) turns a detached source into a stored one. When the source has no id of its own it looks one up by name, type, model, version and attributes — and passed no organisation. That was right while the organisation was not part of a source's identity. With this PR it is, so the lookup asked for a source belonging to none, found nothing, and created an unowned source beside the organisation's own.

The path is narrow — it needs a source that was never flushed — but #2645 widens it: its dry-run work attributes beliefs to a transient source and calls exactly this function on the way to saving. A test covers it, verified red without the fix.

Worth noting how it was missed here: the full suite passes either way, because no existing test refreshes a detached source that belongs to an organisation. A green suite says nothing about a call site whose behaviour only changes under data the suite does not create — and this change alters the meaning of every get_or_create_source call that omits an account. I have re-checked the other such call sites: cli/data_add.py passes one where the user names it and otherwise creates genuinely host-owned sources, and automation_copies.py passes the destination organisation. This was the one that mattered.

🤖 Generated with Claude Code

https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC

@Flix6x
Flix6x merged commit 41e9cc0 into main Oct 3, 2026
13 checks passed
@Flix6x
Flix6x deleted the feat/2625-source-organisation branch October 3, 2026 17:32
Flix6x added a commit that referenced this pull request Oct 3, 2026
…s to organisations

#2634 added a migration with the same parent as this one, which would have left the database with two heads,
so this one now descends from it rather than from their shared parent.

The order is the right way round and is worth keeping: c5e1a7b94d20 fills in each source's organisation,
which the unique index here treats as part of a source's identity.
Two organisations running the same data generator under the same configuration are therefore left alone,
where before they would have looked like duplicates of one another and been merged into one source.

In `get_or_create_source`, both changes stand: the lookup applies the account criterion, and refuses rather than tolerates duplicates.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Flix6x added a commit that referenced this pull request Oct 3, 2026
#2634 added c5e1a7b94d20 on top of b63a02d5e184, so the revision this branch merges was no longer a head
and the database would have been left with two. The merge now names the current head instead.

One conflict needed a choice rather than both sides: this branch turns a sensor page into a 403 for a user of another organisation,
where main had grown assertions about which panels such a user is shown. A 403 answers that outright, so this branch's expectation stands.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>

@nhoening nhoening 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.

Sorry, this review did not come in on time.
This PR does seem to be made well.
I marked only minor things, okay with not merging them in and move on.

""")).fetchall()
per_source: dict[int, set[int]] = {}
for source_id, account_id in rows:
per_source.setdefault(source_id, set()).add(account_id)

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.

What if by some means account_id is null - an if-clause to avoid adding those can't hurt



def _organisations_per_source(connection) -> dict[int, set[int]]:
"""Which organisations each data source without one recorded for, by the evidence in the data."""

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.

Suggested change
"""Which organisations each data source without one recorded for, by the evidence in the data."""
"""Find organisations linked to each unowned data source, by the evidence in the data."""

return per_source


def _sensors_of(connection, account_id: int) -> list[int]:

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.

Suggested change
def _sensors_of(connection, account_id: int) -> list[int]:
def _sensors_of(connection, account_id: int) -> list[int]:
"""Return IDs of sensors owned by an organisation."""

Flix6x added a commit that referenced this pull request Oct 7, 2026
Since #2634 an organisation is part of what identifies a data source, and
`_persist_data_source_id` now resolves `refresh_data_source(self.forecast_source())`
rather than the `data_source` property, which passed `source_account` along.
Without the account, the lookup asks for a source belonging to nobody, does not
find the forecaster's own, and creates an unowned one beside it: every queued
forecast would record under a source that says it belongs to no organisation,
and a `source-account` filter would stop matching the forecasts it used to.

The test has to own its asset to see any of this: `setup_fresh_test_forecast_data`
builds assets without an organisation, so the existing forecasting tests pass
whether or not the account is carried.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>
Flix6x added a commit that referenced this pull request Oct 7, 2026
#2645)

* Refactor forecast cycle persistence into the forecasting service

Keep legacy pipeline and plugin entrypoints compatible while exposing computation-only predictions and cycles. Preserve per-cycle commits and use transient source attribution for synchronous dry runs.

Refs #2486. Validated with 94 forecasting tests and pre-commit hooks.

* docs/changelog: link forecast persistence entry to PR #2645

* Keep a forecaster's transient source with its organisation

Since #2634 an organisation is part of what identifies a data source, and
`_persist_data_source_id` now resolves `refresh_data_source(self.forecast_source())`
rather than the `data_source` property, which passed `source_account` along.
Without the account, the lookup asks for a source belonging to nobody, does not
find the forecaster's own, and creates an unowned one beside it: every queued
forecast would record under a source that says it belongs to no organisation,
and a `source-account` filter would stop matching the forecasts it used to.

The test has to own its asset to see any of this: `setup_fresh_test_forecast_data`
builds assets without an organisation, so the existing forecasting tests pass
whether or not the account is carried.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Say when a cycle saved no forecast at all

`save_forecast` returned on an empty frame before any logging, so a cycle that
computed nothing was indistinguishable in the log from a cycle that never ran.
The dry-run path already reports an empty forecast, through
`_log_forecast_dry_run`, so the saving path should not be the quiet one.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Docs: tell a plugin author what they need, not what the refactor did

The section had grown into a release note for this pull request: cycles,
ForecastCycleResult, transient source attribution and which entrypoints kept
their saving behaviour. None of that is a plugin author's concern, since
FlexMeasures runs the cycles for them, and the detail is in the docstrings of
the methods it describes.

What is left is the one thing they have to act on, that a forecaster of their
own saves its own data, and one rule for the built-in pipeline they may build
on: a `compute` method returns forecasts, a `run` method records them.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0129WrXeJ5gia2pctFH93BqC
Signed-off-by: F.N. Claessen <claessen@seita.nl>

---------

Signed-off-by: F.N. Claessen <claessen@seita.nl>
Co-authored-by: F.N. Claessen <claessen@seita.nl>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
@Flix6x Flix6x mentioned this pull request Nov 4, 2026
7 tasks done
Flix6x added a commit that referenced this pull request Nov 4, 2026
* Tidy up the v1.1.0 section of the main changelog

Group the entries under New features, Infrastructure / Support and Bugfixes by topic, using bold paragraph headings, so the section's table of contents stays as it was.
Merge entries describing one change from several PRs, split entries combining unrelated changes, and cut verbosity, keeping every upgrade instruction.
Correct two PR links whose text and target disagreed or that pointed at the wrong PR (#2585 to #2583, #2631 to #2634), and move the README entry out of "Automations, in detail".
Set the release month to November, leaving the day as a placeholder.

Part of #2772

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Tidy up the v1.1.0 section of the CLI changelog

Order the entries by the same topics as the main changelog, additions before changes within each topic.
Merge the two entries on flexmeasures jobs run-automations, split the entry on saving report outputs from the one on saving plugin forecasts, and give flexmeasures show account an entry of its own.
Set the release month to November, leaving the day as a placeholder.

Part of #2772

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Merge the duplicate entry on reusing a data source for an automation in API v3.0-40

Part of #2772

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Merge the unreleased API versions into v3.0-40, released with v1.1.0

v3.0-33 to v3.0-40, except the resolution change already released with v1.0.1, become the one API version v3.0-40, dated like the v1.1.0 release and grouped by the same topics as the main changelog.
Changes between versions that never shipped are folded into the end state, such as the automation cursor moving from UTC to the automation's own timezone.
The resolution change keeps the number it was released under, v3.0-33, as in the v1.0.1 release.

Part of #2772

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* State the CDN fallback in the changelog entry on serving the UI's libraries

Until a library's files are fetched, browsers load them from its CDN, checked against their hashes.

Part of #2772

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Number the v1.1.0 API version v3.0-34, right after the released v3.0-33

Part of #2772

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Address the review of the v1.1.0 changelog tidy-up

Splits the device-group aggregate entry, moves #2699 to "Automations, in detail", adds upgrade warnings on the new roles and the per-organisation rate limits, links the add-report warning, and clarifies or trims several entries.

Part of #2772

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Address the second part of the review of the v1.1.0 changelog tidy-up

Leads the plugin-forecaster fix with returning results, explains day-ahead prices as realized beliefs about the future, names the kinds of regressors, and stops implying that an input's belief_time keeps its name on purpose.

Part of #2772

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>

* Address the third part of the review of the v1.1.0 changelog tidy-up

Tells hosts, not organisations, to check rate limits before upgrading, and says who calls set_attribute.

Part of #2772

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>

---------

Signed-off-by: F.N. Claessen <claessen@seita.nl>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automations of plugin types skip the data source read check Couple a data generator's data source to the organisation it runs for

2 participants