Repository navigation
fix(atg): invalidate configurationValidationResult on ChargingStationEvents.updated - #1977
Conversation
SummaryThe following content is AI-generated and provides a summary of the pull request: PR Checklist
Does this PR introduce a breaking change?
Bug Fix: Invalidate
|
There was a problem hiding this comment.
The PR is well-structured and correctly addresses the stale-cache bug. The core production change in AutomaticTransactionGenerator.ts — subscribing to ChargingStationEvents.updated in the constructor and unsubscribing symmetrically in deleteInstance — is sound and the listener-leak invariant is properly tested. The main concern flagged is that wireEventEmitter is order-sensitive (must precede getDefinedATG) but this dependency is undocumented in the helper, creating a footgun for future test authors; this should be called out clearly in the JSDoc. The other observations are minor clarity issues rather than defects.
PR Bot Information
Version: 1.27.2
- Agent Instructions:
- Event Trigger:
pull_request.opened - Correlation ID:
34682ef0-7a63-11f1-9485-75cb67e07c6d - LLM:
anthropic--claude-4.6-sonnet - File Content Strategy: Full file content
590112c to
622c886
Compare
…Events.updated Subscribe to ChargingStationEvents.updated in AutomaticTransactionGenerator's constructor so in-flight configuration mutations that reach any charging station mutation emit cannot leave a stale memoized validation decision behind. stop() still clears the cache as a redundant safety net. deleteInstance unsubscribes the handler before removing the entry so the retired instance is not retained via the charging station's listener array across a getInstance/deleteInstance churn cycle. Fixes #1965.
622c886 to
6fd96d6
Compare
Closes #1965.
Step 0 audit trail — bug verified, not trusted
HEAD-at-inspection =
origin/main=c55793e9. Verified each claim against the codebase before touching code (per lesson from #1964 postmortem: issue premises can be stale). Line numbers below reference branch head6fd96d6a(post-diff), notmain(pre-diff), unless annotated.AutomaticTransactionGenerator.tsproduction-side line positions are stable across all force-pushes since590112c1.Confirmed in-flight ATG-config mutation path that bypasses
stop():ChargingStation.ts:1153-1164— thetemplateFileWatcherreload handler.When
this.automaticTransactionGenerator?.started === falseat reload time (L1154):stopAutomaticTransactionGenerator()is skipped → ATG cache not cleared.delete this.automaticTransactionGeneratorConfigurationunconditionally discards the CS-side cached config so the nextgetAutomaticTransactionGeneratorConfiguration()re-reads reloaded template bounds.AutomaticTransactionGenerator.configurationValidationResultremains stale.The
templateFileWatchercallback itself does not emitChargingStationEvents.updated. The fix works via the always-emit invariant ofchargingStation.startAutomaticTransactionGeneratoratChargingStation.ts:1213: every start attempt unconditionally emitsupdatedaftersaveAutomaticTransactionGeneratorConfiguration(), so the ATG-side subscription clears the cache between successive retries.Reproduction (see test 2 for machine-verified reproduction):
chargingStation.startAutomaticTransactionGenerator([1])via broadcast channel (ChargingStationWorkerBroadcastChannel.ts:213-217).chargingStation.startAutomaticTransactionGeneratoratChargingStation.ts:1200-1214delegates toatg.startConnector(1)(notatg.start()) →validateConfigurationon invalid bounds cachesfalse.atg.startedremainsfalse. Post-attempt L1213 emit clears the cache (with this PR).startConnectorre-parses fresh bounds → succeeds. Without this PR, step 2's cache-set-to-falsesurvives (no L1213 subscriber existed) and step 4 short-circuits.Verified ATG side (branch head
6fd96d6a):configurationValidationResult: boolean | undefinedat L51.undefinedin constructor at L63.this.chargingStation.on(ChargingStationEvents.updated, this.onUpdated)).deleteInstanceunsubscribes at L84 (instance.chargingStation.off(ChargingStationEvents.updated, instance.onUpdated)).stop()safety-net clear at L154 (was L140 onmain— shifted by the constructor comment +deleteInstanceunsubscribe plumbing, semantic-identical).onUpdatedat L432-434.validateConfigurationat L576-607 memoizes at L577-578 (was L558-589 / L559-560 onmain).main.Verified emitter side of
ChargingStationEvents.updated— full enumeration:ChargingStation.ts: L1118, L1213 (always-emit invariant), L1312, L1819, L2348, L2370, L2518, L2564.OCPPRequestService.ts: L341, L366.AutomaticTransactionGenerator.ts: L414, L458.src/.Approach chosen: subscribe to
ChargingStationEvents.updatedRationale over alternatives:
validateConfiguration— converts cheap 4-bound-check into unconditional hash+compare per call. Strictly worse. Rejected.logger.errorlines per start cycle). Rejected.Why
updatedis not too chatty: handler is O(1). Chattiness atChargingStation.ts:1819andOCPPRequestService.ts:341/366is negligible.Circular subscription safety: ATG emits
updatedat L414/L458, both aftervalidateConfigurationhas already returned — no race.Listener-leak invariant: constructor subscribes;
deleteInstanceunsubscribes symmetrically. Test 5 assertslistenerCount === 1across 5 successive delete/get cycles and verifies double-consecutivedeleteInstanceidempotency (returnsfalse, listener count stays 0).Serialization boundary safety:
buildUpdatedMessageandbuildStartedMessage(MessageChannelUtils.ts) never structured-clone the ATG instance's arrow-field listener reference.Class-field init order: with
target: ESNext+strict: true, instance-field initializers run before the constructor body.this.onUpdatedis defined by the time L71'sthis.chargingStation.on(...)runs.hashIdinvariance: written once atChargingStation.ts:1637. Never mutated.Changes
src/charging-station/AutomaticTransactionGenerator.ts(+18 lines):ChargingStationEvents.updatedat L71.deleteInstanceunsubscribes at L74-90 (cleanup-then-map-delete pattern, structurally mirrorsCoherentMeterValuesManager.deleteInstance).onUpdatedat L432-434.stop()L154 safety-net clear preserved (constraint honored).validateConfigurationlogic (constraint honored).tests/charging-station/AutomaticTransactionGenerator.test.ts(+182 lines):wireEventEmitter(with explicit ordering-requirement JSDoc warning) andsetATGBoundstest helpers.configurationValidationResult cache invalidation (issue #1965):should invalidate the cache on ChargingStationEvents.updated— unit-level: verifies the cache-clear on emit.should recover startConnector after ChargingStationEvents.updated invalidates a cached-false decision— end-to-end reproduction of the Step 0 bug scenario:startConnector(1)with invalid bounds → cache=false → correct bounds + emit →startConnector(1)→ cache=true.should still clear the cache on stop() (redundant with event-driven invalidation)— safety-net regression guard.should be idempotent under sequential updated events— no exception under back-to-back sync emits.should not leak listeners across deleteInstance/getInstance cycles—listenerCount(ChargingStationEvents.updated)stays at 1 across 5 delete/get cycles plus double-consecutivedeleteInstanceidempotency: second call returnsfalse, listener count stays 0.tests/charging-station/helpers/StationHelpers.factory.ts(+2 lines):off: () => stationno-op for EventEmitter parity.No changes outside the ATG scope
Per mission constraints:
validateConfigurationinternal logic untouched, existing emit sites inChargingStation.tsandOCPPRequestService.tsuntouched,stop()safety-net clear preserved.Verification gates
pnpm formatpnpm typecheckpnpm lintpnpm test(root)pnpm build1across 5 cycles + double-delete idempotency (test 5)Session-established review method — 11 rounds
03caf3b3hot-reload→reload) →b5e9c3c5onChargingStationUpdated→onUpdated; test "back-to-back" → "sequential"; constructor comment pattern-lead + sibling divergence →c4a46687(redundant with event-driven invalidation); new end-to-end test 2 (originally "1b") →590112c1off/onalphabetical order self-contradiction)wireEventEmitter; redundant test 3 assertion dropped →622c8867deleteInstancenot tested)6fd96d6a6fd96d6av7 cross-agent divergence resolutions
emitChargingStationEventguard bypass): Oracle correctly countered that L265 constructor listener guaranteeslistenerCount ≥ 1; test bypass has zero practical impact. Skip.enable: truedead insetATGBounds): Oracle correctly countered that it's a DRY-parity mirror ofaddATGMethodsToStation. Skip.deleteInstance— the loop pattern is{delete; get; delete; get; ...}, never consecutive deletes. Applied Explore's fix.Adapted-scope criteria — all 22 dimensions final
hashIdinvarianceui/*+tests/ocpp-server/)wireEventEmitterJSDoc verified idiomatic — 72+ Markdown-emphasis precedents in codebase)resetATGInstanceshygieneMaxListenersExceededWarningriskorigin/mainunchanged since PR opened; no rebase needed)Total across 11 rounds: 18 reviewer invocations (11 Oracle + 5 Explore + 3 external GitHub bot comments). 13 fixes applied across the session. All quality gates green. GitHub CI: 44/44 checks pass. Awaiting requested reviewer approval.