Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/cloudflare/src/Containers/ContainerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as Containers from "@distilled.cloud/cloudflare/containers";
import { Unowned, Unresolved, UnresolvedIdentity } from "alchemy/AdoptPolicy";
import { AlchemyContext } from "alchemy/AlchemyContext";
import { getStableContextDir } from "alchemy/Bundle/TempRoot";
import { deepEqual, isResolved } from "alchemy/Diff";
import { deepEqual, isResolved, stripEffects } from "alchemy/Diff";
import * as Provider from "alchemy/Provider";
import type { ScopedPlanStatusSession } from "alchemy/Report";
import { type ResourceBinding } from "alchemy/Resource";
Expand Down Expand Up @@ -1306,7 +1306,18 @@ export const LiveContainerProvider = () =>

return ContainerPlatform.Provider.of({
stables: ["accountId", "applicationId"],
diff: Effect.fn(function* ({ id, olds = {}, news = {}, output, newBindings, oldBindings }) {
diff: Effect.fn(function* ({
id,
olds = {},
news: desired = {},
output,
newBindings,
oldBindings,
}) {
// An Effect-native (`main`) Container carries its program as an
// Effect in `exports`, which `isResolved` never accepts. Strip it so
// a guest-code change still reaches the image hash comparison.
const news = stripEffects(desired);
if (!isResolved(news)) return;
yield* validateContainerConfiguration(news, output?.schedulingPolicy);
if (!isResolved(newBindings)) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,55 @@ describe.concurrent.each([
}).pipe(logLevel),
{ timeout },
);

// Guest code changes only the bundle, never a prop. The program in
// `exports` is an Effect, which `isResolved` never accepts, so the live
// diff used to return before its image hash check and plan a noop.
// A stale recorded hash stands in for a changed bundle.
test.skipIf(dev)(
"plans an update when the guest code's image hash changes",
Effect.gen(function* () {
yield* stack;
const effectful = yield* EffectfulStack;
const state = yield* yield* State;
const scope = { stack: effectful.name, stage };
const rows = yield* Effect.forEach(yield* state.list(scope), (fqn) =>
state.get({ ...scope, fqn }).pipe(Effect.map((row) => ({ fqn, row }))),
);
const container = rows.find(
({ row }) =>
row !== undefined &&
"resourceType" in row &&
row.resourceType === "Cloudflare.Container",
);
assert(container?.row && "attr" in container.row);
const { fqn, row } = container;
const { attr } = row;
assert(attr);
const planned = Plan.make(effectful).pipe(
Effect.map((plan) => plan.resources[fqn]?.action),
);

expect(yield* planned).toBe("noop");
yield* state.set({
...scope,
fqn,
value: { ...row, attr: { ...attr, hash: { ...attr.hash, image: "stale" } } },
});
const action = yield* planned.pipe(
Effect.ensuring(state.set({ ...scope, fqn, value: row }).pipe(Effect.orDie)),
);
expect(action).toBe("update");
}).pipe(
Effect.provide(
Layer.effectContext(EffectfulStack.pipe(Effect.map((stack) => stack.services))).pipe(
Layer.provideMerge(Layer.succeed(Stage, stage)),
),
),
logLevel,
),
{ timeout, retry: 0 },
);
});

/**
Expand Down
Loading