feat(deploy): warn-only script-compile-cost advisory on deploy

This commit is contained in:
Joseph Doherty
2026-06-07 15:35:53 -04:00
parent b73ce75402
commit cfbf0b2a17
4 changed files with 142 additions and 2 deletions
@@ -189,6 +189,120 @@ public sealed class AdminOperationsActorTests : ControlPlaneActorTestBase
verify.Deployments.Count().ShouldBe(0);
}
/// <summary>Verifies the warn-only compile-cost advisory: seeding N distinct non-passthrough
/// (genuinely-compiled) Script rows yields an <see cref="StartDeploymentOutcome.Accepted"/>
/// deploy whose <see cref="StartDeploymentResult.Message"/> carries a compile-cost advisory.
/// The guardrail NEVER rejects — it only surfaces the estimated RSS pressure to the operator.</summary>
[Fact]
public void StartDeployment_warns_when_many_scripts_will_compile()
{
const int n = 10;
var dbFactory = NewInMemoryDbFactory();
using (var db = dbFactory.CreateDbContext())
{
for (var i = 0; i < n; i++)
{
db.Scripts.Add(new Configuration.Entities.Script
{
ScriptId = $"s-{i}",
Name = $"script-{i}",
SourceCode = $"return (int)ctx.GetTag(\"a\").Value + {i};", // distinct, non-passthrough
SourceHash = $"hash-{i}",
});
}
db.SaveChanges();
}
var coordinator = CreateTestProbe("coord");
var actor = Sys.ActorOf(AdminOperationsActor.Props(dbFactory, coordinator.Ref, Enumerable.Empty<IDriverProbe>()));
actor.Tell(new StartDeployment("joe", CorrelationId.NewId()));
// Still dispatches — the advisory is non-blocking.
coordinator.ExpectMsg<DispatchDeployment>(TimeSpan.FromSeconds(3));
var reply = ExpectMsg<StartDeploymentResult>(TimeSpan.FromSeconds(3));
reply.Outcome.ShouldBe(StartDeploymentOutcome.Accepted);
reply.Message.ShouldNotBeNull();
reply.Message.ShouldContain("will compile");
reply.Message.ShouldContain($"{n} script"); // the distinct compiled count
using var verify = dbFactory.CreateDbContext();
verify.Deployments.Count().ShouldBe(1);
}
/// <summary>Verifies that passthrough "mirror" scripts (<c>return ctx.GetTag("X").Value;</c>)
/// compile to ~nothing and therefore do NOT count toward the compile-cost advisory: a deploy
/// of only passthrough scripts is <see cref="StartDeploymentOutcome.Accepted"/> with no advisory.</summary>
[Fact]
public void StartDeployment_passthrough_scripts_do_not_count()
{
var dbFactory = NewInMemoryDbFactory();
using (var db = dbFactory.CreateDbContext())
{
for (var i = 0; i < 5; i++)
{
db.Scripts.Add(new Configuration.Entities.Script
{
ScriptId = $"mirror-{i}",
Name = $"mirror-{i}",
SourceCode = $"return ctx.GetTag(\"tag-{i}\").Value;", // passthrough mirror — costs ~nothing
SourceHash = $"mhash-{i}",
});
}
db.SaveChanges();
}
var coordinator = CreateTestProbe("coord");
var actor = Sys.ActorOf(AdminOperationsActor.Props(dbFactory, coordinator.Ref, Enumerable.Empty<IDriverProbe>()));
actor.Tell(new StartDeployment("joe", CorrelationId.NewId()));
coordinator.ExpectMsg<DispatchDeployment>(TimeSpan.FromSeconds(3));
var reply = ExpectMsg<StartDeploymentResult>(TimeSpan.FromSeconds(3));
reply.Outcome.ShouldBe(StartDeploymentOutcome.Accepted);
// No compiled scripts → no advisory emitted.
(reply.Message is null || !reply.Message.Contains("will compile")).ShouldBeTrue();
}
/// <summary>Verifies the advisory counts DISTINCT non-passthrough sources: many rows sharing one
/// identical source collapse to a single compiled unit (the compile cache keys on source), so the
/// advisory reports 1 — not the row count.</summary>
[Fact]
public void StartDeployment_duplicate_sources_collapse_to_one_compiled_unit()
{
const string identical = "return (int)ctx.GetTag(\"a\").Value + 1;";
var dbFactory = NewInMemoryDbFactory();
using (var db = dbFactory.CreateDbContext())
{
for (var i = 0; i < 7; i++)
{
db.Scripts.Add(new Configuration.Entities.Script
{
ScriptId = $"dup-{i}",
Name = $"dup-{i}",
SourceCode = identical, // same source across all rows
SourceHash = "dup-hash",
});
}
db.SaveChanges();
}
var coordinator = CreateTestProbe("coord");
var actor = Sys.ActorOf(AdminOperationsActor.Props(dbFactory, coordinator.Ref, Enumerable.Empty<IDriverProbe>()));
actor.Tell(new StartDeployment("joe", CorrelationId.NewId()));
coordinator.ExpectMsg<DispatchDeployment>(TimeSpan.FromSeconds(3));
var reply = ExpectMsg<StartDeploymentResult>(TimeSpan.FromSeconds(3));
reply.Outcome.ShouldBe(StartDeploymentOutcome.Accepted);
reply.Message.ShouldNotBeNull();
reply.Message.ShouldContain("will compile");
reply.Message.ShouldContain("1 script(s) will compile");
}
/// <summary>Verifies that starting a deployment is refused when another is in flight.</summary>
[Fact]
public void StartDeployment_refuses_when_another_is_in_flight()