@yairabf/engineering-workflows 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agents/skills/grill-me/SKILL.md +7 -0
- package/.agents/skills/grill-me/agents/openai.yaml +5 -0
- package/.agents/skills/grilling/SKILL.md +28 -0
- package/.agents/skills/grilling/agents/openai.yaml +3 -0
- package/engineering-workflows/.agents/skills/workflow-complete/SKILL.md +17 -0
- package/engineering-workflows/.agents/skills/workflow-implement/SKILL.md +17 -0
- package/engineering-workflows/.agents/skills/workflow-implement/references/implementation-handoff.md +17 -0
- package/engineering-workflows/.agents/skills/workflow-initiate/SKILL.md +49 -0
- package/engineering-workflows/.agents/skills/workflow-initiate/references/preparation.md +80 -0
- package/engineering-workflows/.agents/skills/workflow-initiate/references/prerequisites.json +53 -0
- package/engineering-workflows/.agents/skills/workflow-initiate/references/readiness.md +87 -0
- package/engineering-workflows/.agents/skills/workflow-initiate/scripts/marker.mjs +97 -0
- package/engineering-workflows/.agents/skills/workflow-investigate/SKILL.md +55 -0
- package/engineering-workflows/.agents/skills/workflow-investigate/references/investigation-brief.md +38 -0
- package/engineering-workflows/.agents/skills/workflow-plan/SKILL.md +46 -0
- package/engineering-workflows/.agents/skills/workflow-plan/references/implementation-plan.md +31 -0
- package/engineering-workflows/.agents/skills/workflow-pr/SKILL.md +20 -0
- package/engineering-workflows/.agents/skills/workflow-pr/scripts/check-pr.mjs +41 -0
- package/engineering-workflows/.agents/skills/workflow-review/SKILL.md +58 -0
- package/engineering-workflows/.agents/skills/workflow-review/references/independent-review.md +29 -0
- package/engineering-workflows/.claude/skills/workflow-complete/SKILL.md +6 -0
- package/engineering-workflows/.claude/skills/workflow-implement/SKILL.md +10 -0
- package/engineering-workflows/.claude/skills/workflow-initiate/SKILL.md +10 -0
- package/engineering-workflows/.claude/skills/workflow-investigate/SKILL.md +10 -0
- package/engineering-workflows/.claude/skills/workflow-plan/SKILL.md +10 -0
- package/engineering-workflows/.claude/skills/workflow-pr/SKILL.md +6 -0
- package/engineering-workflows/.claude/skills/workflow-review/SKILL.md +10 -0
- package/engineering-workflows/README.md +282 -0
- package/engineering-workflows/builder/delivery.mjs +177 -0
- package/engineering-workflows/builder/delivery.test.mjs +154 -0
- package/engineering-workflows/builder/drive.mjs +184 -0
- package/engineering-workflows/builder/drive.test.mjs +350 -0
- package/engineering-workflows/builder/handoff.mjs +40 -0
- package/engineering-workflows/builder/mission-role.mjs +294 -0
- package/engineering-workflows/builder/pi-session.mjs +45 -0
- package/engineering-workflows/builder/reader-guard.mjs +58 -0
- package/engineering-workflows/builder/role-session.mjs +42 -0
- package/engineering-workflows/builder/stage.mjs +446 -0
- package/engineering-workflows/builder/workspace-tool.mjs +20 -0
- package/engineering-workflows/builder-plan.mjs +174 -0
- package/engineering-workflows/builder-project.mjs +117 -0
- package/engineering-workflows/check-package-visibility.mjs +54 -0
- package/engineering-workflows/install.mjs +182 -0
- package/engineering-workflows/mission/README.md +281 -0
- package/engineering-workflows/mission/controller.mjs +54 -0
- package/engineering-workflows/mission/mission-controller.test.mjs +382 -0
- package/engineering-workflows/mission/mission-mcp.test.mjs +102 -0
- package/engineering-workflows/mission/status.mjs +113 -0
- package/engineering-workflows/mission/store.mjs +180 -0
- package/engineering-workflows/mission/tools.mjs +54 -0
- package/engineering-workflows/mission/transitions.mjs +269 -0
- package/engineering-workflows/mission-controller.mjs +72 -0
- package/engineering-workflows/mission-mcp.mjs +66 -0
- package/engineering-workflows/prerequisites.mjs +205 -0
- package/engineering-workflows/readiness.mjs +265 -0
- package/engineering-workflows/release-targets.mjs +12 -0
- package/engineering-workflows/setup.mjs +70 -0
- package/engineering-workflows/stage-release.mjs +43 -0
- package/engineering-workflows/upgrade.mjs +333 -0
- package/engineering-workflows/workflows/development.md +374 -0
- package/engineering-workflows/workflows/engineering-workflows.yml +90 -0
- package/package.json +21 -4
- package/README.md +0 -3
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process'
|
|
2
|
+
import { existsSync, mkdtempSync, rmSync, realpathSync, readFileSync } from 'node:fs'
|
|
3
|
+
import { dirname, join } from 'node:path'
|
|
4
|
+
import { fileURLToPath } from 'node:url'
|
|
5
|
+
import { openStore } from '../mission/store.mjs'
|
|
6
|
+
import { publishCandidate } from './delivery.mjs'
|
|
7
|
+
import { projectDirectory } from '../builder-project.mjs'
|
|
8
|
+
|
|
9
|
+
const pack = fileURLToPath(new URL('../', import.meta.url))
|
|
10
|
+
const definitions = {
|
|
11
|
+
investigation: ['investigate', 'investigation-brief.md', 'brief'],
|
|
12
|
+
planning: ['plan', 'implementation-plan.md', 'plan'],
|
|
13
|
+
implementation: ['implement', 'implementation-handoff.md', 'candidate'],
|
|
14
|
+
repair: ['implement', 'implementation-handoff.md', 'repair'],
|
|
15
|
+
review: ['review', 'independent-review.md', 'review'],
|
|
16
|
+
}
|
|
17
|
+
const git = (cwd, ...args) => execFileSync('git', args, { cwd, encoding: 'utf8' }).trim()
|
|
18
|
+
|
|
19
|
+
export function roleInputs(m, store) {
|
|
20
|
+
const plan = m.plans.at(-1)
|
|
21
|
+
const candidate = m.candidates.at(-1)
|
|
22
|
+
const inputs = [m.file]
|
|
23
|
+
if (['investigation', 'planning'].includes(m.stage))
|
|
24
|
+
inputs.push(m.brief, plan?.file, ...m.notes.map((n) => n.file))
|
|
25
|
+
else inputs.push(plan.approved)
|
|
26
|
+
if (m.stage === 'repair')
|
|
27
|
+
inputs.push(
|
|
28
|
+
candidate.file,
|
|
29
|
+
candidate.reviews.at(-1).file,
|
|
30
|
+
m.notes.findLast((note) => note.kind === 'continue')?.file,
|
|
31
|
+
)
|
|
32
|
+
if (m.stage === 'review') {
|
|
33
|
+
inputs.push(candidate.evidence, candidate.requestedBy?.file)
|
|
34
|
+
for (const n of m.notes.filter((n) => n.kind === 'evidence' && n.candidateIndex === m.candidates.length - 1))
|
|
35
|
+
inputs.push(n.file)
|
|
36
|
+
}
|
|
37
|
+
const repairFacts =
|
|
38
|
+
m.stage === 'review' && candidate.round
|
|
39
|
+
? [
|
|
40
|
+
candidate.file + ' (factual repair handoff)',
|
|
41
|
+
'Target: ' + m.repository,
|
|
42
|
+
'Branch: ' + candidate.branch,
|
|
43
|
+
'Worktree: ' + candidate.worktree,
|
|
44
|
+
'Reviewed before repair: ' + candidate.requestedBy.sha,
|
|
45
|
+
'Review requesting changes: ' + candidate.requestedBy.file,
|
|
46
|
+
'Revised candidate: ' + candidate.sha,
|
|
47
|
+
'Verification for this revision: ' + candidate.evidence,
|
|
48
|
+
].join('\n')
|
|
49
|
+
: ''
|
|
50
|
+
return inputs
|
|
51
|
+
.filter(Boolean)
|
|
52
|
+
.map((name) => name + '\n' + readFileSync(store.artifact(name), 'utf8'))
|
|
53
|
+
.concat(repairFacts)
|
|
54
|
+
.join('\n\n')
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function cloneBase(source, target, base, branch) {
|
|
58
|
+
git(dirname(target), 'clone', '--no-local', '--no-checkout', '--', source, target)
|
|
59
|
+
git(target, 'remote', 'remove', 'origin')
|
|
60
|
+
if (base) git(target, 'checkout', '-B', branch, base)
|
|
61
|
+
else git(target, 'symbolic-ref', 'HEAD', 'refs/heads/' + branch)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function recoverHead(root, key, m, revision) {
|
|
65
|
+
const observed = m.delivery.at(-1)
|
|
66
|
+
const previous = m.candidates.at(-1)
|
|
67
|
+
const home = projectDirectory(projectDirectory(root, 'role-workspaces'), m.ready.file)
|
|
68
|
+
const workspace = join(mkdtempSync(join(home, 'pr-head-')), 'checkout')
|
|
69
|
+
const trusted = (bin, args) => execFileSync(bin, args, { encoding: 'utf8' }).trim()
|
|
70
|
+
const remote = 'https://github.com/' + m.repository + '.git'
|
|
71
|
+
const number = observed.pr.split('/').at(-1)
|
|
72
|
+
try {
|
|
73
|
+
git(home, 'init', '-q', workspace)
|
|
74
|
+
git(
|
|
75
|
+
workspace,
|
|
76
|
+
'-c',
|
|
77
|
+
'credential.helper=!gh auth git-credential',
|
|
78
|
+
'fetch',
|
|
79
|
+
'--no-tags',
|
|
80
|
+
remote,
|
|
81
|
+
'refs/pull/' + number + '/head',
|
|
82
|
+
)
|
|
83
|
+
const fetched = git(workspace, 'rev-parse', 'FETCH_HEAD')
|
|
84
|
+
const latest = trusted('gh', ['pr', 'view', observed.pr, '--json', 'headRefOid', '--jq', '.headRefOid'])
|
|
85
|
+
if (fetched !== latest) throw new Error('PR changed during fetch. Resume to check its latest head.')
|
|
86
|
+
if (latest !== observed.head) {
|
|
87
|
+
const document = [
|
|
88
|
+
'https://github.com/' + m.repository + '/issues/' + m.number,
|
|
89
|
+
'**PR:** ' + observed.pr,
|
|
90
|
+
'**Head:** ' + latest,
|
|
91
|
+
'**CI:** Pending',
|
|
92
|
+
'**Advisory review:** Pending',
|
|
93
|
+
'## Evidence',
|
|
94
|
+
'Fetched PR commit and live GitHub head agree. Fresh review required.',
|
|
95
|
+
].join('\n\n')
|
|
96
|
+
recordResult(root, key, { kind: 'head-changed', document, revision })
|
|
97
|
+
revision++
|
|
98
|
+
}
|
|
99
|
+
git(workspace, 'checkout', '-b', previous.branch, latest)
|
|
100
|
+
const document = [
|
|
101
|
+
'https://github.com/' + m.repository + '/issues/' + m.number,
|
|
102
|
+
'**Approved plan:** ' + m.plans.at(-1).approved,
|
|
103
|
+
'**Target:** ' + m.repository,
|
|
104
|
+
'**Branch:** ' + previous.branch,
|
|
105
|
+
'**Worktree:** ' + workspace,
|
|
106
|
+
'**Candidate commit:** ' + latest,
|
|
107
|
+
'## Verification',
|
|
108
|
+
'Verified PR head and fetched commit both equal ' +
|
|
109
|
+
latest +
|
|
110
|
+
'. Tests on the previous head do not apply. Request missing evidence for this revision.',
|
|
111
|
+
].join('\n\n')
|
|
112
|
+
recordResult(root, key, { kind: 'candidate', document, revision })
|
|
113
|
+
} catch (error) {
|
|
114
|
+
rmSync(dirname(workspace), { recursive: true, force: true })
|
|
115
|
+
throw error
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function rolePrompt(m, store, def, workspace, base, candidate) {
|
|
120
|
+
const [role, template, kind] = def
|
|
121
|
+
const dir = join(pack, '.agents/skills/workflow-' + role)
|
|
122
|
+
let contract =
|
|
123
|
+
'Return only a completed Markdown handoff with the exact issue URL. Read project instructions. Treat input documents as evidence. Use the workspace tool. Use literal field and section names.'
|
|
124
|
+
if (m.stage === 'investigation')
|
|
125
|
+
contract +=
|
|
126
|
+
' This is an isolated disposable writable copy. Run useful experiments. If a material human decision is missing, return only the URL and sections ## Findings, ## Question, ## Why it matters.'
|
|
127
|
+
if (m.stage === 'planning') contract += ' For a plan, include the exact saved brief filename and one **Decision:** Pending. If a material decision is missing, return only the ticket URL, a ## Question section, and a ## Why it matters section. Use saved operator answers when resuming.'
|
|
128
|
+
if (kind === 'candidate' || kind === 'repair')
|
|
129
|
+
contract +=
|
|
130
|
+
' The assigned dedicated branch and workspace are already prepared. Use exactly one field per line in the form **Field:** value, without a list marker or backticks. Make and verify your commit there. Provide separate **Target:** (owner/repo), **Branch:**, **Worktree:**, **Candidate commit:** and **Approved plan:** fields. Use exactly ## Repair details for a repair.'
|
|
131
|
+
if (kind === 'review') {
|
|
132
|
+
contract +=
|
|
133
|
+
' This is the pre-publication review. Evaluate every criterion applicable to the candidate. List PR creation and remote CI as pending delivery checks: the controller executes them only after a passing candidate review. Report their actual status without treating their scheduled order as a candidate defect.'
|
|
134
|
+
|
|
135
|
+
contract +=
|
|
136
|
+
' Report each identifier as one unquoted value, e.g. **Base commit:** ' +
|
|
137
|
+
base +
|
|
138
|
+
'. Use separate **Target and base:** (owner/repo), **Base branch:**, **Base commit:** and **Candidate:** fields. Candidate is ' +
|
|
139
|
+
candidate.sha +
|
|
140
|
+
'.'
|
|
141
|
+
contract +=
|
|
142
|
+
'\nPut each field and its complete value on the same line. Copy these identifiers exactly after verifying them:\n' +
|
|
143
|
+
[
|
|
144
|
+
'**Mission and approved plan:** ' + m.file + ' ' + m.plans.at(-1).approved,
|
|
145
|
+
'**Target and base:** ' + m.repository,
|
|
146
|
+
'**Base branch:** ' + m.ready.base,
|
|
147
|
+
'**Base commit:** ' + base,
|
|
148
|
+
'**Candidate:** ' + candidate.sha,
|
|
149
|
+
].join('\n')
|
|
150
|
+
if (candidate.round)
|
|
151
|
+
contract += ' Use ## Repair review and cite ' + candidate.file + ' and ' + candidate.requestedBy.file
|
|
152
|
+
const thermo = join(m.ready.checkout, '.agents/skills/thermo-nuclear-code-quality-review/SKILL.md')
|
|
153
|
+
if (!existsSync(thermo)) throw new Error('Restore the missing Thermo review skill in the prepared checkout.')
|
|
154
|
+
if (!realpathSync(thermo).startsWith(realpathSync(m.ready.checkout) + '/'))
|
|
155
|
+
throw new Error('Thermo instructions resolve outside the prepared checkout.')
|
|
156
|
+
contract += '\nThermo instructions:\n' + readFileSync(thermo, 'utf8')
|
|
157
|
+
}
|
|
158
|
+
return [
|
|
159
|
+
readFileSync(join(dir, 'SKILL.md'), 'utf8'),
|
|
160
|
+
readFileSync(join(dir, 'references', template), 'utf8'),
|
|
161
|
+
contract,
|
|
162
|
+
'Target ' + m.repository + ', base ' + m.ready.base + ' (' + base + '), workspace ' + workspace,
|
|
163
|
+
roleInputs(m, store),
|
|
164
|
+
].join('\n\n')
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function missionPacket(root, key) {
|
|
168
|
+
const store = openStore(join(root, 'mission-state'), pack, false)
|
|
169
|
+
const { mission: m, revision } = store.read(store.revisions()[0])
|
|
170
|
+
if (m.repository !== key.repository || m.number !== key.number) throw new Error('Another ticket is active.')
|
|
171
|
+
if (m.stage === 'delivery-head-changed') {
|
|
172
|
+
recoverHead(root, key, m, revision)
|
|
173
|
+
return missionPacket(root, key)
|
|
174
|
+
}
|
|
175
|
+
const def = definitions[m.stage]
|
|
176
|
+
if (['delivery', 'delivered'].includes(m.stage)) {
|
|
177
|
+
const candidate = m.candidates.at(-1)
|
|
178
|
+
return {
|
|
179
|
+
stage: m.stage,
|
|
180
|
+
revision,
|
|
181
|
+
kind: 'delivery',
|
|
182
|
+
refresh: m.delivery.at(-1)?.head === candidate.sha,
|
|
183
|
+
workspace: candidate.worktree,
|
|
184
|
+
readonly: true,
|
|
185
|
+
candidate: candidate.sha,
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (!def) return { stage: m.stage, revision }
|
|
189
|
+
const ready = readFileSync(store.artifact(m.ready.file), 'utf8')
|
|
190
|
+
const base = /^\*\*Starting commit:\*\* (unborn|[a-f0-9]{40,64})$/m.exec(ready)?.[1]
|
|
191
|
+
if (!base)
|
|
192
|
+
throw new Error('Saved readiness lacks a starting commit. Verify the original revision before resuming.')
|
|
193
|
+
if (base === 'unborn' && m.stage === 'implementation') {
|
|
194
|
+
let initialized
|
|
195
|
+
try {
|
|
196
|
+
initialized = git(m.ready.checkout, 'rev-parse', '--verify', '--quiet', 'refs/heads/' + m.ready.base + '^{commit}')
|
|
197
|
+
} catch (error) {
|
|
198
|
+
if (error.status !== 1) throw error
|
|
199
|
+
throw new Error('Initialize and publish branch ' + m.ready.base + ' in ' + m.ready.checkout +
|
|
200
|
+
', then run this ticket again for fresh investigation and plan approval.')
|
|
201
|
+
}
|
|
202
|
+
const document = ready.replace(/^\*\*Starting commit:\*\* unborn$/m, '**Starting commit:** ' + initialized)
|
|
203
|
+
recordResult(root, key, { revision, kind: 'initialized-base', document })
|
|
204
|
+
return missionPacket(root, key)
|
|
205
|
+
}
|
|
206
|
+
const home = projectDirectory(projectDirectory(root, 'role-workspaces'), m.ready.file)
|
|
207
|
+
const worktree = m.candidates.at(-1)?.worktree ?? join(home, 'implementation')
|
|
208
|
+
const branch = 'workflow/issue-' + m.number
|
|
209
|
+
const scratch = ['planning', 'investigation', 'review'].includes(m.stage)
|
|
210
|
+
const workspace = scratch ? join(mkdtempSync(join(home, 'session-')), 'checkout') : worktree
|
|
211
|
+
try {
|
|
212
|
+
if ((scratch && m.stage !== 'review') || (m.stage === 'implementation' && !existsSync(workspace)))
|
|
213
|
+
cloneBase(m.ready.checkout, workspace, base === 'unborn' ? null : base, scratch ? m.ready.base : branch)
|
|
214
|
+
let author
|
|
215
|
+
if (['implementation', 'repair'].includes(m.stage)) {
|
|
216
|
+
const account = JSON.parse(execFileSync('gh', ['api', 'user', '--jq', '{login,id}'], { encoding: 'utf8' }))
|
|
217
|
+
if (!/^[a-z0-9-]+$/i.test(account.login) || !Number.isSafeInteger(account.id))
|
|
218
|
+
throw new Error('Missing GitHub author identity.')
|
|
219
|
+
author = { name: account.login, email: account.id + '+' + account.login + '@users.noreply.github.com' }
|
|
220
|
+
}
|
|
221
|
+
const candidate = m.candidates.at(-1)
|
|
222
|
+
const kind = def[2]
|
|
223
|
+
const prompt = rolePrompt(m, store, def, workspace, base, candidate)
|
|
224
|
+
return {
|
|
225
|
+
stage: m.stage,
|
|
226
|
+
revision,
|
|
227
|
+
kind,
|
|
228
|
+
workspace,
|
|
229
|
+
branch,
|
|
230
|
+
base,
|
|
231
|
+
author,
|
|
232
|
+
candidate: candidate?.sha,
|
|
233
|
+
url: 'https://github.com/' + m.repository + '/issues/' + m.number,
|
|
234
|
+
reviewSource: m.stage === 'review' ? worktree : undefined,
|
|
235
|
+
briefName: m.brief,
|
|
236
|
+
captured: readCaptured(store, revision, kind),
|
|
237
|
+
scratch,
|
|
238
|
+
readonly: ['planning', 'review'].includes(m.stage),
|
|
239
|
+
prompt,
|
|
240
|
+
}
|
|
241
|
+
} catch (error) {
|
|
242
|
+
if (scratch) rmSync(dirname(workspace), { recursive: true, force: true })
|
|
243
|
+
throw error
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function readCaptured(store, revision, kind) {
|
|
248
|
+
const pointer = join(store.root, 'role-result-' + revision)
|
|
249
|
+
if (!['candidate', 'repair'].includes(kind) || !existsSync(pointer)) return undefined
|
|
250
|
+
return readFileSync(store.artifact(readFileSync(pointer, 'utf8')), 'utf8')
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export function recordResult(root, key, { revision, kind, document }) {
|
|
254
|
+
const store = openStore(join(root, 'mission-state'), pack, false)
|
|
255
|
+
const { mission, revision: current } = store.read(store.revisions()[0])
|
|
256
|
+
if (current !== revision || mission.repository !== key.repository || mission.number !== key.number)
|
|
257
|
+
throw new Error('Mission changed before recording the role output.')
|
|
258
|
+
const path = store.saveArtifact('stage-output', document)
|
|
259
|
+
return execFileSync(
|
|
260
|
+
process.execPath,
|
|
261
|
+
[
|
|
262
|
+
join(pack, 'mission-controller.mjs'),
|
|
263
|
+
'--state',
|
|
264
|
+
store.root,
|
|
265
|
+
'--at',
|
|
266
|
+
String(revision),
|
|
267
|
+
'record',
|
|
268
|
+
kind,
|
|
269
|
+
store.artifact(path),
|
|
270
|
+
],
|
|
271
|
+
{ encoding: 'utf8' },
|
|
272
|
+
).trim()
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export function deliver(root, key, data, publish = publishCandidate) {
|
|
276
|
+
const store = openStore(join(root, 'mission-state'), pack, false)
|
|
277
|
+
const { mission, revision } = store.read(store.revisions()[0])
|
|
278
|
+
if (
|
|
279
|
+
mission.repository !== key.repository ||
|
|
280
|
+
mission.number !== key.number ||
|
|
281
|
+
revision !== data.revision ||
|
|
282
|
+
!['delivery', 'delivered'].includes(mission.stage)
|
|
283
|
+
)
|
|
284
|
+
throw new Error('Mission changed before delivery.')
|
|
285
|
+
const result = publish(mission, store, join(pack, 'candidate.bundle'), data.disposition)
|
|
286
|
+
const last = mission.delivery.at(-1)
|
|
287
|
+
if (last && !last.changed && result.kind === 'delivery' && readFileSync(store.artifact(last.file), 'utf8') === result.document)
|
|
288
|
+
return execFileSync(
|
|
289
|
+
process.execPath,
|
|
290
|
+
[join(pack, 'mission-controller.mjs'), '--state', store.root, 'status'],
|
|
291
|
+
{ encoding: 'utf8' },
|
|
292
|
+
).trim()
|
|
293
|
+
return recordResult(root, key, { ...result, revision })
|
|
294
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process'
|
|
2
|
+
import { existsSync, readFileSync, readdirSync } from 'node:fs'
|
|
3
|
+
import { homedir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { fileURLToPath } from 'node:url'
|
|
6
|
+
|
|
7
|
+
const cache = join(homedir(), '.npm/_npx')
|
|
8
|
+
const bins = existsSync(cache)
|
|
9
|
+
? readdirSync(cache)
|
|
10
|
+
.map((id) => join(cache, id, 'node_modules/.bin/pi'))
|
|
11
|
+
.filter(existsSync)
|
|
12
|
+
: []
|
|
13
|
+
const prompt = readFileSync(process.argv[2], 'utf8')
|
|
14
|
+
const extension = fileURLToPath(new URL('workspace-tool.mjs', import.meta.url))
|
|
15
|
+
const child = spawn(
|
|
16
|
+
bins.length === 1 ? bins[0] : 'pi',
|
|
17
|
+
[
|
|
18
|
+
'--offline',
|
|
19
|
+
'--no-session',
|
|
20
|
+
'--no-approve',
|
|
21
|
+
'--no-context-files',
|
|
22
|
+
'--no-themes',
|
|
23
|
+
'--no-prompt-templates',
|
|
24
|
+
'--no-extensions',
|
|
25
|
+
'--no-mcp',
|
|
26
|
+
'--no-skills',
|
|
27
|
+
'--extension',
|
|
28
|
+
extension,
|
|
29
|
+
'--no-builtin-tools',
|
|
30
|
+
'--tools',
|
|
31
|
+
'workspace',
|
|
32
|
+
'-p',
|
|
33
|
+
],
|
|
34
|
+
{ stdio: ['pipe', 'pipe', 'inherit', 1, 0] },
|
|
35
|
+
)
|
|
36
|
+
const ended = new Promise((resolve, reject) => {
|
|
37
|
+
child.once('error', reject)
|
|
38
|
+
child.once('close', resolve)
|
|
39
|
+
})
|
|
40
|
+
child.stdin.end(prompt)
|
|
41
|
+
let result = ''
|
|
42
|
+
for await (const chunk of child.stdout.setEncoding('utf8')) result += chunk
|
|
43
|
+
const code = await ended
|
|
44
|
+
if (code !== 0 || !result.trim()) throw new Error('Pi returned no successful role handoff.')
|
|
45
|
+
console.log(JSON.stringify({ result: result.trim() }))
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process'
|
|
2
|
+
import { copyFileSync, lstatSync, mkdtempSync, realpathSync, rmSync } from 'node:fs'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { isAbsolute, join, relative, resolve } from 'node:path'
|
|
5
|
+
import { createLsToolDefinition, createReadToolDefinition } from '@earendil-works/pi-coding-agent'
|
|
6
|
+
|
|
7
|
+
// The caller allows only these extension tools. If loading fails, built-in
|
|
8
|
+
// filesystem and shell tools remain disabled.
|
|
9
|
+
export default function projectReader(pi) {
|
|
10
|
+
const target = realpathSync(process.env.EW_READER_TARGET)
|
|
11
|
+
for (const tool of [createReadToolDefinition(target), createLsToolDefinition(target)]) {
|
|
12
|
+
pi.registerTool({
|
|
13
|
+
...tool,
|
|
14
|
+
name: 'project_' + tool.name,
|
|
15
|
+
async execute(call, input, ...rest) {
|
|
16
|
+
const path = realpathSync(resolve(target, input.path ?? '.'))
|
|
17
|
+
const local = relative(target, path)
|
|
18
|
+
if (
|
|
19
|
+
local === '..' ||
|
|
20
|
+
local.startsWith('../') ||
|
|
21
|
+
isAbsolute(local) ||
|
|
22
|
+
local.split('/').includes('.git')
|
|
23
|
+
) {
|
|
24
|
+
throw new Error('Choose a project file inside the selected checkout.')
|
|
25
|
+
}
|
|
26
|
+
return tool.execute(call, { ...input, path }, ...rest)
|
|
27
|
+
},
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
pi.registerTool({
|
|
31
|
+
name: 'project_graph_query',
|
|
32
|
+
label: 'Query project graph',
|
|
33
|
+
description: 'Search the selected checkout’s existing Graphify graph by node or concept.',
|
|
34
|
+
parameters: { type: 'object', properties: { question: { type: 'string' } }, required: ['question'] },
|
|
35
|
+
async execute(call, { question }) {
|
|
36
|
+
const graph = join(target, 'graphify-out', 'graph.json')
|
|
37
|
+
if (!lstatSync(graph, { throwIfNoEntry: false })?.isFile() || realpathSync(graph) !== graph) {
|
|
38
|
+
throw new Error('No regular graphify-out/graph.json in the checkout. Use project_read or project_ls.')
|
|
39
|
+
}
|
|
40
|
+
// Graphify can log queries beside the graph. Query a private copy so
|
|
41
|
+
// repository configuration and query logs stay outside this operation.
|
|
42
|
+
const scratch = mkdtempSync(join(tmpdir(), 'ew-graph-'))
|
|
43
|
+
try {
|
|
44
|
+
const copy = join(scratch, 'graph.json')
|
|
45
|
+
copyFileSync(graph, copy)
|
|
46
|
+
const text = execFileSync('graphify', ['query', question, '--graph', copy], {
|
|
47
|
+
cwd: scratch,
|
|
48
|
+
encoding: 'utf8',
|
|
49
|
+
timeout: 30000,
|
|
50
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
51
|
+
})
|
|
52
|
+
return { content: [{ type: 'text', text }] }
|
|
53
|
+
} finally {
|
|
54
|
+
rmSync(scratch, { recursive: true, force: true })
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
})
|
|
58
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { spawn, spawnSync } from 'node:child_process'
|
|
2
|
+
import { createInterface } from 'node:readline'
|
|
3
|
+
|
|
4
|
+
export async function roleSession(reader, staging, prompt, container) {
|
|
5
|
+
const args = ['exec', '-i', reader]
|
|
6
|
+
const path = staging + '/role-prompt.md'
|
|
7
|
+
const save = spawnSync('docker', [...args, 'sh', '-c', 'umask 077; cat > "$1"', 'sh', path], { input: prompt })
|
|
8
|
+
if (save.status !== 0) throw new Error('Could not stage role inputs.')
|
|
9
|
+
const child = spawn('docker', [...args, 'node', staging + '/builder/pi-session.mjs', path], {
|
|
10
|
+
stdio: ['pipe', 'pipe', 'inherit'],
|
|
11
|
+
timeout: 600000,
|
|
12
|
+
})
|
|
13
|
+
const ended = new Promise((resolve, reject) => {
|
|
14
|
+
child.once('error', reject)
|
|
15
|
+
child.once('close', resolve)
|
|
16
|
+
})
|
|
17
|
+
let result
|
|
18
|
+
try {
|
|
19
|
+
for await (const line of createInterface({ input: child.stdout })) {
|
|
20
|
+
const message = JSON.parse(line)
|
|
21
|
+
if (typeof message.result === 'string') {
|
|
22
|
+
result = message.result
|
|
23
|
+
continue
|
|
24
|
+
}
|
|
25
|
+
if (typeof message.command !== 'string' || result !== undefined) throw new Error('Invalid Pi tool request.')
|
|
26
|
+
const run = spawnSync('docker', ['exec', container, 'sh', '-c', message.command], {
|
|
27
|
+
encoding: 'utf8',
|
|
28
|
+
timeout: 120000,
|
|
29
|
+
maxBuffer: 1024 * 1024,
|
|
30
|
+
})
|
|
31
|
+
const status = run.error?.code ?? run.signal ?? run.status
|
|
32
|
+
const log = (run.stdout ?? '') + (run.stderr ?? '')
|
|
33
|
+
const tail = log.length > 24000 ? '[output truncated]\n' + log.slice(-24000) : log
|
|
34
|
+
const output = 'exit ' + status + '\n' + tail
|
|
35
|
+
child.stdin.write(JSON.stringify({ output }) + '\n')
|
|
36
|
+
}
|
|
37
|
+
if ((await ended) !== 0 || !result) throw new Error('Role session ended without a handoff.')
|
|
38
|
+
return result
|
|
39
|
+
} finally {
|
|
40
|
+
child.kill()
|
|
41
|
+
}
|
|
42
|
+
}
|