Compare commits
16 Commits
25e76679c5
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| c1a65f68ac | |||
| 1aec74216a | |||
| 807704aabe | |||
| 3cc7c0469b | |||
| c870efa15a | |||
| 100262b94f | |||
| c2525a6411 | |||
| 85438c4a3e | |||
| 5466630dbd | |||
| 5e7cee15e3 | |||
| 7594ca06f5 | |||
| 890988b72c | |||
| 540862d7d0 | |||
| cb35ee044c | |||
| 3b586ea92d | |||
| 7ef6e3d9a2 |
100
.gitea/workflows/port-to-omp.yml
Normal file
100
.gitea/workflows/port-to-omp.yml
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
name: port-to-omp
|
||||||
|
|
||||||
|
# Keep the omp port of ralpi in lockstep with this repo.
|
||||||
|
#
|
||||||
|
# Two jobs:
|
||||||
|
# - verify: port to a scratch dir, then typecheck + test the ported tree on
|
||||||
|
# EVERY push and PR. Source drift — a base change that breaks a port op —
|
||||||
|
# fails here, on the branch that introduced it, before it reaches master.
|
||||||
|
# (The ported tree contains the full test suite, so this also gives the
|
||||||
|
# base repo its CI test coverage.)
|
||||||
|
# - publish: regenerate the omp port into Mike/omp-ralpi and push it, but
|
||||||
|
# only on master, and only after verify passed — a broken port can never
|
||||||
|
# ship. The port commit lands in the omp repo, never here, so this
|
||||||
|
# workflow cannot re-trigger itself.
|
||||||
|
#
|
||||||
|
# Prerequisites on git.freno.me:
|
||||||
|
# - an access token with write:repository scope, stored as the repo secret
|
||||||
|
# PORTING_KEY (the workflow authenticates as https://Mike:<token>@…)
|
||||||
|
# - a registered Actions runner (act_runner) for this repo
|
||||||
|
# - the omp repo must exist (Mike/omp-ralpi)
|
||||||
|
#
|
||||||
|
# Manual run: Actions tab → Run workflow (workflow_dispatch), or push.
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
verify:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install bun
|
||||||
|
uses: oven-sh/setup-bun@v2
|
||||||
|
|
||||||
|
- name: Port to scratch dir (drift check)
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
bun port-to-omp.mjs --out "$RUNNER_TEMP/omp-port-check"
|
||||||
|
cd "$RUNNER_TEMP/omp-port-check"
|
||||||
|
bun run typecheck
|
||||||
|
bun test
|
||||||
|
|
||||||
|
publish:
|
||||||
|
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
|
||||||
|
needs: verify
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install bun
|
||||||
|
uses: oven-sh/setup-bun@v2
|
||||||
|
|
||||||
|
- name: Port to omp
|
||||||
|
env:
|
||||||
|
PORTING_KEY: ${{ secrets.PORTING_KEY }}
|
||||||
|
OMP_REPO: omp-ralpi
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
# Trim the secret: a stray newline from pasting silently breaks
|
||||||
|
# basic auth. Fail loudly when it is missing entirely.
|
||||||
|
PORTING_KEY="$(printf '%s' "${PORTING_KEY}" | tr -d '[:space:]')"
|
||||||
|
: "${PORTING_KEY:?PORTING_KEY secret is not set}" || exit 1
|
||||||
|
URL="https://Mike:${PORTING_KEY}@git.freno.me/Mike/${OMP_REPO}.git"
|
||||||
|
|
||||||
|
# The omp checkout lives in $RUNNER_TEMP, outside the pi checkout:
|
||||||
|
# the port script refuses to write into a subdirectory of its own
|
||||||
|
# source (cpSync would recurse into itself).
|
||||||
|
PORT_DIR="${RUNNER_TEMP:-/tmp}/omp-port"
|
||||||
|
|
||||||
|
# Preflight: reach the omp repo with the token. Fail with a clear
|
||||||
|
# message instead of a confusing error later at push time.
|
||||||
|
if ! git ls-remote "$URL" HEAD >/dev/null 2>&1; then
|
||||||
|
echo "::error::cannot read Mike/omp-ralpi with PORTING_KEY — is the secret set on this repo, valid, and write:repository-scoped?"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
git clone --depth 1 "$URL" "$PORT_DIR"
|
||||||
|
git -C "$PORT_DIR" config user.name "omp-port"
|
||||||
|
git -C "$PORT_DIR" config user.email "omp-port@freno.me"
|
||||||
|
|
||||||
|
# Regenerate the port directly into the omp checkout. The script
|
||||||
|
# preserves .git, asserts every patch rule, and runs `bun install`
|
||||||
|
# (refreshing bun.lock + node_modules).
|
||||||
|
bun "$GITHUB_WORKSPACE/port-to-omp.mjs" --out "$PORT_DIR"
|
||||||
|
|
||||||
|
cd "$PORT_DIR"
|
||||||
|
# The port must compile against the pinned @oh-my-pi SDK before it
|
||||||
|
# ships to users.
|
||||||
|
bun run typecheck
|
||||||
|
|
||||||
|
if git diff --quiet HEAD; then
|
||||||
|
echo "port unchanged; nothing to push"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
git add -A
|
||||||
|
git commit -m "port: sync from ${GITHUB_REPOSITORY}@${GITHUB_SHA::8}"
|
||||||
|
git push origin HEAD:main
|
||||||
51
.gitea/workflows/publish.yml
Normal file
51
.gitea/workflows/publish.yml
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
name: publish
|
||||||
|
|
||||||
|
# Publish this package and its omp port to the npm registry on version tags.
|
||||||
|
#
|
||||||
|
# Prerequisites on npmjs.com / git.freno.me:
|
||||||
|
# - an npm automation token (publish scope) stored as the repo secret
|
||||||
|
# NPM_TOKEN (consumed via actions/setup-node registry auth)
|
||||||
|
# - the scoped package names claimed on npm (@mikefreno/<name> and
|
||||||
|
# @mikefreno/omp-<name>)
|
||||||
|
#
|
||||||
|
# Both packages ship TypeScript sources — nothing is built — so lifecycle
|
||||||
|
# scripts (prepublishOnly tsc) are skipped: the port workflow already
|
||||||
|
# typechecks the generated port against the pinned @oh-my-pi SDK, and the pi
|
||||||
|
# package typechecks against the global pi SDK that CI does not install.
|
||||||
|
#
|
||||||
|
# Manual publish: Actions tab → Run workflow (workflow_dispatch).
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags: ['v*']
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
publish:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install bun
|
||||||
|
uses: oven-sh/setup-bun@v2
|
||||||
|
|
||||||
|
- name: Setup node (npm registry auth)
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: '22'
|
||||||
|
registry-url: 'https://registry.npmjs.org'
|
||||||
|
|
||||||
|
- name: Publish pi package
|
||||||
|
env:
|
||||||
|
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
||||||
|
run: npm publish --access public --ignore-scripts
|
||||||
|
|
||||||
|
- name: Port to omp
|
||||||
|
run: bun port-to-omp.mjs --out "$RUNNER_TEMP/omp-port"
|
||||||
|
|
||||||
|
- name: Publish omp package
|
||||||
|
working-directory: ${{ runner.temp }}/omp-port
|
||||||
|
env:
|
||||||
|
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
||||||
|
run: npm publish --access public --ignore-scripts
|
||||||
22
AGENTS.md
22
AGENTS.md
@@ -36,7 +36,7 @@ is unavailable).
|
|||||||
|
|
||||||
- `index.ts` — extension entry, command registration (`ralpi`, `ralpi-run`,
|
- `index.ts` — extension entry, command registration (`ralpi`, `ralpi-run`,
|
||||||
`ralpi-plan`, `ralpi-resume`, `ralpi-reset`), execution-mode + loop-option
|
`ralpi-plan`, `ralpi-resume`, `ralpi-reset`), execution-mode + loop-option
|
||||||
prompts, reload auto-resume via `session_start`, progress message renderer
|
prompts, progress message renderer
|
||||||
- `src/` — all logic modules:
|
- `src/` — all logic modules:
|
||||||
- `parser.ts` — task file parsing (Fio/README numbered, phased, checkbox,
|
- `parser.ts` — task file parsing (Fio/README numbered, phased, checkbox,
|
||||||
YAML formats), dependency + parallel-group + timeout parsing,
|
YAML formats), dependency + parallel-group + timeout parsing,
|
||||||
@@ -62,7 +62,6 @@ is unavailable).
|
|||||||
- `constants.ts` — static constants (slash command, task file names,
|
- `constants.ts` — static constants (slash command, task file names,
|
||||||
reflection/review patterns)
|
reflection/review patterns)
|
||||||
- `tests/` — bun test suites for parser and DAG behavior
|
- `tests/` — bun test suites for parser and DAG behavior
|
||||||
- `skills/ralpi-use.md` — Pi skill definition for task execution
|
|
||||||
- `prompts/task-manager.md` — Pi prompt for task planning
|
- `prompts/task-manager.md` — Pi prompt for task planning
|
||||||
|
|
||||||
## Runtime state
|
## Runtime state
|
||||||
@@ -70,18 +69,16 @@ is unavailable).
|
|||||||
All runtime state lives in `.ralpi/` in the **project directory** (not this extension directory):
|
All runtime state lives in `.ralpi/` in the **project directory** (not this extension directory):
|
||||||
|
|
||||||
- `.ralpi/progress.json` — execution progress, supports multiple PRDs
|
- `.ralpi/progress.json` — execution progress, supports multiple PRDs
|
||||||
- `.ralpi/loop-active.json` — marker written while a loop runs; drives
|
- `.ralpi/loop-active.json` — marker written while a loop runs; snapshots the
|
||||||
auto-resume after a session reload
|
mode + loop options so `/ralpi-resume` can continue non-interactively
|
||||||
- `.ralpi/reflections/` — per-task reflection JSON files
|
- `.ralpi/reflections/` — per-task reflection JSON files
|
||||||
- `.ralpi/reviews/<prdKey>/` — full review output JSON (only when
|
- `.ralpi/reviews/<prdKey>/` — full review output JSON (only when
|
||||||
`saveReviews` is on)
|
`saveReviews` is on)
|
||||||
|
- `.ralpi/sessions/` — agent session JSONL files per task (persisted so an
|
||||||
|
interrupted task can be resumed from its prior conversation)
|
||||||
- `.ralpi/prompts/` — generated prompts (timestamped, for debugging)
|
- `.ralpi/prompts/` — generated prompts (timestamped, for debugging)
|
||||||
- `.ralpi/config.yaml` — project-level config (optional)
|
- `.ralpi/config.yaml` — project-level config (optional)
|
||||||
|
|
||||||
There is no `.ralpi/sessions/` directory anymore — full task output is shown
|
|
||||||
inline via expandable `ralpi-progress` chat messages, and review output is
|
|
||||||
persisted under `.ralpi/reviews/`.
|
|
||||||
|
|
||||||
## Task ID convention
|
## Task ID convention
|
||||||
|
|
||||||
Task IDs are zero-padded strings (`"01"`, `"02"`, etc.) with an optional
|
Task IDs are zero-padded strings (`"01"`, `"02"`, etc.) with an optional
|
||||||
@@ -94,8 +91,10 @@ use raw numeric IDs.
|
|||||||
- `/ralpi` — no args → show plan for `README.md`; first token looks like a
|
- `/ralpi` — no args → show plan for `README.md`; first token looks like a
|
||||||
path (`@path`, `./path`, `.md`, `.yaml`, etc.) → run; anything else →
|
path (`@path`, `./path`, `.md`, `.yaml`, etc.) → run; anything else →
|
||||||
error suggesting the dash commands
|
error suggesting the dash commands
|
||||||
- `/ralpi-run [task-file]` — run tasks (auto-resumes when progress already
|
- `/ralpi-run [task-file]` — run tasks (always prompts for execution mode +
|
||||||
exists for the file; otherwise prompts for execution mode + loop options)
|
loop options; a cancelled loop for the file is continued from its progress
|
||||||
|
with the newly chosen settings — it never silently resumes with the old
|
||||||
|
loop's settings, only `/ralpi-resume` does that)
|
||||||
- `/ralpi-plan [prompt]` — loads the bundled `prompts/task-manager.md`
|
- `/ralpi-plan [prompt]` — loads the bundled `prompts/task-manager.md`
|
||||||
template and sends it as a user message. Pi's `sendUserMessage()` sends
|
template and sends it as a user message. Pi's `sendUserMessage()` sends
|
||||||
with `expandPromptTemplates: false`, so the extension does its own
|
with `expandPromptTemplates: false`, so the extension does its own
|
||||||
@@ -125,6 +124,9 @@ Key config fields in `execution`:
|
|||||||
- `autoCommit` / `autoReview` / `saveReviews` — loop options (selectable at
|
- `autoCommit` / `autoReview` / `saveReviews` — loop options (selectable at
|
||||||
loop startup via `selectLoopOptions`; review is asked FIRST, commit is
|
loop startup via `selectLoopOptions`; review is asked FIRST, commit is
|
||||||
mandated when review is on)
|
mandated when review is on)
|
||||||
|
- `inactivityTimeoutMs` — hang detection: if no agent session event arrives
|
||||||
|
within this window (e.g. a bash subprocess that never returns), the task is
|
||||||
|
aborted (agent abort + bash subprocess kill). `0` = disabled (default)
|
||||||
- `models` — slot-aware round-robin model list for parallel mode, with
|
- `models` — slot-aware round-robin model list for parallel mode, with
|
||||||
failover to the next model per task (only after exhausting same-model
|
failover to the next model per task (only after exhausting same-model
|
||||||
retries, see `maxSameModelAttempts`)
|
retries, see `maxSameModelAttempts`)
|
||||||
|
|||||||
@@ -218,6 +218,7 @@ execution:
|
|||||||
reviewTimeoutMs: 0 # timeout for auto-review agent sessions (0 = inherit)
|
reviewTimeoutMs: 0 # timeout for auto-review agent sessions (0 = inherit)
|
||||||
loopTimeoutMs: 0 # max total loop duration in ms (0 = no limit; checked between batches)
|
loopTimeoutMs: 0 # max total loop duration in ms (0 = no limit; checked between batches)
|
||||||
worktrees: parallel # "never" | "parallel" (default) | "always" — git worktree isolation
|
worktrees: parallel # "never" | "parallel" (default) | "always" — git worktree isolation
|
||||||
|
chatStyle: compact # "compact" (default) | "verbose" — per-event tool-call stream
|
||||||
prompts:
|
prompts:
|
||||||
projectContext: "Additional context for all tasks"
|
projectContext: "Additional context for all tasks"
|
||||||
reflectionPrompt: "" # custom suffix for reflection extraction
|
reflectionPrompt: "" # custom suffix for reflection extraction
|
||||||
@@ -251,6 +252,10 @@ them.
|
|||||||
> (or parallel mode with no `models` list) the parent pi session's model is used,
|
> (or parallel mode with no `models` list) the parent pi session's model is used,
|
||||||
> unless `implModel` is set.
|
> unless `implModel` is set.
|
||||||
|
|
||||||
|
> `execution.chatStyle` controls how sub-agent tool calls appear in the chat during task execution:
|
||||||
|
> - **compact** (default): a single completion message per task with an expandable tool-call tree (collapsed shows the last 3 calls, expanded via Ctrl+O shows all).
|
||||||
|
> - **verbose**: each tool event is streamed live as its own chat line (`[01 · task-name] → bash ...` / `← (ok)`), like piolium/pygienium's per-event stream.
|
||||||
|
|
||||||
#### Auto-review and Auto-commit
|
#### Auto-review and Auto-commit
|
||||||
|
|
||||||
At loop startup the review question is asked FIRST. When `autoReview` is
|
At loop startup the review question is asked FIRST. When `autoReview` is
|
||||||
|
|||||||
461
index.ts
461
index.ts
@@ -19,7 +19,7 @@ import { loadTaskManagerPrompt } from "./src/task-manager-prompt";
|
|||||||
import { formatReflections } from "./src/reflection";
|
import { formatReflections } from "./src/reflection";
|
||||||
import { verdictGlyph, verdictSummary, formatFindings } from "./src/review";
|
import { verdictGlyph, verdictSummary, formatFindings } from "./src/review";
|
||||||
import type { ReviewResult } from "./src/types";
|
import type { ReviewResult } from "./src/types";
|
||||||
import { executeBatch, type SendChatMessage } from "./src/executor";
|
import { executeBatch, type SendChatMessage, setStreamForwarder } from "./src/executor";
|
||||||
import {
|
import {
|
||||||
cleanupStaleWorktrees,
|
cleanupStaleWorktrees,
|
||||||
finalizeCommittedWorktrees,
|
finalizeCommittedWorktrees,
|
||||||
@@ -306,9 +306,10 @@ async function executePlanBatches(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write loop-active marker so a session reload can detect an interrupted
|
// Write the loop-active marker so an interrupted loop can be resumed
|
||||||
// loop and resume it (in-process agent sessions die on reload — the marker
|
// non-interactively via /ralpi-resume: it snapshots the execution mode and
|
||||||
// + progress.json in_progress tasks are the signal to re-run them).
|
// loop options (autoCommit/autoReview/saveReviews) that /ralpi-resume
|
||||||
|
// would otherwise re-prompt for.
|
||||||
if (projectDir) {
|
if (projectDir) {
|
||||||
const allTaskIds = plan.batches.flatMap((b) => b.tasks.map((t) => t.id));
|
const allTaskIds = plan.batches.flatMap((b) => b.tasks.map((t) => t.id));
|
||||||
writeLoopActive(projectDir, {
|
writeLoopActive(projectDir, {
|
||||||
@@ -469,9 +470,134 @@ function makeSendProgress(pi: ExtensionAPI): SendChatMessage {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Pick the one useful argument from a tool-call's args (path/command/…). */
|
||||||
|
function summarizeArgs(args: unknown): string {
|
||||||
|
if (!args || typeof args !== "object") return "";
|
||||||
|
const obj = args as Record<string, unknown>;
|
||||||
|
const pickKey = ["file_path", "path", "command", "pattern", "query", "url"].find(
|
||||||
|
(k) => typeof obj[k] === "string",
|
||||||
|
);
|
||||||
|
if (pickKey) {
|
||||||
|
const value = String(obj[pickKey]);
|
||||||
|
return value.length > 120 ? `${value.slice(0, 117)}…` : value;
|
||||||
|
}
|
||||||
|
const json = JSON.stringify(obj);
|
||||||
|
return json.length > 120 ? `${json.slice(0, 117)}…` : json;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Collapse a tool result down to a single short line. */
|
||||||
|
function summarizeToolResult(result: unknown): string {
|
||||||
|
if (result == null) return "";
|
||||||
|
if (typeof result === "string") return result;
|
||||||
|
if (typeof result === "number" || typeof result === "boolean")
|
||||||
|
return String(result);
|
||||||
|
if (Array.isArray(result)) {
|
||||||
|
return result
|
||||||
|
.map((item) => {
|
||||||
|
if (typeof item === "string") return item;
|
||||||
|
if (
|
||||||
|
item &&
|
||||||
|
typeof item === "object" &&
|
||||||
|
"text" in (item as Record<string, unknown>)
|
||||||
|
)
|
||||||
|
return String((item as { text?: unknown }).text ?? "");
|
||||||
|
return JSON.stringify(item);
|
||||||
|
})
|
||||||
|
.join("\n");
|
||||||
|
}
|
||||||
|
if (typeof result !== "object") return "";
|
||||||
|
const obj = result as Record<string, unknown>;
|
||||||
|
if (Array.isArray(obj.content)) {
|
||||||
|
const unwrapped = summarizeToolResult(obj.content);
|
||||||
|
if (unwrapped) return unwrapped;
|
||||||
|
}
|
||||||
|
const preferKey = ["stdout", "output", "text", "content", "result"].find(
|
||||||
|
(k) => typeof obj[k] === "string" && (obj[k] as string).length > 0,
|
||||||
|
);
|
||||||
|
if (preferKey) return obj[preferKey] as string;
|
||||||
|
try {
|
||||||
|
return JSON.stringify(obj);
|
||||||
|
} catch {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Collapse whitespace and cap a line at `max` chars with an ellipsis. */
|
||||||
|
function compactLine(text: string, max: number): string {
|
||||||
|
const collapsed = text.replace(/\s+/g, " ").trim();
|
||||||
|
if (collapsed.length <= max) return collapsed;
|
||||||
|
return `${collapsed.slice(0, max - 1)}…`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Extract joined text from an assistant message's content blocks. */
|
||||||
|
function extractAssistantTextFromContent(content: unknown): string {
|
||||||
|
if (typeof content === "string") return content;
|
||||||
|
if (!Array.isArray(content)) return "";
|
||||||
|
return content
|
||||||
|
.flatMap((c) =>
|
||||||
|
c && typeof c === "object" &&
|
||||||
|
(c as { type?: string }).type === "text"
|
||||||
|
? [(c as { text?: string }).text ?? ""]
|
||||||
|
: [],
|
||||||
|
)
|
||||||
|
.join("");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a stream-event forwarder that posts ralpi-stream messages (one chat
|
||||||
|
* line per tool start/end and assistant turn) into the chat. Only used when
|
||||||
|
* execution.chatStyle is "verbose".
|
||||||
|
*/
|
||||||
|
function makeStreamForwarder(pi: ExtensionAPI): (phase: string, event: import("@earendil-works/pi-coding-agent").AgentSessionEvent) => void {
|
||||||
|
const send = (details: {
|
||||||
|
kind: "tool-start" | "tool-end" | "tool-error" | "assistant";
|
||||||
|
phase: string;
|
||||||
|
toolName?: string;
|
||||||
|
body?: string;
|
||||||
|
}, fallback: string) => {
|
||||||
|
pi.sendMessage({
|
||||||
|
customType: "ralpi-stream",
|
||||||
|
content: fallback,
|
||||||
|
display: true,
|
||||||
|
details,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return (phase: string, event: import("@earendil-works/pi-coding-agent").AgentSessionEvent) => {
|
||||||
|
switch (event.type) {
|
||||||
|
case "tool_execution_start": {
|
||||||
|
const body = summarizeArgs(event.args);
|
||||||
|
send({ kind: "tool-start", phase, toolName: event.toolName, body }, `[${phase}] → ${event.toolName}${body ? ` ${body}` : ""}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case "tool_execution_end": {
|
||||||
|
const body = compactLine(summarizeToolResult(event.result), 200);
|
||||||
|
const kind = event.isError ? "tool-error" : "tool-end";
|
||||||
|
const marker = event.isError ? "✗" : "←";
|
||||||
|
send({ kind, phase, toolName: event.toolName, body }, `[${phase}] ${marker} ${event.toolName}${body ? ` ${body}` : ""}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case "message_end": {
|
||||||
|
const message = event.message as { role?: string; content?: unknown };
|
||||||
|
if (message.role !== "assistant") return;
|
||||||
|
const text = extractAssistantTextFromContent(message.content).trim();
|
||||||
|
if (!text) return;
|
||||||
|
const head = compactLine(text, 240);
|
||||||
|
send({ kind: "assistant", phase, body: head }, `[${phase}] ${head}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// ─── Extension Entry ────────────────────────────────────────────────────────
|
// ─── Extension Entry ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
export default function ralpiLoopExtension(pi: ExtensionAPI): void {
|
export default function ralpiLoopExtension(pi: ExtensionAPI): void {
|
||||||
|
// Wire the verbose stream forwarder — posts each tool event as its own
|
||||||
|
// chat message via the ralpi-stream renderer. Enabled per-run by
|
||||||
|
// `execution.chatStyle: verbose` in the config YAML.
|
||||||
|
setStreamForwarder(makeStreamForwarder(pi));
|
||||||
|
|
||||||
// Register custom message renderer for ralpi progress messages.
|
// Register custom message renderer for ralpi progress messages.
|
||||||
// Renders an expandable tool-call tree: collapsed shows last 3 + "N more",
|
// Renders an expandable tool-call tree: collapsed shows last 3 + "N more",
|
||||||
// expanded (Ctrl+O) shows every tool call.
|
// expanded (Ctrl+O) shows every tool call.
|
||||||
@@ -572,236 +698,72 @@ export default function ralpiLoopExtension(pi: ExtensionAPI): void {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
// ─── Reload detection: resume interrupted loops when session reloads ──
|
// ─── Verbose tool-event stream renderer ─────────────────────────────
|
||||||
//
|
//
|
||||||
// ralpi runs task agent sessions in-process (createAgentSession), so they
|
// When execution.chatStyle is "verbose", each tool_execution_start/end and
|
||||||
// do NOT survive a /reload. When the new session starts, this handler
|
// assistant turn is posted as its own chat message — the piolium/pygienium
|
||||||
// reads the persisted loop-active marker + progress.json: if any task is
|
// per-event stream. When "compact" (default), only the completion message
|
||||||
// still `in_progress`, the loop was interrupted mid-task and we resume it
|
// with its expandable tool-call tree shows (the existing ralpi-progress
|
||||||
// (resetting those tasks to pending so the DAG re-schedules them), using
|
// renderer above).
|
||||||
// the mode + loop options snapshotted in loop-active.json so the resume is
|
|
||||||
// non-interactive.
|
|
||||||
pi.on("session_start", async (event, ctx) => {
|
|
||||||
if (event.reason !== "reload") return;
|
|
||||||
|
|
||||||
// Find the ralpi project directory
|
type StreamLineKind = "tool-start" | "tool-end" | "tool-error" | "assistant";
|
||||||
const projectDir = findRalpiDir(ctx.cwd);
|
|
||||||
if (!projectDir) return;
|
|
||||||
|
|
||||||
// Check if a task execution loop was active before the reload
|
interface StreamLineDetails {
|
||||||
const loopState = readLoopActive(projectDir);
|
kind: StreamLineKind;
|
||||||
if (!loopState) return;
|
phase: string;
|
||||||
|
toolName?: string;
|
||||||
|
body?: string;
|
||||||
|
}
|
||||||
|
|
||||||
// The auto-resume path has no CLI flag, so the gitignore guard is
|
pi.registerMessageRenderer<StreamLineDetails>(
|
||||||
// always on: keep `.ralpi/` out of the user's repo on reload too.
|
"ralpi-stream",
|
||||||
ensureRalpiIgnored(projectDir);
|
(message, _options, theme) => {
|
||||||
|
const details = message.details;
|
||||||
// Load progress state
|
if (!details || typeof details !== "object") {
|
||||||
const progressPath = path.join(projectDir, ".ralpi", "progress.json");
|
const fallback =
|
||||||
|
typeof message.content === "string" ? message.content : "";
|
||||||
/** Re-read progress from disk. */
|
return new Text(theme.fg("muted", fallback), 0, 0);
|
||||||
const readTasks = (): Record<string, { status: string }> | null => {
|
|
||||||
try {
|
|
||||||
const raw = fs.readFileSync(progressPath, "utf-8");
|
|
||||||
const parsed = JSON.parse(raw) as Record<string, any>;
|
|
||||||
return parsed.prds?.[loopState.prdKey]?.tasks ?? parsed.tasks ?? null;
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
};
|
const { kind, phase, toolName, body } = details;
|
||||||
|
const phaseTag = theme.fg("accent", `[${phase}]`);
|
||||||
// ralpi agent sessions run in-process (createAgentSession), so they do
|
const indent = " ".repeat(phase.length + 3);
|
||||||
// NOT survive a session reload. Any task left `in_progress` is therefore
|
let line: string;
|
||||||
// stalled — its agent died with the previous session. Detect that state
|
switch (kind) {
|
||||||
// and actively resume the loop instead of passively polling (which would
|
case "tool-start": {
|
||||||
// spin forever waiting for a dead task to complete).
|
const arrow = theme.fg("muted", "→");
|
||||||
const initialTasks = readTasks();
|
const name = theme.fg("toolTitle", theme.bold(toolName ?? ""));
|
||||||
if (initialTasks) {
|
const args = body ? ` ${theme.fg("muted", body)}` : "";
|
||||||
const inProgressIds = Object.entries(initialTasks).flatMap(([id, t]) =>
|
line = `${phaseTag} ${arrow} ${name}${args}`;
|
||||||
t.status === "in_progress" ? [id] : [],
|
break;
|
||||||
);
|
|
||||||
|
|
||||||
// Build the sendProgress wrapper so resumed task messages render the
|
|
||||||
// same expandable tool-call tree as an interactive run. Defined before
|
|
||||||
// the finalize path below so it can report self-healed merges.
|
|
||||||
const sendProgress: SendChatMessage = (
|
|
||||||
content: string,
|
|
||||||
meta?: {
|
|
||||||
toolCalls?: Array<{ name: string; label: string }>;
|
|
||||||
reviewText?: string;
|
|
||||||
reviewPath?: string;
|
|
||||||
reviewResult?: ReviewResult;
|
|
||||||
},
|
|
||||||
) => {
|
|
||||||
pi.sendMessage({
|
|
||||||
customType: "ralpi-progress",
|
|
||||||
content,
|
|
||||||
display: true,
|
|
||||||
details: {
|
|
||||||
phase: "progress",
|
|
||||||
toolCalls: meta?.toolCalls,
|
|
||||||
reviewText: meta?.reviewText,
|
|
||||||
reviewPath: meta?.reviewPath,
|
|
||||||
reviewResult: meta?.reviewResult,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
if (inProgressIds.length === 0) {
|
|
||||||
// Nothing was mid-flight — the loop either finished cleanly between
|
|
||||||
// the reload landing and this handler running, or was stopped
|
|
||||||
// between tasks. Either way, committed worktree branches from an
|
|
||||||
// interrupted loop may still be unmerged (e.g. a prior resume
|
|
||||||
// attempt reset tasks to pending before it was itself interrupted).
|
|
||||||
// Finalize those first so committed code lands in the workspace,
|
|
||||||
// persist the state to progress.json, update the PRD file, THEN
|
|
||||||
// clean up the stale marker.
|
|
||||||
try {
|
|
||||||
const config = loadConfig(projectDir);
|
|
||||||
// Clear any half-done merge left by an interrupted
|
|
||||||
// conflict-resolution session (it would block every merge below).
|
|
||||||
abortMerge(projectDir);
|
|
||||||
const allIds = Object.entries(initialTasks).flatMap(([id, t]) =>
|
|
||||||
t.status !== "failed" && t.status !== "pending" ? [id] : [],
|
|
||||||
);
|
|
||||||
const fin = finalizeCommittedWorktrees(
|
|
||||||
projectDir,
|
|
||||||
config.paths.stateDir,
|
|
||||||
loopState.prdKey,
|
|
||||||
allIds,
|
|
||||||
);
|
|
||||||
// Persist finalized tasks to progress.json + PRD file so the
|
|
||||||
// state is correct for subsequent /ralpi resume calls.
|
|
||||||
const stateDir = config.paths.stateDir;
|
|
||||||
const progressPath = path.join(projectDir, stateDir, "progress.json");
|
|
||||||
// Batch-update progress.json and PRD file for all finalized tasks
|
|
||||||
if (fin.finalized.length > 0) {
|
|
||||||
const progressRaw = fs.existsSync(progressPath)
|
|
||||||
? JSON.parse(fs.readFileSync(progressPath, "utf-8"))
|
|
||||||
: null;
|
|
||||||
for (const id of fin.finalized) {
|
|
||||||
sendProgress?.(
|
|
||||||
`✓ ${id} — finalized on resume (committed branch merged into main)`,
|
|
||||||
);
|
|
||||||
if (progressRaw) {
|
|
||||||
const tasks =
|
|
||||||
progressRaw.prds?.[loopState.prdKey]?.tasks ??
|
|
||||||
progressRaw.tasks;
|
|
||||||
if (tasks && tasks[id]) {
|
|
||||||
tasks[id].status = "completed";
|
|
||||||
tasks[id].completedAt = new Date().toISOString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const prdPath = loopState.taskFile;
|
|
||||||
if (fs.existsSync(prdPath)) {
|
|
||||||
updateTaskInFile(prdPath, id, "completed");
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Best-effort
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (progressRaw) {
|
|
||||||
fs.writeFileSync(
|
|
||||||
progressPath,
|
|
||||||
JSON.stringify(progressRaw, null, 2),
|
|
||||||
"utf-8",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// ── Handle conflicted tasks ──
|
|
||||||
// Same logic as resumeLoop: reset to pending so the DAG can
|
|
||||||
// re-schedule them, keep the worktree for in-place re-run.
|
|
||||||
const conflictIds = Object.keys(fin.conflicts);
|
|
||||||
if (conflictIds.length > 0) {
|
|
||||||
const detail = conflictIds
|
|
||||||
.map((id) => `${id}: ${fin.conflicts[id].slice(0, 3).join(", ")}`)
|
|
||||||
.join("; ");
|
|
||||||
// Batch-reset all conflicted tasks to pending, then write once
|
|
||||||
const progressRaw = fs.existsSync(progressPath)
|
|
||||||
? JSON.parse(fs.readFileSync(progressPath, "utf-8"))
|
|
||||||
: null;
|
|
||||||
for (const id of conflictIds) {
|
|
||||||
if (progressRaw) {
|
|
||||||
const tasks =
|
|
||||||
progressRaw.prds?.[loopState.prdKey]?.tasks ??
|
|
||||||
progressRaw.tasks;
|
|
||||||
if (tasks && tasks[id]) {
|
|
||||||
tasks[id].status = "pending";
|
|
||||||
delete tasks[id].startedAt;
|
|
||||||
delete tasks[id].error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const prdPath = loopState.taskFile;
|
|
||||||
if (fs.existsSync(prdPath)) {
|
|
||||||
updateTaskInFile(prdPath, id, "pending");
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Best-effort
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (progressRaw) {
|
|
||||||
fs.writeFileSync(
|
|
||||||
progressPath,
|
|
||||||
JSON.stringify(progressRaw, null, 2),
|
|
||||||
"utf-8",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
ctx.ui.notify(
|
|
||||||
`Reset ${conflictIds.length} conflicted task(s) to pending for re-execution (${detail})`,
|
|
||||||
"info",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Best-effort — the marker is removed either way; the worktrees
|
|
||||||
// stay on disk for a manual /ralpi-resume.
|
|
||||||
}
|
}
|
||||||
ctx.ui.notify(
|
case "tool-end": {
|
||||||
"ralpi loop has no in-progress task to resume — marking complete.",
|
const arrow = theme.fg("success", "←");
|
||||||
"info",
|
const result = body
|
||||||
);
|
? ` ${theme.fg("dim", body)}`
|
||||||
deleteLoopActive(projectDir);
|
: ` ${theme.fg("dim", "(ok)")}`;
|
||||||
return;
|
line = `${indent}${arrow}${result}`;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "tool-error": {
|
||||||
|
const marker = theme.fg("error", "✗");
|
||||||
|
const result = body
|
||||||
|
? ` ${theme.fg("error", body)}`
|
||||||
|
: ` ${theme.fg("error", "failed")}`;
|
||||||
|
line = `${indent}${marker}${result}`;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "assistant":
|
||||||
|
line = `${phaseTag} ${theme.fg("muted", body ?? "")}`;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
line =
|
||||||
|
typeof message.content === "string"
|
||||||
|
? theme.fg("muted", message.content)
|
||||||
|
: "";
|
||||||
}
|
}
|
||||||
|
return new Text(line, 0, 0);
|
||||||
const taskCount = loopState.taskIds.length;
|
},
|
||||||
ctx.ui.notify(
|
);
|
||||||
`ralpi loop was interrupted by reload with ${inProgressIds.length} in-progress task(s). ` +
|
|
||||||
`Resuming execution (${taskCount} tasks, ${loopState.mode} mode)...`,
|
|
||||||
"info",
|
|
||||||
);
|
|
||||||
|
|
||||||
// Load config from the project directory so model + thinking level
|
|
||||||
// resolve the same way the interactive command handler does.
|
|
||||||
const config = loadConfig(projectDir);
|
|
||||||
|
|
||||||
try {
|
|
||||||
await resumeLoop(
|
|
||||||
ctx,
|
|
||||||
loopState.taskFile,
|
|
||||||
projectDir,
|
|
||||||
loopState.prdKey,
|
|
||||||
sendProgress,
|
|
||||||
config.model ?? ctx.model,
|
|
||||||
pi.getThinkingLevel(),
|
|
||||||
{
|
|
||||||
mode: loopState.mode,
|
|
||||||
autoCommit: loopState.autoCommit ?? config.execution.autoCommit,
|
|
||||||
autoReview: loopState.autoReview ?? config.execution.autoReview,
|
|
||||||
saveReviews: loopState.saveReviews ?? config.execution.saveReviews,
|
|
||||||
skipFinalStatus: false,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
const msg = error instanceof Error ? error.message : String(error);
|
|
||||||
ctx.ui.notify(`ralpi auto-resume failed: ${msg}`, "error");
|
|
||||||
// Leave loop-active.json in place so the user can retry via
|
|
||||||
// /ralpi resume after addressing the underlying error.
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
pi.registerCommand("ralpi", {
|
pi.registerCommand("ralpi", {
|
||||||
description:
|
description:
|
||||||
@@ -926,38 +888,15 @@ async function handleRun(
|
|||||||
const noGitignore = stripNoGitignore(args);
|
const noGitignore = stripNoGitignore(args);
|
||||||
const taskFile = resolveTaskArg(args[0] || "README.md", ctx.cwd);
|
const taskFile = resolveTaskArg(args[0] || "README.md", ctx.cwd);
|
||||||
|
|
||||||
// If targeting a specific task file and there's existing progress for it,
|
// A cancelled loop for this file is continued below AFTER re-prompting for
|
||||||
// auto-resume instead of starting fresh
|
// settings — /ralpi-run never silently resumes with the old loop's
|
||||||
|
// settings (only /ralpi-resume does that). isResume below keeps the
|
||||||
|
// interrupted task's worktree so it continues rather than restarts.
|
||||||
const existingProgress = findProgressFile(ctx.cwd, taskFile);
|
const existingProgress = findProgressFile(ctx.cwd, taskFile);
|
||||||
if (existingProgress) {
|
|
||||||
return handleResume(
|
|
||||||
ctx,
|
|
||||||
args.slice(0, 1),
|
|
||||||
sendChatMessage,
|
|
||||||
parentModel,
|
|
||||||
parentThinkingLevel,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// No existing progress for this task — check for any progress at all
|
// Resolve the project root from any existing progress so running from a
|
||||||
|
// subdirectory still targets the loop's project.
|
||||||
const found = findProgressFile(ctx.cwd);
|
const found = findProgressFile(ctx.cwd);
|
||||||
if (found && !args[0]) {
|
|
||||||
// Offer to resume instead of starting fresh
|
|
||||||
const shouldResume = await ctx.ui.select(
|
|
||||||
"Found existing ralpi progress. Resume?",
|
|
||||||
["Yes, resume", "No, start fresh"],
|
|
||||||
);
|
|
||||||
|
|
||||||
if (shouldResume?.startsWith("Yes")) {
|
|
||||||
return handleResume(
|
|
||||||
ctx,
|
|
||||||
[],
|
|
||||||
sendChatMessage,
|
|
||||||
parentModel,
|
|
||||||
parentThinkingLevel,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const projectDir = found ? path.dirname(path.dirname(found.path)) : ctx.cwd;
|
const projectDir = found ? path.dirname(path.dirname(found.path)) : ctx.cwd;
|
||||||
ensureIgnoredNote(projectDir, ctx, noGitignore);
|
ensureIgnoredNote(projectDir, ctx, noGitignore);
|
||||||
@@ -1004,6 +943,7 @@ async function handleRun(
|
|||||||
mode,
|
mode,
|
||||||
sendChatMessage,
|
sendChatMessage,
|
||||||
projectDir,
|
projectDir,
|
||||||
|
!!existingProgress, // preserve in-progress worktrees from a cancelled loop
|
||||||
);
|
);
|
||||||
|
|
||||||
const state = progress.getState();
|
const state = progress.getState();
|
||||||
@@ -1025,11 +965,10 @@ async function handleRun(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Resume core: given a resolved task file, project dir, and PRD key,
|
* Resume core: given a resolved task file, project dir, and PRD key,
|
||||||
* build the remaining plan and execute it. Used by both the explicit
|
* build the remaining plan and execute it.
|
||||||
* `/ralpi resume` command and the auto-resume on session reload.
|
|
||||||
*
|
*
|
||||||
* `mode` and loop options (`autoCommit`/`autoReview`/`saveReviews`) may be
|
* `mode` and loop options (`autoCommit`/`autoReview`/`saveReviews`) may be
|
||||||
* passed to skip interactive prompts — this is how a reload resumes
|
* passed to skip interactive prompts — this is how /ralpi-resume resumes
|
||||||
* non-interactively using the snapshot stored in loop-active.json.
|
* non-interactively using the snapshot stored in loop-active.json.
|
||||||
* When omitted, the user is prompted as usual.
|
* When omitted, the user is prompted as usual.
|
||||||
*/
|
*/
|
||||||
@@ -1285,8 +1224,8 @@ async function handleResume(
|
|||||||
|
|
||||||
// Reuse the loop snapshot (mode + autoCommit/autoReview/saveReviews)
|
// Reuse the loop snapshot (mode + autoCommit/autoReview/saveReviews)
|
||||||
// persisted when the loop started, so an interrupted loop resumes
|
// persisted when the loop started, so an interrupted loop resumes
|
||||||
// non-interactively — matching the auto-resume-on-reload path. Only fall
|
// non-interactively. Only fall back to interactive prompts when no
|
||||||
// back to interactive prompts when no snapshot is present.
|
// snapshot is present.
|
||||||
const snapshot = readLoopActive(projectDir);
|
const snapshot = readLoopActive(projectDir);
|
||||||
const loopOpts = (() => {
|
const loopOpts = (() => {
|
||||||
if (
|
if (
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mikefreno/ralpi",
|
"name": "@mikefreno/ralpi",
|
||||||
"version": "0.5.0",
|
"version": "0.6.0",
|
||||||
"description": "Execute tasks from task files/PRD's using DAG-based dependency resolution with persistent progress tracking",
|
"description": "Execute tasks from task files/PRD's using DAG-based dependency resolution with persistent progress tracking",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"pi-package",
|
"pi-package",
|
||||||
@@ -24,7 +24,6 @@
|
|||||||
"files": [
|
"files": [
|
||||||
"index.ts",
|
"index.ts",
|
||||||
"src/",
|
"src/",
|
||||||
"skills/",
|
|
||||||
"prompts/",
|
"prompts/",
|
||||||
"README.md",
|
"README.md",
|
||||||
"LICENSE"
|
"LICENSE"
|
||||||
@@ -41,9 +40,6 @@
|
|||||||
"extensions": [
|
"extensions": [
|
||||||
"./index.ts"
|
"./index.ts"
|
||||||
],
|
],
|
||||||
"skills": [
|
|
||||||
"./skills"
|
|
||||||
],
|
|
||||||
"prompts": [
|
"prompts": [
|
||||||
"./prompts"
|
"./prompts"
|
||||||
]
|
]
|
||||||
|
|||||||
427
port-to-omp.mjs
Normal file
427
port-to-omp.mjs
Normal file
@@ -0,0 +1,427 @@
|
|||||||
|
#!/usr/bin/env bun
|
||||||
|
/**
|
||||||
|
* port-to-omp.mjs — regenerate the omp port of ralpi from this repo.
|
||||||
|
*
|
||||||
|
* The omp port is "base + patch layer"; this script IS the patch layer. This
|
||||||
|
* repo is the single source of truth; ~/.omp/agent/extensions/ralpi (or
|
||||||
|
* --out) is a generated artifact. Every op asserts its target and fails
|
||||||
|
* loudly on base drift — never silently producing a stale port.
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* bun port-to-omp.mjs # write ~/.omp/agent/extensions/ralpi
|
||||||
|
* bun port-to-omp.mjs --out <dir> # write elsewhere (CI: the omp repo clone)
|
||||||
|
*
|
||||||
|
* CI: .gitea/workflows/port-to-omp.yml clones the omp-ralpi repo and
|
||||||
|
* runs this script into it, then commits + pushes when the port changed.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {
|
||||||
|
cpSync,
|
||||||
|
existsSync,
|
||||||
|
mkdirSync,
|
||||||
|
readdirSync,
|
||||||
|
readFileSync,
|
||||||
|
realpathSync,
|
||||||
|
rmSync,
|
||||||
|
writeFileSync,
|
||||||
|
} from "node:fs";
|
||||||
|
import {
|
||||||
|
join,
|
||||||
|
relative,
|
||||||
|
resolve,
|
||||||
|
isAbsolute,
|
||||||
|
dirname,
|
||||||
|
basename,
|
||||||
|
} from "node:path";
|
||||||
|
import { execSync } from "node:child_process";
|
||||||
|
import { homedir } from "node:os";
|
||||||
|
|
||||||
|
const HOME = homedir();
|
||||||
|
const PI = join(HOME, ".pi", "agent", "extensions");
|
||||||
|
const OMP = join(HOME, ".omp", "agent", "extensions");
|
||||||
|
|
||||||
|
const SKIP = new Set([
|
||||||
|
"port-to-omp.mjs",
|
||||||
|
"release-tag.sh",
|
||||||
|
".gitea",
|
||||||
|
".github",
|
||||||
|
"node_modules",
|
||||||
|
".git",
|
||||||
|
".DS_Store",
|
||||||
|
".pi-lens",
|
||||||
|
"bun.lock",
|
||||||
|
"package-lock.json",
|
||||||
|
"dist",
|
||||||
|
]);
|
||||||
|
|
||||||
|
const OMP_SDK = "17.2.12";
|
||||||
|
|
||||||
|
// ─── helpers ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/** Appended to op failures: the base repo is the source of truth, so a failed
|
||||||
|
* assertion means the source drifted and the op needs updating — never a
|
||||||
|
* reason to weaken the assertion. */
|
||||||
|
const DRIFT_HINT =
|
||||||
|
"\n\nBase repo drifted since this op was written — update the op (from/to) in" +
|
||||||
|
" port-to-omp.mjs. The verify CI job runs this script on every push, so this" +
|
||||||
|
" should surface on the branch that introduced the drift.";
|
||||||
|
|
||||||
|
/** Point at the first divergence between an expected op target and the actual
|
||||||
|
* file text, with a little surrounding context. */
|
||||||
|
function driftHint(src, from) {
|
||||||
|
const fromLines = from.split("\n");
|
||||||
|
const srcLines = src.split("\n");
|
||||||
|
const needle = fromLines[0].slice(0, 60);
|
||||||
|
// indexOf failed, so no full match exists. Anchor on the occurrence of the
|
||||||
|
// op's opening line that shares the LONGEST consecutive run with the
|
||||||
|
// expected text — a bare ` try {` can match unrelated blocks.
|
||||||
|
let anchor = -1;
|
||||||
|
let bestRun = -1;
|
||||||
|
for (let i = 0; i < srcLines.length; i++) {
|
||||||
|
if (!srcLines[i].includes(needle)) continue;
|
||||||
|
let run = 0;
|
||||||
|
while (run < fromLines.length && fromLines[run] === srcLines[i + run]) run++;
|
||||||
|
if (run > bestRun) {
|
||||||
|
bestRun = run;
|
||||||
|
anchor = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (anchor === -1) {
|
||||||
|
return `\n\ncould not locate the op's opening line anywhere in the file:\n ${fromLines[0]}`;
|
||||||
|
}
|
||||||
|
const ctx = 2;
|
||||||
|
const i = Math.min(bestRun, fromLines.length - 1);
|
||||||
|
const before = srcLines
|
||||||
|
.slice(Math.max(0, anchor + i - ctx), anchor + i)
|
||||||
|
.map((l) => ` ${l}`);
|
||||||
|
const after = srcLines
|
||||||
|
.slice(anchor + i + 1, anchor + i + 1 + ctx)
|
||||||
|
.map((l) => ` ${l}`);
|
||||||
|
return (
|
||||||
|
`\n\nop matches at file line ${anchor + 1} for ${bestRun} line(s), then diverges at expected line ${i + 1}:\n` +
|
||||||
|
` expected: ${fromLines[i]}\n` +
|
||||||
|
` actual : ${srcLines[anchor + i] ?? "<end of file>"}\n` +
|
||||||
|
(before.length ? `\nactual context before:\n${before.join("\n")}\n` : "") +
|
||||||
|
(after.length ? `\nactual context after:\n${after.join("\n")}` : "")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function assertEdit(src, from, to, file, label = "") {
|
||||||
|
const idx = src.indexOf(from);
|
||||||
|
if (idx === -1) {
|
||||||
|
throw new Error(
|
||||||
|
`[${file}] target not found${label ? ` (${label})` : ""}${driftHint(
|
||||||
|
src,
|
||||||
|
from,
|
||||||
|
)}\n` + `\nexpected target text:\n${from}${DRIFT_HINT}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return src.slice(0, idx) + to + src.slice(idx + from.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
function replaceAll(src, from, to, file, label) {
|
||||||
|
const parts = src.split(from);
|
||||||
|
if (parts.length === 1) {
|
||||||
|
throw new Error(
|
||||||
|
`[${file}] target not found${label ? ` (${label})` : ""}${driftHint(
|
||||||
|
src,
|
||||||
|
from,
|
||||||
|
)}\n` + `\nexpected target text:\n${from}${DRIFT_HINT}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return parts.join(to);
|
||||||
|
}
|
||||||
|
|
||||||
|
function reEdit(src, re, to, file, label) {
|
||||||
|
const out = src.replace(re, to);
|
||||||
|
if (out === src) {
|
||||||
|
throw new Error(
|
||||||
|
`[${file}] regex matched nothing (${label}): ${re}${DRIFT_HINT}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
function reAll(src, re, to, file, label) {
|
||||||
|
let count = 0;
|
||||||
|
const out = src.replace(re, (...args) => {
|
||||||
|
count++;
|
||||||
|
return typeof to === "function" ? to(...args) : to;
|
||||||
|
});
|
||||||
|
if (count === 0) {
|
||||||
|
throw new Error(
|
||||||
|
`[${file}] regex matched nothing (${label}): ${re}${DRIFT_HINT}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Run a list of ops over a file's text. op = {from,to} | {re,to,label}. */
|
||||||
|
function applyOps(src, ops, file) {
|
||||||
|
for (const op of ops) {
|
||||||
|
if ("from" in op) {
|
||||||
|
src = op.all
|
||||||
|
? replaceAll(src, op.from, op.to, file, op.label)
|
||||||
|
: assertEdit(src, op.from, op.to, file, op.label);
|
||||||
|
} else {
|
||||||
|
src = op.all
|
||||||
|
? reAll(src, op.re, op.to, file, op.label)
|
||||||
|
: reEdit(src, op.re, op.to, file, op.label);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return src;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mirrorTree(srcDir, dstDir) {
|
||||||
|
mkdirSync(dstDir, { recursive: true });
|
||||||
|
cpSync(srcDir, dstDir, {
|
||||||
|
recursive: true,
|
||||||
|
force: true,
|
||||||
|
filter: (p) => !SKIP.has(p.split("/").pop()),
|
||||||
|
});
|
||||||
|
// drop stale files in dst that no longer exist in src; keep .git* intact
|
||||||
|
for (const rel of walk(dstDir)) {
|
||||||
|
if (rel.startsWith(".git")) continue;
|
||||||
|
if (!existsSync(join(srcDir, rel)))
|
||||||
|
rmSync(join(dstDir, rel), { force: true, recursive: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function real(p) {
|
||||||
|
try {
|
||||||
|
return realpathSync(p);
|
||||||
|
} catch {
|
||||||
|
// walk to the nearest existing ancestor and realpath it, then re-append
|
||||||
|
const tail = [];
|
||||||
|
let cur = resolve(p);
|
||||||
|
for (;;) {
|
||||||
|
try {
|
||||||
|
return join(realpathSync(cur), ...tail);
|
||||||
|
} catch {}
|
||||||
|
const parent = dirname(cur);
|
||||||
|
if (parent === cur) return resolve(p);
|
||||||
|
tail.unshift(basename(cur));
|
||||||
|
cur = parent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function assertDstOutsideSrc(srcDir, dstDir) {
|
||||||
|
const rel = relative(real(srcDir), real(dstDir));
|
||||||
|
if (rel === "" || (!rel.startsWith("..") && !isAbsolute(rel))) {
|
||||||
|
throw new Error(
|
||||||
|
`refusing to port into a subdirectory of the source: ${dstDir} is inside ${srcDir}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function walk(dir) {
|
||||||
|
const out = [];
|
||||||
|
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
||||||
|
if (SKIP.has(entry.name)) continue;
|
||||||
|
const p = join(dir, entry.name);
|
||||||
|
if (entry.isDirectory())
|
||||||
|
out.push(...walk(p).map((r) => join(entry.name, r)));
|
||||||
|
else out.push(entry.name);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
const SPECIFIERS = [
|
||||||
|
["@earendil-works/pi-coding-agent", "@oh-my-pi/pi-coding-agent"],
|
||||||
|
["@earendil-works/pi-tui", "@oh-my-pi/pi-tui"],
|
||||||
|
["@earendil-works/pi-ai", "@oh-my-pi/pi-ai"],
|
||||||
|
["@earendil-works/pi-agent-core", "@oh-my-pi/pi-agent-core"],
|
||||||
|
];
|
||||||
|
|
||||||
|
function rewriteSpecifiers(src) {
|
||||||
|
for (const [from, to] of SPECIFIERS) src = src.split(from).join(to);
|
||||||
|
return src;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── package.json transforms ────────────────────────────────────────────────
|
||||||
|
|
||||||
|
function pkgName(piName) {
|
||||||
|
if (piName.startsWith("@mikefreno/"))
|
||||||
|
return piName.replace(/^@mikefreno\//, "@mikefreno/omp-");
|
||||||
|
return `@mikefreno/omp-${piName}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function reorder(obj, keys) {
|
||||||
|
const out = {};
|
||||||
|
for (const k of keys) if (k in obj) out[k] = obj[k];
|
||||||
|
for (const k of Object.keys(obj)) if (!(k in out)) out[k] = obj[k];
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
const PKG_RULES = {
|
||||||
|
order: [
|
||||||
|
"name",
|
||||||
|
"version",
|
||||||
|
"description",
|
||||||
|
"keywords",
|
||||||
|
"author",
|
||||||
|
"license",
|
||||||
|
"homepage",
|
||||||
|
"repository",
|
||||||
|
"bugs",
|
||||||
|
"files",
|
||||||
|
"scripts",
|
||||||
|
"engines",
|
||||||
|
"omp",
|
||||||
|
"dependencies",
|
||||||
|
"publishConfig",
|
||||||
|
"devDependencies",
|
||||||
|
],
|
||||||
|
transform(p) {
|
||||||
|
p.name = pkgName(p.name);
|
||||||
|
p.keywords = ["omp", "omp-extension", ...p.keywords.slice(2)];
|
||||||
|
delete p.scripts.prepublishOnly;
|
||||||
|
p.engines.bun = ">=1.3.14";
|
||||||
|
p.omp = p.pi;
|
||||||
|
delete p.pi;
|
||||||
|
delete p.omp.prompts;
|
||||||
|
delete p.peerDependencies;
|
||||||
|
p.devDependencies = {
|
||||||
|
"@oh-my-pi/pi-coding-agent": OMP_SDK,
|
||||||
|
"@oh-my-pi/pi-tui": OMP_SDK,
|
||||||
|
...p.devDependencies,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
function transformPkg() {
|
||||||
|
const raw = JSON.parse(
|
||||||
|
readFileSync(join(import.meta.dir, "package.json"), "utf8"),
|
||||||
|
);
|
||||||
|
if (!raw.pi) throw new Error('expected "pi" manifest key in package.json');
|
||||||
|
const rule = PKG_RULES;
|
||||||
|
rule.transform(raw);
|
||||||
|
const ordered = reorder(raw, rule.order);
|
||||||
|
return JSON.stringify(ordered, null, 2) + "\n";
|
||||||
|
}
|
||||||
|
const README_STUB = `# ralpi (omp port)
|
||||||
|
|
||||||
|
Execute tasks from task files using DAG-based dependency resolution.
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
\`\`\`sh
|
||||||
|
omp install @mikefreno/omp-ralpi
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
This is the omp port of [Mike/ralpi](https://git.freno.me/Mike/ralpi), regenerated automatically from the source repo. See the source repo for full documentation.
|
||||||
|
`;
|
||||||
|
|
||||||
|
|
||||||
|
// publish workflow emitted into the port repo so the omp package can be
|
||||||
|
// released independently (tag push or manual dispatch on the omp repo).
|
||||||
|
const PORT_PUBLISH_WORKFLOW = "name: publish\n\n# Publish this omp port package to the npm registry on version tags.\n#\n# Prerequisites:\n# - npm automation token (publish scope) stored as the repo secret NPM_TOKEN\n# - package name claimed on npm (@mikefreno/omp-<name>)\n#\n# Manual publish: Actions tab → Run workflow (workflow_dispatch).\n\non:\n push:\n tags: ['v*']\n workflow_dispatch:\n\njobs:\n publish:\n runs-on: ubuntu-latest\n steps:\n - name: Checkout\n uses: actions/checkout@v4\n\n - name: Setup node (npm registry auth)\n uses: actions/setup-node@v4\n with:\n node-version: '22'\n registry-url: 'https://registry.npmjs.org'\n\n - name: Publish\n env:\n NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}\n run: npm publish --access public --ignore-scripts\n";
|
||||||
|
|
||||||
|
const FILE_RULES = {
|
||||||
|
"src/utils.ts": [
|
||||||
|
{
|
||||||
|
from: 'import type { AgentSessionEvent } from "@oh-my-pi/pi-coding-agent";\nimport {\n createAgentSession,\n DefaultResourceLoader,\n getAgentDir,\n SessionManager,\n SettingsManager,\n type ModelRuntime,\n} from "@oh-my-pi/pi-coding-agent";',
|
||||||
|
to: 'import type { AgentSessionEvent } from "@oh-my-pi/pi-coding-agent";\nimport type { ModelRegistry } from "@oh-my-pi/pi-coding-agent";\nimport {\n AgentRegistry,\n createAgentSession,\n getAgentDir,\n SessionManager,\n Settings,\n} from "@oh-my-pi/pi-coding-agent";',
|
||||||
|
label: "import block",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
from: "/** Path to the global ralpi config under the user's Pi home directory. */",
|
||||||
|
to: "/** Path to the global ralpi config under the user's omp home directory. */",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
from: ' process.env.HOME || "/tmp",\n ".pi",\n "ralpi",',
|
||||||
|
to: ' process.env.HOME || "/tmp",\n ".omp",\n "ralpi",',
|
||||||
|
label: "GLOBAL_CONFIG_PATH",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
from: "~/.pi/ralpi/config.yaml",
|
||||||
|
to: "~/.omp/ralpi/config.yaml",
|
||||||
|
all: true,
|
||||||
|
label: "config path mentions",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
from: " /** Parent session's model runtime. Must be passed so extension-registered\n * providers (e.g., neuralwatt with its streamSimple wrapper for 429\n * rate-limit normalization) are available. When omitted, the SDK creates\n * a fresh runtime from models.json only — extension providers are lost. */\n modelRuntime?: ModelRuntime,",
|
||||||
|
to: " /** Parent session's model registry. Must be passed so extension-registered\n * providers (e.g., neuralwatt with its streamSimple wrapper for 429\n * rate-limit normalization) are available. When omitted, the SDK creates\n * a fresh registry from models.json only — extension providers are lost. */\n modelRegistry?: ModelRegistry,",
|
||||||
|
label: "modelRuntime signature",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
from: ' try {\n // Loop sessions load the full normal pi context: extensions (so all\n // extension-provided tools register), skills, and project context\n // (AGENTS.md / CLAUDE.md)\n const loader = new DefaultResourceLoader({\n cwd,\n agentDir: getAgentDir(),\n noSkills,\n noPromptTemplates: true,\n noThemes: true,\n noExtensions: false,\n noContextFiles: false,\n });\n await loader.reload();\n\n // Persist sessions under the ralpi project\'s `.ralpi/sessions/` so they\n // survive worktree removal and are findable from the main repo on resume.\n // Worktrees live inside `<project>/.ralpi/worktrees/...`, so walking up\n // from the agent\'s cwd always finds the main project\'s `.ralpi` first.\n const ralpiDir = findRalpiDir(cwd);\n const sessionDir = ralpiDir\n ? path.join(ralpiDir, ".ralpi", "sessions")\n : path.join(cwd, ".ralpi", "sessions");\n\n let sessionManager: SessionManager;\n if (resumeSessionFile && fs.existsSync(resumeSessionFile)) {\n sessionManager = SessionManager.open(resumeSessionFile, sessionDir, cwd);\n } else {\n if (resumeSessionFile) {\n console.warn(\n `[ralpi] resume session file not found (${resumeSessionFile}) — starting a fresh session`,\n );\n }\n sessionManager = SessionManager.create(cwd, sessionDir);\n }\n\n const result = await createAgentSession({\n cwd,\n sessionManager,\n resourceLoader: loader,\n settingsManager: SettingsManager.create(cwd, getAgentDir()),\n modelRuntime,\n // No `tools` allowlist: matches a normal pi session\'s tool set.\n model: model as any,\n thinkingLevel: thinkingLevel as any,\n });',
|
||||||
|
to: ' try {\n // Loop sessions load the full normal omp context: extensions (so all\n // extension-provided tools register) and project context (AGENTS.md).\n // Persist sessions under the ralpi project\'s `.ralpi/sessions/` so they\n // survive worktree removal and are findable from the main repo on resume.\n // Worktrees live inside `<project>/.ralpi/worktrees/...`, so walking up\n // from the agent\'s cwd always finds the main project\'s `.ralpi` first.\n const ralpiDir = findRalpiDir(cwd);\n const sessionDir = ralpiDir\n ? path.join(ralpiDir, ".ralpi", "sessions")\n : path.join(cwd, ".ralpi", "sessions");\n\n let sessionManager: SessionManager;\n if (resumeSessionFile && fs.existsSync(resumeSessionFile)) {\n sessionManager = await SessionManager.open(resumeSessionFile, sessionDir, undefined, {\n initialCwd: cwd,\n });\n } else {\n if (resumeSessionFile) {\n console.warn(\n `[ralpi] resume session file not found (${resumeSessionFile}) — starting a fresh session`,\n );\n }\n sessionManager = SessionManager.create(cwd, sessionDir);\n }\n\n const result = await createAgentSession({\n cwd,\n sessionManager,\n settingsManager: Settings.init({ cwd, agentDir: getAgentDir() }),\n // Loop sessions intentionally load extensions (no disableExtensionDiscovery),\n // plus skills and project context via default discovery.\n skills: noSkills ? [] : undefined,\n promptTemplates: [],\n // No `tools` allowlist: matches a normal omp session\'s tool set.\n model: model as any,\n thinkingLevel: thinkingLevel as any,\n modelRegistry,\n agentRegistry: new AgentRegistry(),\n });',
|
||||||
|
label: "runAgentSession body",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"src/executor.ts": [
|
||||||
|
{
|
||||||
|
from: 'import type {\n\tExtensionContext,\n\tModelRuntime,\n\tAgentSessionEvent,\n} from "@oh-my-pi/pi-coding-agent";',
|
||||||
|
to: 'import type {\n\tExtensionContext,\n\tAgentSessionEvent,\n} from "@oh-my-pi/pi-coding-agent";',
|
||||||
|
label: "ModelRuntime import removal",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
from: "(ctx.modelRegistry as any).runtime as ModelRuntime,",
|
||||||
|
to: "ctx.modelRegistry,",
|
||||||
|
all: true,
|
||||||
|
label: "modelRegistry pass-through",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
from: "\t// Pi's built-in retry (via SettingsManager) handles transient HTTP errors\n\t// with exponential backoff WITHIN a single prompt. Ralpi adds two layers on",
|
||||||
|
to: "\t// The agent's built-in retry handles transient HTTP errors with exponential\n\t// backoff WITHIN a single prompt. Ralpi adds two layers on",
|
||||||
|
label: "retry comment",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
from: '\t\tcase "find":\n\t\t\treturn sanitizeLabel(`${a.path ?? "."} — ${a.glob ?? "*"}`);\n\t\tcase "ls":\n\t\t\treturn sanitizeLabel(truncateMiddle(String(a.path ?? "."), 60));',
|
||||||
|
to: '\t\tcase "glob":\n\t\t\treturn sanitizeLabel(`${a.path ?? "."} — ${a.glob ?? "*"}`);',
|
||||||
|
label: "tool labeler find/ls",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"src/task-manager-prompt.ts": [
|
||||||
|
{
|
||||||
|
from: 'import * as fs from "node:fs";\nimport * as path from "node:path";\nimport { stripFrontmatter } from "@oh-my-pi/pi-coding-agent";\n\nconst TEMPLATE_REL = path.join("prompts", "task-manager.md");',
|
||||||
|
to: 'import * as fs from "node:fs";\nimport * as path from "node:path";\n\nconst TEMPLATE_REL = path.join("prompts", "task-manager.md");\n\n/**\n * Strip leading YAML frontmatter (--- delimited) from template content.\n * Local port of the helper omp does not export from the package root.\n */\nfunction stripFrontmatter(content: string): string {\n const m = /^---\\r?\\n[\\s\\S]*?\\r?\\n---\\r?\\n/.exec(content);\n return m ? content.slice(m[0].length) : content;\n}',
|
||||||
|
label: "vendor stripFrontmatter",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
// ─── main ───────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
const OUT_DEFAULT = join(homedir(), ".omp", "agent", "extensions", "ralpi");
|
||||||
|
|
||||||
|
function portExtension() {
|
||||||
|
const srcDir = import.meta.dir;
|
||||||
|
const dstDir = process.argv.includes("--out")
|
||||||
|
? process.argv[process.argv.indexOf("--out") + 1]
|
||||||
|
: OUT_DEFAULT;
|
||||||
|
if (!dstDir) throw new Error("--out requires a directory argument");
|
||||||
|
if (!existsSync(srcDir)) throw new Error(`no base extension at ${srcDir}`);
|
||||||
|
|
||||||
|
assertDstOutsideSrc(srcDir, dstDir);
|
||||||
|
console.log(`== ${srcDir} -> ${dstDir}`);
|
||||||
|
mirrorTree(srcDir, dstDir);
|
||||||
|
|
||||||
|
for (const rel of walk(dstDir)) {
|
||||||
|
const p = join(dstDir, rel);
|
||||||
|
if (!existsSync(p)) continue;
|
||||||
|
if (rel.endsWith(".ts")) {
|
||||||
|
let text = readFileSync(p, "utf8");
|
||||||
|
text = rewriteSpecifiers(text);
|
||||||
|
if (FILE_RULES[rel]) text = applyOps(text, FILE_RULES[rel], rel);
|
||||||
|
writeFileSync(p, text);
|
||||||
|
} else if (rel === "package.json") {
|
||||||
|
writeFileSync(p, transformPkg());
|
||||||
|
} else if (rel === "README.md") {
|
||||||
|
writeFileSync(p, README_STUB);
|
||||||
|
} else if (rel.endsWith(".md")) {
|
||||||
|
let text = readFileSync(p, "utf8");
|
||||||
|
if (FILE_RULES[rel]) text = applyOps(text, FILE_RULES[rel], rel);
|
||||||
|
writeFileSync(p, text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const wfDir = join(dstDir, ".gitea", "workflows");
|
||||||
|
mkdirSync(wfDir, { recursive: true });
|
||||||
|
writeFileSync(join(wfDir, "publish.yml"), PORT_PUBLISH_WORKFLOW);
|
||||||
|
|
||||||
|
console.log("== bun install (regenerates bun.lock + node_modules)");
|
||||||
|
execSync("bun install", { cwd: dstDir, stdio: "inherit" });
|
||||||
|
console.log("== done");
|
||||||
|
}
|
||||||
|
|
||||||
|
portExtension();
|
||||||
196
scripts/release-tag.sh
Executable file
196
scripts/release-tag.sh
Executable file
@@ -0,0 +1,196 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# release-tag.sh — version bump, commit, tag, and push for this pi extension.
|
||||||
|
#
|
||||||
|
# Mirrors the release flow from PodTui's scripts/release-tag.sh, adapted for
|
||||||
|
# the pi extension repos: the version lives in package.json, and the omp port
|
||||||
|
# derives its version from it during regeneration.
|
||||||
|
#
|
||||||
|
# Usage:
|
||||||
|
# scripts/release-tag.sh interactive release
|
||||||
|
# scripts/release-tag.sh --dry-run plan the bump/tag/pushes without doing
|
||||||
|
#
|
||||||
|
# After this script runs:
|
||||||
|
# - pushing master triggers the port-to-omp workflow, which regenerates and
|
||||||
|
# pushes the omp port (picking up the new version)
|
||||||
|
# - pushing the v* tag triggers the publish workflow, which npm-publishes
|
||||||
|
# this package and its omp port together
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
RED='\033[0;31m'
|
||||||
|
GREEN='\033[0;32m'
|
||||||
|
YELLOW='\033[1;33m'
|
||||||
|
BLUE='\033[0;34m'
|
||||||
|
CYAN='\033[0;36m'
|
||||||
|
NC='\033[0m' # No Color
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||||
|
cd "$PROJECT_ROOT"
|
||||||
|
|
||||||
|
DRY_RUN=0
|
||||||
|
for arg in "$@"; do
|
||||||
|
case "$arg" in
|
||||||
|
--dry-run) DRY_RUN=1 ;;
|
||||||
|
-h | --help)
|
||||||
|
echo "usage: $0 [--dry-run]"
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ ! -d .git ] && [ ! -f .git ]; then
|
||||||
|
echo -e "${RED}✗ not a git repository: $PROJECT_ROOT${NC}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! git diff-index --quiet HEAD --; then
|
||||||
|
echo -e "${RED}✗ working tree is dirty — commit or stash before releasing${NC}"
|
||||||
|
git status --short
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
NAME=$(python3 -c "import json;print(json.load(open('package.json'))['name'])")
|
||||||
|
OMP_NAME=$(python3 -c "import json,re;n=json.load(open('package.json'))['name'];print(re.sub(r'^@mikefreno/(.+)$', r'@mikefreno/omp-\1', n))")
|
||||||
|
CURRENT_VERSION=$(python3 -c "import json;print(json.load(open('package.json'))['version'])")
|
||||||
|
|
||||||
|
echo -e "${CYAN}Package:${NC} ${GREEN}${NAME}${NC}"
|
||||||
|
echo -e "${CYAN}Current version:${NC} ${GREEN}v${CURRENT_VERSION}${NC}"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
IFS='.' read -r MAJOR MINOR PATCH <<<"$CURRENT_VERSION"
|
||||||
|
MAJOR=$(echo "$MAJOR" | sed 's/[^0-9].*//')
|
||||||
|
MINOR=$(echo "$MINOR" | sed 's/[^0-9].*//')
|
||||||
|
PATCH=$(echo "$PATCH" | sed 's/[^0-9].*//')
|
||||||
|
|
||||||
|
echo -e "${CYAN}Select version bump type:${NC}"
|
||||||
|
echo " 1) Major (breaking changes) ${MAJOR}.${MINOR}.${PATCH} → $((MAJOR + 1)).0.0"
|
||||||
|
echo " 2) Minor (new features) ${MAJOR}.${MINOR}.${PATCH} → ${MAJOR}.$((MINOR + 1)).0"
|
||||||
|
echo " 3) Patch (bug fixes) ${MAJOR}.${MINOR}.${PATCH} → ${MAJOR}.${MINOR}.$((PATCH + 1))"
|
||||||
|
echo " 4) Custom version"
|
||||||
|
echo " 5) Cancel"
|
||||||
|
echo ""
|
||||||
|
read -p "Enter choice (1-5): " -n 1 -r CHOICE
|
||||||
|
echo ""
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
case $CHOICE in
|
||||||
|
1) NEW_VERSION="$((MAJOR + 1)).0.0" ;;
|
||||||
|
2) NEW_VERSION="${MAJOR}.$((MINOR + 1)).0" ;;
|
||||||
|
3) NEW_VERSION="${MAJOR}.${MINOR}.$((PATCH + 1))" ;;
|
||||||
|
4)
|
||||||
|
read -p "Enter new version (e.g. 0.6.1): " -r NEW_VERSION
|
||||||
|
echo ""
|
||||||
|
;;
|
||||||
|
5)
|
||||||
|
echo -e "${YELLOW}Cancelled.${NC}"
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo -e "${RED}✗ invalid choice${NC}"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
if ! echo "$NEW_VERSION" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$'; then
|
||||||
|
echo -e "${RED}✗ version must be MAJOR.MINOR.PATCH (got: ${NEW_VERSION})${NC}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if git rev-parse -q --verify "refs/tags/v${NEW_VERSION}" >/dev/null; then
|
||||||
|
echo -e "${RED}✗ tag v${NEW_VERSION} already exists${NC}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${CYAN}New version:${NC} ${GREEN}v${NEW_VERSION}${NC}"
|
||||||
|
echo ""
|
||||||
|
echo -e "${YELLOW}This will:${NC}"
|
||||||
|
echo " 1. Set package.json → \"version\": \"${NEW_VERSION}\""
|
||||||
|
echo " 2. Commit the bump"
|
||||||
|
echo " 3. Create annotated tag v${NEW_VERSION}"
|
||||||
|
echo " 4. Push master and the tag to every remote"
|
||||||
|
REMOTES=$(git remote)
|
||||||
|
for r in $REMOTES; do
|
||||||
|
echo " → $r"
|
||||||
|
done
|
||||||
|
echo ""
|
||||||
|
echo -e "${YELLOW}Note: master push triggers the port-to-omp workflow; the v* tag${NC}"
|
||||||
|
echo -e "push triggers the npm publish workflow (this package + omp port).${NC}"
|
||||||
|
echo ""
|
||||||
|
read -p "Proceed? (y/n) " -n 1 -r
|
||||||
|
echo ""
|
||||||
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
||||||
|
echo -e "${YELLOW}Aborted.${NC}"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$DRY_RUN" -eq 1 ]; then
|
||||||
|
echo -e "${BLUE}── dry run: no changes made ──${NC}"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# ── Apply the bump ───────────────────────────────────────────────────────────
|
||||||
|
echo ""
|
||||||
|
echo -e "${CYAN}[1/4]${NC} Updating package.json..."
|
||||||
|
sed -i.bak "s/\"version\": \"[^\"]*\"/\"version\": \"${NEW_VERSION}\"/" package.json
|
||||||
|
rm -f package.json.bak
|
||||||
|
echo -e "${GREEN}✓ package.json updated${NC}"
|
||||||
|
|
||||||
|
if git diff --quiet -- package.json; then
|
||||||
|
echo -e "${RED}✗ package.json did not change${NC}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${CYAN}[2/4]${NC} Committing..."
|
||||||
|
git add package.json
|
||||||
|
git commit -m "chore: bump version to v${NEW_VERSION}"
|
||||||
|
echo -e "${GREEN}✓ committed${NC}"
|
||||||
|
|
||||||
|
echo -e "${CYAN}[3/4]${NC} Tagging..."
|
||||||
|
git tag -a "v${NEW_VERSION}" -m "${NAME} v${NEW_VERSION}"
|
||||||
|
echo -e "${GREEN}✓ tagged v${NEW_VERSION}${NC}"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
echo -e "${CYAN}[4/4]${NC} Pushing..."
|
||||||
|
FAILED=""
|
||||||
|
for r in $REMOTES; do
|
||||||
|
echo -e " → ${BLUE}${r}${NC} master..."
|
||||||
|
if git push "$r" master >/dev/null 2>&1; then
|
||||||
|
echo -e " ${GREEN}✓ master pushed${NC}"
|
||||||
|
else
|
||||||
|
echo -e " ${RED}✗ master push failed${NC}"
|
||||||
|
FAILED="$FAILED $r"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
echo -e " → ${BLUE}${r}${NC} v${NEW_VERSION}..."
|
||||||
|
if git push "$r" "v${NEW_VERSION}" >/dev/null 2>&1; then
|
||||||
|
echo -e " ${GREEN}✓ tag pushed${NC}"
|
||||||
|
else
|
||||||
|
echo -e " ${RED}✗ tag push failed${NC}"
|
||||||
|
FAILED="$FAILED $r"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
if [ -n "$FAILED" ]; then
|
||||||
|
echo -e "${RED}✗ push failed for remote(s):${FAILED}${NC}"
|
||||||
|
echo -e "${YELLOW}Re-run: git push <remote> master && git push <remote> v${NEW_VERSION}${NC}"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${GREEN}═══════════════════════════════════════${NC}"
|
||||||
|
echo -e "${GREEN}✓ ${NAME} v${NEW_VERSION} released${NC}"
|
||||||
|
echo -e "${GREEN}═══════════════════════════════════════${NC}"
|
||||||
|
echo ""
|
||||||
|
echo -e "${CYAN}Version:${NC} ${CURRENT_VERSION} → ${GREEN}${NEW_VERSION}${NC}"
|
||||||
|
echo -e "${CYAN}Tag:${NC} v${NEW_VERSION}"
|
||||||
|
echo ""
|
||||||
|
echo -e "${BLUE}Next steps (automatic, nothing to do):${NC}"
|
||||||
|
echo " 1. Gitea Actions port-to-omp regenerates and pushes the omp port"
|
||||||
|
echo " (which now carries version ${NEW_VERSION})"
|
||||||
|
echo " 2. Gitea Actions publish npm-publishes ${NAME} and"
|
||||||
|
echo " ${OMP_NAME} to the npm registry"
|
||||||
|
echo ""
|
||||||
|
echo -e "${CYAN}Local port (optional):${NC} run 'bun port-to-omp.mjs' in this repo, or"
|
||||||
|
echo "pull in ~/.omp once CI has pushed."
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
---
|
|
||||||
description: Execute tasks from ralpi task files / PRDs using DAG-based dependency resolution, with persistent progress tracking and reflection support
|
|
||||||
---
|
|
||||||
|
|
||||||
# ralpi-task
|
|
||||||
|
|
||||||
Execute tasks from a ralpi task file (checkbox, Fio, phased, or YAML) using DAG-based dependency resolution, with persistent progress tracking and reflection support.
|
|
||||||
|
|
||||||
## When to Use
|
|
||||||
|
|
||||||
- User asks to execute a task file, PRD, or task list (e.g. "run the tasks", "execute the plan")
|
|
||||||
- User wants to run a full ralpi loop from a task file in the project
|
|
||||||
- User wants to resume an interrupted or paused ralpi run
|
|
||||||
- User wants to plan new tasks with the task-manager prompt
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
```
|
|
||||||
/ralpi [task-file] # No args → show plan; path arg → run all tasks
|
|
||||||
/ralpi-run [task-file] # Run all tasks (auto-resumes if progress exists)
|
|
||||||
/ralpi-resume [task-file] # Resume paused/interrupted execution
|
|
||||||
/ralpi-plan [prompt] # Open the Task Manager to plan tasks
|
|
||||||
```
|
|
||||||
|
|
||||||
Note: ralpi runs whole task plans — there is no single-task or next-batch subcommand. To execute only part of a plan, edit the task file and remove/adjust the tasks first.
|
|
||||||
|
|
||||||
## Task File Location
|
|
||||||
|
|
||||||
Default: `README.md` in the current directory. Can be overridden with an explicit path (`@path`, `./path`, `*.md`, `*.yaml`, `*.yml`).
|
|
||||||
|
|
||||||
## Reflection Format
|
|
||||||
|
|
||||||
After completing a task, the task agent ends its response with a reflection block, which the extension parses and passes to downstream tasks:
|
|
||||||
|
|
||||||
```
|
|
||||||
## REFLECTION
|
|
||||||
SUMMARY: [1-2 sentence description of what was accomplished]
|
|
||||||
FILES: [comma-separated list of files created or modified]
|
|
||||||
LEARNINGS:
|
|
||||||
- [key decision, pattern, or architectural choice]
|
|
||||||
- [important API or interface details]
|
|
||||||
- [anything downstream tasks need to know]
|
|
||||||
BLOCKERS: [any unresolved issues, or 'none']
|
|
||||||
```
|
|
||||||
142
src/executor.ts
142
src/executor.ts
@@ -12,6 +12,7 @@ import type { ProgressTracker } from "./progress";
|
|||||||
import type {
|
import type {
|
||||||
ExtensionContext,
|
ExtensionContext,
|
||||||
ModelRuntime,
|
ModelRuntime,
|
||||||
|
AgentSessionEvent,
|
||||||
} from "@earendil-works/pi-coding-agent";
|
} from "@earendil-works/pi-coding-agent";
|
||||||
import {
|
import {
|
||||||
buildTaskPrompt,
|
buildTaskPrompt,
|
||||||
@@ -56,6 +57,28 @@ import {
|
|||||||
} from "./utils";
|
} from "./utils";
|
||||||
import { updateTaskInFile } from "./parser";
|
import { updateTaskInFile } from "./parser";
|
||||||
|
|
||||||
|
// ─── Stream Forwarder (verbose chat style) ────────────────────────────────────
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module-level callback for verbose per-event chat streaming. Set by
|
||||||
|
* `index.ts` at extension startup via {@link setStreamForwarder} when the
|
||||||
|
* config's `execution.chatStyle` is "verbose". `runTask`'s event callback
|
||||||
|
* checks this and forwards each tool_execution_start/end + message_end as
|
||||||
|
* its own chat message — the piolium/pygienium per-event stream. When null
|
||||||
|
* (compact mode, the default), only the completion message with its
|
||||||
|
* expandable tool-call tree shows.
|
||||||
|
*/
|
||||||
|
let _streamForwarder:
|
||||||
|
| ((phase: string, event: AgentSessionEvent) => void)
|
||||||
|
| null = null;
|
||||||
|
|
||||||
|
/** Register the verbose stream forwarder (called by index.ts at startup). */
|
||||||
|
export function setStreamForwarder(
|
||||||
|
fn: ((phase: string, event: AgentSessionEvent) => void) | null,
|
||||||
|
): void {
|
||||||
|
_streamForwarder = fn;
|
||||||
|
}
|
||||||
|
|
||||||
/** Optional callback to post a progress message into the chat history. */
|
/** Optional callback to post a progress message into the chat history. */
|
||||||
export type SendChatMessage = (
|
export type SendChatMessage = (
|
||||||
content: string,
|
content: string,
|
||||||
@@ -222,6 +245,13 @@ export async function runTask(
|
|||||||
/** Review feedback from a rejected review — injected when re-executing
|
/** Review feedback from a rejected review — injected when re-executing
|
||||||
* a task in review-gated mode so the agent knows what to fix. */
|
* a task in review-gated mode so the agent knows what to fix. */
|
||||||
reviewFeedback?: ReviewResult,
|
reviewFeedback?: ReviewResult,
|
||||||
|
/** Session JSONL from a prior interrupted run of this task. When set and
|
||||||
|
* readable, the agent session reopens it and continues from the prior
|
||||||
|
* conversation instead of starting fresh. */
|
||||||
|
resumeSessionFile?: string,
|
||||||
|
/** Called with the session file path as soon as the agent session is
|
||||||
|
* created, so the caller can persist it for a later resume. */
|
||||||
|
onSessionFile?: (sessionFile: string) => void,
|
||||||
): Promise<{
|
): Promise<{
|
||||||
success: boolean;
|
success: boolean;
|
||||||
reflection?: Reflection;
|
reflection?: Reflection;
|
||||||
@@ -231,6 +261,12 @@ export async function runTask(
|
|||||||
outputPreview?: string;
|
outputPreview?: string;
|
||||||
commitMessages?: string[];
|
commitMessages?: string[];
|
||||||
commitSummary?: string;
|
commitSummary?: string;
|
||||||
|
/** Path to the JSONL session file backing this run (for resume). */
|
||||||
|
sessionFile?: string;
|
||||||
|
/** True when a resume was requested but the session could not be opened
|
||||||
|
* from the file — the caller should clear the stored session file so
|
||||||
|
* retries start fresh. */
|
||||||
|
resumeFailed?: boolean;
|
||||||
}> {
|
}> {
|
||||||
const startMs = Date.now();
|
const startMs = Date.now();
|
||||||
|
|
||||||
@@ -336,6 +372,10 @@ export async function runTask(
|
|||||||
projectDir,
|
projectDir,
|
||||||
timeoutMs,
|
timeoutMs,
|
||||||
(event) => {
|
(event) => {
|
||||||
|
// Forward to the verbose stream when enabled.
|
||||||
|
if (_streamForwarder && config.execution.chatStyle === "verbose") {
|
||||||
|
_streamForwarder(`${task.id} · ${task.title}`, event);
|
||||||
|
}
|
||||||
if (event.type === "tool_execution_start") {
|
if (event.type === "tool_execution_start") {
|
||||||
const label = formatToolArg(event.toolName, event.args);
|
const label = formatToolArg(event.toolName, event.args);
|
||||||
toolCalls.push({
|
toolCalls.push({
|
||||||
@@ -358,6 +398,9 @@ export async function runTask(
|
|||||||
config.thinkingLevel,
|
config.thinkingLevel,
|
||||||
false, // noSkills — task sessions need skills
|
false, // noSkills — task sessions need skills
|
||||||
(ctx.modelRegistry as any).runtime as ModelRuntime,
|
(ctx.modelRegistry as any).runtime as ModelRuntime,
|
||||||
|
config.execution.inactivityTimeoutMs,
|
||||||
|
resumeSessionFile,
|
||||||
|
onSessionFile,
|
||||||
);
|
);
|
||||||
|
|
||||||
const durationMs = Date.now() - startMs;
|
const durationMs = Date.now() - startMs;
|
||||||
@@ -382,6 +425,8 @@ export async function runTask(
|
|||||||
success: false,
|
success: false,
|
||||||
error: output.error,
|
error: output.error,
|
||||||
durationMs,
|
durationMs,
|
||||||
|
sessionFile: output.sessionFile,
|
||||||
|
resumeFailed: output.resumeFailed,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -412,6 +457,7 @@ export async function runTask(
|
|||||||
outputPreview,
|
outputPreview,
|
||||||
commitMessages,
|
commitMessages,
|
||||||
commitSummary,
|
commitSummary,
|
||||||
|
sessionFile: output.sessionFile,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -798,6 +844,12 @@ async function executeTask(
|
|||||||
: null;
|
: null;
|
||||||
const worktreeDir = wt?.dir ?? projectDir;
|
const worktreeDir = wt?.dir ?? projectDir;
|
||||||
|
|
||||||
|
// Session file from a prior interrupted run of this task. The first
|
||||||
|
// attempt reopens it so the agent continues with its prior conversation
|
||||||
|
// (tool calls, findings) instead of restarting from scratch; failover
|
||||||
|
// retries start fresh.
|
||||||
|
const resumeSessionFile = progress.getSessionFile(task.id);
|
||||||
|
|
||||||
while (modelAttempt < maxModelAttempts) {
|
while (modelAttempt < maxModelAttempts) {
|
||||||
// Model advancement happens in the cycling branch below (not here) so a
|
// Model advancement happens in the cycling branch below (not here) so a
|
||||||
// same-model retry `continue` doesn't re-advance and accidentally swap
|
// same-model retry `continue` doesn't re-advance and accidentally swap
|
||||||
@@ -858,6 +910,11 @@ async function executeTask(
|
|||||||
currentModel,
|
currentModel,
|
||||||
batchRender,
|
batchRender,
|
||||||
priorReview,
|
priorReview,
|
||||||
|
modelAttempt === 0 && sameModelAttempt === 0
|
||||||
|
? resumeSessionFile
|
||||||
|
: undefined,
|
||||||
|
(sessionFile) =>
|
||||||
|
progress.setSessionFile(task.id, sessionFile),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
@@ -909,6 +966,10 @@ async function executeTask(
|
|||||||
// A FAILED range computation (broken/stale base ref, git error) is
|
// A FAILED range computation (broken/stale base ref, git error) is
|
||||||
// logged as a distinct warning and is never treated as a clean,
|
// logged as a distinct warning and is never treated as a clean,
|
||||||
// verified task — only a GENUINE "no changes" skips review.
|
// verified task — only a GENUINE "no changes" skips review.
|
||||||
|
// Accumulated rejected reviews from earlier passes — injected
|
||||||
|
// into the next review prompt so the reviewer sees prior
|
||||||
|
// findings and can verify they were addressed.
|
||||||
|
const priorReviews: ReviewResult[] = [];
|
||||||
while (true) {
|
while (true) {
|
||||||
if (!baseRef) {
|
if (!baseRef) {
|
||||||
sendChatMessage?.(
|
sendChatMessage?.(
|
||||||
@@ -946,16 +1007,17 @@ async function executeTask(
|
|||||||
reviewInfo.hash,
|
reviewInfo.hash,
|
||||||
reviewInfo.subject,
|
reviewInfo.subject,
|
||||||
reviewInfo.diff,
|
reviewInfo.diff,
|
||||||
{
|
{
|
||||||
projectContext: config.prompts.projectContext,
|
projectContext: config.prompts.projectContext,
|
||||||
focus: config.prompts.reviewFocus,
|
focus: config.prompts.reviewFocus,
|
||||||
diffOptions: {
|
priorReviews,
|
||||||
extraPatterns: compileIgnorePatterns(
|
diffOptions: {
|
||||||
config.review.extraIgnorePatterns,
|
extraPatterns: compileIgnorePatterns(
|
||||||
),
|
config.review.extraIgnorePatterns,
|
||||||
ignorePaths: config.review.ignorePaths,
|
),
|
||||||
},
|
ignorePaths: config.review.ignorePaths,
|
||||||
},
|
},
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const reviewModel = resolveFollowUpModel(
|
const reviewModel = resolveFollowUpModel(
|
||||||
@@ -977,6 +1039,7 @@ async function executeTask(
|
|||||||
`review-${task.id}`,
|
`review-${task.id}`,
|
||||||
config.execution.reviewTimeoutMs,
|
config.execution.reviewTimeoutMs,
|
||||||
reviewModels,
|
reviewModels,
|
||||||
|
config.execution.inactivityTimeoutMs,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!reviewResult.success) {
|
if (!reviewResult.success) {
|
||||||
@@ -1070,6 +1133,9 @@ async function executeTask(
|
|||||||
break; // changes already committed — merge proceeds
|
break; // changes already committed — merge proceeds
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Accumulate the rejected review so the next review pass
|
||||||
|
// sees prior findings and can verify they were addressed.
|
||||||
|
if (review) priorReviews.push(review);
|
||||||
attempt++;
|
attempt++;
|
||||||
reviewRetries++;
|
reviewRetries++;
|
||||||
sendChatMessage?.(
|
sendChatMessage?.(
|
||||||
@@ -1266,6 +1332,7 @@ async function executeTask(
|
|||||||
finalCommitSummary,
|
finalCommitSummary,
|
||||||
finalReview,
|
finalReview,
|
||||||
reviewRetries,
|
reviewRetries,
|
||||||
|
result.sessionFile,
|
||||||
);
|
);
|
||||||
// Auto-update the PRD source file checkbox
|
// Auto-update the PRD source file checkbox
|
||||||
try {
|
try {
|
||||||
@@ -1279,6 +1346,12 @@ async function executeTask(
|
|||||||
|
|
||||||
// Agent session failed (provider error).
|
// Agent session failed (provider error).
|
||||||
// Pi's built-in in-call retry already exhausted for this attempt.
|
// Pi's built-in in-call retry already exhausted for this attempt.
|
||||||
|
// A resumed session that couldn't be opened (corrupt/missing
|
||||||
|
// JSONL) must not be retried — forget it so later attempts (and
|
||||||
|
// future resumes) start fresh.
|
||||||
|
if (result.resumeFailed) {
|
||||||
|
progress.setSessionFile(task.id, undefined);
|
||||||
|
}
|
||||||
// Reattempt on the SAME model a few more times before cycling — a
|
// Reattempt on the SAME model a few more times before cycling — a
|
||||||
// transient outage can outlast pi's per-prompt backoff window.
|
// transient outage can outlast pi's per-prompt backoff window.
|
||||||
sameModelAttempt++;
|
sameModelAttempt++;
|
||||||
@@ -1411,6 +1484,8 @@ async function runFollowUpSession(
|
|||||||
widgetKeySuffix: string,
|
widgetKeySuffix: string,
|
||||||
timeoutMs: number,
|
timeoutMs: number,
|
||||||
models: unknown[],
|
models: unknown[],
|
||||||
|
/** Inactivity timeout for the session (see runAgentSession). */
|
||||||
|
inactivityTimeoutMs = 0,
|
||||||
): Promise<{
|
): Promise<{
|
||||||
result: Awaited<ReturnType<typeof runAgentSession>>;
|
result: Awaited<ReturnType<typeof runAgentSession>>;
|
||||||
toolCalls: ToolCallEntry[];
|
toolCalls: ToolCallEntry[];
|
||||||
@@ -1509,6 +1584,7 @@ async function runFollowUpSession(
|
|||||||
config.thinkingLevel,
|
config.thinkingLevel,
|
||||||
false, // noSkills=false — follow-up sessions load skills too
|
false, // noSkills=false — follow-up sessions load skills too
|
||||||
(ctx.modelRegistry as any).runtime as ModelRuntime,
|
(ctx.modelRegistry as any).runtime as ModelRuntime,
|
||||||
|
inactivityTimeoutMs,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (result.success) break;
|
if (result.success) break;
|
||||||
@@ -1655,6 +1731,7 @@ async function runCommitSession(
|
|||||||
`commit-${task.id}`,
|
`commit-${task.id}`,
|
||||||
config.execution.commitTimeoutMs,
|
config.execution.commitTimeoutMs,
|
||||||
commitModels,
|
commitModels,
|
||||||
|
config.execution.inactivityTimeoutMs,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (commitResult.success) {
|
if (commitResult.success) {
|
||||||
@@ -1752,6 +1829,52 @@ async function resolveConflictsSession(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Merge failed but produced no unmerged paths — no real conflicts to
|
||||||
|
// resolve. This can happen when the branch tip was already merged by the
|
||||||
|
// first attempt (mergeWorktree) before it aborted, or when the merge
|
||||||
|
// fails for a non-conflict reason (dirty index, stale ref). Don't spawn
|
||||||
|
// an agent session for zero conflicts — abort or complete and finish.
|
||||||
|
if (attempt.conflicts.length === 0) {
|
||||||
|
// Try to complete whatever merge state exists; if there's nothing to
|
||||||
|
// commit, abort to leave the working tree clean.
|
||||||
|
if (completeMerge(projectDir)) {
|
||||||
|
sendChatMessage?.(
|
||||||
|
`✓ conflicts auto-resolved for ${task.id} · ${task.title}`,
|
||||||
|
);
|
||||||
|
removeWorktree(projectDir, worktree);
|
||||||
|
progress.markCompleted(
|
||||||
|
task.id,
|
||||||
|
0,
|
||||||
|
undefined,
|
||||||
|
undefined,
|
||||||
|
undefined,
|
||||||
|
[],
|
||||||
|
"",
|
||||||
|
undefined,
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
updateTaskInFile(project.sourcePath, task.id, "completed");
|
||||||
|
} catch {
|
||||||
|
// Best-effort
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
abortMerge(projectDir);
|
||||||
|
sendChatMessage?.(
|
||||||
|
`~ conflict resolution for ${task.id} · ${task.title} — merge produced no conflicts but could not be completed`,
|
||||||
|
);
|
||||||
|
progress.markFailed(
|
||||||
|
task.id,
|
||||||
|
`Merge of ${branch} produced no conflicts but could not be completed`,
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
updateTaskInFile(project.sourcePath, task.id, "failed");
|
||||||
|
} catch {
|
||||||
|
// Best-effort
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
// Conflicts exist — spawn a resolution agent session.
|
// Conflicts exist — spawn a resolution agent session.
|
||||||
const prompt = buildConflictResolutionPrompt(
|
const prompt = buildConflictResolutionPrompt(
|
||||||
task,
|
task,
|
||||||
@@ -1781,6 +1904,7 @@ async function resolveConflictsSession(
|
|||||||
`resolve-${task.id}`,
|
`resolve-${task.id}`,
|
||||||
config.execution.commitTimeoutMs,
|
config.execution.commitTimeoutMs,
|
||||||
models,
|
models,
|
||||||
|
config.execution.inactivityTimeoutMs,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!result.success) {
|
if (!result.success) {
|
||||||
|
|||||||
@@ -185,11 +185,13 @@ export class ProgressTracker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Mark a task as in progress */
|
/** Mark a task as in progress */
|
||||||
markInProgress(taskId: string): void {
|
markInProgress(taskId: string, sessionFile?: string): void {
|
||||||
const prd = this.getPRD();
|
const prd = this.getPRD();
|
||||||
this.ensureTask(prd, taskId);
|
this.ensureTask(prd, taskId);
|
||||||
prd.tasks[taskId].status = "in_progress";
|
prd.tasks[taskId].status = "in_progress";
|
||||||
prd.tasks[taskId].startedAt = new Date().toISOString();
|
prd.tasks[taskId].startedAt = new Date().toISOString();
|
||||||
|
if (sessionFile !== undefined)
|
||||||
|
prd.tasks[taskId].sessionFile = sessionFile;
|
||||||
this.save();
|
this.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,6 +206,7 @@ export class ProgressTracker {
|
|||||||
commitSummary?: string,
|
commitSummary?: string,
|
||||||
review?: ReviewResult,
|
review?: ReviewResult,
|
||||||
reviewRetries?: number,
|
reviewRetries?: number,
|
||||||
|
sessionFile?: string,
|
||||||
): void {
|
): void {
|
||||||
const prd = this.getPRD();
|
const prd = this.getPRD();
|
||||||
this.ensureTask(prd, taskId);
|
this.ensureTask(prd, taskId);
|
||||||
@@ -218,6 +221,7 @@ export class ProgressTracker {
|
|||||||
if (review) prd.tasks[taskId].review = review;
|
if (review) prd.tasks[taskId].review = review;
|
||||||
if (reviewRetries !== undefined)
|
if (reviewRetries !== undefined)
|
||||||
prd.tasks[taskId].reviewRetries = reviewRetries;
|
prd.tasks[taskId].reviewRetries = reviewRetries;
|
||||||
|
if (sessionFile !== undefined) prd.tasks[taskId].sessionFile = sessionFile;
|
||||||
this.save();
|
this.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,6 +240,22 @@ export class ProgressTracker {
|
|||||||
return prd.tasks[taskId]?.status ?? "pending";
|
return prd.tasks[taskId]?.status ?? "pending";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Get the persisted session file path for a task (for resume), if any. */
|
||||||
|
getSessionFile(taskId: string): string | undefined {
|
||||||
|
const prd = this.getPRD();
|
||||||
|
return prd.tasks[taskId]?.sessionFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Persist the session file path for a task without changing its status.
|
||||||
|
* Called as soon as an agent session is created so an interrupted run
|
||||||
|
* can be resumed from the JSONL history. Pass undefined to clear. */
|
||||||
|
setSessionFile(taskId: string, sessionFile: string | undefined): void {
|
||||||
|
const prd = this.getPRD();
|
||||||
|
this.ensureTask(prd, taskId);
|
||||||
|
prd.tasks[taskId].sessionFile = sessionFile;
|
||||||
|
this.save();
|
||||||
|
}
|
||||||
|
|
||||||
/** Get IDs of all completed tasks */
|
/** Get IDs of all completed tasks */
|
||||||
getCompletedTaskIds(): string[] {
|
getCompletedTaskIds(): string[] {
|
||||||
const prd = this.getPRD();
|
const prd = this.getPRD();
|
||||||
|
|||||||
@@ -29,6 +29,12 @@ export interface ReviewPromptOptions {
|
|||||||
focus?: string;
|
focus?: string;
|
||||||
/** Noise-filter overrides (config.review.*). */
|
/** Noise-filter overrides (config.review.*). */
|
||||||
diffOptions?: DiffOptions;
|
diffOptions?: DiffOptions;
|
||||||
|
/** Prior review results from earlier passes in a review-gated loop.
|
||||||
|
* Each rejected review is injected into the next review prompt so the
|
||||||
|
* reviewer can verify prior findings were addressed and catch new
|
||||||
|
* regressions introduced by the fix attempt — instead of re-reviewing
|
||||||
|
* from scratch. */
|
||||||
|
priorReviews?: ReviewResult[];
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── Task Prompt ─────────────────────────────────────────────────────────────
|
// ─── Task Prompt ─────────────────────────────────────────────────────────────
|
||||||
@@ -258,8 +264,12 @@ export function buildReviewPrompt(
|
|||||||
parts.push(renderDiffSection(summary, filtered, "### Diff"));
|
parts.push(renderDiffSection(summary, filtered, "### Diff"));
|
||||||
parts.push("");
|
parts.push("");
|
||||||
|
|
||||||
// ── Custom Review Focus ──
|
// ── Prior Review History (review-gated re-review) ──
|
||||||
|
|
||||||
|
parts.push(renderPriorReviews(opts.priorReviews ?? []));
|
||||||
|
if (opts.priorReviews && opts.priorReviews.length > 0) parts.push("");
|
||||||
|
|
||||||
|
// ── Custom Review Focus ──
|
||||||
if (opts.focus) {
|
if (opts.focus) {
|
||||||
parts.push("## Custom Review Focus");
|
parts.push("## Custom Review Focus");
|
||||||
parts.push(opts.focus);
|
parts.push(opts.focus);
|
||||||
@@ -360,7 +370,11 @@ export function buildReviewPromptUncommitted(
|
|||||||
parts.push(
|
parts.push(
|
||||||
renderDiffSection(summary, filtered, "### Current Tracked Diff (git diff)"),
|
renderDiffSection(summary, filtered, "### Current Tracked Diff (git diff)"),
|
||||||
);
|
);
|
||||||
parts.push("");
|
|
||||||
|
// ── Prior Review History (review-gated re-review) ──
|
||||||
|
|
||||||
|
parts.push(renderPriorReviews(opts.priorReviews ?? []));
|
||||||
|
if (opts.priorReviews && opts.priorReviews.length > 0) parts.push("");
|
||||||
|
|
||||||
// ── Custom Review Focus ──
|
// ── Custom Review Focus ──
|
||||||
|
|
||||||
@@ -469,6 +483,45 @@ function renderDiffSection(
|
|||||||
return lines.join("\n");
|
return lines.join("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render a "Prior Review History" section from earlier rejected reviews.
|
||||||
|
* Returns an empty string when there are no prior reviews so callers omit
|
||||||
|
* the section entirely.
|
||||||
|
*
|
||||||
|
* Each prior review's verdict, summary, and findings are listed so the
|
||||||
|
* reviewer can verify the developer addressed them and watch for new
|
||||||
|
* regressions — instead of re-reviewing from scratch on each pass.
|
||||||
|
*/
|
||||||
|
function renderPriorReviews(priorReviews: ReviewResult[]): string {
|
||||||
|
if (priorReviews.length === 0) return "";
|
||||||
|
const lines: string[] = [];
|
||||||
|
lines.push("## Prior Review History");
|
||||||
|
lines.push(
|
||||||
|
"Previous review pass(es) rejected this task. Verify each finding was",
|
||||||
|
"addressed in the current diff and watch for new regressions:",
|
||||||
|
);
|
||||||
|
lines.push("");
|
||||||
|
for (let i = 0; i < priorReviews.length; i++) {
|
||||||
|
const r = priorReviews[i];
|
||||||
|
if (!r) continue;
|
||||||
|
lines.push(`### Review ${i + 1} — ${r.verdict.toUpperCase()}`);
|
||||||
|
lines.push(`Summary: ${r.summary}`);
|
||||||
|
if (r.findings.length > 0) {
|
||||||
|
lines.push("Findings:");
|
||||||
|
for (const f of r.findings) {
|
||||||
|
const loc = f.file
|
||||||
|
? f.line
|
||||||
|
? ` (${f.file}:${f.line})`
|
||||||
|
: ` (${f.file})`
|
||||||
|
: "";
|
||||||
|
lines.push(`- [${f.severity}]${loc} ${f.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lines.push("");
|
||||||
|
}
|
||||||
|
return lines.join("\n");
|
||||||
|
}
|
||||||
|
|
||||||
function reviewInstructions(): string[] {
|
function reviewInstructions(): string[] {
|
||||||
return [
|
return [
|
||||||
"- **Correctness**: Does the implementation fulfill the task requirements?",
|
"- **Correctness**: Does the implementation fulfill the task requirements?",
|
||||||
|
|||||||
17
src/types.ts
17
src/types.ts
@@ -161,6 +161,10 @@ export interface TaskProgressInfo {
|
|||||||
commitSummary?: string;
|
commitSummary?: string;
|
||||||
/** Number of review-fix re-execution attempts made (review-gated mode) */
|
/** Number of review-fix re-execution attempts made (review-gated mode) */
|
||||||
reviewRetries?: number;
|
reviewRetries?: number;
|
||||||
|
/** Path to the JSONL session file backing this task's agent session,
|
||||||
|
* persisted so a resume can reopen it and continue where the
|
||||||
|
* interrupted session left off. */
|
||||||
|
sessionFile?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ProgressState {
|
export interface ProgressState {
|
||||||
@@ -205,6 +209,11 @@ export interface RalpiConfig {
|
|||||||
execution: {
|
execution: {
|
||||||
/** Task execution timeout in milliseconds */
|
/** Task execution timeout in milliseconds */
|
||||||
timeoutMs: number;
|
timeoutMs: number;
|
||||||
|
/** Inactivity timeout in milliseconds — if no agent session event
|
||||||
|
* arrives within this window, the task is considered hung (e.g. a
|
||||||
|
* bash subprocess that never returns) and the session is aborted.
|
||||||
|
* 0 = disabled. */
|
||||||
|
inactivityTimeoutMs: number;
|
||||||
/** Maximum parallel tasks (0 = unlimited) */
|
/** Maximum parallel tasks (0 = unlimited) */
|
||||||
maxParallel: number;
|
maxParallel: number;
|
||||||
/** Round-robin model list for parallel tasks (empty = inherit parent model) */
|
/** Round-robin model list for parallel tasks (empty = inherit parent model) */
|
||||||
@@ -262,6 +271,12 @@ export interface RalpiConfig {
|
|||||||
* - "parallel": only when maxParallel > 1 and mode is parallel
|
* - "parallel": only when maxParallel > 1 and mode is parallel
|
||||||
* - "always": every task gets its own worktree */
|
* - "always": every task gets its own worktree */
|
||||||
worktrees: "always" | "parallel" | "never";
|
worktrees: "always" | "parallel" | "never";
|
||||||
|
/** Chat rendering style for tool calls during task execution.
|
||||||
|
* - "compact": single completion message per task with an expandable
|
||||||
|
* tool-call tree (collapsed shows last 3, expanded shows all).
|
||||||
|
* - "verbose": per-event stream — each tool start/end and assistant
|
||||||
|
* turn is its own chat line (piolium/pygienium-style). */
|
||||||
|
chatStyle: "compact" | "verbose";
|
||||||
};
|
};
|
||||||
prompts: {
|
prompts: {
|
||||||
/** Additional context injected into every task prompt */
|
/** Additional context injected into every task prompt */
|
||||||
@@ -295,6 +310,7 @@ export const DEFAULT_CONFIG: RalpiConfig = {
|
|||||||
},
|
},
|
||||||
execution: {
|
execution: {
|
||||||
timeoutMs: 0, // 0 = inherit Pi's own defaults (no ralpi-level timeout)
|
timeoutMs: 0, // 0 = inherit Pi's own defaults (no ralpi-level timeout)
|
||||||
|
inactivityTimeoutMs: 0, // 0 = disabled (no inactivity hang detection)
|
||||||
maxParallel: 3,
|
maxParallel: 3,
|
||||||
models: [],
|
models: [],
|
||||||
autoCommit: true,
|
autoCommit: true,
|
||||||
@@ -310,6 +326,7 @@ export const DEFAULT_CONFIG: RalpiConfig = {
|
|||||||
loopTimeoutMs: 0, // 0 = no limit
|
loopTimeoutMs: 0, // 0 = no limit
|
||||||
worktrees: "parallel", // worktree isolation for parallel tasks by default
|
worktrees: "parallel", // worktree isolation for parallel tasks by default
|
||||||
maxSameModelAttempts: 5, // retry the same model up to 5 times before cycling to the next
|
maxSameModelAttempts: 5, // retry the same model up to 5 times before cycling to the next
|
||||||
|
chatStyle: "compact", // compact = completion message with tool-call tree; verbose = per-event stream
|
||||||
},
|
},
|
||||||
prompts: {
|
prompts: {
|
||||||
projectContext: "",
|
projectContext: "",
|
||||||
|
|||||||
86
src/utils.ts
86
src/utils.ts
@@ -611,6 +611,18 @@ export async function runAgentSession(
|
|||||||
* rate-limit normalization) are available. When omitted, the SDK creates
|
* rate-limit normalization) are available. When omitted, the SDK creates
|
||||||
* a fresh runtime from models.json only — extension providers are lost. */
|
* a fresh runtime from models.json only — extension providers are lost. */
|
||||||
modelRuntime?: ModelRuntime,
|
modelRuntime?: ModelRuntime,
|
||||||
|
/** Inactivity timeout in milliseconds — if no agent session event arrives
|
||||||
|
* within this window, the task is considered hung (e.g. a bash subprocess
|
||||||
|
* that never returns) and the session is aborted. 0 = disabled. */
|
||||||
|
inactivityTimeoutMs = 0,
|
||||||
|
/** Existing session JSONL file to resume. The session reopens the file and
|
||||||
|
* appends to it, so the agent sees the full prior conversation and can
|
||||||
|
* continue rather than redo prior tool calls. When the file is missing,
|
||||||
|
* a fresh session is created instead (with a warning). */
|
||||||
|
resumeSessionFile?: string,
|
||||||
|
/** Called with the session file path as soon as the session is created, so
|
||||||
|
* callers can persist it for resume before the session completes. */
|
||||||
|
onSessionFile?: (sessionFile: string) => void,
|
||||||
): Promise<{
|
): Promise<{
|
||||||
success: boolean;
|
success: boolean;
|
||||||
text: string;
|
text: string;
|
||||||
@@ -618,6 +630,13 @@ export async function runAgentSession(
|
|||||||
toolUsage: ToolUsage;
|
toolUsage: ToolUsage;
|
||||||
stopReason?: string;
|
stopReason?: string;
|
||||||
events: AgentSessionEvent[];
|
events: AgentSessionEvent[];
|
||||||
|
/** Path to the JSONL session file backing this session (set once the
|
||||||
|
* session is created; enables resume). */
|
||||||
|
sessionFile?: string;
|
||||||
|
/** True when a resume was requested but the session could not be created
|
||||||
|
* from the file (corrupt/unreadable JSONL). Callers should clear the
|
||||||
|
* stored session file so retries start fresh. */
|
||||||
|
resumeFailed?: boolean;
|
||||||
}> {
|
}> {
|
||||||
const toolUsage: ToolUsage = {
|
const toolUsage: ToolUsage = {
|
||||||
read: 0,
|
read: 0,
|
||||||
@@ -638,6 +657,16 @@ export async function runAgentSession(
|
|||||||
session?: Awaited<ReturnType<typeof createAgentSession>>["session"];
|
session?: Awaited<ReturnType<typeof createAgentSession>>["session"];
|
||||||
} = {};
|
} = {};
|
||||||
|
|
||||||
|
let sessionFile: string | undefined;
|
||||||
|
let sessionCreated = false;
|
||||||
|
// Inactivity watchdog: aborts the session when no events arrive within
|
||||||
|
// inactivityTimeoutMs. The SDK emits an event for every tool start/end/
|
||||||
|
// update and message start/end, so silence means the agent is stuck
|
||||||
|
// (typically a hung bash subprocess producing no output).
|
||||||
|
let inactivityInterval: NodeJS.Timeout | null = null;
|
||||||
|
let inactivityAborted = false;
|
||||||
|
let lastEventTime = 0;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Loop sessions load the full normal pi context: extensions (so all
|
// Loop sessions load the full normal pi context: extensions (so all
|
||||||
// extension-provided tools register), skills, and project context
|
// extension-provided tools register), skills, and project context
|
||||||
@@ -653,9 +682,30 @@ export async function runAgentSession(
|
|||||||
});
|
});
|
||||||
await loader.reload();
|
await loader.reload();
|
||||||
|
|
||||||
|
// Persist sessions under the ralpi project's `.ralpi/sessions/` so they
|
||||||
|
// survive worktree removal and are findable from the main repo on resume.
|
||||||
|
// Worktrees live inside `<project>/.ralpi/worktrees/...`, so walking up
|
||||||
|
// from the agent's cwd always finds the main project's `.ralpi` first.
|
||||||
|
const ralpiDir = findRalpiDir(cwd);
|
||||||
|
const sessionDir = ralpiDir
|
||||||
|
? path.join(ralpiDir, ".ralpi", "sessions")
|
||||||
|
: path.join(cwd, ".ralpi", "sessions");
|
||||||
|
|
||||||
|
let sessionManager: SessionManager;
|
||||||
|
if (resumeSessionFile && fs.existsSync(resumeSessionFile)) {
|
||||||
|
sessionManager = SessionManager.open(resumeSessionFile, sessionDir, cwd);
|
||||||
|
} else {
|
||||||
|
if (resumeSessionFile) {
|
||||||
|
console.warn(
|
||||||
|
`[ralpi] resume session file not found (${resumeSessionFile}) — starting a fresh session`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
sessionManager = SessionManager.create(cwd, sessionDir);
|
||||||
|
}
|
||||||
|
|
||||||
const result = await createAgentSession({
|
const result = await createAgentSession({
|
||||||
cwd,
|
cwd,
|
||||||
sessionManager: SessionManager.inMemory(),
|
sessionManager,
|
||||||
resourceLoader: loader,
|
resourceLoader: loader,
|
||||||
settingsManager: SettingsManager.create(cwd, getAgentDir()),
|
settingsManager: SettingsManager.create(cwd, getAgentDir()),
|
||||||
modelRuntime,
|
modelRuntime,
|
||||||
@@ -663,8 +713,12 @@ export async function runAgentSession(
|
|||||||
model: model as any,
|
model: model as any,
|
||||||
thinkingLevel: thinkingLevel as any,
|
thinkingLevel: thinkingLevel as any,
|
||||||
});
|
});
|
||||||
|
sessionCreated = true;
|
||||||
sessionRef.session = result.session;
|
sessionRef.session = result.session;
|
||||||
|
|
||||||
|
sessionFile = result.session.sessionFile;
|
||||||
|
if (sessionFile) onSessionFile?.(sessionFile);
|
||||||
|
|
||||||
// Wire external abort signal
|
// Wire external abort signal
|
||||||
const abortHandler = () => result.session.agent.abort();
|
const abortHandler = () => result.session.agent.abort();
|
||||||
signal?.addEventListener("abort", abortHandler, { once: true });
|
signal?.addEventListener("abort", abortHandler, { once: true });
|
||||||
@@ -672,8 +726,26 @@ export async function runAgentSession(
|
|||||||
let finalText = "";
|
let finalText = "";
|
||||||
let errorMessage: string | undefined;
|
let errorMessage: string | undefined;
|
||||||
let stopReason: string | undefined;
|
let stopReason: string | undefined;
|
||||||
|
lastEventTime = Date.now();
|
||||||
|
|
||||||
|
// Inactivity watchdog: check the silence window on an interval and abort
|
||||||
|
// (plus kill any hung bash subprocess) when it is exceeded.
|
||||||
|
if (inactivityTimeoutMs > 0) {
|
||||||
|
const intervalMs = Math.min(inactivityTimeoutMs, 5000);
|
||||||
|
inactivityInterval = setInterval(() => {
|
||||||
|
if (!sessionRef.session) return;
|
||||||
|
if (Date.now() - lastEventTime <= inactivityTimeoutMs) return;
|
||||||
|
inactivityAborted = true;
|
||||||
|
sessionRef.session.agent.abort();
|
||||||
|
sessionRef.session.abortBash();
|
||||||
|
errorMessage = `Task aborted: inactivity timeout (no events for ${Math.round(inactivityTimeoutMs / 1000)}s)`;
|
||||||
|
if (inactivityInterval) clearInterval(inactivityInterval);
|
||||||
|
inactivityInterval = null;
|
||||||
|
}, intervalMs);
|
||||||
|
}
|
||||||
|
|
||||||
const unsubscribe = result.session.subscribe((event) => {
|
const unsubscribe = result.session.subscribe((event) => {
|
||||||
|
lastEventTime = Date.now();
|
||||||
onEvent?.(event);
|
onEvent?.(event);
|
||||||
|
|
||||||
if (event.type === "message_end") {
|
if (event.type === "message_end") {
|
||||||
@@ -685,7 +757,10 @@ export async function runAgentSession(
|
|||||||
};
|
};
|
||||||
if (message.role !== "assistant") return;
|
if (message.role !== "assistant") return;
|
||||||
if (message.stopReason) stopReason = message.stopReason;
|
if (message.stopReason) stopReason = message.stopReason;
|
||||||
if (message.errorMessage) errorMessage = message.errorMessage;
|
// Keep the inactivity-timeout message: the abort's own errorMessage
|
||||||
|
// would otherwise clobber the (more useful) hang explanation.
|
||||||
|
if (message.errorMessage && !inactivityAborted)
|
||||||
|
errorMessage = message.errorMessage;
|
||||||
const text = extractAssistantText(message.content);
|
const text = extractAssistantText(message.content);
|
||||||
if (text) finalText = text;
|
if (text) finalText = text;
|
||||||
}
|
}
|
||||||
@@ -718,6 +793,7 @@ export async function runAgentSession(
|
|||||||
toolUsage,
|
toolUsage,
|
||||||
stopReason,
|
stopReason,
|
||||||
events: [], // streamed to file
|
events: [], // streamed to file
|
||||||
|
sessionFile,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -727,6 +803,7 @@ export async function runAgentSession(
|
|||||||
toolUsage,
|
toolUsage,
|
||||||
stopReason,
|
stopReason,
|
||||||
events: [],
|
events: [],
|
||||||
|
sessionFile,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (timeoutHandle) clearTimeout(timeoutHandle);
|
if (timeoutHandle) clearTimeout(timeoutHandle);
|
||||||
@@ -736,9 +813,14 @@ export async function runAgentSession(
|
|||||||
error: error instanceof Error ? error.message : String(error),
|
error: error instanceof Error ? error.message : String(error),
|
||||||
toolUsage,
|
toolUsage,
|
||||||
events: [],
|
events: [],
|
||||||
|
sessionFile,
|
||||||
|
// A requested resume that failed to open (corrupt/unreadable file)
|
||||||
|
// should not be retried — callers clear the stored file and go fresh.
|
||||||
|
resumeFailed: resumeSessionFile !== undefined && !sessionCreated,
|
||||||
};
|
};
|
||||||
} finally {
|
} finally {
|
||||||
sessionRef.session?.dispose();
|
sessionRef.session?.dispose();
|
||||||
|
if (inactivityInterval) clearInterval(inactivityInterval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user