@iiiokojiadbi/shift 0.33.0 → 0.34.1
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/README.md +4 -0
- package/core/about.ts +4 -4
- package/core/delivery.ts +105 -26
- package/core/docs.ts +99 -43
- package/core/flows.ts +67 -24
- package/core/glossary.ts +4 -0
- package/core/hint.ts +62 -7
- package/core/history.ts +19 -10
- package/core/layout.ts +5 -1
- package/core/loop.ts +91 -18
- package/core/markdown.ts +60 -7
- package/core/records.ts +13 -8
- package/core/settings.ts +46 -15
- package/core/setup.ts +37 -19
- package/core/store.ts +13 -10
- package/core/tasks.ts +27 -4
- package/core/texts/doctrine.ts +43 -35
- package/core/texts/prompts.ts +18 -4
- package/core/texts/rows.ts +2 -1
- package/core/texts/tools.ts +5 -4
- package/core/types.ts +2 -1
- package/index.ts +111 -43
- package/package.json +12 -2
- package/server.ts +161 -53
- package/store.ts +59 -9
package/server.ts
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
import { resolve } from 'node:path'
|
|
2
2
|
import type { Plugin } from '@opencode/plugin'
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
Courier,
|
|
5
|
+
Gauge,
|
|
6
|
+
GaugeKeeper,
|
|
7
|
+
LetGoHarness,
|
|
8
|
+
Level,
|
|
9
|
+
Looks,
|
|
10
|
+
Loop,
|
|
11
|
+
LoopState,
|
|
12
|
+
Note,
|
|
13
|
+
PendingShift,
|
|
14
|
+
Project,
|
|
15
|
+
WaitNote,
|
|
16
|
+
} from './core/index'
|
|
4
17
|
import {
|
|
5
18
|
addGrowth,
|
|
6
19
|
afterTool,
|
|
@@ -8,8 +21,10 @@ import {
|
|
|
8
21
|
afterWrite,
|
|
9
22
|
atPrompt,
|
|
10
23
|
beforeTool,
|
|
24
|
+
compactInstructions,
|
|
11
25
|
contentChars,
|
|
12
26
|
countGrowth,
|
|
27
|
+
dropWaiting,
|
|
13
28
|
FRESH_GAUGE,
|
|
14
29
|
FRESH_LOOKS,
|
|
15
30
|
FRESH_LOOP,
|
|
@@ -20,16 +35,18 @@ import {
|
|
|
20
35
|
RESUME_DESCRIPTION,
|
|
21
36
|
renderSessionFiles,
|
|
22
37
|
resumeShift,
|
|
38
|
+
runEnded,
|
|
23
39
|
SHIFT_COMMAND,
|
|
24
|
-
SHIFT_NOW,
|
|
25
40
|
SHIFT_NOW_DESCRIPTION,
|
|
26
41
|
SOURCES,
|
|
27
42
|
sessionDoctrine,
|
|
28
43
|
sessionFiles,
|
|
29
44
|
shiftBase,
|
|
45
|
+
shiftNow,
|
|
30
46
|
startSession,
|
|
31
47
|
TASKS_COMMAND,
|
|
32
48
|
TASKS_DESCRIPTION,
|
|
49
|
+
TOOL,
|
|
33
50
|
TOOLS,
|
|
34
51
|
tasksView,
|
|
35
52
|
unassistedInstructions,
|
|
@@ -48,11 +65,15 @@ const KEPT_TOKENS = 15_000
|
|
|
48
65
|
const CHARS_PER_TOKEN = 4
|
|
49
66
|
// The rows the owner sees above what the mod sends the agent.
|
|
50
67
|
const CONTINUED = `${MOD}: the letter and the records`
|
|
51
|
-
const NOTES = `${MOD}: notes to the agent`
|
|
52
68
|
|
|
53
69
|
// What the plugin keeps of one session; the plugin is one per project, shared by its sessions.
|
|
54
70
|
type Session = {
|
|
71
|
+
// The project's settings turn the mod on for it, read as the session first comes; the rows that start it, told once.
|
|
72
|
+
enabled: boolean
|
|
73
|
+
rows: { text: string; level: Level }[]
|
|
55
74
|
loop: LoopState
|
|
75
|
+
// The loop's last update, which the next one waits for.
|
|
76
|
+
updating: Promise<void>
|
|
56
77
|
gauge: Gauge
|
|
57
78
|
looks: Looks
|
|
58
79
|
// The last step's prompt size: the history as the model read it.
|
|
@@ -62,6 +83,10 @@ type Session = {
|
|
|
62
83
|
// The Hold of the shift being let go, and what its let-go ended in.
|
|
63
84
|
hold: string | undefined
|
|
64
85
|
letGo: ((end: { text: string } | { error: string }) => void) | undefined
|
|
86
|
+
// The inbox item of the let-go the mod asked for: its cancel ends the wait for it.
|
|
87
|
+
letGoID: string | undefined
|
|
88
|
+
// Runs started in the session: a tool that ends after its run did knows its turn is over.
|
|
89
|
+
runs: number
|
|
65
90
|
// The doctrine and the session's files as the session started or last let go: a block read anew every
|
|
66
91
|
// request would show records the history has not reached yet and break the prompt's cache on every write.
|
|
67
92
|
block: { doctrine: string; files: string | null } | undefined
|
|
@@ -75,6 +100,11 @@ const plugin: Definition = {
|
|
|
75
100
|
const root = ctx.location.directory
|
|
76
101
|
const start = await startSession(await projectAt(root))
|
|
77
102
|
if (!start.enabled) return
|
|
103
|
+
// The docs' tocs and map are refreshed once, as the plugin starts: later the notes settle what changed, each with
|
|
104
|
+
// its note.
|
|
105
|
+
await sessionFiles(await projectAt(root)).catch(() => [])
|
|
106
|
+
// The rows of the plugin's start — `.shift/` just kept out of git — go to the first session that comes.
|
|
107
|
+
const startRows = [...start.rows]
|
|
78
108
|
|
|
79
109
|
const sessions = new Map<string, Session>()
|
|
80
110
|
const parents = new Map<string, string | null>()
|
|
@@ -83,18 +113,26 @@ const plugin: Definition = {
|
|
|
83
113
|
let stopped = false
|
|
84
114
|
|
|
85
115
|
// A session's state, its waiting letter read back from the plugin's storage the first time: it outlives a restart.
|
|
86
|
-
async function state(sessionID: string): Promise<Session> {
|
|
116
|
+
async function state(sessionID: string, project: () => Promise<Project> = once()): Promise<Session> {
|
|
87
117
|
const known = sessions.get(sessionID)
|
|
88
118
|
if (known !== undefined) return known
|
|
89
119
|
const note = ((await ctx.storage.get(waitKey(sessionID))) ?? null) as WaitNote | null
|
|
120
|
+
// A shift accepted before the plugin reloaded lets go at the next run's end.
|
|
121
|
+
const pending = ((await ctx.storage.get(pendingKey(sessionID))) ?? null) as PendingShift | null
|
|
122
|
+
const begun = await startSession(await project())
|
|
90
123
|
const fresh: Session = {
|
|
91
|
-
|
|
124
|
+
enabled: begun.enabled,
|
|
125
|
+
rows: [...startRows.splice(0), ...begun.rows],
|
|
126
|
+
loop: { ...FRESH_LOOP, note, pending },
|
|
127
|
+
updating: Promise.resolve(),
|
|
92
128
|
gauge: FRESH_GAUGE,
|
|
93
129
|
looks: FRESH_LOOKS,
|
|
94
130
|
fill: undefined,
|
|
95
131
|
busy: false,
|
|
96
132
|
hold: undefined,
|
|
97
133
|
letGo: undefined,
|
|
134
|
+
letGoID: undefined,
|
|
135
|
+
runs: 0,
|
|
98
136
|
block: undefined,
|
|
99
137
|
}
|
|
100
138
|
sessions.set(sessionID, sessions.get(sessionID) ?? fresh)
|
|
@@ -107,6 +145,11 @@ const plugin: Definition = {
|
|
|
107
145
|
return (await parentOf(sessionID)) !== null
|
|
108
146
|
}
|
|
109
147
|
|
|
148
|
+
// A session the mod does not serve: a subagent's, or one the settings turned the mod off for.
|
|
149
|
+
async function apart(sessionID: string, project?: () => Promise<Project>): Promise<boolean> {
|
|
150
|
+
return (await child(sessionID)) || !(await state(sessionID, project)).enabled
|
|
151
|
+
}
|
|
152
|
+
|
|
110
153
|
async function parentOf(sessionID: string): Promise<string | null> {
|
|
111
154
|
const known = parents.get(sessionID)
|
|
112
155
|
if (known !== undefined) return known
|
|
@@ -126,30 +169,56 @@ const plugin: Definition = {
|
|
|
126
169
|
|
|
127
170
|
// A row for the owner: a synthetic message the owner sees by its description; the agent reads it with the next one.
|
|
128
171
|
async function tell(sessionID: string, row: string, _level: Level): Promise<void> {
|
|
129
|
-
|
|
172
|
+
// Queued: a running turn does not take it as a message to answer.
|
|
173
|
+
await ctx.session.synthetic({ sessionID, text: row, description: row, resume: false, delivery: 'queue' }).catch(() => {})
|
|
130
174
|
}
|
|
131
175
|
|
|
132
176
|
function loopOf(sessionID: string): Loop {
|
|
133
177
|
return {
|
|
134
178
|
read: async () => (await state(sessionID)).loop,
|
|
179
|
+
// One update after another: a change made while the storage still writes the one before would build on a state
|
|
180
|
+
// that may yet be taken back.
|
|
135
181
|
update: async change => {
|
|
136
182
|
const session = await state(sessionID)
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
183
|
+
const turn = session.updating.then(async () => {
|
|
184
|
+
const before = session.loop
|
|
185
|
+
const after = change(before)
|
|
186
|
+
session.loop = after
|
|
187
|
+
try {
|
|
188
|
+
if (after.pending !== before.pending) await keep(pendingKey(sessionID), after.pending)
|
|
189
|
+
if (after.note !== before.note) await keep(waitKey(sessionID), after.note)
|
|
190
|
+
} catch (error) {
|
|
191
|
+
// Not kept, not changed: a shift the storage did not take is not let go either.
|
|
192
|
+
session.loop = before
|
|
193
|
+
throw error
|
|
194
|
+
}
|
|
195
|
+
})
|
|
196
|
+
session.updating = turn.catch(() => {})
|
|
197
|
+
await turn
|
|
143
198
|
},
|
|
144
199
|
tell: (row, level) => void tell(sessionID, row, level),
|
|
145
200
|
}
|
|
146
201
|
}
|
|
147
202
|
|
|
148
|
-
function
|
|
203
|
+
async function keep(key: string, value: unknown): Promise<void> {
|
|
204
|
+
if (value === null) await ctx.storage.remove(key)
|
|
205
|
+
else await ctx.storage.set(key, value as never)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// A project loaded once for the hook at hand, and only when something asks for it.
|
|
209
|
+
function once(): () => Promise<Project> {
|
|
210
|
+
let loading: Promise<Project> | undefined
|
|
211
|
+
return () => {
|
|
212
|
+
loading ??= projectAt(root)
|
|
213
|
+
return loading
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function courierOf(sessionID: string, project: () => Promise<Project> = once()): Courier {
|
|
149
218
|
return {
|
|
150
219
|
loop: loopOf(sessionID),
|
|
151
|
-
gauge: keeperOf(sessionID),
|
|
152
|
-
project
|
|
220
|
+
gauge: keeperOf(sessionID, project),
|
|
221
|
+
project,
|
|
153
222
|
looks: async () => (await state(sessionID)).looks,
|
|
154
223
|
updateLooks: async change => {
|
|
155
224
|
const session = await state(sessionID)
|
|
@@ -159,7 +228,7 @@ const plugin: Definition = {
|
|
|
159
228
|
}
|
|
160
229
|
|
|
161
230
|
// The gauge in the plugin's memory; the history and the base read from the session.
|
|
162
|
-
function keeperOf(sessionID: string): GaugeKeeper {
|
|
231
|
+
function keeperOf(sessionID: string, project: () => Promise<Project> = once()): GaugeKeeper {
|
|
163
232
|
return {
|
|
164
233
|
read: async () => (await state(sessionID)).gauge,
|
|
165
234
|
update: async change => {
|
|
@@ -167,24 +236,30 @@ const plugin: Definition = {
|
|
|
167
236
|
session.gauge = change(session.gauge)
|
|
168
237
|
},
|
|
169
238
|
history: async () => growthIn(await ctx.session.context({ sessionID })),
|
|
170
|
-
base: async () => shiftBase(await
|
|
239
|
+
base: async () => shiftBase(await project(), sessionID),
|
|
171
240
|
reading: async () => ({ tokens: (await state(sessionID)).fill, ...(await limits(sessionID)) }),
|
|
172
241
|
}
|
|
173
242
|
}
|
|
174
243
|
|
|
175
|
-
// The model's window and where opencode lets the history go by itself: the window less max(15%, 16K), its default
|
|
244
|
+
// The model's window and where opencode lets the history go by itself: the window less max(15%, 16K), its default,
|
|
245
|
+
// or the `buffer` given unless it leaves nothing. Kept per model: a session that changes its model is read anew.
|
|
176
246
|
async function limits(sessionID: string): Promise<{ window: number; threshold: number | undefined }> {
|
|
177
|
-
const known = windows.get(sessionID)
|
|
178
|
-
if (known !== undefined) return known
|
|
179
247
|
const info = await ctx.session.get({ sessionID }).catch(() => undefined)
|
|
180
|
-
|
|
181
|
-
const
|
|
248
|
+
// A session that names no model runs on the configured default.
|
|
249
|
+
const fallback = info?.model === undefined ? (await ctx.model.default().catch(() => undefined))?.data : undefined
|
|
250
|
+
const ref = info?.model ?? (fallback == null ? undefined : { id: fallback.id, providerID: fallback.providerID })
|
|
251
|
+
if (ref === undefined) return { window: 0, threshold: undefined }
|
|
252
|
+
const key = `${ref.providerID}/${ref.id}`
|
|
253
|
+
const known = windows.get(key)
|
|
254
|
+
if (known !== undefined) return known
|
|
255
|
+
const models = (await ctx.model.list().catch(() => undefined))?.data ?? []
|
|
256
|
+
const model = models.find(each => each.id === ref.id && each.providerID === ref.providerID)
|
|
182
257
|
const window = model?.limit.input || model?.limit.context || 0
|
|
183
258
|
if (window === 0) return { window: 0, threshold: undefined }
|
|
184
|
-
const buffer
|
|
185
|
-
|
|
259
|
+
const given = typeof ctx.options.buffer === 'number' && ctx.options.buffer < window ? ctx.options.buffer : undefined
|
|
260
|
+
const buffer = given ?? Math.max(Math.floor(window * 0.15), window >= 32_000 ? 16_000 : 0)
|
|
186
261
|
const found = { window, threshold: window - buffer }
|
|
187
|
-
windows.set(
|
|
262
|
+
windows.set(key, found)
|
|
188
263
|
|
|
189
264
|
return found
|
|
190
265
|
}
|
|
@@ -201,7 +276,7 @@ const plugin: Definition = {
|
|
|
201
276
|
})
|
|
202
277
|
session.hold = instructions
|
|
203
278
|
try {
|
|
204
|
-
await ctx.session.compact({ sessionID, delivery: 'queue' })
|
|
279
|
+
session.letGoID = (await ctx.session.compact({ sessionID, delivery: 'queue' })).id
|
|
205
280
|
const end = await ended
|
|
206
281
|
if ('error' in end) return { skip: end.error }
|
|
207
282
|
await ctx.session.wait({ sessionID })
|
|
@@ -212,6 +287,7 @@ const plugin: Definition = {
|
|
|
212
287
|
} finally {
|
|
213
288
|
session.hold = undefined
|
|
214
289
|
session.letGo = undefined
|
|
290
|
+
session.letGoID = undefined
|
|
215
291
|
}
|
|
216
292
|
},
|
|
217
293
|
continueWith: async prompt => {
|
|
@@ -222,11 +298,12 @@ const plugin: Definition = {
|
|
|
222
298
|
|
|
223
299
|
// The doctrine and the session's files, read at the session's first request and again after each let-go.
|
|
224
300
|
await ctx.session.hook('context', async event => {
|
|
225
|
-
if (await
|
|
301
|
+
if (await apart(event.sessionID)) return
|
|
226
302
|
const session = await state(event.sessionID)
|
|
303
|
+
for (const row of session.rows.splice(0)) await tell(event.sessionID, row.text, row.level)
|
|
227
304
|
if (session.block === undefined) {
|
|
228
305
|
const project = await projectAt(root)
|
|
229
|
-
const files = renderSessionFiles(await sessionFiles(project))
|
|
306
|
+
const files = renderSessionFiles(await sessionFiles(project, { refresh: false }))
|
|
230
307
|
session.block = { doctrine: sessionDoctrine(project.settings).doctrine, files }
|
|
231
308
|
}
|
|
232
309
|
event.system.push({ type: 'text', text: session.block.doctrine })
|
|
@@ -235,28 +312,36 @@ const plugin: Definition = {
|
|
|
235
312
|
|
|
236
313
|
// A compaction the agent asked for holds what its letter says; any other gets the summarizer's instructions.
|
|
237
314
|
await ctx.session.hook('compaction', async event => {
|
|
238
|
-
|
|
315
|
+
if (await apart(event.sessionID)) return
|
|
316
|
+
const session = sessions.get(event.sessionID)
|
|
317
|
+
// One opencode makes by itself while a shift is pending holds that shift's Hold too.
|
|
318
|
+
const pending = session?.loop.pending ?? null
|
|
319
|
+
const hold = session?.hold ?? (pending === null ? undefined : compactInstructions(pending))
|
|
239
320
|
const text = hold ?? unassistedInstructions((await projectAt(root)).settings.files)
|
|
240
321
|
event.system.push({ type: 'text', text })
|
|
241
322
|
})
|
|
242
323
|
|
|
243
324
|
// The owner's message: what changed between turns, a hint, and after a wait the letter go with it.
|
|
325
|
+
// The notes go in the owner's message itself: opencode's inbox takes a message of its own separately, before or after
|
|
326
|
+
// the owner's, whether it steers or waits in the queue.
|
|
244
327
|
await ctx.session.hook('prompt', async event => {
|
|
245
|
-
|
|
328
|
+
const project = once()
|
|
329
|
+
if (await apart(event.sessionID, project)) return
|
|
246
330
|
const { sessionID } = event
|
|
247
|
-
|
|
248
|
-
|
|
331
|
+
const courier = courierOf(sessionID, project)
|
|
332
|
+
await countGrowth(courier.gauge, SOURCES.owner, event.prompt.text.length)
|
|
333
|
+
const told = await atPrompt(courier, { runStarts: !(await state(sessionID)).busy, owner: true })
|
|
249
334
|
if (told.notes.length === 0) return
|
|
250
335
|
const text = told.notes.join('\n\n')
|
|
251
|
-
|
|
252
|
-
await countGrowth(
|
|
336
|
+
event.prompt.text = `${event.prompt.text}\n\n${text}`
|
|
337
|
+
await countGrowth(courier.gauge, SOURCES.attachments, text.length)
|
|
253
338
|
await told.delivered()
|
|
254
339
|
})
|
|
255
340
|
|
|
256
341
|
// Every note to the agent mid-run goes with a tool's result: what changed in the records around a tool of the agent,
|
|
257
342
|
// the docs after a write, a hint. A subagent's write gets only the docs.
|
|
258
343
|
await ctx.tool.hook('execute.before', async event => {
|
|
259
|
-
if (await
|
|
344
|
+
if (await apart(event.sessionID)) return
|
|
260
345
|
outside.set(event.id, await beforeTool(courierOf(event.sessionID)))
|
|
261
346
|
})
|
|
262
347
|
|
|
@@ -265,15 +350,18 @@ const plugin: Definition = {
|
|
|
265
350
|
const done = event.status === 'completed' ? event.result : null
|
|
266
351
|
const path = done !== null && WRITING.has(event.tool) ? (event.input as { path?: unknown }).path : undefined
|
|
267
352
|
const written = typeof path === 'string' ? resolve(root, path) : undefined
|
|
268
|
-
if (done !== null) await countGrowth(keeperOf(sessionID), event.tool, contentChars(done.content))
|
|
269
353
|
if (await child(sessionID)) {
|
|
270
|
-
if (written !== undefined
|
|
354
|
+
if (written !== undefined && (await state(await mainOf(sessionID))).enabled)
|
|
355
|
+
await afterWrite(courierOf(await mainOf(sessionID)), written)
|
|
271
356
|
return
|
|
272
357
|
}
|
|
358
|
+
if (!(await state(sessionID)).enabled) return
|
|
359
|
+
const courier = courierOf(sessionID)
|
|
360
|
+
if (done !== null) await countGrowth(courier.gauge, event.tool, contentChars(done.content))
|
|
273
361
|
const before = outside.get(event.id) ?? []
|
|
274
362
|
outside.delete(event.id)
|
|
275
363
|
const end = done === null ? null : { own: OURS.has(event.tool), readOnly: READING.has(event.tool), written }
|
|
276
|
-
const added = await afterTool(
|
|
364
|
+
const added = await afterTool(courier, before, end)
|
|
277
365
|
if (done === null || added.length === 0) return
|
|
278
366
|
const blocks = added.map(text => ({ type: 'text' as const, text }))
|
|
279
367
|
const content = typeof done.content === 'string' ? [{ type: 'text' as const, text: done.content }] : (done.content ?? [])
|
|
@@ -289,6 +377,11 @@ const plugin: Definition = {
|
|
|
289
377
|
// Called by its name, not through the code-mode tool.
|
|
290
378
|
options: { codemode: false },
|
|
291
379
|
execute: async (input, call) => {
|
|
380
|
+
const session = await state(call.sessionID)
|
|
381
|
+
if (!session.enabled) throw new Error(`${MOD} is off in this project: its settings say enabled = false.`)
|
|
382
|
+
// A tool runs only in a run: one begun before the plugin reloaded has no start the plugin heard.
|
|
383
|
+
session.busy = true
|
|
384
|
+
const run = session.runs
|
|
292
385
|
const done = await tool.run(input as Record<string, unknown>, {
|
|
293
386
|
project: () => projectAt(root),
|
|
294
387
|
sessionId: async () => call.sessionID,
|
|
@@ -301,6 +394,10 @@ const plugin: Definition = {
|
|
|
301
394
|
cannotLetGo: async () => null,
|
|
302
395
|
})
|
|
303
396
|
if ('deny' in done) throw new Error(done.deny)
|
|
397
|
+
// A shift accepted after its run ended — interrupted while it was written — is not made, never one of a later
|
|
398
|
+
// turn.
|
|
399
|
+
if (tool.name === TOOL && (!session.busy || session.runs !== run))
|
|
400
|
+
void afterTurn(await projectAt(root), loopOf(call.sessionID), letGoOf(call.sessionID), true)
|
|
304
401
|
|
|
305
402
|
return { content: done.result }
|
|
306
403
|
},
|
|
@@ -314,12 +411,9 @@ const plugin: Definition = {
|
|
|
314
411
|
name: SHIFT_COMMAND,
|
|
315
412
|
description: SHIFT_NOW_DESCRIPTION,
|
|
316
413
|
execute: async ({ sessionID, prompt }) => {
|
|
317
|
-
await
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
description: `/${SHIFT_COMMAND}`,
|
|
321
|
-
delivery: 'queue',
|
|
322
|
-
})
|
|
414
|
+
const now = await shiftNow(loopOf(sessionID), prompt.text.trim())
|
|
415
|
+
await ctx.session.synthetic({ sessionID, text: now.text, description: `/${SHIFT_COMMAND}`, delivery: 'queue' })
|
|
416
|
+
await now.delivered()
|
|
323
417
|
},
|
|
324
418
|
})
|
|
325
419
|
// The letter first; the owner's words typed with the command come after it as their own message.
|
|
@@ -331,6 +425,8 @@ const plugin: Definition = {
|
|
|
331
425
|
const words = prompt.text.trim()
|
|
332
426
|
if (resumed.prompt === undefined) return tell(sessionID, resumed.text, 'info')
|
|
333
427
|
await ctx.session.synthetic({ sessionID, text: resumed.prompt, description: resumed.text, resume: words === '' })
|
|
428
|
+
// The newest letter went: one waiting for the owner's message is not told again.
|
|
429
|
+
await dropWaiting(loopOf(sessionID))
|
|
334
430
|
if (words !== '') await ctx.session.prompt({ sessionID, text: words })
|
|
335
431
|
},
|
|
336
432
|
})
|
|
@@ -344,12 +440,13 @@ const plugin: Definition = {
|
|
|
344
440
|
|
|
345
441
|
// The runs and the let-gos, by the server's events; a let-go is awaited outside the loop that reads its end.
|
|
346
442
|
async function onEvent(event: { type: string; data?: unknown }): Promise<void> {
|
|
347
|
-
const data = (event.data ?? {}) as Ended & { tokens?: Tokens; reason?: string }
|
|
348
|
-
if (typeof data.sessionID !== 'string' || (await
|
|
443
|
+
const data = (event.data ?? {}) as Ended & { tokens?: Tokens; reason?: string; inboxID?: string }
|
|
444
|
+
if (typeof data.sessionID !== 'string' || (await apart(data.sessionID))) return
|
|
349
445
|
const session = await state(data.sessionID)
|
|
350
446
|
switch (event.type) {
|
|
351
447
|
case 'session.execution.started':
|
|
352
448
|
session.busy = true
|
|
449
|
+
session.runs += 1
|
|
353
450
|
return
|
|
354
451
|
case 'session.step.ended': {
|
|
355
452
|
const tokens = data.tokens as Tokens | undefined
|
|
@@ -359,25 +456,30 @@ const plugin: Definition = {
|
|
|
359
456
|
await countGrowth(keeperOf(data.sessionID), SOURCES.responses, said)
|
|
360
457
|
return
|
|
361
458
|
}
|
|
459
|
+
// Only a let-go asked for answers the shift's: one opencode makes by itself is `auto`. While a shift is pending
|
|
460
|
+
// that one carries the shift's Hold, and the shift still lets go of what came after it.
|
|
362
461
|
case 'session.compaction.ended':
|
|
363
462
|
session.gauge = letGoGauge(data.reason === 'auto')
|
|
364
463
|
session.fill = undefined
|
|
365
464
|
session.block = undefined
|
|
366
|
-
session.letGo?.({ text: data.text ?? '' })
|
|
465
|
+
if (data.reason !== 'auto') session.letGo?.({ text: data.text ?? '' })
|
|
367
466
|
return
|
|
368
467
|
case 'session.compaction.failed':
|
|
369
|
-
session.letGo?.({ error: `the
|
|
468
|
+
if (data.reason !== 'auto') session.letGo?.({ error: `the harness did not let the history go: ${errorText(data.error)}` })
|
|
469
|
+
return
|
|
470
|
+
case 'session.inbox.cancelled':
|
|
471
|
+
if (data.inboxID !== undefined && data.inboxID === session.letGoID) session.letGo?.({ error: 'the let-go was cancelled' })
|
|
370
472
|
return
|
|
371
473
|
// A run is over, whichever way it ended: a shift held in it lets go now, unless the owner interrupted it.
|
|
372
474
|
case 'session.execution.succeeded':
|
|
373
475
|
case 'session.execution.failed':
|
|
374
476
|
case 'session.execution.interrupted': {
|
|
375
477
|
session.busy = false
|
|
376
|
-
const
|
|
377
|
-
await
|
|
478
|
+
const project = once()
|
|
479
|
+
await runEnded(courierOf(data.sessionID, project))
|
|
378
480
|
if (session.loop.pending === null) return
|
|
379
481
|
const aborted = event.type === 'session.execution.interrupted'
|
|
380
|
-
void afterTurn(await
|
|
482
|
+
void afterTurn(await project(), loopOf(data.sessionID), letGoOf(data.sessionID), aborted)
|
|
381
483
|
return
|
|
382
484
|
}
|
|
383
485
|
}
|
|
@@ -405,6 +507,11 @@ function waitKey(sessionID: string): string {
|
|
|
405
507
|
return `wait:${sessionID}`
|
|
406
508
|
}
|
|
407
509
|
|
|
510
|
+
// The shift a session accepted and has not let go yet: a reload of the plugin must not drop it.
|
|
511
|
+
function pendingKey(sessionID: string): string {
|
|
512
|
+
return `pending:${sessionID}`
|
|
513
|
+
}
|
|
514
|
+
|
|
408
515
|
function errorText(error: unknown): string {
|
|
409
516
|
if (typeof error === 'string') return error
|
|
410
517
|
if (typeof error === 'object' && error !== null && 'message' in error) return String(error.message)
|
|
@@ -415,8 +522,9 @@ function errorText(error: unknown): string {
|
|
|
415
522
|
// The growth of the history since the last let-go, counted the way rows are counted as they come.
|
|
416
523
|
function growthIn(messages: readonly unknown[]): Record<string, number> {
|
|
417
524
|
let gauge = FRESH_GAUGE
|
|
418
|
-
for (const message of messages as { type: string; text?: string; content?: unknown }[]) {
|
|
419
|
-
|
|
525
|
+
for (const message of messages as { type: string; status?: string; text?: string; content?: unknown }[]) {
|
|
526
|
+
// A let-go that failed or still runs left the history before it whole.
|
|
527
|
+
if (message.type === 'compaction') gauge = message.status === undefined || message.status === 'completed' ? FRESH_GAUGE : gauge
|
|
420
528
|
else if (message.type === 'user') gauge = addGrowth(gauge, SOURCES.owner, message.text?.length ?? 0)
|
|
421
529
|
else if (message.type === 'synthetic') gauge = addGrowth(gauge, SOURCES.attachments, message.text?.length ?? 0)
|
|
422
530
|
else if (message.type === 'assistant' && Array.isArray(message.content)) {
|
package/store.ts
CHANGED
|
@@ -1,30 +1,47 @@
|
|
|
1
1
|
import { execFile } from 'node:child_process'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import type { Stats } from 'node:fs'
|
|
3
|
+
import { access, chmod, mkdir, readdir, readFile, readlink, realpath, rename, stat, unlink, writeFile } from 'node:fs/promises'
|
|
4
|
+
import { basename, dirname, resolve } from 'node:path'
|
|
4
5
|
import { loadProject, type Project, type Store } from './core/index'
|
|
5
6
|
|
|
7
|
+
let writes = 0
|
|
8
|
+
|
|
6
9
|
// The core's store over node's files and processes: pi runs an extension in its own node process, opencode a plugin
|
|
7
10
|
// in its server under Bun.
|
|
8
11
|
export const nodeStore: Store = {
|
|
9
12
|
read: path => readFile(path, 'utf8').catch(() => null),
|
|
10
13
|
list: async (path, kind) => {
|
|
11
|
-
const entries = await
|
|
14
|
+
const entries = await kinds(path)
|
|
12
15
|
|
|
13
16
|
return entries
|
|
14
|
-
.filter(entry =>
|
|
17
|
+
.filter(entry => entry.kind === kind)
|
|
15
18
|
.map(entry => entry.name)
|
|
16
19
|
.sort()
|
|
17
20
|
},
|
|
18
21
|
stamps: async path => {
|
|
19
|
-
const
|
|
20
|
-
const files = entries.filter(entry => entry.isFile())
|
|
21
|
-
const stats = await Promise.all(files.map(entry => stat(`${path}/${entry.name}`).catch(() => null)))
|
|
22
|
+
const files = (await kinds(path)).filter(entry => entry.kind === 'file')
|
|
22
23
|
|
|
23
|
-
return Object.fromEntries(files.
|
|
24
|
+
return Object.fromEntries(files.map(entry => [entry.name, `${entry.stats.mtimeMs}:${entry.stats.size}`]))
|
|
24
25
|
},
|
|
26
|
+
// Written whole or not at all: a write that fails half way leaves the record as it was. A link is written through,
|
|
27
|
+
// as an editor would, and the file keeps its mode.
|
|
25
28
|
write: async (path, text) => {
|
|
26
29
|
await mkdir(dirname(path), { recursive: true })
|
|
27
|
-
await
|
|
30
|
+
const target = await through(path)
|
|
31
|
+
const mode = await stat(target).then(
|
|
32
|
+
found => found.mode,
|
|
33
|
+
() => null,
|
|
34
|
+
)
|
|
35
|
+
// Its own temporary file for each write: two writes of one file at once never mix.
|
|
36
|
+
const temporary = `${dirname(target)}/.${basename(target)}.${process.pid}.${++writes}.tmp`
|
|
37
|
+
try {
|
|
38
|
+
await writeFile(temporary, text)
|
|
39
|
+
if (mode !== null) await chmod(temporary, mode)
|
|
40
|
+
await rename(temporary, target)
|
|
41
|
+
} catch (error) {
|
|
42
|
+
await unlink(temporary).catch(() => undefined)
|
|
43
|
+
throw error
|
|
44
|
+
}
|
|
28
45
|
},
|
|
29
46
|
exists: path =>
|
|
30
47
|
access(path).then(
|
|
@@ -55,3 +72,36 @@ export const nodeStore: Store = {
|
|
|
55
72
|
export function projectAt(root: string): Promise<Project> {
|
|
56
73
|
return loadProject(nodeStore, root)
|
|
57
74
|
}
|
|
75
|
+
|
|
76
|
+
// The file a write goes to: a link's target, through a chain of links, even one not there yet; a cycle of links is
|
|
77
|
+
// refused, never written over. As many links as the system follows: 40, and the file they end at.
|
|
78
|
+
const MAX_LINKS = 40
|
|
79
|
+
|
|
80
|
+
async function through(path: string): Promise<string> {
|
|
81
|
+
const real = await realpath(path).catch(() => null)
|
|
82
|
+
if (real !== null) return real
|
|
83
|
+
let at = path
|
|
84
|
+
for (let hop = 0; hop <= MAX_LINKS; hop++) {
|
|
85
|
+
const link = await readlink(at).catch(() => null)
|
|
86
|
+
if (link === null) return at
|
|
87
|
+
at = resolve(dirname(at), link)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
throw new Error(`${path}: too many links, or a cycle of them`)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// A folder's files and folders, a link counted as what it points to; an entry that cannot be read is left out.
|
|
94
|
+
async function kinds(path: string): Promise<{ name: string; kind: 'file' | 'dir'; stats: Stats }[]> {
|
|
95
|
+
const entries = await readdir(path).catch(() => [] as string[])
|
|
96
|
+
const found = await Promise.all(
|
|
97
|
+
entries.map(async entry => {
|
|
98
|
+
const stats = await stat(`${path}/${entry}`).catch(() => null)
|
|
99
|
+
if (stats === null) return []
|
|
100
|
+
const kind = stats.isDirectory() ? ('dir' as const) : stats.isFile() ? ('file' as const) : null
|
|
101
|
+
|
|
102
|
+
return kind === null ? [] : [{ name: entry, kind, stats }]
|
|
103
|
+
}),
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
return found.flat()
|
|
107
|
+
}
|