litmus-cli 1.4.3 → 1.4.6

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 (45) hide show
  1. package/dist/commands/doctor.d.ts +2 -21
  2. package/dist/commands/doctor.d.ts.map +1 -1
  3. package/dist/commands/doctor.js +41 -33
  4. package/dist/commands/doctor.js.map +1 -1
  5. package/dist/commands/submit.d.ts.map +1 -1
  6. package/dist/commands/submit.js +17 -1
  7. package/dist/commands/submit.js.map +1 -1
  8. package/dist/lib/config.d.ts +51 -0
  9. package/dist/lib/config.d.ts.map +1 -1
  10. package/dist/lib/config.js +76 -0
  11. package/dist/lib/config.js.map +1 -1
  12. package/dist/lib/deadline.d.ts +92 -14
  13. package/dist/lib/deadline.d.ts.map +1 -1
  14. package/dist/lib/deadline.js +112 -17
  15. package/dist/lib/deadline.js.map +1 -1
  16. package/dist/lib/detect-project.d.ts.map +1 -1
  17. package/dist/lib/detect-project.js +48 -2
  18. package/dist/lib/detect-project.js.map +1 -1
  19. package/dist/lib/detection.d.ts +32 -0
  20. package/dist/lib/detection.d.ts.map +1 -0
  21. package/dist/lib/detection.js +80 -0
  22. package/dist/lib/detection.js.map +1 -0
  23. package/dist/lib/hook-logger.cjs +50 -2
  24. package/dist/lib/platform.d.ts +34 -10
  25. package/dist/lib/platform.d.ts.map +1 -1
  26. package/dist/lib/platform.js +56 -16
  27. package/dist/lib/platform.js.map +1 -1
  28. package/dist/lib/schemas.d.ts +6 -0
  29. package/dist/lib/schemas.d.ts.map +1 -1
  30. package/dist/lib/schemas.js +8 -0
  31. package/dist/lib/schemas.js.map +1 -1
  32. package/dist/lib/shadow.d.ts +79 -0
  33. package/dist/lib/shadow.d.ts.map +1 -0
  34. package/dist/lib/shadow.js +268 -0
  35. package/dist/lib/shadow.js.map +1 -0
  36. package/dist/lib/tracker.d.ts +1 -1
  37. package/dist/lib/tracker.d.ts.map +1 -1
  38. package/dist/lib/tracker.js +42 -4
  39. package/dist/lib/tracker.js.map +1 -1
  40. package/dist/lib/watcher.js +436 -85
  41. package/dist/lib/watcher.js.map +1 -1
  42. package/dist/lib/zip.d.ts.map +1 -1
  43. package/dist/lib/zip.js +24 -1
  44. package/dist/lib/zip.js.map +1 -1
  45. package/package.json +1 -1
@@ -155,6 +155,47 @@ function isAssessmentLitmusDir(litmusDir) {
155
155
  }
156
156
  }
157
157
 
158
+ // ── Shadow fallback (ENG-1453; kept in step with cli/src/lib/shadow.ts) ──
159
+ // `litmus init`/`doctor` mirror each assessment's config to
160
+ // ~/.litmus/assessments/<assessmentId>/config.json, stamped with the project
161
+ // dir it was written for. When the in-repo config is deleted mid-session
162
+ // (the ENG-1446 incident), that mirror lets capture keep working instead of
163
+ // exiting silently. Gated on the caller: only registry-listed project dirs
164
+ // that still exist are ever matched, so a deleted or retired assessment can
165
+ // never resurrect capture (ENG-953 principle).
166
+
167
+ function realpathOr(p) {
168
+ // macOS: process.cwd() is realpath-resolved but stored paths may not be
169
+ // (e.g. /var vs /private/var), so compare both sides through realpath.
170
+ try { return fs.realpathSync(p) } catch { return path.resolve(p) }
171
+ }
172
+
173
+ function readShadowConfigForProject(projectDir) {
174
+ try {
175
+ const home = os.homedir()
176
+ if (!home) return null
177
+ const root = path.join(home, ".litmus", "assessments")
178
+ const target = realpathOr(projectDir)
179
+ for (const id of fs.readdirSync(root)) {
180
+ let cfg
181
+ try {
182
+ cfg = JSON.parse(fs.readFileSync(path.join(root, id, "config.json"), "utf8"))
183
+ } catch { continue }
184
+ if (!cfg || typeof cfg.projectDir !== "string" || typeof cfg.token !== "string") continue
185
+ if (realpathOr(cfg.projectDir) === target) return cfg
186
+ }
187
+ } catch { /* no shadows on this machine */ }
188
+ return null
189
+ }
190
+
191
+ function isLiveProjectDir(p) {
192
+ try {
193
+ return fs.statSync(p).isDirectory()
194
+ } catch {
195
+ return false
196
+ }
197
+ }
198
+
158
199
  // Walk up from `startDir` looking for an assessment .litmus dir.
159
200
  function walkUpForLitmusDir(startDir) {
160
201
  let dir = startDir
@@ -222,7 +263,12 @@ function findLitmusDir(hintDirs = [], sessionDirs = []) {
222
263
  const list = JSON.parse(fs.readFileSync(registry, "utf8"))
223
264
  if (!Array.isArray(list)) return null
224
265
  const valid = list.filter(
225
- p => typeof p === "string" && isAssessmentLitmusDir(path.join(p, ".litmus")),
266
+ p => typeof p === "string" && (
267
+ isAssessmentLitmusDir(path.join(p, ".litmus"))
268
+ // ENG-1453: config wiped but the assessment is alive — the dir still
269
+ // exists and its shadow mirror supplies identity + credentials.
270
+ || (isLiveProjectDir(p) && readShadowConfigForProject(p) !== null)
271
+ ),
226
272
  )
227
273
  if (!valid.length) return null
228
274
 
@@ -245,7 +291,9 @@ function readConfig(litmusDir) {
245
291
  const configPath = path.join(litmusDir, "config.json")
246
292
  return JSON.parse(fs.readFileSync(configPath, "utf8"))
247
293
  } catch {
248
- return null
294
+ // ENG-1453: in-repo config gone — the shadow mirror keeps capture alive
295
+ // until the watcher's self-heal restores the file.
296
+ return readShadowConfigForProject(path.dirname(litmusDir))
249
297
  }
250
298
  }
251
299
 
@@ -17,21 +17,45 @@ export declare function getProcessList(): string;
17
17
  * Calls cb(err, stdout) with raw connection output.
18
18
  * Handles platform fallbacks internally (lsof → ss on Linux).
19
19
  *
20
- * - macOS: `lsof -i -n -P`
21
- * - Linux: `lsof -i -n -P`, falls back to `ss -tnp` if lsof is unavailable
22
- * - Windows: `netstat -ano`
20
+ * - macOS: `lsof -i -n -P +c0` (+c0 = untruncated COMMAND, so the owning
21
+ * process is attributable default lsof cuts it at 9 chars, which mangles
22
+ * names like `com.apple.WebKit.Networking`)
23
+ * - Linux: same lsof, falls back to `ss -tnp` if lsof is unavailable
24
+ * - Windows: `netstat -ano` (PID only; resolve names via resolvePidNames)
23
25
  */
24
26
  export declare function getConnections(cb: (err: Error | null, stdout?: string) => void): void;
27
+ export interface ConnectionMatch {
28
+ ip: string;
29
+ port: string;
30
+ /**
31
+ * Name of the process that owns the connection, exactly as the OS reported
32
+ * it — case matters (macOS: `Claude` is the Desktop app, `claude` is the
33
+ * Claude Code CLI). Empty when the OS output doesn't carry a name on the
34
+ * line (Windows netstat, permission-restricted ss rows).
35
+ */
36
+ owner: string;
37
+ /** Owning PID, only where the OS gives a PID instead of a name (Windows). */
38
+ pid?: string;
39
+ }
25
40
  /**
26
- * Extracts the destination IP address from a single connection output line.
27
- * Returns a regex match array [fullMatch, ip, port] or null.
41
+ * Parses a single connection output line into destination IP/port plus the
42
+ * owning process, or null if the line isn't a connection row.
28
43
  *
29
44
  * Format varies by tool:
30
- * lsof: "... ->104.18.7.42:443 ..."
31
- * ss: "... 104.18.7.42:443" (IP:port at end of line)
32
- * netstat: " TCP local:port foreign:port STATE PID" (3rd column)
45
+ * lsof: "COMMAND PID USER ... ->104.18.7.42:443 (ESTABLISHED)" (owner = col 1)
46
+ * ss -tnp: "... dst:port users:((\"chrome\",pid=123,fd=9))" (owner in users:())
47
+ * netstat: " TCP local:port foreign:port STATE PID" (PID only, last col)
33
48
  *
34
- * On Unix, trying both lsof and ss patterns is safe — they don't cross-match.
49
+ * On Unix, trying both lsof and ss patterns is safe — they don't cross-match:
50
+ * lsof peers carry "->", and an ss peer is followed only by the optional
51
+ * users:() column.
35
52
  */
36
- export declare function extractConnectionIP(line: string): RegExpMatchArray | null;
53
+ export declare function extractConnection(line: string): ConnectionMatch | null;
54
+ /**
55
+ * Resolves PIDs to process image names where connection output only carries
56
+ * PIDs (Windows netstat). Calls cb with a PID → name map, or null on
57
+ * platforms where connection lines already name their owner (Unix) or when
58
+ * tasklist fails. Never throws.
59
+ */
60
+ export declare function resolvePidNames(cb: (names: Map<string, string> | null) => void): void;
37
61
  //# sourceMappingURL=platform.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"platform.d.ts","sourceRoot":"","sources":["../../src/lib/platform.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAOH;;;;;;;;GAQG;AACH,wBAAgB,cAAc,IAAI,MAAM,CAGvC;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,KAAK,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI,CAarF;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,GAAG,IAAI,CAYzE"}
1
+ {"version":3,"file":"platform.d.ts","sourceRoot":"","sources":["../../src/lib/platform.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAOH;;;;;;;;GAQG;AACH,wBAAgB,cAAc,IAAI,MAAM,CAGvC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,KAAK,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,IAAI,GAAG,IAAI,CAarF;AAED,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;IACZ;;;;;OAKG;IACH,KAAK,EAAE,MAAM,CAAA;IACb,6EAA6E;IAC7E,GAAG,CAAC,EAAE,MAAM,CAAA;CACb;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe,GAAG,IAAI,CAqBtE;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,IAAI,KAAK,IAAI,GAAG,IAAI,CAkBrF"}
@@ -23,16 +23,18 @@ export function getProcessList() {
23
23
  * Calls cb(err, stdout) with raw connection output.
24
24
  * Handles platform fallbacks internally (lsof → ss on Linux).
25
25
  *
26
- * - macOS: `lsof -i -n -P`
27
- * - Linux: `lsof -i -n -P`, falls back to `ss -tnp` if lsof is unavailable
28
- * - Windows: `netstat -ano`
26
+ * - macOS: `lsof -i -n -P +c0` (+c0 = untruncated COMMAND, so the owning
27
+ * process is attributable default lsof cuts it at 9 chars, which mangles
28
+ * names like `com.apple.WebKit.Networking`)
29
+ * - Linux: same lsof, falls back to `ss -tnp` if lsof is unavailable
30
+ * - Windows: `netstat -ano` (PID only; resolve names via resolvePidNames)
29
31
  */
30
32
  export function getConnections(cb) {
31
33
  if (isWin) {
32
34
  execFile("netstat", ["-ano"], { timeout: 10000, encoding: "utf8", maxBuffer: 2 * 1024 * 1024 }, cb);
33
35
  return;
34
36
  }
35
- execFile("lsof", ["-i", "-n", "-P"], { timeout: 10000, encoding: "utf8", maxBuffer: 2 * 1024 * 1024 }, (err, stdout) => {
37
+ execFile("lsof", ["-i", "-n", "-P", "+c0"], { timeout: 10000, encoding: "utf8", maxBuffer: 2 * 1024 * 1024 }, (err, stdout) => {
36
38
  if (err && err.code === "ENOENT" && isLinux) {
37
39
  // lsof unavailable on Linux — fall back to ss
38
40
  execFile("ss", ["-tnp"], { timeout: 5000, encoding: "utf8", maxBuffer: 1024 * 1024 }, cb);
@@ -43,27 +45,65 @@ export function getConnections(cb) {
43
45
  });
44
46
  }
45
47
  /**
46
- * Extracts the destination IP address from a single connection output line.
47
- * Returns a regex match array [fullMatch, ip, port] or null.
48
+ * Parses a single connection output line into destination IP/port plus the
49
+ * owning process, or null if the line isn't a connection row.
48
50
  *
49
51
  * Format varies by tool:
50
- * lsof: "... ->104.18.7.42:443 ..."
51
- * ss: "... 104.18.7.42:443" (IP:port at end of line)
52
- * netstat: " TCP local:port foreign:port STATE PID" (3rd column)
52
+ * lsof: "COMMAND PID USER ... ->104.18.7.42:443 (ESTABLISHED)" (owner = col 1)
53
+ * ss -tnp: "... dst:port users:((\"chrome\",pid=123,fd=9))" (owner in users:())
54
+ * netstat: " TCP local:port foreign:port STATE PID" (PID only, last col)
53
55
  *
54
- * On Unix, trying both lsof and ss patterns is safe — they don't cross-match.
56
+ * On Unix, trying both lsof and ss patterns is safe — they don't cross-match:
57
+ * lsof peers carry "->", and an ss peer is followed only by the optional
58
+ * users:() column.
55
59
  */
56
- export function extractConnectionIP(line) {
60
+ export function extractConnection(line) {
57
61
  if (isWin) {
58
62
  const parts = line.trim().split(/\s+/);
59
63
  if (parts.length >= 4 && (parts[0] === "TCP" || parts[0] === "UDP")) {
60
- return parts[2].match(/^(\d+\.\d+\.\d+\.\d+):(\d+)$/);
64
+ const m = parts[2].match(/^(\d+\.\d+\.\d+\.\d+):(\d+)$/);
65
+ if (!m)
66
+ return null;
67
+ return { ip: m[1], port: m[2], owner: "", pid: parts[parts.length - 1] };
61
68
  }
62
69
  return null;
63
70
  }
64
- // lsof "->IP:port" (won't match ss output — no "->" in ss)
65
- // ss "IP:port" at EOL (won't match lsof — lines end with "(STATE)")
66
- return line.match(/->(\d+\.\d+\.\d+\.\d+):(\d+)/) ||
67
- line.match(/\s(\d+\.\d+\.\d+\.\d+):(\d+)\s*$/);
71
+ const lsof = line.match(/->(\d+\.\d+\.\d+\.\d+):(\d+)/);
72
+ if (lsof) {
73
+ return { ip: lsof[1], port: lsof[2], owner: line.split(/\s+/, 1)[0] ?? "" };
74
+ }
75
+ // ss: the peer address is followed by the users:() column when ss can see
76
+ // the owner (own processes, or root), and ends the line when it can't.
77
+ const ss = line.match(/\s(\d+\.\d+\.\d+\.\d+):(\d+)(?:\s+users:\(\("([^"]+)".*)?$/);
78
+ if (ss) {
79
+ return { ip: ss[1], port: ss[2], owner: ss[3] ?? "" };
80
+ }
81
+ return null;
82
+ }
83
+ /**
84
+ * Resolves PIDs to process image names where connection output only carries
85
+ * PIDs (Windows netstat). Calls cb with a PID → name map, or null on
86
+ * platforms where connection lines already name their owner (Unix) or when
87
+ * tasklist fails. Never throws.
88
+ */
89
+ export function resolvePidNames(cb) {
90
+ if (!isWin) {
91
+ cb(null);
92
+ return;
93
+ }
94
+ execFile("tasklist", ["/fo", "csv", "/nh"], { timeout: 10000, encoding: "utf8", maxBuffer: 2 * 1024 * 1024 }, (err, stdout) => {
95
+ if (err || !stdout) {
96
+ cb(null);
97
+ return;
98
+ }
99
+ const names = new Map();
100
+ for (const line of stdout.split("\n")) {
101
+ // CSV row: "Image Name","PID","Session Name","Session#","Mem Usage"
102
+ const m = line.match(/^"([^"]+)","(\d+)"/);
103
+ if (m)
104
+ names.set(m[2], m[1]);
105
+ }
106
+ cb(names);
107
+ });
68
108
  }
69
109
  //# sourceMappingURL=platform.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"platform.js","sourceRoot":"","sources":["../../src/lib/platform.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAElD,MAAM,KAAK,GAAG,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAA;AAC1C,MAAM,OAAO,GAAG,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAA;AAE5C;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc;IAC5B,MAAM,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAA;IACzC,OAAO,QAAQ,CAAC,GAAG,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC,WAAW,EAAE,CAAA;AACzE,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc,CAAC,EAAgD;IAC7E,IAAI,KAAK,EAAE,CAAC;QACV,QAAQ,CAAC,SAAS,EAAE,CAAC,MAAM,CAAC,EAAE,EAAE,OAAO,EAAE,KAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,GAAG,IAAI,GAAG,IAAI,EAAE,EAAE,EAAE,CAAC,CAAA;QACpG,OAAM;IACR,CAAC;IACD,QAAQ,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,OAAO,EAAE,KAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,GAAG,IAA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AAE,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,CAAA;IACvD,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,EAA+C;IAC7E,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,EAAE,CAAC,IAAI,CAAC,CAAA;QACR,OAAM;IACR,CAAC;IACD,QAAQ,CAAC,UAAU,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,EAAE,EAAE,OAAO,EAAE,KAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,GAAG,IAAI,GAAG,IAAI,EAAE,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE;QAC7H,IAAI,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;YACnB,EAAE,CAAC,IAAI,CAAC,CAAA;YACR,OAAM;QACR,CAAC;QACD,MAAM,KAAK,GAAG,IAAI,GAAG,EAAkB,CAAA;QACvC,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;YACtC,oEAAoE;YACpE,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,oBAAoB,CAAC,CAAA;YAC1C,IAAI,CAAC;gBAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;QAC9B,CAAC;QACD,EAAE,CAAC,KAAK,CAAC,CAAA;IACX,CAAC,CAAC,CAAA;AACJ,CAAC"}
@@ -19,6 +19,12 @@ export declare const SubmitResultSchema: z.ZodObject<{
19
19
  dashboardUrl: z.ZodOptional<z.ZodNullable<z.ZodURL>>;
20
20
  }, z.core.$strip>;
21
21
  export type SubmitResult = z.infer<typeof SubmitResultSchema>;
22
+ export declare const ServerTimesSchema: z.ZodObject<{
23
+ startedAt: z.ZodNullable<z.ZodISODateTime>;
24
+ deadline: z.ZodNullable<z.ZodISODateTime>;
25
+ timeLimit: z.ZodNullable<z.ZodNumber>;
26
+ }, z.core.$strip>;
27
+ export type ServerTimes = z.infer<typeof ServerTimesSchema>;
22
28
  export declare const SubmitStatusSchema: z.ZodObject<{
23
29
  videoUploaded: z.ZodBoolean;
24
30
  }, z.core.$strip>;
@@ -1 +1 @@
1
- {"version":3,"file":"schemas.d.ts","sourceRoot":"","sources":["../../src/lib/schemas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,eAAO,MAAM,kBAAkB;;;;;;;;;;;iBAgB7B,CAAA;AAEF,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAA;AAE7D,eAAO,MAAM,kBAAkB;;;;;iBAQ7B,CAAA;AAEF,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAA;AAE7D,eAAO,MAAM,kBAAkB;;iBAE7B,CAAA;AAEF,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAA;AAM7D,eAAO,MAAM,yBAAyB;;;;;;iBAQpC,CAAA;AAEF,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,yBAAyB,CAAC,CAAA"}
1
+ {"version":3,"file":"schemas.d.ts","sourceRoot":"","sources":["../../src/lib/schemas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,eAAO,MAAM,kBAAkB;;;;;;;;;;;iBAgB7B,CAAA;AAEF,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAA;AAE7D,eAAO,MAAM,kBAAkB;;;;;iBAQ7B,CAAA;AAEF,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAA;AAK7D,eAAO,MAAM,iBAAiB;;;;iBAI5B,CAAA;AAEF,MAAM,MAAM,WAAW,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,iBAAiB,CAAC,CAAA;AAE3D,eAAO,MAAM,kBAAkB;;iBAE7B,CAAA;AAEF,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAA;AAM7D,eAAO,MAAM,yBAAyB;;;;;;iBAQpC,CAAA;AAEF,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,yBAAyB,CAAC,CAAA"}
@@ -22,6 +22,14 @@ export const SubmitResultSchema = z.object({
22
22
  // Optional so this stays compatible with mode=NONE responses.
23
23
  dashboardUrl: z.url().nullish(),
24
24
  });
25
+ // ENG-1489: authoritative times riding the /cli/activity response, so a live
26
+ // watcher re-arms when the deadline moves server-side. Offset-tolerant: the
27
+ // backend emits Z-suffixed ISO, but any valid offset is the same instant.
28
+ export const ServerTimesSchema = z.object({
29
+ startedAt: z.iso.datetime({ offset: true }).nullable(),
30
+ deadline: z.iso.datetime({ offset: true }).nullable(),
31
+ timeLimit: z.number().nullable(),
32
+ });
25
33
  export const SubmitStatusSchema = z.object({
26
34
  videoUploaded: z.boolean(),
27
35
  });
@@ -1 +1 @@
1
- {"version":3,"file":"schemas.js","sourceRoot":"","sources":["../../src/lib/schemas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,CAAC,MAAM,CAAC;IACzC,YAAY,EAAE,CAAC,CAAC,IAAI,EAAE;IACtB,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE;IAE1B,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;IACtB,UAAU,EAAE,CAAC,CAAC,GAAG,EA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E;CAChC,CAAC,CAAA;AAIF,6EAA6E;AAC7E,4EAA4E;AAC5E,0EAA0E;AAC1E,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,CAAC,MAAM,CAAC;IACxC,SAAS,EAAE,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,QAAQ,EAAE;IACtD,QAAQ,EAAE,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,QAAQ,EAAE;IACrD,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;CACjC,CAAC,CAAA;AAIF,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,CAAC,MAAM,CAAC;IACzC,aAAa,EAAE,CAAC,CAAC,OAAO,EAAE;CAC3B,CAAC,CAAA;AAIF,kEAAkE;AAClE,uEAAuE;AACvE,2EAA2E;AAC3E,yEAAyE;AACzE,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,CAAC,MAAM,CAAC;IAChD,gBAAgB,EAAE,CAAC,CAAC,MAAM,EAAE;IAC5B,YAAY,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,OAAO,EAAE;IAClC,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,OAAO,EAAE;IACjC,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC;IAC9C,uEAAuE;IACvE,8EAA8E;IAC9E,eAAe,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,OAAO,EAAE;CACtC,CAAC,CAAA"}
@@ -0,0 +1,79 @@
1
+ import type { LitmusConfig } from "./config.js";
2
+ /**
3
+ * Shadow store for tracking-critical state (ENG-1453).
4
+ *
5
+ * Everything AI-capture depends on used to live only inside the assessment
6
+ * repo, gitignored — which put it squarely inside `git clean -fdX`'s blast
7
+ * radius (the ENG-1446 incident: one ordinary cleanup command deleted the
8
+ * config, the Copilot hook, and the forensic log at once). This module keeps
9
+ * a copy of the small, load-bearing pieces OUTSIDE the repo, next to the
10
+ * registry that already lives there:
11
+ *
12
+ * ~/.litmus/assessments/<assessmentId>/
13
+ * config.json verbatim mirror of .litmus/config.json + { projectDir, mirroredAt }
14
+ * chain.json last uploaded chain position { seq, hash }
15
+ * tracker.pid mirror — lets doctor find a live watcher after a repo wipe
16
+ * tracker.nonce mirror — same
17
+ * ~/.litmus/logs/<assessmentId>.log tracker log (relocated, not mirrored)
18
+ *
19
+ * Rules (from the ENG-1453 security review):
20
+ * - The in-repo config is ALWAYS authoritative when present; shadows are read
21
+ * only when it is missing.
22
+ * - Every write here is best-effort: a failing shadow write must never break
23
+ * init, doctor, or the watcher.
24
+ * - Shadows die with the assessment: deleted on submit/terminal outcomes and
25
+ * pruned when their project dir is gone and unregistered.
26
+ */
27
+ export interface ShadowConfig extends LitmusConfig {
28
+ /** Absolute assessment root the mirror was written for — the lookup key
29
+ * when the in-repo config (and its directory) no longer exists. */
30
+ projectDir: string;
31
+ mirroredAt: string;
32
+ }
33
+ export interface ChainCursor {
34
+ seq: number;
35
+ hash: string;
36
+ updatedAt: string;
37
+ }
38
+ export declare function shadowRootDir(): string;
39
+ export declare function shadowDirFor(assessmentId: string): string | null;
40
+ export declare function shadowLogPath(assessmentId: string): string | null;
41
+ /** Mirror a just-written config. Best-effort; returns whether it stuck. */
42
+ export declare function writeShadowConfig(projectDir: string, config: LitmusConfig): boolean;
43
+ export declare function readShadowConfigById(assessmentId: string): ShadowConfig | null;
44
+ /** Find the shadow whose mirror was written for this project dir. */
45
+ export declare function findShadowConfigForDir(projectDir: string): ShadowConfig | null;
46
+ /** Record the last chain position the SERVER has (written after each
47
+ * successful upload), so a replacement watcher whose activity.jsonl is gone
48
+ * continues the chain instead of forking a second genesis. */
49
+ export declare function writeShadowChainCursor(assessmentId: string, seq: number, hash: string): void;
50
+ export declare function readShadowChainCursor(assessmentId: string): ChainCursor | null;
51
+ export declare function writeShadowTrackerFile(assessmentId: string, name: "tracker.pid" | "tracker.nonce", content: string): void;
52
+ export declare function readShadowTrackerFile(assessmentId: string, name: "tracker.pid" | "tracker.nonce"): string | null;
53
+ export declare function shadowTrackerFileMtime(assessmentId: string, name: "tracker.pid" | "tracker.nonce"): number | null;
54
+ export declare function deleteShadowForAssessment(assessmentId: string): void;
55
+ /** Delete the shadow keyed to a project dir — works when the in-repo config
56
+ * (which would have named the assessment) is already gone. */
57
+ export declare function deleteShadowForProject(projectDir: string): void;
58
+ /**
59
+ * Remove shadows whose assessment is demonstrably over on this machine:
60
+ * project dir no longer exists AND no registry entry points at it. A live
61
+ * assessment whose folder was merely wiped keeps its registry entry, so it
62
+ * survives pruning until submit/expiry releases it.
63
+ */
64
+ export declare function pruneShadows(registryPaths: string[]): number;
65
+ export declare const REPAIR_FILE_NAME = "LITMUS-REPAIR.md";
66
+ /** Written by the watcher only when self-heal could not act (server
67
+ * unreachable, no usable shadow, AND the in-memory restore failed to write).
68
+ * Rewritten every heartbeat while the outage lasts — a cleanup pass that
69
+ * deletes it gets it back — and removed the moment the config is restored.
70
+ *
71
+ * Deliberately carries NO credentials: a project-root file can be committed,
72
+ * pushed, read into an AI agent's context, or shipped in the submission zip,
73
+ * so the token must never appear here. The candidate's assessment page
74
+ * ("Something not working?") shows the full copy-paste repair command, and
75
+ * `litmus doctor` run bare prompts for the token pointing at that panel. */
76
+ export declare function repairFileContent(): string;
77
+ export declare function writeRepairFile(projectDir: string): void;
78
+ export declare function removeRepairFile(projectDir: string): void;
79
+ //# sourceMappingURL=shadow.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shadow.d.ts","sourceRoot":"","sources":["../../src/lib/shadow.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,MAAM,WAAW,YAAa,SAAQ,YA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AID,wBAAgB,yBAAyB,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,CAQpE;AAED;+DAC+D;AAC/D,wBAAgB,sBAAsB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAG/D;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,aAAa,EAAE,MAAM,EAAE,GAAG,MAAM,CAe5D;AAID,eAAO,MAAM,gBAAgB,qBAAqB,CAAA;AAElD;;;;;;;;;6EAS6E;AAC7E,wBAAgB,iBAAiB,IAAI,MAAM,CAoB1C;AAED,wBAAgB,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAIxD;AAED,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAEzD"}
@@ -0,0 +1,268 @@
1
+ import fs from "fs";
2
+ import os from "os";
3
+ import path from "path";
4
+ function litmusHome() {
5
+ return path.join(os.homedir(), ".litmus");
6
+ }
7
+ export function shadowRootDir() {
8
+ return path.join(litmusHome(), "assessments");
9
+ }
10
+ /** Assessment ids are server-issued UUIDs; refuse anything path-like. */
11
+ function safeId(assessmentId) {
12
+ return /^[A-Za-z0-9-]{1,64}$/.test(assessmentId) ? assessmentId : null;
13
+ }
14
+ export function shadowDirFor(assessmentId) {
15
+ const id = safeId(assessmentId);
16
+ return id ? path.join(shadowRootDir(), id) : null;
17
+ }
18
+ export function shadowLogPath(assessmentId) {
19
+ const id = safeId(assessmentId);
20
+ return id ? path.join(litmusHome(), "logs", `${id}.log`) : null;
21
+ }
22
+ /** Resolve through whatever ancestors still exist so a deleted project dir
23
+ * still normalizes the same way it did when the mirror was written
24
+ * (macOS /tmp→/private/tmp and symlinked homes). */
25
+ function normalizeDir(p) {
26
+ let base = path.resolve(p);
27
+ const tail = [];
28
+ for (;;) {
29
+ try {
30
+ const real = fs.realpathSync(base);
31
+ return tail.length ? path.join(real, ...tail.reverse()) : real;
32
+ }
33
+ catch { /* keep walking up */ }
34
+ const parent = path.dirname(base);
35
+ if (parent === base)
36
+ return path.resolve(p);
37
+ tail.push(path.basename(base));
38
+ base = parent;
39
+ }
40
+ }
41
+ function atomicWrite(filePath, content) {
42
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
43
+ const tmp = `${filePath}.tmp-${process.pid}-${Date.now()}`;
44
+ fs.writeFileSync(tmp, content, "utf8");
45
+ fs.renameSync(tmp, filePath);
46
+ }
47
+ // ── Config mirror ─────────────────────────────────────────────────
48
+ /** Mirror a just-written config. Best-effort; returns whether it stuck. */
49
+ export function writeShadowConfig(projectDir, config) {
50
+ const dir = shadowDirFor(config.assessmentId);
51
+ if (!dir)
52
+ return false;
53
+ const shadow = {
54
+ ...config,
55
+ projectDir: normalizeDir(projectDir),
56
+ mirroredAt: new Date().toISOString(),
57
+ };
58
+ try {
59
+ atomicWrite(path.join(dir, "config.json"), JSON.stringify(shadow, null, 2));
60
+ return true;
61
+ }
62
+ catch {
63
+ return false;
64
+ }
65
+ }
66
+ function parseShadowConfig(raw) {
67
+ let parsed;
68
+ try {
69
+ parsed = JSON.parse(raw);
70
+ }
71
+ catch {
72
+ return null;
73
+ }
74
+ const s = parsed;
75
+ // Field validation: a shadow missing any of these cannot restore anything.
76
+ for (const key of ["assessmentId", "token", "startedAt", "apiBase", "projectDir"]) {
77
+ if (typeof s[key] !== "string" || !s[key])
78
+ return null;
79
+ }
80
+ return s;
81
+ }
82
+ export function readShadowConfigById(assessmentId) {
83
+ const dir = shadowDirFor(assessmentId);
84
+ if (!dir)
85
+ return null;
86
+ try {
87
+ return parseShadowConfig(fs.readFileSync(path.join(dir, "config.json"), "utf8"));
88
+ }
89
+ catch {
90
+ return null;
91
+ }
92
+ }
93
+ /** Find the shadow whose mirror was written for this project dir. */
94
+ export function findShadowConfigForDir(projectDir) {
95
+ const target = normalizeDir(projectDir);
96
+ for (const entry of listShadowIds()) {
97
+ const shadow = readShadowConfigById(entry);
98
+ if (shadow && normalizeDir(shadow.projectDir) === target)
99
+ return shadow;
100
+ }
101
+ return null;
102
+ }
103
+ function listShadowIds() {
104
+ try {
105
+ return fs.readdirSync(shadowRootDir(), { withFileTypes: true })
106
+ .filter((d) => d.isDirectory())
107
+ .map((d) => d.name);
108
+ }
109
+ catch {
110
+ return [];
111
+ }
112
+ }
113
+ // ── Chain cursor ──────────────────────────────────────────────────
114
+ /** Record the last chain position the SERVER has (written after each
115
+ * successful upload), so a replacement watcher whose activity.jsonl is gone
116
+ * continues the chain instead of forking a second genesis. */
117
+ export function writeShadowChainCursor(assessmentId, seq, hash) {
118
+ const dir = shadowDirFor(assessmentId);
119
+ if (!dir)
120
+ return;
121
+ try {
122
+ atomicWrite(path.join(dir, "chain.json"), JSON.stringify({ seq, hash, updatedAt: new Date().toISOString() }));
123
+ }
124
+ catch { /* best effort */ }
125
+ }
126
+ export function readShadowChainCursor(assessmentId) {
127
+ const dir = shadowDirFor(assessmentId);
128
+ if (!dir)
129
+ return null;
130
+ try {
131
+ const parsed = JSON.parse(fs.readFileSync(path.join(dir, "chain.json"), "utf8"));
132
+ if (typeof parsed.seq !== "number" || typeof parsed.hash !== "string" || !parsed.hash)
133
+ return null;
134
+ return parsed;
135
+ }
136
+ catch {
137
+ return null;
138
+ }
139
+ }
140
+ // ── Tracker pid/nonce mirrors ─────────────────────────────────────
141
+ export function writeShadowTrackerFile(assessmentId, name, content) {
142
+ const dir = shadowDirFor(assessmentId);
143
+ if (!dir)
144
+ return;
145
+ try {
146
+ fs.mkdirSync(dir, { recursive: true });
147
+ fs.writeFileSync(path.join(dir, name), content);
148
+ }
149
+ catch { /* best effort */ }
150
+ }
151
+ export function readShadowTrackerFile(assessmentId, name) {
152
+ const dir = shadowDirFor(assessmentId);
153
+ if (!dir)
154
+ return null;
155
+ try {
156
+ return fs.readFileSync(path.join(dir, name), "utf8");
157
+ }
158
+ catch {
159
+ return null;
160
+ }
161
+ }
162
+ export function shadowTrackerFileMtime(assessmentId, name) {
163
+ const dir = shadowDirFor(assessmentId);
164
+ if (!dir)
165
+ return null;
166
+ try {
167
+ return fs.statSync(path.join(dir, name)).mtimeMs;
168
+ }
169
+ catch {
170
+ return null;
171
+ }
172
+ }
173
+ // ── Lifecycle ─────────────────────────────────────────────────────
174
+ export function deleteShadowForAssessment(assessmentId) {
175
+ const dir = shadowDirFor(assessmentId);
176
+ if (!dir)
177
+ return;
178
+ try {
179
+ fs.rmSync(dir, { recursive: true, force: true });
180
+ }
181
+ catch { /* best effort */ }
182
+ const logPath = shadowLogPath(assessmentId);
183
+ if (logPath) {
184
+ try {
185
+ fs.rmSync(logPath, { force: true });
186
+ }
187
+ catch { /* best effort */ }
188
+ }
189
+ }
190
+ /** Delete the shadow keyed to a project dir — works when the in-repo config
191
+ * (which would have named the assessment) is already gone. */
192
+ export function deleteShadowForProject(projectDir) {
193
+ const shadow = findShadowConfigForDir(projectDir);
194
+ if (shadow)
195
+ deleteShadowForAssessment(shadow.assessmentId);
196
+ }
197
+ /**
198
+ * Remove shadows whose assessment is demonstrably over on this machine:
199
+ * project dir no longer exists AND no registry entry points at it. A live
200
+ * assessment whose folder was merely wiped keeps its registry entry, so it
201
+ * survives pruning until submit/expiry releases it.
202
+ */
203
+ export function pruneShadows(registryPaths) {
204
+ const registered = new Set(registryPaths.map((p) => normalizeDir(p)));
205
+ let pruned = 0;
206
+ for (const id of listShadowIds()) {
207
+ const shadow = readShadowConfigById(id);
208
+ if (!shadow)
209
+ continue; // unreadable shadow: leave for a human, never silently discard
210
+ const dir = normalizeDir(shadow.projectDir);
211
+ let dirExists = false;
212
+ try {
213
+ dirExists = fs.existsSync(dir);
214
+ }
215
+ catch { /* treat as missing */ }
216
+ if (!dirExists && !registered.has(dir)) {
217
+ deleteShadowForAssessment(id);
218
+ pruned++;
219
+ }
220
+ }
221
+ return pruned;
222
+ }
223
+ // ── Candidate-facing repair file (last resort) ────────────────────
224
+ export const REPAIR_FILE_NAME = "LITMUS-REPAIR.md";
225
+ /** Written by the watcher only when self-heal could not act (server
226
+ * unreachable, no usable shadow, AND the in-memory restore failed to write).
227
+ * Rewritten every heartbeat while the outage lasts — a cleanup pass that
228
+ * deletes it gets it back — and removed the moment the config is restored.
229
+ *
230
+ * Deliberately carries NO credentials: a project-root file can be committed,
231
+ * pushed, read into an AI agent's context, or shipped in the submission zip,
232
+ * so the token must never appear here. The candidate's assessment page
233
+ * ("Something not working?") shows the full copy-paste repair command, and
234
+ * `litmus doctor` run bare prompts for the token pointing at that panel. */
235
+ export function repairFileContent() {
236
+ return [
237
+ "# Litmus needs a one-minute repair",
238
+ "",
239
+ "Your assessment's tracking settings went missing, so your activity is not",
240
+ "being recorded and `litmus submit` will not work until they are restored.",
241
+ "",
242
+ "Open your assessment page (the link from your invite email) and under",
243
+ '"Something not working?" copy the repair command shown there. Or run:',
244
+ "",
245
+ "```",
246
+ "npm install -g litmus-cli@latest",
247
+ "litmus doctor",
248
+ "```",
249
+ "",
250
+ "and paste your assessment token when asked (it is on that same page).",
251
+ "Your work and your deadline are not affected. This file disappears once",
252
+ "the repair has run.",
253
+ "",
254
+ ].join("\n");
255
+ }
256
+ export function writeRepairFile(projectDir) {
257
+ try {
258
+ fs.writeFileSync(path.join(projectDir, REPAIR_FILE_NAME), repairFileContent(), "utf8");
259
+ }
260
+ catch { /* best effort */ }
261
+ }
262
+ export function removeRepairFile(projectDir) {
263
+ try {
264
+ fs.rmSync(path.join(projectDir, REPAIR_FILE_NAME), { force: true });
265
+ }
266
+ catch { /* best effort */ }
267
+ }
268
+ //# sourceMappingURL=shadow.js.map