coding-agent-search 0.5.1

Unified TUI search over local coding agent histories
Documentation
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/**
 * cass Archive Cross-Origin Isolation Detector
 *
 * Detects and handles the two-load pattern required for SharedArrayBuffer:
 * - First load: Service Worker installs but COOP/COEP headers not yet applied
 * - Second load: Cross-origin isolated, SharedArrayBuffer available
 *
 * Provides graceful UX for each state:
 * - SW_INSTALLING: Show loading UI while SW installs
 * - NEEDS_RELOAD: Prompt user to reload for full functionality
 * - READY: Proceed to authentication
 * - DEGRADED: Continue with limited functionality
 */

// COI States
export const COI_STATE = {
    SW_INSTALLING: 'SW_INSTALLING',
    NEEDS_RELOAD: 'NEEDS_RELOAD',
    READY: 'READY',
    DEGRADED: 'DEGRADED',
};

let activeReloadController = null;
const serviceWorkerActivationCallbacks = new Set();
let serviceWorkerActivationListenersInstalled = false;
let serviceWorkerActivationDispatchScheduled = false;

function hashScopeId(input) {
    let hash = 0x811c9dc5;
    for (let i = 0; i < input.length; i++) {
        hash ^= input.charCodeAt(i);
        hash = Math.imul(hash, 0x01000193) >>> 0;
    }
    return hash.toString(16).padStart(8, '0');
}

function getSetupCompleteKey() {
    try {
        return `cass-coi-setup-complete-${hashScopeId(new URL('./', window.location.href).href)}`;
    } catch {
        const href = typeof window?.location?.href === 'string'
            ? window.location.href
            : 'unknown';
        return `cass-coi-setup-complete-${hashScopeId(href.split('#')[0].split('?')[0])}`;
    }
}

/**
 * Check if COI setup has been completed before
 * @returns {boolean}
 */
export function isSetupComplete() {
    try {
        return localStorage.getItem(getSetupCompleteKey()) === 'true';
    } catch {
        return false;
    }
}

/**
 * Mark COI setup as complete
 */
export function markSetupComplete() {
    try {
        localStorage.setItem(getSetupCompleteKey(), 'true');
    } catch {
        // localStorage not available
    }
}

/**
 * Clear setup complete flag (for testing)
 */
export function clearSetupComplete() {
    try {
        localStorage.removeItem(getSetupCompleteKey());
    } catch {
        // localStorage not available
    }
}

async function getCurrentServiceWorkerRegistration() {
    if (!('serviceWorker' in navigator)) {
        return null;
    }

    try {
        return (await navigator.serviceWorker.getRegistration()) ?? null;
    } catch {
        return null;
    }
}

/**
 * Check if we're cross-origin isolated
 * @returns {boolean}
 */
export function isCrossOriginIsolated() {
    return window.crossOriginIsolated === true;
}

/**
 * Check if Service Worker is installed and controlling
 * @returns {Promise<boolean>}
 */
export async function isServiceWorkerActive() {
    const registration = await getCurrentServiceWorkerRegistration();
    return registration?.active !== null && registration?.active !== undefined;
}

/**
 * Check if a service worker registration exists for this archive scope
 * @returns {Promise<boolean>}
 */
export async function hasServiceWorkerRegistration() {
    return (await getCurrentServiceWorkerRegistration()) !== null;
}

/**
 * Check if Service Worker is supported
 * @returns {boolean}
 */
export function isServiceWorkerSupported() {
    return 'serviceWorker' in navigator;
}

/**
 * Check if SharedArrayBuffer is available (definitive test for COOP/COEP)
 * @returns {boolean}
 */
export function isSharedArrayBufferAvailable() {
    try {
        new SharedArrayBuffer(1);
        return true;
    } catch {
        return false;
    }
}

/**
 * Determine current COI state
 * @returns {Promise<string>} One of COI_STATE values
 */
export async function getCOIState() {
    // If SW not supported, we're in degraded mode
    if (!isServiceWorkerSupported()) {
        console.log('[COI] Service Workers not supported - degraded mode');
        return COI_STATE.DEGRADED;
    }

    const swActive = await isServiceWorkerActive();
    const coiEnabled = isCrossOriginIsolated();
    const sabAvailable = isSharedArrayBufferAvailable();

    console.log('[COI] State check:', { swActive, coiEnabled, sabAvailable });

    if (!swActive) {
        // SW not yet active - still installing
        return COI_STATE.SW_INSTALLING;
    }

    if (!coiEnabled || !sabAvailable) {
        // SW active but COI not yet enabled - needs reload
        return COI_STATE.NEEDS_RELOAD;
    }

    // Fully ready
    return COI_STATE.READY;
}

/**
 * Get recommended Argon2 configuration based on COI availability
 * @returns {Object} Configuration object
 */
export function getArgon2Config() {
    if (isSharedArrayBufferAvailable()) {
        return {
            parallelism: 4,   // Use all lanes for multi-threaded
            mode: 'wasm-mt',  // Multi-threaded WASM
            expectedTime: '1-3s',
        };
    } else {
        return {
            parallelism: 1,   // Single-threaded fallback
            mode: 'wasm-st',  // Single-threaded WASM
            expectedTime: '3-9s',
        };
    }
}

/**
 * Show installing UI with progress steps
 * @param {HTMLElement} container - Container to render into
 */
export function showInstallingUI(container) {
    container.innerHTML = `
        <div class="coi-status installing">
            <div class="coi-header">
                <span class="coi-logo" aria-hidden="true">&#x1F510;</span>
                <h3>Setting Up Secure Environment</h3>
            </div>
            <p class="coi-detail">One-time setup for fast, secure decryption</p>

            <div class="coi-progress-steps">
                <div class="coi-step" id="coi-step-sw" data-status="loading">
                    <span class="coi-step-icon" aria-hidden="true">&#x23F3;</span>
                    <span class="coi-step-text">Installing security worker...</span>
                </div>
                <div class="coi-step" id="coi-step-headers" data-status="pending">
                    <span class="coi-step-icon" aria-hidden="true">&#x25CB;</span>
                    <span class="coi-step-text">Activating isolation headers...</span>
                </div>
            </div>
        </div>
    `;
    container.classList.remove('hidden');
}

/**
 * Update a progress step's status
 * @param {string} stepId - Step element ID
 * @param {'pending'|'loading'|'complete'|'error'} status - New status
 */
export function updateProgressStep(stepId, status) {
    const step = document.getElementById(stepId);
    if (!step) return;

    step.dataset.status = status;
    const icon = step.querySelector('.coi-step-icon');
    if (icon) {
        switch (status) {
            case 'loading':
                icon.innerHTML = '&#x23F3;'; // Hourglass
                break;
            case 'complete':
                icon.innerHTML = '&#x2705;'; // Check mark
                break;
            case 'error':
                icon.innerHTML = '&#x274C;'; // X mark
                break;
            default:
                icon.innerHTML = '&#x25CB;'; // Circle
        }
    }
}

/**
 * Show reload required UI with auto-countdown
 * @param {HTMLElement} container - Container to render into
 * @param {Object} [options] - Configuration options
 * @param {Function} [options.onReload] - Optional callback before reload
 * @param {number} [options.countdownSeconds=3] - Countdown duration
 * @param {boolean} [options.autoReload=true] - Whether to auto-reload
 */
export function showReloadRequiredUI(container, options = {}) {
    const { onReload = null, countdownSeconds = 3, autoReload = true } = options;

    if (activeReloadController) {
        activeReloadController.cancel();
        activeReloadController = null;
    }

    container.innerHTML = `
        <div class="coi-status needs-reload">
            <div class="coi-header">
                <span class="coi-logo" aria-hidden="true">&#x1F510;</span>
                <h3>Almost There!</h3>
            </div>

            <div class="coi-progress-steps">
                <div class="coi-step" data-status="complete">
                    <span class="coi-step-icon" aria-hidden="true">&#x2705;</span>
                    <span class="coi-step-text">Security worker installed</span>
                </div>
                <div class="coi-step" data-status="loading">
                    <span class="coi-step-icon" aria-hidden="true">&#x23F3;</span>
                    <span class="coi-step-text">Activating isolation headers...</span>
                </div>
            </div>

            <div class="coi-reload-section">
                <p class="coi-reload-message">One-time page reload required to enable optimal performance.</p>

                <div id="coi-countdown-wrapper" class="coi-countdown-wrapper ${autoReload ? '' : 'hidden'}">
                    <span class="coi-countdown-text">Reloading in </span>
                    <span id="coi-countdown-number" class="coi-countdown-number">${countdownSeconds}</span>
                    <span class="coi-countdown-text">...</span>
                </div>

                <div class="coi-reload-buttons">
                    <button id="coi-reload-btn" class="btn btn-primary coi-reload-btn">
                        Reload Now
                    </button>
                    <button id="coi-cancel-btn" class="btn btn-secondary coi-cancel-btn ${autoReload ? '' : 'hidden'}">
                        Cancel
                    </button>
                </div>
            </div>

            <details class="coi-details">
                <summary>Why is this needed?</summary>
                <p>
                    Modern browsers require special security headers for
                    hardware-accelerated encryption. After reloading, the
                    archive will:
                </p>
                <ul>
                    <li>Decrypt 3-5x faster using parallel processing</li>
                    <li>Support offline access</li>
                    <li>Use enhanced memory protection</li>
                </ul>
                <p class="coi-note">This is a one-time setup per browser.</p>
            </details>
        </div>
    `;
    container.classList.remove('hidden');

    const reloadBtn = document.getElementById('coi-reload-btn');
    const cancelBtn = document.getElementById('coi-cancel-btn');
    const countdownWrapper = document.getElementById('coi-countdown-wrapper');
    const countdownNumber = document.getElementById('coi-countdown-number');

    let countdown = countdownSeconds;
    let timerId = null;

    const doReload = () => {
        if (timerId) {
            clearInterval(timerId);
            timerId = null;
        }
        if (activeReloadController === control) {
            activeReloadController = null;
        }
        if (onReload) {
            onReload();
        }
        window.location.reload();
    };

    const cancelCountdown = () => {
        if (timerId) {
            clearInterval(timerId);
            timerId = null;
        }
        if (countdownWrapper) {
            countdownWrapper.classList.add('hidden');
        }
        if (cancelBtn) {
            cancelBtn.classList.add('hidden');
        }
        if (activeReloadController === control) {
            activeReloadController = null;
        }
    };

    // Set up event listeners
    if (reloadBtn) {
        reloadBtn.addEventListener('click', doReload);
    }
    if (cancelBtn) {
        cancelBtn.addEventListener('click', cancelCountdown);
    }

    // Start countdown if auto-reload is enabled
    if (autoReload && countdownNumber) {
        timerId = setInterval(() => {
            countdown--;
            if (countdown <= 0) {
                doReload();
            } else {
                countdownNumber.textContent = countdown.toString();
            }
        }, 1000);
    }

    // Return control object for external management
    const control = {
        cancel: cancelCountdown,
        reload: doReload,
    };
    activeReloadController = control;
    return control;
}

/**
 * Show degraded mode warning banner
 * Displayed when COI is not available but app can still function
 */
export function showDegradedModeWarning() {
    // Check if banner already exists
    if (document.querySelector('.coi-degraded-banner')) return;

    const banner = document.createElement('div');
    banner.className = 'coi-degraded-banner';
    banner.innerHTML = `
        <span class="coi-warning-icon">&#x26A0;&#xFE0F;</span>
        <span class="coi-warning-text">Running in compatibility mode - unlock may take longer</span>
        <button class="coi-dismiss-btn" aria-label="Dismiss">&#x2715;</button>
    `;

    const dismissBtn = banner.querySelector('.coi-dismiss-btn');
    if (dismissBtn) {
        dismissBtn.addEventListener('click', () => {
            banner.remove();
        });
    }

    document.body.prepend(banner);
}

/**
 * Hide COI status UI
 * @param {HTMLElement} container - Container to hide
 */
export function hideStatusUI(container) {
    if (activeReloadController) {
        activeReloadController.cancel();
        activeReloadController = null;
    }
    container.classList.add('hidden');
    container.innerHTML = '';
}

/**
 * Initialize COI detection and handle states
 * @param {Object} options - Configuration options
 * @param {HTMLElement} options.statusContainer - Container for status UI
 * @param {HTMLElement} options.authContainer - Auth screen container
 * @param {Function} options.onReady - Callback when ready to proceed
 * @param {number} [options.maxWaitMs=5000] - Max time to wait for SW installation
 * @param {boolean} [options.autoReload=true] - Whether to auto-reload when needed
 * @param {number} [options.countdownSeconds=3] - Countdown duration before auto-reload
 */
export async function initCOIDetection({
    statusContainer,
    authContainer,
    onReady,
    maxWaitMs = 5000,
    autoReload = true,
    countdownSeconds = 3,
}) {
    let state = await getCOIState();

    console.log('[COI] Initial state:', state);

    // If already set up and ready, skip the setup flow
    if (state === COI_STATE.READY && isSetupComplete()) {
        console.log('[COI] Setup already complete - fast path');
        hideStatusUI(statusContainer);
        if (onReady) onReady();
        return state;
    }

    // Handle SW_INSTALLING state with timeout
    if (state === COI_STATE.SW_INSTALLING) {
        showInstallingUI(statusContainer);

        // Wait for SW to become active
        if ('serviceWorker' in navigator) {
            try {
                await Promise.race([
                    navigator.serviceWorker.ready,
                    new Promise((_, reject) =>
                        setTimeout(() => reject(new Error('SW timeout')), maxWaitMs)
                    ),
                ]);

                // Update step status
                updateProgressStep('coi-step-sw', 'complete');
                updateProgressStep('coi-step-headers', 'loading');

                // Recheck state after SW is ready
                state = await getCOIState();
                console.log('[COI] State after SW ready:', state);
            } catch (error) {
                console.warn('[COI] SW wait timeout or error:', error.message);
                // Continue with current state
                state = await getCOIState();
            }
        }
    }

    // Handle final state
    switch (state) {
        case COI_STATE.READY:
            console.log('[COI] Ready - proceeding to auth');
            markSetupComplete();
            hideStatusUI(statusContainer);
            if (onReady) onReady();
            break;

        case COI_STATE.NEEDS_RELOAD:
            console.log('[COI] Needs reload - showing prompt');
            showReloadRequiredUI(statusContainer, {
                autoReload,
                countdownSeconds,
                onReload: () => console.log('[COI] Reloading...'),
            });
            // Hide auth screen while showing reload prompt
            if (authContainer) {
                authContainer.classList.add('hidden');
            }
            break;

        case COI_STATE.DEGRADED:
            console.log('[COI] Degraded mode - showing warning and proceeding');
            markSetupComplete(); // Still mark complete so we don't keep showing setup
            hideStatusUI(statusContainer);
            showDegradedModeWarning();
            if (onReady) onReady();
            break;

        case COI_STATE.SW_INSTALLING:
            // Still installing after timeout - check if we should show reload or proceed
            console.log('[COI] SW still installing - checking fallback');
            if (!await hasServiceWorkerRegistration()) {
                console.warn('[COI] No service worker registration found after waiting - degrading');
                hideStatusUI(statusContainer);
                showDegradedModeWarning();
                if (onReady) onReady();
                return COI_STATE.DEGRADED;
            }
            if (isSharedArrayBufferAvailable()) {
                // Already have SAB somehow (maybe browser feature)
                markSetupComplete();
                hideStatusUI(statusContainer);
                if (onReady) onReady();
            } else {
                // Show reload prompt as SW should be active soon
                showReloadRequiredUI(statusContainer, {
                    autoReload,
                    countdownSeconds,
                    onReload: () => console.log('[COI] Reloading...'),
                });
                if (authContainer) {
                    authContainer.classList.add('hidden');
                }
            }
            break;
    }

    return state;
}

/**
 * Listen for SW activation and trigger recheck
 * @param {Function} callback - Called when SW activates
 */
export function onServiceWorkerActivated(callback) {
    if (!('serviceWorker' in navigator) || typeof callback !== 'function') {
        return () => {};
    }

    serviceWorkerActivationCallbacks.add(callback);

    if (!serviceWorkerActivationListenersInstalled) {
        const notifyActivation = (reason) => {
            if (serviceWorkerActivationDispatchScheduled) {
                return;
            }

            serviceWorkerActivationDispatchScheduled = true;
            queueMicrotask(() => {
                serviceWorkerActivationDispatchScheduled = false;
                console.log('[COI] Service worker activation detected:', reason);
                [...serviceWorkerActivationCallbacks].forEach((registeredCallback) => {
                    try {
                        Promise.resolve(registeredCallback()).catch((error) => {
                            console.error('[COI] Activation callback failed:', error);
                        });
                    } catch (error) {
                        console.error('[COI] Activation callback failed:', error);
                    }
                });
            });
        };

        navigator.serviceWorker.addEventListener('message', (event) => {
            if (event.data?.type === 'SW_ACTIVATED') {
                notifyActivation('message');
            }
        });

        navigator.serviceWorker.addEventListener('controllerchange', () => {
            notifyActivation('controllerchange');
        });

        serviceWorkerActivationListenersInstalled = true;
    }

    return () => {
        serviceWorkerActivationCallbacks.delete(callback);
    };
}

// Export default
export default {
    COI_STATE,
    isCrossOriginIsolated,
    isServiceWorkerActive,
    hasServiceWorkerRegistration,
    isServiceWorkerSupported,
    isSharedArrayBufferAvailable,
    getCOIState,
    getArgon2Config,
    showInstallingUI,
    showReloadRequiredUI,
    showDegradedModeWarning,
    hideStatusUI,
    initCOIDetection,
    onServiceWorkerActivated,
    updateProgressStep,
    isSetupComplete,
    markSetupComplete,
    clearSetupComplete,
};