(opts: AutoCommitOptions)
| 83 | |
| 84 | export async function runAutoCommit(opts: AutoCommitOptions): Promise<AutoCommitResult> { |
| 85 | const { workspacePath, onEvent, kiloClient, messageId, signal, env } = opts; |
| 86 | const redact = createSecretRedactor(process.env, env ?? {}); |
| 87 | const logToFile = (message: string): void => writeLog(redact(message)); |
| 88 | const released = Promise.withResolvers<void>(); |
| 89 | const previous = worktreeAutoCommits.get(workspacePath) ?? Promise.resolve(); |
| 90 | const current = previous |
| 91 | .then(() => released.promise) |
| 92 | .finally(() => { |
| 93 | if (worktreeAutoCommits.get(workspacePath) === current) |
| 94 | worktreeAutoCommits.delete(workspacePath); |
| 95 | }); |
| 96 | worktreeAutoCommits.set(workspacePath, current); |
| 97 | |
| 98 | logToFile(`auto-commit: starting workspacePath=${workspacePath}`); |
| 99 | |
| 100 | try { |
| 101 | signal?.throwIfAborted(); |
| 102 | const cancelled = Promise.withResolvers<never>(); |
| 103 | const onAbort = () => cancelled.reject(signal?.reason); |
| 104 | signal?.addEventListener('abort', onAbort, { once: true }); |
| 105 | try { |
| 106 | await Promise.race([previous, cancelled.promise]); |
| 107 | } finally { |
| 108 | signal?.removeEventListener('abort', onAbort); |
| 109 | } |
| 110 | signal?.throwIfAborted(); |
| 111 | // Check current branch (agent may have switched branches during execution) |
| 112 | const branch = await getCurrentBranch(workspacePath, GIT_LOCAL_TIMEOUT_MS, signal, env); |
| 113 | logToFile(`auto-commit: branch=${branch || '(detached HEAD)'}`); |
| 114 | if (!branch) { |
| 115 | logToFile('auto-commit: skipping - detached HEAD state'); |
| 116 | emitCompleted( |
| 117 | onEvent, |
| 118 | { |
| 119 | success: true, |
| 120 | message: 'Skipped: detached HEAD state', |
| 121 | skipped: true, |
| 122 | }, |
| 123 | messageId |
| 124 | ); |
| 125 | return { success: true, skipped: true }; |
| 126 | } |
| 127 | |
| 128 | // Branch protection: block auto-commit to main/master unless the user |
| 129 | // explicitly targeted this exact branch via the upstreamBranch API param. |
| 130 | if (branch === 'main' || branch === 'master') { |
| 131 | if (opts.upstreamBranch !== branch) { |
| 132 | logToFile(`auto-commit: skipping - protected branch ${branch}`); |
| 133 | emitCompleted( |
| 134 | onEvent, |
| 135 | { |
| 136 | success: true, |
| 137 | message: `Skipped: cannot commit to ${branch}`, |
| 138 | skipped: true, |
| 139 | }, |
| 140 | messageId |
| 141 | ); |
| 142 | return { success: true, skipped: true }; |
no test coverage detected