/**
 * olam memory logs — stream the agent-memory Docker container's stdout/stderr.
 *
 * Phase B substrate: shells to `docker logs olam-memory-service`. The Phase A
 * file-tail path (`tail -f ~/.olam/memory-service.log`) is retired because the
 * log file no longer exists once the container substrate is in place.
 *
 *   --follow (alias -f): docker logs --follow (Ctrl-C to stop).
 *   --tail N (default 50): docker logs --tail N.
 *
 * Plan reference: docs/plans/memory-service-as-docker-peripheral/phase-b-tasks.md B4
 */
import { spawn } from 'node:child_process';
import { printWarning, printHeader } from '../../output.js';
import { MEMORY_SERVICE_CONTAINER, MemoryServiceContainerController } from '../memory-service-container.js';
export async function runMemoryLogs(opts) {
    const controller = new MemoryServiceContainerController();
    const status = controller.status();
    if (status.state === 'missing') {
        printWarning(`no olam-memory-service container exists. Start it via 'olam memory start'.`);
        return 1;
    }
    const tailN = opts.tail ? parseInt(opts.tail, 10) : 50;
    if (!Number.isFinite(tailN) || tailN < 0) {
        printWarning(`invalid --tail value: ${opts.tail}`);
        return 1;
    }
    const args = ['logs', '--tail', String(tailN)];
    if (opts.follow)
        args.push('--follow');
    args.push(MEMORY_SERVICE_CONTAINER);
    printHeader(`olam memory logs (${opts.follow ? 'follow' : `tail -n ${tailN}`})`);
    const child = spawn('docker', args, { stdio: 'inherit' });
    return new Promise((resolve) => {
        child.on('exit', (code) => resolve(code ?? 0));
    });
}
export function registerMemoryLogs(cmd) {
    cmd
        .command('logs')
        .description('Stream the olam-memory-service container logs (Phase B substrate).')
        .option('-f, --follow', 'Stream new log lines (Ctrl-C to stop)', false)
        .option('--tail <n>', 'Number of trailing lines (default 50)', '50')
        .action(async (opts) => {
        const rc = await runMemoryLogs(opts);
        if (rc !== 0)
            process.exitCode = rc;
    });
}
//# sourceMappingURL=logs.js.map