fix(frontend): skip reserved ids when auto-assigning flow module ids (#10651)

* fix(frontend): skip reserved ids when auto-assigning flow module ids

* test: state the reserved-id invariant only beside the implementation
This commit is contained in:
Ruben Fiszel
2026-08-12 08:41:30 +02:00
committed by GitHub
parent 45a6e4932a
commit 5f819cd344
2 changed files with 18 additions and 3 deletions
@@ -50,4 +50,9 @@ describe('nextId', () => {
const state = stateWith([...ids, 'process', 'my_step', 'failure'])
expect(nextId(state, flowWith(ids))).toBe('c')
})
// "as", the successor of "ar", is the first reserved id the sequence reaches.
it('skips a reserved id instead of assigning it', () => {
expect(nextId(stateWith(['failure']), flowWith(['ar']))).toBe('at')
})
})
@@ -25,13 +25,23 @@ function autoIdNumber(key: string): number | undefined {
// Computes the next available id
export function nextId(flowState: FlowState, fullFlow: OpenFlow): string {
const allIds = dfs(fullFlow.value.modules, (fm) => fm.id)
const allKeys = dfs(fullFlow.value.modules, (fm) => fm.id).concat(Object.keys(flowState))
const takenIds = new Set(allKeys)
const max = allIds.concat(Object.keys(flowState)).reduce((acc, key) => {
const max = allKeys.reduce((acc, key) => {
const num = autoIdNumber(key)
return num === undefined ? acc : Math.max(acc, num + 1)
}, 0)
return numberToChars(max)
// A reserved id (e.g. "as", after "ar") never raises `max`, so assigning
// numberToChars(max) when it is reserved would make every later call return
// it again; skip forward to a free, allowed id instead.
let candidate = max
let id = numberToChars(candidate)
while (reservedIds.has(id) || takenIds.has(id)) {
id = numberToChars(++candidate)
}
return id
}
// Computes a copy id like "a2", "a3", etc. based on the original id