AgentGrantV3Test.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. use App\Models\AiModel;
  3. use App\Models\Discussion;
  4. use App\Services\AuthService;
  5. use Illuminate\Foundation\Testing\RefreshDatabase;
  6. uses(RefreshDatabase::class);
  7. /*
  8. * agent 协作:人类签发授权(POST /v3/agent-grants),模型凭模型 token + X-Agent-Grant 代写。
  9. * 代写的记录 editor = 授权人、co_editor = 模型,权限按授权人判、范围限授权的 channel / book。
  10. */
  11. /** 模型身份的请求头;$grant 给了就带上授权 */
  12. function modelHeader(AiModel $model, ?string $grant = null): array
  13. {
  14. $headers = ['Authorization' => 'Bearer '.AuthService::getUserToken($model->uid)];
  15. if ($grant !== null) {
  16. $headers['X-Agent-Grant'] = $grant;
  17. }
  18. return $headers;
  19. }
  20. /** 人类为自己的模型在 channel 上签一张授权,返回 token */
  21. function grantFor(string $human, AiModel $model, string $channel, int $book = 0): string
  22. {
  23. return test()->postJson('/api/v3/agent-grants', [
  24. 'ai_model' => $model->uid, 'target_type' => 'channel', 'target_id' => $channel, 'book' => $book,
  25. ], authHeader($human))->assertCreated()->json('data.token');
  26. }
  27. /* ------------------------------------------------------------------ 签发 */
  28. it('issues a grant bound to the model, the channel and the grantor', function () {
  29. $human = makeStudio('owner');
  30. $channel = makeChannel($human);
  31. $model = AiModel::factory()->ownedBy($human)->create();
  32. $this->postJson('/api/v3/agent-grants', [
  33. 'ai_model' => $model->uid, 'target_type' => 'channel', 'target_id' => $channel, 'book' => 3,
  34. ], authHeader($human))
  35. ->assertCreated()
  36. ->assertJsonPath('data.grantor', $human)
  37. ->assertJsonPath('data.ai_model', $model->uid)
  38. ->assertJsonPath('data.target_id', $channel)
  39. ->assertJsonPath('data.book', 3)
  40. ->assertJsonStructure(['data' => ['token', 'expires_at']]);
  41. });
  42. it('refuses to issue a grant without the right to do so', function () {
  43. $human = makeStudio('owner');
  44. $channel = makeChannel($human);
  45. $model = AiModel::factory()->ownedBy($human)->create();
  46. $body = ['ai_model' => $model->uid, 'target_type' => 'channel', 'target_id' => $channel];
  47. $this->postJson('/api/v3/agent-grants', $body)->assertUnauthorized();
  48. $this->postJson('/api/v3/agent-grants', $body, modelHeader($model))->assertForbidden(); // 模型不能给自己授权
  49. $this->postJson('/api/v3/agent-grants', $body, authHeader(makeStudio('stranger')))->assertForbidden(); // 模型不是他的
  50. $other = makeChannel(makeStudio('other'));
  51. $this->postJson('/api/v3/agent-grants', [...$body, 'target_id' => $other], authHeader($human))->assertForbidden(); // channel 编不了
  52. $this->postJson('/api/v3/agent-grants', [...$body, 'ai_model' => (string) Str::uuid()], authHeader($human))
  53. ->assertStatus(422)->assertJsonValidationErrors('ai_model');
  54. $this->postJson('/api/v3/agent-grants', [...$body, 'target_type' => 'article'], authHeader($human))
  55. ->assertStatus(422)->assertJsonValidationErrors('target_type');
  56. });
  57. it('lets a team editor grant a model on a team channel, but not a reader', function () {
  58. [$team, $users] = makeTeam();
  59. $channel = makeChannel($team);
  60. $sentence = makeSentence($channel, 1, 1, 1, 'text')->uid;
  61. $model = AiModel::factory()->ownedBy($users['editor'])->create();
  62. $grant = grantFor($users['editor'], $model, $channel);
  63. $this->postJson('/api/v3/discussions', [
  64. 'target_type' => 'sentence', 'target_id' => $sentence, 'type' => 'note', 'content' => 'by agent',
  65. ], modelHeader($model, $grant))
  66. ->assertCreated()
  67. ->assertJsonPath('data.editor.id', $users['editor'])
  68. ->assertJsonPath('data.co_editor.id', $model->uid);
  69. $readerModel = AiModel::factory()->ownedBy($users['reader'])->create();
  70. $this->postJson('/api/v3/agent-grants', [
  71. 'ai_model' => $readerModel->uid, 'target_type' => 'channel', 'target_id' => $channel,
  72. ], authHeader($users['reader']))->assertForbidden();
  73. });
  74. /* ------------------------------------------------------------------ 代写 */
  75. it('records the grantor as editor and the model as co-editor', function () {
  76. $human = makeStudio('owner');
  77. $channel = makeChannel($human);
  78. $sentence = makeSentence($channel, 1, 1, 1, 'text')->uid;
  79. $model = AiModel::factory()->ownedBy($human)->create();
  80. $grant = grantFor($human, $model, $channel);
  81. // 批注:只有句子的管理者能建——按授权人判,所以模型能写进来
  82. $note = $this->postJson('/api/v3/discussions', [
  83. 'target_type' => 'sentence', 'target_id' => $sentence, 'type' => 'commentary', 'content' => '{{1-2-3-4}}',
  84. ], modelHeader($model, $grant))
  85. ->assertCreated()
  86. ->assertJsonPath('data.editor.id', $human)
  87. ->assertJsonPath('data.co_editor.id', $model->uid)
  88. ->json('data');
  89. $this->postJson("/api/v3/discussions/{$note['id']}/replies", ['content' => '补充', 'notify' => false], modelHeader($model, $grant))
  90. ->assertCreated()
  91. ->assertJsonPath('data.co_editor.id', $model->uid);
  92. $this->patchJson("/api/v3/discussions/{$note['id']}", ['pos_end' => 2], modelHeader($model, $grant))->assertOk();
  93. expect(Discussion::find($note['id']))
  94. ->editor_uid->toBe($human)
  95. ->co_editor_uid->toBe($model->uid);
  96. // 人类自己写的没有协作者
  97. $this->getJson("/api/v3/discussions/{$note['id']}")->assertJsonPath('data.co_editor.id', $model->uid);
  98. $this->postJson('/api/v3/discussions', [
  99. 'target_type' => 'sentence', 'target_id' => $sentence, 'title' => 't', 'notify' => false,
  100. ], authHeader($human))->assertCreated()->assertJsonPath('data.co_editor', null);
  101. });
  102. it('lets a model assess a sentence on a grant, overwriting with itself as co-editor', function () {
  103. $human = makeStudio('owner');
  104. $channel = makeChannel($human);
  105. $sentence = makeSentence($channel, 1, 1, 1, 'text')->uid;
  106. $model = AiModel::factory()->ownedBy($human)->create();
  107. $payload = ['target_type' => 'sentence', 'target_id' => $sentence, 'type' => 'assessment', 'severity' => 'llm-judge'];
  108. $this->postJson('/api/v3/discussions', [...$payload, 'score' => 70], modelHeader($model))->assertForbidden();
  109. // 人先评,模型凭授权重评:覆盖同一条,editor 是授权人、co_editor 是模型
  110. $id = $this->postJson('/api/v3/discussions', [...$payload, 'score' => 70], authHeader($human))
  111. ->assertCreated()->assertJsonPath('data.co_editor', null)->json('data.id');
  112. $this->postJson('/api/v3/discussions', [...$payload, 'score' => 81, 'content' => '理由'], modelHeader($model, grantFor($human, $model, $channel)))
  113. ->assertOk()
  114. ->assertJsonPath('data.id', $id)
  115. ->assertJsonPath('data.score', 81)
  116. ->assertJsonPath('data.editor.id', $human)
  117. ->assertJsonPath('data.co_editor.id', $model->uid);
  118. });
  119. it('rejects agent writes that are not covered by a valid grant', function () {
  120. $human = makeStudio('owner');
  121. $channel = makeChannel($human);
  122. $sentence = makeSentence($channel, 1, 1, 1, 'text')->uid;
  123. $model = AiModel::factory()->ownedBy($human)->create();
  124. $body = ['target_type' => 'sentence', 'target_id' => $sentence, 'type' => 'note', 'content' => 'x'];
  125. $this->postJson('/api/v3/discussions', $body, modelHeader($model))->assertForbidden(); // 没授权
  126. $this->postJson('/api/v3/discussions', $body, modelHeader($model, 'garbage'))->assertForbidden(); // 伪造
  127. $otherChannel = makeChannel($human, 'other');
  128. $this->postJson('/api/v3/discussions', $body, modelHeader($model, grantFor($human, $model, $otherChannel)))
  129. ->assertForbidden(); // 别的 channel
  130. $this->postJson('/api/v3/discussions', $body, modelHeader($model, grantFor($human, $model, $channel, 2)))
  131. ->assertForbidden(); // 别的 book
  132. $another = AiModel::factory()->ownedBy($human)->create();
  133. $this->postJson('/api/v3/discussions', $body, modelHeader($another, grantFor($human, $model, $channel)))
  134. ->assertForbidden(); // 授权给的不是它
  135. // 人类带授权:授权只给模型用
  136. $this->postJson('/api/v3/discussions', $body, [...authHeader($human), 'X-Agent-Grant' => grantFor($human, $model, $channel)])
  137. ->assertStatus(422);
  138. });
  139. it('does not accept a v2 access token as a grant', function () {
  140. $human = makeStudio('owner');
  141. $channel = makeChannel($human);
  142. $sentence = makeSentence($channel, 1, 1, 1, 'text')->uid;
  143. $model = AiModel::factory()->ownedBy($human)->create();
  144. // v2 的载荷由客户端决定,伪造 grantor / act 也签得出来
  145. $v2 = $this->postJson('/api/v2/access-token', ['payload' => [[
  146. 'res_type' => 'channel', 'res_id' => $channel, 'power' => 'edit', 'book' => 0,
  147. 'typ' => 'agent-grant', 'grantor' => $human, 'act' => $model->uid, 'ver' => 0,
  148. ]]], authHeader($human))->json('data.rows.0.token');
  149. $this->postJson('/api/v3/discussions', [
  150. 'target_type' => 'sentence', 'target_id' => $sentence, 'type' => 'note', 'content' => 'x',
  151. ], modelHeader($model, $v2))->assertForbidden();
  152. });
  153. it('voids grants when the model token is revoked', function () {
  154. $human = makeStudio('owner');
  155. $channel = makeChannel($human);
  156. $sentence = makeSentence($channel, 1, 1, 1, 'text')->uid;
  157. $model = AiModel::factory()->ownedBy($human)->create();
  158. $grant = grantFor($human, $model, $channel);
  159. $model->increment('token_version'); // 撤销:v2 DELETE /v2/ai-model-token 做的就是这个
  160. $this->postJson('/api/v3/discussions', [
  161. 'target_type' => 'sentence', 'target_id' => $sentence, 'type' => 'note', 'content' => 'x',
  162. ], modelHeader($model->fresh(), $grant))->assertForbidden();
  163. });
  164. it('lets the grantor, not the model, own what the model wrote', function () {
  165. $human = makeStudio('owner');
  166. $channel = makeChannel($human);
  167. $sentence = makeSentence($channel, 1, 1, 1, 'text')->uid;
  168. $model = AiModel::factory()->ownedBy($human)->create();
  169. $id = $this->postJson('/api/v3/discussions', [
  170. 'target_type' => 'sentence', 'target_id' => $sentence, 'title' => 'by agent', 'notify' => false,
  171. ], modelHeader($model, grantFor($human, $model, $channel)))->json('data.id');
  172. $this->getJson("/api/v3/discussions/{$id}", authHeader($human))
  173. ->assertJsonPath('data.abilities.update', true)
  174. ->assertJsonPath('data.abilities.delete', true);
  175. $this->deleteJson("/api/v3/discussions/{$id}", [], authHeader($human))->assertOk();
  176. });
  177. it('clears the co-editor when a person edits the content, but not on a status change', function () {
  178. $human = makeStudio('owner');
  179. $channel = makeChannel($human);
  180. $sentence = makeSentence($channel, 1, 1, 1, 'text')->uid;
  181. $model = AiModel::factory()->ownedBy($human)->create();
  182. $grant = grantFor($human, $model, $channel);
  183. $id = $this->postJson('/api/v3/discussions', [
  184. 'target_type' => 'sentence', 'target_id' => $sentence, 'title' => 'by agent', 'notify' => false,
  185. ], modelHeader($model, $grant))->json('data.id');
  186. $this->patchJson("/api/v3/discussions/{$id}", ['status' => 'close'], authHeader($human))
  187. ->assertJsonPath('data.co_editor.id', $model->uid); // 管理动作,不动署名
  188. $this->patchJson("/api/v3/discussions/{$id}", ['content' => '人改过'], authHeader($human))
  189. ->assertJsonPath('data.co_editor', null); // 人亲自改了内容
  190. $this->patchJson("/api/v3/discussions/{$id}", ['content' => 'AI 又改'], modelHeader($model, $grant))
  191. ->assertJsonPath('data.co_editor.id', $model->uid); // AI 代改,换回模型
  192. });