foottrace 0.0.0-stage → 1.0.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.
- package/LICENSE +21 -0
- package/README.md +73 -2
- package/dist/esm/index.d.ts +27 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/lib/capture/freeze.d.ts +91 -0
- package/dist/esm/lib/capture/freeze.js +313 -0
- package/dist/esm/lib/capture/ownData.d.ts +18 -0
- package/dist/esm/lib/capture/ownData.js +35 -0
- package/dist/esm/lib/capture/valueKinds.d.ts +57 -0
- package/dist/esm/lib/capture/valueKinds.js +141 -0
- package/dist/esm/lib/ids/runtimeStageId.d.ts +173 -0
- package/dist/esm/lib/ids/runtimeStageId.js +193 -0
- package/dist/esm/lib/memory/EventLog.d.ts +105 -0
- package/dist/esm/lib/memory/EventLog.js +147 -0
- package/dist/esm/lib/memory/RecordFrame.d.ts +232 -0
- package/dist/esm/lib/memory/RecordFrame.js +295 -0
- package/dist/esm/lib/memory/SharedMemory.d.ts +46 -0
- package/dist/esm/lib/memory/SharedMemory.js +87 -0
- package/dist/esm/lib/memory/TransactionBuffer.d.ts +521 -0
- package/dist/esm/lib/memory/TransactionBuffer.js +946 -0
- package/dist/esm/lib/memory/admission.d.ts +81 -0
- package/dist/esm/lib/memory/admission.js +152 -0
- package/dist/esm/lib/memory/backtrack.d.ts +286 -0
- package/dist/esm/lib/memory/backtrack.js +456 -0
- package/dist/esm/lib/memory/commitLogUtils.d.ts +217 -0
- package/dist/esm/lib/memory/commitLogUtils.js +383 -0
- package/dist/esm/lib/memory/deltaEncoding.d.ts +66 -0
- package/dist/esm/lib/memory/deltaEncoding.js +121 -0
- package/dist/esm/lib/memory/equality.d.ts +93 -0
- package/dist/esm/lib/memory/equality.js +261 -0
- package/dist/esm/lib/memory/eventPosition.d.ts +18 -0
- package/dist/esm/lib/memory/eventPosition.js +1 -0
- package/dist/esm/lib/memory/honesty.d.ts +93 -0
- package/dist/esm/lib/memory/honesty.js +91 -0
- package/dist/esm/lib/memory/keyPaths.d.ts +125 -0
- package/dist/esm/lib/memory/keyPaths.js +226 -0
- package/dist/esm/lib/memory/logModel.d.ts +76 -0
- package/dist/esm/lib/memory/logModel.js +363 -0
- package/dist/esm/lib/memory/merge.d.ts +26 -0
- package/dist/esm/lib/memory/merge.js +51 -0
- package/dist/esm/lib/memory/pathOps.d.ts +118 -0
- package/dist/esm/lib/memory/pathOps.js +292 -0
- package/dist/esm/lib/memory/paths.d.ts +56 -0
- package/dist/esm/lib/memory/paths.js +60 -0
- package/dist/esm/lib/memory/placeholders.d.ts +39 -0
- package/dist/esm/lib/memory/placeholders.js +39 -0
- package/dist/esm/lib/memory/recordCommit.d.ts +45 -0
- package/dist/esm/lib/memory/recordCommit.js +60 -0
- package/dist/esm/lib/memory/scrub.d.ts +52 -0
- package/dist/esm/lib/memory/scrub.js +72 -0
- package/dist/esm/lib/memory/types.d.ts +209 -0
- package/dist/esm/lib/memory/types.js +9 -0
- package/dist/esm/lib/memory/utils.d.ts +73 -0
- package/dist/esm/lib/memory/utils.js +150 -0
- package/dist/esm/lib/memory/verbs.d.ts +333 -0
- package/dist/esm/lib/memory/verbs.js +426 -0
- package/dist/esm/lib/recorder/CommitRangeIndex.d.ts +147 -0
- package/dist/esm/lib/recorder/CommitRangeIndex.js +206 -0
- package/dist/esm/lib/slice/elementProvenance.d.ts +81 -0
- package/dist/esm/lib/slice/elementProvenance.js +189 -0
- package/dist/esm/lib/slice/forwardSliceForKey.d.ts +88 -0
- package/dist/esm/lib/slice/forwardSliceForKey.js +242 -0
- package/dist/esm/lib/slice/index.d.ts +26 -0
- package/dist/esm/lib/slice/index.js +25 -0
- package/dist/esm/lib/slice/keyIndex.d.ts +137 -0
- package/dist/esm/lib/slice/keyIndex.js +292 -0
- package/dist/esm/lib/slice/keyTimeline.d.ts +49 -0
- package/dist/esm/lib/slice/keyTimeline.js +112 -0
- package/dist/esm/lib/slice/keysReadSources.d.ts +51 -0
- package/dist/esm/lib/slice/keysReadSources.js +94 -0
- package/dist/esm/lib/slice/serialize.d.ts +78 -0
- package/dist/esm/lib/slice/serialize.js +277 -0
- package/dist/esm/lib/slice/sliceForKey.d.ts +40 -0
- package/dist/esm/lib/slice/sliceForKey.js +78 -0
- package/dist/esm/lib/slice/types.d.ts +567 -0
- package/dist/esm/lib/slice/types.js +24 -0
- package/dist/esm/lib/time-travel/axis.d.ts +119 -0
- package/dist/esm/lib/time-travel/axis.js +178 -0
- package/dist/esm/lib/time-travel/bundles.d.ts +77 -0
- package/dist/esm/lib/time-travel/bundles.js +135 -0
- package/dist/esm/lib/time-travel/chain.d.ts +93 -0
- package/dist/esm/lib/time-travel/chain.js +187 -0
- package/dist/esm/lib/time-travel/commitStops.d.ts +55 -0
- package/dist/esm/lib/time-travel/commitStops.js +201 -0
- package/dist/esm/lib/time-travel/index.d.ts +22 -0
- package/dist/esm/lib/time-travel/index.js +20 -0
- package/dist/esm/lib/time-travel/stateAt.d.ts +102 -0
- package/dist/esm/lib/time-travel/stateAt.js +152 -0
- package/dist/esm/lib/time-travel/tagStops.d.ts +72 -0
- package/dist/esm/lib/time-travel/tagStops.js +101 -0
- package/dist/esm/lib/time-travel/timeTravel.d.ts +70 -0
- package/dist/esm/lib/time-travel/timeTravel.js +359 -0
- package/dist/esm/lib/time-travel/types.d.ts +470 -0
- package/dist/esm/lib/time-travel/types.js +22 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/paths.d.ts +4 -0
- package/dist/esm/paths.js +4 -0
- package/dist/esm/write.d.ts +49 -0
- package/dist/esm/write.js +47 -0
- package/dist/index.js +67 -0
- package/dist/lib/capture/freeze.js +319 -0
- package/dist/lib/capture/ownData.js +40 -0
- package/dist/lib/capture/valueKinds.js +145 -0
- package/dist/lib/ids/runtimeStageId.js +209 -0
- package/dist/lib/memory/EventLog.js +151 -0
- package/dist/lib/memory/RecordFrame.js +299 -0
- package/dist/lib/memory/SharedMemory.js +91 -0
- package/dist/lib/memory/TransactionBuffer.js +950 -0
- package/dist/lib/memory/admission.js +158 -0
- package/dist/lib/memory/backtrack.js +462 -0
- package/dist/lib/memory/commitLogUtils.js +398 -0
- package/dist/lib/memory/deltaEncoding.js +127 -0
- package/dist/lib/memory/equality.js +265 -0
- package/dist/lib/memory/eventPosition.js +2 -0
- package/dist/lib/memory/honesty.js +94 -0
- package/dist/lib/memory/keyPaths.js +242 -0
- package/dist/lib/memory/logModel.js +371 -0
- package/dist/lib/memory/merge.js +55 -0
- package/dist/lib/memory/pathOps.js +308 -0
- package/dist/lib/memory/paths.js +66 -0
- package/dist/lib/memory/placeholders.js +42 -0
- package/dist/lib/memory/recordCommit.js +64 -0
- package/dist/lib/memory/scrub.js +77 -0
- package/dist/lib/memory/types.js +10 -0
- package/dist/lib/memory/utils.js +169 -0
- package/dist/lib/memory/verbs.js +443 -0
- package/dist/lib/recorder/CommitRangeIndex.js +210 -0
- package/dist/lib/slice/elementProvenance.js +194 -0
- package/dist/lib/slice/forwardSliceForKey.js +246 -0
- package/dist/lib/slice/index.js +42 -0
- package/dist/lib/slice/keyIndex.js +312 -0
- package/dist/lib/slice/keyTimeline.js +116 -0
- package/dist/lib/slice/keysReadSources.js +100 -0
- package/dist/lib/slice/serialize.js +285 -0
- package/dist/lib/slice/sliceForKey.js +82 -0
- package/dist/lib/slice/types.js +25 -0
- package/dist/lib/time-travel/axis.js +183 -0
- package/dist/lib/time-travel/bundles.js +143 -0
- package/dist/lib/time-travel/chain.js +192 -0
- package/dist/lib/time-travel/commitStops.js +205 -0
- package/dist/lib/time-travel/index.js +31 -0
- package/dist/lib/time-travel/stateAt.js +159 -0
- package/dist/lib/time-travel/tagStops.js +105 -0
- package/dist/lib/time-travel/timeTravel.js +363 -0
- package/dist/lib/time-travel/types.js +23 -0
- package/dist/paths.js +14 -0
- package/dist/types/index.d.ts +27 -0
- package/dist/types/lib/capture/freeze.d.ts +91 -0
- package/dist/types/lib/capture/ownData.d.ts +18 -0
- package/dist/types/lib/capture/valueKinds.d.ts +57 -0
- package/dist/types/lib/ids/runtimeStageId.d.ts +173 -0
- package/dist/types/lib/memory/EventLog.d.ts +105 -0
- package/dist/types/lib/memory/RecordFrame.d.ts +232 -0
- package/dist/types/lib/memory/SharedMemory.d.ts +46 -0
- package/dist/types/lib/memory/TransactionBuffer.d.ts +521 -0
- package/dist/types/lib/memory/admission.d.ts +81 -0
- package/dist/types/lib/memory/backtrack.d.ts +286 -0
- package/dist/types/lib/memory/commitLogUtils.d.ts +217 -0
- package/dist/types/lib/memory/deltaEncoding.d.ts +66 -0
- package/dist/types/lib/memory/equality.d.ts +93 -0
- package/dist/types/lib/memory/eventPosition.d.ts +18 -0
- package/dist/types/lib/memory/honesty.d.ts +93 -0
- package/dist/types/lib/memory/keyPaths.d.ts +125 -0
- package/dist/types/lib/memory/logModel.d.ts +76 -0
- package/dist/types/lib/memory/merge.d.ts +26 -0
- package/dist/types/lib/memory/pathOps.d.ts +118 -0
- package/dist/types/lib/memory/paths.d.ts +56 -0
- package/dist/types/lib/memory/placeholders.d.ts +39 -0
- package/dist/types/lib/memory/recordCommit.d.ts +45 -0
- package/dist/types/lib/memory/scrub.d.ts +52 -0
- package/dist/types/lib/memory/types.d.ts +209 -0
- package/dist/types/lib/memory/utils.d.ts +73 -0
- package/dist/types/lib/memory/verbs.d.ts +333 -0
- package/dist/types/lib/recorder/CommitRangeIndex.d.ts +147 -0
- package/dist/types/lib/slice/elementProvenance.d.ts +81 -0
- package/dist/types/lib/slice/forwardSliceForKey.d.ts +88 -0
- package/dist/types/lib/slice/index.d.ts +26 -0
- package/dist/types/lib/slice/keyIndex.d.ts +137 -0
- package/dist/types/lib/slice/keyTimeline.d.ts +49 -0
- package/dist/types/lib/slice/keysReadSources.d.ts +51 -0
- package/dist/types/lib/slice/serialize.d.ts +78 -0
- package/dist/types/lib/slice/sliceForKey.d.ts +40 -0
- package/dist/types/lib/slice/types.d.ts +567 -0
- package/dist/types/lib/time-travel/axis.d.ts +119 -0
- package/dist/types/lib/time-travel/bundles.d.ts +77 -0
- package/dist/types/lib/time-travel/chain.d.ts +93 -0
- package/dist/types/lib/time-travel/commitStops.d.ts +55 -0
- package/dist/types/lib/time-travel/index.d.ts +22 -0
- package/dist/types/lib/time-travel/stateAt.d.ts +102 -0
- package/dist/types/lib/time-travel/tagStops.d.ts +72 -0
- package/dist/types/lib/time-travel/timeTravel.d.ts +70 -0
- package/dist/types/lib/time-travel/types.d.ts +470 -0
- package/dist/types/paths.d.ts +4 -0
- package/dist/types/write.d.ts +49 -0
- package/dist/write.js +56 -0
- package/package.json +48 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present Sanjay Krishna Anbalagan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,74 @@
|
|
|
1
|
-
#
|
|
1
|
+
# foottrace
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Execution records and the tools to read them: replay, time travel, slices, and evidence about where values came from. No flowchart engine, AI provider, or runtime dependency is required.
|
|
4
|
+
|
|
5
|
+
This package extracts FootPrint's established record layer. It does not introduce a second writer or a different replay law. The first release preserves the frozen record fixtures from footprintjs 9.48.1 and includes the coordinated 9.48.2 copy-safety correction: an own `__proto__` payload field stays data, never the copy's prototype. See the [changelog](CHANGELOG.md) for the limited rich-value behavior change.
|
|
6
|
+
|
|
7
|
+
**Release status:** extraction candidate. Version 1.0.0 is not ready to publish until review and release checks are complete.
|
|
8
|
+
|
|
9
|
+
## Three entry points
|
|
10
|
+
|
|
11
|
+
| Import | Responsibility |
|
|
12
|
+
| --- | --- |
|
|
13
|
+
| `foottrace` | Record types, runtime IDs, replay, time-travel cursors, causal and variable slices, honesty codes |
|
|
14
|
+
| `foottrace/write` | `SharedMemory`, `EventLog`, `RecordFrame`, encoding and write types, writer-owned freezing |
|
|
15
|
+
| `foottrace/paths` | Record-path encoding/decoding, nested access and safe diagnostic writes |
|
|
16
|
+
|
|
17
|
+
Every declaration has one public entry point. There are no public deep imports or `/testing` entry point. FootPrint's engine, observer channels, recorders and stores remain in `footprintjs`.
|
|
18
|
+
|
|
19
|
+
## Write and replay
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { stateAt } from 'foottrace';
|
|
23
|
+
import { EventLog, RecordFrame, SharedMemory } from 'foottrace/write';
|
|
24
|
+
|
|
25
|
+
const state = new SharedMemory(undefined, { count: 0 });
|
|
26
|
+
const log = new EventLog(state.getState());
|
|
27
|
+
const frame = new RecordFrame(state, log);
|
|
28
|
+
frame.useEncoding({ commitValues: 'full', writeProvenance: 'reads-prefix' });
|
|
29
|
+
const count = frame.read([], 'count') as number;
|
|
30
|
+
frame.noteRead([], 'count');
|
|
31
|
+
frame.write(frame.at([], 'count'), count + 1, 'set');
|
|
32
|
+
frame.commit(() => ({
|
|
33
|
+
stage: 'Increment', stageId: 'increment', runtimeStageId: 'increment#0',
|
|
34
|
+
}));
|
|
35
|
+
frame.release();
|
|
36
|
+
|
|
37
|
+
const result = stateAt({ initialState: log.getInitialState(), commitLog: log.list() }, 0);
|
|
38
|
+
console.log(result.state); // { count: 1 }
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Writers record during execution. Readers query what was recorded; they do not reconstruct missing events or infer certainty from absent evidence. A slice can be conservative or incomplete: retain its honesty codes when presenting it to a person or a model.
|
|
42
|
+
|
|
43
|
+
See the [record contract](docs/guides/record-contract.md), [time-travel contract](src/lib/time-travel/README.md), and [slice contract](src/lib/slice/README.md).
|
|
44
|
+
|
|
45
|
+
## Paths are not interchangeable
|
|
46
|
+
|
|
47
|
+
- `idPathSegments` (root entry) splits runtime IDs on `/` and drops empty segments. `isExecutionKey` detects `#`; it is not a complete validator.
|
|
48
|
+
- `pathSegments` and `normaliseStateKey` (`/paths`) decode/encode record paths with the existing unit-separator codec. Empty segments and literal dots are preserved.
|
|
49
|
+
- `nativeGet` and `nativeHas` accept dot-selector strings or literal segment arrays. Use arrays for literal dotted keys.
|
|
50
|
+
- `nativeHas` recognizes own properties, including an own `undefined` and dangerous-name data keys. `nativeGet` refuses dangerous segments and returns its default for `undefined`.
|
|
51
|
+
- `setNestedValue` and `updateNestedValue` refuse `__proto__`, `constructor`, or `prototype` anywhere in the target address before making any write. Refusal is a silent no-op, as in 9.48.1. Payload keys are data and are not filtered. Updates retain the existing shallow object merge and duplicate-preserving array concatenation; empty arrays replace. These helpers are not the record replay merge law.
|
|
52
|
+
|
|
53
|
+
## Ownership and equality
|
|
54
|
+
|
|
55
|
+
`freezeRecord` (`/write`) operates on **writer-owned** data in place and registers mutable built-in values for safe serving. It can replace a sliced buffer view. Do not pass an object whose ownership belongs to a caller. `serveRecord` (root entry) uses that registration to serve protected copies; it does not secure an arbitrary unregistered object. The entries share one freezing module within each module format.
|
|
56
|
+
|
|
57
|
+
`deepFreeze` (`/write`) is ordinary object freezing, not full immutability of Date/Map internals, and it does not descend through an already frozen object. Do not substitute it for `freezeRecord`.
|
|
58
|
+
|
|
59
|
+
`deepEqual` is the record's equality rule, not general-purpose deep equality: absent and own-undefined properties can compare equal, signed zero compares equal, array expando properties are ignored, and Map object keys use identity. The `'copies'` option treats opaque values of the same kind as equivalent. Preserve these distinctions when using it for change detection.
|
|
60
|
+
|
|
61
|
+
## Compatibility and development
|
|
62
|
+
|
|
63
|
+
Node.js 22 or newer; develop with Node.js 24 and npm 11. Both CommonJS and ESM are provided. Browser-capable source uses no Node runtime APIs. Use one module format consistently in an application; CJS and ESM have separate module instances.
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
npm ci
|
|
67
|
+
npm run check
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
No engine is installed in this package's test environment. Engine integration and the original flowchart byte fixtures are checked separately in FootPrint. Runtime dependencies remain empty. The original file history was retained with `git filter-repo`; see [extraction evidence](docs/extraction.md).
|
|
71
|
+
|
|
72
|
+
Record bytes are part of the versioning promise: refactors never re-pin; a named law fix requires a documented minor release; any other byte change requires a major. See the [fixture policy](test/fixtures/README.md).
|
|
73
|
+
|
|
74
|
+
MIT license. Contact: sanjay@footprintjs.dev.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** Read a recorded execution: replay, time travel, slicing and attribution. */
|
|
2
|
+
export type { CommitIdx, ExecutionCounter, ExecutionIndex, RuntimeStageId } from './lib/ids/runtimeStageId.js';
|
|
3
|
+
export { buildRuntimeStageId, createExecutionCounter, parseRuntimeStageId, splitStageId, } from './lib/ids/runtimeStageId.js';
|
|
4
|
+
export { UnknownVerbError } from './lib/memory/verbs.js';
|
|
5
|
+
export type { CommitBundle, MemoryPatch, TraceEntry } from './lib/memory/types.js';
|
|
6
|
+
export type { ExecutionTree } from './lib/memory/types.js';
|
|
7
|
+
export { applySmartMerge } from './lib/memory/verbs.js';
|
|
8
|
+
export type { ValueBasis, ValueBasisOptions, ValueWithBasis, WriterBasis, WriterWithBasis, } from './lib/memory/commitLogUtils.js';
|
|
9
|
+
export { buildCommitIndex, commitIndexOf, commitValueAt, commitValueAtWithBasis, findCommit, findCommits, findLastWriter, findLastWriterWithBasis, inferLegacyPhases, recordsPhases, } from './lib/memory/commitLogUtils.js';
|
|
10
|
+
export type { CommitPhase } from './lib/memory/types.js';
|
|
11
|
+
export type { AxisRefusal, AxisSplit, BookendedAxis, FoldBasis, FoldedState, FoldSource, LogGap, Mark, Move, MoveRefusal, Stop, StopFilter, StopKind, TimeTravel, TimeTravelOptions, TimeTravelSource, TimeTravelStrategy, } from './lib/time-travel/index.js';
|
|
12
|
+
export { commitStops, commitStopsStrategy, filterStops, isCommitBundle, splitAxis, stateAt, tagStops, timeTravel, } from './lib/time-travel/index.js';
|
|
13
|
+
export type { CausalChainOptions, CausalEdge, CausalNode, ControlDependency, ControlDepLookup, EdgeWeigher, KeysReadLookup, } from './lib/memory/backtrack.js';
|
|
14
|
+
export { causalChain, flattenCausalDAG, formatCausalChain } from './lib/memory/backtrack.js';
|
|
15
|
+
export type { UntrackedSource } from './lib/memory/types.js';
|
|
16
|
+
export type { ArrayProvenance, AttributionBasis, ElementBirth, FedBasis, ForwardEdge, ForwardNode, ForwardRead, ForwardSlice, ForwardSliceJSON, HonestyNote, HonestyNoteCode, KeyMoment, KeysReadSource, KeyTimeline, MissingProvenanceReason, MissingSliceReason, ReadsCoverage, SliceJSON, StateKey, VariableSlice, } from './lib/slice/index.js';
|
|
17
|
+
export type { ForwardSliceForKeyOptions, KeyTimelineOptions, SliceForKeyOptions } from './lib/slice/index.js';
|
|
18
|
+
export { arrayProvenance, elementProvenance, formatForwardSlice, formatSlice, formatTimeline, forwardSliceForKey, forwardSliceToJSON, keysReadFromExecutionTree, keysReadFromMap, keyTimeline, resolveKeysReadSource, sliceForKey, sliceToJSON, } from './lib/slice/index.js';
|
|
19
|
+
export type { HonestyCode } from './lib/memory/honesty.js';
|
|
20
|
+
export { HONESTY_CODES } from './lib/memory/honesty.js';
|
|
21
|
+
export type { RangeEntry, RangeToken } from './lib/recorder/CommitRangeIndex.js';
|
|
22
|
+
export { CommitRangeIndex } from './lib/recorder/CommitRangeIndex.js';
|
|
23
|
+
export type { CommitValuesMode } from './lib/memory/types.js';
|
|
24
|
+
export type { EmitSourcePosition, LogAddress } from './lib/memory/eventPosition.js';
|
|
25
|
+
export { EXECUTION_DELIMITER, PATH_DELIMITER, isExecutionKey, joinPath, pathSegments as idPathSegments, stageIdOf, subflowSegmentsOf, } from './lib/ids/runtimeStageId.js';
|
|
26
|
+
export { deepEqual } from './lib/memory/equality.js';
|
|
27
|
+
export { serveRecord } from './lib/capture/freeze.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { buildRuntimeStageId, createExecutionCounter, parseRuntimeStageId, splitStageId, } from './lib/ids/runtimeStageId.js';
|
|
2
|
+
export { UnknownVerbError } from './lib/memory/verbs.js';
|
|
3
|
+
export { applySmartMerge } from './lib/memory/verbs.js';
|
|
4
|
+
export { buildCommitIndex, commitIndexOf, commitValueAt, commitValueAtWithBasis, findCommit, findCommits, findLastWriter, findLastWriterWithBasis, inferLegacyPhases, recordsPhases, } from './lib/memory/commitLogUtils.js';
|
|
5
|
+
export { commitStops, commitStopsStrategy, filterStops, isCommitBundle, splitAxis, stateAt, tagStops, timeTravel, } from './lib/time-travel/index.js';
|
|
6
|
+
export { causalChain, flattenCausalDAG, formatCausalChain } from './lib/memory/backtrack.js';
|
|
7
|
+
export { arrayProvenance, elementProvenance, formatForwardSlice, formatSlice, formatTimeline, forwardSliceForKey, forwardSliceToJSON, keysReadFromExecutionTree, keysReadFromMap, keyTimeline, resolveKeysReadSource, sliceForKey, sliceToJSON, } from './lib/slice/index.js';
|
|
8
|
+
export { HONESTY_CODES } from './lib/memory/honesty.js';
|
|
9
|
+
export { CommitRangeIndex } from './lib/recorder/CommitRangeIndex.js';
|
|
10
|
+
export { EXECUTION_DELIMITER, PATH_DELIMITER, isExecutionKey, joinPath, pathSegments as idPathSegments, stageIdOf, subflowSegmentsOf, } from './lib/ids/runtimeStageId.js';
|
|
11
|
+
export { deepEqual } from './lib/memory/equality.js';
|
|
12
|
+
export { serveRecord } from './lib/capture/freeze.js';
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* freeze.ts — the ONE deep-freeze walk (moved here from `scope/protection/readonlyInput.ts` in F3),
|
|
3
|
+
* and the serve-time law for the commit log (9.44.2).
|
|
4
|
+
*
|
|
5
|
+
* WHY HERE. Saved trees freeze through this walk: the fold base served as
|
|
6
|
+
* `initialState` (`ExecutionRuntime · getFoldBase`), the
|
|
7
|
+
* dev-mode snapshot (`runner/snapshot.ts · servedSnapshot`), every state `stateAt` hands out
|
|
8
|
+
* (`time-travel/stateAt.ts`) and, since F3, every commit bundle (`EventLog · record`). `memory/` must
|
|
9
|
+
* not import `scope/` (that edge closed the memory ⇄ scope ⇄ recorder module cycle F0 removed), so the
|
|
10
|
+
* walk lives in this leaf folder, which imports nothing outside itself. Args use an ownership snapshot
|
|
11
|
+
* instead (`scope/protection/readonlyInput.ts · createFrozenArgs`): never freeze borrowed caller values.
|
|
12
|
+
*
|
|
13
|
+
* FREEZE WHAT CAN BE FROZEN, COPY WHAT CAN'T (9.44.2). `Object.freeze` cannot seal a Date's time, a
|
|
14
|
+
* Map's or Set's entries, a buffer's bytes, a RegExp (`compile()` rewrites a frozen one) or an Error
|
|
15
|
+
* (V8's own `stack` accessor writes through a frozen one). A fresh tree handed to one caller (a `stateAt`
|
|
16
|
+
* state, the dev-mode snapshot) is that caller's own, so freezing it is a guard and nothing more. The
|
|
17
|
+
* COMMIT LOG — each bundle, and the fold base — is served to every reader, so {@link freezeRecord}
|
|
18
|
+
* remembers that a record holds such a value, its first serve maps the containers on the way to each
|
|
19
|
+
* one (its open PATHS), and {@link serveRecord} hands a reader a copy of those paths only: a fresh copy
|
|
20
|
+
* of each such value, fresh frozen containers on the way to it, and every other part shared, frozen, as
|
|
21
|
+
* it is. Until 9.44.2 `snapshot.commitLog[i].overwrite.when.setTime(0)` rewrote every later snapshot,
|
|
22
|
+
* `stateAt`, `commitValueAt`, slice and cursor answer.
|
|
23
|
+
*
|
|
24
|
+
* Every walk here is ITERATIVE (an explicit stack): a record's depth is not bounded by the call stack.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import { deepFreeze, freezeRecord, serveRecord } from './freeze.js';
|
|
29
|
+
*
|
|
30
|
+
* const args = deepFreeze({ order: { lines: [{ sku: 'A' }] }, bytes: new Uint8Array([1]) });
|
|
31
|
+
* Object.isFrozen(args.order.lines[0]); // true
|
|
32
|
+
* Object.isFrozen(args.bytes); // false — a typed array cannot be frozen; it is skipped
|
|
33
|
+
*
|
|
34
|
+
* const record = freezeRecord({ when: new Date(0), tags: ['a'] });
|
|
35
|
+
* const served = serveRecord(record);
|
|
36
|
+
* served.when.setTime(5); // changes the reader's copy
|
|
37
|
+
* serveRecord(record).when.getTime(); // 0 — every serve is the record as recorded
|
|
38
|
+
* served.tags === record.tags; // true — the sealed part is shared, not copied
|
|
39
|
+
* serveRecord(freezeRecord({ n: 1 })); // a record freezing sealed whole: served as itself
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
/**
|
|
43
|
+
* How {@link deepFreeze} walks an ARRAY.
|
|
44
|
+
*
|
|
45
|
+
* - `'every-key'` (the default) — every own property, as for any other object: the index elements
|
|
46
|
+
* AND any expando (`arr.note = { … }`). The dev-mode snapshot relies on this contract.
|
|
47
|
+
* - `'indices'` — the index elements only, so no key string is allocated per element; this is what
|
|
48
|
+
* keeps freezing a 10,000-element commit inside its budget (`bench/element-writes.ts`). An object
|
|
49
|
+
* hung on an array EXPANDO is then left unfrozen, and unseen by {@link serveRecord} — the commit
|
|
50
|
+
* log's named hole (`EventLog · record`; an array expando is out of contract for state).
|
|
51
|
+
*/
|
|
52
|
+
export type ArrayWalk = 'every-key' | 'indices';
|
|
53
|
+
/**
|
|
54
|
+
* Freezes `obj` and every object reachable through its own properties, and returns `obj`.
|
|
55
|
+
*
|
|
56
|
+
* - An object that is ALREADY frozen is left as it is, and not descended — 9.32.0's contract, kept:
|
|
57
|
+
* args may hold a caller's frozen object with live parts inside (a class instance a stage calls),
|
|
58
|
+
* and freezing past it would break them. It is also what ends a cycle. The commit log never meets
|
|
59
|
+
* one: its payloads are fresh `structuredClone`s.
|
|
60
|
+
* - ArrayBuffer views (typed arrays, `DataView`) are skipped: `Object.freeze` throws on a non-empty
|
|
61
|
+
* typed array, so a `Uint8Array` in state, args or the fold base used to fail the run or the
|
|
62
|
+
* snapshot here.
|
|
63
|
+
* - What `Object.freeze` cannot reach stays mutable — Map and Set contents, a Date's time, the
|
|
64
|
+
* bytes behind a buffer. A frozen RegExp's `lastIndex` is read-only, so a `/g` or `/y` regex
|
|
65
|
+
* taken from a frozen tree throws when `exec` or `replace` advance it. The commit log goes through
|
|
66
|
+
* {@link freezeRecord} and {@link serveRecord} instead.
|
|
67
|
+
* - Functions are not descended (they are not values the engine stores).
|
|
68
|
+
*/
|
|
69
|
+
export declare function deepFreeze<T>(obj: T, arrays?: ArrayWalk): T;
|
|
70
|
+
/**
|
|
71
|
+
* {@link deepFreeze} for a record many readers are SERVED (a commit bundle, the fold base), and two more
|
|
72
|
+
* things on the same walk:
|
|
73
|
+
*
|
|
74
|
+
* - it remembers THAT the record holds a value freezing cannot seal — one flag; the record's first
|
|
75
|
+
* serve maps WHERE (the containers on the way to each such value, its open paths), so the run pays
|
|
76
|
+
* nothing more and {@link serveRecord} copies those paths and nothing else;
|
|
77
|
+
* - a typed array or `DataView` that views a SLICE of a bigger buffer (a Node `Buffer` views its
|
|
78
|
+
* shared pool) is replaced, in the record, by a copy of the bytes it views — the record keeps the
|
|
79
|
+
* view's bytes, never the rest of the buffer (a resizable or shared buffer is kept as it is).
|
|
80
|
+
*
|
|
81
|
+
* A part frozen before is not frozen past (deepFreeze's contract) but is looked through, so a value
|
|
82
|
+
* freezing cannot seal inside it still opens the record. Returns `record`.
|
|
83
|
+
*/
|
|
84
|
+
export declare function freezeRecord<T extends object>(record: T, arrays?: ArrayWalk): T;
|
|
85
|
+
/**
|
|
86
|
+
* The record as a reader may hold it — THE serve-time law: a record freezing sealed whole is served as
|
|
87
|
+
* itself (shared, no cost); any other as a copy of its open paths ({@link freezeRecord}): fresh frozen
|
|
88
|
+
* containers down to each value freezing cannot seal, a fresh copy of that value, and every other part
|
|
89
|
+
* shared, frozen, as it is. A copy is served the same way again.
|
|
90
|
+
*/
|
|
91
|
+
export declare function serveRecord<T>(record: T): T;
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* freeze.ts — the ONE deep-freeze walk (moved here from `scope/protection/readonlyInput.ts` in F3),
|
|
3
|
+
* and the serve-time law for the commit log (9.44.2).
|
|
4
|
+
*
|
|
5
|
+
* WHY HERE. Saved trees freeze through this walk: the fold base served as
|
|
6
|
+
* `initialState` (`ExecutionRuntime · getFoldBase`), the
|
|
7
|
+
* dev-mode snapshot (`runner/snapshot.ts · servedSnapshot`), every state `stateAt` hands out
|
|
8
|
+
* (`time-travel/stateAt.ts`) and, since F3, every commit bundle (`EventLog · record`). `memory/` must
|
|
9
|
+
* not import `scope/` (that edge closed the memory ⇄ scope ⇄ recorder module cycle F0 removed), so the
|
|
10
|
+
* walk lives in this leaf folder, which imports nothing outside itself. Args use an ownership snapshot
|
|
11
|
+
* instead (`scope/protection/readonlyInput.ts · createFrozenArgs`): never freeze borrowed caller values.
|
|
12
|
+
*
|
|
13
|
+
* FREEZE WHAT CAN BE FROZEN, COPY WHAT CAN'T (9.44.2). `Object.freeze` cannot seal a Date's time, a
|
|
14
|
+
* Map's or Set's entries, a buffer's bytes, a RegExp (`compile()` rewrites a frozen one) or an Error
|
|
15
|
+
* (V8's own `stack` accessor writes through a frozen one). A fresh tree handed to one caller (a `stateAt`
|
|
16
|
+
* state, the dev-mode snapshot) is that caller's own, so freezing it is a guard and nothing more. The
|
|
17
|
+
* COMMIT LOG — each bundle, and the fold base — is served to every reader, so {@link freezeRecord}
|
|
18
|
+
* remembers that a record holds such a value, its first serve maps the containers on the way to each
|
|
19
|
+
* one (its open PATHS), and {@link serveRecord} hands a reader a copy of those paths only: a fresh copy
|
|
20
|
+
* of each such value, fresh frozen containers on the way to it, and every other part shared, frozen, as
|
|
21
|
+
* it is. Until 9.44.2 `snapshot.commitLog[i].overwrite.when.setTime(0)` rewrote every later snapshot,
|
|
22
|
+
* `stateAt`, `commitValueAt`, slice and cursor answer.
|
|
23
|
+
*
|
|
24
|
+
* Every walk here is ITERATIVE (an explicit stack): a record's depth is not bounded by the call stack.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* import { deepFreeze, freezeRecord, serveRecord } from './freeze.js';
|
|
29
|
+
*
|
|
30
|
+
* const args = deepFreeze({ order: { lines: [{ sku: 'A' }] }, bytes: new Uint8Array([1]) });
|
|
31
|
+
* Object.isFrozen(args.order.lines[0]); // true
|
|
32
|
+
* Object.isFrozen(args.bytes); // false — a typed array cannot be frozen; it is skipped
|
|
33
|
+
*
|
|
34
|
+
* const record = freezeRecord({ when: new Date(0), tags: ['a'] });
|
|
35
|
+
* const served = serveRecord(record);
|
|
36
|
+
* served.when.setTime(5); // changes the reader's copy
|
|
37
|
+
* serveRecord(record).when.getTime(); // 0 — every serve is the record as recorded
|
|
38
|
+
* served.tags === record.tags; // true — the sealed part is shared, not copied
|
|
39
|
+
* serveRecord(freezeRecord({ n: 1 })); // a record freezing sealed whole: served as itself
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
import { putOwn } from './ownData.js';
|
|
43
|
+
import { kindOf, SEALABLE } from './valueKinds.js';
|
|
44
|
+
/**
|
|
45
|
+
* Freezes `obj` and every object reachable through its own properties, and returns `obj`.
|
|
46
|
+
*
|
|
47
|
+
* - An object that is ALREADY frozen is left as it is, and not descended — 9.32.0's contract, kept:
|
|
48
|
+
* args may hold a caller's frozen object with live parts inside (a class instance a stage calls),
|
|
49
|
+
* and freezing past it would break them. It is also what ends a cycle. The commit log never meets
|
|
50
|
+
* one: its payloads are fresh `structuredClone`s.
|
|
51
|
+
* - ArrayBuffer views (typed arrays, `DataView`) are skipped: `Object.freeze` throws on a non-empty
|
|
52
|
+
* typed array, so a `Uint8Array` in state, args or the fold base used to fail the run or the
|
|
53
|
+
* snapshot here.
|
|
54
|
+
* - What `Object.freeze` cannot reach stays mutable — Map and Set contents, a Date's time, the
|
|
55
|
+
* bytes behind a buffer. A frozen RegExp's `lastIndex` is read-only, so a `/g` or `/y` regex
|
|
56
|
+
* taken from a frozen tree throws when `exec` or `replace` advance it. The commit log goes through
|
|
57
|
+
* {@link freezeRecord} and {@link serveRecord} instead.
|
|
58
|
+
* - Functions are not descended (they are not values the engine stores).
|
|
59
|
+
*/
|
|
60
|
+
export function deepFreeze(obj, arrays = 'every-key') {
|
|
61
|
+
if (obj !== null && typeof obj === 'object')
|
|
62
|
+
walkAndFreeze(obj, arrays === 'indices', false);
|
|
63
|
+
return obj;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Containers of a record that hold — on some path below them — a value freezing cannot seal: `true` once
|
|
67
|
+
* mapped (the container is on an open path), `false` for a record {@link freezeRecord} found open whose
|
|
68
|
+
* paths no serve has mapped yet. A container absent here is sealed whole.
|
|
69
|
+
*/
|
|
70
|
+
const OPEN = new WeakMap();
|
|
71
|
+
/**
|
|
72
|
+
* {@link deepFreeze} for a record many readers are SERVED (a commit bundle, the fold base), and two more
|
|
73
|
+
* things on the same walk:
|
|
74
|
+
*
|
|
75
|
+
* - it remembers THAT the record holds a value freezing cannot seal — one flag; the record's first
|
|
76
|
+
* serve maps WHERE (the containers on the way to each such value, its open paths), so the run pays
|
|
77
|
+
* nothing more and {@link serveRecord} copies those paths and nothing else;
|
|
78
|
+
* - a typed array or `DataView` that views a SLICE of a bigger buffer (a Node `Buffer` views its
|
|
79
|
+
* shared pool) is replaced, in the record, by a copy of the bytes it views — the record keeps the
|
|
80
|
+
* view's bytes, never the rest of the buffer (a resizable or shared buffer is kept as it is).
|
|
81
|
+
*
|
|
82
|
+
* A part frozen before is not frozen past (deepFreeze's contract) but is looked through, so a value
|
|
83
|
+
* freezing cannot seal inside it still opens the record. Returns `record`.
|
|
84
|
+
*/
|
|
85
|
+
export function freezeRecord(record, arrays = 'every-key') {
|
|
86
|
+
if (!OPEN.has(record) && walkAndFreeze(record, arrays === 'indices', true))
|
|
87
|
+
OPEN.set(record, false);
|
|
88
|
+
return record;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The record as a reader may hold it — THE serve-time law: a record freezing sealed whole is served as
|
|
92
|
+
* itself (shared, no cost); any other as a copy of its open paths ({@link freezeRecord}): fresh frozen
|
|
93
|
+
* containers down to each value freezing cannot seal, a fresh copy of that value, and every other part
|
|
94
|
+
* shared, frozen, as it is. A copy is served the same way again.
|
|
95
|
+
*/
|
|
96
|
+
export function serveRecord(record) {
|
|
97
|
+
if (record === null || typeof record !== 'object')
|
|
98
|
+
return record;
|
|
99
|
+
const mapped = OPEN.get(record);
|
|
100
|
+
if (mapped === undefined)
|
|
101
|
+
return record;
|
|
102
|
+
if (!mapped)
|
|
103
|
+
mapOpenPaths(record);
|
|
104
|
+
return OPEN.get(record) === true ? copyOpenPaths(record) : record;
|
|
105
|
+
}
|
|
106
|
+
/** A value freezing cannot seal — a view, or a kind `SEALABLE` says no to (a Date, a Map, an Error …). */
|
|
107
|
+
function isUnsealable(value) {
|
|
108
|
+
return ArrayBuffer.isView(value) || !SEALABLE[kindOf(value)];
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Freeze `root` and every container under it, iteratively; `record` adds the look-through of parts
|
|
112
|
+
* frozen before and the compaction of views over a slice. `true` when something under `root` (or `root`
|
|
113
|
+
* itself) cannot be sealed.
|
|
114
|
+
*/
|
|
115
|
+
function walkAndFreeze(root, indices, record) {
|
|
116
|
+
let open = false;
|
|
117
|
+
let lookedThrough;
|
|
118
|
+
const stack = [root];
|
|
119
|
+
while (stack.length > 0) {
|
|
120
|
+
const node = stack.pop();
|
|
121
|
+
if (ArrayBuffer.isView(node)) {
|
|
122
|
+
open = true; // its bytes — and a non-empty one cannot be frozen at all
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
const frozenBefore = Object.isFrozen(node);
|
|
126
|
+
if (frozenBefore) {
|
|
127
|
+
if (!record || OPEN.has(node)) {
|
|
128
|
+
open ||= record;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if ((lookedThrough ??= new Set()).has(node))
|
|
132
|
+
continue;
|
|
133
|
+
lookedThrough.add(node); // a part frozen before: looked through, never frozen past
|
|
134
|
+
}
|
|
135
|
+
if (record && !open && !SEALABLE[kindOf(node)])
|
|
136
|
+
open = true;
|
|
137
|
+
// Children first, while `node` is still open to the compaction below; then the freeze.
|
|
138
|
+
const compacting = record && !frozenBefore;
|
|
139
|
+
if (indices && Array.isArray(node)) {
|
|
140
|
+
for (let i = 0; i < node.length; i++) {
|
|
141
|
+
const child = node[i];
|
|
142
|
+
if (child === null || typeof child !== 'object')
|
|
143
|
+
continue;
|
|
144
|
+
stack.push(compacting && ArrayBuffer.isView(child) ? compact(node, i, child) : child);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
const names = Object.getOwnPropertyNames(node);
|
|
149
|
+
for (let i = 0; i < names.length; i++) {
|
|
150
|
+
const child = node[names[i]];
|
|
151
|
+
if (child === null || typeof child !== 'object')
|
|
152
|
+
continue;
|
|
153
|
+
stack.push(compacting && ArrayBuffer.isView(child) ? compact(node, names[i], child) : child);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (!frozenBefore)
|
|
157
|
+
Object.freeze(node);
|
|
158
|
+
}
|
|
159
|
+
return open;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* A view over a SLICE of a bigger buffer, replaced in `container` by a copy of the bytes it views
|
|
163
|
+
* (the type it is — a Node `Buffer` comes out a `Uint8Array`, as a clone of it does). Anything else is
|
|
164
|
+
* returned as it is. A resizable or shared buffer is kept: a copy would change what it is.
|
|
165
|
+
*/
|
|
166
|
+
function compact(container, key, child) {
|
|
167
|
+
const buffer = child.buffer;
|
|
168
|
+
if (child.byteOffset === 0 && child.byteLength === buffer.byteLength)
|
|
169
|
+
return child;
|
|
170
|
+
if (buffer.resizable === true || !(buffer instanceof ArrayBuffer))
|
|
171
|
+
return child;
|
|
172
|
+
const bytes = buffer.slice(child.byteOffset, child.byteOffset + child.byteLength);
|
|
173
|
+
const tag = Object.prototype.toString.call(child).slice(8, -1);
|
|
174
|
+
const Ctor = globalThis[tag];
|
|
175
|
+
if (typeof Ctor !== 'function')
|
|
176
|
+
return child;
|
|
177
|
+
const copy = new Ctor(bytes);
|
|
178
|
+
try {
|
|
179
|
+
container[key] = copy;
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return child; // a property the record does not let be replaced (a hand-built bundle)
|
|
183
|
+
}
|
|
184
|
+
return copy;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Map the open paths of `root` — mark every container that reaches a value freezing cannot seal, so a
|
|
188
|
+
* serve copies exactly those (DAGs and cycles included: the marks run UP the reverse edges from each
|
|
189
|
+
* such value). Runs once per open record, at its first serve. A node's first holder is kept in one map;
|
|
190
|
+
* any other holder (a shared part, a cycle) in a second, made only when there is one.
|
|
191
|
+
*/
|
|
192
|
+
function mapOpenPaths(root) {
|
|
193
|
+
const holder = new Map();
|
|
194
|
+
let moreHolders;
|
|
195
|
+
const leaves = [];
|
|
196
|
+
const stack = [root];
|
|
197
|
+
while (stack.length > 0) {
|
|
198
|
+
const node = stack.pop();
|
|
199
|
+
if (isUnsealable(node)) {
|
|
200
|
+
leaves.push(node); // a leaf: its contents are copied with it
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
const names = Object.keys(node);
|
|
204
|
+
for (let i = 0; i < names.length; i++) {
|
|
205
|
+
const child = node[names[i]];
|
|
206
|
+
if (child === null || typeof child !== 'object')
|
|
207
|
+
continue;
|
|
208
|
+
if (child !== root && !holder.has(child)) {
|
|
209
|
+
holder.set(child, node);
|
|
210
|
+
stack.push(child);
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
const more = (moreHolders ??= new Map()).get(child);
|
|
214
|
+
if (more === undefined)
|
|
215
|
+
moreHolders.set(child, [node]);
|
|
216
|
+
else
|
|
217
|
+
more.push(node);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
// Marked in THIS map, not by an earlier one: a part another record already mapped still opens
|
|
221
|
+
// every container of this record on the way to it.
|
|
222
|
+
const marked = new Set();
|
|
223
|
+
const up = [];
|
|
224
|
+
const holdersOf = (node) => {
|
|
225
|
+
const first = holder.get(node);
|
|
226
|
+
if (first !== undefined)
|
|
227
|
+
up.push(first);
|
|
228
|
+
const more = moreHolders?.get(node);
|
|
229
|
+
// One push per holder: a spread passes every holder as a call argument, and one object held by
|
|
230
|
+
// 120,000 containers overflowed the stack.
|
|
231
|
+
if (more !== undefined)
|
|
232
|
+
for (let i = 0; i < more.length; i++)
|
|
233
|
+
up.push(more[i]);
|
|
234
|
+
};
|
|
235
|
+
for (const leaf of leaves) {
|
|
236
|
+
if (leaf === root)
|
|
237
|
+
marked.add(root);
|
|
238
|
+
holdersOf(leaf);
|
|
239
|
+
}
|
|
240
|
+
while (up.length > 0) {
|
|
241
|
+
const node = up.pop();
|
|
242
|
+
if (marked.has(node))
|
|
243
|
+
continue;
|
|
244
|
+
marked.add(node);
|
|
245
|
+
holdersOf(node);
|
|
246
|
+
}
|
|
247
|
+
// Only now, the walk done, does the record's flag give way to its map: a mapping that throws leaves
|
|
248
|
+
// the record flagged, so the next serve maps again — it never hands out the record itself.
|
|
249
|
+
if (!marked.has(root))
|
|
250
|
+
OPEN.delete(root);
|
|
251
|
+
for (const node of marked)
|
|
252
|
+
OPEN.set(node, true);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* The served copy of an open record: its open containers copied (fresh, then frozen, and themselves
|
|
256
|
+
* open — a copy is served by copy again), each value freezing cannot seal copied, every sealed part
|
|
257
|
+
* shared. Iterative; one copy per original, so containers shared or cyclic stay shared in the copy. The
|
|
258
|
+
* values freezing cannot seal are copied ONE BY ONE: two of them that share a part (an `ArrayBuffer`
|
|
259
|
+
* and a view of it, an error whose `cause` is another key's Date) come out as separate copies — equal
|
|
260
|
+
* values, the sharing between them not kept.
|
|
261
|
+
*/
|
|
262
|
+
function copyOpenPaths(root) {
|
|
263
|
+
const copies = new Map();
|
|
264
|
+
const shells = [];
|
|
265
|
+
const copyOf = (value, pending) => {
|
|
266
|
+
const done = copies.get(value);
|
|
267
|
+
if (done !== undefined)
|
|
268
|
+
return done;
|
|
269
|
+
let copy;
|
|
270
|
+
if (isUnsealable(value)) {
|
|
271
|
+
copy = copyLeaf(value);
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
copy = Array.isArray(value) ? new Array(value.length) : {};
|
|
275
|
+
shells.push(copy);
|
|
276
|
+
pending.push(value);
|
|
277
|
+
}
|
|
278
|
+
copies.set(value, copy);
|
|
279
|
+
return copy;
|
|
280
|
+
};
|
|
281
|
+
const pending = [];
|
|
282
|
+
const out = copyOf(root, pending);
|
|
283
|
+
while (pending.length > 0) {
|
|
284
|
+
const node = pending.pop();
|
|
285
|
+
const copy = copies.get(node);
|
|
286
|
+
for (const name of Object.keys(node)) {
|
|
287
|
+
const child = node[name];
|
|
288
|
+
putOwn(copy, name, child !== null && typeof child === 'object' && (OPEN.has(child) || isUnsealable(child))
|
|
289
|
+
? copyOf(child, pending)
|
|
290
|
+
: child);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
for (const shell of shells) {
|
|
294
|
+
Object.freeze(shell);
|
|
295
|
+
OPEN.set(shell, true);
|
|
296
|
+
}
|
|
297
|
+
// A leaf copy is the reader's own; frozen where freezing reaches, as the record's parts are.
|
|
298
|
+
for (const copy of copies.values())
|
|
299
|
+
if (!ArrayBuffer.isView(copy))
|
|
300
|
+
Object.freeze(copy);
|
|
301
|
+
return out;
|
|
302
|
+
}
|
|
303
|
+
/** A fresh copy of a value freezing cannot seal. One the clone refuses (hand-built) is served as it is. */
|
|
304
|
+
function copyLeaf(value) {
|
|
305
|
+
if (kindOf(value) === 'date')
|
|
306
|
+
return new Date(Date.prototype.getTime.call(value));
|
|
307
|
+
try {
|
|
308
|
+
return structuredClone(value);
|
|
309
|
+
}
|
|
310
|
+
catch {
|
|
311
|
+
return value;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The private L0 owner of own-slot writes. Selector validation belongs to
|
|
3
|
+
* pathOps: payload names (including `__proto__`) are ordinary data here.
|
|
4
|
+
* Neither operation flattens an existing own accessor or changes its flags.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* `target[name] = value`, preserving existing own-slot behavior while making
|
|
8
|
+
* a missing inherited name an OWN data property. Payload names such as
|
|
9
|
+
* `__proto__` are data, not setters or selector instructions. Shared by the
|
|
10
|
+
* record-serving and copy-on-write copies; this is not a public helper.
|
|
11
|
+
*/
|
|
12
|
+
export declare function putOwn(target: Record<string, unknown>, name: string | number, value: unknown): void;
|
|
13
|
+
/**
|
|
14
|
+
* Nested diagnostic writers require a missing slot to be CREATED, not merely
|
|
15
|
+
* assigned. Keep that contract separate from putOwn: an out-of-range typed
|
|
16
|
+
* array index is an assignment no-op, but cannot be defined as an own slot.
|
|
17
|
+
*/
|
|
18
|
+
export declare function setOwnValue(target: Record<string, unknown>, name: string | number, value: unknown): void;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The private L0 owner of own-slot writes. Selector validation belongs to
|
|
3
|
+
* pathOps: payload names (including `__proto__`) are ordinary data here.
|
|
4
|
+
* Neither operation flattens an existing own accessor or changes its flags.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* `target[name] = value`, preserving existing own-slot behavior while making
|
|
8
|
+
* a missing inherited name an OWN data property. Payload names such as
|
|
9
|
+
* `__proto__` are data, not setters or selector instructions. Shared by the
|
|
10
|
+
* record-serving and copy-on-write copies; this is not a public helper.
|
|
11
|
+
*/
|
|
12
|
+
export function putOwn(target, name, value) {
|
|
13
|
+
if (!Object.prototype.hasOwnProperty.call(target, name) && Reflect.has(target, name)) {
|
|
14
|
+
defineOwnData(target, name, value);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
target[name] = value;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Nested diagnostic writers require a missing slot to be CREATED, not merely
|
|
22
|
+
* assigned. Keep that contract separate from putOwn: an out-of-range typed
|
|
23
|
+
* array index is an assignment no-op, but cannot be defined as an own slot.
|
|
24
|
+
*/
|
|
25
|
+
export function setOwnValue(target, name, value) {
|
|
26
|
+
if (Object.prototype.hasOwnProperty.call(target, name)) {
|
|
27
|
+
putOwn(target, name, value);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
defineOwnData(target, name, value);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function defineOwnData(target, name, value) {
|
|
34
|
+
Object.defineProperty(target, name, { value, enumerable: true, writable: true, configurable: true });
|
|
35
|
+
}
|