typeclaw 0.45.1 → 0.46.1

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 (163) hide show
  1. package/README.md +1 -1
  2. package/package.json +3 -2
  3. package/src/agent/git-nudge.ts +2 -1
  4. package/src/agent/index.ts +44 -30
  5. package/src/agent/live-subagents.ts +18 -0
  6. package/src/agent/multimodal/index.ts +1 -1
  7. package/src/agent/multimodal/look-at.ts +78 -44
  8. package/src/agent/multimodal/looker.ts +161 -39
  9. package/src/agent/network/response-body.ts +26 -0
  10. package/src/agent/network/safe-http.ts +207 -0
  11. package/src/agent/plugin-tools.ts +377 -157
  12. package/src/agent/provider-error.ts +52 -1
  13. package/src/agent/restart/index.ts +61 -40
  14. package/src/agent/restart-handoff/index.ts +73 -4
  15. package/src/agent/reviewer-bash-policy.ts +42 -1
  16. package/src/agent/session-origin.ts +2 -0
  17. package/src/agent/subagent-drain.ts +117 -7
  18. package/src/agent/subagents.ts +49 -16
  19. package/src/agent/throttle-circuit.ts +25 -5
  20. package/src/agent/tool-error-remediation.ts +72 -0
  21. package/src/agent/tool-file-safety.ts +1310 -0
  22. package/src/agent/tools/channel-fetch-attachment.ts +10 -3
  23. package/src/agent/tools/ddg.ts +16 -2
  24. package/src/agent/tools/post-github-review.ts +199 -0
  25. package/src/agent/tools/spawn-subagent.ts +21 -18
  26. package/src/agent/tools/webfetch/fetch.ts +163 -183
  27. package/src/agent/tools/webfetch/tool.ts +2 -3
  28. package/src/agent/tools/websearch.ts +23 -2
  29. package/src/agent/tools-without-local-file-operands.ts +30 -0
  30. package/src/agent/turn-runner.ts +86 -6
  31. package/src/bundled-plugins/backup/README.md +23 -10
  32. package/src/bundled-plugins/backup/git-auth.ts +6 -4
  33. package/src/bundled-plugins/backup/index.ts +85 -44
  34. package/src/bundled-plugins/backup/runner.ts +85 -1
  35. package/src/bundled-plugins/doc-render/render.ts +93 -12
  36. package/src/bundled-plugins/doc-render/skills/typeclaw-render-pdf/SKILL.md +13 -13
  37. package/src/bundled-plugins/github-cli-auth/effective-approval.ts +16 -6
  38. package/src/bundled-plugins/github-cli-auth/gh-command.ts +555 -15
  39. package/src/bundled-plugins/github-cli-auth/git-askpass.ts +74 -26
  40. package/src/bundled-plugins/github-cli-auth/git-command.ts +224 -36
  41. package/src/bundled-plugins/github-cli-auth/graphql-auth-nudge.ts +9 -9
  42. package/src/bundled-plugins/github-cli-auth/index.ts +207 -132
  43. package/src/bundled-plugins/github-cli-auth/review-checkout.ts +138 -0
  44. package/src/bundled-plugins/github-cli-auth/token-class.ts +4 -6
  45. package/src/bundled-plugins/guard/policies/uncommitted-changes.ts +2 -1
  46. package/src/bundled-plugins/memory/README.md +32 -11
  47. package/src/bundled-plugins/memory/append-tool.ts +15 -8
  48. package/src/bundled-plugins/memory/citations.ts +22 -9
  49. package/src/bundled-plugins/memory/dreaming.ts +13 -8
  50. package/src/bundled-plugins/memory/find-entry-tool.ts +4 -0
  51. package/src/bundled-plugins/memory/index.ts +0 -4
  52. package/src/bundled-plugins/memory/load-memory.ts +8 -3
  53. package/src/bundled-plugins/memory/provenance-index.ts +717 -0
  54. package/src/bundled-plugins/memory/provenance-sanitize.ts +40 -0
  55. package/src/bundled-plugins/memory/references/store-reference-tool.ts +4 -0
  56. package/src/bundled-plugins/memory/search-tool.ts +163 -60
  57. package/src/bundled-plugins/memory/stream-events.ts +2 -0
  58. package/src/bundled-plugins/memory/vector/embed-threads.ts +36 -0
  59. package/src/bundled-plugins/memory/vector/embedder.ts +19 -1
  60. package/src/bundled-plugins/memory/vector/hybrid.ts +126 -13
  61. package/src/bundled-plugins/researcher/write-report.ts +8 -5
  62. package/src/bundled-plugins/reviewer/index.ts +2 -2
  63. package/src/bundled-plugins/reviewer/reviewer.ts +10 -3
  64. package/src/bundled-plugins/reviewer/skills/code-review.ts +19 -9
  65. package/src/bundled-plugins/scout/scout.ts +8 -0
  66. package/src/bundled-plugins/security/index.ts +4 -4
  67. package/src/bundled-plugins/security/policies/outbound-secret-scan.ts +1 -0
  68. package/src/bundled-plugins/security/policies/private-surface-read.ts +787 -36
  69. package/src/bundled-plugins/security/policies/secret-exfil-read.ts +1 -0
  70. package/src/bundled-plugins/security/policies/ssrf.ts +147 -42
  71. package/src/channels/adapters/discord-bot-channel-resolver.ts +26 -14
  72. package/src/channels/adapters/discord-bot-thread-room.ts +44 -12
  73. package/src/channels/adapters/discord-bot.ts +66 -6
  74. package/src/channels/adapters/discord-channel-resolver.ts +91 -23
  75. package/src/channels/adapters/discord-id.ts +4 -0
  76. package/src/channels/adapters/discord-resolver-cache.ts +41 -0
  77. package/src/channels/adapters/discord.ts +57 -7
  78. package/src/channels/adapters/github/auth-app.ts +41 -12
  79. package/src/channels/adapters/github/inbound.ts +11 -0
  80. package/src/channels/adapters/github/index.ts +71 -65
  81. package/src/channels/adapters/github/reconcile-cooldown-store.ts +178 -0
  82. package/src/channels/adapters/github/reconcile-open-prs.ts +31 -0
  83. package/src/channels/adapters/github/review-submitter.ts +302 -0
  84. package/src/channels/adapters/kakaotalk-channel-resolver.ts +5 -3
  85. package/src/channels/adapters/kakaotalk-fetch-attachment.ts +8 -4
  86. package/src/channels/adapters/kakaotalk-typing.ts +17 -15
  87. package/src/channels/adapters/slack-attachment-download.ts +71 -0
  88. package/src/channels/adapters/slack-bot-edit.ts +85 -20
  89. package/src/channels/adapters/slack-bot.ts +26 -10
  90. package/src/channels/adapters/slack-classify.ts +21 -2
  91. package/src/channels/adapters/slack-format.ts +245 -0
  92. package/src/channels/adapters/slack.ts +59 -7
  93. package/src/channels/adapters/telegram-bot.ts +18 -5
  94. package/src/channels/adapters/webex-bot.ts +8 -3
  95. package/src/channels/adapters/webex.ts +8 -3
  96. package/src/channels/fetch-attachment.ts +43 -0
  97. package/src/{bundled-plugins/github-cli-auth/approve-idempotency.ts → channels/github-review-verdict-coordinator.ts} +25 -1
  98. package/src/channels/github-token-bridge.ts +12 -6
  99. package/src/channels/manager.ts +9 -0
  100. package/src/channels/router.ts +248 -6
  101. package/src/channels/types.ts +28 -1
  102. package/src/cli/channel.ts +1 -1
  103. package/src/cli/init.ts +1 -1
  104. package/src/cli/model.ts +57 -7
  105. package/src/compose/stop.ts +17 -5
  106. package/src/config/config.ts +6 -6
  107. package/src/config/models-mutation.ts +13 -1
  108. package/src/config/providers.ts +176 -0
  109. package/src/container/docker-app.ts +42 -13
  110. package/src/container/start.ts +62 -10
  111. package/src/cron/bridge.ts +9 -2
  112. package/src/doctor/checks.ts +8 -3
  113. package/src/doctor/commit.ts +2 -1
  114. package/src/doctor/file-ownership.ts +52 -0
  115. package/src/dreams/git.ts +2 -1
  116. package/src/git/bound-gc-config.ts +82 -0
  117. package/src/git/hookless.ts +5 -0
  118. package/src/git/reconcile-ignored.ts +18 -5
  119. package/src/git/system-commit.ts +12 -6
  120. package/src/hostd/spawn.ts +133 -47
  121. package/src/init/dockerfile.ts +99 -5
  122. package/src/init/index.ts +17 -5
  123. package/src/init/models-dev.ts +4 -0
  124. package/src/init/validate-api-key.ts +2 -0
  125. package/src/inspect/replay.ts +21 -9
  126. package/src/inspect/session-list.ts +71 -33
  127. package/src/mcp/tools.ts +17 -3
  128. package/src/network/internal-destinations.ts +267 -0
  129. package/src/permissions/builtins.ts +3 -1
  130. package/src/plugin/context.ts +2 -0
  131. package/src/plugin/index.ts +1 -0
  132. package/src/plugin/manager.ts +4 -0
  133. package/src/plugin/types.ts +22 -0
  134. package/src/run/index.ts +40 -0
  135. package/src/run/llm-fetch-observer.ts +41 -4
  136. package/src/sandbox/build.ts +37 -15
  137. package/src/sandbox/canonical-secrets.ts +18 -0
  138. package/src/sandbox/env-exposure.ts +83 -0
  139. package/src/sandbox/errors.ts +13 -6
  140. package/src/sandbox/hidden-paths.ts +102 -44
  141. package/src/sandbox/index.ts +22 -4
  142. package/src/sandbox/package-install.ts +3 -27
  143. package/src/sandbox/policy.ts +16 -28
  144. package/src/sandbox/privileged-runtime.ts +256 -0
  145. package/src/sandbox/session-tmp.ts +8 -2
  146. package/src/sandbox/writable-zones.ts +204 -140
  147. package/src/secrets/index.ts +8 -0
  148. package/src/secrets/refresh-provider-oauth.ts +128 -0
  149. package/src/server/command-runner.ts +48 -4
  150. package/src/server/index.ts +4 -0
  151. package/src/skills/typeclaw-channel-github/SKILL.md +49 -56
  152. package/src/skills/typeclaw-claude-code/SKILL.md +24 -60
  153. package/src/skills/typeclaw-claude-code/references/auth-flow.md +17 -158
  154. package/src/skills/typeclaw-codex-cli/SKILL.md +19 -49
  155. package/src/skills/typeclaw-codex-cli/references/auth-flow.md +17 -154
  156. package/src/skills/typeclaw-config/SKILL.md +27 -11
  157. package/src/skills/typeclaw-config/references/recommended-mounts.md +24 -24
  158. package/src/skills/typeclaw-github-contributing/SKILL.md +23 -26
  159. package/src/skills/typeclaw-memory/SKILL.md +9 -3
  160. package/src/skills/typeclaw-permissions/SKILL.md +36 -19
  161. package/src/skills/typeclaw-plugins/SKILL.md +37 -0
  162. package/src/usage/scan.ts +11 -2
  163. package/typeclaw.schema.json +25 -0
@@ -11,18 +11,20 @@ This plugin is **auto-loaded** by every TypeClaw agent. There is no `plugins[]`
11
11
  "backup": {
12
12
  "enabled": true,
13
13
  "idleMs": 30000,
14
- "pushToOrigin": true
14
+ "pushToOrigin": true,
15
+ "maintenance": true
15
16
  }
16
17
  }
17
18
  ```
18
19
 
19
- | Field | Default | Effect |
20
- | ------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
21
- | `backup.enabled` | `true` | Master switch. When `false`, all hooks no-op and the runner subagent is never spawned. |
22
- | `backup.idleMs` | `30000` | Debounce window after the agent goes idle (no in-flight prompt turns) before the backup runner fires. Resets on every new prompt. Minimum `1000`. |
23
- | `backup.pushToOrigin` | `true` | When `true`, after committing, the runner attempts `git push`. On non-fast-forward, it `git fetch && git rebase` then re-pushes. On rebase conflict, it aborts the rebase and asks the diagnose subagent to write a human-readable report. Set `false` to commit-only (useful for offline workflows or repos without a remote). |
24
- | `backup.commitTimeoutMs` | `30000` | Per-command wall clock for local git operations (status/add/commit/diff). Mostly an escape hatch — defaults are generous. |
25
- | `backup.networkTimeoutMs` | `60000` | Per-command wall clock for network git operations (push/fetch/rebase). Bounds the failure mode where a stuck remote would otherwise hang the runner indefinitely. `GIT_TERMINAL_PROMPT=0` is also set so auth failures fail fast instead of prompting. |
20
+ | Field | Default | Effect |
21
+ | ------------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
22
+ | `backup.enabled` | `true` | Master switch. When `false`, all hooks no-op and the runner subagent is never spawned. |
23
+ | `backup.idleMs` | `30000` | Debounce window after the agent goes idle (no in-flight prompt turns) before the backup runner fires. Resets on every new prompt. Minimum `1000`. |
24
+ | `backup.pushToOrigin` | `true` | When `true`, after committing, the runner attempts `git push`. On non-fast-forward, it `git fetch && git rebase` then re-pushes. On rebase conflict, it aborts the rebase and asks the diagnose subagent to write a human-readable report. Set `false` to commit-only (useful for offline workflows or repos without a remote). |
25
+ | `backup.maintenance` | `true` | When `true`, after a backup that made a local commit, the runner runs a **bounded, best-effort** `git maintenance run` (under the same git lock) with `loose-objects` and/or `incremental-repack` selected independently by loose-object and pack counts. Reclaims what the backup keeps adding — without which loose objects (and later packs) grow until the (now-disabled) auto-gc would fire a memory-heavy full repack. `incremental-repack` (multi-pack-index) leaves the large established pack alone, and both tasks run with `pack.threads=1`/`pack.windowMemory=64m`/`pack.deltaCacheSize=1` so RSS stays bounded. A maintenance failure is logged, never folded into the backup result. Set `false` to disable. |
26
+ | `backup.commitTimeoutMs` | `30000` | Per-command wall clock for local git operations (status/add/commit/diff). Mostly an escape hatch — defaults are generous. |
27
+ | `backup.networkTimeoutMs` | `60000` | Per-command wall clock for network git operations (push/fetch/rebase). Bounds the failure mode where a stuck remote would otherwise hang the runner indefinitely. `GIT_TERMINAL_PROMPT=0` is also set so auth failures fail fast instead of prompting. |
26
28
 
27
29
  All fields are **restart-required** — the plugin reads them once at boot.
28
30
 
@@ -57,6 +59,17 @@ On non-fast-forward rejection (either push shape), the runner runs `git fetch` t
57
59
 
58
60
  If any network step fails (rebase conflict, auth failure, network timeout), the runner aborts cleanly and spawns the `backup-diagnose` subagent. That subagent has `bash`, `read`, and `write` tools and writes a short human-readable report to `<agentDir>/sessions/backup-diagnostics.log`. The diagnose subagent is explicitly forbidden from force-pushing or resolving merge conflicts itself.
59
61
 
62
+ ## What it maintains
63
+
64
+ Because the backup force-commits `sessions/` every idle window, each cycle lands new objects as small packs. The agent repo disables git's auto-gc (machine-local config written by `typeclaw start`) so a full repack can never fire opportunistically mid-turn — on a GB-scale agent pack that repack was measured at ~1.8GB RSS and OOM-killed the container. Reclamation therefore moves here, to the **idle backup path**:
65
+
66
+ - After a backup that made a **local commit** — including one whose later push failed, since the commit still grew local history and a remote outage must not suppress reclamation — and only when `backup.maintenance` is on, the runner probes `git count-objects -v`. The two reclamation tasks are gated **independently**, because with auto-gc off a commit produces _loose objects_, not packs:
67
+ - `loose-objects` runs when the **loose-object count** (`count:`) exceeds its threshold — the counter that actually grows every idle cycle.
68
+ - `incremental-repack` runs when the **pack count** (`packs:`) exceeds its threshold — consolidating the packs that `loose-objects` itself produces.
69
+ Gating both on pack count alone would let loose objects accumulate forever while packs stayed ~0.
70
+ - **Bounded by construction.** `incremental-repack` uses the multi-pack-index to consolidate only _small_ packs, leaving the large established pack untouched — so both memory and time stay bounded, unlike a monolithic `git gc`. Both tasks are pinned with `-c pack.threads=1 -c pack.windowMemory=64m -c pack.deltaCacheSize=1` so RSS holds even on a repo predating the start-time config.
71
+ - **Same lock, best-effort.** Maintenance runs inside the same `withGitLock` as the commit/push, so a concurrent backup can't race the pack rewrite. Its outcome is logged; a failure is **never** folded into the backup result. Each task consolidates its own input, so the counts reset and both self-throttle — no wall-clock timer.
72
+
60
73
  ## What it contributes
61
74
 
62
75
  | Kind | Name | Notes |
@@ -85,6 +98,6 @@ This feature came up as: "periodically check for dirty files and commit; LLM pic
85
98
 
86
99
  ## Tests
87
100
 
88
- - `runner.test.ts` — deterministic runner unit tests (status parsing, force-add of `sessions/`, push-with-upstream, push-and-set-upstream when origin exists but tracking is absent, commit-only on no-origin / detached-HEAD, rebase-on-non-fast-forward for both push shapes, diagnose-on-rebase-conflict, advisory-throw isolation, sanitize-commit-message).
101
+ - `runner.test.ts` — deterministic runner unit tests (status parsing, force-add of `sessions/`, push-with-upstream, push-and-set-upstream when origin exists but tracking is absent, commit-only on no-origin / detached-HEAD, rebase-on-non-fast-forward for both push shapes, diagnose-on-rebase-conflict, advisory-throw isolation, sanitize-commit-message; and `runMaintenance` — independent loose/pack gates, loose-only vs repack-only vs both, bounded `-c` flags, `.gitstore` layout, no-op on non-repo, failure surfaced without throwing, skip-on-probe-failure, plus a **real-Git lifecycle** test proving auto-gc-disabled commits trip the loose gate and a real `git maintenance run` packs them into a valid repo).
89
102
  - `git-auth.test.ts` — credential-env resolution (App-auth mints for the origin slug; PAT/SSH/non-github/unavailable-token all fall back to inherited env).
90
- - `index.test.ts` — plugin composition tests (subagent/hook surface, config schema defaults and validation, debounce, active-turn gating, self-induced-turn exclusion, coalescing).
103
+ - `index.test.ts` — plugin composition tests (subagent/hook surface, config schema defaults and validation, debounce, active-turn gating, self-induced-turn exclusion, coalescing) and `backupCommitted` per-result-kind classification (post-commit push/rebase failures still gate maintenance; `clean`/`no-repo`/`commit-failed`/`aborted` do not).
@@ -17,14 +17,16 @@ export type BackupPushAuthDeps = {
17
17
  // The backup runner spawns git directly (not via the bash tool), so the
18
18
  // `github-cli-auth` plugin's `tool.before` credential injection never fires for
19
19
  // its push. Without this, App-auth agents push with no credentials and fail.
20
- // We mirror that plugin exactly: only mint for App auth, only for a github.com
21
- // origin, scoped to the origin's own repo slug. PAT/SSH/credential-helper setups
22
- // return null and keep using the runner's inherited process env.
20
+ // A live App resolver is authoritative even when the process also carries an
21
+ // operator PAT: runtime backup always uses the repo-scoped App credential.
22
+ // Without a resolver, PAT/SSH/credential-helper setups keep using the runner's
23
+ // inherited process env.
23
24
  export async function resolveBackupPushAuthEnv(
24
25
  cwd: string,
25
26
  deps: BackupPushAuthDeps,
26
27
  ): Promise<BackupGitAuthEnv | null> {
27
- if (!shouldMintAppToken(deps.ghToken, deps.hasAppTokenResolver())) return null
28
+ const hasAppTokenResolver = deps.hasAppTokenResolver()
29
+ if (!hasAppTokenResolver && !shouldMintAppToken(deps.ghToken, false)) return null
28
30
 
29
31
  const originUrl = await deps.resolveOriginPushUrl(cwd)
30
32
  if (originUrl === null) return null
@@ -1,11 +1,19 @@
1
1
  import { z } from 'zod'
2
2
 
3
+ import { hooklessGitArgs } from '@/git/hookless'
3
4
  import { withGitLock } from '@/git/mutex'
4
5
  import { resolveAgentGit } from '@/git/resolve-agent-git'
5
6
  import { definePlugin, type PluginContext, type SpawnSubagentOptions, type Subagent } from '@/plugin'
6
7
 
7
8
  import { type BackupPushAuthDeps, makeDefaultAskPassEnsurer, resolveBackupPushAuthEnv } from './git-auth'
8
- import { COMMIT_TIMEOUT_MS, makeDefaultGitSpawn, NETWORK_TIMEOUT_MS, runBackup, type BackupResult } from './runner'
9
+ import {
10
+ COMMIT_TIMEOUT_MS,
11
+ makeDefaultGitSpawn,
12
+ NETWORK_TIMEOUT_MS,
13
+ runBackup,
14
+ runMaintenance,
15
+ type BackupResult,
16
+ } from './runner'
9
17
  import {
10
18
  cleanupMessageFile,
11
19
  type CommitMessagePayload,
@@ -35,6 +43,7 @@ const backupConfigSchema = z
35
43
  enabled: z.boolean().default(true),
36
44
  idleMs: z.number().int().min(MIN_IDLE_MS).default(DEFAULT_IDLE_MS),
37
45
  pushToOrigin: z.boolean().default(true),
46
+ maintenance: z.boolean().default(true),
38
47
  commitTimeoutMs: z.number().int().min(1).default(COMMIT_TIMEOUT_MS),
39
48
  networkTimeoutMs: z.number().int().min(1).default(NETWORK_TIMEOUT_MS),
40
49
  })
@@ -42,6 +51,7 @@ const backupConfigSchema = z
42
51
  enabled: true,
43
52
  idleMs: DEFAULT_IDLE_MS,
44
53
  pushToOrigin: true,
54
+ maintenance: true,
45
55
  commitTimeoutMs: COMMIT_TIMEOUT_MS,
46
56
  networkTimeoutMs: NETWORK_TIMEOUT_MS,
47
57
  })
@@ -49,6 +59,7 @@ const backupConfigSchema = z
49
59
  const runnerPayloadSchema = z.object({
50
60
  agentDir: z.string(),
51
61
  pushToOrigin: z.boolean(),
62
+ maintenance: z.boolean(),
52
63
  })
53
64
 
54
65
  type RunnerPayload = z.infer<typeof runnerPayloadSchema>
@@ -59,6 +70,7 @@ export default definePlugin({
59
70
  const enabled = ctx.config.enabled
60
71
  const idleMs = ctx.config.idleMs
61
72
  const pushToOrigin = ctx.config.pushToOrigin
73
+ const maintenance = ctx.config.maintenance
62
74
 
63
75
  const activeTurns = new Set<string>()
64
76
  let idleTimer: ReturnType<typeof setTimeout> | null = null
@@ -86,6 +98,7 @@ export default definePlugin({
86
98
  {
87
99
  agentDir: ctx.agentDir,
88
100
  pushToOrigin,
101
+ maintenance,
89
102
  } satisfies RunnerPayload,
90
103
  // The backup runner is a system-level operation that commits +
91
104
  // pushes on the operator's behalf. It runs after every idle
@@ -187,53 +200,81 @@ async function runBackupOnce(
187
200
  ? ((await resolveBackupAuthEnv(payload.agentDir, ctx.github, ctx.logger)) ?? undefined)
188
201
  : undefined
189
202
 
190
- const result = await withGitLock(payload.agentDir, () =>
191
- runBackup(
192
- { cwd: payload.agentDir, pushToOrigin: payload.pushToOrigin },
193
- {
194
- gitSpawn: makeDefaultGitSpawn(),
195
- pushEnv,
196
- pickCommitMessage: async ({ status, diffstat }) => {
197
- await cleanupMessageFile(messagePath)
198
- const messagePayload: CommitMessagePayload = {
199
- agentDir: payload.agentDir,
200
- status,
201
- diffstat,
202
- outputPath: messagePath,
203
- }
204
- try {
205
- await ctx.spawnSubagent(SUBAGENT_COMMIT_MESSAGE, messagePayload, inheritOwner)
206
- } catch (err) {
207
- ctx.logger.warn(
208
- `${SUBAGENT_COMMIT_MESSAGE} subagent failed, using fallback: ${err instanceof Error ? err.message : String(err)}`,
209
- )
210
- }
211
- const written = await readMessageFile(messagePath)
212
- await cleanupMessageFile(messagePath)
213
- return written ?? 'chore: backup'
214
- },
215
- diagnoseFailure: async (input) => {
216
- const diagPayload: DiagnoseFailurePayload = {
217
- agentDir: input.cwd,
218
- stage: input.stage,
219
- exitCode: input.exitCode,
220
- stderr: input.stderr,
221
- stdout: input.stdout,
222
- }
223
- try {
224
- await ctx.spawnSubagent(SUBAGENT_DIAGNOSE, diagPayload, inheritOwner)
225
- } catch (err) {
226
- ctx.logger.warn(`${SUBAGENT_DIAGNOSE} subagent failed: ${err instanceof Error ? err.message : String(err)}`)
227
- }
228
- },
229
- },
230
- ),
231
- )
203
+ const gitSpawn = makeDefaultGitSpawn()
204
+ const backupDeps = {
205
+ gitSpawn,
206
+ pushEnv,
207
+ pickCommitMessage: async ({ status, diffstat }: { status: string; diffstat: string }): Promise<string> => {
208
+ await cleanupMessageFile(messagePath)
209
+ const messagePayload: CommitMessagePayload = {
210
+ agentDir: payload.agentDir,
211
+ status,
212
+ diffstat,
213
+ outputPath: messagePath,
214
+ }
215
+ try {
216
+ await ctx.spawnSubagent(SUBAGENT_COMMIT_MESSAGE, messagePayload, inheritOwner)
217
+ } catch (err) {
218
+ ctx.logger.warn(
219
+ `${SUBAGENT_COMMIT_MESSAGE} subagent failed, using fallback: ${err instanceof Error ? err.message : String(err)}`,
220
+ )
221
+ }
222
+ const written = await readMessageFile(messagePath)
223
+ await cleanupMessageFile(messagePath)
224
+ return written ?? 'chore: backup'
225
+ },
226
+ diagnoseFailure: async (input: {
227
+ cwd: string
228
+ stage: 'push' | 'rebase'
229
+ exitCode: number
230
+ stderr: string
231
+ stdout: string
232
+ }): Promise<void> => {
233
+ const diagPayload: DiagnoseFailurePayload = {
234
+ agentDir: input.cwd,
235
+ stage: input.stage,
236
+ exitCode: input.exitCode,
237
+ stderr: input.stderr,
238
+ stdout: input.stdout,
239
+ }
240
+ try {
241
+ await ctx.spawnSubagent(SUBAGENT_DIAGNOSE, diagPayload, inheritOwner)
242
+ } catch (err) {
243
+ ctx.logger.warn(`${SUBAGENT_DIAGNOSE} subagent failed: ${err instanceof Error ? err.message : String(err)}`)
244
+ }
245
+ },
246
+ }
247
+
248
+ // Maintenance runs UNDER THE SAME LOCK as the backup, right after it, so a
249
+ // concurrent backup can't race the repack's index/pack rewrite. It fires
250
+ // whenever a LOCAL COMMIT was made — including when the later push failed, since
251
+ // the loose objects the commit created still need reclaiming (a prolonged remote
252
+ // outage must not suppress it). Best-effort: its outcome is logged, never folded
253
+ // into the returned BackupResult.
254
+ const result = await withGitLock(payload.agentDir, async () => {
255
+ const backup = await runBackup({ cwd: payload.agentDir, pushToOrigin: payload.pushToOrigin }, backupDeps)
256
+ if (payload.maintenance && backupCommitted(backup)) {
257
+ const maint = await runMaintenance(payload.agentDir, backupDeps)
258
+ if (!maint.ok) ctx.logger.warn(`[backup] maintenance failed: ${maint.reason}`)
259
+ else if (maint.kind === 'ran') ctx.logger.info(`[backup] maintenance ran (${(maint.tasks ?? []).join(', ')})`)
260
+ }
261
+ return backup
262
+ })
232
263
 
233
264
  await cleanupMessageFile(messagePath)
234
265
  return result
235
266
  }
236
267
 
268
+ // True when a local commit was created this run, regardless of the push outcome.
269
+ // The commit is made before push recovery, so `push-failed`/`rebase-failed` still
270
+ // grew local history and must trigger reclamation; only `clean`/`no-repo` (no
271
+ // commit) and `commit-failed`/`aborted` (commit never happened) skip it. Exported
272
+ // for direct unit tests of this exact per-kind classification.
273
+ export function backupCommitted(result: BackupResult): boolean {
274
+ if (result.ok) return result.kind !== 'clean' && result.kind !== 'no-repo'
275
+ return result.kind === 'push-failed' || result.kind === 'rebase-failed'
276
+ }
277
+
237
278
  async function resolveBackupAuthEnv(
238
279
  agentDir: string,
239
280
  github: PluginContext['github'],
@@ -265,7 +306,7 @@ export async function resolveOriginPushUrl(cwd: string): Promise<string | null>
265
306
  if (!repo) return null
266
307
  try {
267
308
  const proc = bun.spawn({
268
- cmd: ['git', '-C', cwd, ...repo.gitArgs, 'remote', 'get-url', '--push', 'origin'],
309
+ cmd: ['git', ...hooklessGitArgs(['-C', cwd, ...repo.gitArgs, 'remote', 'get-url', '--push', 'origin'])],
269
310
  stdout: 'pipe',
270
311
  stderr: 'ignore',
271
312
  env: { ...process.env, GIT_TERMINAL_PROMPT: '0', GIT_OPTIONAL_LOCKS: '0' },
@@ -1,10 +1,36 @@
1
1
  import { existsSync } from 'node:fs'
2
2
  import { join } from 'node:path'
3
3
 
4
+ import { hooklessGitArgs } from '@/git/hookless'
4
5
  import { type AgentGit, resolveAgentGit } from '@/git/resolve-agent-git'
5
6
 
6
7
  export const COMMIT_TIMEOUT_MS = 30_000
7
8
  export const NETWORK_TIMEOUT_MS = 60_000
9
+ export const MAINTENANCE_TIMEOUT_MS = 120_000
10
+
11
+ // The two maintenance tasks are gated INDEPENDENTLY because a backup commit with
12
+ // auto-gc disabled (Fix 1) produces LOOSE OBJECTS, not packs:
13
+ // - loose-objects fires on the loose-object count (`count:` in count-objects),
14
+ // which is what actually grows every idle backup cycle. Gating this on pack
15
+ // count would let loose objects accumulate forever while packs stayed ~0.
16
+ // - incremental-repack fires on the pack count (`packs:`), consolidating the
17
+ // packs that loose-objects itself produces once they build up.
18
+ // Each task consolidates its own input, so the counts reset and both self-throttle
19
+ // — no wall-clock timer needed.
20
+ const MAINTENANCE_LOOSE_THRESHOLD = 300
21
+ const MAINTENANCE_PACK_THRESHOLD = 12
22
+
23
+ // Memory caps for the maintenance repack, matching the machine-local config Fix 1
24
+ // writes so the bound holds even on a repo that predates it. Passed as `-c` so
25
+ // they apply to this invocation regardless of the persisted config.
26
+ const BOUNDED_PACK_FLAGS = [
27
+ '-c',
28
+ 'pack.threads=1',
29
+ '-c',
30
+ 'pack.windowMemory=64m',
31
+ '-c',
32
+ 'pack.deltaCacheSize=1',
33
+ ] as const
8
34
 
9
35
  const RUNTIME_OWNED_PREFIXES = ['memory/'] as const
10
36
  const FORCE_ADD_PREFIXES = ['sessions/', 'todo/'] as const
@@ -151,6 +177,64 @@ export async function runBackup(options: BackupRunnerOptions, deps: BackupRunner
151
177
  return pushWithRecovery(cwd, deps, repo, plan)
152
178
  }
153
179
 
180
+ export type MaintenanceResult =
181
+ | { ok: true; kind: 'no-repo' | 'skipped' | 'ran'; tasks?: readonly string[] }
182
+ | { ok: false; kind: 'failed'; reason: string }
183
+
184
+ // Bounded, best-effort git maintenance run on the idle backup path. With auto-gc
185
+ // disabled (Fix 1), nothing reclaims what the backup keeps adding, so this runs
186
+ // the two reclamation tasks — each gated on ITS OWN growing counter (see the
187
+ // threshold comment above): `loose-objects` when loose objects pile up (the
188
+ // steady-state effect of every commit), `incremental-repack` when packs pile up.
189
+ // incremental-repack uses the multi-pack-index, touching only SMALL packs and
190
+ // leaving the big established pack alone, so memory and time stay bounded unlike a
191
+ // full `git gc`. Both tasks run under BOUNDED_PACK_FLAGS. NEVER throws: a
192
+ // maintenance failure must not turn a good commit into a reported backup failure.
193
+ // The caller runs this after a commit, holding the same git lock the commit did.
194
+ export type MaintenanceThresholds = { loose?: number; packs?: number }
195
+
196
+ export async function runMaintenance(
197
+ cwd: string,
198
+ deps: BackupRunnerDeps,
199
+ thresholds: MaintenanceThresholds = {},
200
+ ): Promise<MaintenanceResult> {
201
+ const repo = resolveAgentGit(cwd)
202
+ if (!repo) return { ok: true, kind: 'no-repo' }
203
+
204
+ const looseThreshold = thresholds.loose ?? MAINTENANCE_LOOSE_THRESHOLD
205
+ const packThreshold = thresholds.packs ?? MAINTENANCE_PACK_THRESHOLD
206
+ const counts = await countObjects(cwd, deps, repo)
207
+ const tasks: string[] = []
208
+ if (counts.loose > looseThreshold) tasks.push('loose-objects')
209
+ if (counts.packs > packThreshold) tasks.push('incremental-repack')
210
+ if (tasks.length === 0) return { ok: true, kind: 'skipped' }
211
+
212
+ const run = await deps.gitSpawn(
213
+ [...repo.gitArgs, ...BOUNDED_PACK_FLAGS, 'maintenance', 'run', ...tasks.map((task) => `--task=${task}`)],
214
+ { cwd, timeoutMs: MAINTENANCE_TIMEOUT_MS },
215
+ )
216
+ if (run.exitCode !== 0) return { ok: false, kind: 'failed', reason: shortErr(run) }
217
+ return { ok: true, kind: 'ran', tasks }
218
+ }
219
+
220
+ // Reads the loose-object (`count:`) and pack (`packs:`) counts from
221
+ // `git count-objects -v`. Returns zeroes on any failure so a probe error just
222
+ // skips maintenance rather than aborting it.
223
+ async function countObjects(
224
+ cwd: string,
225
+ deps: BackupRunnerDeps,
226
+ repo: AgentGit,
227
+ ): Promise<{ loose: number; packs: number }> {
228
+ const result = await deps.gitSpawn([...repo.gitArgs, 'count-objects', '-v'], { cwd, timeoutMs: COMMIT_TIMEOUT_MS })
229
+ if (result.exitCode !== 0) return { loose: 0, packs: 0 }
230
+ return { loose: readCount(result.stdout, 'count'), packs: readCount(result.stdout, 'packs') }
231
+ }
232
+
233
+ function readCount(stdout: string, field: 'count' | 'packs'): number {
234
+ const match = new RegExp(String.raw`^${field}:\s*(\d+)`, 'm').exec(stdout)
235
+ return match?.[1] !== undefined ? Number.parseInt(match[1], 10) : 0
236
+ }
237
+
154
238
  // `@{upstream}` resolution failing was previously treated as "no push" — but a
155
239
  // fresh agent repo that nobody ran `git push -u` on has a configured `origin`
156
240
  // and no tracking ref, so the runner committed forever and never pushed. The
@@ -328,7 +412,7 @@ export function makeDefaultGitSpawn(): GitSpawn {
328
412
  // GIT_TERMINAL_PROMPT wins; NONINTERACTIVE_ENV's pager/GCM settings still
329
413
  // apply to every call. Local commands pass no `env` and stay token-free.
330
414
  const proc = bun.spawn({
331
- cmd: ['git', ...args],
415
+ cmd: ['git', ...hooklessGitArgs(args)],
332
416
  cwd,
333
417
  stdout: 'pipe',
334
418
  stderr: 'pipe',
@@ -1,8 +1,11 @@
1
1
  #!/usr/bin/env bun
2
+ import { readFile, realpath } from 'node:fs/promises'
3
+ import { isAbsolute, join, relative, sep } from 'node:path'
4
+
2
5
  // Bundled `bun run`-able render script for the doc-render plugin: the agent runs
3
6
  // `bun run <this> <main.typ> <out.pdf>`. Lives as a script (not a registered
4
7
  // tool) so it costs zero always-on system-prompt context, and the agent
5
- // dynamic-imports the Typst compiler from its own /agent/node_modules — installed
8
+ // dynamic-imports the Typst compiler from writable per-session scratch — installed
6
9
  // on first use, never baked into the image. Its on-disk path is published to a
7
10
  // /tmp hint at boot (see index.ts) so the skill can resolve it.
8
11
  //
@@ -16,6 +19,7 @@
16
19
  // 0.7.0 embeds Typst 0.14.2; bumping is a deliberate, re-validated edit.
17
20
  export const COMPILER_PACKAGE = '@myriaddreamin/typst-ts-node-compiler'
18
21
  export const COMPILER_VERSION = '0.7.0'
22
+ export const COMPILER_RUNTIME_DIR = '/tmp/typeclaw-doc-render-runtime'
19
23
 
20
24
  // NodeCompiler does not auto-discover system font dirs the way the Typst CLI
21
25
  // does; without these, CJK glyphs render as .notdef tofu. Filtered by existence
@@ -26,9 +30,10 @@ export function missingCompilerGuidance(): string {
26
30
  return [
27
31
  `doc-render: ${COMPILER_PACKAGE} is not installed.`,
28
32
  '',
29
- 'One-time PDF toolchain install. Run from the agent root (writes node_modules',
30
- '+ package.json + bun.lock, which survive restarts), then re-run this render:',
33
+ 'Install the PDF toolchain in this session’s writable scratch runtime, then',
34
+ 're-run the render from the document directory:',
31
35
  '',
36
+ ` mkdir -p ${COMPILER_RUNTIME_DIR} && cd ${COMPILER_RUNTIME_DIR}`,
32
37
  ` bun add ${COMPILER_PACKAGE}@${COMPILER_VERSION}`,
33
38
  '',
34
39
  'Do NOT fall back to jsPDF, pdfkit, a canvas text dump, a headless-browser',
@@ -67,15 +72,91 @@ type NodeCompilerModule = {
67
72
  }
68
73
  }
69
74
 
70
- // Resolve from the CALLER's cwd, not this script's location: the agent installs
71
- // the compiler into its own /agent/node_modules and runs `bun run <this>` from
72
- // the agent root, but bare `import(pkg)` resolves relative to this file (under
73
- // node_modules/typeclaw/...), which need not see the agent's deps. Resolving
74
- // against cwd, then importing the absolute path, makes the lookup independent of
75
- // where the bundled script physically lives.
76
- export async function loadCompilerModule(cwd: string = process.cwd()): Promise<NodeCompilerModule> {
77
- const resolved = Bun.resolveSync(COMPILER_PACKAGE, cwd)
78
- return (await import(resolved)) as NodeCompilerModule
75
+ async function isMuslLinux(): Promise<boolean> {
76
+ try {
77
+ if ((await readFile('/usr/bin/ldd', 'utf8')).includes('musl')) return true
78
+ } catch {
79
+ // Continue with the same process-report fallback used by the package loader.
80
+ }
81
+ const report = process.report?.getReport() as
82
+ | { header?: { glibcVersionRuntime?: string }; sharedObjects?: string[] }
83
+ | undefined
84
+ if (report?.header?.glibcVersionRuntime) return false
85
+ return report?.sharedObjects?.some((file) => file.includes('libc.musl-') || file.includes('ld-musl-')) ?? false
86
+ }
87
+
88
+ export async function requiredCompilerPlatformPackage(): Promise<string> {
89
+ const prefix = `${COMPILER_PACKAGE}-`
90
+ if (process.platform === 'darwin' && (process.arch === 'x64' || process.arch === 'arm64')) {
91
+ return `${prefix}darwin-${process.arch}`
92
+ }
93
+ if (process.platform === 'win32' && (process.arch === 'x64' || process.arch === 'arm64')) {
94
+ return `${prefix}win32-${process.arch}-msvc`
95
+ }
96
+ if (process.platform === 'android' && process.arch === 'arm64') return `${prefix}android-arm64`
97
+ if (process.platform === 'android' && process.arch === 'arm') return `${prefix}android-arm-eabi`
98
+ if (process.platform === 'linux') {
99
+ const libc = (await isMuslLinux()) ? 'musl' : 'gnu'
100
+ if (process.arch === 'x64' || process.arch === 'arm64') return `${prefix}linux-${process.arch}-${libc}`
101
+ if (process.arch === 'arm' && libc === 'gnu') return `${prefix}linux-arm-gnueabihf`
102
+ }
103
+ throw new Error(
104
+ `doc-render: ${COMPILER_PACKAGE}@${COMPILER_VERSION} has no published platform package for ${process.platform}/${process.arch}`,
105
+ )
106
+ }
107
+
108
+ type ValidatedRuntimePackage = {
109
+ entry: string
110
+ }
111
+
112
+ async function validateRuntimePackage(
113
+ runtimeDir: string,
114
+ packageName: string,
115
+ version: string,
116
+ ): Promise<ValidatedRuntimePackage> {
117
+ const packageDir = join(runtimeDir, 'node_modules', ...packageName.split('/'))
118
+ const manifestPath = join(packageDir, 'package.json')
119
+ let manifest: { name?: unknown; version?: unknown }
120
+ try {
121
+ manifest = JSON.parse(await readFile(manifestPath, 'utf8')) as { name?: unknown; version?: unknown }
122
+ } catch {
123
+ throw new Error(`doc-render: ${packageName}@${version} is not installed in ${runtimeDir}`)
124
+ }
125
+ if (manifest.name !== packageName || manifest.version !== version) {
126
+ throw new Error(
127
+ `doc-render: expected ${packageName}@${version} in ${runtimeDir}, found ${String(manifest.name)}@${String(manifest.version)}`,
128
+ )
129
+ }
130
+
131
+ const resolved = Bun.resolveSync(packageDir, packageDir)
132
+ const [realRuntimeDir, realPackageDir, realEntry] = await Promise.all([
133
+ realpath(runtimeDir),
134
+ realpath(packageDir),
135
+ realpath(resolved),
136
+ ])
137
+ for (const [candidate, label] of [
138
+ [realPackageDir, 'package'],
139
+ [realEntry, 'entry'],
140
+ ] as const) {
141
+ const fromRuntime = relative(realRuntimeDir, candidate)
142
+ if (fromRuntime === '..' || fromRuntime.startsWith(`..${sep}`) || isAbsolute(fromRuntime)) {
143
+ throw new Error(`doc-render: ${packageName} ${label} resolves outside the scratch runtime`)
144
+ }
145
+ }
146
+ const fromPackage = relative(realPackageDir, realEntry)
147
+ if (fromPackage === '..' || fromPackage.startsWith(`..${sep}`) || isAbsolute(fromPackage)) {
148
+ throw new Error(`doc-render: ${packageName} entry resolves outside its runtime-local package directory`)
149
+ }
150
+ return { entry: realEntry }
151
+ }
152
+
153
+ // Resolve only from the pinned scratch runtime. A document may contain an
154
+ // attacker-controlled node_modules tree, so its cwd must never enter resolution.
155
+ export async function loadCompilerModule(runtimeDir: string = COMPILER_RUNTIME_DIR): Promise<NodeCompilerModule> {
156
+ const platformPackage = await requiredCompilerPlatformPackage()
157
+ const compiler = await validateRuntimePackage(runtimeDir, COMPILER_PACKAGE, COMPILER_VERSION)
158
+ await validateRuntimePackage(runtimeDir, platformPackage, COMPILER_VERSION)
159
+ return (await import(compiler.entry)) as NodeCompilerModule
79
160
  }
80
161
 
81
162
  export async function renderPdf(mainFile: string, outFile: string): Promise<number> {
@@ -9,8 +9,8 @@ You can produce professional PDFs from Markdown. The bundled `doc-render` plugin
9
9
  ships two things: a **themed report library** (`lib.typ`) that does all the
10
10
  styling, and a **render script** that does the compile. The only thing installed
11
11
  on demand is the [Typst](https://typst.app) compiler — a single npm package the
12
- agent `bun add`s into its own `node_modules` the **first time** you need a PDF,
13
- then reuses. No Pandoc, no LaTeX, no headless browser, no PDF toolchain baked
12
+ agent `bun add`s into writable session scratch the **first time** you need a PDF,
13
+ then reuses for that session. No Pandoc, no LaTeX, no headless browser, no PDF toolchain baked
14
14
  into the image.
15
15
 
16
16
  The flow is: **(1)** install the compiler once (`bun add`), **(2)** have your
@@ -44,16 +44,15 @@ which theme fits the document.**
44
44
  When plain markdown in chat is fine, **don't** make a PDF. This is for when a
45
45
  _file_ is the deliverable.
46
46
 
47
- ## Step 0 — install the Typst compiler (once per container)
47
+ ## Step 0 — install the Typst compiler (once per session)
48
48
 
49
49
  The PDF compiler is not baked into the image — install it on first use. It is a
50
50
  single version-pinned npm package (npm pulls only this platform's prebuilt
51
- binary — Linux x64/arm64, glibc or musl). It writes to the agent's
52
- `node_modules` + `package.json` + `bun.lock`, all of which survive restarts, so
53
- this only runs once per container life:
51
+ binary — Linux x64/arm64, glibc or musl). Install it in the session's writable
52
+ `/tmp` runtime; the agent-root dependency tree is protected read-only:
54
53
 
55
54
  ```sh
56
- # Idempotent: bun add is a no-op if it's already the installed version.
55
+ mkdir -p /tmp/typeclaw-doc-render-runtime && cd /tmp/typeclaw-doc-render-runtime
57
56
  bun add @myriaddreamin/typst-ts-node-compiler@0.7.0
58
57
  ```
59
58
 
@@ -61,10 +60,11 @@ The `@0.7.0` pin embeds Typst 0.14.2 and keeps the toolchain reproducible. If yo
61
60
  forget this step, the render script in Step 3 stops with the exact `bun add` line
62
61
  to run, so you can also just try the render and follow its guidance.
63
62
 
64
- > **Where it goes:** the agent's own `node_modules` — the canonical home for
65
- > executable dependencies, gitignored, not user-facing. Do **not** create a
66
- > `package.json` or `node_modules` under `workspace/` for this; let `bun add`
67
- > manage it at the agent root like any other dependency.
63
+ > **Where it goes:** `/tmp/typeclaw-doc-render-runtime`, backed by TypeClaw's
64
+ > per-session scratch directory. It is writable to model-driven bash and reused
65
+ > across calls in the same session, but not promised across container restarts.
66
+ > Do **not** modify `/agent/node_modules` or create a dependency tree under
67
+ > `workspace/`.
68
68
 
69
69
  ## Step 1 — have the markdown ready
70
70
 
@@ -313,8 +313,8 @@ might expect:
313
313
  first (Typst's workspace sandbox won't resolve an import from outside the
314
314
  render's working directory).
315
315
  - **Don't** build a `package.json` / `node_modules` / a render script under
316
- `workspace/`. The compiler installs at the agent root via `bun add`; the render
317
- script and theme library are bundled with the plugin (under
316
+ `workspace/` or modify the protected agent-root dependencies. The compiler
317
+ installs in `/tmp/typeclaw-doc-render-runtime`; the render script and theme library are bundled with the plugin (under
318
318
  `/agent/node_modules/typeclaw/src/bundled-plugins/doc-render/`).
319
319
  - **Don't** attach a PDF to a GitHub channel — that adapter rejects attachments.
320
320
  Link or inline instead.