-- Guarded disposable database. Every synthetic user and approval is rolled back. SET NOCOUNT ON; IF DB_NAME()<>N'PlotDirector_Phase2_20261103_2305' OR NOT EXISTS(SELECT 1 FROM sys.extended_properties WHERE class=0 AND name=N'PlotDirector.Phase2.SyntheticOnly' AND CONVERT(nvarchar(20),value)=N'true') THROW 51000,'Guarded synthetic database required',1; BEGIN TRANSACTION; DECLARE @User int,@Other int,@Stamp uniqueidentifier=NEWID(),@ID uniqueidentifier=NEWID(),@Connection uniqueidentifier=NEWID(),@Restart uniqueidentifier=NEWID(),@Rotation uniqueidentifier=NEWID(); DECLARE @Now datetimeoffset='2026-11-06T12:00:00+00:00',@A varbinary(32)=HASHBYTES('SHA2_256',REPLICATE('A',64)),@B varbinary(32)=HASHBYTES('SHA2_256',REPLICATE('B',64)),@C varbinary(32)=HASHBYTES('SHA2_256',REPLICATE('C',64)); INSERT dbo.AppUser(Email,DisplayName,PasswordHash,SecurityStamp) VALUES(CONVERT(nvarchar(36),NEWID())+N'@native-trust.invalid',N'Installation fixture',N'not-a-credential',@Stamp);SET @User=SCOPE_IDENTITY(); INSERT dbo.AppUser(Email,DisplayName,PasswordHash) VALUES(CONVERT(nvarchar(36),NEWID())+N'@native-trust.invalid',N'Other installation fixture',N'not-a-credential');SET @Other=SCOPE_IDENTITY(); CREATE TABLE #Rows(InstallationID uniqueidentifier,UserID int,SecurityStamp uniqueidentifier,DeviceName nvarchar(80),RetainImport bit,ConnectionID uniqueidentifier,RefreshExpiresUtc datetimeoffset,CreatedUtc datetimeoffset,LastRenewedUtc datetimeoffset,LastSeenUtc datetimeoffset,DisconnectedUtc datetimeoffset,RevokedUtc datetimeoffset,AccountName nvarchar(512),PreviousConnectionID uniqueidentifier); INSERT #Rows EXEC dbo.NativeCompanionInstallation @Action='create',@InstallationID=@ID,@UserID=@User,@Stamp=@Stamp,@DeviceName=N'Synthetic Windows',@RetainImport=1,@RefreshHash=@A,@ConnectionID=@Connection,@Now=@Now; IF (SELECT COUNT(*) FROM #Rows)<>1 OR NOT EXISTS(SELECT 1 FROM dbo.NativeCompanionInstallations WHERE InstallationID=@ID AND RefreshHash=@A) THROW 51000,'Approval persistence failed',1; CREATE TABLE #Valid(ID uniqueidentifier); INSERT #Valid EXEC dbo.NativeCompanionInstallation @Action='validate',@InstallationID=@ID,@UserID=@User,@Stamp=@Stamp,@ConnectionID=@Connection,@Now=@Now; IF (SELECT COUNT(*) FROM #Valid)<>1 THROW 51000,'Valid access rejected',1; TRUNCATE TABLE #Rows; INSERT #Rows EXEC dbo.NativeCompanionInstallation @Action='rotate',@InstallationID=@ID,@RefreshHash=@A,@NextHash=@B,@RotationID=@Rotation,@ConnectionID=@Restart,@Now=@Now; IF NOT EXISTS(SELECT 1 FROM #Rows WHERE ConnectionID=@Restart AND PreviousConnectionID=@Connection AND RetainImport=1 AND UserID=@User) THROW 51000,'Rotation changed account/consent or lost runtime identity',1; TRUNCATE TABLE #Valid; INSERT #Valid EXEC dbo.NativeCompanionInstallation @Action='validate',@InstallationID=@ID,@UserID=@User,@Stamp=@Stamp,@ConnectionID=@Connection,@Now=@Now; IF EXISTS(SELECT 1 FROM #Valid) THROW 51000,'Old process token still valid',1; TRUNCATE TABLE #Rows; INSERT #Rows EXEC dbo.NativeCompanionInstallation @Action='rotate',@InstallationID=@ID,@RefreshHash=@A,@NextHash=@B,@RotationID=@Rotation,@ConnectionID=@Restart,@Now=@Now; IF (SELECT COUNT(*) FROM #Rows)<>1 THROW 51000,'Exact lost-response retry failed',1; TRUNCATE TABLE #Rows; DECLARE @OtherRotation uniqueidentifier=NEWID(); INSERT #Rows EXEC dbo.NativeCompanionInstallation @Action='rotate',@InstallationID=@ID,@RefreshHash=@A,@NextHash=@C,@RotationID=@OtherRotation,@ConnectionID=@Restart,@Now=@Now; IF EXISTS(SELECT 1 FROM #Rows) THROW 51000,'Old secret started another rotation',1; INSERT #Rows EXEC dbo.NativeCompanionInstallation @Action='revoke-user',@InstallationID=@ID,@UserID=@Other,@Now=@Now; IF EXISTS(SELECT 1 FROM #Rows) OR EXISTS(SELECT 1 FROM dbo.NativeCompanionInstallations WHERE InstallationID=@ID AND RevokedUtc IS NOT NULL) THROW 51000,'Cross-account revocation succeeded',1; EXEC dbo.NativeCompanionInstallation @Action='disconnect',@InstallationID=@ID,@UserID=@User,@ConnectionID=@Restart,@Now=@Now; IF NOT EXISTS(SELECT 1 FROM dbo.NativeCompanionInstallations WHERE InstallationID=@ID AND ConnectionID=@Restart AND DisconnectedUtc IS NOT NULL AND RevokedUtc IS NULL) THROW 51000,'Exit discarded trust or failed to record disconnection',1; INSERT #Rows EXEC dbo.NativeCompanionInstallation @Action='rotate',@InstallationID=@ID,@RefreshHash=@B,@NextHash=@C,@RotationID=@OtherRotation,@ConnectionID=@Connection,@Now=@Now; IF NOT EXISTS(SELECT 1 FROM #Rows WHERE ConnectionID=@Connection) THROW 51000,'Restart after exit requires reapproval',1; DECLARE @InvalidStamp uniqueidentifier=NEWID();UPDATE dbo.AppUser SET SecurityStamp=@InvalidStamp WHERE UserID=@User; TRUNCATE TABLE #Valid;INSERT #Valid EXEC dbo.NativeCompanionInstallation @Action='validate',@InstallationID=@ID,@UserID=@User,@Stamp=@Stamp,@ConnectionID=@Connection,@Now=@Now; IF EXISTS(SELECT 1 FROM #Valid) THROW 51000,'Password change failed to invalidate access',1; TRUNCATE TABLE #Rows;INSERT #Rows EXEC dbo.NativeCompanionInstallation @Action='rotate',@InstallationID=@ID,@RefreshHash=@C,@NextHash=@A,@RotationID=@Rotation,@ConnectionID=@Connection,@Now=@Now; IF EXISTS(SELECT 1 FROM #Rows) THROW 51000,'Password change allowed renewal',1; UPDATE dbo.AppUser SET SecurityStamp=@Stamp WHERE UserID=@User; TRUNCATE TABLE #Rows;INSERT #Rows EXEC dbo.NativeCompanionInstallation @Action='revoke-all',@UserID=@User,@Now=@Now; TRUNCATE TABLE #Valid;INSERT #Valid EXEC dbo.NativeCompanionInstallation @Action='validate',@InstallationID=@ID,@UserID=@User,@Stamp=@Stamp,@ConnectionID=@Connection,@Now=@Now; IF EXISTS(SELECT 1 FROM #Valid) THROW 51000,'Logout did not invalidate access',1; TRUNCATE TABLE #Rows;INSERT #Rows EXEC dbo.NativeCompanionInstallation @Action='rotate',@InstallationID=@ID,@RefreshHash=@C,@NextHash=@A,@RotationID=@Rotation,@ConnectionID=@Connection,@Now=@Now; IF EXISTS(SELECT 1 FROM #Rows) THROW 51000,'Revoked approval renewed',1; DELETE dbo.AppUser WHERE UserID=@User; IF EXISTS(SELECT 1 FROM dbo.NativeCompanionInstallations WHERE InstallationID=@ID) THROW 51000,'Account deletion left device trust',1; ROLLBACK; PRINT 'PASS durable approval, hashed rotation, lost-response retry, process exit/restart, account isolation, password/logout revocation and account-deletion cascade; synthetic fixtures rolled back';