@shogo-ai/worker 1.0.10

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 (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +432 -0
  3. package/bin/shogo.mjs +38 -0
  4. package/dist/cli.mjs +64 -0
  5. package/package.json +62 -0
  6. package/src/cli.ts +199 -0
  7. package/src/commands/agent.ts +123 -0
  8. package/src/commands/config.ts +29 -0
  9. package/src/commands/doctor.ts +128 -0
  10. package/src/commands/login.ts +126 -0
  11. package/src/commands/logs.ts +24 -0
  12. package/src/commands/project-checkout.ts +179 -0
  13. package/src/commands/project-pull.ts +133 -0
  14. package/src/commands/project-push.ts +89 -0
  15. package/src/commands/runtime.ts +104 -0
  16. package/src/commands/start.ts +290 -0
  17. package/src/commands/status.ts +22 -0
  18. package/src/commands/stop.ts +13 -0
  19. package/src/lib/__tests__/agent-launch.test.ts +92 -0
  20. package/src/lib/__tests__/cloud-login-coverage.test.ts +210 -0
  21. package/src/lib/__tests__/cloud-login.test.ts +218 -0
  22. package/src/lib/__tests__/cloud-sync-watcher.test.ts +214 -0
  23. package/src/lib/__tests__/config.test.ts +141 -0
  24. package/src/lib/__tests__/db-doctor.test.ts +204 -0
  25. package/src/lib/__tests__/git-cloner.test.ts +382 -0
  26. package/src/lib/__tests__/runtime-manager-auto-pull.test.ts +275 -0
  27. package/src/lib/__tests__/runtime-manager-cloud-sync-gate.test.ts +119 -0
  28. package/src/lib/__tests__/runtime-manager-coverage-gaps.test.ts +768 -0
  29. package/src/lib/__tests__/runtime-manager-describe-rejection.test.ts +42 -0
  30. package/src/lib/__tests__/runtime-manager-git-pull.test.ts +207 -0
  31. package/src/lib/__tests__/runtime-manager-idle-eviction.test.ts +196 -0
  32. package/src/lib/__tests__/runtime-manager-restart-cap.test.ts +310 -0
  33. package/src/lib/__tests__/runtime-manager-tree-sitter-env.test.ts +189 -0
  34. package/src/lib/__tests__/runtime-manager-wait-for-health.test.ts +269 -0
  35. package/src/lib/__tests__/runtime-resolver.test.ts +112 -0
  36. package/src/lib/__tests__/tunnel-coverage.test.ts +1094 -0
  37. package/src/lib/__tests__/tunnel-structured-502.test.ts +101 -0
  38. package/src/lib/__tests__/worker-commands-greenfield.test.ts +463 -0
  39. package/src/lib/__tests__/worker-libs-greenfield.test.ts +355 -0
  40. package/src/lib/__tests__/worker-transport-greenfield.test.ts +176 -0
  41. package/src/lib/api-discovery.ts +36 -0
  42. package/src/lib/cloud-login.ts +320 -0
  43. package/src/lib/cloud-sync-watcher.ts +322 -0
  44. package/src/lib/config.ts +68 -0
  45. package/src/lib/db-doctor.ts +355 -0
  46. package/src/lib/device-id.ts +27 -0
  47. package/src/lib/git-cloner.ts +354 -0
  48. package/src/lib/paths.ts +53 -0
  49. package/src/lib/preflight.ts +158 -0
  50. package/src/lib/process-manager.ts +123 -0
  51. package/src/lib/resource-limits.ts +340 -0
  52. package/src/lib/runtime-install.ts +371 -0
  53. package/src/lib/runtime-manager.ts +1873 -0
  54. package/src/lib/runtime-resolver.ts +136 -0
  55. package/src/lib/transport.ts +202 -0
  56. package/src/lib/tunnel.ts +710 -0
  57. package/src/lib/user-agent.ts +28 -0
@@ -0,0 +1,119 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Copyright (C) 2026 Shogo Technologies, Inc.
3
+ /**
4
+ * Tests for `WorkerRuntimeManager.shouldDeferToCloudSync` — the gate that
5
+ * decides whether a spawned runtime gets `SHOGO_CLOUD_SYNC=1` (skip its own
6
+ * S3Sync + checkpoint inserts because the worker's CloudSyncWatcher owns the
7
+ * push back to cloud).
8
+ *
9
+ * The gate must be true ONLY for projects that were actually auto-pulled, not
10
+ * for every project when `autoPull.enabled` is set globally — otherwise a
11
+ * project served from a pre-seeded / template `projectDir` (which has no
12
+ * watcher) would wrongly suppress its own checkpoints.
13
+ *
14
+ * bun test packages/shogo-worker/src/lib/__tests__/runtime-manager-cloud-sync-gate.test.ts
15
+ */
16
+
17
+ import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
18
+ import { mkdtempSync, rmSync, mkdirSync, writeFileSync } from 'node:fs';
19
+ import { tmpdir } from 'node:os';
20
+ import { join } from 'node:path';
21
+
22
+ let scriptedFetch: typeof fetch | null = null;
23
+ const originalFetch = globalThis.fetch;
24
+ beforeEach(() => {
25
+ globalThis.fetch = ((input: any, init?: RequestInit) => {
26
+ if (!scriptedFetch) throw new Error('no scripted fetch set');
27
+ return scriptedFetch(input, init);
28
+ }) as any;
29
+ });
30
+ afterEach(() => {
31
+ globalThis.fetch = originalFetch;
32
+ scriptedFetch = null;
33
+ });
34
+
35
+ /** Manifest + presign + download responses for a files-mode pull. */
36
+ function scriptManifest(files: Array<{ path: string; size: number; content: string }>) {
37
+ return (async (input: any, init?: RequestInit) => {
38
+ const url = typeof input === 'string' ? input : input.url;
39
+ if (url.includes('/workspace/manifest')) {
40
+ return new Response(
41
+ JSON.stringify({
42
+ ok: true,
43
+ projectId: 'p',
44
+ source: 's3',
45
+ generatedAt: '',
46
+ files: files.map((f) => ({ path: f.path, size: f.size, lastModified: null, etag: null })),
47
+ }),
48
+ { status: 200, headers: { 'content-type': 'application/json' } },
49
+ );
50
+ }
51
+ if (url.includes('/s3/presign')) {
52
+ const body = JSON.parse((init?.body as string) || '{}') as { files: Array<{ path: string; action: string }> };
53
+ return new Response(
54
+ JSON.stringify({ ok: true, urls: body.files.map((f) => ({ path: f.path, action: f.action, url: `https://dl.test/${f.path}` })) }),
55
+ { status: 200 },
56
+ );
57
+ }
58
+ if (url.startsWith('https://dl.test/')) {
59
+ const path = url.slice('https://dl.test/'.length);
60
+ const file = files.find((f) => f.path === path);
61
+ return file ? new Response(file.content, { status: 200 }) : new Response('nf', { status: 404 });
62
+ }
63
+ return new Response('unhandled', { status: 500 });
64
+ }) as unknown as typeof fetch;
65
+ }
66
+
67
+ describe('WorkerRuntimeManager.shouldDeferToCloudSync', () => {
68
+ let dir: string;
69
+ beforeEach(() => {
70
+ dir = mkdtempSync(join(tmpdir(), 'wrm-cloud-sync-gate-'));
71
+ });
72
+ afterEach(() => {
73
+ try { rmSync(dir, { recursive: true, force: true }); } catch { /* ignore */ }
74
+ });
75
+
76
+ it('is true only for a project that was actually auto-pulled', async () => {
77
+ const { WorkerRuntimeManager } = await import('../runtime-manager.ts');
78
+ scriptedFetch = scriptManifest([{ path: 'a.ts', size: 1, content: 'A' }]);
79
+
80
+ const mgr = new WorkerRuntimeManager({
81
+ autoPull: { enabled: true, projectsDir: dir, watch: false, useGit: false },
82
+ });
83
+
84
+ // Before any pull: not active.
85
+ expect(mgr.shouldDeferToCloudSync('proj-pulled')).toBe(false);
86
+
87
+ await mgr.ensurePulled('proj-pulled', { cloudUrl: 'https://api.test', apiKey: 'shogo_sk_x' });
88
+
89
+ // Pulled → defer to cloud sync.
90
+ expect(mgr.shouldDeferToCloudSync('proj-pulled')).toBe(true);
91
+ // A different, never-pulled project → keep its own sync/checkpoints.
92
+ expect(mgr.shouldDeferToCloudSync('proj-untouched')).toBe(false);
93
+ });
94
+
95
+ it('is false for a pre-seeded projectDir even when autoPull is enabled (no watcher)', async () => {
96
+ const { WorkerRuntimeManager } = await import('../runtime-manager.ts');
97
+ // No fetch should fire — the caller-provided projectDir short-circuits pull.
98
+ scriptedFetch = (async () => { throw new Error('no fetch expected'); }) as unknown as typeof fetch;
99
+
100
+ const target = join(dir, 'pre-seeded');
101
+ mkdirSync(target, { recursive: true });
102
+ writeFileSync(join(target, 'package.json'), '{}');
103
+
104
+ const mgr = new WorkerRuntimeManager({
105
+ autoPull: { enabled: true, projectsDir: dir, watch: false, useGit: false },
106
+ });
107
+ await mgr.ensurePulled('pre-seeded', { cloudUrl: 'https://api.test', apiKey: 'shogo_sk_x', projectDir: target });
108
+
109
+ // maybeAutoPull returned early (projectDir existed) so the project was
110
+ // never added to pulledProjects → it keeps its own checkpoint behavior.
111
+ expect(mgr.shouldDeferToCloudSync('pre-seeded')).toBe(false);
112
+ });
113
+
114
+ it('is false when autoPull is disabled entirely', async () => {
115
+ const { WorkerRuntimeManager } = await import('../runtime-manager.ts');
116
+ const mgr = new WorkerRuntimeManager({ autoPull: { enabled: false, projectsDir: dir } });
117
+ expect(mgr.shouldDeferToCloudSync('anything')).toBe(false);
118
+ });
119
+ });