ttsc 0.31.3 → 0.31.4

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 (26) hide show
  1. package/lib/compiler/internal/build/BuildExecution.js +33 -4
  2. package/lib/compiler/internal/build/BuildExecution.js.map +1 -1
  3. package/lib/compiler/internal/build/runExternalEmitProvenance.d.ts +15 -0
  4. package/lib/compiler/internal/build/runExternalEmitProvenance.js +67 -4
  5. package/lib/compiler/internal/build/runExternalEmitProvenance.js.map +1 -1
  6. package/lib/internal/probeLocalProcess.d.ts +27 -0
  7. package/lib/internal/probeLocalProcess.js +62 -0
  8. package/lib/internal/probeLocalProcess.js.map +1 -0
  9. package/lib/launcher/internal/runtime/isLocalProcessGone.d.ts +9 -9
  10. package/lib/launcher/internal/runtime/isLocalProcessGone.js +13 -20
  11. package/lib/launcher/internal/runtime/isLocalProcessGone.js.map +1 -1
  12. package/lib/plugin/internal/load/loadProjectPlugins.js +7 -3
  13. package/lib/plugin/internal/load/loadProjectPlugins.js.map +1 -1
  14. package/lib/plugin/internal/source/GoBuildCacheCoordination.js +4 -1
  15. package/lib/plugin/internal/source/GoBuildCacheCoordination.js.map +1 -1
  16. package/lib/plugin/internal/source/PluginBuildLockOwner.d.ts +8 -7
  17. package/lib/plugin/internal/source/PluginBuildLockOwner.js +10 -14
  18. package/lib/plugin/internal/source/PluginBuildLockOwner.js.map +1 -1
  19. package/package.json +8 -8
  20. package/src/compiler/internal/build/BuildExecution.ts +33 -6
  21. package/src/compiler/internal/build/runExternalEmitProvenance.ts +84 -4
  22. package/src/internal/probeLocalProcess.ts +56 -0
  23. package/src/launcher/internal/runtime/isLocalProcessGone.ts +14 -19
  24. package/src/plugin/internal/load/loadProjectPlugins.ts +12 -11
  25. package/src/plugin/internal/source/GoBuildCacheCoordination.ts +4 -1
  26. package/src/plugin/internal/source/PluginBuildLockOwner.ts +10 -13
@@ -37,6 +37,12 @@ import { PassthroughFlags } from "./PassthroughFlags";
37
37
  * supported upstream naming subset; this adapter does not certify arbitrary
38
38
  * compiler behavior.
39
39
  *
40
+ * A foreign compiler host that cannot report its writes, a plugin library's own
41
+ * executable built before ttsc required provenance, is observed instead: the
42
+ * selected tsgo answers the same read-only probes, the host runs unchanged, and
43
+ * its writes are the predicted outputs whose metadata moved across the run.
44
+ * Requiring provenance must never reject such a host outright.
45
+ *
40
46
  * Normally reported ownerless outputs carry their actual failed proof boundary
41
47
  * in emittedSourceProofFailures. These explanations preserve compiler streams
42
48
  * and status; they do not replace missing ownership with a successful claim.
@@ -63,6 +69,16 @@ export function runExternalEmitProvenance(options: {
63
69
  /** Same child environment used by the emitting command. */
64
70
  env: NodeJS.ProcessEnv;
65
71
 
72
+ /**
73
+ * How the emitting command's writes are known. `reported` (the default) reads
74
+ * the producer's own `TSFILE:` rows. `observed` serves a foreign compiler
75
+ * host that cannot report them, a plugin library's own executable such as
76
+ * typia's: `binary` and `args` then only drive the read-only probes, the
77
+ * callback runs that host unchanged, and a write is a predicted output whose
78
+ * metadata moved across the run.
79
+ */
80
+ writes?: "reported" | "observed";
81
+
66
82
  /**
67
83
  * Execute the single emitting command, preserving raw output and whether the
68
84
  * child exited normally rather than being killed during reporting.
@@ -79,15 +95,22 @@ export function runExternalEmitProvenance(options: {
79
95
  const probe = prepareProbe(options, (reason) => {
80
96
  refusal = reason;
81
97
  });
98
+ const observed = options.writes === "observed";
99
+ const outputsBefore =
100
+ observed && probe !== undefined
101
+ ? observePredictedOutputs(probe)
102
+ : undefined;
82
103
  const { result, completedNormally } = options.run(
83
- probe === undefined
104
+ probe === undefined || observed
84
105
  ? options.args
85
106
  : [...options.args, "--listFiles", "true", "--listEmittedFiles", "true"],
86
107
  );
87
108
  const lines = result.stdout.split(/\r?\n/);
88
109
  const sourceLines = new Set(probe?.files);
89
110
  const stdout =
90
- probe === undefined || probe.config.compilerOptions.listFiles === true
111
+ probe === undefined ||
112
+ observed ||
113
+ probe.config.compilerOptions.listFiles === true
91
114
  ? result.stdout
92
115
  : result.stdout
93
116
  .split(/(?<=\n)/)
@@ -100,7 +123,12 @@ export function runExternalEmitProvenance(options: {
100
123
  emittedSourceProofFailures: undefined,
101
124
  stdout,
102
125
  };
103
- const written = writtenJavaScript(result.stdout, options.cwd);
126
+ const written =
127
+ outputsBefore !== undefined
128
+ ? movedOutputs(outputsBefore)
129
+ : observed
130
+ ? []
131
+ : writtenJavaScript(result.stdout, options.cwd);
104
132
  const emittedSources: Record<string, readonly string[]> = Object.create(null);
105
133
  const emittedSourceProofFailures: Record<string, string> =
106
134
  Object.create(null);
@@ -115,7 +143,11 @@ export function runExternalEmitProvenance(options: {
115
143
  };
116
144
  }
117
145
 
118
- const listed = compilerSourceList(lines, options.cwd);
146
+ // An observed host lists nothing; it compiles the same selected tsconfig, and
147
+ // the source observations below still prove that selection held still.
148
+ const listed = observed
149
+ ? probe.files
150
+ : compilerSourceList(lines, options.cwd);
119
151
  const sameSelection = JSON.stringify(listed) === JSON.stringify(probe.files);
120
152
 
121
153
  let stable = sameSelection;
@@ -554,6 +586,54 @@ function createOutputPrediction(
554
586
  };
555
587
  }
556
588
 
589
+ /**
590
+ * Every output the selected sources predict, with its metadata before an
591
+ * observed host runs.
592
+ */
593
+ function observePredictedOutputs(probe: {
594
+ base: string;
595
+ config: { compilerOptions: Record<string, unknown> };
596
+ files: readonly string[];
597
+ }): Map<string, string> {
598
+ const predict = createOutputPrediction(probe.config, probe.base);
599
+ const outputs = new Map<string, string>();
600
+ for (const source of probe.files)
601
+ for (const output of predict(source))
602
+ outputs.set(output, outputObservation(output));
603
+ return outputs;
604
+ }
605
+
606
+ /**
607
+ * The predicted outputs an observed host wrote: present now, with metadata
608
+ * other than before. A rewrite inside one clock tick at the same size and inode
609
+ * stays invisible, which is why a host that can report its writes must declare
610
+ * `emitProvenance` instead.
611
+ */
612
+ function movedOutputs(before: ReadonlyMap<string, string>): string[] {
613
+ const written: string[] = [];
614
+ for (const [output, observation] of before) {
615
+ const after = outputObservation(output);
616
+ if (after !== "missing" && after !== observation) written.push(output);
617
+ }
618
+ return written;
619
+ }
620
+
621
+ /** Device, inode, size and both clocks of one output, or `missing`. */
622
+ function outputObservation(file: string): string {
623
+ try {
624
+ const stats = fs.lstatSync(file, { bigint: true });
625
+ return [
626
+ stats.dev,
627
+ stats.ino,
628
+ stats.size,
629
+ stats.mtimeNs,
630
+ stats.ctimeNs,
631
+ ].join(":");
632
+ } catch {
633
+ return "missing";
634
+ }
635
+ }
636
+
557
637
  /** The writer's TSFILE report, rather than existing files, establishes writes. */
558
638
  function writtenJavaScript(stdout: string, cwd: string): string[] {
559
639
  const outputs = new Set<string>();
@@ -0,0 +1,56 @@
1
+ import fs from "node:fs";
2
+
3
+ /**
4
+ * Observe whether process `pid` on this host is still running.
5
+ *
6
+ * Signal zero answers whether the kernel still holds a process with that id.
7
+ * Only `ESRCH` proves absence; `EPERM` and other failures remain unknown. On
8
+ * Linux a successful probe can still name a zombie: a process that has exited
9
+ * and holds its id only until its parent collects its status. A parent that
10
+ * never collects, such as a container's PID 1 that is not an init or a
11
+ * subreaper that collects only when its own command ends, keeps the zombie
12
+ * indefinitely, so its `/proc` status reports it absent too. A leader whose own
13
+ * thread exited also reads as a zombie while its other threads run, so the
14
+ * zombie must be the process's last thread. Its id cannot be reused while the
15
+ * zombie holds it, so this never names a live process absent.
16
+ *
17
+ * @evidence contracts/common.md#principled-implementation ESRCH, or a Linux /proc status of zombie or dead with no other thread, proves the process exited; success with any other status is presence, and every other failure stays unknown.
18
+ * @evidence contracts/common.md#clear-and-simple-design One probe gives the ttsx run, dependency-lock and plugin-lock owners the same exit decision.
19
+ * @evidence contracts/common.md#prohibited-implementation-shortcuts The native signal-zero probe and the kernel's own process status decide; no PID list, timeout or error-to-absent fallback substitutes for them.
20
+ * @evidence contracts/common.md#meaningful-documentation The paragraph explains why a zombie answers signal zero, which parents leave one behind, why a zombie leader with running threads is not one, and why its id cannot name a live process.
21
+ * @evidence contracts/portability.md#os-neutral-implementation Node's signal-zero probe works on every platform; only Linux exposes zombies through /proc, and elsewhere the probe's answer stands.
22
+ * @evidence contracts/performance.md#efficient-algorithms One syscall, plus one small /proc read on Linux when the process exists.
23
+ * @evidenceExclude contracts/performance.md#reuse-equivalent-work Process state is a current observation and is never cached as proof for a later decision.
24
+ * @evidenceExclude contracts/performance.md#bound-retention-and-release-resources The probe retains no handle; the status text becomes reclaimable on return.
25
+ */
26
+ export function probeLocalProcess(pid: number): {
27
+ result: "present" | "absent" | "unknown";
28
+ errorCode?: string;
29
+ } {
30
+ try {
31
+ process.kill(pid, 0);
32
+ } catch (error) {
33
+ const code = (error as NodeJS.ErrnoException | null)?.code;
34
+ return { result: code === "ESRCH" ? "absent" : "unknown", errorCode: code };
35
+ }
36
+ if (process.platform !== "linux") return { result: "present" };
37
+ let status: string;
38
+ try {
39
+ status = fs.readFileSync(`/proc/${pid}/status`, "utf8");
40
+ } catch (error) {
41
+ const code = (error as NodeJS.ErrnoException | null)?.code;
42
+ // The process was collected between the probe and the read.
43
+ if (code === "ENOENT" || code === "ESRCH") return { result: "absent" };
44
+ return { result: "unknown", errorCode: code };
45
+ }
46
+ // A leader whose own thread exited reads as a zombie while its other threads
47
+ // still run; only a zombie that is the process's last thread has exited.
48
+ const state = /^State:\s*(\S)/m.exec(status)?.[1];
49
+ const threads = /^Threads:\s*(\d+)/m.exec(status)?.[1];
50
+ return {
51
+ result:
52
+ (state === "Z" || state === "X") && threads === "1"
53
+ ? "absent"
54
+ : "present",
55
+ };
56
+ }
@@ -1,15 +1,17 @@
1
1
  import os from "node:os";
2
2
 
3
+ import { probeLocalProcess } from "../../../internal/probeLocalProcess";
4
+
3
5
  /**
4
6
  * Whether the process an owner record names has certainly exited: it ran on
5
7
  * this host, and no process with its pid is alive.
6
8
  *
7
9
  * A record from another host proves nothing, because its pid names a process
8
10
  * this machine cannot see, so it is never reported gone. An `EPERM` from the
9
- * probe does not establish absence, so it counts as alive. Only `ESRCH` proves
10
- * absence; other probe failures remain unknown. A recycled pid makes the answer
11
- * err toward "alive": a caller that reclaims on "gone" never takes a live
12
- * owner's state.
11
+ * probe does not establish absence, so it counts as alive. Only `ESRCH`, or a
12
+ * Linux zombie that has exited but was never collected, proves absence; other
13
+ * probe failures remain unknown. A recycled pid makes the answer err toward
14
+ * "alive": a caller that reclaims on "gone" never takes a live owner's state.
13
15
  *
14
16
  * Callers supply a validated positive process id, not a process-group selector.
15
17
  * Hostname comparison is a host-label convention, not machine authentication:
@@ -18,13 +20,13 @@ import os from "node:os";
18
20
  * An optional diagnostic receives this same observation without another probe;
19
21
  * a diagnostic failure cannot change the ownership decision.
20
22
  *
21
- * @evidence contracts/common.md#principled-implementation Reclamation requires both the recorded local hostname and ESRCH from the native pid probe; permission, invalid-pid and unknown failures cannot establish that an owner is gone.
22
- * @evidence contracts/common.md#clear-and-simple-design One conservative predicate supplies the same owner-liveness decision to lock recovery and directory sweeping.
23
- * @evidence contracts/common.md#prohibited-implementation-shortcuts Native process existence is queried through process.kill(pid, 0); no fixture pid or broad error-to-dead fallback substitutes for ownership evidence.
24
- * @evidence contracts/common.md#meaningful-documentation The native explanation distinguishes a remote host, a denied or unknown probe and pid recycling, stating why ambiguity protects a live owner's state.
23
+ * @evidence contracts/common.md#principled-implementation Reclamation requires both the recorded local hostname and probeLocalProcess's absence proof, ESRCH or an uncollected Linux zombie; permission, invalid-pid and unknown failures cannot establish that an owner is gone.
24
+ * @evidence contracts/common.md#clear-and-simple-design One conservative predicate supplies the same owner-liveness decision to lock recovery and directory sweeping, and shares its probe with plugin build locks.
25
+ * @evidence contracts/common.md#prohibited-implementation-shortcuts Native process existence is queried through process.kill(pid, 0) and the kernel's /proc state; no fixture pid or broad error-to-dead fallback substitutes for ownership evidence.
26
+ * @evidence contracts/common.md#meaningful-documentation The native explanation distinguishes a remote host, a denied or unknown probe, an uncollected zombie and pid recycling, stating why ambiguity protects a live owner's state.
25
27
  * @evidence contracts/portability.md#os-neutral-implementation Node exposes the host's process-existence probe and portable error codes; hostname comparison scopes pid identity to this host without assuming POSIX signals can terminate Windows processes.
26
28
  * @evidenceExclude contracts/performance.md#bound-retention-and-release-resources This observation retains no handle, historical state or cache; temporary lowercase hostname strings become reclaimable after return.
27
- * @evidence contracts/performance.md#efficient-algorithms Native hostname lookup and two lowercase string constructions/comparison process host-label text before at most one native pid probe; the fixed probe count is not a fixed text or operating-system cost.
29
+ * @evidence contracts/performance.md#efficient-algorithms Native hostname lookup and two lowercase string constructions/comparison process host-label text before at most one native pid probe and, on Linux, one /proc state read; the fixed probe count is not a fixed text or operating-system cost.
28
30
  * @evidenceExclude contracts/performance.md#reuse-equivalent-work Host labels and native process existence are current observations; this predicate coordinates no cross-call cache or reusable liveness proof.
29
31
  */
30
32
  export function isLocalProcessGone(
@@ -54,14 +56,7 @@ export function isLocalProcessGone(
54
56
  report("remote");
55
57
  return false;
56
58
  }
57
- try {
58
- process.kill(owner.pid, 0);
59
- report("present");
60
- return false;
61
- } catch (error) {
62
- const code = (error as NodeJS.ErrnoException | null)?.code;
63
- const absent = code === "ESRCH";
64
- report(absent ? "absent" : "unknown", code);
65
- return absent;
66
- }
59
+ const { result, errorCode } = probeLocalProcess(owner.pid);
60
+ report(result, errorCode);
61
+ return result === "absent";
67
62
  }
@@ -529,17 +529,16 @@ export function loadProjectPlugins(options: {
529
529
  records[candidate.index]!.kind === "executable" &&
530
530
  !proposals[candidate.index]!.ownModule,
531
531
  );
532
+ const buildDirectories = pluginBuildDirectories(
533
+ records,
534
+ effectiveEnv,
535
+ watchReplacements,
536
+ packageReaders,
537
+ );
532
538
  // Admission can now narrow the provisional interests without restamping the
533
539
  // retained package/module baselines. Final host errors still keep these roots.
534
- options.onWatchInputs?.(
535
- pluginBuildDirectories(
536
- records,
537
- effectiveEnv,
538
- watchReplacements,
539
- packageReaders,
540
- ),
541
- sourceIdentities,
542
- );
540
+ options.onWatchInputs?.(buildDirectories, sourceIdentities);
541
+ const keyedDirectories = new Set(buildDirectories);
543
542
  const linkedContributors = records
544
543
  .filter((record) => record.stage === "transform")
545
544
  .flatMap((record) =>
@@ -781,10 +780,12 @@ export function loadProjectPlugins(options: {
781
780
  ? { observationsComplete: false as const }
782
781
  : {}),
783
782
  // The directories the watch inputs named before the builds, as the builds
784
- // read them.
783
+ // read them. Package selection shares these readings and also reads a
784
+ // linked plugin's module root, which no build keys on, so only the build
785
+ // directories are reported.
785
786
  pluginSources: Object.fromEntries(
786
787
  [...sourceDigests]
787
- .filter(([directory]) => reportsPluginSource(directory))
788
+ .filter(([directory]) => keyedDirectories.has(directory))
788
789
  .sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))
789
790
  .map(([directory, sourceDigest]) => [
790
791
  directory,
@@ -322,6 +322,9 @@ export namespace GoBuildCacheCoordination {
322
322
  // the child process required for `go build`. The fallback inherits only the
323
323
  // low standard descriptors and checks its parent PID. An IPC channel would
324
324
  // allocate another high descriptor and recreate Darwin's spawn EBADF limit.
325
+ // A dead parent that nobody collects stays a zombie and still answers
326
+ // signal zero, but POSIX reparents the heartbeat when its parent exits, so
327
+ // a changed parent id ends it as well; Windows never reparents.
325
328
  const ready = `${file}.heartbeat-${crypto.randomBytes(16).toString("hex")}`;
326
329
  let heartbeatChild: ReturnType<typeof spawn> | undefined;
327
330
  try {
@@ -334,7 +337,7 @@ export namespace GoBuildCacheCoordination {
334
337
  "const parent = Number(process.argv[3]);",
335
338
  "const ready = process.argv[4];",
336
339
  "const timer = setInterval(() => {",
337
- " try { process.kill(parent, 0); } catch { clearInterval(timer); process.exit(0); }",
340
+ " try { if (process.ppid !== parent) throw 0; process.kill(parent, 0); } catch { clearInterval(timer); process.exit(0); }",
338
341
  " try {",
339
342
  ' if (JSON.parse(fs.readFileSync(file, "utf8")).status === "complete") {',
340
343
  " clearInterval(timer); process.exit(0);",
@@ -2,6 +2,7 @@ import fs from "node:fs";
2
2
  import os from "node:os";
3
3
  import path from "node:path";
4
4
 
5
+ import { probeLocalProcess } from "../../../internal/probeLocalProcess";
5
6
  import { PluginBuildLockProtocol } from "./PluginBuildLockProtocol";
6
7
 
7
8
  /**
@@ -177,15 +178,16 @@ export namespace PluginBuildLockOwner {
177
178
  * process with its id exists. A holder on another host, or one whose id is
178
179
  * alive (possibly reused), is not proven gone.
179
180
  *
180
- * Only ESRCH proves absence. Permission errors, invalid probe arguments and
181
- * other failures remain inconclusive and must not authorize reclamation.
181
+ * Only ESRCH, or a Linux zombie that exited but was never collected, proves
182
+ * absence. Permission errors, invalid probe arguments and other failures
183
+ * remain inconclusive and must not authorize reclamation.
182
184
  *
183
- * @evidence contracts/common.md#principled-implementation Signal zero checks existence without terminating the process; same-host ESRCH establishes absence, while success or any other error does not.
184
- * @evidence contracts/common.md#clear-and-simple-design Host comparison and one native probe express the proof boundary directly, without a second process-discovery mechanism.
185
+ * @evidence contracts/common.md#principled-implementation probeLocalProcess checks existence without terminating the process; same-host ESRCH or an uncollected zombie establishes absence, while presence or any other error does not.
186
+ * @evidence contracts/common.md#clear-and-simple-design Host comparison and the shared probe express the proof boundary directly, without a second process-discovery mechanism.
185
187
  * @evidence contracts/common.md#prohibited-implementation-shortcuts Unexpected probe failures are not converted to a dead-owner special case or hidden behind retries.
186
- * @evidence contracts/common.md#meaningful-documentation Native paragraphs distinguish PID reuse, remote owners and each inconclusive failure class before the tags.
187
- * @evidence contracts/portability.md#os-neutral-implementation os.hostname and Node's platform-independent signal-zero probe preserve native host scope; only the explicit no-such-process code authorizes absence.
188
- * @evidence contracts/performance.md#efficient-algorithms A hostname comparison and one process-existence syscall avoid enumerating native processes; string comparison costs grow only with hostname length.
188
+ * @evidence contracts/common.md#meaningful-documentation Native paragraphs distinguish PID reuse, remote owners, uncollected zombies and each inconclusive failure class before the tags.
189
+ * @evidence contracts/portability.md#os-neutral-implementation os.hostname and Node's platform-independent signal-zero probe preserve native host scope; only the explicit no-such-process code, or Linux's own zombie state, authorizes absence.
190
+ * @evidence contracts/performance.md#efficient-algorithms A hostname comparison and one process-existence syscall, plus one /proc read on Linux, avoid enumerating native processes; string comparison costs grow only with hostname length.
189
191
  *
190
192
  * @evidenceExclude contracts/performance.md#reuse-equivalent-work Liveness is a time-sensitive observation; a previously absent or present PID cannot be cached as proof for a later reclamation.
191
193
  * @evidenceExclude contracts/performance.md#bound-retention-and-release-resources The query owns no retained handle or running task and returns only its immediate proof decision.
@@ -193,12 +195,7 @@ export namespace PluginBuildLockOwner {
193
195
  export function gone(owner: IRecord): boolean {
194
196
  if (owner.hostname.toLowerCase() !== os.hostname().toLowerCase())
195
197
  return false;
196
- try {
197
- process.kill(owner.pid, 0);
198
- return false;
199
- } catch (error) {
200
- return (error as NodeJS.ErrnoException).code === "ESRCH";
201
- }
198
+ return probeLocalProcess(owner.pid).result === "absent";
202
199
  }
203
200
 
204
201
  /**