// Read-only instrumentation for the real app; no routing state is modified.
export async function observeResources(browser: any, ids: string[]) {
  return browser.evaluate(async (ids: string[]) => {
    const { appAtomRegistry } = await import("/src/rpc/atomRegistry.ts");
    const { serverEnvironment } = await import("/src/state/server.ts");
    return Promise.all(ids.map(environmentId => new Promise((resolve, reject) => {
      const atom = serverEnvironment.hostResources({ environmentId, input: {} });
      let off = () => {};
      const timeout = setTimeout(() => { off(); reject(new Error("Resource read did not complete")); }, 15000);
      appAtomRegistry.refresh(atom);
      off = appAtomRegistry.subscribe(atom, (result: any) => {
        if (result._tag === "Success" && !result.waiting) {
          clearTimeout(timeout); queueMicrotask(() => off()); resolve(result.value);
        }
      }, { immediate: true });
    })));
  }, ids);
}
export async function annotate(browser: any, text: string) {
  await browser.evaluate((text: string) => {
    let panel = document.getElementById("routing-proof-observation");
    if (!panel) {
      panel = document.createElement("aside"); panel.id = "routing-proof-observation";
      panel.style.cssText = "position:fixed;z-index:9999;left:12px;top:145px;width:226px;padding:14px;box-sizing:border-box;background:#f0f7ff;color:#162c43;border:1px solid #90b7db;border-radius:8px;font:13px/1.65 system-ui;white-space:pre-line;pointer-events:none";
      document.body.append(panel);
    }
    panel.textContent = text;
  }, text);
}
