runwork 0.14.0 → 0.15.0

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 (83) hide show
  1. package/dist/__tests__/install-scripts.test.js +33 -0
  2. package/dist/agents/__tests__/chatgpt-registry.test.d.ts +1 -0
  3. package/dist/agents/__tests__/chatgpt-registry.test.js +61 -0
  4. package/dist/agents/__tests__/detection.test.js +3 -0
  5. package/dist/agents/detection.js +3 -0
  6. package/dist/agents/intro-skill.js +5 -3
  7. package/dist/agents/registry-data.d.ts +59 -2
  8. package/dist/agents/registry-data.js +176 -0
  9. package/dist/commands/__tests__/clone-args.test.js +8 -1
  10. package/dist/commands/__tests__/clone-source-result.test.d.ts +1 -0
  11. package/dist/commands/__tests__/clone-source-result.test.js +45 -0
  12. package/dist/commands/clone.d.ts +33 -0
  13. package/dist/commands/clone.js +105 -10
  14. package/dist/commands/deploy.js +128 -11
  15. package/dist/commands/dev.js +72 -32
  16. package/dist/commands/doctor.js +64 -3
  17. package/dist/commands/info.d.ts +3 -0
  18. package/dist/commands/info.js +11 -0
  19. package/dist/commands/sync.js +14 -1
  20. package/dist/commands/validate.d.ts +2 -0
  21. package/dist/commands/validate.js +89 -0
  22. package/dist/deploy/__tests__/deploy-state.test.d.ts +1 -0
  23. package/dist/deploy/__tests__/deploy-state.test.js +80 -0
  24. package/dist/deploy/__tests__/deploy-status.test.d.ts +1 -0
  25. package/dist/deploy/__tests__/deploy-status.test.js +41 -0
  26. package/dist/deploy/__tests__/detach-args.test.d.ts +1 -0
  27. package/dist/deploy/__tests__/detach-args.test.js +30 -0
  28. package/dist/deploy/deploy-state.d.ts +36 -0
  29. package/dist/deploy/deploy-state.js +63 -0
  30. package/dist/deploy/deploy-status.d.ts +21 -0
  31. package/dist/deploy/deploy-status.js +36 -0
  32. package/dist/deploy/detach.d.ts +32 -0
  33. package/dist/deploy/detach.js +71 -0
  34. package/dist/dev/__tests__/session.test.js +39 -0
  35. package/dist/dev/__tests__/startup-sync.test.d.ts +1 -0
  36. package/dist/dev/__tests__/startup-sync.test.js +32 -0
  37. package/dist/dev/session.d.ts +9 -0
  38. package/dist/dev/session.js +4 -1
  39. package/dist/dev/startup-sync.d.ts +23 -0
  40. package/dist/dev/startup-sync.js +14 -0
  41. package/dist/generated/version.d.ts +1 -1
  42. package/dist/generated/version.js +1 -1
  43. package/dist/git/__tests__/classify-sync-error.test.d.ts +1 -0
  44. package/dist/git/__tests__/classify-sync-error.test.js +57 -0
  45. package/dist/git/__tests__/ensure-git-credential-helper.test.d.ts +1 -0
  46. package/dist/git/__tests__/ensure-git-credential-helper.test.js +133 -0
  47. package/dist/git/__tests__/ensure-runwork-remote.test.d.ts +1 -0
  48. package/dist/git/__tests__/ensure-runwork-remote.test.js +109 -0
  49. package/dist/git/__tests__/repo-config.test.d.ts +1 -0
  50. package/dist/git/__tests__/repo-config.test.js +26 -0
  51. package/dist/git/classify-sync-error.d.ts +21 -0
  52. package/dist/git/classify-sync-error.js +111 -0
  53. package/dist/git/credentials.d.ts +43 -0
  54. package/dist/git/credentials.js +67 -0
  55. package/dist/git/remote.d.ts +37 -0
  56. package/dist/git/remote.js +75 -0
  57. package/dist/git/repo-config.d.ts +8 -0
  58. package/dist/git/repo-config.js +29 -0
  59. package/dist/health/__tests__/checks.test.js +2 -2
  60. package/dist/health/__tests__/fix.test.d.ts +1 -0
  61. package/dist/health/__tests__/fix.test.js +72 -0
  62. package/dist/health/__tests__/runner-filter.test.d.ts +1 -0
  63. package/dist/health/__tests__/runner-filter.test.js +61 -0
  64. package/dist/health/checks.d.ts +7 -0
  65. package/dist/health/checks.js +50 -36
  66. package/dist/health/fix.d.ts +22 -0
  67. package/dist/health/fix.js +62 -0
  68. package/dist/health/runner.d.ts +7 -1
  69. package/dist/health/runner.js +40 -27
  70. package/dist/index.js +2 -0
  71. package/dist/tools/types.d.ts +5 -0
  72. package/dist/utils/__tests__/help-json.test.d.ts +1 -0
  73. package/dist/utils/__tests__/help-json.test.js +39 -0
  74. package/dist/utils/__tests__/ignore-matcher.test.js +18 -1
  75. package/dist/utils/help-json.d.ts +14 -0
  76. package/dist/utils/help-json.js +4 -1
  77. package/dist/validate/__tests__/validate.test.d.ts +1 -0
  78. package/dist/validate/__tests__/validate.test.js +42 -0
  79. package/dist/validate/freshness.d.ts +28 -0
  80. package/dist/validate/freshness.js +48 -0
  81. package/dist/validate/preview.d.ts +21 -0
  82. package/dist/validate/preview.js +39 -0
  83. package/package.json +1 -1
@@ -13,6 +13,10 @@ import { runAgentWizard } from '../ui/banner.js';
13
13
  import { shouldOutputJson, jsonOut } from '../utils/output.js';
14
14
  import { buildCloneGuide, buildErrorResponse } from '../utils/agent-guidance.js';
15
15
  import { ensureGitIdentity } from '../git/identity.js';
16
+ import { ensureGitCredentialHelper } from '../git/credentials.js';
17
+ import { ensureRunworkRemote } from '../git/remote.js';
18
+ import { hardenRepoForRestrictedFs } from '../git/repo-config.js';
19
+ import { classifySyncError } from '../git/classify-sync-error.js';
16
20
  import { requireGit } from '../git/preflight.js';
17
21
  import { formatError } from '../utils/format-error.js';
18
22
  export async function execClone(client, app, directory, creds) {
@@ -58,16 +62,24 @@ export async function execClone(client, app, directory, creds) {
58
62
  execFileSync('git', ['symbolic-ref', 'HEAD', 'refs/heads/main'], { cwd: dir, stdio: 'pipe' });
59
63
  }
60
64
  catch { /* best-effort: push uses HEAD:main so this is just cleanliness */ }
65
+ // Disable background gc/maintenance so pack rewrites don't fail on
66
+ // restricted filesystems (e.g. some agent "outputs" mounts). Harmless on
67
+ // normal filesystems. See git/repo-config.ts.
68
+ hardenRepoForRestrictedFs(dir);
61
69
  }
62
70
  // Seed a local git identity so subsequent commits do not fail on machines
63
71
  // (commonly fresh Windows installs) without `git config --global user.email`.
64
72
  ensureGitIdentity(dir, creds);
65
- try {
66
- execFileSync('git', ['remote', 'set-url', 'runwork', remoteUrl], { cwd: dir, stdio: 'pipe' });
67
- }
68
- catch {
69
- execFileSync('git', ['remote', 'add', 'runwork', remoteUrl], { cwd: dir, stdio: 'pipe' });
73
+ // Make sure git can authenticate to the runwork remote before we fetch.
74
+ // Headless auth paths (the get_cli_setup MCP tool, or a hand-written
75
+ // ~/.runwork/.credentials) register API creds but not the git helper, so
76
+ // without this the fetch below silently fails and we fall back to a
77
+ // template-only scaffold. See git/credentials.ts:ensureGitCredentialHelper.
78
+ if (creds?.baseUrl) {
79
+ await ensureGitCredentialHelper(creds.baseUrl);
70
80
  }
81
+ ensureRunworkRemote(dir, remoteUrl);
82
+ let source = { pulled: true };
71
83
  try {
72
84
  execFileSync('git', ['fetch', 'runwork', 'main'], { cwd: dir, stdio: 'pipe' });
73
85
  // Reset to remote HEAD - overlays git-tracked files on top of template
@@ -76,11 +88,19 @@ export async function execClone(client, app, directory, creds) {
76
88
  execFileSync('git', ['checkout', '.'], { cwd: dir, stdio: 'pipe' });
77
89
  }
78
90
  catch (err) {
79
- // Distinguish "remote is empty" (expected for brand-new apps) from
80
- // real failures. We print both the generic next-step hint and the
81
- // underlying error so authentication / network failures aren't lost.
82
- console.warn('Could not pull from runwork remote. Starting with template only.');
83
- console.warn(` ${formatError(err)}`);
91
+ source = pullOutcomeFromError(err);
92
+ if (source.reason === 'empty-remote') {
93
+ // Brand-new app: the remote has no `main` branch yet. Expected and
94
+ // benign -- the template scaffold IS the starting point.
95
+ console.warn('No source pushed for this app yet. Starting from the template scaffold.');
96
+ }
97
+ else {
98
+ // A real failure (auth, missing remote, network). The scaffold here is
99
+ // NOT the app's code -- surface it loudly so the caller fails instead of
100
+ // silently editing the wrong files.
101
+ console.warn('Could not pull the app source from the runwork remote.');
102
+ console.warn(` ${formatError(err)}`);
103
+ }
84
104
  }
85
105
  // Write .runwork.json config
86
106
  const config = {
@@ -107,8 +127,45 @@ export async function execClone(client, app, directory, creds) {
107
127
  directory: resolve(dir),
108
128
  workspaceId: app.workspaceId,
109
129
  workspaceName: app.workspaceName,
130
+ source,
110
131
  };
111
132
  }
133
+ /** Pull the raw git stderr off an execFileSync error for classification. */
134
+ function extractStderr(err) {
135
+ const e = err;
136
+ if (e?.stderr)
137
+ return e.stderr.toString().trim();
138
+ return formatError(err);
139
+ }
140
+ /**
141
+ * Whether a failed `git fetch runwork main` means the remote simply has no
142
+ * `main` branch yet (a brand-new app), as opposed to an auth/network/remote
143
+ * failure. Git reports this as "couldn't find remote ref main".
144
+ */
145
+ function isEmptyRemote(stderr) {
146
+ const s = stderr.toLowerCase();
147
+ return s.includes("couldn't find remote ref") || s.includes('could not find remote ref');
148
+ }
149
+ /**
150
+ * Turn a failed-pull error into a CloneSource: an empty remote (benign,
151
+ * brand-new app) vs a classified real failure (auth/network/no-remote) that
152
+ * leaves a template-only scaffold.
153
+ */
154
+ export function pullOutcomeFromError(err) {
155
+ const detail = extractStderr(err);
156
+ if (isEmptyRemote(detail)) {
157
+ return { pulled: false, reason: 'empty-remote', detail };
158
+ }
159
+ return { pulled: false, reason: classifySyncError(detail), detail };
160
+ }
161
+ /**
162
+ * A clone is a real failure -- a template-only scaffold rather than the app's
163
+ * code -- when the source pull didn't happen for any reason other than an
164
+ * empty remote. Callers must not report success in that case.
165
+ */
166
+ export function isCloneSourceFailure(source) {
167
+ return !source.pulled && source.reason !== 'empty-remote';
168
+ }
112
169
  /**
113
170
  * Reconcile positional args with --app. When --app is provided, the first
114
171
  * positional argument is intended as the *directory* -- not another appId
@@ -123,11 +180,24 @@ export function normalizeCloneArgs(appId, directory, options) {
123
180
  }
124
181
  return { appId, directory };
125
182
  }
183
+ /**
184
+ * Help epilogue shown under `runwork clone --help`. Agents tend to read help
185
+ * before running, so we surface the restricted-mount caveat (friction log #9)
186
+ * here. Exported so it can be asserted on directly (commander's help-text
187
+ * hooks aren't reflected by `helpInformation()`).
188
+ */
189
+ export const RESTRICTED_FS_HELP = `
190
+ Note for AI agents: clone into a normal working directory. Some sandboxed
191
+ "outputs" mounts (e.g. certain agent environments) disallow the unlink
192
+ operations git uses for pack files, which can corrupt the repo with errors
193
+ like "unable to unlink '.git/objects/pack/...': Operation not permitted". If
194
+ you hit that, clone into your home directory or another standard path instead.`;
126
195
  export const cloneCommand = new Command('clone')
127
196
  .description('Clone a Runwork app to local development')
128
197
  .argument('[appId]', 'App ID to clone (interactive if omitted)')
129
198
  .argument('[directory]', 'Target directory')
130
199
  .option('--app <name-or-id>', 'App name or ID (skips interactive selection)')
200
+ .addHelpText('after', RESTRICTED_FS_HELP)
131
201
  .action(async (rawAppId, rawDirectory, options) => {
132
202
  requireGit('clone');
133
203
  const { appId, directory } = normalizeCloneArgs(rawAppId, rawDirectory, options);
@@ -156,20 +226,45 @@ export const cloneCommand = new Command('clone')
156
226
  console.log(`Cloning "${app.name}"...`);
157
227
  }
158
228
  const cloneResult = await execClone(client, app, directory, creds);
229
+ // A real pull failure (auth/network/missing-remote) left a template-only
230
+ // scaffold that is NOT the app's deployed code. Report it as a failure so
231
+ // an agent doesn't start editing the wrong files (friction log #1).
232
+ const pullFailed = isCloneSourceFailure(cloneResult.source);
159
233
  if (useJson) {
234
+ if (pullFailed) {
235
+ jsonOut(buildErrorResponse('clone', 'Cloned the scaffold but could not pull the app source', `git could not fetch the app's source (${cloneResult.source.reason}), so the directory holds only the template scaffold, not the deployed code. ${cloneResult.source.detail ?? ''}`.trim(), [
236
+ 'Run `runwork doctor --fix` to repair git auth and the runwork remote',
237
+ `Then re-clone: runwork clone --app ${cloneResult.appName}`,
238
+ ]));
239
+ process.exit(1);
240
+ }
160
241
  const response = {
161
242
  success: true,
162
243
  command: 'clone',
163
244
  result: cloneResult,
245
+ warning: cloneResult.source.reason === 'empty-remote'
246
+ ? 'This app has no source pushed yet. The directory is the starting template scaffold.'
247
+ : undefined,
164
248
  guide: buildCloneGuide(cloneResult.appName, cloneResult.slug),
165
249
  };
166
250
  jsonOut(response);
167
251
  return;
168
252
  }
253
+ if (pullFailed) {
254
+ console.error(`\nCloned the scaffold for "${cloneResult.appName}", but could NOT pull the app source (${cloneResult.source.reason}).`);
255
+ console.error('The directory holds only the template scaffold, not the deployed code.');
256
+ console.error('Fix git access, then re-clone:');
257
+ console.error(' runwork doctor --fix');
258
+ console.error(` runwork clone --app ${cloneResult.appName}`);
259
+ process.exit(1);
260
+ }
169
261
  // No trailing separator: cloneResult.directory is already an absolute
170
262
  // path. Appending '/' here mixed with Windows '\' separators produced
171
263
  // confusing output like `C:\Users\...\app/` on Windows.
172
264
  console.log(`\nApp "${cloneResult.appName}" cloned to ${cloneResult.directory}`);
265
+ if (cloneResult.source.reason === 'empty-remote') {
266
+ console.log('Note: no source pushed for this app yet. Starting from the template scaffold.');
267
+ }
173
268
  console.log(`Remote: ${client.getGitRemoteUrl(cloneResult.workspaceId, cloneResult.appId)}`);
174
269
  await runAgentWizard(cloneResult.directory);
175
270
  console.log(`Next: cd ${cloneResult.slug} && runwork dev`);
@@ -1,11 +1,18 @@
1
- import { Command } from 'commander';
1
+ import { Command, Option } from 'commander';
2
2
  import { readFileSync, existsSync } from 'fs';
3
3
  import { requireAuth } from '../auth/store.js';
4
4
  import { ApiClient } from '../api/client.js';
5
- import { shouldOutputJson, jsonOut } from '../utils/output.js';
5
+ import { shouldOutputJson, jsonOut, jsonLine } from '../utils/output.js';
6
+ import { dim, green, yellow } from '../ui/colors.js';
7
+ import { getHeadSha, writeDeployState, getDeploySummary } from '../deploy/deploy-state.js';
8
+ import { writeDeployStatus, readDeployStatus, deployLogPath } from '../deploy/deploy-status.js';
9
+ import { isInternalDeployChild, buildDeployChildArgs, spawnDetachedDeploy, DEPLOY_DETACHED_CHILD_FLAG } from '../deploy/detach.js';
6
10
  import { buildDeployGuide, buildErrorResponse } from '../utils/agent-guidance.js';
7
11
  import { requireGit } from '../git/preflight.js';
8
12
  import { ensureGitIdentity } from '../git/identity.js';
13
+ import { ensureGitCredentialHelper } from '../git/credentials.js';
14
+ import { ensureRunworkRemote } from '../git/remote.js';
15
+ import { diagnoseSyncError } from '../git/classify-sync-error.js';
9
16
  import { commitWorkingTree } from '../git/auto-commit.js';
10
17
  import { syncWithRemote, hasCommits } from '../git/sync.js';
11
18
  import { snapshotCriticalFiles, restoreMissingCriticalFiles, commitAndPushRestoredFiles } from '../git/critical-files.js';
@@ -14,8 +21,14 @@ import { promptConfirm } from '../utils/prompt.js';
14
21
  export const deployCommand = new Command('deploy')
15
22
  .description('Deploy the current app to production')
16
23
  .option('-y, --yes', 'Skip the confirmation prompt when the working tree differs from the preview')
24
+ .option('--detach', 'Run the deploy in the background and return immediately. Logs go to .runwork/deploy-{stdout,stderr}.log; poll `runwork deploy --status`.')
25
+ .option('--status', 'Show the status (in-progress/succeeded/failed) of the last deploy started from this machine, plus the deployed-vs-local commit.')
26
+ // Hidden marker the `--detach` parent passes to its background child. Not
27
+ // for end users -- registered so commander accepts it on the child.
28
+ .addOption(new Option(DEPLOY_DETACHED_CHILD_FLAG).hideHelp())
17
29
  .action(async (opts, command) => {
18
30
  const useJson = shouldOutputJson(command.optsWithGlobals().json);
31
+ const cwd = process.cwd();
19
32
  if (!existsSync('.runwork.json')) {
20
33
  if (useJson) {
21
34
  jsonOut(buildErrorResponse('deploy', 'No .runwork.json found', 'This directory is not a Runwork app.', ['Run runwork init to create an app first', 'Or cd into an existing app directory']));
@@ -24,11 +37,50 @@ export const deployCommand = new Command('deploy')
24
37
  console.error('No .runwork.json found. Run `runwork init` first.');
25
38
  process.exit(1);
26
39
  }
40
+ // --status: a local read; no auth/git required.
41
+ if (opts.status) {
42
+ printDeployStatus(cwd, useJson);
43
+ return;
44
+ }
27
45
  requireGit('deploy');
28
46
  const config = JSON.parse(readFileSync('.runwork.json', 'utf-8'));
47
+ const isChild = isInternalDeployChild(process.argv);
48
+ // --detach parent: spawn a background child running the real deploy,
49
+ // record an in-progress status, print a poll hint, and exit immediately so
50
+ // the launching shell doesn't time out (friction log #7).
51
+ if (opts.detach && !isChild) {
52
+ const startedAt = new Date().toISOString();
53
+ const handle = spawnDetachedDeploy(buildDeployChildArgs(process.argv), cwd);
54
+ writeDeployStatus(cwd, { state: 'in-progress', startedAt, pid: handle.pid });
55
+ if (useJson) {
56
+ jsonLine({ event: 'deploy_started', detached: true, pid: handle.pid, logPath: deployLogPath(cwd), startedAt, hint: 'Poll `runwork deploy --status` for completion.' });
57
+ }
58
+ else {
59
+ console.log(`Deploy started in the background (pid ${handle.pid}).`);
60
+ console.log(`Logs: ${deployLogPath(cwd)}`);
61
+ console.log('Check: runwork deploy --status');
62
+ }
63
+ return;
64
+ }
65
+ // The detached child finalizes the status file on exit. If it crashes or
66
+ // hits any `process.exit(1)` failure path before the success block marks
67
+ // it done, record `failed` so `--status` doesn't report a stuck deploy.
68
+ // Register this BEFORE requireAuth(): a child whose credentials expired
69
+ // between spawn and run would otherwise exit in requireAuth() with the
70
+ // status stuck at `in-progress`. (requireGit/config above ran identically
71
+ // in the parent before it spawned this child, so they can't fail here.)
72
+ let deployFinalized = false;
73
+ const childStartedAt = readDeployStatus(cwd)?.startedAt ?? new Date().toISOString();
74
+ if (isChild) {
75
+ jsonLine({ event: 'deploy_started', startedAt: childStartedAt });
76
+ process.on('exit', (code) => {
77
+ if (!deployFinalized && code !== 0) {
78
+ writeDeployStatus(cwd, { state: 'failed', startedAt: childStartedAt, finishedAt: new Date().toISOString(), error: `deploy process exited with code ${code}` });
79
+ }
80
+ });
81
+ }
29
82
  const creds = requireAuth();
30
83
  const client = new ApiClient(creds);
31
- const cwd = process.cwd();
32
84
  // Deploy guard: the preview reflects the last *pushed* commit. If the
33
85
  // working tree is dirty or ahead of `runwork/main` and no auto-syncing
34
86
  // dev session is keeping the preview in lockstep, `runwork deploy` will
@@ -42,8 +94,9 @@ export const deployCommand = new Command('deploy')
42
94
  // Agents must never be blocked: the warning rides along in the
43
95
  // final success response below so we proceed without prompting.
44
96
  }
45
- else if (opts.yes || !process.stdin.isTTY) {
46
- // Non-interactive (no TTY) or explicit --yes: warn and proceed.
97
+ else if (opts.yes || isChild || !process.stdin.isTTY) {
98
+ // Non-interactive (no TTY), the detached child, or explicit --yes:
99
+ // warn and proceed without prompting.
47
100
  console.warn(deployGuardWarning);
48
101
  }
49
102
  else {
@@ -61,6 +114,16 @@ export const deployCommand = new Command('deploy')
61
114
  // `ensureGitIdentity` seeds a local identity so the commit cannot fail
62
115
  // on a fresh machine where git user.email/.name were never configured.
63
116
  ensureGitIdentity(cwd, creds);
117
+ // Ensure git can authenticate to the runwork remote before sync/push.
118
+ // Headless auth (get_cli_setup / hand-written credentials) registers API
119
+ // creds but not the git helper. See
120
+ // git/credentials.ts:ensureGitCredentialHelper.
121
+ if (creds?.baseUrl) {
122
+ await ensureGitCredentialHelper(creds.baseUrl);
123
+ }
124
+ // Ensure the `runwork` remote exists and points at the canonical URL, so a
125
+ // manually `git clone`d repo (remote named `origin`) still syncs/pushes.
126
+ ensureRunworkRemote(cwd, client.getGitRemoteUrl(config.workspaceId, config.appId));
64
127
  if (!useJson)
65
128
  console.log('Syncing...');
66
129
  try {
@@ -94,15 +157,14 @@ export const deployCommand = new Command('deploy')
94
157
  }
95
158
  }
96
159
  if (syncResult.status === 'sync-failed') {
97
- const reason = syncResult.error && syncResult.error !== 'stash-conflict'
98
- ? syncResult.error
99
- : 'Local history could not be reconciled with the remote.';
160
+ const diag = diagnoseSyncError(syncResult.error);
100
161
  if (useJson) {
101
- jsonOut(buildErrorResponse('deploy', 'Sync failed before deployment', reason, ['Run git fetch runwork && git rebase runwork/main and resolve any conflicts', 'Or run runwork dev to sync interactively', 'Then retry runwork deploy']));
162
+ jsonOut(buildErrorResponse('deploy', 'Sync failed before deployment', diag.diagnosis, [...diag.suggestions, 'Then retry runwork deploy']));
102
163
  process.exit(1);
103
164
  }
104
- console.error(`Sync failed: ${reason}`);
105
- console.error('Resolve conflicts (git status), or run `runwork dev`, then retry `runwork deploy`.');
165
+ console.error(`Sync failed: ${diag.message}. ${diag.diagnosis}`);
166
+ for (const s of diag.suggestions)
167
+ console.error(` - ${s}`);
106
168
  process.exit(1);
107
169
  }
108
170
  if (!syncResult.pushed) {
@@ -143,6 +205,21 @@ export const deployCommand = new Command('deploy')
143
205
  console.error('Run `runwork info` to check state, or `runwork logs --production` to inspect errors.');
144
206
  process.exit(1);
145
207
  }
208
+ // Record what we just deployed (local HEAD == the pushed/deployed commit)
209
+ // so `runwork info` / `deploy --status` can cheaply compare it to HEAD
210
+ // later without scraping the production bundle.
211
+ const deployedSha = getHeadSha(cwd);
212
+ const finishedAt = new Date().toISOString();
213
+ if (deployedSha) {
214
+ writeDeployState(cwd, { sha: deployedSha, deployedAt: finishedAt, url: deploymentUrl });
215
+ }
216
+ // Mark the deploy finished so the child's exit hook doesn't overwrite this
217
+ // with a `failed` status, and so `runwork deploy --status` reports success.
218
+ deployFinalized = true;
219
+ writeDeployStatus(cwd, { state: 'succeeded', startedAt: childStartedAt, finishedAt, url: deploymentUrl, sha: deployedSha ?? undefined });
220
+ if (isChild) {
221
+ jsonLine({ event: 'deploy_succeeded', url: deploymentUrl, deployedSha, finishedAt });
222
+ }
146
223
  if (useJson) {
147
224
  const response = {
148
225
  success: true,
@@ -151,6 +228,7 @@ export const deployCommand = new Command('deploy')
151
228
  deployed,
152
229
  url: deploymentUrl,
153
230
  appName: config.appName,
231
+ deployedSha,
154
232
  },
155
233
  guide: buildDeployGuide(),
156
234
  ...(deployGuardWarning ? { warning: deployGuardWarning } : {}),
@@ -159,4 +237,43 @@ export const deployCommand = new Command('deploy')
159
237
  return;
160
238
  }
161
239
  console.log(`Deployed: ${deploymentUrl}`);
240
+ if (deployedSha)
241
+ console.log(dim(`Commit: ${deployedSha.slice(0, 7)}`));
162
242
  });
243
+ /**
244
+ * Print the status of the last deploy started from this machine
245
+ * (`runwork deploy --status`), reconciled with the deployed-vs-local commit
246
+ * comparison from deploy-state.
247
+ */
248
+ function printDeployStatus(cwd, useJson) {
249
+ const status = readDeployStatus(cwd);
250
+ const summary = getDeploySummary(cwd);
251
+ if (useJson) {
252
+ jsonOut({ success: true, command: 'deploy', result: { status, deploy: summary } });
253
+ return;
254
+ }
255
+ if (!status) {
256
+ console.log('No deploy has been started from this machine yet.');
257
+ }
258
+ else {
259
+ const label = status.state === 'succeeded' ? green('succeeded')
260
+ : status.state === 'failed' ? yellow('failed')
261
+ : 'in-progress';
262
+ console.log(`Last deploy: ${label}`);
263
+ console.log(dim(` started: ${status.startedAt}`));
264
+ if (status.finishedAt)
265
+ console.log(dim(` finished: ${status.finishedAt}`));
266
+ if (status.url)
267
+ console.log(dim(` url: ${status.url}`));
268
+ if (status.error)
269
+ console.log(yellow(` error: ${status.error}`));
270
+ if (status.state === 'in-progress')
271
+ console.log(dim(` logs: ${deployLogPath(cwd)}`));
272
+ }
273
+ if (summary.deployedShortSha) {
274
+ const sync = summary.inSync === true ? green('in sync')
275
+ : summary.inSync === false ? yellow('local has undeployed commits')
276
+ : dim('unknown');
277
+ console.log(`Commit: deployed ${summary.deployedShortSha}${summary.localShortSha ? `, local ${summary.localShortSha}` : ''} ${sync}`);
278
+ }
279
+ }
@@ -8,6 +8,10 @@ import { watchAndAutoCommit, stopAutoCommit } from '../git/auto-commit.js';
8
8
  import { syncWithRemote } from '../git/sync.js';
9
9
  import { snapshotCriticalFiles, restoreMissingCriticalFiles, commitAndPushRestoredFiles } from '../git/critical-files.js';
10
10
  import { ensureGitIdentity } from '../git/identity.js';
11
+ import { ensureGitCredentialHelper } from '../git/credentials.js';
12
+ import { ensureRunworkRemote } from '../git/remote.js';
13
+ import { diagnoseSyncError } from '../git/classify-sync-error.js';
14
+ import { buildStartupSyncSummary } from '../dev/startup-sync.js';
11
15
  import { requireGit } from '../git/preflight.js';
12
16
  import { startLogTailer } from '../logs/tailer.js';
13
17
  import { startPreviewUrlPoller } from '../dev/preview-url-poller.js';
@@ -110,6 +114,17 @@ export async function execDev(options) {
110
114
  // fresh machine where `git config --global user.email/.name` has never
111
115
  // been set (extremely common on Windows after a clean install).
112
116
  ensureGitIdentity(cwd, creds);
117
+ // Ensure git can authenticate to the runwork remote before the sync loop
118
+ // fetches/pushes. Headless auth (get_cli_setup / hand-written credentials)
119
+ // registers API creds but not the git helper. See
120
+ // git/credentials.ts:ensureGitCredentialHelper.
121
+ if (creds?.baseUrl) {
122
+ await ensureGitCredentialHelper(creds.baseUrl);
123
+ }
124
+ // Ensure the `runwork` remote exists and points at the canonical URL. A
125
+ // manual `git clone <url>` names the remote `origin`, which otherwise makes
126
+ // the sync loop fail with "'runwork' does not appear to be a git repository".
127
+ ensureRunworkRemote(cwd, client.getGitRemoteUrl(config.workspaceId, config.appId));
113
128
  // Detect user edits made outside of `runwork dev`. When this fires
114
129
  // for an AI agent, it almost always means the agent edited code
115
130
  // before starting dev -- the wrong order. We surface this loudly so
@@ -189,6 +204,10 @@ export async function execDev(options) {
189
204
  await populateSkill(cwd, client, config.appId);
190
205
  if (useJson)
191
206
  jsonLine({ event: 'startup', phase: 'skill_fetched', timestamp: ts() });
207
+ // Track the startup sync outcome so `session_started` can tell agents
208
+ // whether the preview reflects their latest edits or is serving stale code
209
+ // (friction log #2: a happy session_started masked a failed initial sync).
210
+ let startupSync = buildStartupSyncSummary({ noSync });
192
211
  // Sync. In no-sync mode we never mutate the working tree: instead of
193
212
  // syncWithRemote (rebase/merge/commit/push) we do a best-effort,
194
213
  // read-only `git fetch` so remote-tracking refs stay current and
@@ -212,6 +231,7 @@ export async function execDev(options) {
212
231
  console.log(dim('Syncing...'));
213
232
  const criticalSnapshot = snapshotCriticalFiles(cwd);
214
233
  const syncResult = syncWithRemote(cwd);
234
+ startupSync = buildStartupSyncSummary({ noSync: false, status: syncResult.status });
215
235
  const restoredCriticalFiles = restoreMissingCriticalFiles(cwd, criticalSnapshot);
216
236
  if (restoredCriticalFiles.length > 0) {
217
237
  const pushed = commitAndPushRestoredFiles(cwd, restoredCriticalFiles);
@@ -233,37 +253,24 @@ export async function execDev(options) {
233
253
  if (useJson) {
234
254
  jsonLine({ event: 'startup', phase: 'sync', status: syncResult.status, pushed: syncResult.pushed, timestamp: ts() });
235
255
  if (syncResult.status === 'sync-failed') {
236
- if (syncResult.error === 'stash-conflict') {
237
- jsonLine({
238
- event: 'error',
239
- phase: 'sync',
240
- timestamp: ts(),
241
- error: {
242
- message: 'Merge conflicts detected after stash pop',
243
- diagnosis: 'Local changes could not be automatically merged with the remote state. The stash pop resulted in conflicts that require manual resolution.',
244
- suggestions: [
245
- 'Resolve the merge conflicts in the affected files',
246
- 'Run git add on resolved files, then git commit',
247
- 'Run runwork dev again after resolving conflicts',
248
- ],
249
- },
250
- });
251
- process.exit(1);
252
- }
256
+ const diag = diagnoseSyncError(syncResult.error);
253
257
  jsonLine({
254
258
  event: 'error',
255
259
  phase: 'sync',
256
260
  timestamp: ts(),
257
261
  error: {
258
- message: syncResult.error ?? 'File sync failed',
259
- diagnosis: 'Could not sync files to the sandbox. This usually means the sandbox session expired or the network connection was interrupted.',
260
- suggestions: [
261
- 'Run runwork dev again to start a fresh session',
262
- 'Check your network connection',
263
- 'Run runwork doctor to diagnose environment issues',
264
- ],
262
+ reason: diag.reason,
263
+ message: diag.message,
264
+ diagnosis: diag.diagnosis,
265
+ suggestions: diag.suggestions,
265
266
  },
266
267
  });
268
+ // A stash-pop conflict leaves the working tree in a state the user
269
+ // must resolve, so we stop. Other sync failures are non-fatal -- the
270
+ // preview keeps serving the last good state -- so we continue.
271
+ if (syncResult.error === 'stash-conflict') {
272
+ process.exit(1);
273
+ }
267
274
  }
268
275
  }
269
276
  else {
@@ -277,16 +284,20 @@ export async function execDev(options) {
277
284
  case 'merged':
278
285
  console.log(yellow(' Merged with Runwork (histories diverged).'));
279
286
  break;
280
- case 'sync-failed':
287
+ case 'sync-failed': {
288
+ const diag = diagnoseSyncError(syncResult.error);
281
289
  if (syncResult.error === 'stash-conflict') {
282
- console.error('Merge conflicts detected after stash pop. Resolve conflicts and restart.');
290
+ console.error(`${diag.message}. ${diag.diagnosis}`);
291
+ for (const s of diag.suggestions)
292
+ console.error(dim(` - ${s}`));
283
293
  process.exit(1);
284
294
  }
285
- console.warn(yellow(`Sync failed. Continuing...`));
286
- if (syncResult.error) {
287
- console.warn(dim(` ${syncResult.error}`));
288
- }
295
+ console.warn(yellow(`Sync failed: ${diag.message}. Continuing...`));
296
+ console.warn(dim(` ${diag.diagnosis}`));
297
+ for (const s of diag.suggestions)
298
+ console.warn(dim(` - ${s}`));
289
299
  break;
300
+ }
290
301
  }
291
302
  if (!syncResult.pushed && syncResult.status !== 'skipped' && syncResult.status !== 'sync-failed') {
292
303
  console.warn(yellow('Push failed. Continuing with current state...'));
@@ -311,13 +322,33 @@ export async function execDev(options) {
311
322
  cliVersion: VERSION,
312
323
  mode,
313
324
  noSync,
325
+ // Persist the startup sync outcome so the detached parent can relay
326
+ // stale-code warnings -- not just the child's own log (friction log #2).
327
+ startupSync,
314
328
  }));
315
- // Emit session_started (JSON) or show banner (human)
329
+ // Emit session_started (JSON) or show banner (human). We always emit
330
+ // session_started -- it's the rendezvous signal `runwork dev --detach`
331
+ // polls for -- but carry the startup sync outcome so agents don't mistake a
332
+ // stale preview for one reflecting their latest edits. `staleCode: true`
333
+ // means the initial sync failed and the preview is serving the last good
334
+ // state, not the current working tree.
316
335
  if (useJson) {
317
- jsonLine({ event: 'session_started', previewUrl: currentPreviewUrl, appName: config.appName, timestamp: ts(), guide: buildDevSessionGuide() });
336
+ jsonLine({
337
+ event: 'session_started',
338
+ previewUrl: currentPreviewUrl,
339
+ appName: config.appName,
340
+ syncStatus: startupSync.syncStatus,
341
+ syncFailed: startupSync.syncFailed,
342
+ staleCode: startupSync.staleCode,
343
+ timestamp: ts(),
344
+ guide: buildDevSessionGuide(),
345
+ });
318
346
  }
319
347
  else {
320
348
  console.log(getDevBanner({ appName: config.appName, previewUrl: currentPreviewUrl, workspaceName: config.workspaceName }));
349
+ if (startupSync.syncFailed) {
350
+ console.log(yellow(' Warning: initial sync failed. The preview shows the last deployed code, NOT your latest changes.'));
351
+ }
321
352
  console.log(getKeyboardHints());
322
353
  console.log('');
323
354
  console.log(dim(' ─────────────────────────────────────────────'));
@@ -568,6 +599,9 @@ async function runDevDetachParent(opts) {
568
599
  switch (outcome.result) {
569
600
  case 'started': {
570
601
  const f = outcome.file;
602
+ // Relay the child's startup sync outcome so a detached caller learns the
603
+ // preview may be serving stale code (the child only logs it otherwise).
604
+ const syncFailed = f.startupSync?.syncFailed ?? false;
571
605
  if (opts.json) {
572
606
  jsonLine({
573
607
  event: 'session_started',
@@ -575,6 +609,9 @@ async function runDevDetachParent(opts) {
575
609
  pid: f.pid,
576
610
  sessionId: f.sessionId,
577
611
  mode: f.mode,
612
+ syncStatus: f.startupSync?.syncStatus,
613
+ syncFailed,
614
+ staleCode: f.startupSync?.staleCode ?? false,
578
615
  timestamp: ts(),
579
616
  guide: buildDevSessionGuide(),
580
617
  });
@@ -583,6 +620,9 @@ async function runDevDetachParent(opts) {
583
620
  console.log('');
584
621
  console.log(`Dev session running in background (PID ${f.pid}).`);
585
622
  console.log(` Preview: ${green(f.previewUrl)}`);
623
+ if (syncFailed) {
624
+ console.log(yellow(' Warning: initial sync failed. The preview shows the last deployed code, NOT your latest changes.'));
625
+ }
586
626
  console.log(dim(` Stop with: runwork dev stop`));
587
627
  console.log(dim(` Logs: tail -f .runwork/dev-stdout.log`));
588
628
  console.log('');