typeclaw 0.46.0 → 0.46.2

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 (75) hide show
  1. package/README.md +1 -1
  2. package/package.json +2 -2
  3. package/src/agent/multimodal/looker.ts +11 -1
  4. package/src/agent/network/safe-http.ts +17 -8
  5. package/src/agent/plugin-tools.ts +1 -3
  6. package/src/agent/tool-file-safety.ts +414 -145
  7. package/src/agent/tools/webfetch/fetch.ts +22 -2
  8. package/src/agent/tools/webfetch/strategies/jq.ts +1 -2
  9. package/src/agent/tools/webfetch/strategies/readability.ts +39 -16
  10. package/src/agent/tools/webfetch/strategies/selector.ts +4 -2
  11. package/src/agent/tools/webfetch/strategies/snapshot.ts +3 -2
  12. package/src/agent/tools/webfetch/tool.ts +3 -3
  13. package/src/agent/tools-without-local-file-operands.ts +9 -6
  14. package/src/bundled-plugins/backup/README.md +23 -10
  15. package/src/bundled-plugins/backup/git-auth.ts +6 -4
  16. package/src/bundled-plugins/backup/index.ts +104 -47
  17. package/src/bundled-plugins/backup/runner.ts +102 -5
  18. package/src/bundled-plugins/github-cli-auth/git-askpass.ts +74 -26
  19. package/src/bundled-plugins/github-cli-auth/git-command.ts +224 -36
  20. package/src/bundled-plugins/github-cli-auth/graphql-auth-nudge.ts +9 -9
  21. package/src/bundled-plugins/github-cli-auth/index.ts +110 -31
  22. package/src/bundled-plugins/github-cli-auth/token-class.ts +4 -6
  23. package/src/bundled-plugins/memory/README.md +9 -0
  24. package/src/bundled-plugins/memory/vector/embed-threads.ts +36 -0
  25. package/src/bundled-plugins/memory/vector/embedder.ts +19 -1
  26. package/src/bundled-plugins/memory/vector/hybrid.ts +10 -2
  27. package/src/bundled-plugins/memory/vector/store.ts +21 -9
  28. package/src/bundled-plugins/researcher/write-report.ts +3 -3
  29. package/src/bundled-plugins/security/policies/outbound-secret-scan.ts +8 -3
  30. package/src/bundled-plugins/security/policies/private-surface-read.ts +616 -50
  31. package/src/bundled-plugins/security/policies/ssrf.ts +119 -34
  32. package/src/channels/adapters/github/inbound.ts +11 -0
  33. package/src/channels/adapters/github/index.ts +48 -54
  34. package/src/channels/adapters/github/reconcile-cooldown-store.ts +178 -0
  35. package/src/channels/adapters/github/reconcile-open-prs.ts +31 -0
  36. package/src/channels/adapters/slack-bot-edit.ts +85 -20
  37. package/src/channels/adapters/slack-bot.ts +7 -2
  38. package/src/channels/adapters/slack-format.ts +245 -0
  39. package/src/channels/github-token-bridge.ts +2 -3
  40. package/src/cli/inspect.ts +16 -0
  41. package/src/container/docker-app.ts +42 -13
  42. package/src/container/shared.ts +19 -6
  43. package/src/container/start.ts +13 -0
  44. package/src/cron/bridge.ts +9 -2
  45. package/src/doctor/checks.ts +5 -2
  46. package/src/git/bound-gc-config.ts +82 -0
  47. package/src/hostd/spawn.ts +133 -47
  48. package/src/init/checkpoint.ts +4 -4
  49. package/src/init/dockerfile.ts +100 -88
  50. package/src/init/gitignore.ts +5 -6
  51. package/src/init/run-bun-install.ts +16 -2
  52. package/src/init/validate-api-key.ts +1 -0
  53. package/src/inspect/item-list.ts +12 -6
  54. package/src/inspect/replay.ts +21 -9
  55. package/src/inspect/session-list.ts +71 -33
  56. package/src/mcp/tools.ts +1 -0
  57. package/src/network/internal-destinations.ts +267 -0
  58. package/src/permissions/permissions.ts +93 -2
  59. package/src/plugin/types.ts +3 -2
  60. package/src/role-claim/code.ts +9 -5
  61. package/src/run/index.ts +45 -36
  62. package/src/sandbox/canonical-secrets.ts +6 -0
  63. package/src/sandbox/env-exposure.ts +18 -15
  64. package/src/sandbox/index.ts +1 -0
  65. package/src/sandbox/policy.ts +5 -3
  66. package/src/sandbox/privileged-runtime.ts +1 -0
  67. package/src/sandbox/symlinks.ts +1 -1
  68. package/src/secrets/export-claude-credentials-file.ts +13 -22
  69. package/src/secrets/export-codex-auth-file.ts +12 -28
  70. package/src/skills/typeclaw-config/SKILL.md +22 -6
  71. package/src/skills/typeclaw-permissions/SKILL.md +22 -9
  72. package/src/skills/typeclaw-plugins/SKILL.md +37 -0
  73. package/src/usage/scan.ts +11 -2
  74. package/typeclaw.schema.json +5 -0
  75. package/src/git/secret-history.ts +0 -371
@@ -7,6 +7,7 @@ import {
7
7
  type PublicHttpDependencies,
8
8
  type PublicHttpResponse,
9
9
  } from '@/agent/network/safe-http'
10
+ import type { InternalDestinationPolicy } from '@/network/internal-destinations'
10
11
 
11
12
  import { MAX_RESPONSE_BYTES } from './types'
12
13
 
@@ -69,6 +70,7 @@ export async function fetchWithLimits(
69
70
  parentSignal?: AbortSignal,
70
71
  antibotWarmup: AntibotWarmup = 'auto',
71
72
  network: WebFetchNetworkDependencies = forceFallbackForTest ? testFetchDependencies : defaultPublicHttpDependencies,
73
+ internalDestinationPolicy?: InternalDestinationPolicy,
72
74
  ): Promise<FetchResult> {
73
75
  const controller = new AbortController()
74
76
  const timeout = setTimeout(() => controller.abort(new Error('timeout')), timeoutSeconds * 1000)
@@ -78,7 +80,15 @@ export async function fetchWithLimits(
78
80
  try {
79
81
  const cookieJar = new WarmupCookieJar()
80
82
  const visitedUrls = new Set<string>()
81
- const first = await fetchOnce(url, controller.signal, network, REQUEST_HEADERS, cookieJar, visitedUrls)
83
+ const first = await fetchOnce(
84
+ url,
85
+ controller.signal,
86
+ network,
87
+ REQUEST_HEADERS,
88
+ cookieJar,
89
+ visitedUrls,
90
+ internalDestinationPolicy,
91
+ )
82
92
  const cookieNames = cookieJar.names()
83
93
  const warmup =
84
94
  antibotWarmup === 'auto' &&
@@ -97,7 +107,15 @@ export async function fetchWithLimits(
97
107
  initialSetCookieNames: cookieNames,
98
108
  })
99
109
  }
100
- const replay = await fetchOnce(url, controller.signal, network, REQUEST_HEADERS, cookieJar)
110
+ const replay = await fetchOnce(
111
+ url,
112
+ controller.signal,
113
+ network,
114
+ REQUEST_HEADERS,
115
+ cookieJar,
116
+ undefined,
117
+ internalDestinationPolicy,
118
+ )
101
119
  return toFetchResult(replay, {
102
120
  attempted: true,
103
121
  triggered: true,
@@ -152,11 +170,13 @@ async function fetchOnce(
152
170
  headers: Record<string, string>,
153
171
  cookieJar?: WarmupCookieJar,
154
172
  visitedUrls?: Set<string>,
173
+ internalDestinationPolicy?: InternalDestinationPolicy,
155
174
  ): Promise<BufferedResponse> {
156
175
  const { response, finalUrl } = await requestPublicHttpUrl(url, {
157
176
  signal,
158
177
  headers,
159
178
  dependencies: network,
179
+ ...(internalDestinationPolicy === undefined ? {} : { internalDestinationPolicy }),
160
180
  headersForUrl(currentUrl): Record<string, string> {
161
181
  visitedUrls?.add(currentUrl)
162
182
  const cookie = cookieJar?.header(currentUrl)
@@ -1,5 +1,3 @@
1
- import { raw } from 'jq-wasm'
2
-
3
1
  export class JqError extends Error {
4
2
  constructor(message: string) {
5
3
  super(message)
@@ -17,6 +15,7 @@ export async function applyJq(content: string, query: string): Promise<string> {
17
15
  }
18
16
 
19
17
  try {
18
+ const { raw } = await import('jq-wasm')
20
19
  const result = await raw(parsed as string | object, query)
21
20
  if (result.exitCode !== 0 || result.stderr) {
22
21
  const detail = result.stderr.trim() || `exit code ${result.exitCode}`
@@ -1,19 +1,42 @@
1
- import { Readability } from '@mozilla/readability'
2
- import { JSDOM } from 'jsdom'
3
- import TurndownService from 'turndown'
4
-
5
- const turndown = new TurndownService({
6
- headingStyle: 'atx',
7
- codeBlockStyle: 'fenced',
8
- bulletListMarker: '-',
9
- emDelimiter: '*',
10
- hr: '---',
11
- })
12
- turndown.remove(['script', 'style', 'meta', 'link', 'noscript', 'iframe'])
13
-
14
- type ReadabilityDocument = ConstructorParameters<typeof Readability>[0]
15
-
16
- export function applyReadability(html: string, url: string): string {
1
+ import type { Readability as ReadabilityClass } from '@mozilla/readability'
2
+ import type TurndownService from 'turndown'
3
+
4
+ // Perf: jsdom (+readability+turndown) costs ~40-60MB RSS at import time. Keep it
5
+ // lazy so idle agents that never call web_fetch don't pay for it. Do NOT hoist to
6
+ // a top-level import.
7
+ type ReadabilityDeps = {
8
+ Readability: typeof ReadabilityClass
9
+ JSDOM: typeof import('jsdom').JSDOM
10
+ turndown: TurndownService
11
+ }
12
+
13
+ let depsPromise: Promise<ReadabilityDeps> | undefined
14
+
15
+ async function loadDeps(): Promise<ReadabilityDeps> {
16
+ depsPromise ??= (async () => {
17
+ const [{ Readability }, { JSDOM }, { default: TurndownCtor }] = await Promise.all([
18
+ import('@mozilla/readability'),
19
+ import('jsdom'),
20
+ import('turndown'),
21
+ ])
22
+ const turndown = new TurndownCtor({
23
+ headingStyle: 'atx',
24
+ codeBlockStyle: 'fenced',
25
+ bulletListMarker: '-',
26
+ emDelimiter: '*',
27
+ hr: '---',
28
+ })
29
+ turndown.remove(['script', 'style', 'meta', 'link', 'noscript', 'iframe'])
30
+ return { Readability, JSDOM, turndown }
31
+ })()
32
+ return depsPromise
33
+ }
34
+
35
+ type ReadabilityDocument = ConstructorParameters<typeof ReadabilityClass>[0]
36
+
37
+ export async function applyReadability(html: string, url: string): Promise<string> {
38
+ const { Readability, JSDOM, turndown } = await loadDeps()
39
+
17
40
  const dom = new JSDOM(html, { url })
18
41
  const document = dom.window.document.cloneNode(true) as unknown as ReadabilityDocument
19
42
  const article = new Readability(document).parse()
@@ -1,4 +1,4 @@
1
- import * as cheerio from 'cheerio'
1
+ import type * as cheerio from 'cheerio'
2
2
 
3
3
  export class SelectorError extends Error {
4
4
  constructor(message: string) {
@@ -7,7 +7,9 @@ export class SelectorError extends Error {
7
7
  }
8
8
  }
9
9
 
10
- export function applySelector(html: string, selector: string): string {
10
+ export async function applySelector(html: string, selector: string): Promise<string> {
11
+ const cheerio = await import('cheerio')
12
+
11
13
  let $: cheerio.CheerioAPI
12
14
  try {
13
15
  $ = cheerio.load(html)
@@ -1,4 +1,4 @@
1
- import * as cheerio from 'cheerio'
1
+ import type * as cheerio from 'cheerio'
2
2
  import type { AnyNode, Element } from 'domhandler'
3
3
 
4
4
  const SEMANTIC_TAGS = new Set([
@@ -65,7 +65,8 @@ const ROLE_FOR_TAG: Record<string, string> = {
65
65
  label: 'label',
66
66
  }
67
67
 
68
- export function applySnapshot(html: string): string {
68
+ export async function applySnapshot(html: string): Promise<string> {
69
+ const cheerio = await import('cheerio')
69
70
  const $ = cheerio.load(html)
70
71
  const lines: string[] = []
71
72
  const body = $('body').get(0)
@@ -226,7 +226,7 @@ async function runStrategy(
226
226
  case 'raw':
227
227
  return applyRaw(body)
228
228
  case 'readability':
229
- return applyReadability(body, url)
229
+ return await applyReadability(body, url)
230
230
  case 'jq':
231
231
  try {
232
232
  return await applyJq(body, params.query ?? '')
@@ -236,7 +236,7 @@ async function runStrategy(
236
236
  }
237
237
  case 'selector':
238
238
  try {
239
- return applySelector(body, params.selector ?? '')
239
+ return await applySelector(body, params.selector ?? '')
240
240
  } catch (error) {
241
241
  if (error instanceof SelectorError) throw new Error(error.message)
242
242
  throw error
@@ -255,7 +255,7 @@ async function runStrategy(
255
255
  throw error
256
256
  }
257
257
  case 'snapshot':
258
- return applySnapshot(body)
258
+ return await applySnapshot(body)
259
259
  }
260
260
  }
261
261
 
@@ -1,10 +1,11 @@
1
- // First-party system tools whose EVERY argument is a control token or a remote
2
- // identifier (channel/workspace/message/thread/cursor ids, subagent names, task
3
- // ids, role capabilities, reload/stream scopes) — none is ever dereferenced as a
4
- // local path. Listing the whole tool is more robust than enumerating each
1
+ // First-party system tools with no caller-supplied local filesystem operand.
2
+ // Their arguments are control tokens, remote identifiers (channel/workspace/
3
+ // message/thread/cursor ids, subagent names, task ids, role capabilities,
4
+ // reload/stream scopes), or sanitized output metadata — none is dereferenced as
5
+ // a caller-selected local path. Listing the whole tool is more robust than enumerating each
5
6
  // operand: these schemas gain new id/enum fields often, and every such field
6
- // hits the same false positives (a Slack `ts` like "1699999999.000100" matches
7
- // the `word.ext` rule; a value equal to an agent-root dir like "memory" hits the
7
+ // hits the same conservative inference rules (a Slack `ts` like
8
+ // "1699999999.000100" looks like a dotted filename; a value equal to an agent-root dir like "memory" hits the
8
9
  // fs-existence probe; a base64 cursor carries a "/"). The security cost is zero
9
10
  // TODAY because none reads a local file; the risk is future drift, so this set
10
11
  // is the single fence, shared by BOTH enforcement points — the file-operand
@@ -17,7 +18,9 @@ export const TOOLS_WITHOUT_LOCAL_FILE_OPERANDS: ReadonlySet<string> = new Set([
17
18
  'channel_history',
18
19
  'channel_edit',
19
20
  'channel_react',
21
+ 'channel_fetch_attachment',
20
22
  'look_at_channel_attachment',
23
+ 'post_github_review',
21
24
  'stream_snapshot',
22
25
  'grant_role',
23
26
  'spawn_subagent',
@@ -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
@@ -6,7 +6,14 @@ import { resolveAgentGit } from '@/git/resolve-agent-git'
6
6
  import { definePlugin, type PluginContext, type SpawnSubagentOptions, type Subagent } from '@/plugin'
7
7
 
8
8
  import { type BackupPushAuthDeps, makeDefaultAskPassEnsurer, resolveBackupPushAuthEnv } from './git-auth'
9
- 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'
10
17
  import {
11
18
  cleanupMessageFile,
12
19
  type CommitMessagePayload,
@@ -36,6 +43,7 @@ const backupConfigSchema = z
36
43
  enabled: z.boolean().default(true),
37
44
  idleMs: z.number().int().min(MIN_IDLE_MS).default(DEFAULT_IDLE_MS),
38
45
  pushToOrigin: z.boolean().default(true),
46
+ maintenance: z.boolean().default(true),
39
47
  commitTimeoutMs: z.number().int().min(1).default(COMMIT_TIMEOUT_MS),
40
48
  networkTimeoutMs: z.number().int().min(1).default(NETWORK_TIMEOUT_MS),
41
49
  })
@@ -43,6 +51,7 @@ const backupConfigSchema = z
43
51
  enabled: true,
44
52
  idleMs: DEFAULT_IDLE_MS,
45
53
  pushToOrigin: true,
54
+ maintenance: true,
46
55
  commitTimeoutMs: COMMIT_TIMEOUT_MS,
47
56
  networkTimeoutMs: NETWORK_TIMEOUT_MS,
48
57
  })
@@ -50,6 +59,7 @@ const backupConfigSchema = z
50
59
  const runnerPayloadSchema = z.object({
51
60
  agentDir: z.string(),
52
61
  pushToOrigin: z.boolean(),
62
+ maintenance: z.boolean(),
53
63
  })
54
64
 
55
65
  type RunnerPayload = z.infer<typeof runnerPayloadSchema>
@@ -60,6 +70,7 @@ export default definePlugin({
60
70
  const enabled = ctx.config.enabled
61
71
  const idleMs = ctx.config.idleMs
62
72
  const pushToOrigin = ctx.config.pushToOrigin
73
+ const maintenance = ctx.config.maintenance
63
74
 
64
75
  const activeTurns = new Set<string>()
65
76
  let idleTimer: ReturnType<typeof setTimeout> | null = null
@@ -87,6 +98,7 @@ export default definePlugin({
87
98
  {
88
99
  agentDir: ctx.agentDir,
89
100
  pushToOrigin,
101
+ maintenance,
90
102
  } satisfies RunnerPayload,
91
103
  // The backup runner is a system-level operation that commits +
92
104
  // pushes on the operator's behalf. It runs after every idle
@@ -188,53 +200,81 @@ async function runBackupOnce(
188
200
  ? ((await resolveBackupAuthEnv(payload.agentDir, ctx.github, ctx.logger)) ?? undefined)
189
201
  : undefined
190
202
 
191
- const result = await withGitLock(payload.agentDir, () =>
192
- runBackup(
193
- { cwd: payload.agentDir, pushToOrigin: payload.pushToOrigin },
194
- {
195
- gitSpawn: makeDefaultGitSpawn(),
196
- pushEnv,
197
- pickCommitMessage: async ({ status, diffstat }) => {
198
- await cleanupMessageFile(messagePath)
199
- const messagePayload: CommitMessagePayload = {
200
- agentDir: payload.agentDir,
201
- status,
202
- diffstat,
203
- outputPath: messagePath,
204
- }
205
- try {
206
- await ctx.spawnSubagent(SUBAGENT_COMMIT_MESSAGE, messagePayload, inheritOwner)
207
- } catch (err) {
208
- ctx.logger.warn(
209
- `${SUBAGENT_COMMIT_MESSAGE} subagent failed, using fallback: ${err instanceof Error ? err.message : String(err)}`,
210
- )
211
- }
212
- const written = await readMessageFile(messagePath)
213
- await cleanupMessageFile(messagePath)
214
- return written ?? 'chore: backup'
215
- },
216
- diagnoseFailure: async (input) => {
217
- const diagPayload: DiagnoseFailurePayload = {
218
- agentDir: input.cwd,
219
- stage: input.stage,
220
- exitCode: input.exitCode,
221
- stderr: input.stderr,
222
- stdout: input.stdout,
223
- }
224
- try {
225
- await ctx.spawnSubagent(SUBAGENT_DIAGNOSE, diagPayload, inheritOwner)
226
- } catch (err) {
227
- ctx.logger.warn(`${SUBAGENT_DIAGNOSE} subagent failed: ${err instanceof Error ? err.message : String(err)}`)
228
- }
229
- },
230
- },
231
- ),
232
- )
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
+ })
233
263
 
234
264
  await cleanupMessageFile(messagePath)
235
265
  return result
236
266
  }
237
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
+
238
278
  async function resolveBackupAuthEnv(
239
279
  agentDir: string,
240
280
  github: PluginContext['github'],
@@ -259,8 +299,11 @@ async function resolveBackupAuthEnv(
259
299
  }
260
300
  }
261
301
 
262
- export async function resolveOriginPushUrl(cwd: string): Promise<string | null> {
263
- const bun = (globalThis as { Bun?: { spawn: typeof Bun.spawn } }).Bun
302
+ export async function resolveOriginPushUrl(
303
+ cwd: string,
304
+ bunOverride?: { spawn: typeof Bun.spawn },
305
+ ): Promise<string | null> {
306
+ const bun = bunOverride ?? (globalThis as { Bun?: { spawn: typeof Bun.spawn } }).Bun
264
307
  if (!bun) return null
265
308
  const repo = resolveAgentGit(cwd)
266
309
  if (!repo) return null
@@ -271,8 +314,22 @@ export async function resolveOriginPushUrl(cwd: string): Promise<string | null>
271
314
  stderr: 'ignore',
272
315
  env: { ...process.env, GIT_TERMINAL_PROMPT: '0', GIT_OPTIONAL_LOCKS: '0' },
273
316
  })
274
- if ((await proc.exited) !== 0) return null
275
- const out = (await new Response(proc.stdout).text()).trim()
317
+ const exited = proc.exited
318
+ const stdoutDrain = new Response(proc.stdout).text()
319
+ let exitCode: number
320
+ let stdout: string
321
+ try {
322
+ ;[exitCode, stdout] = await Promise.all([exited, stdoutDrain])
323
+ } catch {
324
+ // A stdout-drain rejection must not leave the git child running: kill it
325
+ // and await exit + drain before returning, so `Promise.all`'s early reject
326
+ // can't orphan the subprocess or its pipe.
327
+ proc.kill()
328
+ await Promise.allSettled([exited, stdoutDrain])
329
+ return null
330
+ }
331
+ if (exitCode !== 0) return null
332
+ const out = stdout.trim()
276
333
  return out === '' ? null : out
277
334
  } catch {
278
335
  return null