-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
301 lines (251 loc) · 9.58 KB
/
index.ts
File metadata and controls
301 lines (251 loc) · 9.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import type { Plugin } from "@opencode-ai/plugin"
import { tool } from "@opencode-ai/plugin"
const MODIFYING_TOOLS = new Set([
'write', 'edit',
'lsp_rename', 'lsp_code_action_resolve',
'ast_grep_replace'
])
// Session state: track if gate was opened (edits were allowed) this session
const sessionState = new Map<string, { gateOpened: boolean }>()
async function getCurrentDescription($: any): Promise<string> {
try {
return (await $`jj log -r @ --no-graph -T description`.text()).trim()
} catch {
return ''
}
}
async function isJJRepo($: any): Promise<boolean> {
try {
await $`jj root`.text()
return true
} catch {
return false
}
}
async function isSubagent(client: any, sessionID: string): Promise<boolean> {
try {
const session = await client.session.get({ path: { id: sessionID } })
return !!session.data?.parentID
} catch {
return false
}
}
async function getChangeInfo($: any, rev: string = '@'): Promise<{ id: string; description: string; stats: string }> {
try {
const id = (await $`jj log -r ${rev} --no-graph -T 'change_id.shortest(8)'`.text()).trim()
const description = (await $`jj log -r ${rev} --no-graph -T description`.text()).trim()
const stats = (await $`jj diff -r ${rev} --stat`.text()).trim()
return { id, description, stats }
} catch {
return { id: '', description: '', stats: '' }
}
}
async function getCommitStack($: any, bookmark: string, targetRev: string): Promise<string[]> {
try {
const output = (await $`jj log -r '${bookmark}@origin..${targetRev}' --no-graph -T 'change_id.shortest(8) ++ " " ++ description.first_line() ++ "\n"'`.text()).trim()
return output ? output.split('\n').filter(Boolean) : []
} catch {
return []
}
}
interface PushTarget {
revision: string
info: { id: string; description: string; stats: string }
stack: string[]
needsConfirmation: boolean
}
async function findPushTarget($: any, bookmark: string): Promise<PushTarget | null> {
const atInfo = await getChangeInfo($, '@')
if (atInfo.description && atInfo.stats) {
const stack = await getCommitStack($, bookmark, '@')
return { revision: '@', info: atInfo, stack, needsConfirmation: false }
}
const parentInfo = await getChangeInfo($, '@-')
if (parentInfo.description && parentInfo.stats) {
const stack = await getCommitStack($, bookmark, '@-')
return { revision: '@-', info: parentInfo, stack, needsConfirmation: false }
}
try {
const unpushedOutput = (await $`jj log -r '${bookmark}@origin..@' --no-graph -T 'change_id.shortest(8) ++ "|" ++ description.first_line() ++ "\n"'`.text()).trim()
const unpushedLines = unpushedOutput ? unpushedOutput.split('\n').filter(Boolean) : []
const nonEmptyCommits = []
for (const line of unpushedLines) {
const [changeId] = line.split('|')
const info = await getChangeInfo($, changeId)
if (info.stats) {
nonEmptyCommits.push({ changeId, info })
}
}
if (nonEmptyCommits.length > 0) {
const tipCommit = nonEmptyCommits[0]
const stack = await getCommitStack($, bookmark, tipCommit.changeId)
return {
revision: tipCommit.changeId,
info: tipCommit.info,
stack,
needsConfirmation: true
}
}
} catch {}
return null
}
async function isImmutable($: any, revset: string = '@'): Promise<boolean> {
try {
const result = (await $`jj log -r '${revset}' --no-graph -T 'if(immutable, "true", "false")'`.text()).trim()
return result === 'true'
} catch {
return false
}
}
const JJ_ERROR_PATTERNS: Array<{ pattern: RegExp; message: (match: RegExpMatchArray) => string }> = [
{
pattern: /Commit (\S+) is immutable/i,
message: (m) => `Cannot modify commit ${m[1]} - it's immutable (already pushed).
Recovery options:
• Continue with new work: jj describe -m "next task"
• Start fresh from main: jj new main@origin -m "description"
• Undo recent operation: jj undo`
},
{
pattern: /working copy is stale/i,
message: () => `Working copy is stale.
Recovery: jj workspace update-stale`
},
]
function parseJjError(stderr: string): string | null {
for (const { pattern, message } of JJ_ERROR_PATTERNS) {
const match = stderr.match(pattern)
if (match) return message(match)
}
return null
}
const plugin: Plugin = async ({ $, client }) => ({
name: 'jj-opencode',
tool: {
jj_push: tool({
description: `Push current JJ change to a bookmark (default: main).
Auto-detects push target: checks @ first, then @- if @ is empty (common after session idle).
Only specify 'bookmark' if user explicitly requested a specific branch.`,
args: {
bookmark: tool.schema.string().optional().describe(
"Target bookmark/branch. ONLY set if user explicitly specified. Defaults to 'main'."
),
confirmed: tool.schema.boolean().optional().describe(
"Set to true after user confirms the push preview."
),
},
async execute(args) {
if (!await isJJRepo($)) {
return "Not a JJ repository."
}
const bookmark = args.bookmark || 'main'
const target = await findPushTarget($, bookmark)
if (!target) {
return `Nothing to push. No unpushed changes between \`${bookmark}@origin\` and \`@\`.`
}
if (await isImmutable($, target.revision)) {
return `Target commit is already immutable (pushed). Start new work: jj describe -m "description"`
}
const { revision, info, stack, needsConfirmation } = target
const totalCommits = stack.length
if (!args.confirmed) {
let preview = `## Push Preview\n\n`
preview += `**Target:** \`${revision}\` ${revision !== '@' ? '(@ is empty)' : ''}\n\n`
preview += `**${totalCommits} commit(s) will be pushed to \`${bookmark}\`:**\n\n`
preview += `\`\`\`\n`
for (const commit of stack) {
preview += `${commit}\n`
}
preview += `\`\`\`\n\n`
preview += `**Files in tip commit:**\n\`\`\`\n${info.stats}\n\`\`\`\n\n`
if (needsConfirmation) {
preview += `⚠️ **Note:** Had to search beyond \`@-\` to find changes. Please verify this is correct.\n\n`
}
preview += `After pushing, these commits become **immutable**.\n\n`
preview += `Call with \`confirmed: true\` to push.`
return preview
}
try {
if (revision === '@') {
await $`jj new`.text()
await $`jj bookmark set ${bookmark} -r @-`.text()
} else {
await $`jj bookmark set ${bookmark} -r ${revision}`.text()
}
await $`jj git push -b ${bookmark}`.text()
const postPushDesc = (await $`jj log -r @ --no-graph -T description`.text()).trim()
const isClean = !postPushDesc
let result = `Pushed ${totalCommits} commit(s) to \`${bookmark}\`:\n\n`
for (const commit of stack) {
result += `• ${commit}\n`
}
result += `\nThese commits are now **immutable**.`
if (isClean) {
result += ` Working copy is clean and ready.`
} else {
result += `\n\n⚠️ Working copy has description set. Run \`jj new\` if starting fresh work.`
}
return result
} catch (error: any) {
const recoveryMessage = parseJjError(error.message)
if (recoveryMessage) {
return recoveryMessage
}
return `Push failed: ${error.message}`
}
},
}),
},
"tool.execute.before": async ({ tool: toolName, sessionID }) => {
if (!MODIFYING_TOOLS.has(toolName)) return
if (!await isJJRepo($)) return
const description = await getCurrentDescription($)
if (description.length > 0) {
const state = sessionState.get(sessionID) || { gateOpened: false }
state.gateOpened = true
sessionState.set(sessionID, state)
return
}
const subagent = await isSubagent(client, sessionID)
if (subagent) {
throw new Error(
`BLOCKED: JJ gate is closed.\n\n` +
`You are a subagent. Return to the parent agent with this message:\n\n` +
`"Cannot edit files - no JJ change description set. ` +
`Parent must run: jj describe -m \\"description\\" before delegating file edits."`
)
}
throw new Error(
`Describe your intent before editing:\n\n` +
` jj describe -m "what you're about to do"\n\n` +
`When done, run \`jj new\` to commit and start fresh.`
)
},
event: async ({ event }) => {
const props = event.properties as Record<string, unknown> | undefined
if (event.type === "session.deleted") {
const sessionID = props?.sessionID as string | undefined
if (sessionID) sessionState.delete(sessionID)
return
}
if (event.type === "session.idle") {
const sessionID = props?.sessionID as string | undefined
if (!sessionID) return
if (await isSubagent(client, sessionID)) return
const state = sessionState.get(sessionID)
if (!state?.gateOpened) return
if (!await isJJRepo($)) return
const stats = (await $`jj diff --stat`.text()).trim()
if (!stats || stats.includes("0 files changed")) return
const description = await getCurrentDescription($)
if (!description) return
try {
await $`jj new`.quiet()
sessionState.set(sessionID, { gateOpened: false })
} catch {
// Silent fail - user will see uncommitted work next session
}
}
},
})
export default plugin