-
Notifications
You must be signed in to change notification settings - Fork 673
refactor: extract entity ID allocation into idAllocation module #15001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,57 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { describe, expect, it } from 'vitest' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| createLGraphState, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mintGroupId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mintLinkId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mintNodeId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mintRerouteId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| observeGroupId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| observeLinkId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| observeNodeId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| observeRerouteId | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } from '@/lib/litegraph/src/idAllocation' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { toGroupId } from '@/types/groupId' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { toLinkId } from '@/types/linkId' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { toNodeId } from '@/types/nodeId' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { toRerouteId } from '@/types/rerouteId' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('idAllocation', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('mints increasing ids for each entity kind', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const state = createLGraphState() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect([mintNodeId(state), mintNodeId(state)]).toEqual(['1', '2']) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect([mintGroupId(state), mintGroupId(state)]).toEqual([1, 2]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect([mintLinkId(state), mintLinkId(state)]).toEqual([1, 2]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect([mintRerouteId(state), mintRerouteId(state)]).toEqual([1, 2]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('observes higher ids and ignores lower ids', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const state = createLGraphState() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| observeNodeId(state, toNodeId(4)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| observeNodeId(state, toNodeId(2)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| observeGroupId(state, toGroupId(5)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| observeGroupId(state, toGroupId(3)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| observeLinkId(state, toLinkId(6)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| observeLinkId(state, toLinkId(4)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| observeRerouteId(state, toRerouteId(7)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| observeRerouteId(state, toRerouteId(5)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(state).toEqual({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lastGroupId: 5, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lastNodeId: 4, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lastLinkId: 6, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lastRerouteId: 7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it('observes numeric-string node ids', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const state = createLGraphState() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| observeNodeId(state, toNodeId('12')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| observeNodeId(state, toNodeId('named')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(state.lastNodeId).toBe(12) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+41
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Assert the next allocated IDs after observation. The assertions read Proposed test update- expect(state).toEqual({
- lastGroupId: 5,
- lastNodeId: 4,
- lastLinkId: 6,
- lastRerouteId: 7
- })
+ expect(mintNodeId(state)).toBe('5')
+ expect(mintGroupId(state)).toBe(6)
+ expect(mintLinkId(state)).toBe(7)
+ expect(mintRerouteId(state)).toBe(8)
@@
- expect(state.lastNodeId).toBe(12)
+ expect(mintNodeId(state)).toBe('13')As per path instructions, “Review the allocation tests for behavioral coverage rather than implementation details.” 📝 Committable suggestion
Suggested change
🤖 Prompt for AI AgentsSource: Path instructions |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { toGroupId } from '@/types/groupId' | ||
| import type { GroupId } from '@/types/groupId' | ||
| import { toLinkId } from '@/types/linkId' | ||
| import type { LinkId } from '@/types/linkId' | ||
| import { toNodeId } from '@/types/nodeId' | ||
| import type { NodeId } from '@/types/nodeId' | ||
| import { toRerouteId } from '@/types/rerouteId' | ||
| import type { RerouteId } from '@/types/rerouteId' | ||
|
|
||
| export interface LGraphState { | ||
| /** Counter, not an id — brand at the point a group is constructed. */ | ||
| lastGroupId: number | ||
| lastNodeId: number | ||
| lastLinkId: LinkId | ||
| lastRerouteId: RerouteId | ||
| } | ||
|
|
||
| export function createLGraphState(): LGraphState { | ||
| return { | ||
| lastGroupId: 0, | ||
| lastNodeId: 0, | ||
| lastLinkId: toLinkId(0), | ||
| lastRerouteId: toRerouteId(0) | ||
| } | ||
| } | ||
|
|
||
| export function mintNodeId(state: LGraphState): NodeId { | ||
| return toNodeId(++state.lastNodeId) | ||
| } | ||
|
|
||
| export function mintGroupId(state: LGraphState): GroupId { | ||
| return toGroupId(++state.lastGroupId) | ||
| } | ||
|
|
||
| export function mintLinkId(state: LGraphState): LinkId { | ||
| state.lastLinkId = toLinkId(Number(state.lastLinkId) + 1) | ||
| return state.lastLinkId | ||
| } | ||
|
|
||
| export function mintRerouteId(state: LGraphState): RerouteId { | ||
| state.lastRerouteId = toRerouteId(Number(state.lastRerouteId) + 1) | ||
| return state.lastRerouteId | ||
| } | ||
|
|
||
| export function observeNodeId(state: LGraphState, id: NodeId): void { | ||
| const numericId = Number(id) | ||
| if (Number.isInteger(numericId) && numericId > state.lastNodeId) { | ||
| state.lastNodeId = numericId | ||
| } | ||
| } | ||
|
|
||
| export function observeGroupId(state: LGraphState, id: GroupId): void { | ||
| if (id > state.lastGroupId) state.lastGroupId = id | ||
| } | ||
|
|
||
| export function observeLinkId(state: LGraphState, id: LinkId): void { | ||
| if (id > state.lastLinkId) state.lastLinkId = id | ||
| } | ||
|
|
||
| export function observeRerouteId(state: LGraphState, id: RerouteId): void { | ||
| if (id > state.lastRerouteId) state.lastRerouteId = id | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Observe IDs when registering existing links.
Import
observeLinkIdand call it in_addLink.configure()can load links whendata.stateis absent or stale. The nextmintLinkIdcan then reuse an existing key. At Line 1535,_links.set(link.id, link)replaces the previous link and corrupts graph topology.Proposed fix
import { createLGraphState, mintGroupId, mintNodeId, mintRerouteId, observeGroupId, + observeLinkId, observeNodeId, observeRerouteId } from './idAllocation' @@ _addLink(link: LLink): void { + observeLinkId(this.state, link.id) this._links.set(link.id, link) registerLinkTopology(this, link) }🤖 Prompt for AI Agents