heyquilt 0.3.170 → 0.3.172
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/RELEASES.md +30 -0
- package/bin/quilt.js +16 -9
- package/package.json +2 -2
- package/src/agent-task-workflow.js +2 -9
- package/src/base-pick.js +212 -0
- package/src/boxes.js +6 -7
- package/src/catchup.js +1 -1
- package/src/chat-links.js +1 -3
- package/src/connection.js +24 -5
- package/src/control.js +27 -20
- package/src/duties.js +32 -56
- package/src/github-commit.js +1 -1
- package/src/held.js +323 -0
- package/src/hooks.js +27 -66
- package/src/identity.js +1 -1
- package/src/inbox.js +5 -4
- package/src/key-cli.js +1 -1
- package/src/live-merge.js +599 -0
- package/src/mcp.js +57 -102
- package/src/merge3.js +65 -1
- package/src/now-summary.js +4 -6
- package/src/pathrules.js +1 -1
- package/src/persona.js +1 -1
- package/src/presence.js +2 -0
- package/src/protocol.js +2 -2
- package/src/read-bases.js +123 -0
- package/src/relay-ask.js +2 -2
- package/src/relay-live-write.js +179 -0
- package/src/relay-mcp.js +173 -212
- package/src/relay-prune.js +2 -2
- package/src/relay-turn-sync.js +146 -142
- package/src/runner.js +0 -2
- package/src/server.js +253 -375
- package/src/session-follow.js +3 -1
- package/src/session-quilt.js +2 -21
- package/src/session-seam.js +35 -7
- package/src/session.js +795 -553
- package/src/setup.js +15 -25
- package/src/source-live/bring-in.js +5 -5
- package/src/source-live/land.js +20 -3
- package/src/source-live/live-block.js +503 -52
- package/src/status.js +8 -12
- package/src/swap-in.js +81 -0
- package/src/textdiff.js +54 -0
- package/src/turn-sync-plan.js +6 -18
- package/src/turn-sync.js +28 -21
- package/src/ui/agent-guide.js +13 -13
- package/src/ui/app.css +19 -40
- package/src/ui/boxes.js +8 -16
- package/src/ui/common.js +1 -1
- package/src/ui/fileview.js +11 -15
- package/src/ui/loom-model.js +18 -16
- package/src/ui/loom.css +1 -2
- package/src/ui/loom.js +6 -6
- package/src/ui/rail-model.js +25 -64
- package/src/ui/rail.js +48 -60
- package/src/ui/seam-model.js +39 -3
- package/src/ui/session.js +36 -54
- package/src/ui/tree.js +22 -129
- package/src/ui-server.js +2 -6
- package/src/release-words.js +0 -18
- package/src/seam-claims.js +0 -53
package/src/duties.js
CHANGED
|
@@ -2,16 +2,15 @@
|
|
|
2
2
|
// whatever tool it runs in. Pure checks shared by the local MCP, the hosted MCP and the
|
|
3
3
|
// Claude Code hooks, so every agent gets the same rules:
|
|
4
4
|
//
|
|
5
|
-
// - before editing a file: who
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
// - before
|
|
10
|
-
//
|
|
11
|
-
// asked for in its queue (quilt_request_file), not taken.
|
|
5
|
+
// - before editing a file: who wrote it lately (presence) and what was said about it in
|
|
6
|
+
// chat, as context. Nothing holds a file (no-holds, 2026-11-13): no claims, no file
|
|
7
|
+
// queue, no handoffs of files. Edits merge as they arrive; a clash on the same lines
|
|
8
|
+
// goes back to the writer to resolve;
|
|
9
|
+
// - before moving work on (tasks, commits, "done"): every direct message and mention is
|
|
10
|
+
// answered (a message back to that person, or to everyone, after theirs).
|
|
12
11
|
//
|
|
13
|
-
// Chat never blocks a file: Quilt can't tell "don't touch it" from "is it done?". A
|
|
14
|
-
//
|
|
12
|
+
// Chat never blocks a file: Quilt can't tell "don't touch it" from "is it done?". A message
|
|
13
|
+
// about a file is something the agent reads.
|
|
15
14
|
//
|
|
16
15
|
// Messages are chat entries ({ id, by, to, text, ts }) the reader can see.
|
|
17
16
|
import { mentionsMe, mentioned, ALL_AGENTS } from './inbox.js'
|
|
@@ -217,7 +216,7 @@ export function waitingOn (messages, me, { now = Date.now(), windowMs = REQUEST_
|
|
|
217
216
|
for (const m of messages || []) {
|
|
218
217
|
if (!m || !m.by || names.includes(m.by) || typeof m.text !== 'string' || (m.ts || 0) < now - windowMs) continue
|
|
219
218
|
if (settled && settled.has(m.id)) continue // needs no reply (quilt_inbox no_reply)
|
|
220
|
-
if (fileQueueMessage(m)) continue // a
|
|
219
|
+
if (fileQueueMessage(m)) continue // a note from Quilt, or an old file queue request or handoff: no reply needed
|
|
221
220
|
if (m.kind === 'commit') continue // a commit request told to the owner: handled by committing (quilt_commit_request_done)
|
|
222
221
|
if (needsNoReply(m.text)) continue // a goodnight, a greeting, thanks: asks nothing
|
|
223
222
|
const kind = names.includes(m.to) ? 'dm' : !m.to && names.some((n) => mentionsMe(m.text, n, { agent })) ? 'mention' : null
|
|
@@ -227,8 +226,9 @@ export function waitingOn (messages, me, { now = Date.now(), windowMs = REQUEST_
|
|
|
227
226
|
}
|
|
228
227
|
|
|
229
228
|
/**
|
|
230
|
-
* A message that needs no reply:
|
|
231
|
-
*
|
|
229
|
+
* A message that needs no reply: a note from Quilt (nudge), a note that work someone asked to commit
|
|
230
|
+
* was committed (commit.js), or one the relay wrote for the file queue before Quilt stopped holding
|
|
231
|
+
* files (queue, handoff: still in older chat).
|
|
232
232
|
*/
|
|
233
233
|
export const fileQueueMessage = (m) => !!m && (m.kind === 'queue' || m.kind === 'handoff' || m.kind === 'commit' || m.kind === 'nudge')
|
|
234
234
|
|
|
@@ -237,30 +237,8 @@ export function unanswered (events, { messages = [], me } = {}) {
|
|
|
237
237
|
return (events || []).filter((e) => e && (e.kind === 'dm' || e.kind === 'mention') && !e.queue && !e.commit && !e.nudge && e.by !== me && !needsNoReply(e.text) && !answered(messages, me, e.by, e.ts))
|
|
238
238
|
}
|
|
239
239
|
|
|
240
|
-
/**
|
|
241
|
-
|
|
242
|
-
* description, task }] }], from the relay's claim list.
|
|
243
|
-
*/
|
|
244
|
-
export function queuedFor (claims, me) {
|
|
245
|
-
return (claims || []).filter((c) => c && c.by === me && Array.isArray(c.queue) && c.queue.length).map((c) => ({ pattern: c.pattern, queue: c.queue }))
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
const requestLine = (r) => `${r.by}${r.path ? ` (for ${r.path})` : ''}: "${quote(r.title)}"${r.description ? ` — ${quote(r.description)}` : ''}${r.task ? ` (task ${r.task})` : ''}`
|
|
249
|
-
|
|
250
|
-
/** Who is waiting for the files `me` holds, as lines an agent reads with every answer; '' when nobody is. */
|
|
251
|
-
export function renderQueueNotice (held) {
|
|
252
|
-
if (!held || !held.length) return ''
|
|
253
|
-
const lines = held.map((h) => `- ${h.pattern}: ${h.queue.map(requestLine).join('; then ')}`)
|
|
254
|
-
return 'Waiting in the file queue for files you hold:\n' + lines.join('\n') + '\n' +
|
|
255
|
-
'Finish the change you are making to each, then hand it off with quilt_handoff (path, and context: what you changed, what is left, anything they should know). ' +
|
|
256
|
-
'You cannot finish or release these files until you do.'
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/** Why an agent may not finish (or let go of files) yet: files it holds that someone is waiting for. '' when none. */
|
|
260
|
-
export function renderQueued (held, then = 'finish again') {
|
|
261
|
-
if (!held || !held.length) return ''
|
|
262
|
-
return 'Not yet: ' + renderQueueNotice(held).replace(/^Waiting/, 'people are waiting') + ` Then ${then}.`
|
|
263
|
-
}
|
|
240
|
+
/** What the claim tools (kept one release for older agents) answer: nothing is held, and they change nothing. */
|
|
241
|
+
export const NO_HOLDS = 'Quilt holds no files: nothing to claim. Edit any file you may change: edits merge as they arrive, and if your change clashes with someone\'s on the same lines, Quilt tells you and you resolve it.'
|
|
264
242
|
|
|
265
243
|
const quote = (t) => {
|
|
266
244
|
const s = String(t || '').replace(/\s+/g, ' ').trim()
|
|
@@ -282,9 +260,26 @@ export function renderChatAbout (said, { now = Date.now(), max = 8 } = {}) {
|
|
|
282
260
|
'Take it into account: if they asked you to leave a file alone or to change it a certain way, do that, and reply to anyone you have not replied to with quilt_message.'
|
|
283
261
|
}
|
|
284
262
|
|
|
263
|
+
/**
|
|
264
|
+
* Who else wrote the files an agent is about to change lately, and whose unfinished work is boxed on
|
|
265
|
+
* them (quilt_before_edit's `files`: [{ path, recent: [{ by, at }], box?: { by } }]). Presence, never a
|
|
266
|
+
* hold: '' when nobody.
|
|
267
|
+
*/
|
|
268
|
+
export function renderRecent (files, { now = Date.now() } = {}) {
|
|
269
|
+
const lines = []
|
|
270
|
+
for (const f of files || []) {
|
|
271
|
+
if (!f || !f.path) continue
|
|
272
|
+
const who = (f.recent || []).filter((w) => w && w.by).map((w) => `${w.by} ${ago(w.at, now)}`)
|
|
273
|
+
if (who.length) lines.push(`- ${f.path}: changed lately by ${who.join(', ')}`)
|
|
274
|
+
if (f.box && f.box.by) lines.push(`- ${f.path}: ${f.box.by}'s unfinished work is boxed on it; your edit opens the box and merges with it`)
|
|
275
|
+
}
|
|
276
|
+
if (!lines.length) return ''
|
|
277
|
+
return 'Others working on these files (nobody holds files: edit them; changes merge as they arrive, and a clash on the same lines comes back to you to resolve):\n' + lines.join('\n')
|
|
278
|
+
}
|
|
279
|
+
|
|
285
280
|
/**
|
|
286
281
|
* Why an agent may not go on yet: the messages it still owes an answer, or '' when it owes none.
|
|
287
|
-
* `then` says what to do once they're answered ("finish again", "
|
|
282
|
+
* `then` says what to do once they're answered ("finish again", "move the task again").
|
|
288
283
|
*/
|
|
289
284
|
export function renderUnanswered (events, then = 'finish again') {
|
|
290
285
|
if (!events || !events.length) return ''
|
|
@@ -294,22 +289,3 @@ export function renderUnanswered (events, then = 'finish again') {
|
|
|
294
289
|
`One that needs nothing back (thanks, a greeting, an FYI, a status report) gets no reply: settle it with quilt_inbox (no_reply: [its id]). Then ${then}. ` +
|
|
295
290
|
'Reading, messaging and checking files work meanwhile.'
|
|
296
291
|
}
|
|
297
|
-
|
|
298
|
-
/** One refusal for an edit to a file someone else holds: who, and what to do instead of retrying. */
|
|
299
|
-
export function heldRefusal (rel, claim, error) {
|
|
300
|
-
const holder = claim ? claim.by : 'someone else'
|
|
301
|
-
const why = claim ? (claim.note ? ` (${claim.note})` : '') : error ? ` (${error})` : ''
|
|
302
|
-
// A box (docs/superpowers/specs/2026-10-16-git-boxes-design.md): their work isn't in this folder yet.
|
|
303
|
-
if (claim && claim.box) return `${rel} is boxed by ${holder}${why}: their unfinished work, which isn't in your folder until they commit or unpack it. Quilt refuses edits to it and would undo them. ${askForIt(rel, claim)}`
|
|
304
|
-
const covered = claim && claim.pattern && claim.pattern !== rel ? `, as part of their claim on ${claim.pattern}` : ''
|
|
305
|
-
return `${rel} is claimed by ${holder}${why}${covered}, so Quilt refuses edits to it and would undo them. ${askForIt(rel, claim)}`
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/** What to do about a file someone else holds: ask for it in its file queue, and carry on. */
|
|
309
|
-
export function askForIt (rel, claim) {
|
|
310
|
-
const holder = claim ? claim.by : 'its holder'
|
|
311
|
-
const waiting = claim && Array.isArray(claim.queue) && claim.queue.length ? ` ${claim.queue.length} already waiting for it.` : ''
|
|
312
|
-
return `Do not retry or work around it.${waiting} ` +
|
|
313
|
-
`Ask for it in its file queue with quilt_request_file (path "${rel}", a title like "Working on <what> for <task>", and a description of up to 300 characters: what you plan to change and why). ` +
|
|
314
|
-
`${holder} is told, and hands it to you with their context when they are done; you are woken when it is yours. Carry on with other work meanwhile.`
|
|
315
|
-
}
|
package/src/github-commit.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// R3 removed GitHub commits; only this pure helper stays, because test/unit/history.test.js (
|
|
1
|
+
// R3 removed GitHub commits; only this pure helper stays, because test/unit/history.test.js (in
|
|
2
2
|
// another square, sq:09a100d0) still imports it from here. Delete this file with that test's import.
|
|
3
3
|
import { blameChange } from './history.js'
|
|
4
4
|
|
package/src/held.js
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
// Held conflicts (Seam's plans/quilt-source-control/07 "Conflicts in the live state", ruling
|
|
2
|
+
// 2026-11-13): a live write whose lines clashed with what had landed first. The shared file keeps
|
|
3
|
+
// the first-landed version; the writer's clashing lines are kept here, in the room's document, so
|
|
4
|
+
// every member's app and every AI (local MCP, hosted MCP, quilt_status) sees each open one until
|
|
5
|
+
// someone resolves it. Nothing holds the file meanwhile. One map for the whole session, whatever
|
|
6
|
+
// branch or square the file is on, because the room's document is the one every member has
|
|
7
|
+
// (a branch document is read-only once the session is in Seam, and a square's live document only
|
|
8
|
+
// reaches the members on that square).
|
|
9
|
+
import crypto from 'node:crypto'
|
|
10
|
+
import { isSafeRelPath } from './pathrules.js'
|
|
11
|
+
|
|
12
|
+
export const HELD_MAP = 'held'
|
|
13
|
+
export const KINDS = ['lines', 'stale', 'parse', 'binary']
|
|
14
|
+
export const HOWS = ['theirs', 'mine', 'edited']
|
|
15
|
+
export const MAX_TEXT = 64 * 1024 // each of a record's texts; past it the texts stay on the writer's machine (`local`)
|
|
16
|
+
export const MAX_HUNKS = 20
|
|
17
|
+
export const MAX_HUNK_LINES = 40
|
|
18
|
+
export const MAX_HELD = 60
|
|
19
|
+
export const DONE_TTL_MS = 24 * 60 * 60 * 1000
|
|
20
|
+
// Seam's 07 setting `stale_write_minutes`: a write that removes lines someone else added this recently is held, not taken.
|
|
21
|
+
export const STALE_WRITE_MINUTES = 15
|
|
22
|
+
export const SETTINGS_MAP = 'mergeSettings'
|
|
23
|
+
// Seam's 07 "Who is asked to resolve, and the escalation budget" (ruling 2026-10-30): an open record
|
|
24
|
+
// nobody resolved in ESCALATE_MS tells its writer's person (an agent's: the person it works for); at
|
|
25
|
+
// ADMIN_MS the session's owner is told too. Each once per record (`flagged4`, `flagged24`). The relay
|
|
26
|
+
// does it (server.js sweepHeld), so it happens whatever AI tool wrote it and with nobody's app open.
|
|
27
|
+
export const ESCALATE_MS = 4 * 60 * 60 * 1000
|
|
28
|
+
export const ADMIN_MS = 24 * 60 * 60 * 1000
|
|
29
|
+
// The two-attempt budget: an attempt is a resolution ("mine" or "edited") whose resolver's next write on
|
|
30
|
+
// that path, within ATTEMPT_WINDOW_MS, is held again. The record that would need a third becomes a task
|
|
31
|
+
// on the board for the writer's person, and every AI is told to leave it to them.
|
|
32
|
+
export const ATTEMPT_BUDGET = 2
|
|
33
|
+
export const ATTEMPT_WINDOW_MS = 2 * 60 * 60 * 1000
|
|
34
|
+
|
|
35
|
+
const HEX_ID = /^[0-9a-f]{16}$/
|
|
36
|
+
// eslint-disable-next-line no-control-regex
|
|
37
|
+
const CONTROL = /[\u0000-\u001f\u007f]+/g
|
|
38
|
+
const stamp = (v) => (typeof v === 'number' && Number.isFinite(v) && v > 0 ? v : null)
|
|
39
|
+
const flat = (s, max) => String(s == null ? '' : s).replace(CONTROL, ' ').replace(/\s+/g, ' ').trim().slice(0, max)
|
|
40
|
+
const capLines = (ls) => (Array.isArray(ls) ? ls : []).slice(0, MAX_HUNK_LINES).map((l) => String(l).slice(0, 500))
|
|
41
|
+
|
|
42
|
+
/** A record as readers show it, or null for anything a modified client pushed that is not this shape. */
|
|
43
|
+
export function publicHeld (v) {
|
|
44
|
+
if (!v || typeof v !== 'object' || typeof v.id !== 'string' || !HEX_ID.test(v.id)) return null
|
|
45
|
+
if (typeof v.path !== 'string' || v.path.length > 1024 || !isSafeRelPath(v.path)) return null
|
|
46
|
+
if (!KINDS.includes(v.kind) || (v.state !== 'open' && v.state !== 'resolved')) return null
|
|
47
|
+
if (typeof v.by !== 'string' || !v.by || typeof v.ts !== 'number') return null
|
|
48
|
+
if (v.how != null && !HOWS.includes(v.how)) return null
|
|
49
|
+
return {
|
|
50
|
+
id: v.id,
|
|
51
|
+
path: v.path,
|
|
52
|
+
kind: v.kind,
|
|
53
|
+
state: v.state,
|
|
54
|
+
by: flat(v.by, 80),
|
|
55
|
+
byId: typeof v.byId === 'string' ? v.byId.slice(0, 128) : null,
|
|
56
|
+
others: Array.isArray(v.others) ? v.others.filter((n) => typeof n === 'string').slice(0, 10).map((n) => flat(n, 80)) : [],
|
|
57
|
+
where: v.where && typeof v.where === 'object' ? { ...(typeof v.where.branch === 'string' ? { branch: v.where.branch.slice(0, 200) } : {}), ...(typeof v.where.square === 'string' ? { square: v.where.square.slice(0, 64) } : {}) } : {},
|
|
58
|
+
ts: v.ts,
|
|
59
|
+
hunks: Array.isArray(v.hunks) ? v.hunks.slice(0, MAX_HUNKS).map((h) => ({ at: Number(h && h.at) || 0, base: capLines(h && h.base), ours: capLines(h && h.ours), theirs: capLines(h && h.theirs) })) : [],
|
|
60
|
+
more: Number(v.more) || 0,
|
|
61
|
+
lines: Array.isArray(v.lines) ? v.lines.filter((l) => typeof l === 'string').slice(0, 50) : [],
|
|
62
|
+
reason: flat(v.reason, 500),
|
|
63
|
+
result: typeof v.result === 'string' ? v.result : null,
|
|
64
|
+
mine: typeof v.mine === 'string' ? v.mine : null,
|
|
65
|
+
local: !!v.local,
|
|
66
|
+
how: v.how ?? null,
|
|
67
|
+
resolvedBy: v.resolvedBy ? flat(v.resolvedBy, 80) : null,
|
|
68
|
+
resolvedTs: typeof v.resolvedTs === 'number' ? v.resolvedTs : null,
|
|
69
|
+
attempts: Number.isInteger(v.attempts) && v.attempts > 0 ? Math.min(v.attempts, 99) : 0,
|
|
70
|
+
prev: typeof v.prev === 'string' && HEX_ID.test(v.prev) ? v.prev : null,
|
|
71
|
+
flagged4: stamp(v.flagged4),
|
|
72
|
+
flagged24: stamp(v.flagged24),
|
|
73
|
+
task: typeof v.task === 'string' && HEX_ID.test(v.task) ? v.task : null,
|
|
74
|
+
taskFor: v.taskFor ? flat(v.taskFor, 80) : null
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// The relay's escalation notes on records (noteHeld), kept apart from the records themselves.
|
|
79
|
+
export const NOTES_MAP = 'heldNotes'
|
|
80
|
+
const NOTE_KEYS = ['flagged4', 'flagged24', 'task', 'taskFor']
|
|
81
|
+
const notesOf = (map) => (map && map.doc ? map.doc.getMap(NOTES_MAP) : null)
|
|
82
|
+
const withNote = (v, note) => {
|
|
83
|
+
if (!v || typeof v !== 'object' || !note || typeof note !== 'object') return v
|
|
84
|
+
const out = { ...v }
|
|
85
|
+
for (const k of NOTE_KEYS) if (note[k] != null) out[k] = note[k]
|
|
86
|
+
return out
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Every valid record, open ones first, oldest first. */
|
|
90
|
+
export function readHeld (map) {
|
|
91
|
+
const out = []
|
|
92
|
+
if (!map) return out
|
|
93
|
+
const notes = notesOf(map)
|
|
94
|
+
map.forEach((v, key) => { const r = publicHeld(withNote(v, notes && notes.get(key))); if (r && r.id === key) out.push(r) })
|
|
95
|
+
return out.sort((a, b) => (a.state === b.state ? 0 : a.state === 'open' ? -1 : 1) || a.ts - b.ts)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export const openHeld = (map) => readHeld(map).filter((r) => r.state === 'open')
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The resolution a new clash of `by`'s on `path` follows, making it a further attempt: the newest record
|
|
102
|
+
* on that path `by` resolved "mine" or "edited" within ATTEMPT_WINDOW_MS. Null when there is none.
|
|
103
|
+
*/
|
|
104
|
+
export function priorAttempt (list, { path, by, now = Date.now() }) {
|
|
105
|
+
const tried = (list || []).filter((r) => r.state === 'resolved' && r.path === path && r.resolvedBy === by && (r.how === 'mine' || r.how === 'edited') && now - (r.resolvedTs || 0) <= ATTEMPT_WINDOW_MS)
|
|
106
|
+
return tried.sort((a, b) => (b.resolvedTs || 0) - (a.resolvedTs || 0) || b.attempts - a.attempts || b.ts - a.ts)[0] || null
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Whether a record is past the two-attempt budget: its writer's AI stops, and it is a task for their person. */
|
|
110
|
+
export const overBudget = (r) => !!r && (r.attempts || 0) >= ATTEMPT_BUDGET
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Keeps a writer's clashing lines aside. `result`: what the shared file got; `mine`: what the writer
|
|
114
|
+
* wanted (resolving "mine" writes it, merged with whatever changed since). Texts past MAX_TEXT are
|
|
115
|
+
* left out of the shared record (`local`): only the writer's machine has them (in .quilt/held/<id>/).
|
|
116
|
+
* A clash that follows the writer's own resolution on the path (priorAttempt) counts one more attempt.
|
|
117
|
+
*/
|
|
118
|
+
export function addHeld (doc, map, { path, kind, by, byId = null, others = [], where = {}, hunks = [], lines = [], reason = '', result = null, mine = null, now = Date.now() }, origin) {
|
|
119
|
+
if (!KINDS.includes(kind)) throw new Error('bad held kind')
|
|
120
|
+
const fits = (t) => t == null || t.length <= MAX_TEXT
|
|
121
|
+
const local = !fits(result) || !fits(mine)
|
|
122
|
+
const prior = priorAttempt(readHeld(map), { path, by, now })
|
|
123
|
+
const rec = {
|
|
124
|
+
id: crypto.randomBytes(8).toString('hex'),
|
|
125
|
+
path, kind, state: 'open', by, byId, others: others.filter((n) => n && n !== by).slice(0, 10), where, ts: now,
|
|
126
|
+
hunks: hunks.slice(0, MAX_HUNKS).map((h) => ({ at: h.at, base: capLines(h.base), ours: capLines(h.ours), theirs: capLines(h.theirs) })),
|
|
127
|
+
more: Math.max(0, hunks.length - MAX_HUNKS),
|
|
128
|
+
lines: lines.slice(0, 50).map((l) => String(l).slice(0, 500)),
|
|
129
|
+
reason: flat(reason, 500),
|
|
130
|
+
result: local ? null : result, mine: local ? null : mine, local,
|
|
131
|
+
how: null, resolvedBy: null, resolvedTs: null,
|
|
132
|
+
attempts: prior ? prior.attempts + 1 : 0,
|
|
133
|
+
prev: prior ? prior.id : null
|
|
134
|
+
}
|
|
135
|
+
const out = publicHeld(rec)
|
|
136
|
+
if (!out) throw new Error('bad held record')
|
|
137
|
+
doc.transact(() => {
|
|
138
|
+
pruneIn(map)
|
|
139
|
+
map.set(rec.id, rec)
|
|
140
|
+
}, origin)
|
|
141
|
+
return out
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** The record `ref` names: its id, or the oldest open one for that path (else the newest resolved one), or null. */
|
|
145
|
+
export function findHeld (map, ref) { return findHeldIn(readHeld(map), ref) }
|
|
146
|
+
|
|
147
|
+
/** findHeld over a list already read (status's, an answer's). */
|
|
148
|
+
export function findHeldIn (all, ref) {
|
|
149
|
+
const key = String(ref || '').trim().replace(/^\.\//, '')
|
|
150
|
+
if (!key) return null
|
|
151
|
+
const byId = all.find((r) => r.id === key)
|
|
152
|
+
if (byId) return byId
|
|
153
|
+
return all.find((r) => r.path === key && r.state === 'open') || all.filter((r) => r.path === key).sort((a, b) => (b.resolvedTs || 0) - (a.resolvedTs || 0))[0] || null
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Marks a record resolved. { record, already }: `already` when it was resolved before (nothing changes then). */
|
|
157
|
+
export function settleHeld (doc, map, id, { how, by }, origin) {
|
|
158
|
+
if (!HOWS.includes(how)) throw new Error(`how is one of ${HOWS.join(', ')}`)
|
|
159
|
+
const cur = publicHeld(map.get(id))
|
|
160
|
+
if (!cur) throw new Error('no such held conflict')
|
|
161
|
+
if (cur.state === 'resolved') return { record: cur, already: true }
|
|
162
|
+
const next = { ...map.get(id), state: 'resolved', how, resolvedBy: by, resolvedTs: Date.now() }
|
|
163
|
+
doc.transact(() => map.set(id, next), origin)
|
|
164
|
+
return { record: publicHeld(next), already: false }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Notes the relay's escalation on a record: `flagged4` / `flagged24` (when each notice went), `task` and
|
|
169
|
+
* `taskFor` (the board task it became). They go in their own map (NOTES_MAP), never over the record, so
|
|
170
|
+
* a resolution that crosses the relay's note on the wire is never lost to it.
|
|
171
|
+
*/
|
|
172
|
+
export function noteHeld (doc, map, id, { flagged4, flagged24, task, taskFor }, origin) {
|
|
173
|
+
const notes = notesOf(map)
|
|
174
|
+
if (!notes || !publicHeld(map.get(id))) return null
|
|
175
|
+
const next = { ...(notes.get(id) || {}) }
|
|
176
|
+
if (stamp(flagged4)) next.flagged4 = flagged4
|
|
177
|
+
if (stamp(flagged24)) next.flagged24 = flagged24
|
|
178
|
+
if (typeof task === 'string' && HEX_ID.test(task)) next.task = task
|
|
179
|
+
if (taskFor) next.taskFor = flat(taskFor, 80)
|
|
180
|
+
doc.transact(() => notes.set(id, next), origin)
|
|
181
|
+
return publicHeld(withNote(map.get(id), next))
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The escalation notices due at `now`: [{ record, step }], step 4 (tell the writer's person) or 24 (tell
|
|
186
|
+
* the session's owner too), each once per open record.
|
|
187
|
+
*/
|
|
188
|
+
export function heldNoticesDue (list, now = Date.now()) {
|
|
189
|
+
const out = []
|
|
190
|
+
for (const r of list || []) {
|
|
191
|
+
if (r.state !== 'open') continue
|
|
192
|
+
if (!r.flagged4 && now - r.ts >= ESCALATE_MS) out.push({ record: r, step: 4 })
|
|
193
|
+
if (!r.flagged24 && now - r.ts >= ADMIN_MS) out.push({ record: r, step: 24 })
|
|
194
|
+
}
|
|
195
|
+
return out
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** The open records past the attempt budget that are not a task yet. */
|
|
199
|
+
export const heldTasksDue = (list) => (list || []).filter((r) => r.state === 'open' && overBudget(r) && !r.task)
|
|
200
|
+
|
|
201
|
+
/** When the next escalation notice falls due (for a session nobody has open), or null with none to come. */
|
|
202
|
+
export function nextHeldDue (list) {
|
|
203
|
+
let next = null
|
|
204
|
+
for (const r of list || []) {
|
|
205
|
+
if (r.state !== 'open') continue
|
|
206
|
+
const at = !r.flagged4 ? r.ts + ESCALATE_MS : !r.flagged24 ? r.ts + ADMIN_MS : null
|
|
207
|
+
if (at !== null && (next === null || at < next)) next = at
|
|
208
|
+
}
|
|
209
|
+
return next
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** The board task's title for a record past the budget. */
|
|
213
|
+
export const heldTaskTitle = (r) => `Resolve \`${r.path}\``
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* What a notice says. `step` 4: to the writer's person (`self`: the writer is them; `owner`: an agent with
|
|
217
|
+
* no person, so the session's owner); 24: to the session's owner, `person` having been told at 4 hours.
|
|
218
|
+
*/
|
|
219
|
+
export function heldNoticeText (r, step, { person = '', self = false, owner = false } = {}) {
|
|
220
|
+
const whose = self ? 'your' : `${r.by}'s`
|
|
221
|
+
const what = `${whose} ${r.kind === 'stale' ? 'removal of lines from' : 'changes to'} \`${r.path}\` (id ${r.id}) ${r.kind === 'stale' ? 'is' : 'are'} held back, because ${WHAT[r.kind](r)}`
|
|
222
|
+
const how = 'Resolve it with quilt_resolve_merge { path or id, how: "theirs" (drop the held lines), "mine" (put them in) or "edited" (you fixed the file yourself) }, or ask your AI to.'
|
|
223
|
+
if (step === 24) return `⚖️ A conflict has been open a day: ${what}. ${person ? `${person} was told at 4 hours. ` : ''}Anyone in the session may resolve it. ${how}`
|
|
224
|
+
return `⚖️ A conflict has been open 4 hours: ${what}. ${self ? '' : owner ? `${r.by} is an agent with no person of its own here, so it comes to you as the session's owner. ` : `${r.by} works for you, so it comes to you. `}${how}`
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Drops resolved records older than a day and, past MAX_HELD, the oldest resolved ones. Open ones always stay. */
|
|
228
|
+
function pruneIn (map, now = Date.now()) {
|
|
229
|
+
const all = []
|
|
230
|
+
const junk = []
|
|
231
|
+
map.forEach((v, key) => { const r = publicHeld(v); if (!r || r.id !== key) junk.push(key); else all.push(r) })
|
|
232
|
+
for (const key of junk) map.delete(key)
|
|
233
|
+
const notes = notesOf(map)
|
|
234
|
+
if (notes) for (const key of [...notes.keys()]) if (!map.has(key) || junk.includes(key)) notes.delete(key)
|
|
235
|
+
const done = all.filter((r) => r.state === 'resolved').sort((a, b) => (a.resolvedTs || 0) - (b.resolvedTs || 0))
|
|
236
|
+
let count = all.length
|
|
237
|
+
for (const r of done) {
|
|
238
|
+
if ((r.resolvedTs && now - r.resolvedTs > DONE_TTL_MS) || count >= MAX_HELD) { map.delete(r.id); if (notes) notes.delete(r.id); count-- }
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* The paths with an open held conflict on square `square` (Seam's 07 "Terms held aside": a path with a
|
|
244
|
+
* held term counts as conflicted, so it cannot land until it is resolved). `square` may be a prefix of
|
|
245
|
+
* the id, or the id of a record's prefix. Records held on a branch document are not a square's.
|
|
246
|
+
*/
|
|
247
|
+
export function heldPathsOn (list, square) {
|
|
248
|
+
const sq = String(square || '').replace(/^sq:/, '')
|
|
249
|
+
if (!sq) return []
|
|
250
|
+
const on = (r) => r && r.state === 'open' && r.where && !r.where.branch && typeof r.where.square === 'string' && r.where.square && (r.where.square.startsWith(sq) || sq.startsWith(r.where.square))
|
|
251
|
+
return [...new Set((list || []).filter(on).map((r) => r.path))]
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Why a commit or merge leaves `paths` out (an open held conflict on each); `none`: nothing else was there to land. */
|
|
255
|
+
export function heldLandingNote (paths, { none = false } = {}) {
|
|
256
|
+
const one = paths.length === 1
|
|
257
|
+
const named = paths.slice(0, 5).map((p) => `\`${p}\``).join(', ') + (paths.length > 5 ? ` and ${paths.length - 5} more` : '')
|
|
258
|
+
return `${named} ${one ? 'has an open held conflict' : 'have open held conflicts'} (two writes changed the same lines and the second writer's are held), so ${one ? 'it does' : 'they do'} not land until resolved: ${none ? 'nothing else was there to commit' : 'everything else landed'}. ` +
|
|
259
|
+
`Resolve ${one ? 'it' : 'each'} with quilt_resolve_merge { path, how: "theirs" (drop the held lines), "mine" (put them in) or "edited" (you fixed the file yourself) }, then commit again.`
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** The session's `stale_write_minutes`: the owner's setting (as voice's transcripts are), else 15. */
|
|
263
|
+
export function staleWriteMinutes (doc, members = []) {
|
|
264
|
+
const v = doc && doc.getMap(SETTINGS_MAP).get('staleWriteMinutes')
|
|
265
|
+
const owner = (members || []).find((m) => m && m.role === 'owner')
|
|
266
|
+
if (!v || typeof v !== 'object' || !owner || v.by !== owner.name) return STALE_WRITE_MINUTES
|
|
267
|
+
const n = Number(v.minutes)
|
|
268
|
+
return Number.isFinite(n) && n >= 0 && n <= 24 * 60 ? n : STALE_WRITE_MINUTES
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** The owner sets `stale_write_minutes` for everyone in the session (0 turns the check off). */
|
|
272
|
+
export function setStaleWriteMinutes (doc, { minutes, by, now = Date.now() }) {
|
|
273
|
+
const n = Number(minutes)
|
|
274
|
+
if (!Number.isFinite(n) || n < 0 || n > 24 * 60) throw new Error('stale_write_minutes is a number of minutes from 0 to 1440')
|
|
275
|
+
doc.getMap(SETTINGS_MAP).set('staleWriteMinutes', { minutes: Math.round(n), by, ts: now })
|
|
276
|
+
return Math.round(n)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const ago = (ts, now = Date.now()) => {
|
|
280
|
+
const s = Math.max(0, Math.round((now - ts) / 1000))
|
|
281
|
+
if (s < 60) return `${s}s ago`
|
|
282
|
+
if (s < 3600) return `${Math.round(s / 60)}m ago`
|
|
283
|
+
return `${Math.round(s / 3600)}h ago`
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const WHAT = {
|
|
287
|
+
lines: (r) => `${r.others.length ? `${r.others.join(', ')} had changed` : 'someone had changed'} the same lines first`,
|
|
288
|
+
stale: () => 'the write removed lines someone else added minutes before',
|
|
289
|
+
parse: () => 'the merged file would not parse',
|
|
290
|
+
binary: (r) => `${r.others.length ? r.others.join(', ') : 'someone'} changed it first`
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** One line per open conflict, for the top of every tool answer and quilt_status. '' when there are none. */
|
|
294
|
+
export function renderHeld (list, { me = '', max = 5 } = {}) {
|
|
295
|
+
const open = (list || []).filter((r) => r.state === 'open')
|
|
296
|
+
if (!open.length) return ''
|
|
297
|
+
const shown = open.slice(0, max).map((r) => {
|
|
298
|
+
if (overBudget(r)) return `- \`${r.path}\` (id ${r.id}, ${ago(r.ts)}): held again after ${r.attempts} resolutions, so it is a task for ${r.taskFor || `${r.by}'s person`} on the board${r.task ? ` (${heldTaskTitle(r)})` : ''}. AIs: stop working on this conflict and leave it to ${r.taskFor || 'them'}.`
|
|
299
|
+
const n = r.hunks.length + (r.more || 0)
|
|
300
|
+
const first = r.hunks[0]
|
|
301
|
+
const where = first ? ` at line ${first.at + 1}` : ''
|
|
302
|
+
const whose = r.by === me ? 'your' : `${r.by}'s`
|
|
303
|
+
const ask = r.by === me ? 'you are asked first' : `${r.by} is asked first; any AI may take it`
|
|
304
|
+
return `- \`${r.path}\` (id ${r.id}, ${ago(r.ts)}): ${whose} ${r.kind === 'binary' ? 'version' : `${n} change${n === 1 ? '' : 's'}${where}`} held back, because ${WHAT[r.kind](r)}. The file keeps the version that landed first; ${ask}.`
|
|
305
|
+
})
|
|
306
|
+
const more = open.length > max ? `\n- and ${open.length - max} more (quilt_status lists them)` : ''
|
|
307
|
+
return `⚖️ Open conflicts in this session (${open.length}). Resolve each with quilt_resolve_merge { path or id, how: "theirs" (drop the held lines), "mine" (put them over the shared ones) or "edited" (you wrote the resolved file yourself) }:\n${shown.join('\n')}${more}`
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** A record in full for an AI deciding how to resolve it: each hunk's three sides. */
|
|
311
|
+
export function describeHeld (r) {
|
|
312
|
+
const side = (label, ls) => ` ${label}:\n${ls.length ? ls.map((l) => ` ${l}`).join('\n') : ' (nothing)'}`
|
|
313
|
+
const hunks = r.hunks.map((h, i) => `Hunk ${i + 1}, at line ${h.at + 1}:\n${side('base', h.base)}\n${side('kept (landed first)', h.theirs)}\n${side(`held (${r.by}'s)`, h.ours)}`)
|
|
314
|
+
const stop = overBudget(r) ? [`It was held again after ${r.attempts} resolutions, so it is a task for ${r.taskFor || `${r.by}'s person`} on the board: AIs stop working on it and leave it to them.`] : []
|
|
315
|
+
return [`\`${r.path}\` (id ${r.id}): ${r.reason || WHAT[r.kind](r)}.`, ...stop, ...hunks, ...(r.more ? [`… and ${r.more} more hunk(s).`] : [])].join('\n')
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** quilt_resolve_merge's description, the same for the local and the hosted MCP. */
|
|
319
|
+
export const RESOLVE_DESCRIPTION = 'Resolve a held conflict. When two writes change the same lines, the file keeps the version that landed first and the second writer\'s lines are held (also: a write that removed lines someone else just added, or a merge that would not parse). ' +
|
|
320
|
+
'Every open one is listed at the top of every Quilt answer until it is resolved; its writer is asked first, but any AI in the session may take it, and nothing holds the file meanwhile. ' +
|
|
321
|
+
'Give the path or id, and how: "theirs" drops the held lines (the file stays as it is), "mine" puts them over the shared ones (merged with whatever changed since), "edited" says you already wrote the resolved file (read it, combine both sides by hand, then resolve "edited"). ' +
|
|
322
|
+
'Without how, it shows the conflict in full: each hunk\'s base, the kept lines and the held ones. Resolving one already resolved is harmless and says so. ' +
|
|
323
|
+
'If your writes to a path are held again twice after you resolved them, the third becomes a task on the board for your person: stop working on that conflict and leave it to them. One left open 4 hours is sent to its writer\'s person, and at 24 hours to the session\'s owner.'
|