Repository navigation
Expand file tree
/
Copy pathdeveloper-guide.html
More file actions
335 lines (319 loc) · 41.3 KB
/
Copy pathdeveloper-guide.html
File metadata and controls
335 lines (319 loc) · 41.3 KB
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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Siteation_DebugBar developer guide</title>
<style>
:root {
color-scheme: light dark;
--bg: #f6f7f9;
--surface: #ffffff;
--surface-2: #f0f2f5;
--border: #dfe3e8;
--text: #14181f;
--text-dim: #5b6572;
--text-faint: #8a94a2;
--accent: #3159d8;
--mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
--sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--bg: #0e1116;
--surface: #161b22;
--surface-2: #1c222b;
--border: #2a313c;
--text: #e6e9ee;
--text-dim: #9aa4b2;
--text-faint: #6e7885;
--accent: #7f9cf5;
}
}
* { box-sizing: border-box; }
body {
margin: 0;
padding: 2.5rem 1.25rem 6rem;
background: var(--bg);
color: var(--text);
font-family: var(--sans);
font-size: 15px;
line-height: 1.6;
-webkit-font-smoothing: antialiased;
}
main { max-width: 48rem; margin: 0 auto; }
nav.family {
max-width: 48rem;
margin: 0 auto 2.5rem;
display: flex;
gap: 1rem;
font-size: 0.85rem;
color: var(--text-faint);
}
nav.family a { color: var(--text-dim); }
h1 { font-size: 1.9rem; line-height: 1.2; margin: 0 0 1.5rem; letter-spacing: -0.02em; }
h2 {
font-size: 1.25rem;
margin: 3rem 0 0.75rem;
padding-top: 1.5rem;
border-top: 1px solid var(--border);
letter-spacing: -0.01em;
}
h3 { font-size: 1rem; margin: 2rem 0 0.5rem; }
h4 { font-size: 0.95rem; margin: 1.5rem 0 0.5rem; color: var(--text-dim); }
p, ul, ol, table { margin: 0 0 1rem; }
li { margin-bottom: 0.35rem; }
li > code:first-child { font-weight: 600; }
a { color: var(--accent); }
img {
display: block;
max-width: 100%;
height: auto;
margin: 0 0 1.25rem;
border: 1px solid var(--border);
border-radius: 8px;
}
a.anchor { color: inherit; text-decoration: none; }
a.anchor:hover { text-decoration: underline; }
code {
font-family: var(--mono);
font-size: 0.86em;
background: var(--surface-2);
border-radius: 4px;
padding: 0.1em 0.35em;
}
pre {
background: var(--surface);
border: 1px solid var(--border);
border-radius: 8px;
padding: 0.9rem 1rem;
overflow-x: auto;
margin: 0 0 1.25rem;
}
pre code { background: none; padding: 0; font-size: 0.83rem; line-height: 1.55; }
table { width: 100%; border-collapse: collapse; font-size: 0.92rem; display: block; overflow-x: auto; }
th, td { text-align: left; padding: 0.5rem 0.75rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--text-dim); font-weight: 600; white-space: nowrap; }
footer {
max-width: 48rem;
margin: 4rem auto 0;
padding-top: 1.5rem;
border-top: 1px solid var(--border);
color: var(--text-faint);
font-size: 0.85rem;
}
</style>
</head>
<body>
<nav class="family">
<a href="user-guide.html">User guide</a>
<a href="SKILL.md">Agent skill</a>
<a href="https://github.com/Siteation/magento2-debugbar">Repository</a>
</nav>
<main>
<h1 id="developer-guide"><a class="anchor" href="#developer-guide">Developer guide</a></h1>
<p>For working on Siteation_DebugBar itself. If you want to use it, read the <a href="user-guide.html">user guide</a>.</p>
<h2 id="layout"><a class="anchor" href="#layout">Layout</a></h2>
<table><tbody><tr><td><code>Api/</code></td><td><code>CollectorInterface</code>, <code>RuleInterface</code>, <code>McpToolInterface</code>, <code>SectionViewInterface</code></td></tr><tr><td><code>Plugin/</code></td><td>The entry hook and the seven collection hooks</td></tr><tr><td><code>Collector/</code></td><td>Ten collectors and the abstract they share</td></tr><tr><td><code>Analysis/</code></td><td>Findings, rules, the query analyzer, the timeline, the comparer</td></tr><tr><td><code>Model/</code></td><td>Config, the gate, the access key, the store, the redactor, the layout file lookup, SEO sources, the admin edit link, the sections other modules add</td></tr><tr><td><code>Presentation/</code></td><td>Bar injection, the markdown report, the MCP presenter</td></tr><tr><td><code>Mcp/</code></td><td>JSON-RPC server and six tools</td></tr><tr><td><code>Controller/</code></td><td><code>Profile/</code>: <code>view</code>, <code>history</code>, <code>compare</code>; <code>Adminhtml/Entity/Edit</code>: the admin edit link</td></tr><tr><td><code>Console/</code></td><td><code>siteation:debugbar:mcp</code>, <code>siteation:debugbar:report</code></td></tr><tr><td><code>src-js/</code></td><td>Alpine sources for the bar; build output is committed</td></tr><tr><td><code>view/base/web/</code></td><td>The committed build output</td></tr><tr><td><code>dev/</code></td><td><code>smoke</code>, an HTTP test over a running store, <code>docs</code>, the guide renderer, and <code>fixture-section</code>, a module that adds a section for the browser suite</td></tr></tbody></table>
<p>PHP is edited in place. The package installs as a Composer path symlink, so nothing needs reinstalling after an edit.</p>
<h2 id="how-a-request-becomes-a-profile"><a class="anchor" href="#how-a-request-becomes-a-profile">How a request becomes a profile</a></h2>
<p><strong>One entry hook.</strong> An <code>around</code> plugin on <code>Magento\Framework\App\Http::launch()</code> brackets every web request in every area.</p>
<p>It has to be <code>around</code>. <code>launch()</code> switches the object manager to the request's area, <code>PluginList</code> reloads its scoped data as it does so, and an <code>after</code> listener does not survive that reload: it dies inside <code>getPlugin()</code>. There is no ordering trick that fixes it; the bracket has to be taken before the switch.</p>
<p>In order:</p>
<ol>
<li><code>RequestEligibility::allows()</code> decides whether to profile at all. It reads the config flag, the area list, the IP allowlist, the access key, and whether this is one of the bar's own endpoints.</li>
<li><code>ProfileManager</code> starts collecting. Every plugin records through it, but branches on <code>CollectionState</code>, a dependency free object holding a single boolean, so an unprofiled request pays a plugin hop and no config read. The plugins hold the manager and their collectors behind proxies in <code>etc/di.xml</code>, which are only built once the state says yes: a request that is not profiled constructs none of them. A new plugin follows the same rule (<code>LazyPluginsTest</code> checks it), and one that is built by hand takes the state first.</li>
<li><code>proceed()</code> runs the application. The query, event, observer, block, cache, layout and page cache plugins record as they fire.</li>
<li><code>finalize()</code> asks each enabled collector for its section, builds the timeline, runs the rules, and hands the result to <code>ProfileStore</code>.</li>
<li><code>BarInjector</code> sets the profile header, marks the response unshareable, and injects the bar if the response can carry one.</li>
</ol>
<p><strong>Nothing may read configuration while a request is being collected.</strong> A config read can miss the cache, a cache miss issues queries and dispatches events, and the query, cache and event collectors all call back in. <code>Config</code> resolves every value once in one guarded pass, at launch, before any collector is active.</p>
<p><strong>Never break the page being debugged.</strong> Every collector call and the whole finalize step is wrapped. A throw discards the profile and returns the untouched response.</p>
<h2 id="adding-a-collector"><a class="anchor" href="#adding-a-collector">Adding a collector</a></h2>
<p>Implement <code>Api\CollectorInterface</code>, extend <code>Collector\AbstractCollector</code>, and register it in the sorted array on <code>ProfileManager</code> in <code>etc/di.xml</code>.</p>
<p><code>AbstractCollector</code> does redaction, bounding and <code>at_ms</code> stamping once for all of them. Do not do any of those in your collector; a collector that bypasses the abstract is how the contract stops meaning anything.</p>
<p>Recording is done through <code>ProfileManager::quietly()</code>, which is also where the "is this section switched on" decision is honoured, so a section that is off costs nothing rather than being gathered and hidden.</p>
<p>Add the section to <code>src-js/src/sections.js</code> as well, or the panel will never be drawn. <code>Test/Unit/Analysis/RuleSectionTest.php</code> reads that list from PHP, so the two cannot drift.</p>
<h2 id="adding-a-section"><a class="anchor" href="#adding-a-section">Adding a section</a></h2>
<p>A section is what the sidebar lists. It does not need a collector: Alpine, Magewire, the data layer and Web Vitals read the live page and have none.</p>
<ol>
<li>An entry in <code>SECTIONS</code> in <code>src-js/src/sections.js</code>: <code>id</code>, <code>label</code>, a <code>lead</code> that says what the panel is for, and <code>graded: false</code> when no rule can produce a finding for it.</li>
<li>Its badge in <code>countFor()</code> in the same file, or <code>null</code> for none.</li>
<li>Its panel in <code>src-js/src/template.js</code>, inside <code>isSection('<id>')</code>. Sub views use <code>subTabs()</code> from <code>tabs.js</code>.</li>
<li>An option in <code>Model\Config\Source\Sections</code>, with the same id, so it can be switched off. Findings and Overview are left out of that list on purpose.</li>
</ol>
<p>The browser suite walks every section and every sub view, so a new one is checked for a binding that throws as soon as it is in the list. That walk does not wait for the section's rows or look at them, so a section that renders empty passes it. Give a new section a test of its own that waits for its items and checks what they show. A section that reads something the test store lacks, as Magewire does, gets a stand-in installed before the page's scripts.</p>
<h2 id="a-section-from-another-module"><a class="anchor" href="#a-section-from-another-module">A section from another module</a></h2>
<p>Everything above is for sections that ship with the bar. Another module, such as an integration with an ERP, adds its own without touching this package: a collector, rules and MCP tools register through the same DI arrays as the built in ones, and the panel is declared on <code>Model\ContributedSections</code>.</p>
<pre><code><type name="Siteation\DebugBar\Model\ContributedSections">
<arguments>
<argument name="sections" xsi:type="array">
<item name="c365" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Commerce365</item>
<item name="lead" xsi:type="string" translate="true">What the page was priced with.</item>
<item name="graded" xsi:type="boolean">true</item>
<item name="view" xsi:type="object">Vendor\Module\Presentation\PricesView</item>
<item name="scripts" xsi:type="array">
<item name="head" xsi:type="array">
<item name="events" xsi:type="string">Vendor_Module::js/early.js</item>
</item>
<item name="body" xsi:type="array">
<item name="reader" xsi:type="string">Vendor_Module::js/section.js</item>
</item>
</item>
</item>
</argument>
</arguments>
</type></code></pre>
<p>The item name is the section id: lower case letters, digits and underscores, and not one the bar already uses. A collector feeding the section is registered on <code>ProfileManager</code> under the same id, because that id is what the <strong>Sections</strong> setting switches. Only <code>label</code> is required. A declaration that is wrong is left out and logged rather than breaking the bar.</p>
<p><strong>The bar cannot draw another module's markup.</strong> It runs Alpine's CSP build in a shadow root, so a template with expressions of its own would neither evaluate nor be safe. A section describes its panel as blocks instead, and the bar draws them with its own markup and styles. A panel is <code>{blocks: [...]}</code>, or <code>{tabs: [{id, label, count, blocks}]}</code> for sub views, with an optional <code>count</code> for the sidebar badge.</p>
<table><thead><tr><th>Block</th><th>Fields</th></tr></thead><tbody><tr><td><code>heading</code></td><td><code>text</code></td></tr><tr><td><code>note</code></td><td><code>text</code>, <code>tone</code></td></tr><tr><td><code>list</code></td><td><code>items</code>, strings</td></tr><tr><td><code>facts</code></td><td><code>items</code>: <code>label</code>, <code>value</code>, <code>tone</code>, <code>mono</code></td></tr><tr><td><code>table</code></td><td><code>columns</code>: a key, or <code>key</code>, <code>label</code>, <code>num</code>, <code>mono</code>; <code>rows</code>: objects by key or arrays in column order, a cell either a value or <code>{text, tone}</code>, a row's <code>_tone</code>, and <code>_target</code> for the outline; <code>empty</code></td></tr><tr><td><code>code</code></td><td><code>text</code>, <code>language</code> (<code>json</code>, <code>sql</code> or <code>javascript</code>)</td></tr><tr><td><code>json</code></td><td><code>value</code>, drawn as highlighted JSON</td></tr></tbody></table>
<p>A tone is <code>ok</code>, <code>warn</code> or <code>bad</code>. Every value is drawn as text. A table is cut at 500 rows and says how many it left out, and a block type the bar does not know is drawn as a note saying so.</p>
<p><strong>Two halves, either optional.</strong> <code>view</code> is a <code>SectionViewInterface</code> that builds blocks from the stored profile. It runs when <code>profile/view</code> is read, not when the profile is written, so stored profiles stay data and a changed view applies to them too. It receives the whole profile, so a section can be drawn from its own collector, from the built in ones, or both.</p>
<p>The other half reads the live page. A script listed under <code>body</code> loads as a module before the bar's own, and registers a reader through the watcher <code>early.js</code> installs:</p>
<pre><code>window.__siteationDebugBar.registerSection({
id: 'c365',
read(context) { return { tabs: [...] } },
watch(refresh) { window.addEventListener('update-prices', refresh) },
highlight(target, on) { /* outline what the row's _target names */ },
})</code></pre>
<p><code>read</code> may return a promise. Its <code>context</code> holds the profile on screen, this section's payload, the requests the page made since it loaded, the value policy and a <code>clean</code> that applies it, and <code>loadProfile(id)</code>, which fetches another stored profile with its views. Treat all of it as read only. <code>read</code> is called when the section is opened, when another profile is shown, and a frame after each <code>refresh</code>. What it throws is shown in the panel. Both halves are drawn as one panel: tabs with the same id merge, the server's blocks first, and the live count wins over the server's.</p>
<p>A script listed under <code>head</code> loads right after <code>early.js</code>, before the theme's scripts, for anything that has to be in place first, as the data layer recorder is. Both lists take <code>Vendor_Module::path.js</code> assets from <code>view/base/web</code>, and neither is loaded while the section is switched off.</p>
<p><strong>Findings and MCP come along.</strong> A rule whose finding names the section shows it at the top of the panel, and its <code>action.filter</code> names the tab to open. <code>get_debug_profile_section</code> reads any contributed section and pages through its <code>payload.items</code>.</p>
<p><code>dev/fixture-section</code> is a module that uses all of it, for the browser suite. Install it to run the three tests that cover this; without it they skip.</p>
<h2 id="adding-a-rule"><a class="anchor" href="#adding-a-rule">Adding a rule</a></h2>
<p>Implement <code>Api\RuleInterface</code> and register it in the rules array in <code>etc/di.xml</code>. A rule that throws is skipped and logged rather than taking the profile with it.</p>
<p>A rule returns <code>Analysis\Finding</code> objects. Each carries a severity, a message, where it came from, what to check next, and the section and filter holding its evidence. Point the action at a filter that actually narrows: mapping <code>repeated</code> to <code>all</code> is no filter at all, which is a mistake this codebase has already made once.</p>
<p>Thirteen rules ship, producing nineteen kinds of finding. <code>RequestErrorRule</code>, <code>RepeatedQueryRule</code>, <code>SeoCanonicalRule</code> and <code>SeoHeadingRule</code> emit two each, and <code>SeoSchemaOfferRule</code> three.</p>
<h2 id="the-profile"><a class="anchor" href="#the-profile">The profile</a></h2>
<p>One JSON file per request under <code>var/siteation_debugbar/</code>, <code>0600</code> inside a <code>0700</code> directory, pruned to 20 files or 60 minutes.</p>
<p>Retention is enforced twice. <code>ProfileStore::tidy()</code> sweeps on a write and when the history endpoint is read, and <code>get()</code> refuses anything past the age bound whether or not a sweep has reached it. <code>get()</code> refuses rather than deletes: the MCP tools read through the same path and are advertised as read only.</p>
<p><code>Model\Redactor</code> masks sensitive keys at record time, bounds depth and item counts, and strips string literals from SQL. <code>src-js/src/redact.js</code> mirrors it for the Alpine section, which reads live objects rather than a stored profile and therefore has to apply the policy itself.</p>
<p><strong>Query shapes ignore numbers as well as quoted values</strong>, because Magento interpolates ids unquoted and an N+1 made of them was invisible otherwise. Identifiers are protected, so <code>t1</code> is not <code>t2</code>, and <code>LIMIT 10</code> and <code>LIMIT 100</code> are deliberately one shape.</p>
<h2 id="the-gate-and-the-endpoints"><a class="anchor" href="#the-gate-and-the-endpoints">The gate and the endpoints</a></h2>
<p><code>RequestEligibility::allows()</code> decides whether a request is profiled. <code>allowsRead()</code> decides whether one may be read. Three controllers call <code>allowsRead()</code> rather than repeating the check, because an endpoint that hands out a profile to someone the bar would not have collected for is a way around the gate rather than a thing behind it.</p>
<p>Three endpoints, all behind the same gate: <code>profile/view</code> for one profile, <code>profile/history</code> for the list, <code>profile/compare</code> for the diff. Anything that hands out profile ids sits behind the same check as the thing that hands out profiles.</p>
<p><code>Model\AccessKey</code> is what makes a live site possible. <code>Config::MIN_ACCESS_KEY_LENGTH</code> is 32; below that the bar refuses to run rather than treating the key as absent, since absent means keyless. <code>Model\Config\Backend\AccessKey</code> refuses it on save so the refusal is visible, and <code>Model\AccessAttempts</code> stops answering an address that offers ten wrong keys inside fifteen minutes.</p>
<p><code>Model\ClientAddress</code> is the one definition of who a request came from: <code>REMOTE_ADDR</code>, unless that peer is listed in <code>trusted_proxies</code>, in which case <code>X-Forwarded-For</code> is walked from the right to the first entry that is not a trusted proxy. The allowlist and the lockout both read it, because a difference between them would be a way around one of the two.</p>
<h2 id="the-bar"><a class="anchor" href="#the-bar">The bar</a></h2>
<p>Sources in <code>src-js/src/</code>, output committed to <code>view/base/web/</code>. Installing the module needs no build step, and that is deliberate.</p>
<pre><code>cd src-js
npm install
npm run build # or npm run dev, which watches the main bundle</code></pre>
<p><code>npm run dev</code> builds <code>early.js</code> once and then watches the main bundle only. Editing <code>early.js</code> during a watch session needs another <code>npm run build</code>.</p>
<p><strong>The host is a custom element</strong>, <code><siteation-debugbar></code>, written before <code></body></code> alongside the profile JSON and the module script. It mounts itself in <code>connectedCallback</code>, so the tag name is the only thing <code>BarInjector</code>, <code>debugbar.js</code> and <code>early.js</code> have to agree on.</p>
<p><strong>A shadow root</strong> keeps the bar's styles off the store and the store's styles off the bar, and hides the bar's markup from the theme's Alpine.</p>
<p><strong>Its own bundled Alpine</strong>, the CSP build, under a <code>data-ndb-</code> prefix, started with <code>Alpine.initTree(root)</code> and never <code>Alpine.start()</code>. A full start installs a document wide mutation observer that would claim the host theme's <code>@click</code> shorthands and bind every handler twice.</p>
<p><strong>No inline script or style.</strong> The profile travels as <code><script type="application/json"></code> and everything else is an external file, so <code>Magento_Csp</code> needs no nonce and no <code>unsafe-inline</code>.</p>
<p><strong>Only summaries are embedded</strong>, about 1.5 kB. Section payloads are fetched from <code>profile/view</code> on first open.</p>
<p>File by file: <code>state.js</code> is the Alpine component, <code>template.js</code> the markup, <code>sections.js</code> the section list, <code>header.js</code> the header used by both the dock and the sheet, <code>palette.js</code> the command list, <code>alpine.js</code> and <code>magewire.js</code> the two live readers, <code>early.js</code> the request watcher and error capture that loads in the head. <code>nav.js</code> draws the sidebar, <code>tabs.js</code> the sub views, <code>dock.js</code> keeps the collapsed bar inside the screen, <code>requests.js</code> brings the early watcher's requests into the bar, and <code>transport.js</code> is every JSON read the bar makes.</p>
<p><code>seo.js</code> holds the SEO checks as plain functions that take a <code>Document</code>, plus an environment for what only a browser can answer (is it visible, how big is it drawn, where is the first screen). <code>seo-page.js</code> is the live half: it reads the real document, keeps the elements it found out of the bar's reactive state, and draws and removes the outlines. A new check goes in <code>seo.js</code> with a fixture in <code>test/seo.test.js</code>.</p>
<p><code>Collector\SeoCollector</code> is the server's half. It parses the response with <code>DOMDocument</code> in <code>finalize()</code>, after the page has rendered and before the bar is injected, and keeps what the HTML told search engines. Its checks mirror the ones in <code>seo.js</code> where both read the same tag, so a canonical found by one is found by the other: <code>rel</code> is a token list matched without case, and headings inside a <code><template></code> do not count. The SEO rules read only its summary, which is embedded in every page, so anything long goes in the payload. <code>serverVersusLive()</code> in <code>seo.js</code> compares that summary with the live reading, so a value both sides cut (the server keeps 300 characters, the bar 160) is compared up to the shorter cut. <code>ProfileComparer</code> lists the summary fields that differ between two profiles, already formatted, so the bar, the report and MCP print the same values.</p>
<p><code>schema.js</code> is the Schema view: it reads JSON-LD and microdata into one entity shape and checks them against <code>RICH_RESULT_RULES</code>, one table of required and recommended properties per type with the Google page each came from. When Google changes a rule, change that table and its test in <code>test/schema.test.js</code>.</p>
<p><code>datalayer.js</code> holds the Data layer checks, as plain functions over the pushes early.js records; <code>datalayer-page.js</code> reads the buffer, <code>window.google_tag_manager</code> and the tab's session storage. The recorder itself is in <code>early.js</code>, because it has to be in place before the page's tags load. Its tests in <code>test/early.test.js</code> run the real file in a <code>vm</code> context, including a push replaced the way GTM replaces it.</p>
<p><code>vitals.js</code> works out the Web Vitals from performance entries, as plain functions: LCP's last candidate, CLS's session windows, INP's 98th percentile, each rated against <code>THRESHOLDS</code>. <code>vitals-page.js</code> keeps a <code>PerformanceObserver</code> with <code>buffered: true</code> on each entry type, so the bar needs nothing in <code>early.js</code> for them, and keeps the elements the rows point at. It tells the bar's own nodes apart by its host element, which is what the browser reports for anything inside the shadow root.</p>
<p>The admin edit link has a server half. <code>Model\AdminEditLink</code> names the entity from the storefront request's route and id parameter and points at the admin route <code>siteation_debugbar/entity/edit</code>. It builds that URL from configuration rather than through the backend URL model, whose session would otherwise start inside the storefront's. <code>Controller\Adminhtml\Entity\Edit</code> is a secret key public action that checks the target's own ACL resource and redirects through the backend URL model, which adds the key. Besides the entities it has two settings targets, <code>design_config</code> and <code>catalog_config</code>, for the screens the SEO defaults and canonical switches live on; <code>AdminEditLink::forTarget()</code> links to any of them.</p>
<p><code>Model\SeoSources</code> says where each SEO value came from. It follows the rule Magento's own code uses (<code>Catalog\Helper\Product\View</code>, <code>Catalog\Block\Category\View</code>, <code>Cms\Block\Page</code>, and <code>Framework\View\Page\Config</code> and <code>Title</code> for the defaults and the title prefix and suffix), predicts the value, and compares it with what SeoCollector read. The product and category come from the registry, as the controller left them, so there is no dependency on the catalog module and nothing on a full page cache hit. When Magento changes one of those rules, change it here. It also states the product's final price in both tax modes and whether it is salable, which <code>SeoSchemaOfferRule</code> checks the structured data's offers against.</p>
<h2 id="mcp"><a class="anchor" href="#mcp">MCP</a></h2>
<p><code>bin/magento siteation:debugbar:mcp</code>, hand rolled JSON-RPC 2.0 over newline delimited stdio, six read only tools. It starts in about 0.2 seconds because it only reads files.</p>
<table><thead><tr><th>Tool</th><th>Class</th></tr></thead><tbody><tr><td><code>list_debug_profiles</code></td><td><code>Mcp\Tool\ListProfiles</code></td></tr><tr><td><code>get_debug_findings</code></td><td><code>Mcp\Tool\GetFindings</code></td></tr><tr><td><code>get_debug_profile_section</code></td><td><code>Mcp\Tool\GetSection</code></td></tr><tr><td><code>inspect_debug_queries</code></td><td><code>Mcp\Tool\InspectQueries</code></td></tr><tr><td><code>compare_debug_profiles</code></td><td><code>Mcp\Tool\CompareProfiles</code></td></tr><tr><td><code>get_debug_seo</code></td><td><code>Mcp\Tool\GetSeo</code></td></tr></tbody></table>
<p>Add a tool by implementing <code>Api\McpToolInterface</code> and registering it in the tools array in <code>etc/di.xml</code>. Shaping belongs in <code>Presentation\McpProfilePresenter</code>, which bounds every response by item count and byte budget and labels the payload as recorded data.</p>
<p><strong>Every successful tool result carries the recorded data note.</strong> A tool from another module that builds its own envelope gets the <code>recorded_data</code> line from <code>Mcp\Server</code>, after <code>status</code>, unless it set one itself. An error result gets none.</p>
<p><strong>The presenter's envelope is built inside the byte budget</strong>, so anything added to it is paid for out of the response rather than pushing it over.</p>
<h2 id="testing"><a class="anchor" href="#testing">Testing</a></h2>
<pre><code>vendor/bin/phpcs --standard=<pkg>/phpcs.xml.dist <pkg>
vendor/bin/phpstan analyse -c <pkg>/phpstan.neon.dist
vendor/bin/phpunit --configuration <pkg>/phpunit.xml.dist \
--bootstrap vendor/autoload.php # 360 tests
<pkg>/dev/smoke https://your-store.test admin # 31 checks over HTTP
cd <pkg>/src-js && npm test # 193, no store needed
cd <pkg>/src-js && npm run test:browser # 34, needs the store up</code></pre>
<p><strong>The supported PHP range is checked separately</strong>, because <code>phpstan.neon.dist</code> has to keep working with the PHPStan a Magento install carries, and that one is pinned to 1.x by Magento and by rector. <code>phpstan-versions.neon.dist</code> layers the range from <code>composer.json</code> on top of it and needs PHPStan 2.1 or newer, so bring your own and run it from the Magento root, where it finds the autoloader:</p>
<pre><code>composer global require phpstan/phpstan:^2.2
~/.composer/vendor/bin/phpstan analyse -c <pkg>/phpstan-versions.neon.dist</code></pre>
<p>That pass is what says the constraint is honest. It reports a typed class constant or a <code>json_validate()</code> as unavailable rather than waiting for a 8.2 install to find it at runtime. Change the constraint in <code>composer.json</code> and change the range beside it.</p>
<h3 id="ci"><a class="anchor" href="#ci">CI</a></h3>
<p><code>.github/workflows/ci.yml</code> runs every check above that needs no store, on each pull request and on main: PHPUnit and PHPStan on PHP 8.2 to 8.5, the PHPStan range pass and phpcs once, the JS unit tests, and a fresh <code>npm run build</code> that fails when <code>view/base/web</code> differs from what is committed. phpcs fails on errors and reports warnings. The smoke and browser suites need a store, so they stay manual.</p>
<p>There is no shop in CI. <code>dev/ci</code> is a small Composer project that installs Magento from the Mage-OS public mirror, so no Magento keys are needed, plus PHPUnit, PHPStan 2 and the Magento coding standard. The workflow checks the module out at <code>module/</code> and copies <code>dev/ci</code> to <code>host/</code> beside it. <code>dev/ci/bootstrap.php</code> registers the generator Magento's own unit tests use, so factories that only exist once a store generates them, such as <code>RawFactory</code>, can be mocked and analysed. To reproduce a run locally:</p>
<pre><code>mkdir ci && git clone <repo> ci/module && cp -R ci/module/dev/ci ci/host
composer install -d ci/host && cd ci
host/vendor/bin/phpunit -c module/phpunit.xml.dist --bootstrap host/bootstrap.php
host/vendor/bin/phpstan analyse -c module/phpstan.neon.dist --autoload-file host/bootstrap.php</code></pre>
<h3 id="dev-smoke"><a class="anchor" href="#dev-smoke">dev/smoke</a></h3>
<pre><code>dev/smoke <base-url> [admin-path]
dev/smoke https://your-store.test admin</code></pre>
<p>An end to end check over HTTP, needing <code>bash</code>, <code>curl</code> and <code>python3</code>. It loads the cart page and checks the bar is injected the way it should be (profile header, the early watcher, the profile embedded as data with summaries only, the value policy and editor root), reads that profile back through <code>profile/view</code> section by section, including the SEO facts and the markdown format, lists the history, compares two stored profiles, refuses bad ids, and checks that the admin, GraphQL and REST are profiled with their bodies untouched and that the bar's own endpoint is not profiled. The admin path defaults to <code>admin</code>.</p>
<p>Run it against a store in developer mode with no access key, or every check that needs the profile header fails. A check that cannot run, such as a compare on a store holding only one profile, is counted as skipped rather than passed. It exits non-zero when anything fails, and 64 when no URL is given.</p>
<h3 id="dev-docs"><a class="anchor" href="#dev-docs">dev/docs</a></h3>
<p><code>php dev/docs</code> renders <code>docs/user-guide.md</code> and <code>docs/developer-guide.md</code> to the HTML beside them. See <a href="#documentation">Documentation</a>.</p>
<p>Magento's integration framework is deliberately not used. It needs its own database and install and runs in minutes; <code>dev/smoke</code> drives a real store over HTTP across all four areas in seconds and asserts the same regressions.</p>
<p><strong>The browser suite is the one that catches a binding that throws.</strong> <code>early.js</code> watches <code>console.warn</code> for every Alpine on the page, including the bar's own, and the suite walks every section and sub-tab and asserts that buffer is empty. A throwing binding renders as an empty element and says nothing otherwise. Two of the tests run under an injected policy that forbids <code>unsafe-eval</code>, because the store's own policy is report-only on most pages and the violations the bar used to commit were reported and never blocked.</p>
<p>Two habits that suite enforces: the header is built once and used twice, so a header selector without <code>.ndb-sheet</code> matches both; and selecting a section starts work, so wait for the state rather than for a length of time.</p>
<p>When looking at layout in a browser, measure geometry with JavaScript before diagnosing from a screenshot. Screenshots are scaled and have misled more than once.</p>
<h2 id="adding-a-setting"><a class="anchor" href="#adding-a-setting">Adding a setting</a></h2>
<ol>
<li>A field in <code>etc/adminhtml/system.xml</code>, under the existing <code>general</code> group.</li>
<li>A default in <code>etc/config.xml</code>.</li>
<li>An <code>XML_PATH_</code> constant and a resolved property in <code>Model\Config</code>, set inside <code>resolve()</code>.</li>
</ol>
<p>Settings live at <code>siteation_debugbar/general/*</code> under a Siteation tab, deliberately not under Advanced > Developer: Magento hides the whole <code>dev</code> section in production mode, which is exactly where the access key makes the bar usable.</p>
<p>One group rather than several, because every field's <code>depends</code> on Enabled would otherwise become a cross group dependency.</p>
<h2 id="traps-that-cost-hours"><a class="anchor" href="#traps-that-cost-hours">Traps that cost hours</a></h2>
<p>These are the ones that have actually bitten. Check them before believing a bug is real.</p>
<p><strong>Stale artifacts, in order of likelihood.</strong></p>
<ol>
<li><strong>The interception cache.</strong> <code>cache:clean config</code> does not reach it. After adding or changing a plugin, run <code>cache:flush</code> or the plugin silently never runs.</li>
<li><strong>Generated interceptors.</strong> Developer mode creates them once and never rechecks, so changing a constructor gives <code>Too few arguments ... generated/code/.../Interceptor.php</code>. Fix with <code>rm -rf generated/code/Siteation</code>.</li>
<li><strong><code>generated/metadata</code>.</strong> Adding an item to a <code>di.xml</code> argument array survives both of the above. It takes <code>rm -rf generated/metadata</code> as well.</li>
<li><strong>Static assets.</strong> Asset URLs carry the built file's mtime, so a rebuilt bundle gets a new URL. If the bar looks unchanged after a build, hard reload.</li>
</ol>
<p><strong>PHP and Magento.</strong></p>
<ul>
<li>An interface plugin fires <strong>once per implementing object in the chain</strong>, not once per logical operation. <code>LoggerInterface</code> resolves to <code>LoggerProxy</code>, which delegates to another implementer, so every query was counted twice for months.</li>
<li><code>preg_replace</code> reads backslashes in the <strong>replacement</strong> as escapes, so injecting a payload containing PHP class names corrupts it silently. Use <code>preg_replace_callback</code>.</li>
<li>Two <code><comment></code> elements on one <code>system.xml</code> field cancel each other out: Magento collapses the repeat into an array and renders neither.</li>
<li><code>Cache-Control: private</code> is not a milder <code>no-store</code> to Magento's Varnish. It means a 24 hour hit-for-pass that tag invalidation cannot clear.</li>
<li>The cookie manager resolves its domain through the store, so it cannot be used at the top of <code>launch()</code>: there is no store yet. Issue cookies after <code>proceed()</code>.</li>
<li>Magento hides the whole <code>dev</code> configuration section in production mode. The check runs against the structure path, not the stored one.</li>
</ul>
<p><strong>The bar.</strong></p>
<ul>
<li>Inside a shadow root, <code>rem</code> resolves against the <strong>document</strong> root. Size in <code>px</code>.</li>
<li>A shadow root isolates the bar from the page, not from itself. Generic class names collide.</li>
<li><code>x-show</code> <strong>evaluates everything inside it while hidden</strong>, so a panel bound to state that starts as <code>null</code> throws once per binding on every page load, invisibly. Use <code>x-if</code> for a panel that has no data yet. <code>x-show</code> also defers every reveal after the first through a <code>setTimeout</code>, so nothing can be timed against it.</li>
<li>An <code>x-for</code> variable that shares a name with a component property silently redirects every write inside the loop. Nothing throws.</li>
<li><code>Object.keys</code> on Alpine's merged scope proxy returns <code>[]</code>. Use <code>Reflect.ownKeys</code>.</li>
<li><code>Alpine.$data(el)</code> folds every parent scope into the child. For one component's own state, read <code>el._x_dataStack[0]</code>.</li>
<li>Alpine 3.14.3, which Hyvä ships, has no <code>setErrorHandler</code> and no <code>injectMagics</code>. Expression errors have to be read from <code>console.warn</code>.</li>
<li>Writing to a key named <code>__proto__</code> on a plain object sets the prototype instead of the key, so the value disappears. Use <code>Object.create(null)</code> for anything built from page data.</li>
<li>A background tab throttles timers and stops <code>requestAnimationFrame</code>, so anything timing sensitive measures as broken there.</li>
</ul>
<p><strong>Conventions.</strong></p>
<ul>
<li>A query's call site is stored under <code>callsite</code>, one word, relative to the application root.</li>
<li>Label over value beats label beside value. <code>facts()</code> in <code>facts.js</code> builds the grid.</li>
<li>No tiny type. 15px body, 14px mono, 12px floor. The uppercase micro label is 12px.</li>
<li>An editor URL template writes <code>://file%f</code>, never <code>://file/%f</code>: <code>%f</code> is absolute, so the second form carries two slashes and Zed ignores it silently.</li>
</ul>
<h2 id="testing-a-crash"><a class="anchor" href="#testing-a-crash">Testing a crash</a></h2>
<p>There is no fixture for a 500, on purpose: a route that throws does not belong on a working instance. Four files under <code>app/code/Siteation/BoomTest</code> recreate one, a controller whose <code>execute()</code> throws. Give it a real return type; <code>never</code> breaks Magento's generated interceptor before the exception ever happens. Delete it afterwards.</p>
<h2 id="documentation"><a class="anchor" href="#documentation">Documentation</a></h2>
<p>The two guides are Markdown, and their HTML is generated:</p>
<pre><code>php dev/docs</code></pre>
<p>Edit the <code>.md</code>. The <code>.html</code> is a view of it, and <code>dev/docs</code> is the only place the two can differ. It needs no dependencies, for the same reason the bar ships prebuilt.</p>
<p>It understands the Markdown the guides use: headings, paragraphs, lists without nesting, fenced code, tables, inline code, bold, links and images. Write an image reference style, <code>![alt][name]</code> with a <code>[name]: ../assets/file.webp "Title"</code> line at the end of the guide, so the same path works on GitHub and from the rendered page.</p>
<p>The screenshots live in <code>assets/</code> and are WebP. The README and the user guide both use them.</p>
<h2 id="releasing"><a class="anchor" href="#releasing">Releasing</a></h2>
<ol>
<li>Everything green: phpcs, phpstan, phpunit, smoke, both JS suites.</li>
<li><code>npm run build</code>, and commit the output if it changed.</li>
<li><code>php dev/docs</code> if a guide changed.</li>
<li>A dated section in <code>CHANGELOG.md</code>.</li>
<li>An annotated tag, plain semver with no <code>v</code> prefix, matching the other Siteation packages.</li>
<li><code>git push origin main && git push origin <tag></code>.</li>
</ol>
<p>There are no GitHub Actions and none are wanted: tests run locally, the build output is committed, and Packagist reads the tag through a webhook.</p>
</main>
<footer>Generated from docs/developer-guide.md by dev/docs. Edit the Markdown, not this file.</footer>
</body>
</html>