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.
Files changed (62) hide show
  1. package/RELEASES.md +30 -0
  2. package/bin/quilt.js +16 -9
  3. package/package.json +2 -2
  4. package/src/agent-task-workflow.js +2 -9
  5. package/src/base-pick.js +212 -0
  6. package/src/boxes.js +6 -7
  7. package/src/catchup.js +1 -1
  8. package/src/chat-links.js +1 -3
  9. package/src/connection.js +24 -5
  10. package/src/control.js +27 -20
  11. package/src/duties.js +32 -56
  12. package/src/github-commit.js +1 -1
  13. package/src/held.js +323 -0
  14. package/src/hooks.js +27 -66
  15. package/src/identity.js +1 -1
  16. package/src/inbox.js +5 -4
  17. package/src/key-cli.js +1 -1
  18. package/src/live-merge.js +599 -0
  19. package/src/mcp.js +57 -102
  20. package/src/merge3.js +65 -1
  21. package/src/now-summary.js +4 -6
  22. package/src/pathrules.js +1 -1
  23. package/src/persona.js +1 -1
  24. package/src/presence.js +2 -0
  25. package/src/protocol.js +2 -2
  26. package/src/read-bases.js +123 -0
  27. package/src/relay-ask.js +2 -2
  28. package/src/relay-live-write.js +179 -0
  29. package/src/relay-mcp.js +173 -212
  30. package/src/relay-prune.js +2 -2
  31. package/src/relay-turn-sync.js +146 -142
  32. package/src/runner.js +0 -2
  33. package/src/server.js +253 -375
  34. package/src/session-follow.js +3 -1
  35. package/src/session-quilt.js +2 -21
  36. package/src/session-seam.js +35 -7
  37. package/src/session.js +795 -553
  38. package/src/setup.js +15 -25
  39. package/src/source-live/bring-in.js +5 -5
  40. package/src/source-live/land.js +20 -3
  41. package/src/source-live/live-block.js +503 -52
  42. package/src/status.js +8 -12
  43. package/src/swap-in.js +81 -0
  44. package/src/textdiff.js +54 -0
  45. package/src/turn-sync-plan.js +6 -18
  46. package/src/turn-sync.js +28 -21
  47. package/src/ui/agent-guide.js +13 -13
  48. package/src/ui/app.css +19 -40
  49. package/src/ui/boxes.js +8 -16
  50. package/src/ui/common.js +1 -1
  51. package/src/ui/fileview.js +11 -15
  52. package/src/ui/loom-model.js +18 -16
  53. package/src/ui/loom.css +1 -2
  54. package/src/ui/loom.js +6 -6
  55. package/src/ui/rail-model.js +25 -64
  56. package/src/ui/rail.js +48 -60
  57. package/src/ui/seam-model.js +39 -3
  58. package/src/ui/session.js +36 -54
  59. package/src/ui/tree.js +22 -129
  60. package/src/ui-server.js +2 -6
  61. package/src/release-words.js +0 -18
  62. package/src/seam-claims.js +0 -53
@@ -0,0 +1,179 @@
1
+ // A folder's live write, applied by the relay (plans/no-holds.md decision 2: "The relay merges each
2
+ // write against the version the writer started from, in one step, one write at a time per file. No
3
+ // check-then-write race."). A folder that saw a text file change on disk (live-block.js scan, a
4
+ // square's live document; session.js ingest, a branch document) sends the relay
5
+ //
6
+ // MSG_BRANCH { op: 'live-write', branch, path, base, baseText?, text, older?, mode?, at?, trusted?, retry?, baseUnknown?, fromGit?, seen? }
7
+ //
8
+ // `base`: the version (sha1, read-bases.js versionOf) of the shared text the folder last synced for the
9
+ // path, null for a file it never had; `baseText`: that text itself, when the relay may not know it
10
+ // (the folder sends it when it is small, or when the relay asked for it); `text`: what disk holds now;
11
+ // `older`: the text the save was found to be made from, when older than the base (base-pick.js);
12
+ // `fromGit`: a diff branch's first edit of a file only git had, `base` being git's text (the shared file
13
+ // has no entry: it is git's, so the base is what it holds); `seen`: the folder can tell who read its files
14
+ // and vouches that the save was made on `base` (or `older`), so nothing is held for merely looking like an
15
+ // edit made from an older read (live-merge.js LiveRules.write `seen`).
16
+ // The relay merges it here, synchronously (nothing else runs on the room between the read of the
17
+ // shared text and the write of the result), through the same engine every live write uses
18
+ // (live-merge.js LiveRules: same lines keep what landed first and the writer's are held, the stale-write,
19
+ // removed-name and parse rules), and puts the result into the file's Y.Text in one transaction of whole
20
+ // lines (textdiff.js applyLineDiff), so two writes of one line never come out mixed letter by letter.
21
+ // The reply: { ok, path, version, same, text? (the result, when it is not what was sent), detail,
22
+ // held: [ids], notices, logs, gone, local } or { ok, needBase: true } (send baseText, or `retry`
23
+ // without it) or { ok, fallback: why } (write it as before: an edit of the Y.Text the relay checks).
24
+ //
25
+ // A base the relay no longer has, with no text for it, is never replaced by the latest: the write is
26
+ // merged against the oldest version kept, as a stale write (`strict`: every member's recent lines stay,
27
+ // the writer's own included).
28
+ //
29
+ // Apps from before this (0.3.170 and earlier) keep sending Yjs edits of the text as they always did:
30
+ // they are combined as a CRDT does, without these guarantees (NOT-DONE.md).
31
+ import * as Y from 'yjs'
32
+ import { LiveRules } from './live-merge.js'
33
+ import { readBasesOf, versionOf } from './read-bases.js'
34
+ import { applyLineDiff } from './textdiff.js'
35
+ import { staleWriteMinutes } from './held.js'
36
+ import { isSafeRelPath } from './pathrules.js'
37
+ import { isLiveKey, liveMaps, liveHead, LIVE_PREFIX } from './source-live/live-square.js'
38
+
39
+ export const WRITE_ORIGIN = 'relay-live-write'
40
+ /** A text written live: at most this long (a square's live text, live-block.js MAX_LIVE_TEXT). */
41
+ export const MAX_WRITE_TEXT = 2 * 1024 * 1024
42
+ const HEX40 = /^[0-9a-f]{40}$/
43
+
44
+ /** The member who last changed `rel` on `key`, from the room's activity (a branch document's last editor): { by, ts }, or null. */
45
+ function lastEditor (room, key, rel) {
46
+ const list = room.doc.getArray('activity').toArray()
47
+ for (let i = list.length - 1; i >= 0; i--) {
48
+ const e = list[i]
49
+ if (e && e.path === rel && (!e.branch || e.branch === key)) return e.by ? { by: e.by, ts: e.ts || 0 } : null
50
+ }
51
+ return null
52
+ }
53
+
54
+ /** The text files of a document, as [path, text] (the stale and gone-name checks read them). */
55
+ function * textsOf (files) {
56
+ for (const [p, t] of files) if (t && typeof t.toString === 'function' && isSafeRelPath(p)) yield [p, t.toString()]
57
+ }
58
+
59
+ /**
60
+ * The relay's half of a folder's live write: merged and applied at once. `room` is the relay's Room
61
+ * (server.js); `ws` the writer's connection. Returns the reply's fields.
62
+ */
63
+ export function liveWrite (room, ws, req, { origin = WRITE_ORIGIN, now = Date.now() } = {}) {
64
+ const key = String(req.branch ?? '')
65
+ const k = room.resolveKey(key)
66
+ const e = k === ws.branch ? room.store.get(k) : null
67
+ if (!e) throw new Error(`you are not on ${key}`)
68
+ const a = room.access.get(ws)
69
+ if (!a || (a.state && a.state !== 'approved')) throw new Error('not in this session')
70
+ if (a.role === 'viewer') throw new Error('You can only view this session; file changes are refused.')
71
+ const rel = String(req.path ?? '').trim().replace(/^\.\//, '')
72
+ if (!isSafeRelPath(rel)) throw new Error('That is not a path inside the project.')
73
+ if (typeof req.text !== 'string' || req.text.length > MAX_WRITE_TEXT) throw new Error('a live write is a text of at most 2 MiB')
74
+ const live = isLiveKey(k)
75
+ // What the relay would refuse or treat apart from a plain edit goes the old way: the folder edits the
76
+ // text as before and the relay's checks (checkChange, read-only branches, size limits) apply as ever.
77
+ if (!room.mayWrite(a, rel)) return { ok: true, fallback: 'refused' }
78
+ if (room.unsavable || room.branchFull(k)) return { ok: true, fallback: 'full' }
79
+ if (!live && (room.meta.layout !== 2 || room.meta.syncStopped)) return { ok: true, fallback: 'read-only' }
80
+ if (live && room.movedOnTo(k)) return { ok: true, fallback: 'moved' }
81
+ const doc = e.doc
82
+ const files = doc.getMap('files')
83
+ const blobs = doc.getMap('blobs')
84
+ const m = live ? liveMaps(doc) : null
85
+ const t0 = files.get(rel)
86
+ if (!live && !t0 && blobs.has(rel)) return { ok: true, fallback: 'binary' }
87
+ // What the shared file holds now. A square's file with no live text (or one marked back to its head)
88
+ // holds its head's text, which the relay never reads: the folder's base is that text.
89
+ const headOnly = live && (!t0 || m.clean.has(rel)) && !m.gone.has(rel) && !m.blobs.has(rel)
90
+ const baseText = typeof req.baseText === 'string' && req.baseText.length <= MAX_WRITE_TEXT ? req.baseText : null
91
+ const bases = readBasesOf(room)
92
+ const v = typeof req.base === 'string' ? req.base.trim().toLowerCase() : null
93
+ // The base: the version the folder names, by its hash; its text when the relay has none of it.
94
+ let base = null
95
+ let strict = false
96
+ if (v) {
97
+ if (!HEX40.test(v)) throw new Error('base is a version: 40 hex characters')
98
+ base = bases.find(k, rel, v)
99
+ if (base === null && baseText !== null && versionOf(baseText) === v) base = baseText
100
+ if (base === null && t0 && !headOnly && versionOf(t0.toString()) === v) base = t0.toString()
101
+ if (base === null) {
102
+ // Not known here: the folder sends the text (it has it), or says it has not (retry).
103
+ if (!req.retry) return { ok: true, needBase: true }
104
+ strict = true
105
+ }
106
+ } else if (req.baseUnknown === true) strict = true // the folder knew the file but no longer has the text it synced (a restart)
107
+ // A head file: the shared text is the head's, which is the folder's base (null for a file new to the square).
108
+ let theirs = headOnly ? base : t0 ? t0.toString() : null
109
+ // A diff branch's file only git has (no entry, not deleted in the session): it holds git's text, the base.
110
+ if (!live && req.fromGit === true && !t0 && typeof base === 'string' && !doc.getMap('gone').has(rel)) theirs = base
111
+ if (strict) base = bases.oldest(k, rel) ?? theirs
112
+ // Made by two at once (the folder never had it, the session has it now): both whole, at one spot.
113
+ if (!v && req.baseUnknown !== true && theirs !== null) { base = ''; strict = true }
114
+ if (typeof theirs === 'string') bases.record(k, rel, theirs)
115
+ if (typeof base === 'string' && base) bases.record(k, rel, base)
116
+ const by = room.names.get(ws) || ''
117
+ const byId = a.key || null
118
+ // Who landed first: the square's writers map; on a branch, the last live write here, else the room's activity.
119
+ if (!room.liveWriters) room.liveWriters = new Map()
120
+ const wkey = `${k}\0${rel}`
121
+ const newer = (x, y) => (!x ? y : !y ? x : (y.ts || 0) > (x.ts || 0) ? y : x)
122
+ const others = [live ? ((m.writers.get(rel) || {}).by || null) : (newer(room.liveWriters.get(wkey), lastEditor(room, k, rel)) || {}).by].filter((n) => n && n !== by)
123
+ const notices = []
124
+ const logs = []
125
+ const local = []
126
+ const rules = new LiveRules({
127
+ room: () => room.doc,
128
+ staleMinutes: () => staleWriteMinutes(room.doc, room.memberList ? room.memberList() : []),
129
+ notice: (s) => notices.push(s),
130
+ log: (s) => logs.push(s),
131
+ texts: () => textsOf(files),
132
+ origin,
133
+ keepLocal: (id, texts) => local.push({ id, ...texts }),
134
+ folder: true
135
+ })
136
+ if (req.trusted === true) rules.trust(rel, req.text)
137
+ const where = live ? { square: k.slice(LIVE_PREFIX.length) } : { branch: k }
138
+ // The older text the save was found to be made from (base-pick.js): put onto the base first, newer lines kept.
139
+ const older = typeof req.older === 'string' && req.older.length <= MAX_WRITE_TEXT ? req.older : null
140
+ let r = null
141
+ let detail = ''
142
+ const before = t0 && !headOnly ? t0.toString() : theirs
143
+ // The merge, what it notes in the file's document (additions, for the next stale check) and the result: one
144
+ // transaction, so every member gets the write as one update.
145
+ doc.transact(() => {
146
+ r = rules.write({ doc, where, path: rel, base, ours: req.text, theirs, by, byId, others, strict, nameSearch: false, older, seen: req.seen === true && !strict })
147
+ let t = files.get(rel)
148
+ if (live) {
149
+ m.clean.delete(rel); m.gone.delete(rel); m.blobs.delete(rel)
150
+ m.writers.set(rel, { by, ts: now, at: typeof req.at === 'string' ? req.at : liveHead(doc) })
151
+ if (req.mode === 'exec') { if (m.modes.get(rel) !== 'exec') m.modes.set(rel, 'exec') } else if (req.mode === 'file' && m.modes.has(rel)) m.modes.delete(rel)
152
+ // A head file's first live text: the head's text first, so what changed is told from it.
153
+ if (t && headOnly && t.toString() !== (theirs ?? '')) { files.delete(rel); t = null }
154
+ } else blobs.delete(rel)
155
+ if (!t) { t = new Y.Text(); if (typeof theirs === 'string') t.insert(0, theirs); files.set(rel, t) }
156
+ detail = applyLineDiff(t, r.text)
157
+ }, origin)
158
+ if (!live && r.text !== (theirs ?? '')) {
159
+ room.liveWriters.delete(wkey)
160
+ room.liveWriters.set(wkey, { by, ts: now })
161
+ if (room.liveWriters.size > 5000) room.liveWriters.delete(room.liveWriters.keys().next().value)
162
+ }
163
+ if (room.noteActivity && room.holderKeys) room.noteActivity(room.holderKeys(ws))
164
+ const version = bases.note(by || 'folder', k, rel, r.text)
165
+ return {
166
+ ok: true,
167
+ path: rel,
168
+ version,
169
+ same: r.text === req.text,
170
+ ...(r.text !== req.text ? { text: r.text } : {}),
171
+ detail,
172
+ before: before ?? '',
173
+ held: r.held.map((h) => h.id),
174
+ notices,
175
+ logs,
176
+ gone: r.gone || [],
177
+ ...(local.length ? { local } : {})
178
+ }
179
+ }