@iiiokojiadbi/shift 0.0.0-stage → 0.28.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/LICENSE +21 -0
- package/README.md +12 -2
- package/core/about.ts +98 -0
- package/core/archive.ts +41 -0
- package/core/delivery.ts +131 -0
- package/core/docs.ts +396 -0
- package/core/flows.ts +473 -0
- package/core/glossary.ts +75 -0
- package/core/hint.ts +241 -0
- package/core/history.ts +73 -0
- package/core/index.ts +25 -0
- package/core/input.ts +198 -0
- package/core/layout.ts +173 -0
- package/core/loop.ts +142 -0
- package/core/markdown.ts +194 -0
- package/core/paths.ts +51 -0
- package/core/project.ts +27 -0
- package/core/records.ts +272 -0
- package/core/settings.ts +239 -0
- package/core/setup.ts +105 -0
- package/core/store.ts +144 -0
- package/core/tasks.ts +172 -0
- package/core/texts/doctrine.ts +373 -0
- package/core/texts/prompts.ts +261 -0
- package/core/texts/rows.ts +46 -0
- package/core/texts/tools.ts +243 -0
- package/core/tools.ts +193 -0
- package/core/track.ts +129 -0
- package/core/types.ts +129 -0
- package/index.ts +322 -0
- package/package.json +29 -4
- package/store.ts +56 -0
package/core/types.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// Generated from core/src by scripts/sync-core.ts — edit the source, not this copy.
|
|
2
|
+
import type { AFTER, KINDS, OUTCOMES } from './layout'
|
|
3
|
+
import type { Placement } from './settings'
|
|
4
|
+
|
|
5
|
+
export type ShiftAfter = (typeof AFTER)[number]
|
|
6
|
+
|
|
7
|
+
// The fill the stretch started from, the fill at the last hint, a harness-made let-go the agent is not told of yet,
|
|
8
|
+
// the characters the stretch's history grew by, per source, and whether the count holds the whole stretch:
|
|
9
|
+
// a fresh process starts with none of it and restores it once from the last shift and the history.
|
|
10
|
+
export type Gauge = { base: number | null; hinted: number | null; unassisted: boolean; growth: Record<string, number>; whole: boolean }
|
|
11
|
+
|
|
12
|
+
export type ShiftInput = { letter: string; after: ShiftAfter; about: string }
|
|
13
|
+
|
|
14
|
+
// What task_track asks for: items of a task's track, or the task's own line in TASKS.md.
|
|
15
|
+
// One track item's change: an item found by its id, or a new item by its text.
|
|
16
|
+
export type ItemChange = ({ id: number } | { item: string }) & { status: string; note: string | null }
|
|
17
|
+
// Track items changed together, all or none, and the task's own status in TASKS.md.
|
|
18
|
+
export type TrackInput = { task: string; items: ItemChange[]; own: { status: string; note: string | null } | null }
|
|
19
|
+
|
|
20
|
+
// A task to open: its folder name, its title, goal and done-when for task.md, its first items, and its line with
|
|
21
|
+
// the links under it for TASKS.md.
|
|
22
|
+
export type OpenInput = {
|
|
23
|
+
name: string
|
|
24
|
+
title: string
|
|
25
|
+
goal: string
|
|
26
|
+
doneWhen: string
|
|
27
|
+
status: string
|
|
28
|
+
about: string
|
|
29
|
+
items: { name: string; note: string | null }[]
|
|
30
|
+
links: { kind: 'needs' | 'after' | 'continues'; name: string; why: string | null }[]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type ArchiveInput = { name: string; outcome: (typeof OUTCOMES)[number]; about: string }
|
|
34
|
+
|
|
35
|
+
// A `- [status] name — note` line of a block: `rest` is all after the status, `index` the line's place in the text.
|
|
36
|
+
export type Item = { status: string; id: number | null; name: string; note: string | null; rest: string; line: string; index: number }
|
|
37
|
+
|
|
38
|
+
// The board's tasks: status, folder name and the note after the dash.
|
|
39
|
+
export type FocusTask = { status: string; name: string; line: string }
|
|
40
|
+
|
|
41
|
+
// A task's folder: task.md, track.md and the notes in its notes/ folder, each a file name with `.md` and its text.
|
|
42
|
+
export type TaskFiles = { name: string; task: string | null; track: string | null; notes: { name: string; text: string }[] }
|
|
43
|
+
|
|
44
|
+
export type Kind = keyof typeof KINDS
|
|
45
|
+
|
|
46
|
+
// A file of `knowledge/`: its name with `.md` and its text.
|
|
47
|
+
export type KnowledgeFile = { name: string; text: string }
|
|
48
|
+
|
|
49
|
+
// A file loaded into every session, its path from the project's root.
|
|
50
|
+
export type SessionFile = { path: string; text: string }
|
|
51
|
+
|
|
52
|
+
export type Work = {
|
|
53
|
+
board: string
|
|
54
|
+
owner: string | null
|
|
55
|
+
tasks: TaskFiles[]
|
|
56
|
+
knowledge: KnowledgeFile[]
|
|
57
|
+
// TASKS.md: the open tasks and their links.
|
|
58
|
+
taskList: string | null
|
|
59
|
+
glossary: string | null
|
|
60
|
+
// SHIFT.md: the project's rules and the map of its documents.
|
|
61
|
+
shift: string | null
|
|
62
|
+
// Folder names in the archive.
|
|
63
|
+
archived: string[]
|
|
64
|
+
// owner.md is still there under its old name.
|
|
65
|
+
oldOwner: boolean
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// What a shift hands back beside the letter: the records as they are now, where new knowledge goes, the last shifts,
|
|
69
|
+
// the shift's folder and the documents with remarks.
|
|
70
|
+
export type HandedBack = {
|
|
71
|
+
work: Work | null
|
|
72
|
+
knowledge: Placement | null
|
|
73
|
+
// OWNER.md from the project's root, where its settings put it.
|
|
74
|
+
owner: string
|
|
75
|
+
recent: Recent
|
|
76
|
+
dir: string
|
|
77
|
+
remarks: string | null
|
|
78
|
+
// Tasks whose files went unchanged through many shifts, with how many.
|
|
79
|
+
stuck: Stuck[]
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export type Stuck = { name: string; status: string; shifts: number }
|
|
83
|
+
|
|
84
|
+
// What one stretch of work did, from the files against the previous shift's snapshot.
|
|
85
|
+
export type Segment = { done: string[]; dropped: string[]; archived: string[]; open: string[] }
|
|
86
|
+
|
|
87
|
+
export type Snapshot = { work: Work; index: string | null }
|
|
88
|
+
|
|
89
|
+
export type AboutMeta = {
|
|
90
|
+
about: string
|
|
91
|
+
sessionId: string
|
|
92
|
+
at: number
|
|
93
|
+
after: ShiftAfter
|
|
94
|
+
segment: Segment
|
|
95
|
+
tokensBefore?: number | undefined
|
|
96
|
+
tokensAfter?: number | undefined
|
|
97
|
+
summaryChars?: number | undefined
|
|
98
|
+
result: string
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export type Reading = { tokens: number | undefined; window: number; threshold: number | undefined }
|
|
102
|
+
|
|
103
|
+
export type HistoryEntry = { name: string; about: string | null }
|
|
104
|
+
// The last shifts' about.md, oldest first, and how many shifts the project has in all.
|
|
105
|
+
export type Recent = { entries: HistoryEntry[]; total: number }
|
|
106
|
+
|
|
107
|
+
// A shift accepted and waiting for the turn to end: what the let-go and the message after it need.
|
|
108
|
+
export type PendingShift = {
|
|
109
|
+
after: ShiftAfter
|
|
110
|
+
letter: string
|
|
111
|
+
hold: string
|
|
112
|
+
dir: string
|
|
113
|
+
board: string
|
|
114
|
+
// OWNER.md, absolute, where the project's settings put it.
|
|
115
|
+
owner: string
|
|
116
|
+
sessionId: string
|
|
117
|
+
about: string
|
|
118
|
+
at: number
|
|
119
|
+
segment: Segment
|
|
120
|
+
// The tasks in progress, each with its items in progress: what the summarizer keeps the reasoning around fuller.
|
|
121
|
+
// Missing from a shift accepted by an older version of the mod.
|
|
122
|
+
focus?: string[]
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// The letter waiting for the owner's next message, the shift folder it came from and the files that go with it.
|
|
126
|
+
export type WaitNote = { context: string; dir: string; files: string[] }
|
|
127
|
+
|
|
128
|
+
// What follows a let-go: the agent goes on from the prompt, or the note waits for the owner; the row tells the owner.
|
|
129
|
+
export type Settled = { after: 'continue'; streak: number; prompt: string; row: string } | { after: 'wait'; note: WaitNote; row: string }
|
package/index.ts
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { resolve } from 'node:path'
|
|
2
|
+
import type { CompactionEntry, CompactionResult, ExtensionAPI, ExtensionContext, SessionEntry } from '@earendil-works/pi-coding-agent'
|
|
3
|
+
import { estimateTokens, findCutPoint, SettingsManager } from '@earendil-works/pi-coding-agent'
|
|
4
|
+
import type { TSchema } from 'typebox'
|
|
5
|
+
import type { Courier, Gauge, GaugeKeeper, LetGoHarness, Looks, Loop, LoopState, Note, WaitNote } from './core/index'
|
|
6
|
+
import {
|
|
7
|
+
addGrowth,
|
|
8
|
+
afterTool,
|
|
9
|
+
afterTurn,
|
|
10
|
+
afterWrite,
|
|
11
|
+
atPrompt,
|
|
12
|
+
beforeTool,
|
|
13
|
+
contentChars,
|
|
14
|
+
countGrowth,
|
|
15
|
+
FRESH_GAUGE,
|
|
16
|
+
FRESH_LOOKS,
|
|
17
|
+
FRESH_LOOP,
|
|
18
|
+
formatK,
|
|
19
|
+
gaugeStatus,
|
|
20
|
+
letGoGauge,
|
|
21
|
+
MOD,
|
|
22
|
+
ownerSpoke,
|
|
23
|
+
RESUME_COMMAND,
|
|
24
|
+
RESUME_DESCRIPTION,
|
|
25
|
+
resumeShift,
|
|
26
|
+
SETTINGS,
|
|
27
|
+
SOURCES,
|
|
28
|
+
sessionDoctrine,
|
|
29
|
+
sessionFiles,
|
|
30
|
+
shiftBase,
|
|
31
|
+
startSession,
|
|
32
|
+
TASKS_COMMAND,
|
|
33
|
+
TASKS_DESCRIPTION,
|
|
34
|
+
TOOLS,
|
|
35
|
+
tasksView,
|
|
36
|
+
} from './core/index'
|
|
37
|
+
import { projectAt } from './store'
|
|
38
|
+
|
|
39
|
+
const OURS: ReadonlySet<string> = new Set(TOOLS.map(tool => tool.name))
|
|
40
|
+
// pi's tools that only read: nothing to look at after them.
|
|
41
|
+
const READING: ReadonlySet<string> = new Set(['read', 'grep', 'find', 'ls'])
|
|
42
|
+
|
|
43
|
+
// The custom entry that keeps a wait note across a reload: the extension's own state lives only as long as its runtime.
|
|
44
|
+
const WAIT_ENTRY = `${MOD}:wait`
|
|
45
|
+
|
|
46
|
+
export default function shift(pi: ExtensionAPI): void {
|
|
47
|
+
let loop: LoopState = FRESH_LOOP
|
|
48
|
+
let gauge: Gauge = FRESH_GAUGE
|
|
49
|
+
// Set by the owner's own input, consumed by the run it starts.
|
|
50
|
+
let ownerTyped = false
|
|
51
|
+
// false when the project's settings turn the mod off; read at each session start.
|
|
52
|
+
let enabled = true
|
|
53
|
+
// The doctrine with the tools this project shows; read at each session start, built at the first run without one.
|
|
54
|
+
let doctrine: string | null = null
|
|
55
|
+
// The records as last seen and the agent's tools running now; what changed before each tool began, by its call.
|
|
56
|
+
let looks: Looks = FRESH_LOOKS
|
|
57
|
+
const outside = new Map<string, Note[]>()
|
|
58
|
+
|
|
59
|
+
const text = (body: string) => ({ content: [{ type: 'text' as const, text: body }], details: undefined })
|
|
60
|
+
const schema = (shape: object) => shape as TSchema
|
|
61
|
+
|
|
62
|
+
// Every tool of the table; the session's start takes the ones the project does not show out of the active set.
|
|
63
|
+
for (const tool of TOOLS) {
|
|
64
|
+
pi.registerTool({
|
|
65
|
+
name: tool.name,
|
|
66
|
+
label: tool.label,
|
|
67
|
+
description: tool.description,
|
|
68
|
+
parameters: schema(tool.schema),
|
|
69
|
+
...(tool.writes ? { executionMode: 'sequential' as const } : {}),
|
|
70
|
+
async execute(_id, params, _signal, _onUpdate, ctx) {
|
|
71
|
+
const done = await tool.run(params as Record<string, unknown>, {
|
|
72
|
+
project: () => projectAt(ctx.cwd),
|
|
73
|
+
sessionId: async () => ctx.sessionManager.getSessionId(),
|
|
74
|
+
now: async () => Date.now(),
|
|
75
|
+
subagent: false,
|
|
76
|
+
loop: loopOf(ctx),
|
|
77
|
+
// pi reads the session's files anew before every run, so its copy is never older than them.
|
|
78
|
+
loaded: async () => null,
|
|
79
|
+
status: () => gaugeStatus(keeperOf(ctx)),
|
|
80
|
+
cannotLetGo: async () => cannotLetGo(ctx),
|
|
81
|
+
})
|
|
82
|
+
if ('deny' in done) throw new Error(done.deny)
|
|
83
|
+
|
|
84
|
+
return text(done.result)
|
|
85
|
+
},
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
pi.registerCommand(TASKS_COMMAND, {
|
|
90
|
+
description: TASKS_DESCRIPTION,
|
|
91
|
+
handler: async (_args, ctx) => ctx.ui.notify(await tasksView(await projectAt(ctx.cwd)), 'info'),
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
pi.registerCommand(RESUME_COMMAND, {
|
|
95
|
+
description: RESUME_DESCRIPTION,
|
|
96
|
+
// The owner's words typed with the command go after the letter, as their own message that starts the turn.
|
|
97
|
+
handler: async (args, ctx) => {
|
|
98
|
+
if (!enabled) return ctx.ui.notify(`${MOD} is off in this project: ${SETTINGS} has enabled = false.`, 'info')
|
|
99
|
+
const resumed = await resumeShift(await projectAt(ctx.cwd))
|
|
100
|
+
ctx.ui.notify(resumed.text, 'info')
|
|
101
|
+
if (resumed.prompt === undefined) return
|
|
102
|
+
const words = args.trim()
|
|
103
|
+
pi.sendMessage({ customType: MOD, content: resumed.prompt, display: true }, { triggerTurn: words === '' })
|
|
104
|
+
if (words !== '') pi.sendUserMessage(words)
|
|
105
|
+
},
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
// Tools are registered before the project is known, so an off project only takes them out of the active set.
|
|
109
|
+
// A wait note survives a reload: the last wait entry on the branch says whether one is still undelivered.
|
|
110
|
+
pi.on('session_start', async (_event, ctx) => {
|
|
111
|
+
const start = await startSession(await projectAt(ctx.cwd))
|
|
112
|
+
enabled = start.enabled
|
|
113
|
+
doctrine = start.doctrineBody
|
|
114
|
+
for (const row of start.rows) tell(ctx, row.text, row.level)
|
|
115
|
+
const shown = start.tools.map(tool => tool.name)
|
|
116
|
+
pi.setActiveTools([...pi.getActiveTools().filter(name => !OURS.has(name)), ...shown])
|
|
117
|
+
if (!enabled) return
|
|
118
|
+
const last = ctx.sessionManager
|
|
119
|
+
.getBranch()
|
|
120
|
+
.filter(entry => entry.type === 'custom' && entry.customType === WAIT_ENTRY)
|
|
121
|
+
.at(-1)
|
|
122
|
+
loop = { ...loop, note: last?.type === 'custom' ? ((last.data as { note: WaitNote | null } | undefined)?.note ?? null) : null }
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
// The owner's own input, idle or typed mid-run, starts the count of shifts without them over; only a message that
|
|
126
|
+
// starts a run takes the waiting letter with it.
|
|
127
|
+
pi.on('input', async (event, ctx) => {
|
|
128
|
+
if (!enabled || event.source === 'extension') return
|
|
129
|
+
await ownerSpoke(loopOf(ctx))
|
|
130
|
+
if (event.streamingBehavior === undefined) ownerTyped = true
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
// The doctrine every run; between turns a hint, and after a wait the letter, follow the owner's message.
|
|
134
|
+
pi.on('before_agent_start', async (event, ctx) => {
|
|
135
|
+
if (!enabled) return
|
|
136
|
+
doctrine ??= sessionDoctrine((await projectAt(ctx.cwd)).settings).doctrineBody
|
|
137
|
+
event.systemPromptOptions.sections[MOD] = doctrine
|
|
138
|
+
// SHIFT.md, OWNER.md, GLOSSARY.md and TASKS.md go where pi puts AGENTS.md; the same paths are replaced, not added twice.
|
|
139
|
+
const loaded = await sessionFiles(await projectAt(ctx.cwd))
|
|
140
|
+
const ours = new Set(loaded.map(file => file.path))
|
|
141
|
+
event.systemPromptOptions.contextFiles = [
|
|
142
|
+
...event.systemPromptOptions.contextFiles.filter(file => !ours.has(file.path)),
|
|
143
|
+
...loaded.map(file => ({ path: file.path, content: file.text })),
|
|
144
|
+
]
|
|
145
|
+
// Records changed between runs — by the owner's hand or another session — a hint, and after a wait the letter.
|
|
146
|
+
const owner = event.messageType === 'user' && ownerTyped
|
|
147
|
+
if (owner) ownerTyped = false
|
|
148
|
+
const told = await atPrompt(courierOf(ctx), { runStarts: true, owner })
|
|
149
|
+
if (told.notes.length === 0) return
|
|
150
|
+
await told.delivered()
|
|
151
|
+
|
|
152
|
+
return { message: { customType: MOD, content: told.notes.join('\n\n'), display: true } }
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
// Every message of the history is counted by source.
|
|
156
|
+
pi.on('message_end', async (event, ctx) => {
|
|
157
|
+
if (!enabled) return
|
|
158
|
+
const source = growthSource(event.message)
|
|
159
|
+
if (source !== null) await countGrowth(keeperOf(ctx), source, messageChars(event.message))
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
// Every note to the agent mid-run goes with a tool's result: what changed in the records around a tool of the agent,
|
|
163
|
+
// the docs after a write, a hint. A nested tool's write gets only the docs.
|
|
164
|
+
pi.on('tool_call', async (event, ctx) => {
|
|
165
|
+
if (!enabled || event.parentToolCallId !== undefined) return
|
|
166
|
+
outside.set(event.toolCallId, await beforeTool(courierOf(ctx)))
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
pi.on('tool_result', async (event, ctx) => {
|
|
170
|
+
if (!enabled) return
|
|
171
|
+
const path = (event.toolName === 'edit' || event.toolName === 'write') && !event.isError ? event.input.path : undefined
|
|
172
|
+
const written = typeof path === 'string' ? resolve(ctx.cwd, path) : undefined
|
|
173
|
+
let added: string[]
|
|
174
|
+
if (event.parentToolCallId === undefined) {
|
|
175
|
+
const before = outside.get(event.toolCallId) ?? []
|
|
176
|
+
outside.delete(event.toolCallId)
|
|
177
|
+
added = await afterTool(courierOf(ctx), before, { own: OURS.has(event.toolName), readOnly: READING.has(event.toolName), written })
|
|
178
|
+
} else added = written === undefined ? [] : await afterWrite(courierOf(ctx), written)
|
|
179
|
+
if (added.length === 0) return
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
content: [...event.content, ...added.map(each => ({ type: 'text' as const, text: each }))],
|
|
183
|
+
structuredContent: event.structuredContent,
|
|
184
|
+
}
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
// The shift lets go once the run has settled: compacting aborts whatever runs, so never from inside the turn.
|
|
188
|
+
pi.on('agent_settled', async (event, ctx) => {
|
|
189
|
+
if (loop.pending === null) return
|
|
190
|
+
await afterTurn(await projectAt(ctx.cwd), loopOf(ctx), letGoOf(ctx), event.aborted)
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
// Any compaction starts a new stretch; one the agent did not ask for is told to it at its next look.
|
|
194
|
+
pi.on('session_compact', event => {
|
|
195
|
+
gauge = letGoGauge(event.reason !== 'manual')
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
// The notes to the agent over the extension's runtime: the looks at the records, the loop and the gauge.
|
|
199
|
+
function courierOf(ctx: ExtensionContext): Courier {
|
|
200
|
+
return {
|
|
201
|
+
loop: loopOf(ctx),
|
|
202
|
+
gauge: keeperOf(ctx),
|
|
203
|
+
project: () => projectAt(ctx.cwd),
|
|
204
|
+
looks: async () => looks,
|
|
205
|
+
updateLooks: async change => {
|
|
206
|
+
looks = change(looks)
|
|
207
|
+
},
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// The loop's state in the extension's runtime; a wait note also in a session entry, so it survives a reload.
|
|
212
|
+
function loopOf(ctx: ExtensionContext): Loop {
|
|
213
|
+
return {
|
|
214
|
+
read: async () => loop,
|
|
215
|
+
update: async change => {
|
|
216
|
+
const before = loop
|
|
217
|
+
loop = change(loop)
|
|
218
|
+
if (loop.note !== before.note) pi.appendEntry(WAIT_ENTRY, { note: loop.note })
|
|
219
|
+
},
|
|
220
|
+
tell: (row, level) => tell(ctx, row, level),
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// pi compacts with callbacks; the next turn starts from a custom message.
|
|
225
|
+
function letGoOf(ctx: ExtensionContext): LetGoHarness {
|
|
226
|
+
return {
|
|
227
|
+
fill: async () => ctx.getContextUsage()?.tokens ?? undefined,
|
|
228
|
+
letGo: instructions =>
|
|
229
|
+
new Promise<Awaited<ReturnType<LetGoHarness['letGo']>>>(done => {
|
|
230
|
+
ctx.compact({
|
|
231
|
+
customInstructions: instructions,
|
|
232
|
+
onError: error => done({ skip: error.message }),
|
|
233
|
+
onComplete: (result: CompactionResult) =>
|
|
234
|
+
done({
|
|
235
|
+
messages: [{ role: 'digest', text: result.summary }],
|
|
236
|
+
before: result.tokensBefore ?? undefined,
|
|
237
|
+
after: result.estimatedTokensAfter ?? undefined,
|
|
238
|
+
}),
|
|
239
|
+
})
|
|
240
|
+
}).catch((error: unknown) => ({ skip: `did not start: ${String(error)}` })),
|
|
241
|
+
continueWith: async prompt => {
|
|
242
|
+
pi.sendMessage({ customType: MOD, content: prompt, display: true }, { triggerTurn: true })
|
|
243
|
+
},
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// The gauge in the extension's runtime; the history and the base read from the session. pi compacts by itself once
|
|
248
|
+
// the free room falls under `reserveTokens`.
|
|
249
|
+
function keeperOf(ctx: ExtensionContext): GaugeKeeper {
|
|
250
|
+
return {
|
|
251
|
+
read: async () => gauge,
|
|
252
|
+
update: async change => {
|
|
253
|
+
gauge = change(gauge)
|
|
254
|
+
},
|
|
255
|
+
history: async () => {
|
|
256
|
+
let counted = FRESH_GAUGE
|
|
257
|
+
for (const message of ctx.sessionManager.buildSessionProjection().messages) {
|
|
258
|
+
const source = growthSource(message)
|
|
259
|
+
if (source !== null) counted = addGrowth(counted, source, messageChars(message))
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return counted.growth
|
|
263
|
+
},
|
|
264
|
+
base: async () => shiftBase(await projectAt(ctx.cwd), ctx.sessionManager.getSessionId()),
|
|
265
|
+
reading: async () => {
|
|
266
|
+
const usage = ctx.getContextUsage()
|
|
267
|
+
const window = usage?.contextWindow ?? ctx.model?.contextWindow ?? 0
|
|
268
|
+
const settings = SettingsManager.create(ctx.cwd).getCompactionSettings(ctx.model)
|
|
269
|
+
|
|
270
|
+
return { tokens: usage?.tokens ?? undefined, window, threshold: settings.enabled ? window - settings.reserveTokens : undefined }
|
|
271
|
+
},
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// pi lets go only of the messages before the cut it finds by keepRecentTokens, counted from the last let-go; with none
|
|
277
|
+
// there — a history no bigger, the cut on its first message — it refuses as "session too small". The same cut is
|
|
278
|
+
// found here, before the shift is accepted.
|
|
279
|
+
// What a let-go summarizes: the conversation, not the system prompt pi records as a message of its own.
|
|
280
|
+
function summarized(entry: SessionEntry): boolean {
|
|
281
|
+
return entry.type === 'message' ? entry.message.role !== 'system' : entry.type === 'custom_message' || entry.type === 'branch_summary'
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function cannotLetGo(ctx: ExtensionContext): string | null {
|
|
285
|
+
const keep = SettingsManager.create(ctx.cwd).getCompactionSettings(ctx.model).keepRecentTokens
|
|
286
|
+
const branch = ctx.sessionManager.getBranch()
|
|
287
|
+
const last = branch.findLastIndex(entry => entry.type === 'compaction')
|
|
288
|
+
const kept = last === -1 ? -1 : branch.findIndex(entry => entry.id === (branch[last] as CompactionEntry).firstKeptEntryId)
|
|
289
|
+
const start = kept === -1 ? Math.max(0, last + 1) : kept
|
|
290
|
+
const cut = findCutPoint(branch, start, branch.length, keep)
|
|
291
|
+
if (branch.slice(start, cut.firstKeptEntryIndex).some(summarized)) return null
|
|
292
|
+
const since = branch.slice(start).reduce((sum, entry) => sum + (entry.type === 'message' ? estimateTokens(entry.message) : 0), 0)
|
|
293
|
+
|
|
294
|
+
return `the history since the last let-go is about ${formatK(since)} tokens, and pi keeps its last ${formatK(keep)} whole, so it has nothing to let go.`
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// A row for the owner; a context gone stale with its session has no one to tell.
|
|
298
|
+
function tell(ctx: ExtensionContext, row: string, kind: 'info' | 'warning' | 'error'): void {
|
|
299
|
+
try {
|
|
300
|
+
ctx.ui.notify(row, kind)
|
|
301
|
+
} catch {
|
|
302
|
+
// The row is in about.md all the same.
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Who a message came from, as the hint names it; digests of earlier history are not counted.
|
|
307
|
+
function growthSource(message: { role: string; toolName?: string }): string | null {
|
|
308
|
+
if (message.role === 'assistant') return SOURCES.responses
|
|
309
|
+
if (message.role === 'user' || message.role === 'bashExecution') return SOURCES.owner
|
|
310
|
+
if (message.role === 'toolResult') return message.toolName ?? SOURCES.tools
|
|
311
|
+
if (message.role === 'compactionSummary' || message.role === 'branchSummary') return null
|
|
312
|
+
|
|
313
|
+
return SOURCES.attachments
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// A shell command the owner ran has its command and output instead of content.
|
|
317
|
+
function messageChars(message: object): number {
|
|
318
|
+
if ('content' in message) return contentChars(message.content)
|
|
319
|
+
if ('output' in message && typeof message.output === 'string') return message.output.length
|
|
320
|
+
|
|
321
|
+
return 0
|
|
322
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iiiokojiadbi/shift",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.28.0",
|
|
4
|
+
"description": "The agent decides by itself when to shift: it keeps a work board and task folders, writes a letter to itself, and continues from where it stopped.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/iiiokojiadbi/shift.git",
|
|
10
|
+
"directory": "adapters/pi"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"pi-package"
|
|
14
|
+
],
|
|
15
|
+
"files": [
|
|
16
|
+
"index.ts",
|
|
17
|
+
"store.ts",
|
|
18
|
+
"core",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"pi": {
|
|
23
|
+
"extensions": [
|
|
24
|
+
"./index.ts"
|
|
25
|
+
]
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
29
|
+
"typebox": "*"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/store.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process'
|
|
2
|
+
import { access, mkdir, readdir, readFile, rename, stat, writeFile } from 'node:fs/promises'
|
|
3
|
+
import { dirname } from 'node:path'
|
|
4
|
+
import { loadProject, type Project, type Store } from './core/index'
|
|
5
|
+
|
|
6
|
+
// The core's store over node's files and processes: an extension runs inside pi's own node process.
|
|
7
|
+
export const nodeStore: Store = {
|
|
8
|
+
read: path => readFile(path, 'utf8').catch(() => null),
|
|
9
|
+
list: async (path, kind) => {
|
|
10
|
+
const entries = await readdir(path, { withFileTypes: true }).catch(() => [])
|
|
11
|
+
|
|
12
|
+
return entries
|
|
13
|
+
.filter(entry => (kind === 'dir' ? entry.isDirectory() : entry.isFile()))
|
|
14
|
+
.map(entry => entry.name)
|
|
15
|
+
.sort()
|
|
16
|
+
},
|
|
17
|
+
stamps: async path => {
|
|
18
|
+
const entries = await readdir(path, { withFileTypes: true }).catch(() => [])
|
|
19
|
+
const files = entries.filter(entry => entry.isFile())
|
|
20
|
+
const stats = await Promise.all(files.map(entry => stat(`${path}/${entry.name}`).catch(() => null)))
|
|
21
|
+
|
|
22
|
+
return Object.fromEntries(files.flatMap((entry, i) => (stats[i] ? [[entry.name, `${stats[i].mtimeMs}:${stats[i].size}`]] : [])))
|
|
23
|
+
},
|
|
24
|
+
write: async (path, text) => {
|
|
25
|
+
await mkdir(dirname(path), { recursive: true })
|
|
26
|
+
await writeFile(path, text)
|
|
27
|
+
},
|
|
28
|
+
exists: path =>
|
|
29
|
+
access(path).then(
|
|
30
|
+
() => true,
|
|
31
|
+
() => false,
|
|
32
|
+
),
|
|
33
|
+
move: async (from, to) => {
|
|
34
|
+
try {
|
|
35
|
+
await mkdir(dirname(to), { recursive: true })
|
|
36
|
+
await rename(from, to)
|
|
37
|
+
|
|
38
|
+
return null
|
|
39
|
+
} catch (error) {
|
|
40
|
+
return String(error)
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
git: (root, args) =>
|
|
44
|
+
new Promise(done => {
|
|
45
|
+
execFile('git', ['-C', root, ...args], (error, stdout) => {
|
|
46
|
+
// A number is git's exit code; a string (ENOENT) means there is no git to run.
|
|
47
|
+
const code = error === null ? 0 : error.code
|
|
48
|
+
done(typeof code === 'number' ? { code, stdout } : null)
|
|
49
|
+
})
|
|
50
|
+
}),
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// The project at `root`, loaded once for the event or tool call at hand.
|
|
54
|
+
export function projectAt(root: string): Promise<Project> {
|
|
55
|
+
return loadProject(nodeStore, root)
|
|
56
|
+
}
|