@@ -1764,6 +1764,12 @@ export function agentRoutes(db: Db) {
17641764 router . post ( "/agents/:id/pause" , async ( req , res ) => {
17651765 assertBoard ( req ) ;
17661766 const id = req . params . id as string ;
1767+ const existing = await svc . getById ( id ) ;
1768+ if ( ! existing ) {
1769+ res . status ( 404 ) . json ( { error : "Agent not found" } ) ;
1770+ return ;
1771+ }
1772+ assertCompanyAccess ( req , existing . companyId ) ;
17671773 const agent = await svc . pause ( id ) ;
17681774 if ( ! agent ) {
17691775 res . status ( 404 ) . json ( { error : "Agent not found" } ) ;
@@ -1787,6 +1793,12 @@ export function agentRoutes(db: Db) {
17871793 router . post ( "/agents/:id/resume" , async ( req , res ) => {
17881794 assertBoard ( req ) ;
17891795 const id = req . params . id as string ;
1796+ const existing = await svc . getById ( id ) ;
1797+ if ( ! existing ) {
1798+ res . status ( 404 ) . json ( { error : "Agent not found" } ) ;
1799+ return ;
1800+ }
1801+ assertCompanyAccess ( req , existing . companyId ) ;
17901802 const agent = await svc . resume ( id ) ;
17911803 if ( ! agent ) {
17921804 res . status ( 404 ) . json ( { error : "Agent not found" } ) ;
@@ -1808,6 +1820,12 @@ export function agentRoutes(db: Db) {
18081820 router . post ( "/agents/:id/terminate" , async ( req , res ) => {
18091821 assertBoard ( req ) ;
18101822 const id = req . params . id as string ;
1823+ const existing = await svc . getById ( id ) ;
1824+ if ( ! existing ) {
1825+ res . status ( 404 ) . json ( { error : "Agent not found" } ) ;
1826+ return ;
1827+ }
1828+ assertCompanyAccess ( req , existing . companyId ) ;
18111829 const agent = await svc . terminate ( id ) ;
18121830 if ( ! agent ) {
18131831 res . status ( 404 ) . json ( { error : "Agent not found" } ) ;
@@ -1831,6 +1849,12 @@ export function agentRoutes(db: Db) {
18311849 router . delete ( "/agents/:id" , async ( req , res ) => {
18321850 assertBoard ( req ) ;
18331851 const id = req . params . id as string ;
1852+ const existing = await svc . getById ( id ) ;
1853+ if ( ! existing ) {
1854+ res . status ( 404 ) . json ( { error : "Agent not found" } ) ;
1855+ return ;
1856+ }
1857+ assertCompanyAccess ( req , existing . companyId ) ;
18341858 const agent = await svc . remove ( id ) ;
18351859 if ( ! agent ) {
18361860 res . status ( 404 ) . json ( { error : "Agent not found" } ) ;
@@ -1852,34 +1876,56 @@ export function agentRoutes(db: Db) {
18521876 router . get ( "/agents/:id/keys" , async ( req , res ) => {
18531877 assertBoard ( req ) ;
18541878 const id = req . params . id as string ;
1879+ const agent = await svc . getById ( id ) ;
1880+ if ( ! agent ) {
1881+ res . status ( 404 ) . json ( { error : "Agent not found" } ) ;
1882+ return ;
1883+ }
1884+ assertCompanyAccess ( req , agent . companyId ) ;
18551885 const keys = await svc . listKeys ( id ) ;
18561886 res . json ( keys ) ;
18571887 } ) ;
18581888
18591889 router . post ( "/agents/:id/keys" , validate ( createAgentKeySchema ) , async ( req , res ) => {
18601890 assertBoard ( req ) ;
18611891 const id = req . params . id as string ;
1862- const key = await svc . createApiKey ( id , req . body . name ) ;
1863-
18641892 const agent = await svc . getById ( id ) ;
1865- if ( agent ) {
1866- await logActivity ( db , {
1867- companyId : agent . companyId ,
1868- actorType : "user" ,
1869- actorId : req . actor . userId ?? "board" ,
1870- action : "agent.key_created" ,
1871- entityType : "agent" ,
1872- entityId : agent . id ,
1873- details : { keyId : key . id , name : key . name } ,
1874- } ) ;
1893+ if ( ! agent ) {
1894+ res . status ( 404 ) . json ( { error : "Agent not found" } ) ;
1895+ return ;
18751896 }
1897+ assertCompanyAccess ( req , agent . companyId ) ;
1898+ const key = await svc . createApiKey ( id , req . body . name ) ;
1899+
1900+ await logActivity ( db , {
1901+ companyId : agent . companyId ,
1902+ actorType : "user" ,
1903+ actorId : req . actor . userId ?? "board" ,
1904+ action : "agent.key_created" ,
1905+ entityType : "agent" ,
1906+ entityId : agent . id ,
1907+ details : { keyId : key . id , name : key . name } ,
1908+ } ) ;
18761909
18771910 res . status ( 201 ) . json ( key ) ;
18781911 } ) ;
18791912
18801913 router . delete ( "/agents/:id/keys/:keyId" , async ( req , res ) => {
18811914 assertBoard ( req ) ;
1915+ const id = req . params . id as string ;
18821916 const keyId = req . params . keyId as string ;
1917+ const agent = await svc . getById ( id ) ;
1918+ if ( ! agent ) {
1919+ res . status ( 404 ) . json ( { error : "Agent not found" } ) ;
1920+ return ;
1921+ }
1922+ assertCompanyAccess ( req , agent . companyId ) ;
1923+ // Verify the key belongs to this agent to prevent cross-agent key revocation
1924+ const keys = await svc . listKeys ( id ) ;
1925+ if ( ! keys . some ( ( k ) => k . id === keyId ) ) {
1926+ res . status ( 404 ) . json ( { error : "Key not found for this agent" } ) ;
1927+ return ;
1928+ }
18831929 const revoked = await svc . revokeKey ( keyId ) ;
18841930 if ( ! revoked ) {
18851931 res . status ( 404 ) . json ( { error : "Key not found" } ) ;
@@ -2098,6 +2144,12 @@ export function agentRoutes(db: Db) {
20982144 router . post ( "/heartbeat-runs/:runId/cancel" , async ( req , res ) => {
20992145 assertBoard ( req ) ;
21002146 const runId = req . params . runId as string ;
2147+ const existing = await heartbeat . getRun ( runId ) ;
2148+ if ( ! existing ) {
2149+ res . status ( 404 ) . json ( { error : "Heartbeat run not found" } ) ;
2150+ return ;
2151+ }
2152+ assertCompanyAccess ( req , existing . companyId ) ;
21012153 const run = await heartbeat . cancelRun ( runId ) ;
21022154
21032155 if ( run ) {
0 commit comments