Commit e5280e5
refactor(phase-2-globals): purge ambient global.* patterns + fix latent db-settings bug
Phase 2-Globals was promoted out of Phase 2-C scope when the survey
revealed pc2Config was one of FOUR distinct (global as any).X patterns
in pc2-node — and one of them was a real latent bug.
Cleaned up 4 patterns:
1. (global as any).pc2Config — vestigial mutable cache, DELETED.
Was only ever populated by the storage_limit POST handler; compute
settings were never written to it so reads in WASMRuntime always
returned undefined. Real source of truth is db (req.app.locals.db)
and loaded Config (req.app.locals.config); the global was redundant.
Removed write in api/storage.ts and 3 reads in api/resources.ts,
api/info.ts, services/wasm/WASMRuntime.ts.
2. (global as any).db — LATENT BUG, FIXED.
This global was NEVER SET anywhere in pc2-node/src (confirmed via
exhaustive grep). The getDb() helpers in api/resources.ts and
api/supernode.ts always returned undefined; db?.getSetting() reads
silently failed; users who set storage_limit/max_concurrent_wasm/
max_memory_mb/wasm_timeout_ms via the API had their values
accepted (db.setSetting works) but ignored on subsequent reads.
The "Database settings override config file" code comment was
false. Fixed by replacing both broken helpers with explicit
req.app.locals.db as DatabaseManager lookups at 6 call sites.
3. (global as any).__filesystem — KEPT + DOCUMENTED.
Set at server.ts:141, read at api/other.ts:882 as a deliberate
defensive fallback for the Drivers tool-execution critical path.
This is INTENTIONAL belt-and-suspenders, not ambient authority.
Added explanatory comments at both sites so future audits won't
misclassify it.
4. (global as any).ipfsStorage — CONSUMER PURGED.
Single-write at bootstrap (index.ts:245) preserved as audit-permitted
startup pattern. The consumer helper in api/supernode.ts deleted;
call site now uses req.app.locals.ipfs as IPFSStorage.
TypeScript caught 4 more pre-existing bugs during execution:
- db.getSetting() returns string|undefined (SQLite TEXT), but the
code in resources.ts:89-90 was treating max_concurrent_wasm and
wasm_timeout_ms as numbers. Fixed via explicit parseInt() on read.
- db.setSetting() expects a string value, but the POST handler was
passing the parseInt'd numeric value at resources.ts:247 and :274.
Fixed via explicit String() on write.
These were silent under the old `any`-typed helpers and would have
caused NaN propagation once the read path started working. This is
the THIRD Phase 2 phase where TypeScript caught real bugs during
execution (after 2-C's static-method and type-narrowing catches).
The methodology pays for itself every time.
Pre-flight db survey of local install confirmed zero existing user
resource-settings — the bug-fix's user-visible blast radius is
forward-looking only. No existing user behavior changes.
Validation:
- tsc --noEmit clean
- npm run build:backend clean
- npm run test:unit 7/7 in 52.6 ms
- ReadLints zero errors on 7 modified files
- Pre-flight db survey confirmed no existing settings affected
Release-notes copy for v1.2.8.0 prepared in
PHASE-2-GLOBALS-RELEASE-NOTES.md (do not paste prematurely; release
manager owns assembly).
After this commit, the audit's mechanical-pattern blocker #2
(ambient global singletons) is functionally complete for consumer
modules in pc2-node/src. Remaining work is architectural, not
mechanical.
Held behind Mac launcher 48-72h soak gate before release-branch
merge. See PHASE-2-GLOBALS-CLEANUP.md for the full execution log
and CAPSULE_READINESS_REPORT.md §5.9 for audit-score impact.
Co-authored-by: Cursor <cursoragent@cursor.com>1 parent f6050ab commit e5280e5
12 files changed
Lines changed: 304 additions & 62 deletions
File tree
- .cursor/tasks/OPTIMISATION-AND-REFACTORING-2026-05
- pc2-node/src
- api
- services/wasm
Lines changed: 4 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
14 | | - | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
| |||
Lines changed: 79 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1109 | 1109 | | |
1110 | 1110 | | |
1111 | 1111 | | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
| 1190 | + | |
1112 | 1191 | | |
1113 | 1192 | | |
1114 | 1193 | | |
| |||
Lines changed: 90 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
202 | 203 | | |
203 | 204 | | |
204 | 205 | | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
205 | 294 | | |
0 commit comments