astrocode-workflow 0.4.1 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/astro/workflow-runner.d.ts +1 -5
- package/dist/src/astro/workflow-runner.js +6 -17
- package/dist/src/hooks/inject-provider.js +23 -0
- package/dist/src/index.js +0 -6
- package/dist/src/tools/health.js +0 -31
- package/dist/src/tools/index.js +0 -3
- package/dist/src/tools/repair.js +4 -37
- package/dist/src/tools/workflow.js +192 -209
- package/package.json +1 -1
- package/src/astro/workflow-runner.ts +5 -25
- package/src/hooks/inject-provider.ts +24 -0
- package/src/index.ts +0 -7
- package/src/tools/health.ts +0 -29
- package/src/tools/index.ts +2 -5
- package/src/tools/repair.ts +4 -38
- package/src/tools/workflow.ts +25 -44
- package/src/state/repo-lock.ts +0 -706
- package/src/state/workflow-repo-lock.ts +0 -111
- package/src/tools/lock.ts +0 -75
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Executes the workflow loop.
|
|
3
3
|
* Everything that mutates the repo (tool calls, steps) runs inside this scope.
|
|
4
4
|
*
|
|
5
5
|
* Replace the internals with your actual astro/opencode driver loop.
|
|
6
6
|
*/
|
|
7
7
|
export declare function runAstroWorkflow(opts: {
|
|
8
|
-
lockPath: string;
|
|
9
|
-
repoRoot: string;
|
|
10
|
-
sessionId: string;
|
|
11
|
-
owner?: string;
|
|
12
8
|
proceedOneStep: () => Promise<{
|
|
13
9
|
done: boolean;
|
|
14
10
|
}>;
|
|
@@ -1,25 +1,14 @@
|
|
|
1
1
|
// src/astro/workflow-runner.ts
|
|
2
|
-
import { acquireRepoLock } from "../state/repo-lock";
|
|
3
|
-
import { workflowRepoLock } from "../state/workflow-repo-lock";
|
|
4
2
|
/**
|
|
5
|
-
*
|
|
3
|
+
* Executes the workflow loop.
|
|
6
4
|
* Everything that mutates the repo (tool calls, steps) runs inside this scope.
|
|
7
5
|
*
|
|
8
6
|
* Replace the internals with your actual astro/opencode driver loop.
|
|
9
7
|
*/
|
|
10
8
|
export async function runAstroWorkflow(opts) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
fn: async () => {
|
|
17
|
-
// ✅ Lock is held ONCE for the entire run. Tool calls can "rattle through".
|
|
18
|
-
while (true) {
|
|
19
|
-
const { done } = await opts.proceedOneStep();
|
|
20
|
-
if (done)
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
},
|
|
24
|
-
});
|
|
9
|
+
while (true) {
|
|
10
|
+
const { done } = await opts.proceedOneStep();
|
|
11
|
+
if (done)
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
25
14
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { selectEligibleInjects } from "../tools/injects";
|
|
2
2
|
import { injectChatPrompt } from "../ui/inject";
|
|
3
3
|
import { nowISO } from "../shared/time";
|
|
4
|
+
import { getActiveRun } from "../workflow/state-machine";
|
|
4
5
|
export function createInjectProvider(opts) {
|
|
5
6
|
const { ctx, config, runtime } = opts;
|
|
6
7
|
const { db } = runtime;
|
|
@@ -184,6 +185,28 @@ export function createInjectProvider(opts) {
|
|
|
184
185
|
await maybeAutoApprove(sessionId);
|
|
185
186
|
// Inject eligible injects after workflow tool execution
|
|
186
187
|
await injectEligibleInjects(sessionId, `tool_after:${input.tool}`);
|
|
188
|
+
// Inject Workflow Pulse if a run is active
|
|
189
|
+
await maybeInjectWorkflowPulse(sessionId);
|
|
187
190
|
},
|
|
188
191
|
};
|
|
192
|
+
async function maybeInjectWorkflowPulse(sessionId) {
|
|
193
|
+
if (!db)
|
|
194
|
+
return;
|
|
195
|
+
try {
|
|
196
|
+
const active = getActiveRun(db);
|
|
197
|
+
if (!active)
|
|
198
|
+
return;
|
|
199
|
+
const agentSuffix = active.current_stage_key ? ` (Agent: ${active.current_stage_key})` : "";
|
|
200
|
+
const pulseText = `📡 **ASTRO PULSE:** Run \`${active.run_id}\` is \`${active.status}\`${agentSuffix}.`;
|
|
201
|
+
await injectChatPrompt({
|
|
202
|
+
ctx,
|
|
203
|
+
sessionId,
|
|
204
|
+
text: pulseText,
|
|
205
|
+
agent: "Astro"
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
// Ignore pulse errors
|
|
210
|
+
}
|
|
211
|
+
}
|
|
189
212
|
}
|
package/dist/src/index.js
CHANGED
|
@@ -37,12 +37,6 @@ const Astrocode = async (ctx) => {
|
|
|
37
37
|
throw new Error("Astrocode requires ctx.directory to be a string repo root.");
|
|
38
38
|
}
|
|
39
39
|
const repoRoot = ctx.directory;
|
|
40
|
-
// NOTE: Repo locking is handled at the workflow level via workflowRepoLock.
|
|
41
|
-
// The workflow tool correctly acquires and holds the lock for the entire workflow execution.
|
|
42
|
-
// Plugin-level locking is unnecessary and architecturally incorrect since:
|
|
43
|
-
// - The lock would be held for the entire session lifecycle (too long)
|
|
44
|
-
// - Individual tools are designed to be called within workflow context where lock is held
|
|
45
|
-
// - Workflow-level locking with refcounting prevents lock churn during tool execution
|
|
46
40
|
// Always load config first - this provides defaults even in limited mode
|
|
47
41
|
let pluginConfig;
|
|
48
42
|
try {
|
package/dist/src/tools/health.js
CHANGED
|
@@ -19,36 +19,6 @@ export function createAstroHealthTool(opts) {
|
|
|
19
19
|
lines.push(`- PID: ${process.pid || "unknown"}`);
|
|
20
20
|
lines.push(`- Repo: ${repoRoot}`);
|
|
21
21
|
lines.push(`- DB Path: ${fullDbPath}`);
|
|
22
|
-
// Lock status
|
|
23
|
-
const lockPath = `${repoRoot}/.astro/astro.lock`;
|
|
24
|
-
try {
|
|
25
|
-
if (fs.existsSync(lockPath)) {
|
|
26
|
-
const lockContent = fs.readFileSync(lockPath, "utf8").trim();
|
|
27
|
-
const parts = lockContent.split(" ");
|
|
28
|
-
if (parts.length >= 2) {
|
|
29
|
-
const pid = parseInt(parts[0]);
|
|
30
|
-
const startedAt = parts[1];
|
|
31
|
-
// Check if PID is still running
|
|
32
|
-
try {
|
|
33
|
-
process.kill(pid, 0); // Signal 0 just checks if process exists
|
|
34
|
-
lines.push(`- Lock: HELD by PID ${pid} (started ${startedAt})`);
|
|
35
|
-
}
|
|
36
|
-
catch {
|
|
37
|
-
lines.push(`- Lock: STALE (PID ${pid} not running, started ${startedAt})`);
|
|
38
|
-
lines.push(` → Run: rm "${lockPath}"`);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
lines.push(`- Lock: MALFORMED (${lockContent})`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
lines.push(`- Lock: NONE (no lock file)`);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
catch (e) {
|
|
50
|
-
lines.push(`- Lock: ERROR (${String(e)})`);
|
|
51
|
-
}
|
|
52
22
|
// DB file status
|
|
53
23
|
const dbExists = fs.existsSync(fullDbPath);
|
|
54
24
|
const walExists = fs.existsSync(`${fullDbPath}-wal`);
|
|
@@ -109,7 +79,6 @@ export function createAstroHealthTool(opts) {
|
|
|
109
79
|
lines.push(`## Status`);
|
|
110
80
|
lines.push(`✅ DB accessible`);
|
|
111
81
|
lines.push(`✅ Schema valid`);
|
|
112
|
-
lines.push(`✅ Lock file checked`);
|
|
113
82
|
if (walExists || shmExists) {
|
|
114
83
|
lines.push(`⚠️ WAL/SHM files present - indicates unclean shutdown or active transaction`);
|
|
115
84
|
}
|
package/dist/src/tools/index.js
CHANGED
|
@@ -11,7 +11,6 @@ import { createAstroRepairTool } from "./repair";
|
|
|
11
11
|
import { createAstroHealthTool } from "./health";
|
|
12
12
|
import { createAstroResetTool } from "./reset";
|
|
13
13
|
import { createAstroMetricsTool } from "./metrics";
|
|
14
|
-
import { createAstroLockStatusTool } from "./lock";
|
|
15
14
|
export function createAstroTools(opts) {
|
|
16
15
|
const { ctx, config, agents, runtime } = opts;
|
|
17
16
|
const { db } = runtime;
|
|
@@ -23,7 +22,6 @@ export function createAstroTools(opts) {
|
|
|
23
22
|
tools.astro_health = createAstroHealthTool({ ctx, config, db });
|
|
24
23
|
tools.astro_reset = createAstroResetTool({ ctx, config, db });
|
|
25
24
|
tools.astro_metrics = createAstroMetricsTool({ ctx, config });
|
|
26
|
-
tools.astro_lock_status = createAstroLockStatusTool({ ctx });
|
|
27
25
|
// Recovery tool - available even in limited mode to allow DB initialization
|
|
28
26
|
tools.astro_init = createAstroInitTool({ ctx, config, runtime });
|
|
29
27
|
// Database-dependent tools
|
|
@@ -85,7 +83,6 @@ export function createAstroTools(opts) {
|
|
|
85
83
|
["_astro_health", "astro_health"],
|
|
86
84
|
["_astro_reset", "astro_reset"],
|
|
87
85
|
["_astro_metrics", "astro_metrics"],
|
|
88
|
-
["_astro_lock_status", "astro_lock_status"],
|
|
89
86
|
];
|
|
90
87
|
// Only add aliases for tools that exist
|
|
91
88
|
for (const [alias, target] of aliases) {
|
package/dist/src/tools/repair.js
CHANGED
|
@@ -1,53 +1,20 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
1
|
import { tool } from "@opencode-ai/plugin/tool";
|
|
3
2
|
import { withTx } from "../state/db";
|
|
4
3
|
import { repairState, formatRepairReport } from "../workflow/repair";
|
|
5
4
|
import { putArtifact } from "../workflow/artifacts";
|
|
6
5
|
import { nowISO } from "../shared/time";
|
|
7
|
-
import { getLockStatus, tryRemoveStaleLock } from "../state/repo-lock";
|
|
8
6
|
export function createAstroRepairTool(opts) {
|
|
9
7
|
const { ctx, config, db } = opts;
|
|
10
8
|
return tool({
|
|
11
|
-
description: "Repair Astrocode invariants and recover from inconsistent DB state.
|
|
9
|
+
description: "Repair Astrocode invariants and recover from inconsistent DB state. Writes a repair report artifact.",
|
|
12
10
|
args: {
|
|
13
11
|
write_report_artifact: tool.schema.boolean().default(true),
|
|
14
|
-
repair_lock: tool.schema.boolean().default(true).describe("Attempt to remove stale/dead lock files"),
|
|
15
12
|
},
|
|
16
|
-
execute: async ({ write_report_artifact
|
|
13
|
+
execute: async ({ write_report_artifact }) => {
|
|
17
14
|
const repoRoot = ctx.directory;
|
|
18
|
-
|
|
19
|
-
// First, check and repair lock if requested
|
|
20
|
-
const lockLines = [];
|
|
21
|
-
const lockStatus = getLockStatus(lockPath);
|
|
22
|
-
if (lockStatus.exists) {
|
|
23
|
-
lockLines.push("## Lock Status");
|
|
24
|
-
lockLines.push(`- Lock found: ${lockPath}`);
|
|
25
|
-
lockLines.push(`- PID: ${lockStatus.pid} (${lockStatus.pidAlive ? 'alive' : 'dead'})`);
|
|
26
|
-
lockLines.push(`- Age: ${lockStatus.ageMs ? Math.floor(lockStatus.ageMs / 1000) : '?'}s`);
|
|
27
|
-
lockLines.push(`- Status: ${lockStatus.isStale ? 'stale' : 'fresh'}`);
|
|
28
|
-
if (repair_lock) {
|
|
29
|
-
const result = tryRemoveStaleLock(lockPath);
|
|
30
|
-
if (result.removed) {
|
|
31
|
-
lockLines.push(`- **Removed**: ${result.reason}`);
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
lockLines.push(`- **Not removed**: ${result.reason}`);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
if (!lockStatus.pidAlive || lockStatus.isStale) {
|
|
39
|
-
lockLines.push(`- **Recommendation**: Run with repair_lock=true to remove this ${!lockStatus.pidAlive ? 'dead' : 'stale'} lock`);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
lockLines.push("");
|
|
43
|
-
}
|
|
44
|
-
// Then repair database state
|
|
15
|
+
// Repair database state
|
|
45
16
|
const report = withTx(db, () => repairState(db, config));
|
|
46
|
-
const
|
|
47
|
-
// Combine lock and DB repair
|
|
48
|
-
const fullMd = lockLines.length > 0
|
|
49
|
-
? `# Astrocode Repair Report\n\n${lockLines.join("\n")}\n${dbMd.replace(/^# Astrocode repair report\n*/i, "")}`
|
|
50
|
-
: dbMd;
|
|
17
|
+
const fullMd = formatRepairReport(report);
|
|
51
18
|
if (write_report_artifact) {
|
|
52
19
|
const rel = `.astro/repair/repair_${nowISO().replace(/[:.]/g, "-")}.md`;
|
|
53
20
|
const a = putArtifact({ repoRoot, db, run_id: null, stage_key: null, type: "log", rel_path: rel, content: fullMd, meta: { kind: "repair" } });
|