@orgloop/agentctl 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,96 @@
1
+ import type { AgentAdapter, AgentSession, LaunchOpts, LifecycleEvent, ListOpts, PeekOpts, StopOpts } from "../core/types.js";
2
+ export interface PidInfo {
3
+ pid: number;
4
+ cwd: string;
5
+ args: string;
6
+ /** Process start time from `ps -p <pid> -o lstart=`, used to detect PID recycling */
7
+ startTime?: string;
8
+ }
9
+ /** Metadata persisted by launch() so status checks survive wrapper exit */
10
+ export interface LaunchedSessionMeta {
11
+ sessionId: string;
12
+ pid: number;
13
+ /** Process start time from `ps -p <pid> -o lstart=` for PID recycling detection */
14
+ startTime?: string;
15
+ /** The PID of the wrapper (agentctl launch) — may differ from `pid` (pi process) */
16
+ wrapperPid?: number;
17
+ cwd: string;
18
+ model?: string;
19
+ prompt?: string;
20
+ launchedAt: string;
21
+ }
22
+ export interface PiAdapterOpts {
23
+ piDir?: string;
24
+ sessionsMetaDir?: string;
25
+ getPids?: () => Promise<Map<number, PidInfo>>;
26
+ /** Override PID liveness check for testing (default: process.kill(pid, 0)) */
27
+ isProcessAlive?: (pid: number) => boolean;
28
+ }
29
+ /**
30
+ * Pi adapter — reads session data from ~/.pi/agent/sessions/
31
+ * and cross-references with running PIDs. NEVER maintains its own registry.
32
+ *
33
+ * Pi stores sessions as JSONL files in ~/.pi/agent/sessions/<cwd-slug>/<timestamp>_<id>.jsonl
34
+ * Each file starts with a type:'session' header line containing metadata.
35
+ */
36
+ export declare class PiAdapter implements AgentAdapter {
37
+ readonly id = "pi";
38
+ private readonly piDir;
39
+ private readonly sessionsDir;
40
+ private readonly sessionsMetaDir;
41
+ private readonly getPids;
42
+ private readonly isProcessAlive;
43
+ constructor(opts?: PiAdapterOpts);
44
+ list(opts?: ListOpts): Promise<AgentSession[]>;
45
+ peek(sessionId: string, opts?: PeekOpts): Promise<string>;
46
+ /** Extract assistant messages from a JSONL session file */
47
+ private peekFromJsonl;
48
+ /** Get the log file path for a pending session from metadata */
49
+ private getLogPathForSession;
50
+ status(sessionId: string): Promise<AgentSession>;
51
+ launch(opts: LaunchOpts): Promise<AgentSession>;
52
+ /**
53
+ * Poll the launch log file for up to `timeoutMs` to extract the real session ID.
54
+ * Pi's JSONL output includes a session header with type: "session" and id field.
55
+ */
56
+ private pollForSessionId;
57
+ /**
58
+ * Fallback: poll the Pi sessions directory for a new JSONL file
59
+ * created after the launch time, matching the cwd.
60
+ */
61
+ private pollSessionDir;
62
+ stop(sessionId: string, opts?: StopOpts): Promise<void>;
63
+ resume(sessionId: string, message: string): Promise<void>;
64
+ events(): AsyncIterable<LifecycleEvent>;
65
+ /**
66
+ * Scan ~/.pi/agent/sessions/ recursively for .jsonl files and parse headers.
67
+ * Pi stores sessions at <sessionsDir>/<cwd-slug>/<timestamp>_<id>.jsonl
68
+ */
69
+ private discoverSessions;
70
+ /** Extract session ID from filename format: <timestamp>_<id>.jsonl */
71
+ private extractSessionIdFromFilename;
72
+ /** Parse the session header (type:'session') from the first few lines of a JSONL file */
73
+ private parseSessionHeader;
74
+ private buildSession;
75
+ private isSessionRunning;
76
+ /**
77
+ * Check whether a process plausibly belongs to a session by verifying
78
+ * the process started at or after the session's creation time.
79
+ * When start time is unavailable, defaults to false (assume no match).
80
+ */
81
+ private processStartedAfterSession;
82
+ private findMatchingPid;
83
+ /** Parse session tail for model, tokens, and cost aggregation */
84
+ private parseSessionTail;
85
+ /** Get the first user prompt from a session JSONL file */
86
+ private getFirstPrompt;
87
+ /** Find a session by exact or prefix ID match */
88
+ private findSession;
89
+ private findPidForSession;
90
+ /** Write session metadata to disk so status checks survive wrapper exit */
91
+ writeSessionMeta(meta: Omit<LaunchedSessionMeta, "startTime">): Promise<void>;
92
+ /** Read persisted session metadata */
93
+ readSessionMeta(sessionId: string): Promise<LaunchedSessionMeta | null>;
94
+ /** Delete stale session metadata */
95
+ private deleteSessionMeta;
96
+ }