Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | 14x 14x 2x 14x 3x 3x 1x 14x 50x 50x 72x 5x 72x 72x 72x 30x 26x 30x 27x 21x 6x 25x 13x 25x 17x 9x 8x 8x 14x 14x 7x 7x 8x 7x 8x 51x 51x 51x 5x 5x 5x 2x 3x 2x 1x 1x 5x | /** @typedef {import('./Tracer').TraceRecord} TraceRecord */
/**
* A node of the reassembled causal tree returned by
* {@link InMemorySink#toTree}.
*
* @typedef {Object} TraceTreeNode
* @property {TraceRecord} record
* @property {TraceTreeNode[]} children
*/
/**
* Render one record as a tree-node label: the trigram, its producing phase
* when present, and (optionally) its captured data.
*
* @param {TraceRecord} record
* @param {boolean} showData
* @returns {string}
*/
function nodeLabel(record, showData) {
let label = `☯ {${record.t}, ${record.a}, ${record.o}}`;
if (record.via) {
label += ` ·${record.via}`;
}
if (showData && typeof record.data !== 'undefined') {
try {
label += ` ${JSON.stringify(record.data)}`;
} catch {
label += ' [unserializable data]';
}
}
return label;
}
/**
* Collects signal records in memory and reassembles them into causal trees.
* `format()` renders an ASCII tree — a compact, product-language map of what
* the application actually did, suitable for tests, debugging, and agents.
*
* @export
* @class InMemorySink
*/
export default class InMemorySink {
/**
* Creates an instance of InMemorySink.
* @param {Object} [opts]
* @param {number} [opts.limit=10000] - ring-buffer capacity: at capacity
* the oldest record is evicted, and its orphaned children surface
* as roots
* @memberof InMemorySink
*/
constructor({ limit = 10000 } = {}) {
this._limit = limit;
this.clear();
}
/**
* Sink interface: retain one dispatched signal and index it by id and by
* parent.
*
* @param {TraceRecord} record
* @memberof InMemorySink
*/
signal(record) {
if (this._records.length >= this._limit) {
this._evictOldest();
}
this._records.push(record);
this._byId.set(record.signalId, record);
if (record.parentId) {
if (!this._children.has(record.parentId)) {
this._children.set(record.parentId, []);
}
this._children.get(record.parentId).push(record);
}
}
/**
* All retained records in arrival order (a fresh copy).
*
* @type {TraceRecord[]}
* @readonly
* @memberof InMemorySink
*/
get records() {
return [...this._records];
}
/**
* Number of records currently retained.
*
* @type {number}
* @readonly
* @memberof InMemorySink
*/
get size() {
return this._records.length;
}
/**
* Look up a retained record by its signal id.
*
* @param {string} signalId
* @returns {TraceRecord|undefined} undefined when never seen or evicted
* @memberof InMemorySink
*/
byId(signalId) {
return this._byId.get(signalId);
}
/**
* Records chained by the given signal's handlers, in arrival order
* (a fresh copy).
*
* @param {string} signalId
* @returns {TraceRecord[]}
* @memberof InMemorySink
*/
childrenOf(signalId) {
return [...(this._children.get(signalId) || [])];
}
/**
* Local roots: records with no parent, or whose parent was never seen
* locally (remote continuation) or has been evicted.
*
* @returns {TraceRecord[]}
* @memberof InMemorySink
*/
roots() {
return this._records.filter(
(record) => !record.parentId || !this._byId.has(record.parentId),
);
}
/**
* Reassemble the retained records into causal trees, one per local root.
*
* @returns {TraceTreeNode[]}
* @memberof InMemorySink
*/
toTree() {
const toNode = (record) => ({
record,
children: this.childrenOf(record.signalId).map(toNode),
});
return this.roots().map(toNode);
}
/**
* Render the causal trees as an ASCII tree, one line per signal.
*
* @param {Object} [opts]
* @param {boolean} [opts.showData=false] - append each record's captured
* data to its label
* @returns {string}
* @memberof InMemorySink
*/
format({ showData = false } = {}) {
const lines = [];
const walk = (node, prefix, childPrefix) => {
lines.push(prefix + nodeLabel(node.record, showData));
node.children.forEach((child, i) => {
const last = i === node.children.length - 1;
walk(
child,
childPrefix + (last ? '└── ' : '├── '),
childPrefix + (last ? ' ' : '│ '),
);
});
};
for (const root of this.toTree()) {
walk(root, '', '');
}
return lines.join('\n');
}
/**
* Drop every retained record and index.
*
* @memberof InMemorySink
*/
clear() {
this._records = [];
this._byId = new Map();
this._children = new Map();
}
/**
* Evict the oldest record (ring-buffer overflow), unindexing it from its
* parent's children and releasing its own child index.
*/
_evictOldest() {
const evicted = this._records.shift();
this._byId.delete(evicted.signalId);
if (evicted.parentId && this._children.has(evicted.parentId)) {
const siblings = this._children
.get(evicted.parentId)
.filter((record) => record !== evicted);
// Stryker disable next-line ConditionalExpression: keeping an empty siblings list is observationally identical to deleting it
if (siblings.length) {
this._children.set(evicted.parentId, siblings);
} else {
this._children.delete(evicted.parentId);
}
}
// the evicted signal's children (if any) surface as roots via roots()
this._children.delete(evicted.signalId);
}
}
|