@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/flows.ts
ADDED
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
// Generated from core/src by scripts/sync-core.ts — edit the source, not this copy.
|
|
2
|
+
import { OUTCOME, renderAbout, shiftFill } from './about'
|
|
3
|
+
import { addToIndex, checkArchive, indexLine } from './archive'
|
|
4
|
+
import { docsReport, refreshDocs } from './docs'
|
|
5
|
+
import { renderHistory, renderSummary, segment, shiftDir } from './history'
|
|
6
|
+
import {
|
|
7
|
+
ARCHIVE,
|
|
8
|
+
BOARD,
|
|
9
|
+
CLOSED,
|
|
10
|
+
DIR,
|
|
11
|
+
GUIDE_TOOL,
|
|
12
|
+
HOLD,
|
|
13
|
+
INDEX,
|
|
14
|
+
KNOWLEDGE,
|
|
15
|
+
LETTER,
|
|
16
|
+
MAX_AUTO_STREAK,
|
|
17
|
+
NAMES,
|
|
18
|
+
OLD_DIR,
|
|
19
|
+
SETTINGS,
|
|
20
|
+
SHIFTS,
|
|
21
|
+
STUCK_LOOKBACK,
|
|
22
|
+
STUCK_SHIFTS,
|
|
23
|
+
TASK_LIST,
|
|
24
|
+
TASK_SECTIONS,
|
|
25
|
+
TASKS,
|
|
26
|
+
TRACK_TOOL,
|
|
27
|
+
taskFile,
|
|
28
|
+
} from './layout'
|
|
29
|
+
import { formatItem, formatK, itemsOf, sections } from './markdown'
|
|
30
|
+
import { basename, dirname, join, resolvePath } from './paths'
|
|
31
|
+
import type { Project } from './project'
|
|
32
|
+
import { checkLetter, checkTask, checkWork, checkWritten, listedTasks } from './records'
|
|
33
|
+
import { lastShifts, previousShift, readRecords, readSnapshot, readTask, recentShifts, shiftNames, writeSnapshot } from './store'
|
|
34
|
+
import { archiveInList, checkTaskList, formatTaskLine, parseTaskList, withTask } from './tasks'
|
|
35
|
+
import {
|
|
36
|
+
ACCEPTED,
|
|
37
|
+
ARCHIVED,
|
|
38
|
+
continuePrompt,
|
|
39
|
+
deliveredFiles,
|
|
40
|
+
EXCLUDED,
|
|
41
|
+
NOT_IGNORED,
|
|
42
|
+
OPENED,
|
|
43
|
+
resumePrompt,
|
|
44
|
+
waitContext,
|
|
45
|
+
} from './texts/prompts'
|
|
46
|
+
import { CONTINUING, NOT_MADE, renderTasks, type TaskView, UNSETTLED, WAITING } from './texts/rows'
|
|
47
|
+
import { numberTracks } from './track'
|
|
48
|
+
import type { ArchiveInput, HandedBack, OpenInput, PendingShift, SessionFile, Settled, ShiftInput, Stuck, TaskFiles, Work } from './types'
|
|
49
|
+
|
|
50
|
+
// The flows that do not depend on the harness: an adapter hands them the loaded project and what only it knows.
|
|
51
|
+
|
|
52
|
+
export type ShiftCall = { input: ShiftInput; at: number; sessionId: string }
|
|
53
|
+
|
|
54
|
+
// What the mod last saw of the records: per file, by its path from the project's root, its stamp and a fingerprint of
|
|
55
|
+
// its text as `<stamp>#<fingerprint>`.
|
|
56
|
+
export type Seen = Record<string, string>
|
|
57
|
+
|
|
58
|
+
// Who changed the records since the mod last looked: the agent's own tool — a write, a shell command, a script — or
|
|
59
|
+
// someone else between its tools: the owner's hand, another session.
|
|
60
|
+
export type ChangedBy = 'agent' | 'other'
|
|
61
|
+
|
|
62
|
+
// The records changed since the mod last looked, each checked by the checks of its file, so what the shift would refuse
|
|
63
|
+
// is told as it is written, not hundreds of K later. The agent's own change is told only when broken; someone else's
|
|
64
|
+
// is told always, so the agent reads it before writing over it. The first look only remembers; a removed file is the
|
|
65
|
+
// shift's to tell.
|
|
66
|
+
export async function changedRecords(project: Project, seen: Seen | null, by: ChangedBy): Promise<{ seen: Seen; body: string | null }> {
|
|
67
|
+
if (!project.settings.enabled) return { seen: seen ?? {}, body: null }
|
|
68
|
+
// A new stamp alone is not a change: a file rewritten with the same text, or edited and put back, says nothing.
|
|
69
|
+
const now: Seen = {}
|
|
70
|
+
const changed: string[] = []
|
|
71
|
+
for (const [path, stamp] of Object.entries(await recordStamps(project))) {
|
|
72
|
+
const was = seen?.[path] ?? ''
|
|
73
|
+
const at = was.lastIndexOf('#')
|
|
74
|
+
if (at !== -1 && was.slice(0, at) === stamp) {
|
|
75
|
+
now[path] = was
|
|
76
|
+
continue
|
|
77
|
+
}
|
|
78
|
+
const text = await project.store.read(`${project.root}/${path}`)
|
|
79
|
+
if (text === null) continue
|
|
80
|
+
const hash = String(fingerprint(text))
|
|
81
|
+
now[path] = `${stamp}#${hash}`
|
|
82
|
+
if (seen !== null && was.slice(at + 1) !== hash) changed.push(path)
|
|
83
|
+
}
|
|
84
|
+
if (changed.length === 0) return { seen: now, body: null }
|
|
85
|
+
|
|
86
|
+
const work = await readRecords(project)
|
|
87
|
+
const lines: string[] = []
|
|
88
|
+
let broken = false
|
|
89
|
+
for (const path of changed) {
|
|
90
|
+
const problem = path === LETTER ? await letterProblem(project) : work === null ? null : checkWritten(path, work, project)
|
|
91
|
+
broken ||= problem !== null
|
|
92
|
+
if (by === 'other') lines.push(`\`${path}\` changed outside your tools — by the owner or another session: read it before you write it.`)
|
|
93
|
+
if (problem !== null) lines.push(`\`${path}\`: ${problem}`)
|
|
94
|
+
}
|
|
95
|
+
if (broken) lines.push('The shift refuses until they are fixed; if you are still writing them, go on.')
|
|
96
|
+
|
|
97
|
+
return { seen: now, body: lines.length === 0 ? null : lines.join('\n') }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Every file the record checks look at, by its path from the project's root, with its stamp: a listing per folder,
|
|
101
|
+
// no file read.
|
|
102
|
+
async function recordStamps({ store, root, settings }: Project): Promise<Seen> {
|
|
103
|
+
const stamped = async (dir: string, only?: (name: string) => boolean): Promise<Seen> =>
|
|
104
|
+
Object.fromEntries(
|
|
105
|
+
Object.entries(await store.stamps(join(root, dir)))
|
|
106
|
+
.filter(([name]) => (name.endsWith('.md') || join(dir, name) === SETTINGS) && (only?.(name) ?? true))
|
|
107
|
+
.map(([name, stamp]) => [join(dir, name), stamp]),
|
|
108
|
+
)
|
|
109
|
+
const own = new Set([BOARD, TASK_LIST, LETTER, SETTINGS, settings.files.owner, settings.files.glossary])
|
|
110
|
+
const tasks = await store.list(`${root}/${TASKS}`, 'dir')
|
|
111
|
+
const parts = await Promise.all([
|
|
112
|
+
...[...new Set([DIR, dirname(settings.files.owner), dirname(settings.files.glossary)])].map(dir =>
|
|
113
|
+
stamped(dir, name => own.has(join(dir, name))),
|
|
114
|
+
),
|
|
115
|
+
...tasks.flatMap(name => [
|
|
116
|
+
stamped(`${TASKS}/${name}`, file => file === NAMES.task || file === NAMES.track),
|
|
117
|
+
stamped(`${TASKS}/${name}/${NAMES.notes}`),
|
|
118
|
+
]),
|
|
119
|
+
stamped(KNOWLEDGE),
|
|
120
|
+
])
|
|
121
|
+
|
|
122
|
+
return Object.assign({}, ...parts)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// FNV-1a over the text: whether a file restamped really changed, without keeping its text.
|
|
126
|
+
function fingerprint(text: string): number {
|
|
127
|
+
let hash = 0x811c9dc5
|
|
128
|
+
for (let i = 0; i < text.length; i++) hash = Math.imul(hash ^ text.charCodeAt(i), 0x01000193)
|
|
129
|
+
|
|
130
|
+
return hash >>> 0
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function letterProblem({ store, root }: Project): Promise<string | null> {
|
|
134
|
+
const letter = await store.read(`${root}/${LETTER}`)
|
|
135
|
+
|
|
136
|
+
return letter === null ? null : checkLetter(letter)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Checks the letter and the records, snapshots them into the new shift folder and leaves a pending about.md there.
|
|
140
|
+
export async function acceptShift(project: Project, call: ShiftCall): Promise<{ deny: string } | { shift: PendingShift; result: string }> {
|
|
141
|
+
const { store, root, settings } = project
|
|
142
|
+
const { input } = call
|
|
143
|
+
const stale = await staleLayout(project)
|
|
144
|
+
if (stale !== null) return { deny: stale }
|
|
145
|
+
const letter = await store.read(resolvePath(root, input.letter))
|
|
146
|
+
if (letter === null) return { deny: `No letter at ${input.letter}. Write it (usually ${LETTER}) and call again.` }
|
|
147
|
+
const letterProblem = checkLetter(letter)
|
|
148
|
+
if (letterProblem !== null) return { deny: letterProblem }
|
|
149
|
+
|
|
150
|
+
if (settings.enabled) await refreshDocs(project)
|
|
151
|
+
await numberTracks(project)
|
|
152
|
+
const work = await readRecords(project)
|
|
153
|
+
if (work === null) return { deny: `No board: create ${BOARD} by the sections in ${GUIDE_TOOL} and call again.` }
|
|
154
|
+
const previousDir = await previousShift(project)
|
|
155
|
+
const previous = previousDir === null ? null : await readSnapshot(store, previousDir, `${previousDir}/${NAMES.index}`)
|
|
156
|
+
const workProblem = checkWork(work, previous?.work ?? null, project)
|
|
157
|
+
if (workProblem !== null) return { deny: workProblem }
|
|
158
|
+
const kept = await keepRecordsOut(project)
|
|
159
|
+
if ('deny' in kept) return kept
|
|
160
|
+
|
|
161
|
+
const index = await store.read(`${root}/${INDEX}`)
|
|
162
|
+
const dir = shiftDir(root, call.at)
|
|
163
|
+
const shift: PendingShift = {
|
|
164
|
+
after: input.after,
|
|
165
|
+
letter,
|
|
166
|
+
hold: sections(letter).get(HOLD) ?? '',
|
|
167
|
+
dir,
|
|
168
|
+
board: `${root}/${BOARD}`,
|
|
169
|
+
owner: `${root}/${settings.files.owner}`,
|
|
170
|
+
sessionId: call.sessionId,
|
|
171
|
+
about: input.about,
|
|
172
|
+
at: call.at,
|
|
173
|
+
segment: segment({ work, index }, previous),
|
|
174
|
+
focus: focusOf(work),
|
|
175
|
+
}
|
|
176
|
+
await writeSnapshot(store, dir, { work, letter, index, settings: project.settingsText })
|
|
177
|
+
// Rewritten with the outcome once the turn ends; `pending` stays if the session dies first.
|
|
178
|
+
await store.write(`${dir}/${NAMES.about}`, renderAbout({ ...shift, result: OUTCOME.pending }))
|
|
179
|
+
|
|
180
|
+
// Knowledge is rarely written: a stretch that added none is asked once, while its history is still whole.
|
|
181
|
+
const learned = previous === null || knowledgeText(work) !== knowledgeText(previous.work)
|
|
182
|
+
const accepted = ACCEPTED(dir, learned, input.after)
|
|
183
|
+
|
|
184
|
+
return { shift, result: kept.excluded === null ? accepted : `${EXCLUDED(kept.excluded)}\n\n${accepted}` }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// The tasks in progress, each with the names of its items in progress.
|
|
188
|
+
function focusOf(work: Work): string[] {
|
|
189
|
+
return listedTasks(work)
|
|
190
|
+
.filter(listed => listed.status === 'in progress')
|
|
191
|
+
.map(listed => {
|
|
192
|
+
const track = work.tasks.find(task => task.name === listed.name)?.track ?? ''
|
|
193
|
+
const doing = itemsOf(track)
|
|
194
|
+
.filter(item => item.status === 'in progress')
|
|
195
|
+
.map(item => item.name)
|
|
196
|
+
|
|
197
|
+
return doing.length === 0 ? listed.name : `${listed.name}: ${doing.join('; ')}`
|
|
198
|
+
})
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function knowledgeText(work: Work): string {
|
|
202
|
+
return work.knowledge.map(file => `${file.name}\n${file.text}`).join('\n')
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Moves a finished or dropped task's folder to the archive and adds its line to the index.
|
|
206
|
+
// A task opened valid by construction: task.md by its sections, track.md with its first items, and its line with the
|
|
207
|
+
// links in TASKS.md — all checked before anything is written, so a refusal leaves the records as they were.
|
|
208
|
+
export async function openTask(project: Project, input: OpenInput): Promise<{ deny: string } | { result: string }> {
|
|
209
|
+
const { store, root } = project
|
|
210
|
+
const folder = `${root}/${TASKS}/${input.name}`
|
|
211
|
+
if (await store.exists(folder)) return { deny: `${TASKS}/${input.name}/ already exists: set its status with ${TRACK_TOOL}.` }
|
|
212
|
+
const archived = await store.list(`${root}/${ARCHIVE}`, 'dir')
|
|
213
|
+
if (archived.includes(input.name))
|
|
214
|
+
return { deny: `${input.name} is archived: open a task under another name with continues: ${input.name}.` }
|
|
215
|
+
const task: TaskFiles = {
|
|
216
|
+
name: input.name,
|
|
217
|
+
task: renderTask(input),
|
|
218
|
+
track: input.items.map((item, n) => `${formatItem('open', n + 1, item.name, item.note)}\n`).join(''),
|
|
219
|
+
notes: [],
|
|
220
|
+
}
|
|
221
|
+
const listPath = `${root}/${TASK_LIST}`
|
|
222
|
+
const list = withTask(await store.read(listPath), input, archived)
|
|
223
|
+
const folders = [...(await store.list(`${root}/${TASKS}`, 'dir')), input.name]
|
|
224
|
+
const problem = checkTask(task) ?? checkTaskList(list, folders, archived)
|
|
225
|
+
if (problem !== null) return { deny: `Nothing was written. ${problem}` }
|
|
226
|
+
|
|
227
|
+
await store.write(`${folder}/${NAMES.task}`, task.task ?? '')
|
|
228
|
+
await store.write(`${folder}/${NAMES.track}`, task.track ?? '')
|
|
229
|
+
await store.write(listPath, list)
|
|
230
|
+
|
|
231
|
+
return { result: OPENED(input.name, formatTaskLine(input.status, input.name, input.name, input.about), task.track ?? '') }
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function renderTask(input: OpenInput): string {
|
|
235
|
+
const body: Record<string, string> = { Goal: input.goal, 'Done when': input.doneWhen }
|
|
236
|
+
|
|
237
|
+
return `# ${input.title}\n${TASK_SECTIONS.map(section => `\n## ${section}\n\n${body[section] ?? 'none'}\n`).join('')}`
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export async function archiveTask(project: Project, input: ArchiveInput): Promise<{ deny: string } | { result: string }> {
|
|
241
|
+
const { store, root } = project
|
|
242
|
+
const from = `${root}/${TASKS}/${input.name}`
|
|
243
|
+
const to = `${root}/${ARCHIVE}/${input.name}`
|
|
244
|
+
const task = await readTask(store, `${root}/${DIR}`, input.name)
|
|
245
|
+
const problem = checkArchive(input, task, await store.exists(to))
|
|
246
|
+
if (problem !== null) return { deny: problem }
|
|
247
|
+
|
|
248
|
+
const listPath = `${root}/${TASK_LIST}`
|
|
249
|
+
const list = await store.read(listPath)
|
|
250
|
+
const listed = list === null ? [] : parseTaskList(list)
|
|
251
|
+
const entry = typeof listed === 'string' ? undefined : listed.find(t => t.name === input.name)
|
|
252
|
+
const continued = (entry?.links ?? []).filter(link => link.kind === 'continues').map(link => link.name)
|
|
253
|
+
|
|
254
|
+
const failed = await store.move(from, to)
|
|
255
|
+
if (failed !== null) return { deny: `Could not move ${from} to ${to}: ${failed}` }
|
|
256
|
+
const index = await store.read(`${root}/${INDEX}`)
|
|
257
|
+
await store.write(`${root}/${INDEX}`, addToIndex(index, indexLine(input, task.task ?? '', continued)))
|
|
258
|
+
if (list !== null) await store.write(listPath, archiveInList(list, input.name))
|
|
259
|
+
|
|
260
|
+
return { result: ARCHIVED(input.name) }
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
export async function shiftHistory(project: Project, count: number): Promise<string> {
|
|
264
|
+
const { entries, total } = await lastShifts(project, count)
|
|
265
|
+
|
|
266
|
+
return renderHistory(entries, total)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// The last shift picked up in a session that has not seen it: the text for the owner, and the prompt when there is one.
|
|
270
|
+
export async function resumeShift(project: Project): Promise<{ text: string; prompt?: string }> {
|
|
271
|
+
const dir = await previousShift(project)
|
|
272
|
+
if (dir === null) return { text: `No shifts in this project: ${SHIFTS}/ is empty.` }
|
|
273
|
+
const letter = await project.store.read(`${dir}/${NAMES.letter}`)
|
|
274
|
+
if (letter === null) return { text: `No letter.md in ${dir}/ — nothing to pick up.` }
|
|
275
|
+
|
|
276
|
+
return { text: `Picking up shift ${basename(dir)}.`, prompt: resumePrompt(letter, await handBack(project, dir)) }
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// The records, the last shifts and the documents' remarks as they are now, for the message after a shift.
|
|
280
|
+
async function handBack(project: Project, dir: string): Promise<HandedBack> {
|
|
281
|
+
return {
|
|
282
|
+
work: await readRecords(project),
|
|
283
|
+
knowledge: project.settings.knowledge,
|
|
284
|
+
owner: project.settings.files.owner,
|
|
285
|
+
recent: await recentShifts(project),
|
|
286
|
+
dir,
|
|
287
|
+
remarks: await docsReport(project),
|
|
288
|
+
stuck: await stuckTasks(project),
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// The listed tasks whose task.md and track.md are the same in the latest shifts' snapshots as now, through at least
|
|
293
|
+
// STUCK_SHIFTS of them: counted from the snapshots, since the records carry no dates.
|
|
294
|
+
export async function stuckTasks(project: Project): Promise<Stuck[]> {
|
|
295
|
+
const { store, root } = project
|
|
296
|
+
const work = await readRecords(project)
|
|
297
|
+
if (work === null) return []
|
|
298
|
+
const shifts = (await shiftNames(project)).slice(-STUCK_LOOKBACK).reverse()
|
|
299
|
+
const stuck: Stuck[] = []
|
|
300
|
+
for (const listed of listedTasks(work)) {
|
|
301
|
+
const now = work.tasks.find(task => task.name === listed.name)
|
|
302
|
+
if (now === undefined) continue
|
|
303
|
+
let count = 0
|
|
304
|
+
for (const name of shifts) {
|
|
305
|
+
const base = `${root}/${SHIFTS}/${name}`
|
|
306
|
+
const same =
|
|
307
|
+
(await store.read(taskFile(base, listed.name, 'task'))) === now.task &&
|
|
308
|
+
(await store.read(taskFile(base, listed.name, 'track'))) === now.track
|
|
309
|
+
if (!same) break
|
|
310
|
+
count++
|
|
311
|
+
}
|
|
312
|
+
if (count >= STUCK_SHIFTS) stuck.push({ name: listed.name, status: listed.status, shifts: count })
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return stuck
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// The harness did not let the history go: about.md says why, the row tells the owner — even when about.md fails.
|
|
319
|
+
export async function shiftNotMade(project: Project, shift: PendingShift, why: string, tokensBefore: number | undefined): Promise<string> {
|
|
320
|
+
const row = NOT_MADE(basename(shift.dir), why)
|
|
321
|
+
|
|
322
|
+
return recordFailure(project, shift, renderAbout({ ...shift, tokensBefore, result: OUTCOME.notShifted(why) }), row)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// The harness let the history go, but what follows failed: about.md and the row say so, the letter stays on disk.
|
|
326
|
+
export async function shiftUnsettled(
|
|
327
|
+
project: Project,
|
|
328
|
+
shift: PendingShift,
|
|
329
|
+
why: string,
|
|
330
|
+
tokensBefore: number | undefined,
|
|
331
|
+
): Promise<string> {
|
|
332
|
+
const row = UNSETTLED(basename(shift.dir), why)
|
|
333
|
+
|
|
334
|
+
return recordFailure(project, shift, renderAbout({ ...shift, tokensBefore, result: OUTCOME.unsettled(why) }), row)
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// A failure's about.md, written before the row is told; a failed write still leaves the row.
|
|
338
|
+
async function recordFailure(project: Project, shift: PendingShift, about: string, row: string): Promise<string> {
|
|
339
|
+
try {
|
|
340
|
+
await project.store.write(`${shift.dir}/${NAMES.about}`, about)
|
|
341
|
+
|
|
342
|
+
return row
|
|
343
|
+
} catch (error) {
|
|
344
|
+
return `${row} (and ${NAMES.about} was not written: ${String(error)})`
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export type LetGo = {
|
|
349
|
+
shift: PendingShift
|
|
350
|
+
// What the history was let go into, as the harness gives it back.
|
|
351
|
+
messages: readonly { role: string; text: string }[]
|
|
352
|
+
before: number | undefined
|
|
353
|
+
after: number
|
|
354
|
+
// `continue` shifts in a row without the owner before this one.
|
|
355
|
+
streak: number
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// After the let-go: the summary and the outcome on disk, then continue, or wait — asked for, or forced after
|
|
359
|
+
// `MAX_AUTO_STREAK` continues in a row without the owner.
|
|
360
|
+
export async function settleShift(project: Project, letGo: LetGo): Promise<Settled> {
|
|
361
|
+
const { store } = project
|
|
362
|
+
const { shift, before, after } = letGo
|
|
363
|
+
const summary = renderSummary(letGo.messages)
|
|
364
|
+
await store.write(`${shift.dir}/${NAMES.summary}`, summary)
|
|
365
|
+
const streak = letGo.streak + 1
|
|
366
|
+
const isCapped = shift.after === 'continue' && streak > MAX_AUTO_STREAK
|
|
367
|
+
const shrank = `${formatK(before ?? 0)} → ${formatK(after)}`
|
|
368
|
+
const outcome = (result: string) =>
|
|
369
|
+
store.write(
|
|
370
|
+
`${shift.dir}/${NAMES.about}`,
|
|
371
|
+
renderAbout({ ...shift, tokensBefore: before, tokensAfter: after, summaryChars: summary.length, result }),
|
|
372
|
+
)
|
|
373
|
+
const back = await handBack(project, shift.dir)
|
|
374
|
+
const name = basename(shift.dir)
|
|
375
|
+
|
|
376
|
+
if (shift.after === 'continue' && !isCapped) {
|
|
377
|
+
await outcome(OUTCOME.continuing(shrank))
|
|
378
|
+
|
|
379
|
+
return {
|
|
380
|
+
after: 'continue',
|
|
381
|
+
streak,
|
|
382
|
+
prompt: continuePrompt(shift.letter, back),
|
|
383
|
+
row: CONTINUING(name, shrank, shift.dir),
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const why = isCapped ? `waiting: ${MAX_AUTO_STREAK} shifts in a row without the owner` : 'waiting for the owner'
|
|
388
|
+
await outcome(OUTCOME.waiting(shrank, why))
|
|
389
|
+
const note = { context: waitContext(shift.letter, back), dir: shift.dir, files: deliveredFiles(back.work) }
|
|
390
|
+
|
|
391
|
+
return { after: 'wait', note, row: WAITING(name, shrank, isCapped) }
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
// The fill the stretch started from, if the last shift was this session's: a fresh process counts from there.
|
|
395
|
+
export async function shiftBase(project: Project, sessionId: string): Promise<number | null> {
|
|
396
|
+
const dir = await previousShift(project)
|
|
397
|
+
const about = dir === null ? null : await project.store.read(`${dir}/${NAMES.about}`)
|
|
398
|
+
|
|
399
|
+
return about === null ? null : shiftFill(about, sessionId)
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// Unless the owner lets the project commit them, the records stay out of its git: when they are not ignored, the mod
|
|
403
|
+
// adds them to the repository's own exclude file. The exclude file's path when it did; null when nothing was needed.
|
|
404
|
+
export async function keepRecordsOut(project: Project): Promise<{ excluded: string | null } | { deny: string }> {
|
|
405
|
+
const { store, root } = project
|
|
406
|
+
if (project.settings.commit) return { excluded: null }
|
|
407
|
+
// 1 — not ignored; any other answer, or no git at all, means nothing to keep the records out of.
|
|
408
|
+
const ignored = await store.git(root, ['check-ignore', '-q', DIR])
|
|
409
|
+
if (ignored === null || ignored.code !== 1) return { excluded: null }
|
|
410
|
+
// git names it relative to the root, or absolute in a linked worktree.
|
|
411
|
+
const found = await store.git(root, ['rev-parse', '--git-path', 'info/exclude'])
|
|
412
|
+
const path = found === null || found.code !== 0 ? '' : found.stdout.trim()
|
|
413
|
+
if (path === '') return { deny: NOT_IGNORED }
|
|
414
|
+
const file = path.startsWith('/') ? path : `${root}/${path}`
|
|
415
|
+
const text = (await store.read(file)) ?? ''
|
|
416
|
+
await store.write(file, `${text}${text === '' || text.endsWith('\n') ? '' : '\n'}${DIR}/\n`)
|
|
417
|
+
|
|
418
|
+
return { excluded: file }
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// What comes into every session: SHIFT.md with a fresh map of the docs, OWNER.md, GLOSSARY.md and TASKS.md, with
|
|
422
|
+
// paths from the project's root; none when the mod is off here.
|
|
423
|
+
export async function sessionFiles(project: Project): Promise<SessionFile[]> {
|
|
424
|
+
const { store, root, settings } = project
|
|
425
|
+
if (!settings.enabled || (await staleLayout(project)) !== null) return []
|
|
426
|
+
await refreshDocs(project)
|
|
427
|
+
const paths = [settings.files.shift, settings.files.owner, settings.files.glossary, TASK_LIST, ...settings.files.always]
|
|
428
|
+
const files = await Promise.all(paths.map(async path => ({ path, text: await store.read(`${root}/${path}`) })))
|
|
429
|
+
|
|
430
|
+
return files.filter((file): file is SessionFile => file.text !== null)
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// Records the mod cannot work on yet: an older name of its folder, or the folder the shift skill left. Then the mod writes
|
|
434
|
+
// nothing — a map appended to the skill's own SHIFT.md would come into every session — and says what to move.
|
|
435
|
+
export async function staleLayout({ store, root }: Project): Promise<string | null> {
|
|
436
|
+
if (await store.exists(`${root}/${OLD_DIR}`)) {
|
|
437
|
+
return (await store.exists(`${root}/${DIR}`))
|
|
438
|
+
? `Both ${OLD_DIR}/ and ${DIR}/ are here: the records live in ${DIR}/ now. Merge what ${OLD_DIR}/ holds into ${DIR}/ by ${GUIDE_TOOL}, then remove ${OLD_DIR}/.`
|
|
439
|
+
: `The records are in ${OLD_DIR}/, the mod's old name: rename the folder to ${DIR}/ as it is, and call again.`
|
|
440
|
+
}
|
|
441
|
+
if ((await store.exists(`${root}/${DIR}/sessions`)) && !(await store.exists(`${root}/${BOARD}`))) {
|
|
442
|
+
return `${DIR}/ here is the shift skill's: it has sessions/ and no board. Move it in place by "Coming from the shift skill" in ${GUIDE_TOOL}, then call again.`
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
return null
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// What /shift-tasks shows: the open tasks as listed, with their items' count, and the items not closed of those in
|
|
449
|
+
// progress. Read only; a list the shift would refuse is shown as far as it parses.
|
|
450
|
+
export async function tasksView(project: Project): Promise<string> {
|
|
451
|
+
const { store, root } = project
|
|
452
|
+
const parsed = parseTaskList((await store.read(`${root}/${TASK_LIST}`)) ?? '')
|
|
453
|
+
if (typeof parsed === 'string') return `${TASK_LIST}: ${parsed}`
|
|
454
|
+
const views: TaskView[] = []
|
|
455
|
+
for (const task of parsed) {
|
|
456
|
+
const track = (await store.read(taskFile(`${root}/${DIR}`, task.name, 'track'))) ?? ''
|
|
457
|
+
const items = itemsOf(track).filter(item => item.status !== '')
|
|
458
|
+
const open = items.filter(item => !CLOSED.has(item.status))
|
|
459
|
+
views.push({
|
|
460
|
+
status: task.status,
|
|
461
|
+
name: task.name,
|
|
462
|
+
about: task.about,
|
|
463
|
+
items:
|
|
464
|
+
task.status === 'in progress'
|
|
465
|
+
? open.map(item => ({ status: item.status, name: item.id === null ? item.name : `#${item.id} ${item.name}` }))
|
|
466
|
+
: null,
|
|
467
|
+
closed: items.length - open.length,
|
|
468
|
+
total: items.length,
|
|
469
|
+
})
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
return renderTasks(views)
|
|
473
|
+
}
|
package/core/glossary.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Generated from core/src by scripts/sync-core.ts — edit the source, not this copy.
|
|
2
|
+
import { GLOSSARY_MAX_LINES } from './layout'
|
|
3
|
+
import { blocks, dated, overLimit } from './markdown'
|
|
4
|
+
|
|
5
|
+
// GLOSSARY.md: the project's own words, one article each, loaded whole into every session.
|
|
6
|
+
//
|
|
7
|
+
// ## Shift
|
|
8
|
+
// Variants: shifts, shifted
|
|
9
|
+
// Means: the boundary between stretches of work: records updated, a letter written, the history let go.
|
|
10
|
+
// Avoid: compaction, handoff
|
|
11
|
+
// Where: everywhere the agent reads; the owner says «сдвиг».
|
|
12
|
+
// Unconfirmed: is a stretch the same as a session?
|
|
13
|
+
|
|
14
|
+
export const GLOSSARY_LABELS = ['Variants', 'Means', 'Avoid', 'Where', 'Unconfirmed'] as const
|
|
15
|
+
export type GlossaryLabel = (typeof GLOSSARY_LABELS)[number]
|
|
16
|
+
export type Article = { term: string; fields: Partial<Record<GlossaryLabel, string>> }
|
|
17
|
+
|
|
18
|
+
// The articles, or what is wrong with a line; text before the first article is the file's own heading and intro.
|
|
19
|
+
export function parseGlossary(text: string, where: string): Article[] | string {
|
|
20
|
+
const articles: Article[] = []
|
|
21
|
+
for (const block of blocks(text, 2)) {
|
|
22
|
+
const article: Article = { term: block.title, fields: {} }
|
|
23
|
+
articles.push(article)
|
|
24
|
+
for (const line of block.body.split('\n').map(each => each.trim())) {
|
|
25
|
+
if (line === '') continue
|
|
26
|
+
const signed = /^([A-Za-z]+): *(.*)$/.exec(line)
|
|
27
|
+
const label = signed?.[1] as GlossaryLabel | undefined
|
|
28
|
+
if (signed === null || label === undefined || !(GLOSSARY_LABELS as readonly string[]).includes(label)) {
|
|
29
|
+
return `${where}, "${article.term}": "${line}" is not a signed line; the labels are ${GLOSSARY_LABELS.join(', ')}.`
|
|
30
|
+
}
|
|
31
|
+
if (article.fields[label] !== undefined) return `${where}, "${article.term}": "${label}:" twice; one line each.`
|
|
32
|
+
article.fields[label] = (signed[2] ?? '').trim()
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return articles
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function checkGlossary(text: string | null, where: string): string | null {
|
|
40
|
+
if (text === null) return null
|
|
41
|
+
const size =
|
|
42
|
+
overLimit(where, text, GLOSSARY_MAX_LINES, 'it comes into every session. Drop general words and terms no longer in use.') ??
|
|
43
|
+
dated(where, text, 'A term means what it means now.')
|
|
44
|
+
if (size !== null) return size
|
|
45
|
+
const articles = parseGlossary(text, where)
|
|
46
|
+
if (typeof articles === 'string') return articles
|
|
47
|
+
|
|
48
|
+
const seen = new Map<string, string>()
|
|
49
|
+
for (const article of articles) {
|
|
50
|
+
if (!article.fields.Means) return `${where}, "${article.term}" has no "Means:" — what it is, in one or two sentences.`
|
|
51
|
+
for (const name of [article.term, ...words(article.fields.Variants)]) {
|
|
52
|
+
const key = name.toLowerCase()
|
|
53
|
+
const earlier = seen.get(key)
|
|
54
|
+
if (earlier !== undefined) return `${where}: "${name}" is in both "${earlier}" and "${article.term}". One word, one article.`
|
|
55
|
+
seen.set(key, article.term)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
for (const article of articles) {
|
|
59
|
+
for (const avoided of words(article.fields.Avoid)) {
|
|
60
|
+
const owner = seen.get(avoided.toLowerCase())
|
|
61
|
+
if (owner !== undefined) {
|
|
62
|
+
return `${where}: "${article.term}" says to avoid "${avoided}", but "${owner}" uses that word. Keep one of them.`
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return null
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function words(list: string | undefined): string[] {
|
|
71
|
+
return (list ?? '')
|
|
72
|
+
.split(',')
|
|
73
|
+
.map(word => word.trim())
|
|
74
|
+
.filter(word => word !== '')
|
|
75
|
+
}
|