<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<title>Writing by Jagrit Vats</title>
	<subtitle>Posts by Jagrit Vats, a backend engineer in Bangalore.</subtitle>
	<link href="https://jagritvats.com/feed.xml" rel="self" type="application/atom+xml" />
	<link href="https://jagritvats.com/writing/" rel="alternate" type="text/html" />
	<updated>2026-11-06T12:30:00Z</updated>
	<id>https://jagritvats.com/writing/</id>
	<author>
		<name>Jagrit Vats</name>
		<uri>https://jagritvats.com/</uri>
	</author>
	<icon>https://jagritvats.com/favicon.svg</icon>
	<entry>
		<title>Making TeamTakes multi-tenant while people were using it</title>
		<link href="https://jagritvats.com/writing/teamtakes-multi-tenant/" rel="alternate" type="text/html" />
		<id>https://jagritvats.com/writing/teamtakes-multi-tenant/</id>
		<published>2026-11-06T12:30:00Z</published>
		<updated>2026-11-06T12:30:00Z</updated>
		<summary>How I moved TeamTakes from one team to many on a single Postgres database with row-level security. I wrote the leak test first and rehearsed on a copy of production, and it still broke once someone was in a second team.</summary>
		<content type="html">&lt;p&gt;TeamTakes is opinionated polls and play-money prediction games for engineering teams. It started out as an app for one team. There was no &lt;code&gt;team_id&lt;/code&gt; column anywhere, and every approved user could see everything.&lt;/p&gt;
&lt;p&gt;To make it multi-tenant I went with one Postgres database, a &lt;code&gt;team_id&lt;/code&gt; on every row, and row-level security (RLS) deciding who sees what. Each team also gets its own subdomain.&lt;/p&gt;
&lt;p&gt;The tricky part was that people were already using it. The production dump I rehearsed on had 16 profiles and 63 votes (a vote is what the app calls a poll). That&#39;s small, but it&#39;s real people&#39;s data, and I really didn&#39;t want any of it showing up on another team&#39;s screen.&lt;/p&gt;
&lt;h2&gt;The leak test came first&lt;/h2&gt;
&lt;p&gt;I wrote the leak test before I touched any policy. It seeds two teams, A and B, each with an owner and a member, and one row of each kind of team data: a vote, a comment, a notification, a DM thread. Then it signs in as team A&#39;s owner through the normal RLS-constrained client and tries to get at team B&#39;s data.&lt;/p&gt;
&lt;p&gt;The commit that added it says &amp;quot;Expected RED until Phase 2&amp;quot;. At that point any approved user could read everything, so each failing probe was an item on the to-do list.&lt;/p&gt;
&lt;p&gt;Each surface it probes also has a positive control. Team A not seeing team B&#39;s votes only means something if team A can still see its own.&lt;/p&gt;
&lt;!-- adapted from tests/leak/harness.ts in team-takes --&gt;
&lt;pre class=&quot;language-ts&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-ts&quot;&gt;&lt;span class=&quot;token function&quot;&gt;expectVisible&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;name&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; count&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;count &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;results&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; status&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;pass&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;results&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;push&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
      name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      status&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;error&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
      detail&lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token string&quot;&gt;&quot;control failed: user could not see their OWN team&#39;s row (query/seed bug, not isolation)&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Otherwise a broken query that returns nothing would look the same as perfect isolation. The suite has about sixty checks now, and some of them are RPCs that have to refuse to work across teams, like starting a DM with someone who isn&#39;t in your team.&lt;/p&gt;
&lt;h2&gt;Isolation as restrictive policies&lt;/h2&gt;
&lt;p&gt;The app already had around 150 permissive RLS policies for the in-team rules: who can vote, who can comment, admin stuff. Rewriting all of them would have been an easy way to break something without noticing.&lt;/p&gt;
&lt;p&gt;So I put isolation in as a separate layer. Postgres has &lt;code&gt;AS RESTRICTIVE&lt;/code&gt; policies, which get ANDed with the permissive ones, so a row has to pass the existing rules &lt;em&gt;and&lt;/em&gt; the team check. There&#39;s one restrictive policy each for select, update and delete on 42 tables, and I left the existing logic alone. Inserts are handled by a trigger that stamps the right team on new rows.&lt;/p&gt;
&lt;p&gt;A row with no &lt;code&gt;team_id&lt;/code&gt; is invisible. &lt;code&gt;is_team_member(NULL)&lt;/code&gt; is false, so if a row never got stamped, nobody sees it. I wanted it to fail closed like that.&lt;/p&gt;
&lt;p&gt;The migration also checks its own work. If any table that should have the restrictive policy doesn&#39;t have it, the migration raises an error instead of finishing. The catch is that this only proves anything on the day the migration runs, which bit me later.&lt;/p&gt;
&lt;p&gt;Along the way I hit a latent &amp;quot;infinite recursion detected in policy&amp;quot; bug. The policy on &lt;code&gt;team_members&lt;/code&gt; read from &lt;code&gt;team_members&lt;/code&gt;, which triggered the same policy again. I fixed it by routing every membership check through a &lt;code&gt;SECURITY DEFINER&lt;/code&gt; function, and that function is now the one predicate everything else uses.&lt;/p&gt;
&lt;h2&gt;Rehearsing on a copy of production&lt;/h2&gt;
&lt;p&gt;I didn&#39;t want the first run of 18 migrations against real data to be the production run. So I wrote scripts to back up production, restore the dump into a scratch database, apply every migration in order and compare row counts. The rehearsal ran on a local Postgres 17 container.&lt;/p&gt;
&lt;p&gt;It found three bugs, none of which would have shown up on an empty database:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;One of my migrations had the same version number as a migration already applied on production. The runner would have skipped mine without any error.&lt;/li&gt;
&lt;li&gt;Backfilling &lt;code&gt;team_id&lt;/code&gt; on vote responses tripped an existing trigger that refuses changes once a poll is closed or about to close. There are no closed polls in an empty database, but production has them.&lt;/li&gt;
&lt;li&gt;I used &lt;code&gt;max(team_id)&lt;/code&gt; on a uuid column, and Postgres doesn&#39;t have &lt;code&gt;max(uuid)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A second rehearsal on a fresh dump went through clean. After that I checked the actual isolation: I created a team B with one vote and queried as a user who was only in team A, through the &lt;code&gt;authenticated&lt;/code&gt; role and not as the superuser, which skips RLS. That user saw 0 team-B votes and 63 from their own team.&lt;/p&gt;
&lt;h2&gt;What broke after the cutover&lt;/h2&gt;
&lt;p&gt;Then the migrations went to production, and things broke as soon as a second team was created.&lt;/p&gt;
&lt;p&gt;Creating a team makes its creator an active owner straight away, so the creator was now in two teams. On the original team, their daily bits claims started returning 400. Reactions, comments and votes broke too. The fix commit says: &amp;quot;The single-team dry run never hit it.&amp;quot;&lt;/p&gt;
&lt;p&gt;When a row is inserted without a team, a trigger works out which team it belongs to. With one membership that&#39;s easy. With two, the trigger couldn&#39;t tell which team was meant, so it failed closed, which is what I&#39;d designed it to do. The economy functions had the same rule and failed the same way.&lt;/p&gt;
&lt;p&gt;So the active team had to come along with the request. The frontend works out the team from the subdomain and sends it as an &lt;code&gt;x-team-id&lt;/code&gt; header. The database reads it, but only trusts it if you&#39;re an active member of that team, so a forged header just gets ignored.&lt;/p&gt;
&lt;p&gt;The leak test and the rehearsal both only used people who were in exactly one team. In a multi-tenant app someone in two teams is normal, and I hadn&#39;t tested with one even once.&lt;/p&gt;
&lt;h2&gt;Two leaks from the active-team header&lt;/h2&gt;
&lt;p&gt;For a user in several teams, the header also narrows reads to the active team, through one more restrictive policy. If there&#39;s no header, that policy does nothing, so single-team users and server-side code keep working.&lt;/p&gt;
&lt;p&gt;I added the policy with a one-time sweep that finds every table with the isolation policy and gives it the active-team one too.&lt;/p&gt;
&lt;!-- adapted from supabase/migrations/20260628140000000_active_team_request_context.sql --&gt;
&lt;pre class=&quot;language-sql&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-sql&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;FOR&lt;/span&gt; r &lt;span class=&quot;token operator&quot;&gt;IN&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;DISTINCT&lt;/span&gt; tablename
    &lt;span class=&quot;token keyword&quot;&gt;FROM&lt;/span&gt; pg_policies
   &lt;span class=&quot;token keyword&quot;&gt;WHERE&lt;/span&gt; schemaname &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;public&#39;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;AND&lt;/span&gt; policyname &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;tenant_isolation_select&#39;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;LOOP&lt;/span&gt;
  &lt;span class=&quot;token keyword&quot;&gt;EXECUTE&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;CREATE POLICY %I ON public.%I AS RESTRICTIVE FOR SELECT &#39;&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;USING (public._req_team_id() IS NULL OR team_id = public._req_team_id())&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&#39;tenant_active_team_select&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; r&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;tablename
  &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;END&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;LOOP&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The problem is it only ran once. Tables created after it, like the ones behind Daily Docket, got the isolation policy but not the active-team one. So a person in two teams could see the other team&#39;s dockets in this team&#39;s feed, and the same thing happened on every tenant table added after the sweep.&lt;/p&gt;
&lt;p&gt;This never showed data to someone outside the team. Membership isolation still held, so what leaked was always the union of that person&#39;s own teams. It was still wrong though, and the same drift on a different policy could have been a real leak. The fix re-ran the sweep. I also added a check to the tenancy verifier, a script that reads the migration files without needing a database. It fails if any migration that creates an isolation policy runs after the last sweep. Nothing in the deploy pipeline runs that script yet, so it only runs when I run it or as part of the rehearsal.&lt;/p&gt;
&lt;p&gt;The second leak was harder to spot. On someone&#39;s first visit to a newly joined team&#39;s subdomain, a cached profile let the app render before the active team was known. The first queries went out with no header, the narrowing policy did nothing, and the union of all their teams got cached under this team&#39;s keys. After a refresh everything looked right, so it was easy to miss. The fix makes the app wait for the team on a subdomain, skips prefetching until the team is known, and drops the cache if the team changes under it.&lt;/p&gt;
&lt;p&gt;Both of these trace back to treating a missing header as a no-op. I did that so everything else kept working during the migration, but it means that&#39;s the one place in the design that doesn&#39;t fail closed.&lt;/p&gt;
&lt;h2&gt;The app_settings rename&lt;/h2&gt;
&lt;p&gt;Later I split the single-row &lt;code&gt;app_settings&lt;/code&gt; table into one row per team. I renamed the old table and put a view in its place under the old name. Chat and DM sends then started failing for multi-team users with &amp;quot;more than one row returned by a subquery used as an expression&amp;quot;.&lt;/p&gt;
&lt;p&gt;RLS policies reference tables by OID, so when I renamed the table, three policies followed it, and that table now had a row per team. Function bodies are stored as text, so they kept pointing at the name, and the name was now the view. The fix scoped each lookup to the row&#39;s own team, and a smoke test checks that no policy still uses the old pattern.&lt;/p&gt;
&lt;h2&gt;Looking back&lt;/h2&gt;
&lt;p&gt;I&#39;d have put a user who&#39;s in two teams into the leak suite right at the start. Every bug after the cutover in this post needed one. I still haven&#39;t done it, actually. The leak suite only seeds single-team users, and the multi-team cases are separate SQL smoke tests that came in with the fixes.&lt;/p&gt;
&lt;p&gt;The sweep check has the same problem. It exists, but nothing in the deploy pipeline runs it, so it only catches drift when I run it by hand. That should run on every change.&lt;/p&gt;
&lt;p&gt;The missing header is the other one. I made unstamped rows fail closed on purpose, but let a missing header do nothing because it was convenient, and both leaks came out of that.&lt;/p&gt;
&lt;p&gt;Most of the code in this post was written with Claude Code.&lt;/p&gt;
</content>
	</entry>
	<entry>
		<title>Let the model suggest, let code decide</title>
		<link href="https://jagritvats.com/writing/scenepilot-model-suggests-code-decides/" rel="alternate" type="text/html" />
		<id>https://jagritvats.com/writing/scenepilot-model-suggests-code-decides/</id>
		<published>2026-11-06T00:00:00Z</published>
		<updated>2026-11-06T00:00:00Z</updated>
		<summary>In ScenePilot, Gemini proposes new shoot schedules and plain Python checks whether they&#39;re allowed. The Python had its own bugs.</summary>
		<content type="html">&lt;p&gt;ScenePilot is an AI assistant director I built for Google Cloud&#39;s Agentic Cinema hackathon, in the Parallel partner track. When something goes wrong on a shoot day, say rain is forecast for the afternoon of day 4, it helps the producer work out a new schedule.&lt;/p&gt;
&lt;p&gt;The easy way to build this is to give the model the schedule and the forecast and ask for a new plan. You&#39;d get one, and it would probably look fine. But nothing in that setup checks whether the plan runs a rooftop scene past a noise curfew, or starts a daylight exterior after sunset. Those aren&#39;t small mistakes on a film set, since a permit window or a curfew is enforced by someone other than you.&lt;/p&gt;
&lt;p&gt;So the whole project runs on one rule: the model can suggest whatever it wants, but it never decides whether a schedule is allowed.&lt;/p&gt;
&lt;h2&gt;Who does what&lt;/h2&gt;
&lt;p&gt;When a disruption comes in, the rescue runs as a short sequence of steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Check the disruption against live web sources.&lt;/li&gt;
&lt;li&gt;Work out which scenes it affects. If nothing is affected, stop there.&lt;/li&gt;
&lt;li&gt;Generate candidate schedules in code, and check each one.&lt;/li&gt;
&lt;li&gt;Let Gemini propose up to two more, checked the same way.&lt;/li&gt;
&lt;li&gt;Rank them, and let Gemini explain the options.&lt;/li&gt;
&lt;li&gt;Stop and wait for the producer.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The candidates in step 3 are plain enumeration: orderings of the day&#39;s scenes, with up to two affected scenes deferred and cover scenes pulled in. Any set bigger than six scenes is skipped, so a set has at most 720 orderings, which is cheap to brute-force. Every one goes through the checker: cast and location availability, the disruption window, usable daylight, golden hour, permit windows and curfews, meal breaks and turnaround before the next call. Each gets a score and a list of violations.&lt;/p&gt;
&lt;p&gt;Gemini&#39;s proposals go through the exact same function.&lt;/p&gt;
&lt;!-- adapted from services/agent/scenepilot/services/recovery.py --&gt;
&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;add_proposed_option&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;project&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; day&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; disruption&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; existing&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; order_numbers&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; deferred_numbers&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; title&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; strategy&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;Validate a Gemini-proposed ordering through the same deterministic pipeline.&quot;&quot;&quot;&lt;/span&gt;
    baseline &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;sorted&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;day&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;items&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; key&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;lambda&lt;/span&gt; i&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt; to_minutes&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;start&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        order &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;project&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;scene_by_number&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; n &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; order_numbers&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
        deferred &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;project&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;scene_by_number&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; n &lt;span class=&quot;token keyword&quot;&gt;in&lt;/span&gt; deferred_numbers&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;except&lt;/span&gt; KeyError&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;not&lt;/span&gt; order&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;
    opt &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; build_option&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;project&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; day&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; disruption&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; baseline&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; order&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; deferred&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; origin&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;gemini&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A proposal that names a scene which doesn&#39;t exist gets dropped. And if Gemini comes up with an option that&#39;s already on the list, the existing one is marked as Gemini&#39;s too.&lt;/p&gt;
&lt;p&gt;The screen shows at most three feasible and two rejected options, but Gemini&#39;s proposals are always shown, pass or fail. I wanted the producer to see when the model got overruled, which only works if its rejected options stay on screen.&lt;/p&gt;
&lt;p&gt;The one place the model does get the last word is the explanation, where it writes up the trade-offs in plain English. If that call fails, a fallback rationale gets built from the checker&#39;s own scores, so the producer still sees a reason. After that the run ends at &amp;quot;awaiting approval&amp;quot;, and the schedule stays as it was until a person says yes.&lt;/p&gt;
&lt;h2&gt;Facts from the web get graded&lt;/h2&gt;
&lt;p&gt;This part came from the track. The Parallel track was built around their web research APIs, so ScenePilot looks up things like noise curfews and permit authorities for each location. The catch with web facts is that a curfew found on some random page shouldn&#39;t be able to reject a schedule by itself.&lt;/p&gt;
&lt;p&gt;So every fact gets graded by how well it&#39;s backed:&lt;/p&gt;
&lt;!-- adapted from services/agent/scenepilot/services/dossier.py --&gt;
&lt;pre class=&quot;language-python&quot; tabindex=&quot;0&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;_binding&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;basis&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token triple-quoted-string string&quot;&gt;&quot;&quot;&quot;The confidence gate. No basis at all is treated as unverified, never as fact.&quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; basis &lt;span class=&quot;token keyword&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; FactBinding&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ADVISORY&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;
    confidence &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;basis&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;confidence &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;lower&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; confidence &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;high&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;FactBinding&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;HARD &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; basis&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;citations &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; FactBinding&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;SOFT&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; confidence
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; confidence &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;medium&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; FactBinding&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;SOFT&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; confidence
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; FactBinding&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ADVISORY&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; confidence &lt;span class=&quot;token keyword&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;None&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A fact can only be HARD if it&#39;s high confidence and has a citation. Anything below that is shown for someone to check, but it never reaches the scheduler.&lt;/p&gt;
&lt;p&gt;Even a high-confidence fact has to be in a shape the checker can test, and there are only two: a ban on working at a location during a time window, like a noise curfew, or a ban on an activity there, like drones or pyrotechnics. Anything else gets downgraded to SOFT, since I didn&#39;t want to pretend I could parse prose into rules. And even a HARD fact does nothing until the producer accepts it.&lt;/p&gt;
&lt;h2&gt;The model overwrote the record&lt;/h2&gt;
&lt;p&gt;An earlier bug had the same problem outside scheduling. ScenePilot uses Gemini to break a scene down into duration, interior or exterior, and time of day. For scenes already on the schedule, that breakdown was overwriting what the production had entered.&lt;/p&gt;
&lt;p&gt;So a scene planned at one length could quietly become another, just because the model estimated differently. Now production data is the source of truth for anything already scheduled. Gemini&#39;s estimate is still kept in its own field, and the UI shows it when the two disagree.&lt;/p&gt;
&lt;h2&gt;The checker had bugs too&lt;/h2&gt;
&lt;p&gt;None of this helps much if the checker is wrong, and in places it was. I&#39;d spent all this effort keeping the model away from decisions, and the code making the decisions had bugs of its own.&lt;/p&gt;
&lt;p&gt;The worst one came out of a pre-submission audit in the last week before the deadline. A curfew like 22:00 to 06:00 wraps past midnight, and the checker split it into pieces that left a gap between them. Shoot days are measured from their own midnight, so a night unit can legitimately run to 28:00, which is 4 am the next morning. Against the seed data with the curfew accepted:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A 150-minute scene from 22:00 to 24:30 scored 120 minutes in breach, which is right.&lt;/li&gt;
&lt;li&gt;From 23:30 to 26:00 it scored 30, also right.&lt;/li&gt;
&lt;li&gt;From 24:00 to 26:30 it scored zero, even though that&#39;s 2:30 am and right inside the ban.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Moving the scene an hour later was enough to get it past the curfew, and the curfew was the rule the whole demo was built around. The fix tiles the window at plus and minus 24 hours, so each banned period is one continuous interval.&lt;/p&gt;
&lt;p&gt;The audit caught a second one too. Two tests checked that a 19:00 slot loses part of golden hour, but dusk shifts through the year. It was 19:02 on 8 September and would be 19:00 by the 10th, so those tests would have started failing on 10 September, the day after the deadline. That&#39;s during judging, on a test suite the README told judges to run. I confirmed it by pinning the clock to different dates.&lt;/p&gt;
&lt;p&gt;The last one was a state bug. Two buttons, each fine on its own, could put the same scene on two shoot days if a producer pressed them in a pretty natural order. Now both refuse, and the refusal says which day to undo first. I chose refusing over auto-fixing on purpose, because un-committing another day&#39;s schedule is a second decision and the producer should be the one making it.&lt;/p&gt;
&lt;h2&gt;Where the model actually helps&lt;/h2&gt;
&lt;p&gt;The model still does a lot of the work. It plans the research for a scene and grades the search results that come back. It can propose schedules the enumeration wouldn&#39;t produce, like deferring a scene the disruption didn&#39;t touch. It also writes the explanations, which a producer can actually read.&lt;/p&gt;
&lt;p&gt;What I didn&#39;t want was the model&#39;s confidence standing in for a check, since it sounds just as sure when it&#39;s wrong. The rule checker is boring, but when it rejects a plan you can see which rule and which source.&lt;/p&gt;
&lt;p&gt;It also helps when model calls fail, which they do. A Gemini call that makes more than six tool calls gets cut off and retried without tools. A billing error from Gemini used to be retried with a sleep on every call; now it opens a breaker, and calls fall back to recorded answers labelled as replayed. Since the checker doesn&#39;t depend on the model, a rescue still comes back with checked options when Gemini is down. It won&#39;t have Gemini&#39;s proposals, and it uses the fallback rationale.&lt;/p&gt;
&lt;h2&gt;If I did it again&lt;/h2&gt;
&lt;p&gt;The curfew gap and the golden-hour tests were both time bugs, one across midnight and one across dates. Next time I&#39;d test both of those from the first day I write any time-window logic.&lt;/p&gt;
&lt;p&gt;The boundary between the model and the checker is also weaker than it looks. Right now it holds only because &lt;code&gt;build_option&lt;/code&gt; is the only place an option gets made. Nothing in the types stops someone adding a second path, so it&#39;s a convention I have to keep in mind. I&#39;d rather the types enforced it.&lt;/p&gt;
&lt;p&gt;The code is public at &lt;a href=&quot;https://github.com/jagritvats/scenepilot-public&quot;&gt;github.com/jagritvats/scenepilot-public&lt;/a&gt;, with more than 600 tests.&lt;/p&gt;
</content>
	</entry>
</feed>
