@adrifer/winix 0.3.0-preview.4 → 0.3.0-preview.5

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.
package/README.md CHANGED
@@ -1005,9 +1005,30 @@ cancels the whole switch without activation or cleanup. Switches without removal
1005
1005
  and `--dry` do not prompt. Use `winix switch --yes` (or `-y`) to accept removals
1006
1006
  for automation; noninteractive switches with removals require this flag.
1007
1007
 
1008
- Startup file hashes and installed package versions detect drift. External edits
1009
- or version changes block reconciliation rather than silently deleting or
1010
- overwriting managed resources. Restore the recorded resource, or explicitly
1008
+ During native Windows `switch`, a package without an inline `version` uses its
1009
+ lock as a minimum version and fresh-install target. An absent package installs
1010
+ the lock; an older installed version upgrades; a newer installed version is kept
1011
+ and reported. The generated activation document targets that installed version,
1012
+ not the older lock. External upgrades of owned packages retain ownership and
1013
+ refresh the observed snapshot after successful activation. Pre-installed packages
1014
+ remain unowned even when upgraded by Winix.
1015
+
1016
+ An inline declaration such as `windows.package({ id: "Git.Git", version: "2.44.0" })`
1017
+ is exact. Installed version mismatches stop before activation, without an
1018
+ automatic downgrade. Unequal versions that are not dotted numeric values cannot
1019
+ be reliably ordered and fail with instructions to resolve them explicitly.
1020
+
1021
+ After a successful interactive switch that kept a newer floating package,
1022
+ Winix asks `Update winix-windows.lock to <version>? [y/N]`. Declining keeps the
1023
+ lock and still completes the switch. Dry/noninteractive runs never ask or
1024
+ update observed-version pins; `--yes` confirms removals, not lock updates.
1025
+ Normal missing-pin resolution remains unchanged. These defaults apply to
1026
+ `winix switch`, not direct generated-script/DSC activation or plain `apply`.
1027
+
1028
+ Startup file hashes and installed package versions still protect removals.
1029
+ External shortcut edits, package downgrades, and exact-pin drift block
1030
+ reconciliation rather than silently deleting or overwriting managed resources.
1031
+ Restore the recorded resource, or explicitly
1011
1032
  release Winix's removal rights without deleting anything:
1012
1033
 
1013
1034
  ```powershell
@@ -1,8 +1,11 @@
1
+ import { evaluate } from "../../evaluator/index.js";
1
2
  import { type WindowsLock, type WindowsPackageVersionResolver, type WindowsModuleVersionResolver } from "../../backends/windows/lockfile.js";
2
3
  import { type WindowsWslImageResolver } from "../../backends/windows/wsl.js";
3
4
  import { type WindowsWslRepositoryResolver } from "../../backends/windows/wsl-setup.js";
4
5
  import { type ActivationPlatform } from "../activation.js";
5
6
  interface ApplyOptions {
7
+ /** Activation-only overrides; the committed lock and inline declarations remain unchanged. */
8
+ windowsPackageVersions?: (hosts: ReturnType<typeof evaluate>, lock: WindowsLock) => Promise<Record<string, string>>;
6
9
  host?: string;
7
10
  dry: boolean;
8
11
  diff: boolean;
@@ -32,8 +32,14 @@ export async function applyWorkspace(cwd, opts) {
32
32
  ? await prepareWindowsLock(configDir, windowsHosts, opts.dry || opts.windowsLockDry === true, opts.resolveWindowsPackageVersion ?? resolveWingetVersion, opts.resolveWindowsModuleVersion ?? resolvePowerShellModuleVersion, opts.resolveWindowsWslImage ?? resolveNixosWslImage, opts.resolveWindowsWslRepository ?? resolveWslRepository)
33
33
  : undefined;
34
34
  const nixOutput = nixHosts.length > 0 ? generateNix(workspace, nixHosts) : undefined;
35
+ const runtimeVersions = windowsLock && opts.windowsPackageVersions
36
+ ? await opts.windowsPackageVersions(windowsHosts, windowsLock) : {};
37
+ const activationLock = windowsLock ? {
38
+ ...windowsLock,
39
+ packages: Object.fromEntries(Object.entries(windowsLock.packages).map(([id, pin]) => [id, runtimeVersions[id] === undefined ? pin : { ...pin, version: runtimeVersions[id] }])),
40
+ } : undefined;
35
41
  const windowsOutput = windowsHosts.length > 0
36
- ? generateWindows(windowsHosts, windowsLock, configDir)
42
+ ? generateWindows(windowsHosts, activationLock, configDir)
37
43
  : undefined;
38
44
  printWarnings(nixOutput?.warnings ?? []);
39
45
  printWarnings(windowsOutput?.warnings ?? []);
@@ -7,6 +7,8 @@ import { loadWorkspace } from "../loader.js";
7
7
  import { runCommand } from "../run.js";
8
8
  import { activationCommand, assertActivationSupported, platformForEvaluatedHost, } from "../activation.js";
9
9
  import { applyWorkspace } from "./apply.js";
10
+ import { readWindowsLock, serializeWindowsLock, writeWindowsLock } from "../../backends/windows/lockfile.js";
11
+ import { compareWindowsPackageVersions, confirmWindowsPackageLockUpdate, effectiveWindowsPackageVersion, } from "../windows-package-versions.js";
10
12
  export async function switchCommand(cwd, opts) {
11
13
  const { workspace, configDir } = await loadWorkspace(cwd);
12
14
  const evaluated = evaluate(workspace);
@@ -21,18 +23,67 @@ export async function switchCommand(cwd, opts) {
21
23
  const statePath = platform === "windows" && process.platform === "win32"
22
24
  ? opts.windowsState?.path ?? windowsStatePath() : undefined;
23
25
  const performSwitch = async () => {
26
+ const adapter = opts.windowsState?.adapter ?? nativeWindowsStateAdapter;
27
+ const requirements = {};
28
+ const activationVersions = {};
29
+ const newer = [];
24
30
  const result = await applyWorkspace(cwd, {
25
31
  host: hostName,
26
32
  dry: opts.dry && platform === "windows",
27
33
  diff: false,
28
34
  windowsLockDry: opts.dry,
35
+ windowsPackageVersions: statePath ? async (hosts, lock) => {
36
+ const versions = {};
37
+ for (const host of hosts) {
38
+ for (const resource of managedWindowsResources(host)) {
39
+ if (resource.kind !== "package")
40
+ continue;
41
+ const pkg = host.windows.packages?.[resource.id];
42
+ const version = pkg?.version ?? lock.packages[resource.id]?.version;
43
+ if (!version)
44
+ throw new Error(`Missing package version: ${resource.id}`);
45
+ const requirement = { version, exact: pkg?.version !== undefined };
46
+ requirements[resourceKey(resource)] = requirement;
47
+ const snapshot = await adapter.inspect(resource);
48
+ if (snapshot !== null && !("version" in snapshot))
49
+ throw new Error(`Invalid package snapshot: ${resource.id}`);
50
+ const installed = snapshot?.version;
51
+ versions[resource.id] = effectiveWindowsPackageVersion(resource.id, requirement, installed);
52
+ activationVersions[resourceKey(resource)] = versions[resource.id];
53
+ if (!requirement.exact && installed !== undefined && compareWindowsPackageVersions(installed, version) > 0) {
54
+ console.log(`Keeping ${resource.id} ${installed} (locked ${version}).`);
55
+ newer.push({ id: resource.id, installed, locked: version });
56
+ }
57
+ }
58
+ }
59
+ return versions;
60
+ } : undefined,
29
61
  });
30
62
  const activationTarget = platform === "windows"
31
63
  ? windowsConfigurationForHost(result.outDir, hostName)
32
64
  : flakeRefForHost(result.outDir, hostName);
33
65
  const command = activationCommand(platform, activationTarget);
34
66
  console.log(`\nRunning: ${command.join(" ")}`);
35
- const activate = () => runCommand(command[0], command.slice(1), { dry: opts.dry });
67
+ const activate = async () => {
68
+ if (statePath && !opts.dry) {
69
+ for (const resource of managedWindowsResources(selected)) {
70
+ if (resource.kind !== "package")
71
+ continue;
72
+ const key = resourceKey(resource);
73
+ const current = await adapter.inspect(resource);
74
+ if (current !== null) {
75
+ if (!("version" in current))
76
+ throw new Error(`Invalid package snapshot: ${resource.id}`);
77
+ effectiveWindowsPackageVersion(resource.id, requirements[key], current.version);
78
+ if (!requirements[key].exact &&
79
+ compareWindowsPackageVersions(current.version, activationVersions[key]) > 0) {
80
+ throw new Error(`Package ${resource.id} updated during switch. Rerun switch to keep ${current.version}; refusing stale activation.`);
81
+ }
82
+ }
83
+ }
84
+ }
85
+ await runCommand(command[0], command.slice(1), { dry: opts.dry });
86
+ };
36
87
  if (statePath) {
37
88
  const desired = managedWindowsResources(selected);
38
89
  const windows = selected.windows;
@@ -50,12 +101,40 @@ export async function switchCommand(cwd, opts) {
50
101
  statePath,
51
102
  owner: await windowsStateOwner(configDir, hostName),
52
103
  desired, definitions,
104
+ packageRequirements: requirements,
53
105
  dry: opts.dry,
54
106
  yes: opts.yes,
55
- adapter: opts.windowsState?.adapter ?? nativeWindowsStateAdapter,
107
+ adapter,
56
108
  lockHeld: true,
57
109
  activate,
58
110
  });
111
+ if (!opts.dry && lock) {
112
+ let expected = serializeWindowsLock(lock);
113
+ for (const candidate of newer) {
114
+ const current = await adapter.inspect({
115
+ kind: "package", id: candidate.id, source: lock.packages[candidate.id].source,
116
+ });
117
+ if (!current || !("version" in current) || current.version !== candidate.installed) {
118
+ console.log(`Not updating lock for ${candidate.id}: installed version changed during activation.`);
119
+ continue;
120
+ }
121
+ if (!await confirmWindowsPackageLockUpdate(candidate.id, candidate.installed, candidate.locked))
122
+ continue;
123
+ const saved = readWindowsLock(configDir);
124
+ if (!saved || serializeWindowsLock(saved) !== expected) {
125
+ throw new Error("winix-windows.lock changed during switch; refusing to overwrite concurrent changes.");
126
+ }
127
+ const now = new Date().toISOString();
128
+ const updated = {
129
+ ...saved, generatedAt: now, packages: {
130
+ ...saved.packages, [candidate.id]: { ...saved.packages[candidate.id], version: candidate.installed, resolvedAt: now },
131
+ },
132
+ };
133
+ writeWindowsLock(configDir, updated);
134
+ expected = serializeWindowsLock(updated);
135
+ console.log(`Updated winix-windows.lock: ${candidate.id} ${candidate.locked} -> ${candidate.installed}.`);
136
+ }
137
+ }
59
138
  }
60
139
  else {
61
140
  if (platform === "windows")
@@ -0,0 +1,8 @@
1
+ /** Only dotted numeric versions have an unambiguous ordering across Windows installers. */
2
+ export declare function compareWindowsPackageVersions(left: string, right: string): number;
3
+ export interface WindowsPackageRequirement {
4
+ version: string;
5
+ exact: boolean;
6
+ }
7
+ export declare function effectiveWindowsPackageVersion(id: string, requirement: WindowsPackageRequirement, installed?: string): string;
8
+ export declare function confirmWindowsPackageLockUpdate(id: string, installed: string, locked: string): Promise<boolean>;
@@ -0,0 +1,43 @@
1
+ import { createInterface } from "node:readline/promises";
2
+ /** Only dotted numeric versions have an unambiguous ordering across Windows installers. */
3
+ export function compareWindowsPackageVersions(left, right) {
4
+ if (left === right)
5
+ return 0;
6
+ if (!/^\d+(?:\.\d+)*$/.test(left) || !/^\d+(?:\.\d+)*$/.test(right)) {
7
+ throw new Error(`Cannot reliably compare Windows package versions "${left}" and "${right}". Declare an exact version or resolve the package version explicitly.`);
8
+ }
9
+ const a = left.split(".").map(BigInt);
10
+ const b = right.split(".").map(BigInt);
11
+ for (let index = 0; index < Math.max(a.length, b.length); index++) {
12
+ const difference = (a[index] ?? 0n) - (b[index] ?? 0n);
13
+ if (difference !== 0n)
14
+ return difference > 0n ? 1 : -1;
15
+ }
16
+ return 0;
17
+ }
18
+ export function effectiveWindowsPackageVersion(id, requirement, installed) {
19
+ if (installed === undefined)
20
+ return requirement.version;
21
+ if (requirement.exact) {
22
+ if (installed !== requirement.version) {
23
+ throw new Error(`Exact version mismatch for ${id}: installed ${installed}, required ${requirement.version}. Resolve it explicitly; Winix will not silently replace or downgrade it.`);
24
+ }
25
+ return requirement.version;
26
+ }
27
+ return compareWindowsPackageVersions(installed, requirement.version) >= 0 ? installed : requirement.version;
28
+ }
29
+ export async function confirmWindowsPackageLockUpdate(id, installed, locked) {
30
+ if (!process.stdin.isTTY || !process.stdout.isTTY)
31
+ return false;
32
+ const prompt = createInterface({ input: process.stdin, output: process.stdout });
33
+ const controller = new AbortController();
34
+ prompt.once("close", () => controller.abort());
35
+ prompt.once("SIGINT", () => controller.abort());
36
+ try {
37
+ const answer = await prompt.question(`${id}: installed ${installed}, locked ${locked}. Update winix-windows.lock to ${installed}? [y/N] `, { signal: controller.signal });
38
+ return /^(y|yes)$/i.test(answer.trim());
39
+ }
40
+ finally {
41
+ prompt.close();
42
+ }
43
+ }
@@ -1,4 +1,5 @@
1
1
  import type { EvaluatedHost } from "../evaluator/index.js";
2
+ import { type WindowsPackageRequirement } from "./windows-package-versions.js";
2
3
  export type ManagedResource = {
3
4
  kind: "startup";
4
5
  name: string;
@@ -18,6 +19,7 @@ interface Entry {
18
19
  owned: boolean;
19
20
  snapshot: ResourceSnapshot;
20
21
  definition: string;
22
+ packageExact?: boolean;
21
23
  }
22
24
  interface AppliedState {
23
25
  version: 1;
@@ -28,6 +30,7 @@ export interface WindowsStateAdapter {
28
30
  remove(resource: ManagedResource, snapshot: NonNullable<ResourceSnapshot>): Promise<void>;
29
31
  }
30
32
  export interface ReconcileOptions {
33
+ packageRequirements?: Record<string, WindowsPackageRequirement>;
31
34
  statePath: string;
32
35
  owner: string;
33
36
  desired: ManagedResource[];
@@ -6,6 +6,7 @@ import { dirname, join } from "node:path";
6
6
  import { createInterface } from "node:readline/promises";
7
7
  import { powerShellLiteral } from "../helpers/utils.js";
8
8
  import { runCommand } from "./run.js";
9
+ import { compareWindowsPackageVersions, effectiveWindowsPackageVersion } from "./windows-package-versions.js";
9
10
  export function resourceKey(resource) {
10
11
  return JSON.stringify(resource.kind === "startup"
11
12
  ? ["startup", resource.name.toLowerCase()]
@@ -99,6 +100,7 @@ export function parseAppliedState(text) {
99
100
  key !== resourceKey(entry.resource) || typeof entry.owned !== "boolean" ||
100
101
  typeof entry.definition !== "string" ||
101
102
  !Array.isArray(entry.owners) || !entry.owners.every((owner) => typeof owner === "string") ||
103
+ (entry.packageExact !== undefined && typeof entry.packageExact !== "boolean") ||
102
104
  !(entry.snapshot === null || (entry.resource.kind === "startup"
103
105
  ? typeof entry.snapshot?.fingerprint === "string" && /^[a-f0-9]{64}$/.test(entry.snapshot.fingerprint)
104
106
  : typeof entry.snapshot?.version === "string" && entry.snapshot.version.length > 0))) {
@@ -207,14 +209,31 @@ export async function reconcileWindows(opts) {
207
209
  const current = await opts.adapter.inspect(resource);
208
210
  before.set(key, current);
209
211
  const entry = state.entries[key];
212
+ const requirement = resource.kind === "package" ? opts.packageRequirements?.[key] : undefined;
213
+ if (requirement && current && "version" in current) {
214
+ effectiveWindowsPackageVersion(resource.kind === "package" ? resource.id : key, requirement, current.version);
215
+ }
210
216
  if (typeof opts.definitions[key] !== "string")
211
217
  throw new Error(`Missing resource definition: ${key}`);
212
218
  if (entry?.owners.some((owner) => owner !== opts.owner) && entry.definition !== opts.definitions[key]) {
213
219
  throw new Error(`Conflicting declaration for resource required by another workspace/host: ${key}. ` +
214
220
  "Run winix state owners; after a checkout move or host rename, explicitly preview state migrate-owner <id> --host <name> --dry.");
215
221
  }
222
+ if (requirement && entry?.owners.some((owner) => owner !== opts.owner)) {
223
+ if (entry.packageExact !== undefined && entry.packageExact !== requirement.exact) {
224
+ throw new Error(`Conflicting exact/minimum package policies across workspace/hosts: ${key}`);
225
+ }
226
+ if (!sameSnapshot(current, entry.snapshot) && entry.packageExact !== false) {
227
+ throw new Error(`Package changed while required by another exact or legacy workspace/host: ${key}`);
228
+ }
229
+ }
216
230
  if (entry?.owned && current !== null && !sameSnapshot(current, entry.snapshot)) {
217
- throw new Error(`Managed resource changed outside Winix: ${key}. Restore it or explicitly resolve ownership before activation.`);
231
+ const upgrade = requirement && !requirement.exact && "version" in current &&
232
+ entry.snapshot && "version" in entry.snapshot &&
233
+ compareWindowsPackageVersions(current.version, entry.snapshot.version) > 0;
234
+ if (!upgrade)
235
+ throw new Error(`Managed resource changed outside Winix: ${key}. Restore it or explicitly resolve ownership before activation.`);
236
+ report(`Keeping externally upgraded package: ${key} ${current.version}`);
218
237
  }
219
238
  }
220
239
  const removals = [];
@@ -253,8 +272,25 @@ export async function reconcileWindows(opts) {
253
272
  if (current === null)
254
273
  throw new Error(`Activation succeeded but managed resource is missing: ${key}`);
255
274
  const entry = state.entries[key];
275
+ const requirement = resource.kind === "package" ? opts.packageRequirements?.[key] : undefined;
276
+ if (requirement) {
277
+ if (!("version" in current) || (requirement.exact
278
+ ? current.version !== requirement.version
279
+ : compareWindowsPackageVersions(current.version, requirement.version) < 0)) {
280
+ throw new Error(`Activation did not satisfy package version requirement: ${key}`);
281
+ }
282
+ const initial = before.get(key);
283
+ if (!requirement.exact && initial && "version" in initial &&
284
+ compareWindowsPackageVersions(current.version, initial.version) < 0) {
285
+ throw new Error(`Activation unexpectedly downgraded package: ${key}`);
286
+ }
287
+ }
256
288
  if (entry && entry.owners.some((owner) => owner !== opts.owner) && !sameSnapshot(current, entry.snapshot)) {
257
- throw new Error(`Activation changed a resource required by another workspace/host: ${key}`);
289
+ const upgrade = requirement && !requirement.exact && entry.packageExact === false && "version" in current &&
290
+ entry.snapshot && "version" in entry.snapshot &&
291
+ compareWindowsPackageVersions(current.version, entry.snapshot.version) > 0;
292
+ if (!upgrade)
293
+ throw new Error(`Activation changed a resource required by another workspace/host: ${key}`);
258
294
  }
259
295
  after.set(key, current);
260
296
  }
@@ -272,6 +308,9 @@ export async function reconcileWindows(opts) {
272
308
  (resource.kind === "startup" && !sameSnapshot(initial, current)),
273
309
  snapshot: current,
274
310
  definition: opts.definitions[key],
311
+ ...(resource.kind === "package" && opts.packageRequirements?.[key]
312
+ ? { packageExact: opts.packageRequirements[key].exact }
313
+ : previous?.packageExact === undefined ? {} : { packageExact: previous.packageExact }),
275
314
  };
276
315
  }
277
316
  await saveState(opts.statePath, state);
package/dist/skill.js CHANGED
@@ -388,8 +388,19 @@ are automatically uninstalled after successful activation; always preview
388
388
  with \`winix switch --dry\`. Other recorded owners prevent removal.
389
389
  Switches with package/startup removals prompt before activation; declining cancels
390
390
  the entire switch. Use \`--yes\`/\`-y\` for automation; noninteractive runs with
391
- removals require it. Dry runs and switches without removals do not prompt.
392
- External shortcut edits/package-version drift block reconciliation.
391
+ removals require it. Dry runs do not prompt.
392
+ Native Windows switch treats floating package locks as minimum versions/fresh
393
+ install targets: install missing, upgrade older, keep newer without downgrading.
394
+ Owned external upgrades retain ownership and refresh observed snapshots after
395
+ successful activation; pre-installed packages remain unowned. An inline version
396
+ is exact: existing mismatches fail explicitly before activation.
397
+ Unequal non-dotted-numeric versions cannot be ordered and require explicit
398
+ resolution. Interactive successful switches offer a separate [y/N] lock update
399
+ for newer floating packages. Declining continues; dry/noninteractive runs do not
400
+ update observed-version pins, and --yes does not accept lock-update prompts.
401
+ Plain apply/direct DSC scripts keep lock-based generation.
402
+ External shortcut edits, package downgrades and exact-pin drift still block
403
+ reconciliation; externally modified removals remain protected.
393
404
  \`winix state forget startup <name>\` or
394
405
  \`winix state forget package <winget|msstore> <id>\` releases removal ownership
395
406
  without deleting the resource; --dry previews. Do not copy applied state to a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adrifer/winix",
3
- "version": "0.3.0-preview.4",
3
+ "version": "0.3.0-preview.5",
4
4
  "description": "TypeScript-first system configuration for NixOS, nix-darwin, and Home Manager",
5
5
  "license": "MIT",
6
6
  "author": "Adrian Fernandez",