Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/integration/assets/pi/herdr-agent-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// managed by herdr; reinstalling or updating the integration overwrites this file.
// add custom hooks/plugins beside this file instead of editing it.
// HERDR_INTEGRATION_ID=pi
// HERDR_INTEGRATION_VERSION=2
// HERDR_INTEGRATION_VERSION=3
// @ts-nocheck

import { createConnection } from "node:net";
Expand Down Expand Up @@ -73,6 +73,18 @@ function parseDurationEnv(name: string, fallback: number): number {
return parsed;
}

function flushSessionBeforeReport(ctx: any): void {
try {
// Pi normally defers creating a session file until the first assistant
// response. External resume needs the initial model/thinking state on disk
// before we advertise the path to Herdr. Optional for older Pi versions.
ctx?.sessionManager?.flush?.();
} catch {
// Session reporting must remain best-effort; an unsupported or failed flush
// must not break Pi startup or Herdr state reporting.
}
}

function updateSessionRef(ctx: any): void {
try {
const file = ctx?.sessionManager?.getSessionFile?.();
Expand Down Expand Up @@ -251,6 +263,7 @@ export default function (pi) {
}

pi.on("session_start", (_event, ctx) => {
flushSessionBeforeReport(ctx);
updateSessionRef(ctx);
publishState(true);
});
Expand Down
4 changes: 3 additions & 1 deletion src/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::layout::PaneId;
pub(crate) const HERDR_PANE_ID_ENV_VAR: &str = "HERDR_PANE_ID";
const PI_EXTENSION_INSTALL_NAME: &str = "herdr-agent-state.ts";
const PI_EXTENSION_ASSET: &str = include_str!("assets/pi/herdr-agent-state.ts");
const PI_INTEGRATION_VERSION: u32 = 2;
const PI_INTEGRATION_VERSION: u32 = 3;
const OMP_EXTENSION_INSTALL_NAME: &str = "herdr-omp-agent-state.ts";
const OMP_EXTENSION_ASSET: &str = include_str!("assets/omp/herdr-agent-state.ts");
const OMP_INTEGRATION_VERSION: u32 = 2;
Expand Down Expand Up @@ -2113,6 +2113,8 @@ mod tests {

assert_eq!(path, ext_dir.join(PI_EXTENSION_INSTALL_NAME));
assert_eq!(content, PI_EXTENSION_ASSET);
assert!(content.contains("sessionManager?.flush?.()"));
assert!(content.contains("HERDR_INTEGRATION_VERSION=3"));

std::env::remove_var("HOME");
let _ = fs::remove_dir_all(base);
Expand Down
Loading