@chroxy/server 0.11.10 → 0.11.11

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 (153) hide show
  1. package/CONFIG.md +4 -2
  2. package/package.json +3 -3
  3. package/src/acp-session.js +5 -1
  4. package/src/agent-connections.js +12 -1
  5. package/src/agent-control/client.js +29 -0
  6. package/src/agent-control/events.js +8 -4
  7. package/src/agent-control/local-connection.js +34 -16
  8. package/src/agent-control/mcp-server.js +2 -0
  9. package/src/anthropic-compatible-config.js +1 -0
  10. package/src/anthropic-gemini-shim.js +444 -0
  11. package/src/anthropic-gemini-translate.js +1030 -0
  12. package/src/base-session.js +319 -2
  13. package/src/bound-client-policy.js +12 -0
  14. package/src/byok-mcp-config.js +9 -5
  15. package/src/byok-mcp-trust.js +34 -20
  16. package/src/byok-session.js +62 -9
  17. package/src/byok-tool-executor.js +81 -1
  18. package/src/claude-setting-sources.js +74 -0
  19. package/src/claude-tui/pty-driver.js +5 -1
  20. package/src/claude-tui/usage-limit.js +2 -2
  21. package/src/claude-tui-session.js +57 -4
  22. package/src/cli/agent-cmd.js +236 -0
  23. package/src/cli/connection-state.js +54 -0
  24. package/src/cli/freshness-cmd.js +53 -0
  25. package/src/cli/pages-cmd.js +6 -4
  26. package/src/cli/pair-code-cmd.js +7 -6
  27. package/src/cli/pair-discord-cmd.js +7 -6
  28. package/src/cli/schedule-cmd.js +25 -3
  29. package/src/cli/server-cmd.js +6 -0
  30. package/src/cli/shell-cmd.js +7 -4
  31. package/src/cli/status-cmd.js +19 -5
  32. package/src/cli-session.js +190 -11
  33. package/src/cli.js +4 -0
  34. package/src/codex-app-server-session.js +78 -17
  35. package/src/codex-error-text.js +114 -0
  36. package/src/config-dir-migration.js +11 -6
  37. package/src/config.js +23 -3
  38. package/src/connection-info.js +73 -14
  39. package/src/continuations.js +153 -40
  40. package/src/conversation-scanner.js +2 -21
  41. package/src/conversation-search.js +4 -17
  42. package/src/dev-preview.js +2 -1
  43. package/src/docker-session.js +10 -1
  44. package/src/doctor.js +14 -3
  45. package/src/durable-write.js +35 -0
  46. package/src/environments/authority.js +7 -1
  47. package/src/event-normalizer.js +35 -2
  48. package/src/folder-trust-wire.js +137 -0
  49. package/src/folder-trust-worker.js +17 -0
  50. package/src/folder-trust.js +1572 -0
  51. package/src/freshness/activity-meter.js +101 -0
  52. package/src/freshness/adapter.js +199 -0
  53. package/src/freshness/checks.js +239 -0
  54. package/src/freshness/config.js +98 -0
  55. package/src/freshness/core.js +185 -0
  56. package/src/freshness/derive.js +680 -0
  57. package/src/freshness/exchange.js +305 -0
  58. package/src/freshness/file-ledger.js +400 -0
  59. package/src/freshness/grade.js +81 -0
  60. package/src/freshness/hash.js +45 -0
  61. package/src/freshness/inbox.js +163 -0
  62. package/src/freshness/index.js +20 -0
  63. package/src/freshness/investigation.js +474 -0
  64. package/src/freshness/keys.js +177 -0
  65. package/src/freshness/ledger.js +122 -0
  66. package/src/freshness/outbox.js +131 -0
  67. package/src/freshness/policy.js +599 -0
  68. package/src/freshness/queue.js +843 -0
  69. package/src/freshness/read-errors.js +23 -0
  70. package/src/freshness/render.js +153 -0
  71. package/src/freshness/runtime.js +113 -0
  72. package/src/freshness/session-registry-adapter.js +161 -0
  73. package/src/freshness/state-file-adapter.js +167 -0
  74. package/src/freshness-runner/build-info.js +139 -0
  75. package/src/freshness-runner/cli.js +105 -0
  76. package/src/freshness-runner/run-tick.js +109 -0
  77. package/src/freshness-runner/stamp.js +57 -0
  78. package/src/freshness-sources/claude-transcript-adapter.js +211 -0
  79. package/src/freshness-sources/codex-transcript-adapter.js +33 -0
  80. package/src/freshness-sources/index.js +8 -0
  81. package/src/gemini-byok-session.js +292 -0
  82. package/src/gemini-credentials.js +67 -0
  83. package/src/gemini-key-safety.js +246 -0
  84. package/src/gemini-models.js +386 -0
  85. package/src/handler-utils.js +6 -13
  86. package/src/handlers/checkpoint-handlers.js +8 -0
  87. package/src/handlers/folder-trust-handlers.js +90 -0
  88. package/src/handlers/input-handlers.js +40 -11
  89. package/src/handlers/principal-admin-handlers.js +229 -0
  90. package/src/handlers/principal-handlers.js +262 -0
  91. package/src/handlers/scheduler-handlers.js +4 -4
  92. package/src/handlers/session-handlers.js +10 -1
  93. package/src/handlers/settings-handlers.js +42 -8
  94. package/src/handlers/skills-handlers.js +7 -0
  95. package/src/history-page.js +8 -0
  96. package/src/jsonl-reader.js +90 -10
  97. package/src/jsonl-subprocess-session.js +4 -1
  98. package/src/logger.js +105 -15
  99. package/src/mailbox-route.js +102 -0
  100. package/src/notification-prefs.js +15 -0
  101. package/src/orchestration/orchestration-manager.js +38 -9
  102. package/src/orchestration/permission-gate.js +4 -1
  103. package/src/orchestration/run-record.js +11 -0
  104. package/src/orchestration/turn-driver.js +151 -33
  105. package/src/orphan-reaper.js +77 -20
  106. package/src/outgoing-policy.js +10 -4
  107. package/src/permission-floor.js +54 -2
  108. package/src/permission-manager.js +32 -13
  109. package/src/permission-resolved-by.js +89 -0
  110. package/src/permission-resolver.js +7 -3
  111. package/src/platform.js +156 -4
  112. package/src/principal-audit.js +189 -0
  113. package/src/principal-binding.js +25 -0
  114. package/src/principal-brakes.js +21 -0
  115. package/src/principal-effect.js +101 -0
  116. package/src/principal-journal.js +905 -0
  117. package/src/principal-kill-switch.js +183 -0
  118. package/src/principal-label.js +5 -0
  119. package/src/principal-pending-permissions.js +177 -0
  120. package/src/principal-policy.js +1094 -0
  121. package/src/principal-sessions-snapshot.js +208 -0
  122. package/src/principal-store.js +488 -0
  123. package/src/principal-subscriptions.js +283 -0
  124. package/src/providers.js +2 -0
  125. package/src/redaction.js +181 -22
  126. package/src/scheduled-task-store.js +12 -0
  127. package/src/scheduler.js +304 -17
  128. package/src/sdk-session.js +60 -7
  129. package/src/server-cli/push-notification-handler.js +6 -1
  130. package/src/server-cli.js +30 -1
  131. package/src/session-ci-watcher.js +210 -37
  132. package/src/session-manager.js +590 -19
  133. package/src/session-message-history.js +121 -2
  134. package/src/session-name-allocator.js +685 -0
  135. package/src/statusline.js +78 -19
  136. package/src/summarize-session.js +2 -0
  137. package/src/turn-outcome.js +5 -2
  138. package/src/utils/argv-safety.js +29 -0
  139. package/src/utils/binary-package-manifest.js +57 -15
  140. package/src/utils/bounded-regex-worker.js +4 -0
  141. package/src/utils/bounded-regex.js +16 -7
  142. package/src/utils/pid-liveness.js +47 -0
  143. package/src/web-task-manager.js +45 -4
  144. package/src/worktree-gc.js +195 -4
  145. package/src/ws-auth.js +103 -19
  146. package/src/ws-broadcaster.js +39 -7
  147. package/src/ws-client-manager.js +8 -0
  148. package/src/ws-file-ops/browser.js +35 -3
  149. package/src/ws-forwarding.js +23 -3
  150. package/src/ws-handler-context.js +18 -0
  151. package/src/ws-history.js +79 -12
  152. package/src/ws-message-handlers.js +10 -2
  153. package/src/ws-server.js +253 -13
@@ -0,0 +1,122 @@
1
+ /**
2
+ * In-memory append-only ledger with idempotent append.
3
+ *
4
+ * A record's `id` is its idempotency key. Appending a record whose key is
5
+ * already present with the same payload is ignored. Appending one whose key is
6
+ * present with a different payload appends an `idempotency_conflict` system
7
+ * record and has no other effect. Nothing is ever changed after it is written.
8
+ *
9
+ * The payload compared is the record without the envelope fields the ledger
10
+ * itself assigns (`seq`, `ts`, `prev`): a repeat written later, at a different
11
+ * time, is still the same record.
12
+ *
13
+ * Pure in-memory data structure; the clock is an argument.
14
+ */
15
+
16
+ import { canonical, sha256 } from './hash.js'
17
+ import { systemKey } from './keys.js'
18
+
19
+ export const KINDS = Object.freeze(['tick', 'check', 'report', 'activity', 'emit', 'investigation', 'surface', 'owner_ack', 'config', 'system'])
20
+
21
+ export function createLedger({ ledgerId }) {
22
+ if (!ledgerId) throw new Error('createLedger: ledgerId is required')
23
+ return { ledgerId, records: [], byKey: new Map(), lastHash: null }
24
+ }
25
+
26
+ /**
27
+ * Rebuild a ledger's in-memory index from its records alone, as a process
28
+ * would after a restart. The records are the durable part; the index is not.
29
+ */
30
+ export function loadLedger({ ledgerId, records }) {
31
+ const ledger = createLedger({ ledgerId })
32
+ for (const r of records) {
33
+ ledger.records.push(r)
34
+ ledger.byKey.set(r.id, payloadOf(r))
35
+ }
36
+ ledger.lastHash = records.length ? sha256(canonical(records[records.length - 1])) : null
37
+ return ledger
38
+ }
39
+
40
+ /** The part of a record that identifies its content. */
41
+ export function payloadOf(record) {
42
+ const { seq: _seq, ts: _ts, prev: _prev, ...rest } = record
43
+ return canonical(rest)
44
+ }
45
+
46
+ export function has(ledger, key) {
47
+ return ledger.byKey.has(key)
48
+ }
49
+
50
+ /** The record with this key, or null. Linear in the worst case; for lookups the caller keeps its own index. */
51
+ export function find(ledger, key) {
52
+ if (!ledger.byKey.has(key)) return null
53
+ for (let i = ledger.records.length - 1; i >= 0; i--) {
54
+ if (ledger.records[i].id === key) return ledger.records[i]
55
+ }
56
+ return null
57
+ }
58
+
59
+ function write(ledger, draft, now) {
60
+ const record = {
61
+ v: 1,
62
+ seq: ledger.records.length + 1,
63
+ ts: new Date(now).toISOString(),
64
+ kind: draft.kind,
65
+ id: draft.id,
66
+ actor: draft.actor ?? { class: 'system', id: 'reconciler', label: 'freshness' },
67
+ session: draft.session ?? null,
68
+ workKey: draft.workKey ?? null,
69
+ refs: draft.refs ?? {},
70
+ body: draft.body ?? {},
71
+ ext: draft.ext ?? {},
72
+ prev: ledger.lastHash,
73
+ }
74
+ ledger.records.push(record)
75
+ ledger.byKey.set(record.id, payloadOf(record))
76
+ ledger.lastHash = sha256(canonical(record))
77
+ return record
78
+ }
79
+
80
+ /**
81
+ * Append a draft record. Returns { status, record } where status is
82
+ * 'appended', 'duplicate' (same key, same payload: ignored) or 'conflict'
83
+ * (same key, different payload: an idempotency_conflict record is appended
84
+ * instead, and `record` is that system record).
85
+ */
86
+ export function append(ledger, draft, now) {
87
+ if (!KINDS.includes(draft.kind)) throw new Error(`append: unknown kind ${draft.kind}`)
88
+ if (!draft.id) throw new Error('append: a record needs its key as id')
89
+ const probe = {
90
+ v: 1,
91
+ kind: draft.kind,
92
+ id: draft.id,
93
+ actor: draft.actor ?? { class: 'system', id: 'reconciler', label: 'freshness' },
94
+ session: draft.session ?? null,
95
+ workKey: draft.workKey ?? null,
96
+ refs: draft.refs ?? {},
97
+ body: draft.body ?? {},
98
+ ext: draft.ext ?? {},
99
+ }
100
+ const existing = ledger.byKey.get(draft.id)
101
+ if (existing !== undefined) {
102
+ if (existing === canonical(probe)) return { status: 'duplicate', record: null }
103
+ const conflictDraft = {
104
+ kind: 'system',
105
+ id: systemKey('idempotency_conflict', [draft.id]),
106
+ body: { event: 'idempotency_conflict', detail: { ids: [draft.id] } },
107
+ }
108
+ if (ledger.byKey.has(conflictDraft.id)) return { status: 'conflict', record: null }
109
+ return { status: 'conflict', record: write(ledger, conflictDraft, now) }
110
+ }
111
+ return { status: 'appended', record: write(ledger, draft, now) }
112
+ }
113
+
114
+ /** Verify the hash chain; returns the first broken seq, or 0. */
115
+ export function verifyChain(ledger) {
116
+ let prev = null
117
+ for (const r of ledger.records) {
118
+ if (r.prev !== prev) return r.seq
119
+ prev = sha256(canonical(r))
120
+ }
121
+ return 0
122
+ }
@@ -0,0 +1,131 @@
1
+ /**
2
+ * The outbox: what the reconciler has to say, as a file any consumer may read
3
+ * and deliver. The reconciler writes it and sends nothing.
4
+ *
5
+ * Schema, version 1. `outbox.jsonl` is UTF-8, one JSON object per line, ended
6
+ * by "\n", append-only: a line is never changed or removed by the reconciler.
7
+ * A consumer reads it from the start or from where it stopped, ignores a last
8
+ * line with no newline (a write in flight) and any line it cannot parse, and
9
+ * may rotate or truncate the file between ticks. It may be read from another
10
+ * machine: a line holds only the fields below, never a path, a secret or the
11
+ * body of a session.
12
+ *
13
+ * v 1
14
+ * at ISO time the line was written
15
+ * rec this line's id (32 hex); a receipt or an acknowledgement names it
16
+ * key the message this line is about (32 hex); every line of one item
17
+ * has the same key, so a consumer edits one message in place
18
+ * op "post" the first delivery of the key
19
+ * "repost" a further delivery of the same key (a reminder, a
20
+ * return, a show after a deferral): send it again
21
+ * "updated" the key's content changed: edit the message in place
22
+ * class "item" | "digest" | "echo" | "alert"
23
+ * status "open" | "closed" | "info"
24
+ * step "shown" | "reminded" | "returned" for an item delivery, else null
25
+ * n the item's presentation number, else null
26
+ * origin "owner" | "attention" | "gone" | "outage" | "seed", else null
27
+ * source the adapter's source name, else null
28
+ * text what to show, made by a template (render.js)
29
+ *
30
+ * Writes are one `write` and an `fsync` per line, so a crash leaves at most one
31
+ * cut-off tail, which the next write starts after with a newline. `rec` is
32
+ * deterministic, so a line the reconciler writes again after a crash is found in
33
+ * the file's tail and not written twice.
34
+ */
35
+
36
+ import fs from 'node:fs'
37
+ import path from 'node:path'
38
+
39
+ export const OUTBOX_VERSION = 1
40
+ export const OUTBOX_FIELDS = Object.freeze(['v', 'at', 'rec', 'key', 'op', 'class', 'status', 'step', 'n', 'origin', 'source', 'text'])
41
+ export const OUTBOX_OPS = Object.freeze(['post', 'repost', 'updated'])
42
+ export const OUTBOX_CLASSES = Object.freeze(['item', 'digest', 'echo', 'alert'])
43
+ export const MAX_TEXT = 2000
44
+ const TAIL_BYTES = 256 * 1024
45
+
46
+ /** One outbox line from named values; any field not in the schema is dropped. */
47
+ export function outboxRecord({ at, rec, key, op, cls, status, step = null, n = null, origin = null, source = null, text }) {
48
+ return {
49
+ v: OUTBOX_VERSION,
50
+ at: new Date(at).toISOString(),
51
+ rec,
52
+ key,
53
+ op,
54
+ class: cls,
55
+ status,
56
+ step,
57
+ n,
58
+ origin,
59
+ source,
60
+ text: String(text).length > MAX_TEXT ? String(text).slice(0, MAX_TEXT - 1) + '…' : String(text),
61
+ }
62
+ }
63
+
64
+ /** The ids of the lines in the last part of the file, and whether it ends mid-line. */
65
+ function readTail(file) {
66
+ let fd
67
+ try {
68
+ fd = fs.openSync(file, 'r')
69
+ const size = fs.fstatSync(fd).size
70
+ const len = Math.min(size, TAIL_BYTES)
71
+ const buf = Buffer.alloc(len)
72
+ fs.readSync(fd, buf, 0, len, size - len)
73
+ const text = buf.toString('utf8')
74
+ const open = len > 0 && text[text.length - 1] !== '\n'
75
+ const recs = new Set()
76
+ for (const line of text.split('\n')) {
77
+ try {
78
+ const v = JSON.parse(line)
79
+ if (v && typeof v.rec === 'string') recs.add(v.rec)
80
+ } catch {
81
+ // a cut-off or foreign line is not a record
82
+ }
83
+ }
84
+ return { recs, open }
85
+ } catch (err) {
86
+ if (err?.code === 'ENOENT') return { recs: new Set(), open: false }
87
+ throw err
88
+ } finally {
89
+ if (fd !== undefined) fs.closeSync(fd)
90
+ }
91
+ }
92
+
93
+ /** An append-only outbox file. `append` returns 'written' or 'duplicate' and throws if it cannot write. */
94
+ export function openOutbox(file) {
95
+ let tail = null
96
+ let known = null
97
+ return {
98
+ file,
99
+ append(record) {
100
+ for (const k of Object.keys(record)) if (!OUTBOX_FIELDS.includes(k)) throw new Error(`outbox: field ${k} is not in the schema`)
101
+ fs.mkdirSync(path.dirname(file), { recursive: true })
102
+ try {
103
+ if (!fs.lstatSync(file).isFile()) throw new Error('outbox: the file is not a regular file')
104
+ } catch (err) {
105
+ if (err?.code !== 'ENOENT') throw err
106
+ }
107
+ // A consumer that rotated or truncated the file: what was read of it is stale.
108
+ let seen = null
109
+ try {
110
+ seen = fs.statSync(file)
111
+ } catch {
112
+ seen = null
113
+ }
114
+ if (tail && (!seen || !known || seen.ino !== known.ino || seen.size < known.size)) tail = null
115
+ tail ??= readTail(file)
116
+ if (tail.recs.has(record.rec)) return 'duplicate'
117
+ const fd = fs.openSync(file, 'a', 0o644)
118
+ try {
119
+ fs.writeSync(fd, (tail.open ? '\n' : '') + JSON.stringify(record) + '\n')
120
+ fs.fsyncSync(fd)
121
+ } finally {
122
+ fs.closeSync(fd)
123
+ }
124
+ tail.open = false
125
+ tail.recs.add(record.rec)
126
+ const after = fs.statSync(file)
127
+ known = { ino: after.ino, size: after.size }
128
+ return 'written'
129
+ },
130
+ }
131
+ }