Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(ui): post each log line under the page it was written on (K24)

Bernhard Jackiewicz0bb4c3783f2d
diff --git a/graph-ui/src/app/ui-log-install.test.ts b/graph-ui/src/app/ui-log-install.test.ts
index 30e16e2fd..87c163903 100644
--- a/graph-ui/src/app/ui-log-install.test.ts
+++ b/graph-ui/src/app/ui-log-install.test.ts
@@ -77,6 +77,26 @@ describe('installUiLog', () => {
expect(all[0]?.detail).toContain('build v');
});
+ it('posts a line under the address it was written on, also after a project switch in the page (K24)', async () => {
+ window.history.replaceState(null, '', '/?project=django-demo&workspace=galaxy');
+ const posts: UiLogPayload[] = [];
+ const quiet = { debug: () => {}, log: () => {}, info: () => {}, warn: () => {}, error: () => {} } as unknown as Console;
+ try {
+ handle = installUiLog({ console: quiet, session: 'switch', transport: { send: async (payload) => { posts.push(payload); return true; } } });
+ // The project switch changes the address with pushState and keeps the page (app/project-windows.tsx).
+ window.history.pushState(null, '', '/?project=cbm&workspace=galaxy');
+ quiet.warn('after the switch');
+ await handle.buffer.flush();
+ await handle.buffer.flush();
+ expect(posts.map((post) => [post.page, post.entries.map((entry) => [entry.message, entry.project])])).toEqual([
+ ['/?project=django-demo&workspace=galaxy', [['session switch started on /?project=django-demo&workspace=galaxy', 'django-demo']]],
+ ['/?project=cbm&workspace=galaxy', [['after the switch', 'cbm']]],
+ ]);
+ } finally {
+ window.history.replaceState(null, '', '/');
+ }
+ });
+
it('lets the console print as before and records what it printed', async () => {
const { buffer, posts, printed, fakeConsole } = install();
const failure = new Error('twin failed');
diff --git a/graph-ui/src/app/ui-log-install.ts b/graph-ui/src/app/ui-log-install.ts
index b572f2f24..2ed30ea17 100644
--- a/graph-ui/src/app/ui-log-install.ts
+++ b/graph-ui/src/app/ui-log-install.ts
@@ -139,8 +139,10 @@ export function installUiLog(options: InstallUiLogOptions = {}): UiLogHandle {
const page = `${target.location.pathname}${target.location.search}`;
const getProject = options.getProject ?? (() => activeProject
?? new URLSearchParams(target.location.search).get('project') ?? '');
+ // A project switch changes the address and keeps the page (K24): each line names the address it was written on.
+ const getPage = (): string => `${target.location.pathname}${target.location.search}`;
const buffer = options.buffer
- ?? new UiLogBuffer({ page, session, getProject, transport: options.transport ?? httpUiLogTransport() });
+ ?? new UiLogBuffer({ page, getPage, session, getProject, transport: options.transport ?? httpUiLogTransport() });
let recording = false;
const guarded = (fn: () => void): void => {
diff --git a/graph-ui/src/app/ui-log.test.ts b/graph-ui/src/app/ui-log.test.ts
index eff9f1db5..096aae56d 100644
--- a/graph-ui/src/app/ui-log.test.ts
+++ b/graph-ui/src/app/ui-log.test.ts
@@ -80,6 +80,37 @@ describe('UiLogBuffer', () => {
]);
});
+ it('posts each entry under the page it was recorded on, also when the address changed in the page (K24)', async () => {
+ // A project switch stays in the page and only changes the address; the server files a post's entries under its one page.
+ let page = '/?project=django-demo&workspace=galaxy';
+ const posts: { payload: UiLogPayload; final: boolean }[] = [];
+ const buffer = new UiLogBuffer({
+ page, getPage: () => page, session: 'switch', getProject: () => new URLSearchParams(page.slice(1)).get('project') ?? '',
+ transport: { send: async (payload, final) => { posts.push({ payload, final }); return true; } },
+ schedule: () => 0, cancel: () => undefined,
+ });
+ buffer.record('warn', 'console', 'before the switch');
+ page = '/?project=cbm&workspace=galaxy';
+ buffer.record('warn', 'console', 'after the switch');
+ buffer.record('info', 'console', 'still after');
+ await buffer.flush();
+ await buffer.flush();
+ expect(posts.map(({ payload }) => [payload.page, payload.entries.map((entry) => [entry.message, entry.project])])).toEqual([
+ ['/?project=django-demo&workspace=galaxy', [['before the switch', 'django-demo']]],
+ ['/?project=cbm&workspace=galaxy', [['after the switch', 'cbm'], ['still after', 'cbm']]],
+ ]);
+ expect(buffer.page).toBe('/?project=cbm&workspace=galaxy');
+ // A page that is going away sends the entries of every page it was on.
+ buffer.record('warn', 'console', 'cbm again');
+ page = '/?project=django-demo&workspace=explore';
+ buffer.record('warn', 'console', 'django-demo again');
+ await buffer.flush(true);
+ expect(posts.slice(2).map(({ payload, final }) => [payload.page, payload.entries.map((entry) => entry.message), final])).toEqual([
+ ['/?project=cbm&workspace=galaxy', ['cbm again'], true],
+ ['/?project=django-demo&workspace=explore', ['django-demo again'], true],
+ ]);
+ });
+
it('queues entries with a sequence and a timestamp, and posts them as one batch on the timer', async () => {
const { buffer, posts, timers, fire } = harness();
buffer.record('error', 'rpc', 'get_code_snippet returned no source', { detail: 'HTTP 200' });
diff --git a/graph-ui/src/app/ui-log.ts b/graph-ui/src/app/ui-log.ts
index 38073a1b2..32b112e45 100644
--- a/graph-ui/src/app/ui-log.ts
+++ b/graph-ui/src/app/ui-log.ts
@@ -39,6 +39,10 @@
* when the page is left (`final`), so the journal holds a handful of
* lines, not hundreds. The same message from another place or with
* another response body is another entry and is recorded in full.
+ * - **One page per post.** The server files every entry of a post under
+ * the post's page. A project switch stays in the page and only changes
+ * the address (app/project-windows.tsx), so each entry keeps the page it
+ * was recorded on, and a batch never spans two of them.
*
* Time and timers are injectable so a test runs in one tick.
*/
@@ -106,6 +110,8 @@ export const UI_LOG_REPEAT_KEYS_MAX = 256;
export interface UiLogOptions {
page: string;
+ /** The page as it is now, sampled when recording like the project; `page` without it. */
+ getPage?: () => string;
session: string;
transport: UiLogTransport;
/** Sampled when recording, never when sending a delayed or retried batch. */
@@ -158,6 +164,8 @@ const powerOfTen = (count: number): boolean => count >= 10 && /^10*$/.test(Strin
export class UiLogBuffer {
private readonly queue: UiLogEntry[] = [];
+ /** The page each queued entry was recorded on. */
+ private readonly pages = new WeakMap<UiLogEntry, string>();
private readonly repeats = new Map<string, Repeat>();
private seq = 0;
private sent = 0;
@@ -191,7 +199,7 @@ export class UiLogBuffer {
}
get page(): string {
- return this.options.page;
+ return this.options.getPage?.() ?? this.options.page;
}
/** Queue one entry. Never throws and never writes to the console. */
@@ -261,8 +269,9 @@ export class UiLogBuffer {
/**
* Send what is queued, one batch now and the rest on the timer. With
- * `final` the page is going away: the transport is told so, and nothing
- * is rescheduled.
+ * `final` the page is going away: the transport is told so, nothing is
+ * rescheduled, and a batch goes out for each page the entries were
+ * recorded on.
*/
async flush(final = false): Promise<void> {
if (this.timer !== undefined) {
@@ -283,11 +292,17 @@ export class UiLogBuffer {
if (this.queue.length === 0) {
return;
}
- const batch = this.queue.splice(0, this.batchMax);
+ // One page per post: the batch ends where an entry of another page starts.
+ const page = this.pageOf(this.queue[0]);
+ let size = 1;
+ while (size < Math.min(this.batchMax, this.queue.length) && this.pageOf(this.queue[size]) === page) {
+ size += 1;
+ }
+ const batch = this.queue.splice(0, size);
if (this.droppedSinceFlush > 0) {
const lost = this.droppedSinceFlush;
this.droppedSinceFlush = 0;
- batch.unshift({
+ const notice: UiLogEntry = {
// Loss can span several projects. Do not attribute the aggregate
// to whichever project happens to be open when delivery resumes.
project: '',
@@ -296,10 +311,12 @@ export class UiLogBuffer {
level: 'warn',
source: 'ui-log',
message: `${lost} entries were dropped before this batch: the page keeps ${this.bufferMax} while the server does not answer`,
- });
+ };
+ this.pages.set(notice, page);
+ batch.unshift(notice);
}
const payload: UiLogPayload = {
- page: this.options.page,
+ page,
session: this.options.session,
entries: batch,
};
@@ -317,6 +334,9 @@ export class UiLogBuffer {
this.backoffStep = 0;
if (this.queue.length > 0 && !final) {
this.arm(0);
+ } else if (final && this.queue.length > 0 && this.pageOf(this.queue[0]) !== page) {
+ // The page is going away: the entries of the other page it was on go with it.
+ await this.flush(true);
}
return;
}
@@ -343,9 +363,14 @@ export class UiLogBuffer {
this.dropped += 1;
this.droppedSinceFlush += 1;
}
+ this.pages.set(entry, this.page);
this.queue.push(entry);
}
+ private pageOf(entry: UiLogEntry | undefined): string {
+ return (entry === undefined ? undefined : this.pages.get(entry)) ?? this.options.page;
+ }
+
private arm(ms: number): void {
if (this.timer !== undefined || this.inFlight) {
return;