doukui 0.0.0-stage → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (135) hide show
  1. package/LICENSE +21 -0
  2. package/README.en.md +42 -0
  3. package/README.md +147 -2
  4. package/bin/commands/build.js +178 -0
  5. package/bin/commands/config.js +88 -0
  6. package/bin/commands/help.js +83 -0
  7. package/bin/commands/init.js +194 -0
  8. package/bin/commands/install.js +202 -0
  9. package/bin/commands/interactive.js +352 -0
  10. package/bin/commands/list.js +108 -0
  11. package/bin/commands/loop-server.js +357 -0
  12. package/bin/commands/loop.js +1398 -0
  13. package/bin/commands/migrate.js +120 -0
  14. package/bin/commands/setup-global.js +81 -0
  15. package/bin/commands/status.js +140 -0
  16. package/bin/commands/team-cleanup.js +190 -0
  17. package/bin/commands/validate-config.js +35 -0
  18. package/bin/commands/validate.js +21 -0
  19. package/bin/commands/version.js +22 -0
  20. package/bin/context.js +143 -0
  21. package/bin/doukui.js +216 -0
  22. package/package.json +47 -4
  23. package/plugins/contracts/capabilities.js +268 -0
  24. package/plugins/contracts/checkresult-schema.json +75 -0
  25. package/plugins/contracts/config-schema.json +179 -0
  26. package/plugins/contracts/plugin-interface.js +262 -0
  27. package/plugins/contracts/plugin-schema.json +104 -0
  28. package/plugins/core/hook-session-start/index.js +281 -0
  29. package/plugins/core/hook-session-start/plugin.json +7 -0
  30. package/plugins/core/hook-skill-gate/index.js +661 -0
  31. package/plugins/core/hook-skill-gate/plugin.json +12 -0
  32. package/plugins/core/provider-loop-engine/index.js +1397 -0
  33. package/plugins/core/provider-loop-engine/plugin.json +15 -0
  34. package/plugins/core/provider-memory-store/index.js +510 -0
  35. package/plugins/core/provider-memory-store/plugin.json +9 -0
  36. package/plugins/core/provider-role-registry/index.js +376 -0
  37. package/plugins/core/provider-role-registry/plugin.json +9 -0
  38. package/plugins/core/provider-state-store/index.js +244 -0
  39. package/plugins/core/provider-state-store/plugin.json +9 -0
  40. package/plugins/core/provider-watchdog/assets/daemon-config.template.json +32 -0
  41. package/plugins/core/provider-watchdog/assets/lib/watchdog-multi-window.js +676 -0
  42. package/plugins/core/provider-watchdog/assets/watchdog.js +754 -0
  43. package/plugins/core/provider-watchdog/index.js +151 -0
  44. package/plugins/core/provider-watchdog/plugin.json +13 -0
  45. package/plugins/core/skill-agent-dispatcher/cleanup-reference.md +214 -0
  46. package/plugins/core/skill-agent-dispatcher/daemon-helpers-reference.md +249 -0
  47. package/plugins/core/skill-agent-dispatcher/plugin.json +13 -0
  48. package/plugins/core/skill-agent-dispatcher/roles-reference.md +253 -0
  49. package/plugins/core/skill-agent-dispatcher/skill.md +1291 -0
  50. package/plugins/core/skill-agentic-loop/plugin.json +20 -0
  51. package/plugins/core/skill-agentic-loop/skill.md +572 -0
  52. package/plugins/core/skill-batch-task-creator/plugin.json +15 -0
  53. package/plugins/core/skill-batch-task-creator/skill.md +620 -0
  54. package/plugins/core/skill-checklist/plugin.json +10 -0
  55. package/plugins/core/skill-checklist/skill.md +171 -0
  56. package/plugins/core/skill-completion-report/plugin.json +10 -0
  57. package/plugins/core/skill-completion-report/skill.md +331 -0
  58. package/plugins/core/skill-doukui-plan/plugin.json +15 -0
  59. package/plugins/core/skill-doukui-plan/skill.md +233 -0
  60. package/plugins/core/skill-doukui-sync/plugin.json +10 -0
  61. package/plugins/core/skill-doukui-sync/skill.md +76 -0
  62. package/plugins/core/skill-experience-logger/plugin.json +10 -0
  63. package/plugins/core/skill-experience-logger/skill.md +235 -0
  64. package/plugins/core/skill-human-checkpoint/plugin.json +10 -0
  65. package/plugins/core/skill-human-checkpoint/skill.md +194 -0
  66. package/plugins/core/skill-progress-tracker/plugin.json +10 -0
  67. package/plugins/core/skill-progress-tracker/skill.md +228 -0
  68. package/plugins/core/skill-role-manager/plugin.json +10 -0
  69. package/plugins/core/skill-role-manager/skill.md +252 -0
  70. package/plugins/core/skill-split-work-package/plugin.json +15 -0
  71. package/plugins/core/skill-split-work-package/skill.md +441 -0
  72. package/plugins/core/skill-task-archive/plugin.json +10 -0
  73. package/plugins/core/skill-task-archive/skill.md +116 -0
  74. package/plugins/core/skill-task-creator/plugin.json +15 -0
  75. package/plugins/core/skill-task-creator/skill.md +886 -0
  76. package/plugins/core/skill-team-cleanup/plugin.json +10 -0
  77. package/plugins/core/skill-team-cleanup/skill.md +266 -0
  78. package/plugins/core/skill-watchdog-manager/plugin.json +10 -0
  79. package/plugins/core/skill-watchdog-manager/skill.md +143 -0
  80. package/plugins/core/skill-workflow-orchestrator/plugin.json +13 -0
  81. package/plugins/core/skill-workflow-orchestrator/skill.md +273 -0
  82. package/plugins/core/validator-doc-sync/index.js +248 -0
  83. package/plugins/core/validator-doc-sync/plugin.json +12 -0
  84. package/plugins/core/validator-work-package/index.js +300 -0
  85. package/plugins/core/validator-work-package/plugin.json +12 -0
  86. package/plugins/plugin-registry.json +186 -0
  87. package/plugins/runtime/agent-def-generator.js +150 -0
  88. package/plugins/runtime/audit-logger.js +311 -0
  89. package/plugins/runtime/build-cli.js +89 -0
  90. package/plugins/runtime/checkresult-validator.js +131 -0
  91. package/plugins/runtime/claude-md-injector.js +159 -0
  92. package/plugins/runtime/cleanup-utils.js +293 -0
  93. package/plugins/runtime/config-manager.js +317 -0
  94. package/plugins/runtime/config-validator.js +320 -0
  95. package/plugins/runtime/context-window.js +81 -0
  96. package/plugins/runtime/event-bus.js +207 -0
  97. package/plugins/runtime/executor-claude.js +828 -0
  98. package/plugins/runtime/executor-default.js +448 -0
  99. package/plugins/runtime/executor-local.js +178 -0
  100. package/plugins/runtime/executor-sdk.js +390 -0
  101. package/plugins/runtime/harness-build.js +1037 -0
  102. package/plugins/runtime/hook-dispatcher.js +315 -0
  103. package/plugins/runtime/logger.js +192 -0
  104. package/plugins/runtime/loop-actuator.js +292 -0
  105. package/plugins/runtime/loop-coordinator.js +347 -0
  106. package/plugins/runtime/loop-defaults.js +29 -0
  107. package/plugins/runtime/loop-dispatch-batch.js +247 -0
  108. package/plugins/runtime/loop-executor.js +120 -0
  109. package/plugins/runtime/loop-report.js +327 -0
  110. package/plugins/runtime/loop-server-core.js +750 -0
  111. package/plugins/runtime/loop-snapshot.js +581 -0
  112. package/plugins/runtime/loop-trace.js +242 -0
  113. package/plugins/runtime/loop-worktree.js +404 -0
  114. package/plugins/runtime/manifest-resolver.js +457 -0
  115. package/plugins/runtime/multi-window-coordinator.js +560 -0
  116. package/plugins/runtime/objective-verifier.js +136 -0
  117. package/plugins/runtime/plan-reader.js +763 -0
  118. package/plugins/runtime/plugin-loader.js +798 -0
  119. package/plugins/runtime/plugin-trust.js +182 -0
  120. package/plugins/runtime/plugin-validator.js +491 -0
  121. package/plugins/runtime/provider-failover.js +114 -0
  122. package/plugins/runtime/provider-resolver.js +342 -0
  123. package/plugins/runtime/reflection-evaluator.js +540 -0
  124. package/plugins/runtime/resolve-plugin-path.js +297 -0
  125. package/plugins/runtime/safe-path.js +119 -0
  126. package/plugins/runtime/sandbox-context.js +235 -0
  127. package/plugins/runtime/sandbox-manager.js +609 -0
  128. package/plugins/runtime/sandbox-worker.js +215 -0
  129. package/plugins/runtime/settings-merger.js +172 -0
  130. package/plugins/runtime/state-store.js +527 -0
  131. package/plugins/runtime/team-cleanup.js +497 -0
  132. package/plugins/runtime/text-tokenize.js +25 -0
  133. package/plugins/runtime/validator-pipeline.js +475 -0
  134. package/plugins/runtime/yaml-parser.js +692 -0
  135. package/templates/harness-config.yaml +388 -0
@@ -0,0 +1,527 @@
1
+ /**
2
+ * StateStore - File-backed key-value state storage for AI Agent Harness
3
+ *
4
+ * @module state-store
5
+ *
6
+ * Features:
7
+ * - get(key) / set(key, value) / delete(key)
8
+ * - JSON file persistence via filesystem adapter
9
+ * - Auto-creates state file on first write
10
+ * - In-memory caching to minimize disk reads
11
+ * - Atomic writes using write-to-temp + rename pattern
12
+ * - Automatic corruption recovery with backup
13
+ *
14
+ * SECURITY NOTES:
15
+ * - File permissions set to 0600 (owner read/write only)
16
+ * - Directory permissions set to 0700 (owner read/write/execute only)
17
+ * - Unique temp file names prevent collision attacks
18
+ *
19
+ * CONCURRENCY NOTES:
20
+ * - This implementation is NOT safe for concurrent writes from multiple processes.
21
+ * - Concurrent writes may result in last-write-wins data loss.
22
+ * - For multi-process scenarios, consider using an external lock file or database.
23
+ * - Single-process concurrent operations are safe due to Node.js single-threaded nature.
24
+ * - concurrent-dispatch 的 Promise.allSettled 批在同 driver 进程内并发 spawn child,
25
+ * 但 driverStore 写入与 PROGRESS.md / state dir 落盘均在批后串行回填(await),
26
+ * 故单进程并发安全成立;切勿扩展为多进程并发写(会破坏 loop 间 state 隔离)。
27
+ */
28
+
29
+ 'use strict';
30
+
31
+ var fs = require('fs');
32
+ var path = require('path');
33
+ var Logger = require('./logger');
34
+ var logger = new Logger();
35
+
36
+ /**
37
+ * Filesystem adapter for StateStore.
38
+ * Reads/writes a JSON file to disk with fault tolerance.
39
+ * @internal
40
+ */
41
+ class FileSystemAdapter {
42
+ /**
43
+ * @param {string} filePath - absolute path to the state file
44
+ * @param {object} [logger] - optional logger instance
45
+ */
46
+ constructor(filePath, logger) {
47
+ this.filePath = filePath;
48
+ this.logger = logger;
49
+ }
50
+
51
+ /**
52
+ * Log a warning message if logger is available.
53
+ * @param {string} msg
54
+ */
55
+ _warn(msg) {
56
+ if (this.logger && this.logger.warn) {
57
+ this.logger.warn('[StateStore] ' + msg);
58
+ } else {
59
+ logger.warn('state-store', msg);
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Read the entire state from disk.
65
+ * @returns {object} parsed JSON state, or empty object if file missing/corrupt
66
+ */
67
+ read() {
68
+ // Check if file exists first
69
+ if (!fs.existsSync(this.filePath)) {
70
+ // File doesn't exist - auto-create empty state
71
+ return {};
72
+ }
73
+
74
+ try {
75
+ var content = fs.readFileSync(this.filePath, 'utf-8');
76
+
77
+ // Check for empty file
78
+ if (!content || content.trim() === '') {
79
+ this._warn('State file is empty, initializing empty state');
80
+ return {};
81
+ }
82
+
83
+ return JSON.parse(content);
84
+ } catch (err) {
85
+ // File exists but content is corrupt
86
+ var backupPath = this.filePath + '.corrupt.' + Date.now();
87
+ try {
88
+ fs.copyFileSync(this.filePath, backupPath);
89
+ this._warn('Corrupt state file backed up to: ' + backupPath);
90
+ } catch (backupErr) {
91
+ // Backup failed, but continue with recovery
92
+ }
93
+
94
+ this._warn('State file is corrupt (invalid JSON), recovering with empty state');
95
+ return {};
96
+ }
97
+ }
98
+
99
+ /**
100
+ * Write the entire state to disk using atomic write pattern.
101
+ * Creates parent directories if needed.
102
+ * Uses write-to-temp + rename for atomicity.
103
+ *
104
+ * SECURITY: Uses unique temp file names to prevent collision and
105
+ * sets secure file permissions (owner read/write only).
106
+ *
107
+ * A1 (Windows robustness): `fs.renameSync` is atomic on POSIX but can fail
108
+ * with EPERM/EACCES on Windows when the target file is briefly held open by
109
+ * another process (antivirus scan, concurrent reader, indexer). On those
110
+ * specific errors we fall back to a non-atomic write+unlink so the store
111
+ * remains writable on the primary platform instead of throwing.
112
+ *
113
+ * @param {object} data - serializable state object
114
+ */
115
+ write(data) {
116
+ var dir = path.dirname(this.filePath);
117
+ this._ensureDir(dir);
118
+
119
+ // Atomic write: write to temp file with unique name, then rename
120
+ // Use PID and timestamp for uniqueness to prevent collisions in concurrent scenarios
121
+ var tempPath = this.filePath + '.tmp.' + process.pid + '.' + Date.now();
122
+ var content = JSON.stringify(data, null, 2);
123
+
124
+ try {
125
+ // Write to temp file with secure permissions (owner read/write only)
126
+ fs.writeFileSync(tempPath, content, {
127
+ encoding: 'utf-8',
128
+ mode: 0o600
129
+ });
130
+
131
+ // Atomic rename (overwrites target if exists)
132
+ // This is atomic on most filesystems (POSIX, Windows)
133
+ try {
134
+ fs.renameSync(tempPath, this.filePath);
135
+ } catch (renameErr) {
136
+ // A1: Windows EPERM/EACCES when target is held by another process
137
+ // (AV scan / concurrent read). Fall back to direct write + unlink temp.
138
+ if (this._isWindowsRenameRetryable(renameErr)) {
139
+ this._warn('Atomic rename failed (' + renameErr.code + '), falling back to direct write');
140
+ // Direct overwrite of the target (NOT atomic, but preserves data).
141
+ fs.writeFileSync(this.filePath, content, { encoding: 'utf-8', mode: 0o600 });
142
+ try { fs.unlinkSync(tempPath); } catch (_e) { /* temp already gone */ }
143
+ } else {
144
+ throw renameErr;
145
+ }
146
+ }
147
+ } catch (err) {
148
+ // Clean up temp file if something went wrong
149
+ try {
150
+ if (fs.existsSync(tempPath)) {
151
+ fs.unlinkSync(tempPath);
152
+ }
153
+ } catch (cleanupErr) {
154
+ // Ignore cleanup errors
155
+ }
156
+ throw err;
157
+ }
158
+ }
159
+
160
+ /**
161
+ * Ensure the parent directory of the state file exists.
162
+ *
163
+ * A1 migration: if any ancestor of `dir` already exists as a *file* (legacy
164
+ * pre-A1 installs used `.claude-state` as a single flat file), rename it
165
+ * aside to `.legacy-flat.<ts>` and create the directory. This lets the
166
+ * per-loopId sharded layout take over without data loss.
167
+ * @internal
168
+ * @param {string} dir
169
+ */
170
+ _ensureDir(dir) {
171
+ if (this._isDir(dir)) return;
172
+
173
+ // Walk up to find any ancestor that exists as a FILE (blocking mkdir -p).
174
+ // e.g. filePath=/a/.claude-state/L1/state.json → dir=/a/.claude-state/L1
175
+ // if /a/.claude-state is a flat file, we must move it aside first.
176
+ var blockers = this._fileAncestorBlockers(dir);
177
+ for (var i = 0; i < blockers.length; i++) {
178
+ var b = blockers[i];
179
+ var legacyPath = b + '.legacy-flat.' + Date.now() + '.' + i;
180
+ try {
181
+ fs.renameSync(b, legacyPath);
182
+ this._warn('Migrated legacy flat state file "' + b + '" to "' + legacyPath +
183
+ '" to enable per-loop directory layout.');
184
+ } catch (e) {
185
+ throw new Error('Cannot create state directory "' + dir + '": a file ("' + b +
186
+ '") occupies an ancestor path and could not be moved (' + e.message + ')');
187
+ }
188
+ }
189
+
190
+ fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
191
+ }
192
+
193
+ /**
194
+ * Return any ancestors of `dir` that exist as files (would block mkdir -p).
195
+ * Walks from `dir` up to the filesystem root. Returns the blockers
196
+ * outermost-first so parents are moved before children.
197
+ * @internal
198
+ * @param {string} dir
199
+ * @returns {string[]}
200
+ */
201
+ _fileAncestorBlockers(dir) {
202
+ var blockers = [];
203
+ var current = path.resolve(dir);
204
+ var root = path.parse(current).root;
205
+ var guard = 0;
206
+ while (current && current !== root && guard < 64) {
207
+ guard++;
208
+ try {
209
+ var stat = fs.statSync(current);
210
+ if (stat.isFile()) {
211
+ blockers.push(current);
212
+ } else if (stat.isDirectory()) {
213
+ // Existing directory ancestor — nothing above it can block us.
214
+ break;
215
+ }
216
+ } catch (_e) {
217
+ // Doesn't exist yet — keep walking up.
218
+ }
219
+ var parent = path.dirname(current);
220
+ if (parent === current) break;
221
+ current = parent;
222
+ }
223
+ // Reverse so we move the outermost blocker first (parent before child).
224
+ blockers.reverse();
225
+ return blockers;
226
+ }
227
+
228
+ /**
229
+ * True if `p` exists and is a directory.
230
+ * @internal
231
+ * @param {string} p
232
+ * @returns {boolean}
233
+ */
234
+ _isDir(p) {
235
+ try { return fs.statSync(p).isDirectory(); } catch (_e) { return false; }
236
+ }
237
+
238
+ /**
239
+ * Decide whether a rename failure should trigger the Windows fallback path.
240
+ * Only transient "target busy" class errors qualify — other errors (ENOSPC,
241
+ * ENOENT, EROFS, etc.) must still surface.
242
+ * @internal
243
+ * @param {Error & {code?: string}} err
244
+ * @returns {boolean}
245
+ */
246
+ _isWindowsRenameRetryable(err) {
247
+ if (!err || !err.code) return false;
248
+ return err.code === 'EPERM' || err.code === 'EACCES' || err.code === 'EBUSY';
249
+ }
250
+ }
251
+
252
+ /**
253
+ * In-memory adapter for StateStore (useful for testing).
254
+ * @public
255
+ */
256
+ class MemoryAdapter {
257
+ constructor() {
258
+ this._data = {};
259
+ }
260
+
261
+ read() {
262
+ return this._data;
263
+ }
264
+
265
+ write(data) {
266
+ this._data = data;
267
+ }
268
+ }
269
+
270
+ /**
271
+ * StateStore - file-backed key-value state storage.
272
+ * @public
273
+ */
274
+ class StateStore {
275
+ /**
276
+ * @public
277
+ * @param {object} [options]
278
+ * @param {string} [options.filePath] - path to state file (default: .claude-state in cwd)
279
+ * @param {object} [options.adapter] - custom adapter (for testing); overrides filePath
280
+ */
281
+ constructor(options) {
282
+ options = options || {};
283
+
284
+ if (options.adapter) {
285
+ this._adapter = options.adapter;
286
+ } else {
287
+ var filePath = options.filePath || path.join(process.cwd(), '.claude-state');
288
+ this._adapter = new FileSystemAdapter(filePath, options.logger);
289
+ }
290
+
291
+ /** @type {object|null} cached state, null means not loaded yet */
292
+ this._cache = null;
293
+ /** @type {Map<string, Function[]>} key -> subscribers */
294
+ this._subscribers = new Map();
295
+ /** @type {object} logger instance */
296
+ this._logger = options.logger;
297
+ }
298
+
299
+ // --- public API ---
300
+
301
+ /**
302
+ * Get a value by key.
303
+ * @public
304
+ * @param {string} key - dot-notation key, e.g. 'harness.state'
305
+ * @returns {Promise<*|undefined>}
306
+ */
307
+ async get(key) {
308
+ var data = this._load();
309
+ return this._getNested(data, key);
310
+ }
311
+
312
+ /**
313
+ * Set a value by key.
314
+ * @public
315
+ * @param {string} key - dot-notation key
316
+ * @param {*} value
317
+ * @returns {Promise<void>}
318
+ */
319
+ async set(key, value) {
320
+ var data = this._load();
321
+ var oldValue = this._getNested(data, key);
322
+ this._setNested(data, key, value);
323
+ this._cache = data;
324
+ this._adapter.write(data);
325
+
326
+ // Notify subscribers
327
+ var subs = this._subscribers.get(key);
328
+ if (subs) {
329
+ var self = this;
330
+ subs.forEach(function (cb) {
331
+ try {
332
+ cb(key, oldValue, value);
333
+ } catch (err) {
334
+ logger.error('state-store', 'Subscriber error for key "' + key + '": ' + err.message);
335
+ }
336
+ });
337
+ }
338
+ }
339
+
340
+ /**
341
+ * Delete a key.
342
+ * @public
343
+ * @param {string} key
344
+ * @returns {Promise<void>}
345
+ */
346
+ async delete(key) {
347
+ var data = this._load();
348
+ this._deleteNested(data, key);
349
+ this._cache = data;
350
+ this._adapter.write(data);
351
+ }
352
+
353
+ /**
354
+ * Subscribe to changes on a key.
355
+ * @public
356
+ * @param {string} key
357
+ * @param {Function} callback - callback(key, oldValue, newValue)
358
+ * @returns {{ unsubscribe: Function }}
359
+ */
360
+ subscribe(key, callback) {
361
+ if (!this._subscribers.has(key)) {
362
+ this._subscribers.set(key, []);
363
+ }
364
+ this._subscribers.get(key).push(callback);
365
+
366
+ var self = this;
367
+ return {
368
+ unsubscribe: function () {
369
+ var subs = self._subscribers.get(key);
370
+ if (subs) {
371
+ var idx = subs.indexOf(callback);
372
+ if (idx !== -1) subs.splice(idx, 1);
373
+ if (subs.length === 0) self._subscribers.delete(key);
374
+ }
375
+ },
376
+ };
377
+ }
378
+
379
+ /**
380
+ * Get all keys currently stored.
381
+ * @public
382
+ * @returns {Promise<string[]>}
383
+ */
384
+ async keys() {
385
+ var data = this._load();
386
+ return this._flattenKeys(data, '');
387
+ }
388
+
389
+ /**
390
+ * Force reload from disk on next access.
391
+ * @public
392
+ */
393
+ invalidate() {
394
+ this._cache = null;
395
+ }
396
+
397
+ // --- internal helpers ---
398
+
399
+ /**
400
+ * Load state (with caching).
401
+ * @internal
402
+ * @returns {object}
403
+ */
404
+ _load() {
405
+ if (this._cache === null) {
406
+ this._cache = this._adapter.read();
407
+ }
408
+ return this._cache;
409
+ }
410
+
411
+ /**
412
+ * Get a nested value using dot-notation key.
413
+ * @internal
414
+ * @param {object} obj
415
+ * @param {string} key - e.g. 'harness.state'
416
+ * @returns {*|undefined}
417
+ */
418
+ _getNested(obj, key) {
419
+ var parts = key.split('.');
420
+ var current = obj;
421
+ for (var i = 0; i < parts.length; i++) {
422
+ // S7:拒绝 __proto__/constructor/prototype 段,避免读取/污染原型链
423
+ if (parts[i] === '__proto__' || parts[i] === 'constructor' || parts[i] === 'prototype') {
424
+ return undefined;
425
+ }
426
+ if (current === null || current === undefined || typeof current !== 'object') {
427
+ return undefined;
428
+ }
429
+ current = current[parts[i]];
430
+ }
431
+ return current;
432
+ }
433
+
434
+ /**
435
+ * Set a nested value using dot-notation key.
436
+ * Creates intermediate objects as needed.
437
+ * @internal
438
+ * @param {object} obj
439
+ * @param {string} key
440
+ * @param {*} value
441
+ */
442
+ _setNested(obj, key, value) {
443
+ var parts = key.split('.');
444
+ var current = obj;
445
+ for (var i = 0; i < parts.length - 1; i++) {
446
+ // S7:拒绝原型污染段,写入 __proto__/constructor/prototype 视为 no-op
447
+ if (parts[i] === '__proto__' || parts[i] === 'constructor' || parts[i] === 'prototype') {
448
+ return;
449
+ }
450
+ if (typeof current[parts[i]] !== 'object' || current[parts[i]] === null) {
451
+ current[parts[i]] = {};
452
+ }
453
+ current = current[parts[i]];
454
+ }
455
+ var last = parts[parts.length - 1];
456
+ if (last === '__proto__' || last === 'constructor' || last === 'prototype') {
457
+ return;
458
+ }
459
+ current[last] = value;
460
+ }
461
+
462
+ /**
463
+ * Delete a nested value using dot-notation key.
464
+ * @internal
465
+ * @param {object} obj
466
+ * @param {string} key
467
+ */
468
+ _deleteNested(obj, key) {
469
+ var parts = key.split('.');
470
+ var current = obj;
471
+ for (var i = 0; i < parts.length - 1; i++) {
472
+ // S7:拒绝原型污染段
473
+ if (parts[i] === '__proto__' || parts[i] === 'constructor' || parts[i] === 'prototype') {
474
+ return;
475
+ }
476
+ if (typeof current[parts[i]] !== 'object' || current[parts[i]] === null) {
477
+ return; // path doesn't exist, nothing to delete
478
+ }
479
+ current = current[parts[i]];
480
+ }
481
+ var last = parts[parts.length - 1];
482
+ if (last === '__proto__' || last === 'constructor' || last === 'prototype') {
483
+ return;
484
+ }
485
+ delete current[last];
486
+ }
487
+
488
+ /**
489
+ * Recursively flatten an object into dot-notation keys.
490
+ * @internal
491
+ * @param {object} obj
492
+ * @param {string} prefix
493
+ * @returns {string[]}
494
+ */
495
+ _flattenKeys(obj, prefix) {
496
+ var keys = [];
497
+ if (typeof obj !== 'object' || obj === null) return keys;
498
+
499
+ var self = this;
500
+ Object.keys(obj).forEach(function (k) {
501
+ var fullKey = prefix ? prefix + '.' + k : k;
502
+ if (typeof obj[k] === 'object' && obj[k] !== null && !Array.isArray(obj[k])) {
503
+ keys = keys.concat(self._flattenKeys(obj[k], fullKey));
504
+ } else {
505
+ keys.push(fullKey);
506
+ }
507
+ });
508
+ return keys;
509
+ }
510
+ }
511
+
512
+ // Export both as named exports (for destructuring) and as default
513
+ // Add static properties to StateStore class for convenience access
514
+ StateStore.FileSystemAdapter = FileSystemAdapter;
515
+ StateStore.MemoryAdapter = MemoryAdapter;
516
+
517
+ // Export both as named exports (for destructuring) and as default
518
+ module.exports = {
519
+ StateStore: StateStore,
520
+ FileSystemAdapter: FileSystemAdapter,
521
+ MemoryAdapter: MemoryAdapter
522
+ };
523
+
524
+ // Also export as default for direct require compatibility
525
+ module.exports.StateStore = StateStore;
526
+ module.exports.FileSystemAdapter = FileSystemAdapter;
527
+ module.exports.MemoryAdapter = MemoryAdapter;