Repository navigation
feat(schedule): run AI jobs with the saver's key, fall back to the shared key - #966
Conversation
…ared key AI scheduled tasks used only the project owner's key for the model's CLI and never the platform's shared CodeBuddy key, so a project whose owner had not configured a key could not run any AI job, even when the member who set it up had a key or the platform had a shared one. Each AI job is now bound on save to whoever saves it (runAsUserId, never taken from the request). A run uses that user's own key for the model's CLI, else the platform's shared CodeBuddy key limited to the models the administrator allows; with neither the save is rejected with a reminder. A bound user who is gone or can no longer write to the project stops the job instead of falling back to another account. Jobs saved before the binding keep the project owner's keys and may also use the shared key. Split out of #958. Co-authored-by: yowucai <yowucai@tencent.com>
defaultProjectId read the signed-in user from globalThis.state, but app.js declares state with a top-level const, which never lands on globalThis. The user ID was always undefined, so the console fell back to the welcome project or the first listed one, which can be a project the user cannot write. A member then saw no AI scheduled tasks and saving one returned 403 from that unrelated project.
| if project, ok := h.repo.Project(job.ProjectID); ok { | ||
| if user, ok := try(project.OwnerID); ok { | ||
| return user, nil | ||
| if user, shared, ok := try(project.OwnerID); ok { |
There was a problem hiding this comment.
[P2] 保留已有任务可用的个人 Key
项目更换 Owner 后,旧的独立任务仍可能记录前一位 Owner 的 ownerUserId。原逻辑在现 Owner 缺 Key 时会尝试任务记录里的个人 Key。这里先给现 Owner 套用共享 Key,导致原本可跑的模型被共享白名单拒绝。临时 Go 用例已复现切换到共享 Key 后身份改变。建议先按原顺序查个人 Key,再回退到共享 Key。
There was a problem hiding this comment.
已在 452507b 修复:没有执行身份的旧任务,先按原顺序(项目 Owner、任务记录的 Owner)查个人 Key,都没有时才回退到平台共享 Key。新增 TestScheduleJobLegacyOwnerKeyBeforeSharedKey 覆盖换 Owner 后仍用原 Owner 个人 Key,以及都没有个人 Key 时回退共享 Key 两种情况;撤掉修复时这个用例会失败(身份变为现 Owner + 共享 Key)。
| return true | ||
| } | ||
| engine, _ := assistantModelEngine(job.AssistantModel) | ||
| _, shared, ok := scheduleKeyUser(h.repo, self, engine) |
There was a problem hiding this comment.
[P2] 允许没有可用 Key 的项目成员停用任务
「启用」复选框和任务编辑走同一个 PUT。项目成员有该任务的写权限,但没有个人或共享 Key 时,即使只把 enabled 改为 false 也会在这里收到 400。我用有 Key 的 Owner 创建已启用任务,再让无 Key 的成员停用,复现了这个结果。建议允许单独停用并保留现有配置和运行身份,编辑可执行配置或启用时再校验凭据。
There was a problem hiding this comment.
已在 452507b 修复:保存人没有可用 Key(或所选模型不在共享 Key 白名单内)时,如果这次保存是停用任务,且提示词、模型、项目、订阅版本和参数都没变,就保留原执行身份并允许保存。改了这些执行配置,或者重新启用,仍然需要保存人的 Key:任务停用后仍可「立即运行」,不能让没有 Key 的成员借别人的 Key 跑改过的提示词。有 Key 的成员保存时仍按原规则改绑到自己。新增 TestScheduleJobMemberWithoutKeyCanStopIt 覆盖:只停用通过并保留 Owner 身份;停用同时改提示词返回 400;无 Key 重新启用返回 400。
There was a problem hiding this comment.
[P1] 停用时保留原任务的执行输入
旧任务 Key 顺序和停用的原回归用例均已通过。scheduleRunConfigUnchanged 仍允许在保留原 RunAsUserID 时修改任务名称和通知设置。scheduledAssistantPrompt 会把任务名和 AI 通知指令拼进发给模型的消息。我用普通的任务改名复测,校验仍判定执行配置未变,但实际消息已经改变。停用的任务可以手动运行,届时依然使用原执行人的权限。建议无 Key 的停用路径只更新 enabled,其余字段沿用库中的值。
There was a problem hiding this comment.
说得对,已在 6a2cc82 修复:没有可用 Key 的成员停用任务时,服务端直接沿用库里的任务,只把 enabled 改成 false,请求里的名称、通知、调度等其余字段都忽略,执行身份保持不变。改了提示词、模型等执行配置的请求仍然返回 400,避免这类修改被悄悄丢掉。TestScheduleJobMemberWithoutKeyCanStopIt 补了两种情况:停用时改名,以及停用时改通知和间隔,都断言保存后与原任务一致。撤掉修复时用例失败,名称被改成请求里的值。
There was a problem hiding this comment.
[P2] 停用任务时保留并发保存的配置
停用时改名和修改通知的回归已通过。我补测了并发保存:停用请求读到原任务后,一次正常编辑先提交。停用随后返回 200,却把任务名和提示词恢复成旧值。*job = prior 取的是请求开始时的快照,而 PutScheduleJob 会写回完整 payload。建议停用路径在仓储事务内更新当前行的 enabled,同步更新 payload 中的值,保留已经提交的配置。
There was a problem hiding this comment.
说得对,已在 b1e0c64 修复:没有 Key 的停用不再写回请求开始时读到的整条任务,改为调用新增的仓储方法 DisableScheduleJob,在事务内锁住当前行,只把 payload 和 enabled 列改为停用,然后返回库里的最新任务,期间其他人已提交的名称、提示词和执行身份都保留。是否允许走这条路径的判断不变(执行配置未变才放行)。新增仓储用例 TestDisableScheduleJobKeepsEditsSavedMeanwhile:读取后先提交一次编辑,再停用,断言编辑保留且任务已停用。原有的停用用例也都通过。
There was a problem hiding this comment.
[P1] 停用时在锁内核对任务的项目归属
并发编辑的回归已通过。requireScheduleJob 校验的是最初读到的项目,新的 DisableScheduleJob 在事务里只按任务 ID 锁行。如果任务在此期间被移到另一项目,停用会作用于该项目的记录,随后的独立查询还会返回完整任务配置。请在锁内校验当前记录仍属于已授权的项目,并直接返回事务中已核对的结果。
There was a problem hiding this comment.
说得对,已在 731cefa 修复。这几轮的问题根源相同:停用路径在锁外读取和判断,在锁内写入。这次把它改为乐观并发:
DisableScheduleJob(id, unchanged)在行锁内对当前记录执行全部判断(scheduleStopAllowed):仍在已授权的原项目,且提示词、模型、项目、订阅版本、参数仍与请求一致。只要有一项不满足就返回409(任务已被其他人修改或移动,请刷新后重试),不尝试合并。- 通过时只改
enabled,并直接返回事务里核对过的那一行,不再单独查询。任务已被删除时同样返回 409。 - 测试:
TestScheduleStopAllowedChecksTheStoredJob覆盖期间改名(放行)、移到其他项目、改提示词、改模型、改参数、改成触发任务(都拒绝),其中移项目这一项在旧逻辑下失败;TestDisableScheduleJobKeepsEditsSavedMeanwhile断言判断函数拿到的是锁内的当前行、被拒时不改任何字段、通过时保留期间的编辑,并返回该行。
执行身份另有一道运行时校验:每次运行都会检查绑定成员仍存在、仍是项目可写成员,否则不运行。普通保存(有 Key 的编辑)的并发语义没有改,仍然是后写覆盖先写,和 PR 之前一致,不在这次范围内。
Keeps defaultProjectId a pure function, so web/default_project_test.go can evaluate it on its own without the app's top-level state.
…ers stop jobs A job saved before the run-as binding tried the shared key for the current project owner before the recorded owner's personal key, so after an owner change a job that ran on the old owner's key switched identity and could be refused by the shared key's model allowlist. Personal keys of every candidate now go first, in the original order, and the shared key is only the fallback. Unchecking 启用 goes through the same save as editing, so a writer without a usable key got 400 even when only disabling the job. A save that disables the job and leaves everything it runs unchanged now keeps the existing binding instead. Changing the prompt, model, project or subscription, or enabling the job again, still needs the saver's key, because a stopped job can still be run by hand.
A writer without a usable key could stop a job and keep the previous saver's binding while also renaming it or changing its notifications. Both go into the prompt sent to the model, and a stopped job can still be run by hand under that binding. Such a save now keeps the stored job and only turns enabled off.
The keyless stop wrote back the job as the request had read it, so an edit saved in between was reverted while the stop still returned 200. It now flips enabled in a locked transaction on the stored row and returns that row.
cocofhu
left a comment
There was a problem hiding this comment.
保存人 Key、旧任务先个人 Key 再共享 Key、无 Key 停用只改 enabled,以及首次加载选中自己所在项目,这几处和现有用例是对齐的。还差停用路径的项目归属:权限校验用的是请求开始时的快照,DisableScheduleJob 锁内只按任务 ID 更新,事务外再查一次又把整行返回。任务在这两次读取之间被移到另一个项目时,停用会作用到那个项目,调用方还能拿到完整配置。请在锁内确认当前行仍属于已授权的项目;对不上就不要改、也不要返回该行,并直接返回事务里核对过的结果。
| if job.Enabled || !scheduleRunConfigUnchanged(prior, *job) { | ||
| return false | ||
| } | ||
| if err := h.repo.DisableScheduleJob(prior.ID); err != nil { |
There was a problem hiding this comment.
[P1] 停用时在锁内核对任务仍属于已授权的项目
requireScheduleJob 校验的是请求开始时读到的 prior。这里调用的 DisableScheduleJob 在事务里只按任务 ID 锁行,把 enabled 改成 false,然后锁外再 ScheduleJob 一次,忽略查询错误,把整行返回。
保存按 id 可以改 projectId,而且原项目和目标项目都会单独做写权限校验。窗口里如果另一次保存把任务挪到调用方没有写权限的项目:
- 这次停用仍返回 200,并关掉已经不属于原项目的任务;
- 响应里是目标项目上的完整任务(提示词、通知、执行身份),调用方本来读不到。
请把已授权的 projectId 传进仓储方法。锁住当前行之后,先确认 payload 里的项目仍是这个项目;不一致就返回错误,不要更新,也不要把该行写进响应。一致时只改 enabled,并返回事务内这份已经核对过的任务,不要再锁外读一次。
There was a problem hiding this comment.
谢谢,这条和上面 crispinhu27 那条是同一个问题,已在 731cefa 按这个思路修复:仓储方法 DisableScheduleJob 在行锁内先核对当前记录仍在已授权的原项目(以及执行配置未变),不一致就返回 409,既不更新也不在响应里返回该行;一致时只改 enabled,直接返回事务内核对过的任务,锁外不再读取。详见上一条回复和 TestScheduleStopAllowedChecksTheStoredJob、TestDisableScheduleJobKeepsEditsSavedMeanwhile。
The keyless stop was authorized against the job as first read, but the transaction only locked the row by id, so a job moved to another project meanwhile was still turned off and then returned in full by a separate read. Every condition of the stop path, project included, is now checked under the row lock against the stored job; if it no longer holds the save gets 409, and the response is the row the transaction checked.
crispinhu27
left a comment
There was a problem hiding this comment.
已复测旧任务个人 Key 优先级和无 Key 停用。并发编辑会保留,跨项目移动和执行配置变更会被事务内检查拦截。相关 Go 回归用例通过。
cocofhu
left a comment
There was a problem hiding this comment.
停用路径已按上次意见改完。DisableScheduleJob 在行锁内用 scheduleStopAllowed 核对当前行仍在已授权的原项目,且提示词、模型、项目、订阅和参数仍与请求一致;对不上返回 409,不更新、也不把该行写进响应。通过时只改 enabled,直接返回事务里的那一行。改名等非执行配置的并发保存会保留。TestScheduleStopAllowedChecksTheStoredJob 和 TestDisableScheduleJobKeepsEditsSavedMeanwhile 覆盖了移项目被拒、被拒时不改字段、通过时保留期间编辑。
What changed
AI 定时任务以前只能用项目 Owner 在所选 CLI 上的 Key 执行,而且不会借用平台共享 CodeBuddy Key。只要项目 Owner 没配 Key,项目里的 AI 定时任务就都跑不起来,哪怕创建任务的成员自己有 Key,或者平台配了共享 Key。
现在改为用保存人的 Key 执行:
runAsUserId/runAsName设为当前登录用户,请求里传的值不生效。新建时是创建人;其他成员编辑保存后改用他的 Key,不会借用别人的 Key。另外修了一个顺带发现的前端问题:控制台首次加载时,
defaultProjectId通过globalThis.state取当前用户,但state是app.js顶层的const,不在globalThis上,所以取到的永远是 undefined,"优先选我参与的项目"的逻辑一直没生效,默认选中的是welcome或列表里的第一个项目。项目列表对所有登录用户可见,第一个项目可能是自己不是成员的项目:成员打开定时任务页时看不到本项目的任务,新建任务被提交到那个项目,返回 403(同时报「没有该项目的通知管理权限」)。现在改为直接读state.currentUser,并补了回归测试。前端弹窗、模型提示、运行提示的文案同步更新,文档(
docs/ai-scheduled-task-capabilities.md、docs/api/orchestration.md、openapi.yaml)同步说明。这部分是从 #958 的 7cbf79a 单独拆出来的,先合入;#958 里执行记录、会话模式等其余改动另行讨论。
Scope
Compatibility
runAsUserId/runAsName存在任务的 JSON payload 里。Validation
本地部署实测:用本分支构建 Docker 镜像,compose 部署,真实 CodeBuddy CLI,平台共享 iOA Key 只开放
deepseek-v4.1-flash。deepseek-v4.1-flashSafety