@namzu/sdk 74.1.0 → 75.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +47 -0
- package/dist/bridge/acp/server.d.ts +2 -0
- package/dist/bridge/acp/server.d.ts.map +1 -1
- package/dist/bridge/acp/server.js +9 -1
- package/dist/bridge/acp/server.js.map +1 -1
- package/dist/connector/mcp/adapter.d.ts +1 -1
- package/dist/connector/mcp/adapter.d.ts.map +1 -1
- package/dist/connector/mcp/adapter.js +3 -2
- package/dist/connector/mcp/adapter.js.map +1 -1
- package/dist/connector/mcp/client.d.ts +4 -25
- package/dist/connector/mcp/client.d.ts.map +1 -1
- package/dist/connector/mcp/client.js +32 -32
- package/dist/connector/mcp/client.js.map +1 -1
- package/dist/provider/errors.d.ts.map +1 -1
- package/dist/provider/errors.js +13 -18
- package/dist/provider/errors.js.map +1 -1
- package/dist/provider/stream-request-timeout.d.ts +9 -0
- package/dist/provider/stream-request-timeout.d.ts.map +1 -0
- package/dist/provider/stream-request-timeout.js +34 -0
- package/dist/provider/stream-request-timeout.js.map +1 -0
- package/dist/public-runtime.d.ts +1 -0
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +1 -0
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-tools.d.ts +1 -1
- package/dist/public-tools.d.ts.map +1 -1
- package/dist/public-tools.js +1 -1
- package/dist/public-tools.js.map +1 -1
- package/dist/runtime/query/executor.d.ts +3 -0
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +15 -3
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/file-evidence-replay.d.ts +1 -0
- package/dist/runtime/query/file-evidence-replay.d.ts.map +1 -1
- package/dist/runtime/query/file-evidence-replay.js +64 -21
- package/dist/runtime/query/file-evidence-replay.js.map +1 -1
- package/dist/runtime/query/file-evidence-seed.d.ts +7 -0
- package/dist/runtime/query/file-evidence-seed.d.ts.map +1 -1
- package/dist/runtime/query/file-evidence-seed.js +12 -1
- package/dist/runtime/query/file-evidence-seed.js.map +1 -1
- package/dist/runtime/query/index.d.ts +2 -0
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +5 -2
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/path-escalation.d.ts.map +1 -1
- package/dist/runtime/query/path-escalation.js +4 -3
- package/dist/runtime/query/path-escalation.js.map +1 -1
- package/dist/runtime/query/tooling.d.ts +2 -0
- package/dist/runtime/query/tooling.d.ts.map +1 -1
- package/dist/runtime/query/tooling.js +1 -0
- package/dist/runtime/query/tooling.js.map +1 -1
- package/dist/sandbox/provider/local.d.ts.map +1 -1
- package/dist/sandbox/provider/local.js +45 -18
- package/dist/sandbox/provider/local.js.map +1 -1
- package/dist/schedules/cron.js +1 -1
- package/dist/schedules/cron.js.map +1 -1
- package/dist/schedules/describe.js +1 -1
- package/dist/schedules/describe.js.map +1 -1
- package/dist/schedules/next-fire.d.ts +2 -2
- package/dist/schedules/next-fire.d.ts.map +1 -1
- package/dist/schedules/next-fire.js +7 -4
- package/dist/schedules/next-fire.js.map +1 -1
- package/dist/schedules/spec.d.ts.map +1 -1
- package/dist/schedules/spec.js +23 -7
- package/dist/schedules/spec.js.map +1 -1
- package/dist/schedules/types.d.ts +2 -0
- package/dist/schedules/types.d.ts.map +1 -1
- package/dist/store/session-index/sqlite.d.ts.map +1 -1
- package/dist/store/session-index/sqlite.js +7 -3
- package/dist/store/session-index/sqlite.js.map +1 -1
- package/dist/store/session-log/core.d.ts.map +1 -1
- package/dist/store/session-log/core.js +88 -24
- package/dist/store/session-log/core.js.map +1 -1
- package/dist/store/session-log/disk.js +1 -1
- package/dist/store/session-log/disk.js.map +1 -1
- package/dist/tools/builtins/code-analysis.d.ts.map +1 -1
- package/dist/tools/builtins/code-analysis.js +4 -1
- package/dist/tools/builtins/code-analysis.js.map +1 -1
- package/dist/tools/builtins/edit-apply.d.ts.map +1 -1
- package/dist/tools/builtins/edit-apply.js +24 -10
- package/dist/tools/builtins/edit-apply.js.map +1 -1
- package/dist/tools/builtins/edit.d.ts.map +1 -1
- package/dist/tools/builtins/edit.js +31 -15
- package/dist/tools/builtins/edit.js.map +1 -1
- package/dist/tools/builtins/file-text.d.ts +3 -0
- package/dist/tools/builtins/file-text.d.ts.map +1 -0
- package/dist/tools/builtins/file-text.js +14 -0
- package/dist/tools/builtins/file-text.js.map +1 -0
- package/dist/tools/builtins/glob.d.ts.map +1 -1
- package/dist/tools/builtins/glob.js +7 -2
- package/dist/tools/builtins/glob.js.map +1 -1
- package/dist/tools/builtins/grep.d.ts.map +1 -1
- package/dist/tools/builtins/grep.js +8 -5
- package/dist/tools/builtins/grep.js.map +1 -1
- package/dist/tools/builtins/ls.d.ts.map +1 -1
- package/dist/tools/builtins/ls.js +2 -1
- package/dist/tools/builtins/ls.js.map +1 -1
- package/dist/tools/builtins/notebook-edit.d.ts.map +1 -1
- package/dist/tools/builtins/notebook-edit.js +19 -10
- package/dist/tools/builtins/notebook-edit.js.map +1 -1
- package/dist/tools/builtins/read-file.d.ts.map +1 -1
- package/dist/tools/builtins/read-file.js +48 -24
- package/dist/tools/builtins/read-file.js.map +1 -1
- package/dist/tools/builtins/read-regular-file.d.ts +5 -0
- package/dist/tools/builtins/read-regular-file.d.ts.map +1 -0
- package/dist/tools/builtins/read-regular-file.js +23 -0
- package/dist/tools/builtins/read-regular-file.js.map +1 -0
- package/dist/tools/builtins/search-support.d.ts +2 -0
- package/dist/tools/builtins/search-support.d.ts.map +1 -1
- package/dist/tools/builtins/search-support.js +14 -0
- package/dist/tools/builtins/search-support.js.map +1 -1
- package/dist/tools/builtins/stale-before-review.d.ts.map +1 -1
- package/dist/tools/builtins/stale-before-review.js +8 -5
- package/dist/tools/builtins/stale-before-review.js.map +1 -1
- package/dist/tools/builtins/write-file.d.ts.map +1 -1
- package/dist/tools/builtins/write-file.js +17 -13
- package/dist/tools/builtins/write-file.js.map +1 -1
- package/dist/tools/paths.d.ts +9 -0
- package/dist/tools/paths.d.ts.map +1 -1
- package/dist/tools/paths.js +39 -9
- package/dist/tools/paths.js.map +1 -1
- package/dist/tools/posix-path.d.ts +2 -14
- package/dist/tools/posix-path.d.ts.map +1 -1
- package/dist/tools/posix-path.js +7 -0
- package/dist/tools/posix-path.js.map +1 -1
- package/dist/types/code-analysis/index.d.ts +1 -1
- package/dist/types/code-analysis/index.d.ts.map +1 -1
- package/dist/types/provider/errors.d.ts.map +1 -1
- package/dist/types/provider/errors.js +3 -5
- package/dist/types/provider/errors.js.map +1 -1
- package/dist/types/tool/index.d.ts +2 -0
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/package.json +1 -1
- package/src/bridge/acp/server.ts +16 -7
- package/src/connector/mcp/adapter.ts +4 -3
- package/src/connector/mcp/client.ts +33 -32
- package/src/provider/errors.ts +12 -19
- package/src/provider/stream-request-timeout.ts +34 -0
- package/src/public-runtime.ts +1 -0
- package/src/public-tools.ts +1 -0
- package/src/runtime/query/executor.ts +29 -3
- package/src/runtime/query/file-evidence-replay.ts +73 -19
- package/src/runtime/query/file-evidence-seed.ts +16 -0
- package/src/runtime/query/index.ts +9 -1
- package/src/runtime/query/path-escalation.ts +4 -3
- package/src/runtime/query/tooling.ts +3 -0
- package/src/sandbox/provider/local.ts +53 -25
- package/src/schedules/cron.ts +1 -1
- package/src/schedules/describe.ts +1 -1
- package/src/schedules/next-fire.ts +7 -4
- package/src/schedules/spec.ts +36 -7
- package/src/schedules/types.ts +2 -0
- package/src/store/session-index/sqlite.ts +8 -4
- package/src/store/session-log/core.ts +96 -25
- package/src/store/session-log/disk.ts +1 -1
- package/src/tools/builtins/code-analysis.ts +6 -1
- package/src/tools/builtins/edit-apply.ts +25 -10
- package/src/tools/builtins/edit.ts +31 -15
- package/src/tools/builtins/file-text.ts +14 -0
- package/src/tools/builtins/glob.ts +6 -1
- package/src/tools/builtins/grep.ts +7 -5
- package/src/tools/builtins/ls.ts +2 -1
- package/src/tools/builtins/notebook-edit.ts +18 -11
- package/src/tools/builtins/read-file.ts +52 -27
- package/src/tools/builtins/read-regular-file.ts +25 -0
- package/src/tools/builtins/search-support.ts +13 -0
- package/src/tools/builtins/stale-before-review.ts +8 -6
- package/src/tools/builtins/write-file.ts +18 -12
- package/src/tools/paths.ts +49 -7
- package/src/tools/posix-path.ts +9 -0
- package/src/types/code-analysis/index.ts +1 -1
- package/src/types/provider/errors.ts +3 -5
- package/src/types/tool/index.ts +2 -0
|
@@ -440,8 +440,9 @@ export class SessionLogCore implements SessionLog {
|
|
|
440
440
|
/** Records accepted into the chain that the medium has not taken yet, oldest first. */
|
|
441
441
|
#pending: { readonly bytes: Buffer; readonly sync: boolean }[] = []
|
|
442
442
|
#pendingBytes = 0
|
|
443
|
-
/**
|
|
444
|
-
#
|
|
443
|
+
/** The verified chain on the medium while the in-memory chain is ahead. */
|
|
444
|
+
#durableChain: SessionLogChain | undefined
|
|
445
|
+
#unrecordedDrops = 0
|
|
445
446
|
#degradedCause: SessionLogDegradedCause | undefined
|
|
446
447
|
#droppedRecords = 0
|
|
447
448
|
#droppedNoticeSent = false
|
|
@@ -512,7 +513,7 @@ export class SessionLogCore implements SessionLog {
|
|
|
512
513
|
*/
|
|
513
514
|
async #catchUp(): Promise<void> {
|
|
514
515
|
// Records waiting for the disk are ahead of it; the lease keeps others out.
|
|
515
|
-
if (this.#
|
|
516
|
+
if (this.#durableChain !== undefined) return
|
|
516
517
|
const size = await this.#medium.size()
|
|
517
518
|
if (size === this.#synced + this.#torn && size >= this.#synced) return
|
|
518
519
|
if (size < this.#synced) this.#reset()
|
|
@@ -831,7 +832,11 @@ export class SessionLogCore implements SessionLog {
|
|
|
831
832
|
}
|
|
832
833
|
const bytes = Buffer.from(line, 'utf8')
|
|
833
834
|
const sync = this.#sync === 'all' || (this.#sync === 'boundaries' && BOUNDARY_TYPES.has(type))
|
|
834
|
-
await this.#persist(bytes, sync)
|
|
835
|
+
const recovered = await this.#persist(bytes, sync)
|
|
836
|
+
if (recovered !== undefined) {
|
|
837
|
+
this.#turns.apply(recovered.record)
|
|
838
|
+
return recovered
|
|
839
|
+
}
|
|
835
840
|
const entry = this.#chain.accept(bytes, this.#synced)
|
|
836
841
|
this.#turns.apply(entry.record)
|
|
837
842
|
this.#synced += bytes.byteLength
|
|
@@ -856,15 +861,19 @@ export class SessionLogCore implements SessionLog {
|
|
|
856
861
|
* buffer and goes out, in order, with the next record that can be written.
|
|
857
862
|
* Any other error is thrown as before. Caller holds the mutex.
|
|
858
863
|
*/
|
|
859
|
-
async #persist(bytes: Buffer, sync: boolean): Promise<
|
|
860
|
-
if (this.#
|
|
864
|
+
async #persist(bytes: Buffer, sync: boolean): Promise<SessionLogEntry | undefined> {
|
|
865
|
+
if (this.#durableChain === undefined) {
|
|
861
866
|
try {
|
|
862
867
|
await this.#medium.append(bytes, this.#synced, sync)
|
|
863
|
-
return
|
|
868
|
+
return undefined
|
|
864
869
|
} catch (error) {
|
|
865
870
|
const cause = sessionLogDegradedCause(error)
|
|
866
871
|
if (cause === undefined) throw error
|
|
867
|
-
this.#
|
|
872
|
+
this.#durableChain = new SessionLogChain({
|
|
873
|
+
head: this.#chain.head,
|
|
874
|
+
sessionId: this.sessionId,
|
|
875
|
+
gen: this.#chain.gen,
|
|
876
|
+
})
|
|
868
877
|
this.#degradedCause = cause
|
|
869
878
|
this.#bufferRecord(bytes, sync)
|
|
870
879
|
this.#notify({
|
|
@@ -873,12 +882,12 @@ export class SessionLogCore implements SessionLog {
|
|
|
873
882
|
bufferedRecords: this.#pending.length,
|
|
874
883
|
bufferedBytes: this.#pendingBytes,
|
|
875
884
|
})
|
|
876
|
-
return
|
|
885
|
+
return undefined
|
|
877
886
|
}
|
|
878
887
|
}
|
|
879
888
|
const queued = this.#bufferRecord(bytes, sync)
|
|
880
889
|
try {
|
|
881
|
-
await this.#flushPending()
|
|
890
|
+
return await this.#flushPending()
|
|
882
891
|
} catch (error) {
|
|
883
892
|
// Only the volume refusing writes keeps the session going; anything else
|
|
884
893
|
// fails this record, which must then not stay queued.
|
|
@@ -890,6 +899,7 @@ export class SessionLogCore implements SessionLog {
|
|
|
890
899
|
}
|
|
891
900
|
throw error
|
|
892
901
|
}
|
|
902
|
+
return undefined
|
|
893
903
|
}
|
|
894
904
|
}
|
|
895
905
|
|
|
@@ -899,11 +909,14 @@ export class SessionLogCore implements SessionLog {
|
|
|
899
909
|
this.#pendingBytes += bytes.byteLength
|
|
900
910
|
let dropped = false
|
|
901
911
|
// The newest record always stays, whatever the bound.
|
|
902
|
-
|
|
903
|
-
|
|
912
|
+
// The first record is required even if the volume refused the session start.
|
|
913
|
+
const keepStart = this.#durableChain?.head === null ? 1 : 0
|
|
914
|
+
while (this.#pendingBytes > this.#degradedBufferBytes && this.#pending.length > keepStart + 1) {
|
|
915
|
+
const [oldest] = this.#pending.splice(keepStart, 1)
|
|
904
916
|
if (oldest === undefined) break
|
|
905
917
|
this.#pendingBytes -= oldest.bytes.byteLength
|
|
906
918
|
this.#droppedRecords += 1
|
|
919
|
+
this.#unrecordedDrops += 1
|
|
907
920
|
dropped = true
|
|
908
921
|
}
|
|
909
922
|
if (dropped && !this.#droppedNoticeSent && this.#degradedCause !== undefined) {
|
|
@@ -919,20 +932,56 @@ export class SessionLogCore implements SessionLog {
|
|
|
919
932
|
return queued
|
|
920
933
|
}
|
|
921
934
|
|
|
922
|
-
/** Write the buffer in order
|
|
923
|
-
async #flushPending(): Promise<
|
|
924
|
-
|
|
935
|
+
/** Write the buffer in order, leaving a valid prefix even if a later write fails. */
|
|
936
|
+
async #flushPending(): Promise<SessionLogEntry | undefined> {
|
|
937
|
+
const chain = this.#durableChain
|
|
938
|
+
if (chain === undefined) return
|
|
925
939
|
const size = await this.#medium.size()
|
|
926
|
-
if (size >
|
|
927
|
-
|
|
940
|
+
if (size > chain.end) await this.#medium.truncate(chain.end, size)
|
|
941
|
+
let last: SessionLogEntry | undefined
|
|
942
|
+
for (;;) {
|
|
928
943
|
const next = this.#pending[0]
|
|
929
944
|
if (next === undefined) break
|
|
930
|
-
|
|
931
|
-
|
|
945
|
+
// The pending bytes were already validated when the record was accepted.
|
|
946
|
+
const record = JSON.parse(next.bytes.toString('utf8')) as SessionRecord
|
|
947
|
+
if (this.#unrecordedDrops > 0 && chain.head !== null) {
|
|
948
|
+
const gap = {
|
|
949
|
+
v: SESSION_RECORD_SCHEMA_VERSION,
|
|
950
|
+
id: generateRecordId(),
|
|
951
|
+
sessionId: this.sessionId,
|
|
952
|
+
ts: record.ts,
|
|
953
|
+
gen: record.gen,
|
|
954
|
+
type: 'log_gap',
|
|
955
|
+
ignorable: true,
|
|
956
|
+
seq: chain.head.seq + 1,
|
|
957
|
+
prev: chain.head,
|
|
958
|
+
droppedRecords: this.#unrecordedDrops,
|
|
959
|
+
firstDroppedSeq: record.seq - this.#unrecordedDrops,
|
|
960
|
+
lastDroppedSeq: record.seq - 1,
|
|
961
|
+
}
|
|
962
|
+
const bytes = Buffer.from(`${JSON.stringify(gap)}\n`, 'utf8')
|
|
963
|
+
await this.#appendPendingLine(chain, bytes, this.#sync !== 'none')
|
|
964
|
+
this.#unrecordedDrops = 0
|
|
965
|
+
}
|
|
966
|
+
const head = chain.head
|
|
967
|
+
let bytes = next.bytes
|
|
968
|
+
if (record.seq !== (head?.seq ?? 0) + 1 || record.prev?.sha256 !== head?.sha256) {
|
|
969
|
+
// Skip links into the provisional chain are no longer valid.
|
|
970
|
+
const { prevText: _prevText, ...payload } = record
|
|
971
|
+
bytes = Buffer.from(
|
|
972
|
+
formatSessionLogLine({ ...payload, seq: (head?.seq ?? 0) + 1, prev: head }),
|
|
973
|
+
'utf8',
|
|
974
|
+
)
|
|
975
|
+
}
|
|
976
|
+
last = await this.#appendPendingLine(chain, bytes, next.sync)
|
|
932
977
|
this.#pendingBytes -= next.bytes.byteLength
|
|
933
|
-
this.#pending.shift()
|
|
934
978
|
this.#flushedWhileDegraded += 1
|
|
979
|
+
// Drop each durable record immediately so a retry starts after it.
|
|
980
|
+
this.#pending.shift()
|
|
935
981
|
}
|
|
982
|
+
this.#chain = chain
|
|
983
|
+
this.#synced = chain.end
|
|
984
|
+
this.#durableChain = undefined
|
|
936
985
|
const flushed = this.#flushedWhileDegraded
|
|
937
986
|
this.#flushedWhileDegraded = 0
|
|
938
987
|
const dropped = this.#droppedRecords
|
|
@@ -941,6 +990,19 @@ export class SessionLogCore implements SessionLog {
|
|
|
941
990
|
this.#droppedNoticeSent = false
|
|
942
991
|
this.#pendingBytes = 0
|
|
943
992
|
this.#notify({ state: 'recovered', flushedRecords: flushed, droppedRecords: dropped })
|
|
993
|
+
return last
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
async #appendPendingLine(
|
|
997
|
+
chain: SessionLogChain,
|
|
998
|
+
bytes: Buffer,
|
|
999
|
+
sync: boolean,
|
|
1000
|
+
): Promise<SessionLogEntry> {
|
|
1001
|
+
const offset = chain.end
|
|
1002
|
+
await this.#medium.append(bytes, offset, sync)
|
|
1003
|
+
const entry = chain.accept(bytes, offset)
|
|
1004
|
+
if (this.#ledgerKey !== undefined) verifiedPrefixOf(this.#ledgerKey)?.append(entry)
|
|
1005
|
+
return entry
|
|
944
1006
|
}
|
|
945
1007
|
|
|
946
1008
|
/**
|
|
@@ -1037,10 +1099,10 @@ export class SessionLogCore implements SessionLog {
|
|
|
1037
1099
|
}
|
|
1038
1100
|
const content = candidate.content as Message & { toolCallId?: unknown }
|
|
1039
1101
|
const key =
|
|
1040
|
-
|
|
1041
|
-
?
|
|
1042
|
-
:
|
|
1043
|
-
?
|
|
1102
|
+
type === 'message_replaced' || type === 'message_edited'
|
|
1103
|
+
? `record:${String(candidate.id)}`
|
|
1104
|
+
: content.role === 'tool' && typeof content.toolCallId === 'string'
|
|
1105
|
+
? content.toolCallId
|
|
1044
1106
|
: `message:${String(candidate.messageId)}`
|
|
1045
1107
|
const ref = await this.#spillOrLose(key, 'message', JSON.stringify(content))
|
|
1046
1108
|
const text =
|
|
@@ -1272,7 +1334,16 @@ export async function* walkSessionLog(
|
|
|
1272
1334
|
}
|
|
1273
1335
|
|
|
1274
1336
|
const splitter = new LineSplitter(start)
|
|
1275
|
-
|
|
1337
|
+
// A resumed walk starts past its verified cursor.
|
|
1338
|
+
const cursor = chain.head
|
|
1339
|
+
const anchor = options.expectHead
|
|
1340
|
+
let anchorSeen =
|
|
1341
|
+
cursor !== null &&
|
|
1342
|
+
anchor !== undefined &&
|
|
1343
|
+
cursor.seq === anchor.seq &&
|
|
1344
|
+
cursor.offset === anchor.offset &&
|
|
1345
|
+
cursor.length === anchor.length &&
|
|
1346
|
+
cursor.sha256 === anchor.sha256
|
|
1276
1347
|
let stopped = false
|
|
1277
1348
|
outer: for await (const chunk of medium.stream(start)) {
|
|
1278
1349
|
let lines: Generator<{ line: Uint8Array; offset: number }>
|
|
@@ -120,7 +120,7 @@ export class DiskLogMedium implements LogMedium {
|
|
|
120
120
|
|
|
121
121
|
async append(bytes: Uint8Array, expectedOffset: number, sync: boolean): Promise<void> {
|
|
122
122
|
await mkdir(dirname(this.file), { recursive: true, mode: 0o700 })
|
|
123
|
-
const handle = await open(this.file, 'a', 0o600)
|
|
123
|
+
const handle = await open(this.file, 'a+', 0o600)
|
|
124
124
|
try {
|
|
125
125
|
const before = (await handle.stat()).size
|
|
126
126
|
if (before !== expectedOffset) {
|
|
@@ -86,7 +86,12 @@ const findLiteralOp = z.object({
|
|
|
86
86
|
.string()
|
|
87
87
|
.optional()
|
|
88
88
|
.describe('File or directory to search; defaults to the working directory.'),
|
|
89
|
-
case_sensitive: z
|
|
89
|
+
case_sensitive: z
|
|
90
|
+
.boolean()
|
|
91
|
+
.optional()
|
|
92
|
+
.describe(
|
|
93
|
+
'Default true. False matches using Unicode simple case folding without locale tailoring.',
|
|
94
|
+
),
|
|
90
95
|
...paging,
|
|
91
96
|
})
|
|
92
97
|
|
|
@@ -363,25 +363,29 @@ function nearLine(content: string, needle: string): number | undefined {
|
|
|
363
363
|
return undefined
|
|
364
364
|
}
|
|
365
365
|
|
|
366
|
+
function insertionLineEnding(content: string): '\r\n' | '\n' {
|
|
367
|
+
return content.includes('\r\n') ? '\r\n' : '\n'
|
|
368
|
+
}
|
|
369
|
+
|
|
366
370
|
function applyLineInsert(
|
|
367
371
|
content: string,
|
|
368
372
|
input: Extract<NormalizedEditInput, { operation: 'insert' }>,
|
|
369
373
|
): { success: true; content: string; replacements: number } {
|
|
374
|
+
const ending = insertionLineEnding(content)
|
|
370
375
|
const hasTrailingNewline = content.endsWith('\n')
|
|
371
|
-
const lines = content.split(
|
|
376
|
+
const lines = content === '' ? [] : content.split(/\r?\n/)
|
|
372
377
|
if (hasTrailingNewline) lines.pop()
|
|
373
378
|
|
|
374
379
|
const line =
|
|
375
380
|
input.insertLine === 'end'
|
|
376
381
|
? lines.length
|
|
377
382
|
: Math.min(Math.max(input.insertLine, 0), lines.length)
|
|
378
|
-
const inserted = input.newString.
|
|
379
|
-
|
|
380
|
-
: input.newString.split('\n')
|
|
383
|
+
const inserted = input.newString.split(/\r?\n/)
|
|
384
|
+
if (input.newString.endsWith('\n')) inserted.pop()
|
|
381
385
|
lines.splice(line, 0, ...inserted)
|
|
382
386
|
return {
|
|
383
387
|
success: true,
|
|
384
|
-
content: `${lines.join(
|
|
388
|
+
content: `${lines.join(ending)}${hasTrailingNewline ? ending : ''}`,
|
|
385
389
|
replacements: 1,
|
|
386
390
|
}
|
|
387
391
|
}
|
|
@@ -568,12 +572,15 @@ export function replayOperationsWithin(
|
|
|
568
572
|
*/
|
|
569
573
|
function predictOne(content: string, operation: NormalizedEditInput, allowance: number): number {
|
|
570
574
|
if (operation.operation === 'insert') {
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
//
|
|
574
|
-
//
|
|
575
|
+
const ending = insertionLineEnding(content)
|
|
576
|
+
const { newString } = operation
|
|
577
|
+
// Existing and inserted breaks are normalized before joining. A nonempty
|
|
578
|
+
// file contributes one join boundary, while the insert's final break is removed.
|
|
575
579
|
return (
|
|
576
|
-
content
|
|
580
|
+
normalizedLineEndingLength(content, ending.length) +
|
|
581
|
+
normalizedLineEndingLength(newString, ending.length) +
|
|
582
|
+
(content === '' ? 0 : ending.length) -
|
|
583
|
+
(newString.endsWith('\n') ? ending.length : 0)
|
|
577
584
|
)
|
|
578
585
|
}
|
|
579
586
|
const { oldString, newString, replace_all } = normalizeLineEndings(content, operation)
|
|
@@ -591,6 +598,14 @@ function predictOne(content: string, operation: NormalizedEditInput, allowance:
|
|
|
591
598
|
return Math.max(content.length + countOccurrences(content, oldString, cap) * growth, 0)
|
|
592
599
|
}
|
|
593
600
|
|
|
601
|
+
function normalizedLineEndingLength(text: string, endingLength: number): number {
|
|
602
|
+
let length = text.length
|
|
603
|
+
for (let index = text.indexOf('\n'); index !== -1; index = text.indexOf('\n', index + 1)) {
|
|
604
|
+
length += endingLength - (text[index - 1] === '\r' ? 2 : 1)
|
|
605
|
+
}
|
|
606
|
+
return length
|
|
607
|
+
}
|
|
608
|
+
|
|
594
609
|
/** Non-overlapping matches, up to `cap`, counted the way `split` counts them. */
|
|
595
610
|
function countOccurrences(content: string, needle: string, cap: number): number {
|
|
596
611
|
// `split('')` yields one part per character; scanning for it would never
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { readFile } from 'node:fs/promises'
|
|
2
|
-
|
|
3
1
|
import { z } from 'zod'
|
|
4
2
|
import type { ToolContext, ToolResult } from '../../types/tool/index.js'
|
|
5
3
|
import type { ToolFacts } from '../../types/tool/presentation.js'
|
|
6
4
|
import { defineTool } from '../defineTool.js'
|
|
7
|
-
import { resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
5
|
+
import { resolveAllowedFile, resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
6
|
+
import { refuseSandboxHomePath } from '../posix-path.js'
|
|
8
7
|
import { atomicWriteFile } from './atomic-write-file.js'
|
|
9
8
|
import { clip, describeChange } from './classifier-input.js'
|
|
10
9
|
import {
|
|
@@ -21,9 +20,11 @@ import {
|
|
|
21
20
|
normalizeEditInput,
|
|
22
21
|
} from './edit-apply.js'
|
|
23
22
|
import { withFileMutationLock } from './file-mutation-lock.js'
|
|
23
|
+
import { decodeFileText, unreadableFileText } from './file-text.js'
|
|
24
24
|
import { jsonStringEscapes } from './json-string-hint.js'
|
|
25
25
|
import { lineDiff } from './line-diff.js'
|
|
26
26
|
import { notebookRefusal } from './notebook-guard.js'
|
|
27
|
+
import { readRegularFileBytes } from './read-regular-file.js'
|
|
27
28
|
import { staleBeforeReview } from './stale-before-review.js'
|
|
28
29
|
|
|
29
30
|
type RawEditInput = {
|
|
@@ -550,12 +551,17 @@ async function refuseEditBeforeReview(
|
|
|
550
551
|
// unread-file refusals are given when the call runs there, so a guest that is
|
|
551
552
|
// not admitted yet, or that holds a file the host has never seen, is not
|
|
552
553
|
// judged from a read made before anyone approved the call.
|
|
553
|
-
if (context.sandbox) return staleBeforeReview(context, parsed.data.path, 'edit')
|
|
554
554
|
let key: string
|
|
555
555
|
let content: string
|
|
556
556
|
try {
|
|
557
|
-
|
|
558
|
-
|
|
557
|
+
const allowed = await resolveAllowedFile(context, parsed.data.path)
|
|
558
|
+
if (context.sandbox && allowed === undefined)
|
|
559
|
+
return staleBeforeReview(context, parsed.data.path, 'edit')
|
|
560
|
+
key = allowed ?? (await resolveWithinAnyReal(toolRoots(context), parsed.data.path))
|
|
561
|
+
const bytes = await readRegularFileBytes(key, 'Edit')
|
|
562
|
+
const decoded = decodeFileText(bytes)
|
|
563
|
+
if (decoded === undefined) return { error: unreadableFileText(bytes.length) }
|
|
564
|
+
content = decoded
|
|
559
565
|
} catch {
|
|
560
566
|
return undefined
|
|
561
567
|
}
|
|
@@ -703,25 +709,31 @@ export const EditTool = defineTool({
|
|
|
703
709
|
}
|
|
704
710
|
}
|
|
705
711
|
|
|
712
|
+
const allowed = await resolveAllowedFile(context, parsed.data.path)
|
|
713
|
+
const sandbox = allowed === undefined ? context.sandbox : undefined
|
|
706
714
|
// Host-side containment, on the host branch only. The sandbox has its
|
|
707
715
|
// own root and its own resolver; canonicalizing a sandbox-relative
|
|
708
716
|
// path against the HOST filesystem asks a question about the wrong
|
|
709
717
|
// machine, and answers it with whatever happens to exist there.
|
|
710
|
-
const filePath =
|
|
711
|
-
|
|
712
|
-
: await resolveWithinAnyReal(toolRoots(context), parsed.data.path)
|
|
718
|
+
const filePath =
|
|
719
|
+
allowed ??
|
|
720
|
+
(sandbox ? undefined : await resolveWithinAnyReal(toolRoots(context), parsed.data.path))
|
|
713
721
|
// Read-modify-write is not atomic on its own: two edits to the same
|
|
714
722
|
// path interleave their reads, and the second write lands on content
|
|
715
723
|
// the first had already replaced — so one edit vanishes and the loser
|
|
716
724
|
// reports "old_string not found", blaming the model for a race. The
|
|
717
725
|
// key spans both branches because sandbox and local are distinct
|
|
718
726
|
// files even when the path string matches.
|
|
719
|
-
const lockKey =
|
|
727
|
+
const lockKey = sandbox ? `sandbox:${parsed.data.path}` : `local:${filePath as string}`
|
|
720
728
|
|
|
721
729
|
return withFileMutationLock(lockKey, async () => {
|
|
722
|
-
if (
|
|
723
|
-
|
|
724
|
-
const
|
|
730
|
+
if (sandbox) {
|
|
731
|
+
refuseSandboxHomePath(parsed.data.path)
|
|
732
|
+
const buffer = await sandbox.readFile(parsed.data.path)
|
|
733
|
+
const content = decodeFileText(buffer)
|
|
734
|
+
if (content === undefined) {
|
|
735
|
+
return { success: false as const, output: '', error: unreadableFileText(buffer.length) }
|
|
736
|
+
}
|
|
725
737
|
const admitted = admitAgainstRead(context, parsed.data.path, content, normalized.operations)
|
|
726
738
|
if (!admitted.success) return admitted
|
|
727
739
|
const result = admitted.result
|
|
@@ -729,7 +741,7 @@ export const EditTool = defineTool({
|
|
|
729
741
|
const notebook = notebookRefusal(parsed.data.path, result.content, content)
|
|
730
742
|
if (notebook) return { success: false as const, output: '', error: notebook }
|
|
731
743
|
|
|
732
|
-
await
|
|
744
|
+
await sandbox.writeFile(parsed.data.path, result.content)
|
|
733
745
|
recordEditedContent(context, parsed.data.path, result.content)
|
|
734
746
|
const changed = editFacts(parsed.data.path, result.replacements, content, result.content)
|
|
735
747
|
return {
|
|
@@ -745,7 +757,11 @@ export const EditTool = defineTool({
|
|
|
745
757
|
}
|
|
746
758
|
|
|
747
759
|
const hostPath = filePath as string
|
|
748
|
-
const
|
|
760
|
+
const buffer = await readRegularFileBytes(hostPath, 'Edit')
|
|
761
|
+
const content = decodeFileText(buffer)
|
|
762
|
+
if (content === undefined) {
|
|
763
|
+
return { success: false as const, output: '', error: unreadableFileText(buffer.length) }
|
|
764
|
+
}
|
|
749
765
|
|
|
750
766
|
const admitted = admitAgainstRead(context, hostPath, content, normalized.operations)
|
|
751
767
|
if (!admitted.success) return admitted
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { humanBytes } from './read-render.js'
|
|
2
|
+
|
|
3
|
+
// Keep a leading BOM in the body so read-modify-write tools can round-trip it.
|
|
4
|
+
export function decodeFileText(bytes: Buffer): string | undefined {
|
|
5
|
+
try {
|
|
6
|
+
return new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(bytes)
|
|
7
|
+
} catch {
|
|
8
|
+
return undefined
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function unreadableFileText(bytes: number): string {
|
|
13
|
+
return `This file is not readable as text (${humanBytes(bytes)} of binary or non-UTF-8 data). Inspect it with a shell command such as file or xxd, or use a tool made for its format.`
|
|
14
|
+
}
|
|
@@ -5,7 +5,7 @@ import { walkFilesLocally } from '../../sandbox/file-walk.js'
|
|
|
5
5
|
import type { SandboxWalkFilesOptions, SandboxWalkSummary } from '../../types/sandbox/index.js'
|
|
6
6
|
import { defineTool } from '../defineTool.js'
|
|
7
7
|
import { resolveWithin, resolveWithinAny, resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
8
|
-
import { relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
8
|
+
import { refuseSandboxHomePath, relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
9
9
|
import {
|
|
10
10
|
SEARCH_BUDGET_MS,
|
|
11
11
|
SEARCH_TOOL_TIMEOUT_MS,
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
ignoredHint,
|
|
14
14
|
isEntryLimit,
|
|
15
15
|
isReadDenied,
|
|
16
|
+
missingSearchRoot,
|
|
16
17
|
openSearchBudget,
|
|
17
18
|
plural,
|
|
18
19
|
quoteControlPath,
|
|
@@ -106,10 +107,14 @@ export const GlobTool = defineTool({
|
|
|
106
107
|
async execute(input, context) {
|
|
107
108
|
context.abortSignal?.throwIfAborted()
|
|
108
109
|
const sandbox = context.sandbox
|
|
110
|
+
if (sandbox) refuseSandboxHomePath(input.path)
|
|
109
111
|
const lexicalRoot = sandbox
|
|
110
112
|
? resolveWithinPosix(sandbox.rootDir, input.path)
|
|
111
113
|
: resolveWithinAny(toolRoots(context), input.path)
|
|
112
114
|
const root = sandbox ? lexicalRoot : await resolveWithinAnyReal(toolRoots(context), input.path)
|
|
115
|
+
if (!sandbox && (await missingSearchRoot(root))) {
|
|
116
|
+
return { success: false, output: '', error: `search path does not exist: ${lexicalRoot}` }
|
|
117
|
+
}
|
|
113
118
|
const pattern = relativePattern(input.pattern, lexicalRoot, sandbox !== undefined)
|
|
114
119
|
const walkSandbox = sandbox?.walkFiles?.bind(sandbox)
|
|
115
120
|
if (sandbox && !walkSandbox) {
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
resolveWithinReal,
|
|
15
15
|
toolRoots,
|
|
16
16
|
} from '../paths.js'
|
|
17
|
-
import { relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
17
|
+
import { refuseSandboxHomePath, relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
18
18
|
import {
|
|
19
19
|
CAN_REFUSE_LINKS,
|
|
20
20
|
REFUSE_LINK_FLAG,
|
|
@@ -30,6 +30,7 @@ import {
|
|
|
30
30
|
ignoredHint,
|
|
31
31
|
isEntryLimit,
|
|
32
32
|
isReadDenied,
|
|
33
|
+
missingSearchRoot,
|
|
33
34
|
openSearchBudget,
|
|
34
35
|
plural,
|
|
35
36
|
readAhead,
|
|
@@ -388,10 +389,14 @@ export const GrepTool = defineTool({
|
|
|
388
389
|
}
|
|
389
390
|
|
|
390
391
|
const sandbox = context.sandbox
|
|
392
|
+
if (sandbox) refuseSandboxHomePath(input.path)
|
|
391
393
|
const lexicalRoot = sandbox
|
|
392
394
|
? resolveWithinPosix(sandbox.rootDir, input.path)
|
|
393
395
|
: resolveWithinAny(toolRoots(context), input.path)
|
|
394
396
|
const root = sandbox ? lexicalRoot : await resolveWithinAnyReal(toolRoots(context), input.path)
|
|
397
|
+
if (!sandbox && (await missingSearchRoot(root))) {
|
|
398
|
+
return { success: false, output: '', error: `search path does not exist: ${lexicalRoot}` }
|
|
399
|
+
}
|
|
395
400
|
const filePattern = relativeInclude(input.include ?? '**/*', lexicalRoot, sandbox !== undefined)
|
|
396
401
|
const walkSandbox = sandbox?.walkFiles?.bind(sandbox)
|
|
397
402
|
if (sandbox && !walkSandbox) {
|
|
@@ -830,10 +835,7 @@ function multilineHits(content: string, regex: RegExp): [number, number][] {
|
|
|
830
835
|
regex.lastIndex = 0
|
|
831
836
|
for (let m = regex.exec(content); m !== null; m = regex.exec(content)) {
|
|
832
837
|
const text = m[0]
|
|
833
|
-
if (text.length === 0)
|
|
834
|
-
regex.lastIndex++
|
|
835
|
-
continue
|
|
836
|
-
}
|
|
838
|
+
if (text.length === 0) regex.lastIndex++
|
|
837
839
|
// A match that ends on a newline covers the lines before it, not the next one.
|
|
838
840
|
const end = m.index + text.length - (text.endsWith('\n') ? 1 : 0)
|
|
839
841
|
hits.push([lineOf(m.index), lineOf(Math.max(m.index, end))])
|
package/src/tools/builtins/ls.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { Sandbox } from '../../types/sandbox/index.js'
|
|
|
5
5
|
import type { ToolResult } from '../../types/tool/index.js'
|
|
6
6
|
import { defineTool } from '../defineTool.js'
|
|
7
7
|
import { resolveWithinAny, toolRoots } from '../paths.js'
|
|
8
|
-
import { relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
8
|
+
import { refuseSandboxHomePath, relativePosix, resolveWithinPosix } from '../posix-path.js'
|
|
9
9
|
|
|
10
10
|
const inputSchema = z.object({
|
|
11
11
|
path: z.string().default('.').describe('Directory path to list. Defaults to working directory.'),
|
|
@@ -134,6 +134,7 @@ async function listInSandbox(
|
|
|
134
134
|
input: { path: string; all: boolean; recursive: boolean; max_depth: number },
|
|
135
135
|
sandbox: Sandbox,
|
|
136
136
|
): Promise<ToolResult> {
|
|
137
|
+
refuseSandboxHomePath(input.path)
|
|
137
138
|
const root = resolveWithinPosix(sandbox.rootDir, input.path)
|
|
138
139
|
const entries = await sandbox.listFiles(root)
|
|
139
140
|
|
|
@@ -4,10 +4,12 @@ import type { ToolContext, ToolResult } from '../../types/tool/index.js'
|
|
|
4
4
|
import type { ToolFacts } from '../../types/tool/presentation.js'
|
|
5
5
|
import { defineTool } from '../defineTool.js'
|
|
6
6
|
import { resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
7
|
+
import { refuseSandboxHomePath } from '../posix-path.js'
|
|
7
8
|
import { atomicWriteFile } from './atomic-write-file.js'
|
|
8
9
|
import { clip } from './classifier-input.js'
|
|
9
10
|
import { CURRENT_ON_DISK, movedSinceRead, staleFileError } from './content-fingerprint.js'
|
|
10
11
|
import { withFileMutationLock } from './file-mutation-lock.js'
|
|
12
|
+
import { decodeFileText, unreadableFileText } from './file-text.js'
|
|
11
13
|
import { lineDiff } from './line-diff.js'
|
|
12
14
|
import { applyNotebookEdit } from './notebook-cells.js'
|
|
13
15
|
import { isNotebookPath, notebookRefusal } from './notebook-guard.js'
|
|
@@ -117,9 +119,9 @@ function record(context: ToolContext, key: string, content: string): void {
|
|
|
117
119
|
tracker.recordRead(key, content)
|
|
118
120
|
}
|
|
119
121
|
|
|
120
|
-
async function readHost(path: string): Promise<
|
|
122
|
+
async function readHost(path: string): Promise<Buffer | undefined> {
|
|
121
123
|
try {
|
|
122
|
-
return await readFile(path
|
|
124
|
+
return await readFile(path)
|
|
123
125
|
} catch (error) {
|
|
124
126
|
if ((error as { code?: unknown } | null)?.code === 'ENOENT') return undefined
|
|
125
127
|
throw error
|
|
@@ -141,14 +143,16 @@ async function refuseBeforeReview(
|
|
|
141
143
|
if (!isNotebookPath(path)) return { error: NOT_A_NOTEBOOK }
|
|
142
144
|
if (context.sandbox) return undefined
|
|
143
145
|
let key: string
|
|
144
|
-
let
|
|
146
|
+
let bytes: Buffer | undefined
|
|
145
147
|
try {
|
|
146
148
|
key = await resolveWithinAnyReal(toolRoots(context), path)
|
|
147
|
-
|
|
149
|
+
bytes = await readHost(key)
|
|
148
150
|
} catch {
|
|
149
151
|
return undefined
|
|
150
152
|
}
|
|
151
|
-
if (
|
|
153
|
+
if (bytes === undefined) return undefined
|
|
154
|
+
const current = decodeFileText(bytes)
|
|
155
|
+
if (current === undefined) return { error: unreadableFileText(bytes.length) }
|
|
152
156
|
const problem = freshnessProblem(context, key, current)
|
|
153
157
|
return problem
|
|
154
158
|
? { error: problem.error, ...(problem.facts ? { facts: problem.facts } : {}) }
|
|
@@ -213,22 +217,25 @@ export const NotebookEditTool = defineTool({
|
|
|
213
217
|
const lockKey = context.sandbox ? `sandbox:${key}` : `local:${key}`
|
|
214
218
|
|
|
215
219
|
return withFileMutationLock(lockKey, async () => {
|
|
216
|
-
let
|
|
220
|
+
let bytes: Buffer | undefined
|
|
217
221
|
if (context.sandbox) {
|
|
222
|
+
refuseSandboxHomePath(key)
|
|
218
223
|
try {
|
|
219
|
-
|
|
224
|
+
bytes = await context.sandbox.readFile(key)
|
|
220
225
|
} catch {
|
|
221
|
-
|
|
226
|
+
bytes = undefined
|
|
222
227
|
}
|
|
223
228
|
} else {
|
|
224
|
-
|
|
229
|
+
bytes = await readHost(key)
|
|
225
230
|
}
|
|
226
|
-
if (
|
|
227
|
-
if (
|
|
231
|
+
if (bytes === undefined) return failure(`Notebook file does not exist: ${key}.`)
|
|
232
|
+
if (bytes.length > NOTEBOOK_MAX_BYTES) {
|
|
228
233
|
return failure(
|
|
229
234
|
`${key} is larger than this tool reads. Change it with \`edit\` or a shell command. The file was not changed.`,
|
|
230
235
|
)
|
|
231
236
|
}
|
|
237
|
+
const before = decodeFileText(bytes)
|
|
238
|
+
if (before === undefined) return failure(unreadableFileText(bytes.length))
|
|
232
239
|
const stale = freshnessProblem(context, key, before)
|
|
233
240
|
if (stale) return stale
|
|
234
241
|
|