package com.gzzm.lobster.runtime;

import java.util.function.Consumer;

/**
 * RunEventBus —— 进程内 run 事件分发 / In-process run event dispatcher.
 *
 * <p><b>非</b>事件溯源、<b>非</b>持久化 —— 纯内存、纯进程内。用途是把
 * {@link AgentRuntime} 主循环从散落的 {@code emitter.xxx} / {@code auditService.record(...)}
 * 抽出来：主循环只 {@link #publish publish}，SSE / audit / test hook 各自 {@link #subscribe}。
 *
 * <p>实现见 {@link InMemoryRunEventBus}。
 */
public interface RunEventBus {

    /**
     * 同步分发一个事件 / Publish an event synchronously.
     *
     * <p>订阅者的 {@link Consumer#accept} 在本调用线程执行；任何订阅者抛异常
     * 都会被吞掉并日志（不能让一个坏订阅者影响主循环）。
     */
    void publish(RunEvent event);

    /**
     * 注册订阅者 / Subscribe.
     *
     * @return 句柄 {@link AutoCloseable}，{@code close()} 取消订阅；
     *         订阅期间 bus 生命周期长于订阅者时必须记得 close。
     */
    AutoCloseable subscribe(Consumer<RunEvent> listener);
}
