patchDepends :: (TS m, MonadError JobError m) => Job.Name -> DependencyPatch -> m ()
patchDepends name (DependencyPatch adding rming) = do
    js <- existing _AJob name
    alreadyWaiting <- maybe (throwError $ JobMustBe name ExpectWaiting) (pure . Job.toSet) $ js ^? Job.stage . Job._Waiting
    let deps  = js ^. Job.dependsOn . Job.dependenciesMap
        deps' = foldl' (flip delete) (addMap <> deps) rming
        js'   = js & Job.dependsOn .~ Job.Dependencies deps'
    newlyWaiting <- namesToWaitFor addDeps
    setAndPerhapsQueue js' . shouldBeWaiting $ (newlyWaiting `union` alreadyWaiting) \\ fromList rming
    when (deps' /= deps) . ES.setJobDependsOn name $ Job.Dependencies deps'
    mapM_ (ES.addRevDep name) addDeps
    mapM_ (ES.removeRevDep name) rming
  where
    addDeps = adding ^. Job.dependenciesList
    addMap  = adding ^. Job.dependenciesMap
patchDepends :: (TS m, MonadError JobError m) => Job.Name -> DependencyPatch -> m ()
patchDepends name (DependencyPatch adding rming) = do
    js <- existing _AJob name
    alreadyWaiting <- maybe (throwError $ JobMustBe name ExpectWaiting) (pure . Job.toSet) $ js ^? Job.stage . Job._Waiting
    let deps  = js ^. Job.dependsOn . Job.dependenciesMap
        deps' = foldl' (flip delete) (addMap <> deps) rming
        js'   = js & Job.dependsOn .~ Job.Dependencies deps'
    newlyWaiting <- namesToWaitFor addDeps
    setAndPerhapsQueue js' . shouldBeWaiting $ (newlyWaiting `union` alreadyWaiting) \\ fromList rming
    when (deps' /= deps) . ES.setJobDependsOn name $ Job.Dependencies deps'
    mapM_ (ES.addRevDep name) addDeps
    mapM_ (ES.removeRevDep name) rming
  where
    addDeps = adding ^. Job.dependenciesList
    addMap  = adding ^. Job.dependenciesMap
patchDepends name (DependencyPatch adding rming) = do
    js <- existing _AJob name
    alreadyWaiting <- maybe (throwError $ JobMustBe name ExpectWaiting) (pure . Job.toSet) $ js ^? Job.stage . Job._Waiting
    let deps  = js ^. Job.dependsOn . Job.dependenciesMap
        deps' = foldl' (flip delete) (addMap <> deps) rming
        js'   = js & Job.dependsOn .~ Job.Dependencies deps'
    newlyWaiting <- namesToWaitFor addDeps
    setAndPerhapsQueue js' . shouldBeWaiting $ (newlyWaiting `union` alreadyWaiting) \\ fromList rming
    when (deps' /= deps) . ES.setJobDependsOn name $ Job.Dependencies deps'
    mapM_ (ES.addRevDep name) addDeps
    mapM_ (ES.removeRevDep name) rming
  where
    addDeps = adding ^. Job.dependenciesList
    addMap  = adding ^. Job.dependenciesMap