ttsc 0.31.1 → 0.31.3

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 (146) hide show
  1. package/lib/compiler/internal/build/BuildExecution.d.ts +6 -3
  2. package/lib/compiler/internal/build/BuildExecution.js +40 -12
  3. package/lib/compiler/internal/build/BuildExecution.js.map +1 -1
  4. package/lib/compiler/internal/build/RunBuildOptions.d.ts +2 -1
  5. package/lib/compiler/internal/startResidentTransform.js +5 -4
  6. package/lib/compiler/internal/startResidentTransform.js.map +1 -1
  7. package/lib/compiler/internal/transformProjectInMemory.js +9 -7
  8. package/lib/compiler/internal/transformProjectInMemory.js.map +1 -1
  9. package/lib/internal/javascriptRuntimeCapabilities.d.ts +13 -4
  10. package/lib/internal/javascriptRuntimeCapabilities.js +56 -6
  11. package/lib/internal/javascriptRuntimeCapabilities.js.map +1 -1
  12. package/lib/internal/pluginSource.d.ts +1 -0
  13. package/lib/internal/pluginSource.js +1 -0
  14. package/lib/internal/pluginSource.js.map +1 -1
  15. package/lib/internal/resolveNodeBinary.d.ts +6 -1
  16. package/lib/internal/resolveNodeBinary.js +6 -2
  17. package/lib/internal/resolveNodeBinary.js.map +1 -1
  18. package/lib/internal/runtimeExecutableIdentity.d.ts +22 -13
  19. package/lib/internal/runtimeExecutableIdentity.js +192 -102
  20. package/lib/internal/runtimeExecutableIdentity.js.map +1 -1
  21. package/lib/launcher/internal/runTtsc.js +2 -2
  22. package/lib/launcher/internal/runTtsc.js.map +1 -1
  23. package/lib/launcher/internal/runtime/installRuntimeHooks.js +58 -4
  24. package/lib/launcher/internal/runtime/installRuntimeHooks.js.map +1 -1
  25. package/lib/launcher/internal/ttscserver/captureLSPPluginSelectionInputs.d.ts +5 -1
  26. package/lib/launcher/internal/ttscserver/captureLSPPluginSelectionInputs.js +7 -2
  27. package/lib/launcher/internal/ttscserver/captureLSPPluginSelectionInputs.js.map +1 -1
  28. package/lib/launcher/internal/ttscserver/runTtscserver.js +8 -1
  29. package/lib/launcher/internal/ttscserver/runTtscserver.js.map +1 -1
  30. package/lib/launcher/internal/watch/WatchTopology.d.ts +20 -3
  31. package/lib/launcher/internal/watch/WatchTopology.js +31 -9
  32. package/lib/launcher/internal/watch/WatchTopology.js.map +1 -1
  33. package/lib/plugin/internal/CapabilityResolutionFormat.d.ts +21 -62
  34. package/lib/plugin/internal/CapabilityResolutionFormat.js +15 -106
  35. package/lib/plugin/internal/CapabilityResolutionFormat.js.map +1 -1
  36. package/lib/plugin/internal/load/PluginDescriptorEvaluationCache.d.ts +6 -0
  37. package/lib/plugin/internal/load/PluginDescriptorEvaluationCache.js +4 -4
  38. package/lib/plugin/internal/load/PluginDescriptorEvaluationCache.js.map +1 -1
  39. package/lib/plugin/internal/load/loadProjectPlugins.d.ts +17 -3
  40. package/lib/plugin/internal/load/loadProjectPlugins.js +86 -23
  41. package/lib/plugin/internal/load/loadProjectPlugins.js.map +1 -1
  42. package/lib/plugin/internal/readCapabilityResolution.d.ts +6 -0
  43. package/lib/plugin/internal/readCapabilityResolution.js +8 -5
  44. package/lib/plugin/internal/readCapabilityResolution.js.map +1 -1
  45. package/lib/plugin/internal/source/GoEnvironmentReading.d.ts +9 -3
  46. package/lib/plugin/internal/source/GoEnvironmentReading.js +53 -7
  47. package/lib/plugin/internal/source/GoEnvironmentReading.js.map +1 -1
  48. package/lib/plugin/internal/source/NativeSourcePackages.d.ts +44 -6
  49. package/lib/plugin/internal/source/NativeSourcePackages.js +150 -8
  50. package/lib/plugin/internal/source/NativeSourcePackages.js.map +1 -1
  51. package/lib/plugin/internal/source/PluginBuildEnvironmentReadings.d.ts +21 -3
  52. package/lib/plugin/internal/source/PluginBuildEnvironmentReadings.js +32 -7
  53. package/lib/plugin/internal/source/PluginBuildEnvironmentReadings.js.map +1 -1
  54. package/lib/plugin/internal/source/PluginContentIdentities.d.ts +265 -0
  55. package/lib/plugin/internal/source/PluginContentIdentities.js +452 -0
  56. package/lib/plugin/internal/source/PluginContentIdentities.js.map +1 -0
  57. package/lib/plugin/internal/source/PluginLoadAnswers.d.ts +63 -0
  58. package/lib/plugin/internal/source/PluginLoadAnswers.js +128 -0
  59. package/lib/plugin/internal/source/PluginLoadAnswers.js.map +1 -0
  60. package/lib/plugin/internal/source/SourceBuildCacheLayout.d.ts +10 -0
  61. package/lib/plugin/internal/source/SourceBuildCacheLayout.js +12 -0
  62. package/lib/plugin/internal/source/SourceBuildCacheLayout.js.map +1 -1
  63. package/lib/plugin/internal/source/SourcePluginWorkspace.d.ts +13 -2
  64. package/lib/plugin/internal/source/SourcePluginWorkspace.js +140 -4
  65. package/lib/plugin/internal/source/SourcePluginWorkspace.js.map +1 -1
  66. package/lib/plugin/internal/source/buildSourcePlugin.d.ts +15 -2
  67. package/lib/plugin/internal/source/buildSourcePlugin.js +27 -4
  68. package/lib/plugin/internal/source/buildSourcePlugin.js.map +1 -1
  69. package/lib/plugin/internal/source/computeCacheKey.d.ts +21 -5
  70. package/lib/plugin/internal/source/computeCacheKey.js +71 -29
  71. package/lib/plugin/internal/source/computeCacheKey.js.map +1 -1
  72. package/lib/plugin/internal/source/hashPluginBuildEnvironment.d.ts +7 -3
  73. package/lib/plugin/internal/source/hashPluginBuildEnvironment.js +74 -35
  74. package/lib/plugin/internal/source/hashPluginBuildEnvironment.js.map +1 -1
  75. package/lib/plugin/internal/source/pluginBuildEnvironment.d.ts +8 -3
  76. package/lib/plugin/internal/source/pluginBuildEnvironment.js +8 -4
  77. package/lib/plugin/internal/source/pluginBuildEnvironment.js.map +1 -1
  78. package/lib/plugin/internal/source/pluginBuildEnvironmentWorker.js +12 -2
  79. package/lib/plugin/internal/source/pluginBuildEnvironmentWorker.js.map +1 -1
  80. package/lib/plugin/internal/source/pluginBuildVersions.d.ts +2 -2
  81. package/lib/plugin/internal/source/pluginBuildVersions.js +4 -17
  82. package/lib/plugin/internal/source/pluginBuildVersions.js.map +1 -1
  83. package/lib/plugin/internal/source/pluginSourceState.d.ts +6 -2
  84. package/lib/plugin/internal/source/pluginSourceState.js +20 -8
  85. package/lib/plugin/internal/source/pluginSourceState.js.map +1 -1
  86. package/lib/plugin/internal/source/pluginSourceStateHolds.d.ts +7 -2
  87. package/lib/plugin/internal/source/pluginSourceStateHolds.js +12 -7
  88. package/lib/plugin/internal/source/pluginSourceStateHolds.js.map +1 -1
  89. package/lib/plugin/internal/source/processPluginBuildEnvironment.d.ts +7 -2
  90. package/lib/plugin/internal/source/processPluginBuildEnvironment.js +8 -3
  91. package/lib/plugin/internal/source/processPluginBuildEnvironment.js.map +1 -1
  92. package/lib/plugin/internal/source/provenPluginSourceDigest.d.ts +28 -0
  93. package/lib/plugin/internal/source/provenPluginSourceDigest.js +41 -0
  94. package/lib/plugin/internal/source/provenPluginSourceDigest.js.map +1 -0
  95. package/lib/plugin/internal/source/pruneCacheFileRoot.d.ts +3 -2
  96. package/lib/plugin/internal/source/pruneCacheFileRoot.js +3 -2
  97. package/lib/plugin/internal/source/pruneCacheFileRoot.js.map +1 -1
  98. package/lib/plugin/internal/source/ttscPackageVersion.d.ts +26 -0
  99. package/lib/plugin/internal/source/ttscPackageVersion.js +47 -0
  100. package/lib/plugin/internal/source/ttscPackageVersion.js.map +1 -0
  101. package/lib/plugin/internal/writeCapabilityResolution.d.ts +6 -0
  102. package/lib/plugin/internal/writeCapabilityResolution.js +11 -7
  103. package/lib/plugin/internal/writeCapabilityResolution.js.map +1 -1
  104. package/lib/plugin/resolveCapabilityPlugins.js +67 -15
  105. package/lib/plugin/resolveCapabilityPlugins.js.map +1 -1
  106. package/lib/structures/internal/TtscSingleFileEmitOptions.d.ts +5 -1
  107. package/package.json +8 -8
  108. package/src/compiler/internal/build/BuildExecution.ts +48 -14
  109. package/src/compiler/internal/build/RunBuildOptions.ts +2 -1
  110. package/src/compiler/internal/startResidentTransform.ts +12 -3
  111. package/src/compiler/internal/transformProjectInMemory.ts +10 -2
  112. package/src/internal/javascriptRuntimeCapabilities.ts +71 -5
  113. package/src/internal/pluginSource.ts +1 -0
  114. package/src/internal/resolveNodeBinary.ts +12 -1
  115. package/src/internal/runtimeExecutableIdentity.ts +227 -141
  116. package/src/launcher/internal/runTtsc.ts +6 -2
  117. package/src/launcher/internal/runtime/installRuntimeHooks.ts +68 -3
  118. package/src/launcher/internal/ttscserver/captureLSPPluginSelectionInputs.ts +11 -2
  119. package/src/launcher/internal/ttscserver/runTtscserver.ts +18 -1
  120. package/src/launcher/internal/watch/WatchTopology.ts +38 -9
  121. package/src/plugin/internal/CapabilityResolutionFormat.ts +23 -107
  122. package/src/plugin/internal/load/PluginDescriptorEvaluationCache.ts +14 -4
  123. package/src/plugin/internal/load/loadProjectPlugins.ts +226 -98
  124. package/src/plugin/internal/readCapabilityResolution.ts +17 -6
  125. package/src/plugin/internal/source/GoEnvironmentReading.ts +74 -5
  126. package/src/plugin/internal/source/NativeSourcePackages.ts +262 -5
  127. package/src/plugin/internal/source/PluginBuildEnvironmentReadings.ts +41 -5
  128. package/src/plugin/internal/source/PluginContentIdentities.ts +563 -0
  129. package/src/plugin/internal/source/PluginLoadAnswers.ts +151 -0
  130. package/src/plugin/internal/source/SourceBuildCacheLayout.ts +14 -0
  131. package/src/plugin/internal/source/SourcePluginWorkspace.ts +176 -2
  132. package/src/plugin/internal/source/buildSourcePlugin.ts +43 -3
  133. package/src/plugin/internal/source/computeCacheKey.ts +103 -47
  134. package/src/plugin/internal/source/hashPluginBuildEnvironment.ts +144 -29
  135. package/src/plugin/internal/source/pluginBuildEnvironment.ts +9 -2
  136. package/src/plugin/internal/source/pluginBuildEnvironmentWorker.ts +18 -2
  137. package/src/plugin/internal/source/pluginBuildVersions.ts +5 -26
  138. package/src/plugin/internal/source/pluginSourceState.ts +28 -12
  139. package/src/plugin/internal/source/pluginSourceStateHolds.ts +15 -8
  140. package/src/plugin/internal/source/processPluginBuildEnvironment.ts +8 -2
  141. package/src/plugin/internal/source/provenPluginSourceDigest.ts +45 -0
  142. package/src/plugin/internal/source/pruneCacheFileRoot.ts +3 -2
  143. package/src/plugin/internal/source/ttscPackageVersion.ts +45 -0
  144. package/src/plugin/internal/writeCapabilityResolution.ts +29 -8
  145. package/src/plugin/resolveCapabilityPlugins.ts +87 -13
  146. package/src/structures/internal/TtscSingleFileEmitOptions.ts +8 -1
@@ -0,0 +1,151 @@
1
+ import crypto from "node:crypto";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+
5
+ import { E2ETrace } from "../../../internal/E2ETrace";
6
+ import type { PluginContentIdentities } from "./PluginContentIdentities";
7
+ import { SourceBuildCacheLayout } from "./SourceBuildCacheLayout";
8
+ import { recordCacheFileUse } from "./recordCacheFileUse";
9
+
10
+ /**
11
+ * Answers a plugin load computed from inputs it could identify by content, kept
12
+ * in the plugin cache root under that identity so a later process with equal
13
+ * inputs reads the answer instead of computing it again.
14
+ *
15
+ * WARNING (#1721, #1723): the key is the whole proof. A caller must fold into
16
+ * `identity` every input that can change the answer, as content identities
17
+ * (`PluginContentIdentities`) or the authoritative live probe results, never a
18
+ * path, a timestamp or a value it merely expects to be stable. An answer is
19
+ * written only after the computation succeeded on inputs the caller proved
20
+ * unchanged around it; a failed or unproved computation is never recorded,
21
+ * since a later process could not tell it from a current one.
22
+ *
23
+ * Entries are single files published by rename, aged out by the single-file
24
+ * collector and removed by `ttsc clean`. Their identity includes the format tag
25
+ * and the ttsc version, so another release never reads them.
26
+ *
27
+ * @evidence contracts/common.md#principled-implementation The content-addressed key carries every caller-declared input identity; reads accept only a matching format, version and kind, and writes are refused for unproved computations by the caller contract.
28
+ * @evidence contracts/common.md#clear-and-simple-design One owner stores and loads opaque answers; callers own key composition, validation of the answer shape and the decision to record.
29
+ * @evidence contracts/common.md#prohibited-implementation-shortcuts No answer is guessed; a missing, corrupt or foreign entry is a miss and the caller computes.
30
+ * @evidence contracts/common.md#meaningful-documentation Native paragraphs state the key contract that prevents stale answers, the recording rule and the storage lifetime.
31
+ * @evidence contracts/portability.md#os-neutral-implementation Native path joins and same-directory rename publication; keys are hashed text, independent of path dialect.
32
+ * @evidenceExclude contracts/performance.md#efficient-algorithms Members state their own costs.
33
+ * @evidenceExclude contracts/performance.md#reuse-equivalent-work Members define reuse through the caller's identity.
34
+ * @evidenceExclude contracts/performance.md#bound-retention-and-release-resources Members state retention; the namespace holds no state.
35
+ */
36
+ export namespace PluginLoadAnswers {
37
+ /**
38
+ * The answer recorded for `identity`, or `undefined` when none is.
39
+ *
40
+ * @param store The opened store, or `undefined` for no persistence.
41
+ * @param kind Answer family; families never share entries.
42
+ * @param identity Every input identity the answer depends on.
43
+ * @evidence contracts/common.md#principled-implementation An entry is returned only for the same format, version, kind and identity hash; the caller validates the value's shape.
44
+ * @evidence contracts/common.md#clear-and-simple-design One lookup returns an opaque value or undefined.
45
+ * @evidence contracts/common.md#prohibited-implementation-shortcuts Unreadable or foreign entries are misses.
46
+ * @evidence contracts/common.md#meaningful-documentation Parameters describe the identity contract.
47
+ * @evidence contracts/portability.md#os-neutral-implementation Native path join and file read.
48
+ * @evidence contracts/performance.md#efficient-algorithms Hashes the serialized identity and reads one small file; a hit refreshes its use time.
49
+ * @evidence contracts/performance.md#reuse-equivalent-work Processes share an answer only for an identical serialized identity under the same version.
50
+ * @evidence contracts/performance.md#bound-retention-and-release-resources Nothing is retained in memory; the collector owns entry lifetime.
51
+ */
52
+ export function read(
53
+ store: PluginContentIdentities.Store | undefined,
54
+ kind: string,
55
+ identity: unknown,
56
+ ): unknown {
57
+ if (store === undefined) return undefined;
58
+ const file = answerFile(store, kind, identity);
59
+ try {
60
+ const entry = JSON.parse(
61
+ fs.readFileSync(file, "utf8"),
62
+ ) as Partial<IEntry>;
63
+ if (
64
+ entry.format !== FORMAT ||
65
+ entry.version !== store.version ||
66
+ entry.kind !== kind ||
67
+ !Object.prototype.hasOwnProperty.call(entry, "value")
68
+ )
69
+ return undefined;
70
+ recordCacheFileUse(file);
71
+ E2ETrace.capabilityResolution("plugin-load-answer", {
72
+ kind,
73
+ outcome: "reused",
74
+ });
75
+ return entry.value;
76
+ } catch {
77
+ return undefined;
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Record `value` as the answer for `identity`. Failures are ignored: the
83
+ * answer just computed stands, and the next process computes it again.
84
+ *
85
+ * @param store The opened store, or `undefined` for no persistence.
86
+ * @param kind Answer family.
87
+ * @param identity Every input identity the answer depends on.
88
+ * @param value JSON-serializable answer the caller proved for `identity`.
89
+ * @evidence contracts/common.md#principled-implementation Publication happens only when the caller decided the computation was proven; the entry records the same identity framing that read checks.
90
+ * @evidence contracts/common.md#clear-and-simple-design One atomic publication through the shared metadata writer.
91
+ * @evidence contracts/common.md#prohibited-implementation-shortcuts A failed write never fails the load or fabricates an entry.
92
+ * @evidence contracts/common.md#meaningful-documentation Native prose states failure behavior.
93
+ * @evidence contracts/portability.md#os-neutral-implementation Same-directory staging and rename through the shared writer.
94
+ * @evidence contracts/performance.md#efficient-algorithms Serializes the identity and value once and writes one small file.
95
+ * @evidenceExclude contracts/performance.md#reuse-equivalent-work Publication creates the reusable entry; read owns reuse.
96
+ * @evidence contracts/performance.md#bound-retention-and-release-resources One file per identity, removed by the single-file collector or `ttsc clean`; staging cleanup belongs to the shared writer.
97
+ */
98
+ export function write(
99
+ store: PluginContentIdentities.Store | undefined,
100
+ kind: string,
101
+ identity: unknown,
102
+ value: unknown,
103
+ ): void {
104
+ if (store === undefined) return;
105
+ const file = answerFile(store, kind, identity);
106
+ try {
107
+ fs.mkdirSync(path.dirname(file), { recursive: true });
108
+ SourceBuildCacheLayout.replaceCacheMetadataFile(
109
+ file,
110
+ JSON.stringify({
111
+ format: FORMAT,
112
+ version: store.version,
113
+ kind,
114
+ value,
115
+ } satisfies IEntry),
116
+ );
117
+ E2ETrace.capabilityResolution("plugin-load-answer", {
118
+ kind,
119
+ outcome: "recorded",
120
+ });
121
+ } catch {
122
+ // The answer just computed stands; only its reuse is lost.
123
+ }
124
+ }
125
+
126
+ /** Entry format tag; move it when the entry shape changes. */
127
+ const FORMAT = "ttsc-plugin-load-answer-v1";
128
+
129
+ interface IEntry {
130
+ format: string;
131
+ version: string;
132
+ kind: string;
133
+ value: unknown;
134
+ }
135
+
136
+ function answerFile(
137
+ store: PluginContentIdentities.Store,
138
+ kind: string,
139
+ identity: unknown,
140
+ ): string {
141
+ const key = crypto
142
+ .createHash("sha256")
143
+ .update(JSON.stringify([FORMAT, store.version, kind, identity]))
144
+ .digest("hex");
145
+ return path.join(
146
+ store.root,
147
+ SourceBuildCacheLayout.ANSWER_CACHE_DIRNAME,
148
+ `${key}.json`,
149
+ );
150
+ }
151
+ }
@@ -53,6 +53,18 @@ export namespace SourceBuildCacheLayout {
53
53
  /** Directory of the capability-resolution answers inside the cache root. */
54
54
  export const CAPABILITY_CACHE_DIRNAME = "capabilities";
55
55
 
56
+ /**
57
+ * Directory of the content identities a plugin load proves from metadata
58
+ * (`PluginContentIdentities`) inside the cache root.
59
+ */
60
+ export const IDENTITY_CACHE_DIRNAME = "identities";
61
+
62
+ /**
63
+ * Directory of the plugin-load answers keyed by those identities, such as Go
64
+ * package selections and runtime capabilities (`PluginLoadAnswers`).
65
+ */
66
+ export const ANSWER_CACHE_DIRNAME = "answers";
67
+
56
68
  /** Directory of the ttsx runtime inside the cache root. */
57
69
  export const RUNTIME_CACHE_DIRNAME = "ttsx";
58
70
 
@@ -72,6 +84,8 @@ export namespace SourceBuildCacheLayout {
72
84
  export const CACHE_FILE_DIRNAMES: readonly string[] = [
73
85
  DESCRIPTOR_CACHE_DIRNAME,
74
86
  CAPABILITY_CACHE_DIRNAME,
87
+ IDENTITY_CACHE_DIRNAME,
88
+ ANSWER_CACHE_DIRNAME,
75
89
  ORPHAN_CACHE_DIRNAME,
76
90
  ];
77
91
 
@@ -1,11 +1,15 @@
1
+ import crypto from "node:crypto";
1
2
  import fs from "node:fs";
2
3
  import os from "node:os";
3
4
  import path from "node:path";
4
5
 
5
6
  import { OwnedSynchronousProcess } from "../../../internal/OwnedSynchronousProcess";
7
+ import { runtimeExecutableIdentity } from "../../../internal/runtimeExecutableIdentity";
6
8
  import { GoEnvironmentReading } from "./GoEnvironmentReading";
7
9
  import { GoSourceInputs } from "./GoSourceInputs";
8
10
  import { PluginBuildEnvironmentWitness } from "./PluginBuildEnvironmentWitness";
11
+ import { PluginContentIdentities } from "./PluginContentIdentities";
12
+ import { PluginLoadAnswers } from "./PluginLoadAnswers";
9
13
  import { SourcePluginAdmission } from "./SourcePluginAdmission";
10
14
  import { copiesPluginSourceEntry } from "./copiesPluginSourceEntry";
11
15
  import { formatGoWorkPath } from "./formatGoWorkPath";
@@ -240,13 +244,23 @@ export namespace SourcePluginWorkspace {
240
244
  * Caller views share that native state and syntax, while each default
241
245
  * diagnostic label remains with its current caller.
242
246
  *
247
+ * An optional record store carries the Go environment-file discovery hint
248
+ * across processes, and the parse of identical bytes by the same Go command
249
+ * (#1729): every launch otherwise ran `go mod edit -json` on an unchanged
250
+ * `go.mod`. A recorded parse is keyed by the manifest bytes, the content
251
+ * identities of the launcher and of the `GOROOT/bin/go` the live `go env
252
+ * -json GOROOT` reports, the `GOENV` file's bytes, and every `GO*`/`CGO_*`
253
+ * variable the command receives. It is consulted and written only under the
254
+ * same qualified tool witness that admits in-load sharing, so the live
255
+ * witness query still runs on every load.
256
+ *
243
257
  * @evidence contracts/common.md#principled-implementation Actual go mod edit JSON determines module/replacement identities; current bytes, selected/dispatched-tool metadata and Go's environment-file witness qualify reuse under the existing metadata-distinguishability premise. Environment values are copied at reader creation. Missing go.mod returns the existing empty module observation, and moving tool/manifest inputs cannot publish a parsed record.
244
258
  * @evidence contracts/common.md#clear-and-simple-design One factory acquires load-owned native reader state by selected tool; a thin caller view forwards its diagnostic label without duplicating the byte-content map. Current reads and post-parse byte/tool comparisons qualify insertion.
245
259
  * @evidence contracts/common.md#prohibited-implementation-shortcuts No custom Go manifest parser or global context-insensitive memo is introduced.
246
260
  * @evidence contracts/common.md#meaningful-documentation Native prose states this operation's authority and delegated boundaries; fields retain separate native comments.
247
261
  * @evidence contracts/portability.md#os-neutral-implementation Native path resolution and temporary cwd avoid deep Windows working-directory limits; the selected tool handles manifest syntax.
248
262
  * @evidence contracts/performance.md#efficient-algorithms Reader acquisition performs a selected-tool registry lookup; every nonmissing read qualifies tool/environment metadata and reads current manifest bytes. A cold or stale witness first observes the selected launcher; an unavailable or nonfile launcher skips optional environment acquisition and permits only the actual uncached parse attempt. An observed file lends its same pre-read stat to the witness before acquiring current Go values and GOENV evidence through the common reader, without a GOROOT walk. Only complete native GOENV/GOROOT locations qualify syntax sharing; a dispatched tool is witnessed before parsing. Unavailable native environment acquisition disables syntax sharing while preserving actual parse success or failure. Each distinct byte string that earns stable successful syntax in that tool epoch runs Go mod edit and complete JSON parsing once, then rechecks bytes/tool metadata before caching; copied manifests share that parse. Failed queries remain uncached and report the current caller's diagnostic label.
249
- * @evidence contracts/performance.md#reuse-equivalent-work The caller may share this reader only within one load and the same pinned tool/effective environment; parsed syntax is not cross-load or native containment authority. Caller views share native state while preserving failure attribution; diagnostic labels are not syntax cache inputs.
263
+ * @evidence contracts/performance.md#reuse-equivalent-work The caller may share this reader only within one load and the same pinned tool/effective environment; parsed syntax is never native containment authority. Across loads only a parse recorded under the content identities of the bytes, both Go executables, the GOENV file and the Go variables is reused, and only under a qualified live tool witness. Caller views share native state while preserving failure attribution; diagnostic labels are not syntax cache inputs.
250
264
  * @evidence contracts/performance.md#bound-retention-and-release-resources The map retains distinct manifest bytes and parsed syntax for its caller's load/build lifetime; process/output capture cleanup remains delegated.
251
265
  */
252
266
  export function createGoModReader(
@@ -254,6 +268,7 @@ export namespace SourcePluginWorkspace {
254
268
  pluginName: string,
255
269
  env: NodeJS.ProcessEnv,
256
270
  readers?: Map<string, GoModReader>,
271
+ identities?: PluginContentIdentities.Store,
257
272
  ): GoModReader {
258
273
  const existing = readers?.get(goBinary);
259
274
  if (existing !== undefined)
@@ -275,6 +290,9 @@ export namespace SourcePluginWorkspace {
275
290
  const cache = new Map<string, GoModInfo>();
276
291
  const environment = { ...env };
277
292
  let toolWitness: PluginBuildEnvironmentWitness.Record | undefined;
293
+ // The tool part of a recorded parse's identity, formed with each qualified
294
+ // witness; undefined without a store or a provable tool.
295
+ let toolIdentity: unknown;
278
296
  const reader: GoModReader = {
279
297
  read(dir, diagnosticPlugin = pluginName) {
280
298
  OwnedSynchronousProcess.checkpoint();
@@ -315,6 +333,7 @@ export namespace SourcePluginWorkspace {
315
333
  environment,
316
334
  ["GOROOT"],
317
335
  witness,
336
+ identities,
318
337
  )
319
338
  : undefined;
320
339
  if (
@@ -338,6 +357,16 @@ export namespace SourcePluginWorkspace {
338
357
  complete && PluginBuildEnvironmentWitness.holds(witness)
339
358
  ? witness
340
359
  : undefined;
360
+ toolIdentity =
361
+ toolWitness === undefined || identities === undefined
362
+ ? undefined
363
+ : goModToolIdentity(
364
+ goBinary,
365
+ observed!.GOROOT as string,
366
+ observed!.GOENV as string,
367
+ environment,
368
+ identities,
369
+ );
341
370
  }
342
371
  // UTF-8 decoding is lossy for malformed input. Only identical bytes
343
372
  // can share Go syntax, even when Go serializes two inputs identically.
@@ -346,6 +375,26 @@ export namespace SourcePluginWorkspace {
346
375
  if (cached !== undefined) {
347
376
  return cached;
348
377
  }
378
+ // WARNING (#1729): a new process asks the record store before running
379
+ // Go on bytes this exact command already parsed. Never key this on a
380
+ // path or on the reader's metadata witness alone.
381
+ const answer =
382
+ toolIdentity === undefined
383
+ ? undefined
384
+ : [
385
+ toolIdentity,
386
+ crypto.createHash("sha256").update(bytes).digest("hex"),
387
+ ];
388
+ const recalled =
389
+ answer === undefined
390
+ ? undefined
391
+ : recallGoModInfo(
392
+ PluginLoadAnswers.read(identities, GO_MOD_SYNTAX, answer),
393
+ );
394
+ if (recalled !== undefined) {
395
+ cache.set(byteKey, recalled);
396
+ return recalled;
397
+ }
349
398
  const info = readGoModInfo(
350
399
  resolved,
351
400
  goBinary,
@@ -365,7 +414,14 @@ export namespace SourcePluginWorkspace {
365
414
  );
366
415
  // An unavailable environment observation can still execute a fresh Go
367
416
  // parse, but it cannot authorize sharing that result on another call.
368
- if (toolWitness !== undefined) cache.set(byteKey, info);
417
+ if (toolWitness !== undefined) {
418
+ cache.set(byteKey, info);
419
+ if (answer !== undefined)
420
+ PluginLoadAnswers.write(identities, GO_MOD_SYNTAX, answer, {
421
+ modulePath: info.modulePath,
422
+ directives: info.directives,
423
+ });
424
+ }
369
425
  return info;
370
426
  },
371
427
  };
@@ -445,6 +501,124 @@ export namespace SourcePluginWorkspace {
445
501
  };
446
502
  }
447
503
 
504
+ /** Answer family of `go mod edit -json` parses (`PluginLoadAnswers`). */
505
+ const GO_MOD_SYNTAX = "go-mod-syntax";
506
+
507
+ /**
508
+ * Everything besides the manifest bytes that decides how `go mod edit -json`
509
+ * parses them, by content: the launcher, the `GOROOT/bin/go` the live query
510
+ * reported, the `GOENV` file that can select another toolchain, and the Go
511
+ * variables the command receives. `undefined` when an executable cannot be
512
+ * identified or the file cannot be read, so the parse runs.
513
+ */
514
+ function goModToolIdentity(
515
+ goBinary: string,
516
+ goRoot: string,
517
+ goEnv: string,
518
+ env: NodeJS.ProcessEnv,
519
+ identities: PluginContentIdentities.Store,
520
+ ): unknown {
521
+ const dispatchedPath = path.join(
522
+ goRoot,
523
+ "bin",
524
+ process.platform === "win32" ? "go.exe" : "go",
525
+ );
526
+ // Only an executable the store can prove from metadata may key the record:
527
+ // streaming both executables on a device without a reference would cost
528
+ // more than the parse it replaces.
529
+ if (
530
+ !provableFromMetadata(goBinary, identities) ||
531
+ !provableFromMetadata(dispatchedPath, identities)
532
+ )
533
+ return undefined;
534
+ const launcher = runtimeExecutableIdentity(goBinary, identities);
535
+ const dispatched = runtimeExecutableIdentity(dispatchedPath, identities);
536
+ if (launcher === undefined || dispatched === undefined) return undefined;
537
+ let goEnvFile: string | null;
538
+ if (goEnv === "off") goEnvFile = "off";
539
+ else
540
+ try {
541
+ goEnvFile = crypto
542
+ .createHash("sha256")
543
+ .update(fs.readFileSync(goEnv))
544
+ .digest("hex");
545
+ } catch (error) {
546
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT")
547
+ return undefined;
548
+ goEnvFile = null;
549
+ }
550
+ return [
551
+ 1,
552
+ launcher,
553
+ goRoot,
554
+ dispatched,
555
+ goEnv,
556
+ goEnvFile,
557
+ Object.entries(GoSourceInputs.goBuildEnv(goBinary, undefined, env))
558
+ .filter(
559
+ (entry): entry is [string, string] =>
560
+ entry[1] !== undefined && /^(GO|CGO_)/i.test(entry[0]),
561
+ )
562
+ .sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0)),
563
+ ];
564
+ }
565
+
566
+ /**
567
+ * Whether the file's stamp is separable from the store's reference on its
568
+ * device, the precondition for proving its bytes from a record.
569
+ */
570
+ function provableFromMetadata(
571
+ file: string,
572
+ identities: PluginContentIdentities.Store,
573
+ ): boolean {
574
+ try {
575
+ return PluginContentIdentities.separable(
576
+ identities.references,
577
+ fs.statSync(file, { bigint: true }),
578
+ );
579
+ } catch {
580
+ return false;
581
+ }
582
+ }
583
+
584
+ /**
585
+ * A recorded parse in the shape `readGoModInfo` returns, or `undefined` when
586
+ * the record does not have it.
587
+ */
588
+ function recallGoModInfo(value: unknown): GoModInfo | undefined {
589
+ if (typeof value !== "object" || value === null) return undefined;
590
+ const record = value as { modulePath?: unknown; directives?: unknown };
591
+ if (
592
+ (record.modulePath !== null && typeof record.modulePath !== "string") ||
593
+ !Array.isArray(record.directives) ||
594
+ !record.directives.every(isReplaceDirective)
595
+ )
596
+ return undefined;
597
+ const directives = record.directives as NonNullable<GoModJson["Replace"]>;
598
+ return {
599
+ modulePath: record.modulePath,
600
+ directives,
601
+ replacements: directives
602
+ .map(jsonReplacementToGoModReplacement)
603
+ .filter((replacement) => replacement !== null),
604
+ };
605
+ }
606
+
607
+ function isReplaceDirective(value: unknown): boolean {
608
+ if (typeof value !== "object" || value === null) return false;
609
+ const directive = value as Record<string, unknown>;
610
+ return [directive.Old, directive.New].every(
611
+ (side) =>
612
+ side === undefined ||
613
+ (typeof side === "object" &&
614
+ side !== null &&
615
+ ["Path", "Version"].every((key) => {
616
+ const field = (side as Record<string, unknown>)[key];
617
+ return field === undefined || typeof field === "string";
618
+ })),
619
+ );
620
+ }
621
+
448
622
  function jsonReplacementToGoModReplacement(
449
623
  replacement: NonNullable<GoModJson["Replace"]>[number],
450
624
  ): GoModReplacement | null {
@@ -18,6 +18,7 @@ import { PluginBinaryUse } from "./PluginBinaryUse";
18
18
  import { PluginBuildEnvironmentWitness } from "./PluginBuildEnvironmentWitness";
19
19
  import type { PluginBuildLockLease } from "./PluginBuildLockLease";
20
20
  import { PluginBuildLockProtocol } from "./PluginBuildLockProtocol";
21
+ import { PluginContentIdentities } from "./PluginContentIdentities";
21
22
  import { SourceBuildCacheLayout } from "./SourceBuildCacheLayout";
22
23
  import type { SourceBuildFilesystemOperations } from "./SourceBuildFilesystemOperations";
23
24
  import { SourcePluginAdmission } from "./SourcePluginAdmission";
@@ -88,8 +89,8 @@ import { withGoBuildCacheLease } from "./withGoBuildCacheLease";
88
89
  * @evidence contracts/common.md#prohibited-implementation-shortcuts A changed witness is never overwritten or accepted. A fresh epoch takes a new toolchain reading and performs the actual build or qualified adoption; source, native and cleanup failures stay terminal. No selected SDK tool is warmed by a special command, and no foreign environment or memo is rebased. Shared Go objects retain their owner's toolID/actionID contract; same-version custom tool semantics beyond that contract are not newly guaranteed.
89
90
  * @evidence contracts/common.md#meaningful-documentation Native paragraphs explain effective environment, recorded-input comparisons and their trust/observation limits, reader registration and managed versus explicit roots; option-map comments state their reading provenance with blank member separation.
90
91
  * @evidence contracts/portability.md#os-neutral-implementation Node path/physical-cache/temp APIs preserve native identities; executable resolution and Windows command handling are isolated owners, and the binary filename explicitly follows its executable platform.
91
- * @evidence contracts/performance.md#efficient-algorithms At most three epochs redo native toolchain observation, key construction, materialization and guarded admission; each can run one Go build, preceded by one batched package metadata command when the loader requires ownership admission. The shared compiler reader can itself make three version observations, hence at most nine such cold observations for a persistently moving compiler and no build when its key witness fails. Full-file bytes, entries, path/sort text, contributors, external trees and witness populations drive work. Scoped cancellation is sampled between phases/entries/commands and does not bound native calls or delegated hashing. First source digest readings are reused across epochs, while each scratch is independently checked. Existing lock contention has its separate wait budget and native Go compiler work remains delegated.
92
- * @evidence contracts/performance.md#reuse-equivalent-work Existing binaries and concurrent builders share the version/platform/source/environment key with reader admission before return, assuming trustworthy cache producers and supplied digest maps. Fixed trimpath compilation removes disposable snapshot paths from Go object identities. Shared load readings and sequential source/toolchain comparisons reject observed changes; metadata reuse and unobserved concurrent mutation remain the underlying witnesses' limits.
92
+ * @evidence contracts/performance.md#efficient-algorithms At most three epochs redo native toolchain observation, key construction, materialization and guarded admission; the first can key on the load's witnessed same-directory reading instead of observing the toolchain again, and later ones always observe it. Each can run one Go build, preceded by one batched package metadata command when the loader requires ownership admission. The shared compiler reader can itself make three version observations, hence at most nine such cold observations for a persistently moving compiler and no build when its key witness fails. Full-file bytes, entries, path/sort text, contributors, external trees and witness populations drive work. Scoped cancellation is sampled between phases/entries/commands and does not bound native calls or delegated hashing. First source digest readings are reused across epochs, while each scratch is independently checked. Existing lock contention has its separate wait budget and native Go compiler work remains delegated.
93
+ * @evidence contracts/performance.md#reuse-equivalent-work Existing binaries and concurrent builders share the version/platform/source/environment key with reader admission before return, assuming trustworthy cache producers and supplied digest maps. The key's content digests come from the record store when their separable metadata still matches, so a new process does not read unchanged sources, SDK or compiler bytes to find an existing binary (#1722). Fixed trimpath compilation removes disposable snapshot paths from Go object identities. Shared load readings and sequential source/toolchain comparisons reject observed changes; metadata reuse and unobserved concurrent mutation remain the underlying witnesses' limits.
93
94
  * @evidence contracts/performance.md#bound-retention-and-release-resources Scratch directories and build/cache leases have finally-based cleanup, including cancellation; cancelled work admits no new post-build prune pass. Scratch removal or lease cleanup can fail, and selected synchronous child settlement does not join arbitrary descendants. Repeated commands can repeat external tool effects; only ttsc-owned failed outputs are discarded, and Go-owned object entries retain their existing lifetime. Pending binary cleanup is best-effort. Reader tokens and their process map grow with distinct physical keys until process exit. Managed pruning attempts age/LRU reclamation while protecting live, unknown and selected entries, so it is not a hard disk bound; explicit roots remain caller-managed.
94
95
  */
95
96
  export function buildSourcePlugin(opts: {
@@ -113,7 +114,21 @@ export function buildSourcePlugin(opts: {
113
114
  */
114
115
  environmentDigests?: Map<string, string>;
115
116
 
117
+ /**
118
+ * Witnesses of the readings in `environmentDigests` (`computeCacheKey`), so
119
+ * the first epoch keys on a reading package selection already took. A
120
+ * discarded epoch reads the toolchain again instead.
121
+ */
122
+ environmentWitnesses?: Map<string, PluginBuildEnvironmentWitness.Record>;
123
+
116
124
  filesystem?: Partial<SourceBuildFilesystemOperations>;
125
+
126
+ /**
127
+ * Record store the loader opened for this load, so its key reuses content
128
+ * identities across processes (#1722); a standalone build opens its own.
129
+ */
130
+ identities?: PluginContentIdentities.Store;
131
+
117
132
  label?: string;
118
133
  overlayDirs?: readonly string[];
119
134
  quiet?: boolean;
@@ -140,9 +155,17 @@ export function buildSourcePlugin(opts: {
140
155
  for (let attempt = 0; ; attempt += 1) {
141
156
  OwnedSynchronousProcess.checkpoint();
142
157
  const environmentDigests = new Map(opts.environmentDigests);
158
+ // Only the first epoch may key on a reading taken before it; a discarded
159
+ // epoch means the toolchain moved, so later ones read it again.
160
+ const environmentWitnesses =
161
+ opts.environmentWitnesses === undefined
162
+ ? undefined
163
+ : new Map(attempt === 0 ? opts.environmentWitnesses : []);
143
164
  try {
144
165
  const binary = buildSourcePluginAttempt(
145
- attempt === 0 ? opts : { ...opts, goModReaders: undefined },
166
+ attempt === 0
167
+ ? { ...opts, environmentWitnesses }
168
+ : { ...opts, environmentWitnesses, goModReaders: undefined },
146
169
  env,
147
170
  sourceDigests,
148
171
  environmentDigests,
@@ -152,6 +175,8 @@ export function buildSourcePlugin(opts: {
152
175
  opts.sourceDigests?.set(directory, digest);
153
176
  for (const [directory, digest] of environmentDigests)
154
177
  opts.environmentDigests?.set(directory, digest);
178
+ for (const [directory, witness] of environmentWitnesses ?? [])
179
+ opts.environmentWitnesses?.set(directory, witness);
155
180
  return binary;
156
181
  } catch (error) {
157
182
  if (
@@ -192,6 +217,18 @@ function buildSourcePluginAttempt(
192
217
  );
193
218
  ensureExecutableGoToolchain(goBinary, compiler.bundled);
194
219
  OwnedSynchronousProcess.checkpoint();
220
+ const identities =
221
+ opts.identities ??
222
+ PluginContentIdentities.open({
223
+ projectRoot: opts.baseDir,
224
+ cacheDir: opts.cacheDir,
225
+ env,
226
+ sources: [
227
+ dir,
228
+ ...overlayDirs,
229
+ ...contributors.map((contributor) => contributor.source),
230
+ ],
231
+ });
195
232
  // The digest of every directory the key covers, as the key read it, which
196
233
  // the build proves against what it compiled.
197
234
  const goModReader = SourcePluginWorkspace.createGoModReader(
@@ -199,6 +236,7 @@ function buildSourcePluginAttempt(
199
236
  opts.pluginName,
200
237
  env,
201
238
  opts.goModReaders,
239
+ identities,
202
240
  );
203
241
  const environmentWitness: PluginBuildEnvironmentWitness.Record = new Map();
204
242
  const key = computeCacheKey({
@@ -211,8 +249,10 @@ function buildSourcePluginAttempt(
211
249
  goModReader,
212
250
  filesystem: opts.filesystem,
213
251
  goBinary,
252
+ identities,
214
253
  overlayDirs,
215
254
  environmentDigests,
255
+ environmentWitnesses: opts.environmentWitnesses,
216
256
  sourceDigests,
217
257
  ttscVersion: opts.ttscVersion,
218
258
  tsgoVersion: opts.tsgoVersion,