@justjoshn/pi-pstack-v2 0.1.6 → 0.1.7
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.
- package/package.json +1 -1
- package/src/subagent/README.md +2 -1
- package/src/subagent/agents.ts +12 -5
- package/src/subagent/run-in-tmux.ts +32 -13
- package/src/subagent/types.ts +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@justjoshn/pi-pstack-v2",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Use the pstack skills and workflow in Pi, with the upstream subagent tool spawning Pi in tmux and shell jobs running in tmux.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
package/src/subagent/README.md
CHANGED
|
@@ -140,10 +140,11 @@ System prompt for the agent goes here.
|
|
|
140
140
|
When `model` is omitted, the subagent inherits the dispatching session's active model and thinking level.
|
|
141
141
|
|
|
142
142
|
**Locations:**
|
|
143
|
+
- `src/subagent/agents/*.md` - Bundled with this package (every scope)
|
|
143
144
|
- `~/.pi/agent/agents/*.md` - User-level (with `agentScope: "user"` or `"both"`)
|
|
144
145
|
- `.pi/agents/*.md` - Project-level (only with `agentScope: "project"` or `"both"`)
|
|
145
146
|
|
|
146
|
-
|
|
147
|
+
User agents override bundled agents with the same name, and project agents override both.
|
|
147
148
|
|
|
148
149
|
## Sample Agents
|
|
149
150
|
|
package/src/subagent/agents.ts
CHANGED
|
@@ -4,19 +4,25 @@
|
|
|
4
4
|
|
|
5
5
|
import * as fs from 'node:fs';
|
|
6
6
|
import * as path from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
7
8
|
|
|
8
9
|
import { CONFIG_DIR_NAME, getAgentDir, parseFrontmatter } from '@earendil-works/pi-coding-agent';
|
|
9
10
|
import { z } from 'zod';
|
|
10
11
|
|
|
11
12
|
export type AgentScope = 'user' | 'project' | 'both';
|
|
12
13
|
|
|
14
|
+
/** Where an agent came from. Package agents ship in this extension's `agents` directory. */
|
|
15
|
+
export type AgentSource = 'package' | 'user' | 'project';
|
|
16
|
+
|
|
17
|
+
const PACKAGE_AGENTS_DIR = fileURLToPath(new URL('./agents', import.meta.url));
|
|
18
|
+
|
|
13
19
|
export interface AgentConfig {
|
|
14
20
|
name: string;
|
|
15
21
|
description: string;
|
|
16
22
|
tools?: string[] | undefined;
|
|
17
23
|
model?: string | undefined;
|
|
18
24
|
systemPrompt: string;
|
|
19
|
-
source:
|
|
25
|
+
source: AgentSource;
|
|
20
26
|
filePath: string;
|
|
21
27
|
}
|
|
22
28
|
|
|
@@ -58,7 +64,7 @@ const AgentFrontmatterSchema = z.looseObject({
|
|
|
58
64
|
model: z.unknown().optional(),
|
|
59
65
|
});
|
|
60
66
|
|
|
61
|
-
function readAgentFile(dir: string, entry: fs.Dirent, source:
|
|
67
|
+
function readAgentFile(dir: string, entry: fs.Dirent, source: AgentSource): AgentConfig | undefined {
|
|
62
68
|
if (!entry.name.endsWith('.md')) return undefined;
|
|
63
69
|
if (!entry.isFile() && !entry.isSymbolicLink()) return undefined;
|
|
64
70
|
|
|
@@ -77,7 +83,7 @@ function readAgentFile(dir: string, entry: fs.Dirent, source: 'user' | 'project'
|
|
|
77
83
|
return { name, description, tools: parseToolList(tools), model: typeof model === 'string' ? model : undefined, systemPrompt: body, source, filePath };
|
|
78
84
|
}
|
|
79
85
|
|
|
80
|
-
function loadAgentsFromDir(dir: string, source:
|
|
86
|
+
function loadAgentsFromDir(dir: string, source: AgentSource): AgentConfig[] {
|
|
81
87
|
if (!fs.existsSync(dir)) return [];
|
|
82
88
|
|
|
83
89
|
let entries: fs.Dirent[];
|
|
@@ -121,8 +127,9 @@ export function discoverAgents(cwd: string, scope: AgentScope): AgentDiscoveryRe
|
|
|
121
127
|
const userAgents = scope === 'user' || scope === 'both' ? loadAgentsFromDir(userDir, 'user') : [];
|
|
122
128
|
const projectAgents = scope === 'user' || !projectAgentsDir ? [] : loadAgentsFromDir(projectAgentsDir, 'project');
|
|
123
129
|
|
|
124
|
-
// Later entries win
|
|
125
|
-
const
|
|
130
|
+
// Later entries win: user agents override the agents this package ships, and project agents override both.
|
|
131
|
+
const packageAgents = loadAgentsFromDir(PACKAGE_AGENTS_DIR, 'package');
|
|
132
|
+
const agentMap = new Map([...packageAgents, ...userAgents, ...projectAgents].map((agent) => [agent.name, agent] as const));
|
|
126
133
|
|
|
127
134
|
return { agents: Array.from(agentMap.values()), projectAgentsDir };
|
|
128
135
|
}
|
|
@@ -3,6 +3,8 @@ import * as fs from 'node:fs';
|
|
|
3
3
|
import * as os from 'node:os';
|
|
4
4
|
import * as path from 'node:path';
|
|
5
5
|
|
|
6
|
+
import { hostEnv } from '../host-env.ts';
|
|
7
|
+
|
|
6
8
|
export interface ProcessOutcome {
|
|
7
9
|
code: number | null;
|
|
8
10
|
signal: NodeJS.Signals | null;
|
|
@@ -18,13 +20,28 @@ function shellQuote(value: string): string {
|
|
|
18
20
|
return `'${value.replace(/'/g, `'"'"'`)}'`;
|
|
19
21
|
}
|
|
20
22
|
|
|
21
|
-
const
|
|
23
|
+
const SOCKET_NAME = 'tmux.sock';
|
|
22
24
|
|
|
23
|
-
/**
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Each run gets its own tmux server, started with this process's environment, so the child inherits the dispatching
|
|
27
|
+
* Pi's agent dir, credentials, and PATH. A shared server would hand it the environment that server started with.
|
|
28
|
+
* tmux reaches the socket by a relative path from the run directory, which keeps it under the Unix socket path limit.
|
|
29
|
+
*/
|
|
30
|
+
function tmuxArgs(dir: string, args: string[]): [string, string[], { cwd: string; env: NodeJS.ProcessEnv }] {
|
|
31
|
+
const { TMUX: _tmux, TMUX_PANE: _pane, ...env } = hostEnv();
|
|
32
|
+
return ['tmux', ['-S', SOCKET_NAME, ...args], { cwd: dir, env }];
|
|
26
33
|
}
|
|
27
34
|
|
|
35
|
+
function tmuxOk(dir: string, args: string[]): boolean {
|
|
36
|
+
const [command, argv, options] = tmuxArgs(dir, args);
|
|
37
|
+
return spawnSync(command, argv, { ...options, stdio: 'ignore' }).status === 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A server that is already gone makes this fail, which is expected. */
|
|
41
|
+
const tmuxKillServer = (dir: string): void => {
|
|
42
|
+
tmuxOk(dir, ['kill-server']);
|
|
43
|
+
};
|
|
44
|
+
|
|
28
45
|
const sleep = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms));
|
|
29
46
|
|
|
30
47
|
function readRange(file: string, from: number, to: number): Buffer {
|
|
@@ -82,16 +99,17 @@ function prepareRunFiles(command: string, args: string[]): RunFiles {
|
|
|
82
99
|
|
|
83
100
|
interface WaitContext {
|
|
84
101
|
exitFile: string;
|
|
102
|
+
dir: string;
|
|
85
103
|
session: string;
|
|
86
104
|
pump: () => void;
|
|
87
105
|
aborted: () => boolean;
|
|
88
106
|
}
|
|
89
107
|
|
|
90
108
|
/** Polls until the exit file appears. Resolves false when the session vanished without writing one. */
|
|
91
|
-
async function waitForExit({ exitFile, session, pump, aborted }: WaitContext): Promise<boolean> {
|
|
109
|
+
async function waitForExit({ exitFile, dir, session, pump, aborted }: WaitContext): Promise<boolean> {
|
|
92
110
|
while (!fs.existsSync(exitFile)) {
|
|
93
111
|
pump();
|
|
94
|
-
const alive =
|
|
112
|
+
const alive = tmuxOk(dir, ['has-session', '-t', `=${session}`]);
|
|
95
113
|
if (!alive && aborted()) return true;
|
|
96
114
|
if (!alive) {
|
|
97
115
|
await sleep(POLL_INTERVAL_MS);
|
|
@@ -121,26 +139,27 @@ export async function runPiInTmux(options: {
|
|
|
121
139
|
signal?: AbortSignal | undefined;
|
|
122
140
|
onStdoutChunk: (chunk: string) => void;
|
|
123
141
|
onStderrChunk: (chunk: string) => void;
|
|
124
|
-
/** Test hook: called after the tmux session exists and before waiting for exit. */
|
|
125
|
-
onSessionStarted?: ((session: string) => void) | undefined;
|
|
142
|
+
/** Test hook: called after the tmux session exists and before waiting for exit, with the session and its server's socket. */
|
|
143
|
+
onSessionStarted?: ((session: string, socket: string) => void) | undefined;
|
|
126
144
|
}): Promise<ProcessOutcome> {
|
|
127
145
|
const session = `pi-subagent-${process.pid}-${Date.now().toString(BASE36)}-${Math.random().toString(BASE36).slice(2, SESSION_SUFFIX_END)}`;
|
|
128
146
|
const files = prepareRunFiles(options.command, options.args);
|
|
129
147
|
|
|
130
148
|
try {
|
|
131
|
-
|
|
149
|
+
const [command, argv, spawnOptions] = tmuxArgs(files.dir, ['new-session', '-d', '-s', session, '-c', options.cwd, 'bash', files.wrapper]);
|
|
150
|
+
execFileSync(command, argv, { ...spawnOptions, stdio: 'ignore' });
|
|
132
151
|
} catch (error) {
|
|
133
152
|
fs.rmSync(files.dir, { recursive: true, force: true });
|
|
134
153
|
return { code: null, signal: null, spawnError: error instanceof Error ? error : new Error(String(error)) };
|
|
135
154
|
}
|
|
136
155
|
|
|
137
|
-
options.onSessionStarted?.(session);
|
|
156
|
+
options.onSessionStarted?.(session, path.join(files.dir, SOCKET_NAME));
|
|
138
157
|
|
|
139
158
|
let wasAborted = false;
|
|
140
159
|
const abort = () => {
|
|
141
160
|
if (wasAborted) return;
|
|
142
161
|
wasAborted = true;
|
|
143
|
-
|
|
162
|
+
tmuxKillServer(files.dir);
|
|
144
163
|
};
|
|
145
164
|
if (options.signal?.aborted) abort();
|
|
146
165
|
else options.signal?.addEventListener('abort', abort, { once: true });
|
|
@@ -153,13 +172,13 @@ export async function runPiInTmux(options: {
|
|
|
153
172
|
};
|
|
154
173
|
|
|
155
174
|
try {
|
|
156
|
-
const exited = await waitForExit({ exitFile: files.exitFile, session, pump, aborted: () => wasAborted });
|
|
175
|
+
const exited = await waitForExit({ exitFile: files.exitFile, dir: files.dir, session, pump, aborted: () => wasAborted });
|
|
157
176
|
if (!exited) return { code: null, signal: wasAborted ? 'SIGTERM' : null };
|
|
158
177
|
pump();
|
|
159
178
|
return readOutcome(files.exitFile, wasAborted);
|
|
160
179
|
} finally {
|
|
161
180
|
options.signal?.removeEventListener('abort', abort);
|
|
162
|
-
|
|
181
|
+
tmuxKillServer(files.dir);
|
|
163
182
|
fs.rmSync(files.dir, { recursive: true, force: true });
|
|
164
183
|
}
|
|
165
184
|
}
|
package/src/subagent/types.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AgentToolResult, ThinkingLevel } from '@earendil-works/pi-agent-core';
|
|
2
2
|
import type { Message } from '@earendil-works/pi-ai';
|
|
3
|
-
import type { AgentConfig } from './agents.ts';
|
|
3
|
+
import type { AgentConfig, AgentSource } from './agents.ts';
|
|
4
4
|
|
|
5
5
|
export interface UsageStats {
|
|
6
6
|
input: number;
|
|
@@ -16,7 +16,7 @@ export type ChildStatus = 'pending' | 'running' | 'completed' | 'failed' | 'abor
|
|
|
16
16
|
|
|
17
17
|
export interface SingleResult {
|
|
18
18
|
agent: string;
|
|
19
|
-
agentSource:
|
|
19
|
+
agentSource: AgentSource | 'unknown';
|
|
20
20
|
task: string;
|
|
21
21
|
status: ChildStatus;
|
|
22
22
|
exitCode: number | null;
|